mxnet
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
resource.h
Go to the documentation of this file.
1 
6 #ifndef MXNET_RESOURCE_H_
7 #define MXNET_RESOURCE_H_
8 
9 #include <dmlc/logging.h>
10 #include "./base.h"
11 #include "./engine.h"
12 
13 namespace mxnet {
14 
20  enum Type {
25  };
34  ResourceRequest(Type type) // NOLINT(*)
35  : type(type) {}
36 };
37 
38 
44 struct Resource {
50  int32_t id;
55  void *ptr_;
57  Resource() : id(0) {}
64  template<typename xpu, typename DType>
65  inline mshadow::Random<xpu, DType>* get_random(
66  mshadow::Stream<xpu> *stream) const {
68  mshadow::Random<xpu, DType> *ret =
69  static_cast<mshadow::Random<xpu, DType>*>(ptr_);
70  ret->set_stream(stream);
71  return ret;
72  }
83  template<typename xpu, int ndim>
84  inline mshadow::Tensor<xpu, ndim, real_t> get_space(
85  mshadow::Shape<ndim> shape, mshadow::Stream<xpu> *stream) const {
86  return get_space_typed<xpu, ndim, real_t>(shape, stream);
87  }
96  template<int ndim>
97  inline mshadow::Tensor<cpu, ndim, real_t> get_host_space(
98  mshadow::Shape<ndim> shape) const {
99  return get_host_space_typed<cpu, ndim, real_t>(shape);
100  }
111  template<typename xpu, int ndim, typename DType>
112  inline mshadow::Tensor<xpu, ndim, DType> get_space_typed(
113  mshadow::Shape<ndim> shape, mshadow::Stream<xpu> *stream) const {
115  return mshadow::Tensor<xpu, ndim, DType>(
116  reinterpret_cast<DType*>(get_space_internal(shape.Size() * sizeof(DType))),
117  shape, shape[ndim - 1], stream);
118  }
128  template<int ndim, typename DType>
129  inline mshadow::Tensor<cpu, ndim, DType> get_host_space_typed(
130  mshadow::Shape<ndim> shape) const {
131  return mshadow::Tensor<cpu, ndim, DType>(
132  reinterpret_cast<DType*>(get_host_space_internal(shape.Size() * sizeof(DType))),
133  shape, shape[ndim - 1], NULL);
134  }
140  void* get_space_internal(size_t size) const;
146  void *get_host_space_internal(size_t size) const;
147 };
148 
151  public:
160  virtual Resource Request(Context ctx, const ResourceRequest &req) = 0;
165  virtual void SeedRandom(uint32_t seed) = 0;
167  virtual ~ResourceManager() DMLC_THROW_EXCEPTION {}
171  static ResourceManager *Get();
172 };
173 } // namespace mxnet
174 #endif // MXNET_RESOURCE_H_
Engine that schedules all the operations according to dependency.
virtual Resource Request(Context ctx, const ResourceRequest &req)=0
Get resource of requested type.
engine::VarHandle var
engine variable
Definition: resource.h:48
int32_t id
identifier of id information, used for debug purpose
Definition: resource.h:50
The resources that can be requested by Operator.
Definition: resource.h:18
ResourceRequest(Type type)
constructor, allow implicit conversion
Definition: resource.h:34
ResourceRequest req
The original request.
Definition: resource.h:46
Type type
type of resources
Definition: resource.h:27
A dynamic temp space that can be arbitrary size.
Definition: resource.h:24
mshadow::Tensor< cpu, ndim, DType > get_host_space_typed(mshadow::Shape< ndim > shape) const
Get CPU space as mshadow Tensor in specified type. The caller can request arbitrary size...
Definition: resource.h:129
Global resource manager.
Definition: resource.h:150
Resources used by mxnet operations. A resource is something special other than NDArray, but will still participate.
Definition: resource.h:44
mshadow::Random< xpu, DType > * get_random(mshadow::Stream< xpu > *stream) const
Get random number generator.
Definition: resource.h:65
mshadow::Tensor< xpu, ndim, DType > get_space_typed(mshadow::Shape< ndim > shape, mshadow::Stream< xpu > *stream) const
Get space requested as mshadow Tensor in specified type. The caller can request arbitrary size...
Definition: resource.h:112
mshadow::Tensor< xpu, ndim, real_t > get_space(mshadow::Shape< ndim > shape, mshadow::Stream< xpu > *stream) const
Get space requested as mshadow Tensor. The caller can request arbitrary size.
Definition: resource.h:84
ResourceRequest()
default constructor
Definition: resource.h:29
virtual ~ResourceManager() DMLC_THROW_EXCEPTION
virtual destructor
Definition: resource.h:167
Type
Resource type, indicating what the pointer type is.
Definition: resource.h:20
Resource()
default constructor
Definition: resource.h:57
Var * VarHandle
Variable pointer type, usually hold by user used to specify dependencies.
Definition: engine.h:27
configuation of mxnet as well as basic data structure.
mshadow::Tensor< cpu, ndim, real_t > get_host_space(mshadow::Shape< ndim > shape) const
Get cpu space requested as mshadow Tensor. The caller can request arbitrary size. ...
Definition: resource.h:97
mshadow::Random<xpu> object
Definition: resource.h:22
void * ptr_
pointer to the resource, do not use directly, access using member functions
Definition: resource.h:55
void * get_space_internal(size_t size) const
internal function to get space from resources.
virtual void SeedRandom(uint32_t seed)=0
Seed all the allocated random numbers.
void * get_host_space_internal(size_t size) const
internal function to get cpu space from resources.
static ResourceManager * Get()
Context information about the execution enviroment.
Definition: base.h:90