mxnet
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
resource.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
25 #ifndef MXNET_RESOURCE_H_
26 #define MXNET_RESOURCE_H_
27 
28 #include <dmlc/logging.h>
29 #include "./base.h"
30 #include "./engine.h"
31 
32 namespace mxnet {
33 
39  enum Type {
44  };
53  ResourceRequest(Type type) // NOLINT(*)
54  : type(type) {}
55 };
56 
57 
63 struct Resource {
69  int32_t id;
74  void *ptr_;
76  Resource() : id(0) {}
83  template<typename xpu, typename DType>
84  inline mshadow::Random<xpu, DType>* get_random(
85  mshadow::Stream<xpu> *stream) const {
87  mshadow::Random<xpu, DType> *ret =
88  static_cast<mshadow::Random<xpu, DType>*>(ptr_);
89  ret->set_stream(stream);
90  return ret;
91  }
108  template<typename xpu, int ndim>
109  inline mshadow::Tensor<xpu, ndim, real_t> get_space(
110  mshadow::Shape<ndim> shape, mshadow::Stream<xpu> *stream) const {
111  return get_space_typed<xpu, ndim, real_t>(shape, stream);
112  }
121  template<int ndim>
122  inline mshadow::Tensor<cpu, ndim, real_t> get_host_space(
123  mshadow::Shape<ndim> shape) const {
124  return get_host_space_typed<cpu, ndim, real_t>(shape);
125  }
136  template<typename xpu, int ndim, typename DType>
137  inline mshadow::Tensor<xpu, ndim, DType> get_space_typed(
138  mshadow::Shape<ndim> shape, mshadow::Stream<xpu> *stream) const {
140  return mshadow::Tensor<xpu, ndim, DType>(
141  reinterpret_cast<DType*>(get_space_internal(shape.Size() * sizeof(DType))),
142  shape, shape[ndim - 1], stream);
143  }
153  template<int ndim, typename DType>
154  inline mshadow::Tensor<cpu, ndim, DType> get_host_space_typed(
155  mshadow::Shape<ndim> shape) const {
156  return mshadow::Tensor<cpu, ndim, DType>(
157  reinterpret_cast<DType*>(get_host_space_internal(shape.Size() * sizeof(DType))),
158  shape, shape[ndim - 1], NULL);
159  }
165  void* get_space_internal(size_t size) const;
171  void *get_host_space_internal(size_t size) const;
172 };
173 
176  public:
185  virtual Resource Request(Context ctx, const ResourceRequest &req) = 0;
190  virtual void SeedRandom(uint32_t seed) = 0;
192  virtual ~ResourceManager() DMLC_THROW_EXCEPTION {}
196  static ResourceManager *Get();
197 };
198 } // namespace mxnet
199 #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:67
int32_t id
identifier of id information, used for debug purpose
Definition: resource.h:69
The resources that can be requested by Operator.
Definition: resource.h:37
ResourceRequest(Type type)
constructor, allow implicit conversion
Definition: resource.h:53
ResourceRequest req
The original request.
Definition: resource.h:65
Type type
type of resources
Definition: resource.h:46
A dynamic temp space that can be arbitrary size.
Definition: resource.h:43
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:154
Global resource manager.
Definition: resource.h:175
Resources used by mxnet operations. A resource is something special other than NDArray, but will still participate.
Definition: resource.h:63
mshadow::Random< xpu, DType > * get_random(mshadow::Stream< xpu > *stream) const
Get random number generator.
Definition: resource.h:84
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:137
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:109
ResourceRequest()
default constructor
Definition: resource.h:48
virtual ~ResourceManager() DMLC_THROW_EXCEPTION
virtual destructor
Definition: resource.h:192
Type
Resource type, indicating what the pointer type is.
Definition: resource.h:39
Resource()
default constructor
Definition: resource.h:76
Var * VarHandle
Variable pointer type, usually hold by user used to specify dependencies.
Definition: engine.h:47
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:122
mshadow::Random<xpu> object
Definition: resource.h:41
void * ptr_
pointer to the resource, do not use directly, access using member functions
Definition: resource.h:74
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 environment.
Definition: base.h:142