mxnet
base.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_BASE_H_
26 #define MXNET_BASE_H_
27 
28 #include <dmlc/base.h>
29 #include <dmlc/io.h>
30 #include <dmlc/type_traits.h>
31 #include <dmlc/parameter.h>
32 #include <mshadow/tensor.h>
33 // nnvm headers for symbolic construction.
34 #include <nnvm/op.h>
35 #include <nnvm/tuple.h>
36 #include <nnvm/symbolic.h>
37 #include <string>
38 
42 #ifndef MXNET_USE_OPENCV
43 #define MXNET_USE_OPENCV 1
44 #endif
45 
49 #ifndef MXNET_USE_CUDA
50 #define MXNET_USE_CUDA MSHADOW_USE_CUDA
51 #endif
52 
56 #ifndef MXNET_USE_CUDNN
57 #define MXNET_USE_CUDNN MSHADOW_USE_CUDNN
58 #endif
59 
63 #ifndef MXNET_USE_CUSOLVER
64 #define MXNET_USE_CUSOLVER MSHADOW_USE_CUSOLVER
65 #endif
66 
68 #define MXNET_GPU_NOT_ENABLED_ERROR "GPU is not enabled"
69 
74 #if DMLC_USE_CXX11 && defined(__GNUC__) && !defined(__clang_version__)
75 #if __GNUC__ == 4 && __GNUC_MINOR__ < 8
76 #error "Currently we need g++ 4.8 or higher to fully support c++11 features"
77 #define override
78 #define final
79 #endif
80 #endif
81 
85 #ifdef _MSC_VER
86 #ifdef MXNET_EXPORTS
87 #define MXNET_API __declspec(dllexport)
88 #else
89 #define MXNET_API __declspec(dllimport)
90 #endif
91 #else
92 #define MXNET_API
93 #endif
94 
98 #ifndef MXNET_PREDICT_ONLY
99 #define MXNET_PREDICT_ONLY 0
100 #endif
101 
105 #if MXNET_USE_PROFILER
106 #define PROFILER_MESSAGE(msg) msg
107 #else
108 #define PROFILER_MESSAGE(msg) nullptr
109 #endif
110 
112 #define MXNET_MAJOR 1
113 
114 #define MXNET_MINOR 0
115 
116 #define MXNET_PATCH 0
117 
118 #define MXNET_VERSION (MXNET_MAJOR*10000 + MXNET_MINOR*100 + MXNET_PATCH)
119 
120 #define MXNET_MAKE_VERSION(major, minor, patch) ((major)*10000 + (minor)*100 + patch)
121 
124 #define PROFILER_MESSAGE_FUNCNAME PROFILER_MESSAGE(__FUNCTION__)
125 
127 namespace mxnet {
135 typedef mshadow::default_real_t real_t;
139 using Op = nnvm::Op;
140 
142 struct Context {
144  enum DeviceType {
145  kCPU = cpu::kDevMask,
146  kGPU = gpu::kDevMask,
149  };
153  int32_t dev_id;
155  Context() : dev_type(kCPU), dev_id(0) {}
160  inline DeviceType dev_mask() const {
161  if (dev_type == kCPUPinned || dev_type == kCPUShared) return kCPU;
162  return dev_type;
163  }
167  inline int real_dev_id() const {
168  if (dev_type == kGPU) return dev_id;
169  return 0;
170  }
176  inline bool operator<(const Context &b) const;
182  inline bool operator==(const Context &b) const {
183  return dev_type == b.dev_type && dev_id == b.dev_id;
184  }
190  inline bool operator!=(const Context &b) const {
191  return !(*this == b);
192  }
197  inline void Save(dmlc::Stream *strm) const {
198  strm->Write(&dev_type, sizeof(dev_type));
199  strm->Write(&dev_id, sizeof(dev_id));
200  }
206  inline bool Load(dmlc::Stream *strm) {
207  if (strm->Read(&dev_type, sizeof(dev_type)) != sizeof(dev_type)) return false;
208  if (strm->Read(&dev_id, sizeof(int32_t)) != sizeof(int32_t)) return false;
209  return true;
210  }
212  static const int32_t kMaxDevType = 6;
214  static const int32_t kMaxDevID = 16;
220  inline static Context Create(DeviceType dev_type, int32_t dev_id = -1);
222  inline static Context CPU(int32_t dev_id = 0);
228  inline static Context GPU(int32_t dev_id = -1);
234  inline static Context CPUPinned(int32_t dev_id = -1);
240  inline static Context CPUShared(int32_t dev_id = 0);
246  inline static Context FromString(std::string str);
247 };
248 
253 struct RunContext {
259  void *stream;
265  template<typename xpu>
266  inline mshadow::Stream<xpu>* get_stream() const {
267  return static_cast<mshadow::Stream<xpu>*>(stream);
268  }
270  inline const Context& get_ctx() const {
271  return ctx;
272  }
273 };
274 } // namespace mxnet
275 
277 namespace mxnet {
278 // implementing Context
279 inline bool Context::operator<(const Context &b) const {
280  if (dev_type == b.dev_type) {
281  return dev_id < b.dev_id;
282  } else {
283  return dev_type < b.dev_type;
284  }
285 }
287  Context ctx;
288  ctx.dev_type = dev_type;
289  if (dev_id < 0) {
290  ctx.dev_id = 0;
291  if (dev_type & kGPU) {
292 #if MXNET_USE_CUDA
293  CHECK_EQ(cudaGetDevice(&ctx.dev_id), cudaSuccess);
294 #else
295  LOG(FATAL) << "Please compile with CUDA enabled for cuda features";
296 #endif
297  }
298  } else {
299  ctx.dev_id = dev_id;
300  }
301  return ctx;
302 }
303 inline Context Context::CPU(int32_t dev_id) {
304  return Create(kCPU, dev_id);
305 }
306 
307 inline Context Context::CPUPinned(int32_t dev_id) {
308  return Create(kCPUPinned, dev_id);
309 }
310 
311 inline Context Context::CPUShared(int32_t dev_id) {
312  return Create(kCPUShared, dev_id);
313 }
314 
315 inline Context Context::GPU(int32_t dev_id) {
316  return Create(kGPU, dev_id);
317 }
318 
319 inline Context Context::FromString(std::string str) {
320  Context ret;
321  try {
322  std::string::size_type l = str.find('(');
323  CHECK_NE(l, std::string::npos);
324  std::string::size_type r = str.find(')');
325  CHECK_EQ(r, str.length()-1);
326 
327  std::string type = str.substr(0, l);
328  int id = std::stoi(str.substr(l+1, r-l-1));
329  if (type == "cpu") {
330  ret = CPU(id);
331  } else if (type == "gpu") {
332  ret = GPU(id);
333  } else if (type == "cpu_pinned") {
334  ret = CPUPinned(id);
335  } else if (type == "cpu_shared") {
336  ret = CPUShared(id);
337  } else {
338  LOG(FATAL) << "Invalid context string " << str;
339  }
340  } catch (...) {
341  LOG(FATAL) << "Invalid context string " << str;
342  }
343  return ret;
344 }
345 
346 inline std::ostream& operator<<(std::ostream &out, const Context &ctx) {
347  if (ctx.dev_type == Context::kCPU) {
348  out << "cpu(";
349  } else if (ctx.dev_type == Context::kGPU) {
350  out << "gpu(";
351  } else if (ctx.dev_type == Context::kCPUPinned) {
352  out << "cpu_pinned(";
353  } else if (ctx.dev_type == Context::kCPUShared) {
354  out << "cpu_shared(";
355  } else {
356  out << "unknown(";
357  }
358  out << ctx.dev_id << ")";
359  return out;
360 }
361 
362 // describe op registration point
363 #define STRINGIZE_DETAIL(x) #x
364 #define STRINGIZE(x) STRINGIZE_DETAIL(x)
365 #define MXNET_DESCRIBE(...) describe(__VA_ARGS__ "\n\nFrom:" __FILE__ ":" STRINGIZE(__LINE__))
366 #define ADD_FILELINE "\n\nDefined in " __FILE__ ":L" STRINGIZE(__LINE__)
367 
368 } // namespace mxnet
369 
370 #include "./tensor_blob.h"
372 #endif // MXNET_BASE_H_
DeviceType dev_mask() const
Get corresponding device mask.
Definition: base.h:160
static const int32_t kMaxDevID
the maximal device index
Definition: base.h:214
namespace of mxnet
Definition: base.h:127
mshadow::Stream< xpu > * get_stream() const
get mshadow stream from Context
Definition: base.h:266
bool Load(dmlc::Stream *strm)
load the content from binary stream
Definition: base.h:206
mshadow::default_real_t real_t
data type that will be used to store ndarray
Definition: base.h:135
static Context GPU(int32_t dev_id=-1)
nnvm::TShape TShape
Shape data structure used to record shape information.
Definition: base.h:137
Context ctx
base Context
Definition: base.h:255
bool operator<(const Context &b) const
Comparator, used to enable Context as std::map key.
static const int32_t kMaxDevType
the maximal device type
Definition: base.h:212
execution time context. The information needed in runtime for actual execution.
Definition: base.h:253
DeviceType dev_type
the device type we run the op on
Definition: base.h:151
Definition: base.h:145
int32_t dev_id
device id we are going to run it on
Definition: base.h:153
Definition: base.h:147
static Context FromString(std::string str)
void * stream
the stream of the device, can be NULL or Stream<gpu>* in GPU mode
Definition: base.h:259
void Save(dmlc::Stream *strm) const
save the content into binary stream
Definition: base.h:197
mshadow::gpu gpu
mxnet gpu
Definition: base.h:131
const Context & get_ctx() const
get the base Context from RunContext
Definition: base.h:270
Definition: base.h:146
DeviceType
Type of device.
Definition: base.h:144
static Context CPUShared(int32_t dev_id=0)
mshadow::cpu cpu
mxnet cpu
Definition: base.h:129
int real_dev_id() const
Returns dev_id for kGPU, 0 otherwise.
Definition: base.h:167
nnvm::Op Op
operator structure from NNVM
Definition: base.h:139
Context()
default constructor
Definition: base.h:155
static Context Create(DeviceType dev_type, int32_t dev_id=-1)
Create a new context.
bool operator!=(const Context &b) const
check if current context not equals another one
Definition: base.h:190
static Context CPU(int32_t dev_id=0)
std::ostream & operator<<(std::ostream &out, const NDArray &ndarray)
static Context CPUPinned(int32_t dev_id=-1)
Definition: base.h:148
mshadow::index_t index_t
index type usually use unsigned
Definition: base.h:133
TBlob class that holds common representation of arbirary dimension tensor, can be used to transformed...
Context information about the execution environment.
Definition: base.h:142
bool operator==(const Context &b) const
check if current context equals another one
Definition: base.h:182
unsigned index_t
Definition: base.h:37