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 <string>
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 namespace {
67 inline std::string __extract_fname(const std::string& path) {
68  std::size_t last_dir_pos = path.find_last_of("/\\");
69  if (last_dir_pos == std::string::npos) {
70  return path;
71  }
72  return path.substr(last_dir_pos + 1);
73 }
74 } // anonymous namespace
75 
76 #if (defined(__GNUC__) || defined(__GNUG__)) && !defined(__clang__)
77 #define MXNET_RESOURCE_DEFAULT_NAME_FARG(tag) \
78  std::string(tag) + " (" + __extract_fname(__builtin_FILE()) + " +" + \
79  std::to_string(__builtin_LINE()) + ")"
80 #else // !__GNUC__ || __clang__
81 #define MXNET_RESOURCE_DEFAULT_NAME_FARG(tag) \
82  std::string(tag) + " (" + __extract_fname(__FILE__) + " +" + std::to_string(__LINE__) + ")"
83 #endif // __GNUC__ && !__clang__
84 
90 struct Resource {
96  int32_t id;
101  void* ptr_;
103  Resource() : id(0) {}
110  template <typename xpu, typename DType>
112  CHECK_EQ(req.type, ResourceRequest::kRandom);
114  ret->set_stream(stream);
115  return ret;
116  }
117 
124  template <typename xpu, typename DType>
127  return static_cast<common::random::RandGenerator<xpu, DType>*>(ptr_);
128  }
129 
147  template <typename xpu, int ndim>
149  mshadow::Shape<ndim> shape,
150  mshadow::Stream<xpu>* stream,
151  const std::string& name = MXNET_RESOURCE_DEFAULT_NAME_FARG("temp_space")) const {
152  return get_space_typed<xpu, ndim, real_t>(shape, stream, name);
153  }
162  template <int ndim>
164  return get_host_space_typed<cpu, ndim, real_t>(shape);
165  }
177  template <typename xpu, int ndim, typename DType>
179  mshadow::Shape<ndim> shape,
180  mshadow::Stream<xpu>* stream,
181  const std::string& name = MXNET_RESOURCE_DEFAULT_NAME_FARG("temp_space")) const {
184  reinterpret_cast<DType*>(get_space_internal(shape.Size() * sizeof(DType), name)),
185  shape,
186  shape[ndim - 1],
187  stream);
188  }
189 #if MXNET_USE_CUDNN == 1
190 
199  void get_cudnn_dropout_desc(
200  cudnnDropoutDescriptor_t* dropout_desc,
201  mshadow::Stream<gpu>* stream,
202  const float dropout,
203  const std::string& name = MXNET_RESOURCE_DEFAULT_NAME_FARG("cudnn_dropout_state")) const;
204 #endif // MXNET_USE_CUDNN == 1
205 
215  template <int ndim, typename DType>
218  reinterpret_cast<DType*>(get_host_space_internal(shape.Size() * sizeof(DType))),
219  shape,
220  shape[ndim - 1],
221  nullptr);
222  }
229  void* get_space_internal(size_t size, const std::string& name) const;
235  void* get_host_space_internal(size_t size) const;
236 };
237 
240  public:
249  virtual Resource Request(Context ctx, const ResourceRequest& req) = 0;
254  virtual void SeedRandom(uint32_t seed) = 0;
259  virtual void SeedRandom(Context ctx, uint32_t seed) = 0;
265  static ResourceManager* Get();
266 };
267 } // namespace mxnet
268 #endif // MXNET_RESOURCE_H_
mxnet
namespace of mxnet
Definition: api_registry.h:33
mshadow::Shape::Size
MSHADOW_XINLINE index_t Size(void) const
Definition: tensor.h:158
mxnet::Resource::get_host_space
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:163
mxnet::ResourceRequest::Type
Type
Resource type, indicating what the pointer type is.
Definition: resource.h:40
mxnet::ResourceRequest
The resources that can be requested by Operator.
Definition: resource.h:38
mshadow::Stream
computaion stream structure, used for asynchronous computations
Definition: tensor.h:488
mxnet::Resource::get_parallel_random
common::random::RandGenerator< xpu, DType > * get_parallel_random() const
Get parallel random number generator.
Definition: resource.h:125
mxnet::Resource::Resource
Resource()
default constructor
Definition: resource.h:103
mxnet::Resource::get_host_space_internal
void * get_host_space_internal(size_t size) const
internal function to get cpu space from resources.
mxnet::Resource::id
int32_t id
identifier of id information, used for debug purpose
Definition: resource.h:96
mxnet::Resource::var
engine::VarHandle var
engine variable
Definition: resource.h:94
mxnet::ResourceRequest::ResourceRequest
ResourceRequest()
default constructor
Definition: resource.h:56
mshadow::Random
random number generator
Definition: random.h:45
mxnet::common::random::RandGenerator
Definition: random_generator.h:41
mxnet::ResourceRequest::kRandom
@ kRandom
mshadow::Random<xpu> object
Definition: resource.h:42
mxnet::Resource::get_space_typed
mshadow::Tensor< xpu, ndim, DType > get_space_typed(mshadow::Shape< ndim > shape, mshadow::Stream< xpu > *stream, const std::string &name=MXNET_RESOURCE_DEFAULT_NAME_FARG("temp_space")) const
Get space requested as mshadow Tensor in specified type. The caller can request arbitrary size.
Definition: resource.h:178
mxnet::Resource::get_host_space_typed
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:216
mshadow::Tensor
general tensor
Definition: tensor.h:525
mxnet::Resource::req
ResourceRequest req
The original request.
Definition: resource.h:92
mxnet::Resource::ptr_
void * ptr_
pointer to the resource, do not use directly, access using member functions
Definition: resource.h:101
mxnet::ResourceManager
Global resource manager.
Definition: resource.h:239
mxnet::ResourceManager::Get
static ResourceManager * Get()
mxnet::Resource::get_space_internal
void * get_space_internal(size_t size, const std::string &name) const
internal function to get space from resources.
mshadow::Stream< gpu >
Definition: stream_gpu-inl.h:37
MXNET_RESOURCE_DEFAULT_NAME_FARG
#define MXNET_RESOURCE_DEFAULT_NAME_FARG(tag)
Definition: resource.h:81
mxnet::ResourceManager::SeedRandom
virtual void SeedRandom(uint32_t seed)=0
Seed all the allocated random number generators.
mxnet::ResourceRequest::type
Type type
type of resources
Definition: resource.h:54
mxnet::Resource::get_random
mshadow::Random< xpu, DType > * get_random(mshadow::Stream< xpu > *stream) const
Get random number generator.
Definition: resource.h:111
mxnet::Context
Context information about the execution environment.
Definition: base.h:90
mxnet::engine::Var
base class of engine variables.
Definition: engine.h:111
mxnet::Resource
Resources used by mxnet operations. A resource is something special other than NDArray,...
Definition: resource.h:90
mshadow::Shape
shape of a tensor
Definition: tensor.h:64
mxnet::ResourceRequest::kTempSpace
@ kTempSpace
A dynamic temp space that can be arbitrary size.
Definition: resource.h:44
mxnet::Resource::get_space
mshadow::Tensor< xpu, ndim, real_t > get_space(mshadow::Shape< ndim > shape, mshadow::Stream< xpu > *stream, const std::string &name=MXNET_RESOURCE_DEFAULT_NAME_FARG("temp_space")) const
Get space requested as mshadow Tensor. The caller can request arbitrary size.
Definition: resource.h:148
mxnet::ResourceManager::~ResourceManager
virtual ~ResourceManager() DMLC_THROW_EXCEPTION
virtual destructor
Definition: resource.h:261
engine.h
Engine that schedules all the operations according to dependency.
mxnet::ResourceRequest::kParallelRandom
@ kParallelRandom
common::RandGenerator<xpu> object, which can be used in GPU kernel functions
Definition: resource.h:46
DMLC_THROW_EXCEPTION
#define DMLC_THROW_EXCEPTION
Definition: base.h:233
mxnet::ResourceManager::Request
virtual Resource Request(Context ctx, const ResourceRequest &req)=0
Get resource of requested type.
mxnet::ResourceRequest::ResourceRequest
ResourceRequest(Type type)
constructor, allow implicit conversion
Definition: resource.h:61
base.h
configuration of MXNet as well as basic data structure.
random_generator.h
Parallel random number generator.