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 "./random_generator.h"
32 
33 namespace mxnet {
34 
40  enum Type {
47 #if MXNET_USE_CUDNN == 1
48  ,
50  kCuDNNDropoutDesc
51 #endif // MXNET_USE_CUDNN == 1
52  };
61  ResourceRequest(Type type) // NOLINT(*)
62  : type(type) {}
63 };
64 
65 
71 struct Resource {
77  int32_t id;
82  void *ptr_;
84  Resource() : id(0) {}
91  template<typename xpu, typename DType>
93  mshadow::Stream<xpu> *stream) const {
94  CHECK_EQ(req.type, ResourceRequest::kRandom);
96  static_cast<mshadow::Random<xpu, DType>*>(ptr_);
97  ret->set_stream(stream);
98  return ret;
99  }
100 
107  template<typename xpu, typename DType>
109  CHECK_EQ(req.type, ResourceRequest::kParallelRandom);
110  return static_cast<common::random::RandGenerator<xpu, DType>*>(ptr_);
111  }
112 
129  template<typename xpu, int ndim>
131  mshadow::Shape<ndim> shape, mshadow::Stream<xpu> *stream) const {
132  return get_space_typed<xpu, ndim, real_t>(shape, stream);
133  }
142  template<int ndim>
144  mshadow::Shape<ndim> shape) const {
145  return get_host_space_typed<cpu, ndim, real_t>(shape);
146  }
157  template<typename xpu, int ndim, typename DType>
159  mshadow::Shape<ndim> shape, mshadow::Stream<xpu> *stream) const {
160  CHECK_EQ(req.type, ResourceRequest::kTempSpace);
162  reinterpret_cast<DType*>(get_space_internal(shape.Size() * sizeof(DType))),
163  shape, shape[ndim - 1], stream);
164  }
165 #if MXNET_USE_CUDNN == 1
166 
173  void get_cudnn_dropout_desc(
174  cudnnDropoutDescriptor_t* dropout_desc,
175  mshadow::Stream<gpu> *stream,
176  const float dropout,
177  uint64_t seed) const;
178 #endif // MXNET_USE_CUDNN == 1
179 
189  template<int ndim, typename DType>
191  mshadow::Shape<ndim> shape) const {
193  reinterpret_cast<DType*>(get_host_space_internal(shape.Size() * sizeof(DType))),
194  shape, shape[ndim - 1], nullptr);
195  }
201  void* get_space_internal(size_t size) const;
207  void *get_host_space_internal(size_t size) const;
208 };
209 
212  public:
221  virtual Resource Request(Context ctx, const ResourceRequest &req) = 0;
226  virtual void SeedRandom(uint32_t seed) = 0;
231  virtual void SeedRandom(Context ctx, uint32_t seed) = 0;
237  static ResourceManager *Get();
238 };
239 } // namespace mxnet
240 #endif // MXNET_RESOURCE_H_
random number generator
Definition: random.h:53
MSHADOW_XINLINE index_t Size(void) const
Definition: tensor.h:145
Engine that schedules all the operations according to dependency.
engine::VarHandle var
engine variable
Definition: resource.h:75
namespace of mxnet
Definition: api_registry.h:33
shape of a tensor
Definition: tensor.h:54
Definition: stream_gpu-inl.h:38
#define DMLC_THROW_EXCEPTION
Definition: base.h:224
int32_t id
identifier of id information, used for debug purpose
Definition: resource.h:77
The resources that can be requested by Operator.
Definition: resource.h:38
ResourceRequest(Type type)
constructor, allow implicit conversion
Definition: resource.h:61
Parallel random number generator.
ResourceRequest req
The original request.
Definition: resource.h:73
Type type
type of resources
Definition: resource.h:54
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:190
base class of engine variables.
Definition: engine.h:44
Global resource manager.
Definition: resource.h:211
Resources used by mxnet operations. A resource is something special other than NDArray, but will still participate.
Definition: resource.h:71
mshadow::Random< xpu, DType > * get_random(mshadow::Stream< xpu > *stream) const
Get random number generator.
Definition: resource.h:92
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:158
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:130
ResourceRequest()
default constructor
Definition: resource.h:56
virtual ~ResourceManager() DMLC_THROW_EXCEPTION
virtual destructor
Definition: resource.h:233
Type
Resource type, indicating what the pointer type is.
Definition: resource.h:40
Resource()
default constructor
Definition: resource.h:84
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:143
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:82
Context information about the execution environment.
Definition: base.h:102
general tensor
Definition: tensor.h:421
common::random::RandGenerator< xpu, DType > * get_parallel_random() const
Get parallel random number generator.
Definition: resource.h:108
computaion stream structure, used for asynchronous computations
Definition: tensor.h:384