mxnet
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
base.h
Go to the documentation of this file.
1 
6 #ifndef MXNET_BASE_H_
7 #define MXNET_BASE_H_
8 
9 #include <dmlc/base.h>
10 #include <dmlc/io.h>
11 #include <dmlc/type_traits.h>
12 #include <dmlc/parameter.h>
13 #include <mshadow/tensor.h>
14 #include <string>
15 
19 #ifndef MXNET_USE_OPENCV
20 #define MXNET_USE_OPENCV 1
21 #endif
22 
26 #ifndef MXNET_USE_CUDA
27 #define MXNET_USE_CUDA MSHADOW_USE_CUDA
28 #endif
29 
33 #ifndef MXNET_USE_CUDNN
34 #define MXNET_USE_CUDNN MSHADOW_USE_CUDNN
35 #endif
36 
38 #define MXNET_GPU_NOT_ENABLED_ERROR "GPU is not enabled"
39 
44 #if DMLC_USE_CXX11 && defined(__GNUC__) && !defined(__clang_version__)
45 #if __GNUC__ == 4 && __GNUC_MINOR__ < 8
46 #error "Currently we need g++ 4.8 or higher to fully support c++11 features"
47 #define override
48 #define final
49 #endif
50 #endif
51 
55 #ifdef _MSC_VER
56 #ifdef MXNET_EXPORTS
57 #define MXNET_API __declspec(dllexport)
58 #else
59 #define MXNET_API __declspec(dllimport)
60 #endif
61 #else
62 #define MXNET_API
63 #endif
64 
68 #ifndef MXNET_PREDICT_ONLY
69 #define MXNET_PREDICT_ONLY 0
70 #endif
71 
72 
74 namespace mxnet {
76 typedef mshadow::cpu cpu;
78 typedef mshadow::gpu gpu;
82 typedef mshadow::default_real_t real_t;
83 
88 
90 struct Context {
92  enum DeviceType {
93  kCPU = cpu::kDevMask,
94  kGPU = gpu::kDevMask,
96  };
100  int32_t dev_id;
107  inline int dev_mask() const {
108  if (dev_type == kCPUPinned) return cpu::kDevMask;
109  return dev_type;
110  }
116  inline bool operator<(const Context &b) const;
122  inline bool operator==(const Context &b) const {
123  return dev_type == b.dev_type && dev_id == b.dev_id;
124  }
130  inline bool operator!=(const Context &b) const {
131  return !(*this == b);
132  }
137  inline void Save(dmlc::Stream *strm) const {
138  strm->Write(&dev_type, sizeof(dev_type));
139  strm->Write(&dev_id, sizeof(dev_id));
140  }
146  inline bool Load(dmlc::Stream *strm) {
147  if (strm->Read(&dev_type, sizeof(dev_type)) != sizeof(dev_type)) return false;
148  if (strm->Read(&dev_id, sizeof(int32_t)) != sizeof(int32_t)) return false;
149  return true;
150  }
152  static const int32_t kMaxDevType = 4;
154  static const int32_t kMaxDevID = 16;
160  inline static Context Create(DeviceType dev_type, int32_t dev_id);
162  inline static Context CPU();
168  inline static Context GPU(int32_t dev_id);
174  inline static Context CPUPinned(int32_t dev_id);
175 };
176 
181 struct RunContext {
185  void *stream;
191  template<typename xpu>
192  inline mshadow::Stream<xpu>* get_stream() const {
193  return static_cast<mshadow::Stream<xpu>*>(stream);
194  }
195 };
196 } // namespace mxnet
197 
199 namespace mxnet {
200 // implementing Context
201 inline bool Context::operator<(const Context &b) const {
202  if (dev_type == b.dev_type) {
203  return dev_id < b.dev_id;
204  } else {
205  return dev_type < b.dev_type;
206  }
207 }
208 inline Context Context::Create(DeviceType dev_type, int32_t dev_id) {
209  Context ctx;
210  ctx.dev_type = dev_type;
211  ctx.dev_id = dev_id;
212  return ctx;
213 }
214 inline Context Context::CPU() {
215  return Create(kCPU, 0);
216 }
217 
218 inline Context Context::CPUPinned(int32_t dev_id) {
219  return Create(kCPUPinned, dev_id);
220 }
221 
222 inline Context Context::GPU(int32_t dev_id) {
223  return Create(kGPU, dev_id);
224 }
225 } // namespace mxnet
226 
227 namespace dmlc {
228 // Add a few patches to support TShape in dmlc/parameter.
229 DMLC_DECLARE_TYPE_NAME(mxnet::TShape, "Shape(tuple)");
230 
231 namespace parameter {
232 template<>
233 class FieldEntry<mxnet::TShape>
234  : public FieldEntryBase<FieldEntry<mxnet::TShape>, mxnet::TShape> {
235  public:
236  FieldEntry() : enforce_nonzero_(false), expect_ndim_(0) {}
237  // parent class
238  typedef FieldEntryBase<FieldEntry<mxnet::TShape>, mxnet::TShape> Parent;
239 
240  virtual void Check(void *head) const {
241  Parent::Check(head);
242  mxnet::TShape &v = this->Get(head);
243  if (expect_ndim_ != 0 && v.ndim() != expect_ndim_) {
244  std::ostringstream os;
245  os << "value " << v << "for Parameter " << this->key_
246  << " has wrong dimensions, expected dimension=" << expect_ndim_;
247  throw dmlc::ParamError(os.str());
248  }
249  if (enforce_nonzero_) {
250  for (mxnet::index_t i = 0; i < v.ndim(); ++i) {
251  if (v[i] == 0U) {
252  std::ostringstream os;
253  os << "value " << v << "for Parameter " << this->key_
254  << " is invalid, the input shape must be nonzero in all dimensions";
255  throw dmlc::ParamError(os.str());
256  }
257  }
258  }
259  }
260  inline FieldEntry<mxnet::TShape> &enforce_nonzero() {
261  this->enforce_nonzero_ = true;
262  return this->self();
263  }
264  inline FieldEntry<mxnet::TShape> &set_expect_ndim(mshadow::index_t ndim) {
265  expect_ndim_ = ndim;
266  return this->self();
267  }
268 
269  private:
270  // whether all the entries need to be nonzero
271  bool enforce_nonzero_;
272  // expected number of dimension, default = 0 means no restriction.
273  mxnet::index_t expect_ndim_;
274 };
275 } // namespace parameter
276 } // namespace dmlc
278 #endif // MXNET_BASE_H_
mshadow::TShape TShape
dynamic shape type
Definition: base.h:85
static const int32_t kMaxDevID
the maximal device index
Definition: base.h:154
mshadow::Stream< xpu > * get_stream() const
get mshadow stream from Context
Definition: base.h:192
static Context CPU()
bool Load(dmlc::Stream *strm)
load the content from binary stream
Definition: base.h:146
mshadow::default_real_t real_t
data type that will be used to store ndarray
Definition: base.h:82
bool operator<(const Context &b) const
Comparator, used to enable Context as std::map key.
mshadow::TBlob TBlob
storage container type
Definition: base.h:87
int dev_mask() const
Get corresponding device mask.
Definition: base.h:107
static const int32_t kMaxDevType
the maximal device type
Definition: base.h:152
execution time context. The information needed in runtime for actual execution.
Definition: base.h:181
static Context Create(DeviceType dev_type, int32_t dev_id)
Create a new context.
DeviceType dev_type
the device type we run the op on
Definition: base.h:98
Definition: base.h:93
static Context CPUPinned(int32_t dev_id)
int32_t dev_id
device id we are going to run it on
Definition: base.h:100
Definition: base.h:95
void * stream
the stream of the device, can be NULL or Stream<gpu>* in GPU mode
Definition: base.h:185
void Save(dmlc::Stream *strm) const
save the content into binary stream
Definition: base.h:137
mshadow::gpu gpu
mxnet gpu
Definition: base.h:78
Definition: base.h:94
DeviceType
Type of device.
Definition: base.h:92
mshadow::cpu cpu
mxnet cpu
Definition: base.h:76
Context()
default constructor
Definition: base.h:102
bool operator!=(const Context &b) const
check if current context not equals another one
Definition: base.h:130
mshadow::index_t index_t
index type usually use unsigned
Definition: base.h:80
static Context GPU(int32_t dev_id)
Context information about the execution enviroment.
Definition: base.h:90
bool operator==(const Context &b) const
check if current context equals another one
Definition: base.h:122