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 
24 #ifndef MXNET_RESOURCE_H_
25 #define MXNET_RESOURCE_H_
26 
27 #include <dmlc/logging.h>
28 #include "./base.h"
29 #include "./engine.h"
30 #include "./random_generator.h"
31 
32 namespace mxnet {
33 
39  enum Type {
46 #if MXNET_USE_CUDNN == 1
47  ,
49  kCuDNNDropoutDesc
50 #endif // MXNET_USE_CUDNN == 1
51  };
60  ResourceRequest(Type type) // NOLINT(*)
61  : type(type) {}
62 };
63 
64 
70 struct Resource {
76  int32_t id;
81  void *ptr_;
83  Resource() : id(0) {}
90  template<typename xpu, typename DType>
92  mshadow::Stream<xpu> *stream) const {
93  CHECK_EQ(req.type, ResourceRequest::kRandom);
95  static_cast<mshadow::Random<xpu, DType>*>(ptr_);
96  ret->set_stream(stream);
97  return ret;
98  }
99 
106  template<typename xpu, typename DType>
108  CHECK_EQ(req.type, ResourceRequest::kParallelRandom);
109  return static_cast<common::random::RandGenerator<xpu, DType>*>(ptr_);
110  }
111 
128  template<typename xpu, int ndim>
130  mshadow::Shape<ndim> shape, mshadow::Stream<xpu> *stream) const {
131  return get_space_typed<xpu, ndim, real_t>(shape, stream);
132  }
141  template<int ndim>
143  mshadow::Shape<ndim> shape) const {
144  return get_host_space_typed<cpu, ndim, real_t>(shape);
145  }
156  template<typename xpu, int ndim, typename DType>
158  mshadow::Shape<ndim> shape, mshadow::Stream<xpu> *stream) const {
159  CHECK_EQ(req.type, ResourceRequest::kTempSpace);
161  reinterpret_cast<DType*>(get_space_internal(shape.Size() * sizeof(DType))),
162  shape, shape[ndim - 1], stream);
163  }
164 #if MXNET_USE_CUDNN == 1
165 
172  void get_cudnn_dropout_desc(
173  cudnnDropoutDescriptor_t* dropout_desc,
174  mshadow::Stream<gpu> *stream,
175  const float dropout,
176  uint64_t seed) const;
177 #endif // MXNET_USE_CUDNN == 1
178 
188  template<int ndim, typename DType>
190  mshadow::Shape<ndim> shape) const {
192  reinterpret_cast<DType*>(get_host_space_internal(shape.Size() * sizeof(DType))),
193  shape, shape[ndim - 1], nullptr);
194  }
200  void* get_space_internal(size_t size) const;
206  void *get_host_space_internal(size_t size) const;
207 };
208 
211  public:
220  virtual Resource Request(Context ctx, const ResourceRequest &req) = 0;
225  virtual void SeedRandom(uint32_t seed) = 0;
230  virtual void SeedRandom(Context ctx, uint32_t seed) = 0;
236  static ResourceManager *Get();
237 };
238 } // namespace mxnet
239 #endif // MXNET_RESOURCE_H_
random number generator
Definition: random.h:52
Engine that schedules all the operations according to dependency.
engine::VarHandle var
engine variable
Definition: resource.h:74
namespace of mxnet
Definition: api_registry.h:33
shape of a tensor
Definition: tensor.h:53
common::random::RandGenerator< xpu, DType > * get_parallel_random() const
Get parallel random number generator.
Definition: resource.h:107
Definition: stream_gpu-inl.h:37
#define DMLC_THROW_EXCEPTION
Definition: base.h:233
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:129
int32_t id
identifier of id information, used for debug purpose
Definition: resource.h:76
The resources that can be requested by Operator.
Definition: resource.h:37
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:157
ResourceRequest(Type type)
constructor, allow implicit conversion
Definition: resource.h:60
Parallel random number generator.
mshadow::Random< xpu, DType > * get_random(mshadow::Stream< xpu > *stream) const
Get random number generator.
Definition: resource.h:91
ResourceRequest req
The original request.
Definition: resource.h:72
Type type
type of resources
Definition: resource.h:53
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:189
A dynamic temp space that can be arbitrary size.
Definition: resource.h:43
base class of engine variables.
Definition: engine.h:43
Global resource manager.
Definition: resource.h:210
Resources used by mxnet operations. A resource is something special other than NDArray, but will still participate.
Definition: resource.h:70
common::RandGenerator<xpu> object, which can be used in GPU kernel functions
Definition: resource.h:45
ResourceRequest()
default constructor
Definition: resource.h:55
virtual ~ResourceManager() DMLC_THROW_EXCEPTION
virtual destructor
Definition: resource.h:232
Type
Resource type, indicating what the pointer type is.
Definition: resource.h:39
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:142
Resource()
default constructor
Definition: resource.h:83
mshadow::Random<xpu> object
Definition: resource.h:41
Definition: random_generator.h:41
void * ptr_
pointer to the resource, do not use directly, access using member functions
Definition: resource.h:81
Context information about the execution environment.
Definition: base.h:101
general tensor
Definition: tensor.h:420
MSHADOW_XINLINE index_t Size(void) const
Definition: tensor.h:144
computaion stream structure, used for asynchronous computations
Definition: tensor.h:383