mxnet
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 #include "../../src/common/random_generator.h"
32 
33 namespace mxnet {
34 
40  enum Type {
47  };
56  ResourceRequest(Type type) // NOLINT(*)
57  : type(type) {}
58 };
59 
60 
66 struct Resource {
72  int32_t id;
77  void *ptr_;
79  Resource() : id(0) {}
86  template<typename xpu, typename DType>
87  inline mshadow::Random<xpu, DType>* get_random(
88  mshadow::Stream<xpu> *stream) const {
89  CHECK_EQ(req.type, ResourceRequest::kRandom);
90  mshadow::Random<xpu, DType> *ret =
91  static_cast<mshadow::Random<xpu, DType>*>(ptr_);
92  ret->set_stream(stream);
93  return ret;
94  }
95 
102  template<typename xpu, typename DType>
104  CHECK_EQ(req.type, ResourceRequest::kParallelRandom);
105  return static_cast<common::random::RandGenerator<xpu, DType>*>(ptr_);
106  }
107 
124  template<typename xpu, int ndim>
125  inline mshadow::Tensor<xpu, ndim, real_t> get_space(
126  mshadow::Shape<ndim> shape, mshadow::Stream<xpu> *stream) const {
127  return get_space_typed<xpu, ndim, real_t>(shape, stream);
128  }
137  template<int ndim>
138  inline mshadow::Tensor<cpu, ndim, real_t> get_host_space(
139  mshadow::Shape<ndim> shape) const {
140  return get_host_space_typed<cpu, ndim, real_t>(shape);
141  }
152  template<typename xpu, int ndim, typename DType>
153  inline mshadow::Tensor<xpu, ndim, DType> get_space_typed(
154  mshadow::Shape<ndim> shape, mshadow::Stream<xpu> *stream) const {
155  CHECK_EQ(req.type, ResourceRequest::kTempSpace);
156  return mshadow::Tensor<xpu, ndim, DType>(
157  reinterpret_cast<DType*>(get_space_internal(shape.Size() * sizeof(DType))),
158  shape, shape[ndim - 1], stream);
159  }
169  template<int ndim, typename DType>
170  inline mshadow::Tensor<cpu, ndim, DType> get_host_space_typed(
171  mshadow::Shape<ndim> shape) const {
172  return mshadow::Tensor<cpu, ndim, DType>(
173  reinterpret_cast<DType*>(get_host_space_internal(shape.Size() * sizeof(DType))),
174  shape, shape[ndim - 1], NULL);
175  }
181  void* get_space_internal(size_t size) const;
187  void *get_host_space_internal(size_t size) const;
188 };
189 
192  public:
201  virtual Resource Request(Context ctx, const ResourceRequest &req) = 0;
206  virtual void SeedRandom(uint32_t seed) = 0;
211  virtual void SeedRandom(Context ctx, uint32_t seed) = 0;
213  virtual ~ResourceManager() DMLC_THROW_EXCEPTION {}
217  static ResourceManager *Get();
218 };
219 } // namespace mxnet
220 #endif // MXNET_RESOURCE_H_
Engine that schedules all the operations according to dependency.
engine::VarHandle var
engine variable
Definition: resource.h:70
namespace of mxnet
Definition: base.h:118
int32_t id
identifier of id information, used for debug purpose
Definition: resource.h:72
The resources that can be requested by Operator.
Definition: resource.h:38
ResourceRequest(Type type)
constructor, allow implicit conversion
Definition: resource.h:56
ResourceRequest req
The original request.
Definition: resource.h:68
Type type
type of resources
Definition: resource.h:49
A dynamic temp space that can be arbitrary size.
Definition: resource.h:44
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:170
Global resource manager.
Definition: resource.h:191
Resources used by mxnet operations. A resource is something special other than NDArray, but will still participate.
Definition: resource.h:66
mshadow::Random< xpu, DType > * get_random(mshadow::Stream< xpu > *stream) const
Get random number generator.
Definition: resource.h:87
common::RandGenerator<xpu> object, which can be used in GPU kernel functions
Definition: resource.h:46
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:153
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:125
ResourceRequest()
default constructor
Definition: resource.h:51
virtual ~ResourceManager() DMLC_THROW_EXCEPTION
virtual destructor
Definition: resource.h:213
Type
Resource type, indicating what the pointer type is.
Definition: resource.h:40
Resource()
default constructor
Definition: resource.h:79
Var * VarHandle
Variable pointer type, usually hold by user used to specify dependencies.
Definition: engine.h:47
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:138
mshadow::Random<xpu> object
Definition: resource.h:42
Definition: random_generator.h:42
void * ptr_
pointer to the resource, do not use directly, access using member functions
Definition: resource.h:77
Context information about the execution environment.
Definition: base.h:133
common::random::RandGenerator< xpu, DType > * get_parallel_random() const
Get parallel random number generator.
Definition: resource.h:103