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 
24 #ifndef MXNET_BASE_H_
25 #define MXNET_BASE_H_
26 
27 #include <dmlc/base.h>
28 #include <dmlc/io.h>
29 #include <dmlc/type_traits.h>
30 #include <dmlc/parameter.h>
31 #include <mshadow/tensor.h>
32 // nnvm headers for symbolic construction.
33 #include <nnvm/op.h>
34 #include <nnvm/tuple.h>
35 #include <nnvm/symbolic.h>
36 #include <string>
37 
41 #ifndef MXNET_USE_OPENCV
42 #define MXNET_USE_OPENCV 1
43 #endif
44 
48 #ifndef MXNET_USE_CUDA
49 #define MXNET_USE_CUDA MSHADOW_USE_CUDA
50 #endif
51 
55 #ifndef MXNET_USE_CUDNN
56 #define MXNET_USE_CUDNN MSHADOW_USE_CUDNN
57 #endif
58 
62 #ifndef MXNET_USE_CUSOLVER
63 #define MXNET_USE_CUSOLVER MSHADOW_USE_CUSOLVER
64 #endif
65 
67 #define MXNET_GPU_NOT_ENABLED_ERROR "GPU is not enabled"
68 
73 #if DMLC_USE_CXX11 && defined(__GNUC__) && !defined(__clang_version__)
74 #if __GNUC__ == 4 && __GNUC_MINOR__ < 8
75 #error "Currently we need g++ 4.8 or higher to fully support c++11 features"
76 #define override
77 #define final
78 #endif
79 #endif
80 
84 #ifdef _MSC_VER
85 #ifdef MXNET_EXPORTS
86 #define MXNET_API __declspec(dllexport)
87 #else
88 #define MXNET_API __declspec(dllimport)
89 #endif
90 #else
91 #define MXNET_API
92 #endif
93 
97 #ifndef MXNET_PREDICT_ONLY
98 #define MXNET_PREDICT_ONLY 0
99 #endif
100 
104 #if MXNET_USE_PROFILER
105 #define PROFILER_MESSAGE(msg) msg
106 #else
107 #define PROFILER_MESSAGE(msg) nullptr
108 #endif
109 
111 #define MXNET_MAJOR 0
112 
113 #define MXNET_MINOR 11
114 
115 #define MXNET_PATCH 0
116 
117 #define MXNET_VERSION (MXNET_MAJOR*10000 + MXNET_MINOR*100 + MXNET_PATCH)
118 
119 #define MXNET_MAKE_VERSION(major, minor, patch) ((major)*10000 + (minor)*100 + patch)
120 
123 #define PROFILER_MESSAGE_FUNCNAME PROFILER_MESSAGE(__FUNCTION__)
124 
126 namespace mxnet {
134 typedef mshadow::default_real_t real_t;
138 using Op = nnvm::Op;
139 
141 struct Context {
143  enum DeviceType {
144  kCPU = cpu::kDevMask,
145  kGPU = gpu::kDevMask,
147  };
151  int32_t dev_id;
153  Context() : dev_type(kCPU), dev_id(0) {}
158  inline int dev_mask() const {
159  if (dev_type == kCPUPinned) return cpu::kDevMask;
160  return dev_type;
161  }
167  inline bool operator<(const Context &b) const;
173  inline bool operator==(const Context &b) const {
174  return dev_type == b.dev_type && dev_id == b.dev_id;
175  }
181  inline bool operator!=(const Context &b) const {
182  return !(*this == b);
183  }
188  inline void Save(dmlc::Stream *strm) const {
189  strm->Write(&dev_type, sizeof(dev_type));
190  strm->Write(&dev_id, sizeof(dev_id));
191  }
197  inline bool Load(dmlc::Stream *strm) {
198  if (strm->Read(&dev_type, sizeof(dev_type)) != sizeof(dev_type)) return false;
199  if (strm->Read(&dev_id, sizeof(int32_t)) != sizeof(int32_t)) return false;
200  return true;
201  }
203  static const int32_t kMaxDevType = 4;
205  static const int32_t kMaxDevID = 16;
211  inline static Context Create(DeviceType dev_type, int32_t dev_id = -1);
213  inline static Context CPU(int32_t dev_id = 0);
219  inline static Context GPU(int32_t dev_id = -1);
225  inline static Context CPUPinned(int32_t dev_id = -1);
231  inline static Context FromString(std::string str);
232 };
233 
238 struct RunContext {
244  void *stream;
250  template<typename xpu>
251  inline mshadow::Stream<xpu>* get_stream() const {
252  return static_cast<mshadow::Stream<xpu>*>(stream);
253  }
255  inline const Context& get_ctx() const {
256  return ctx;
257  }
258 };
259 } // namespace mxnet
260 
262 namespace mxnet {
263 // implementing Context
264 inline bool Context::operator<(const Context &b) const {
265  if (dev_type == b.dev_type) {
266  return dev_id < b.dev_id;
267  } else {
268  return dev_type < b.dev_type;
269  }
270 }
272  Context ctx;
273  ctx.dev_type = dev_type;
274  if (dev_id < 0) {
275  ctx.dev_id = 0;
276  if (dev_type != kCPU) {
277 #if MXNET_USE_CUDA
278  CHECK_EQ(cudaGetDevice(&ctx.dev_id), cudaSuccess);
279 #else
280  LOG(FATAL) << "Please compile with CUDA enabled for cuda features";
281 #endif
282  }
283  } else {
284  ctx.dev_id = dev_id;
285  }
286  return ctx;
287 }
288 inline Context Context::CPU(int32_t dev_id) {
289  return Create(kCPU, dev_id);
290 }
291 
292 inline Context Context::CPUPinned(int32_t dev_id) {
293  return Create(kCPUPinned, dev_id);
294 }
295 
296 inline Context Context::GPU(int32_t dev_id) {
297  return Create(kGPU, dev_id);
298 }
299 
300 inline Context Context::FromString(std::string str) {
301  Context ret;
302  try {
303  std::string::size_type l = str.find('(');
304  CHECK_NE(l, std::string::npos);
305  std::string::size_type r = str.find(')');
306  CHECK_EQ(r, str.length()-1);
307 
308  std::string type = str.substr(0, l);
309  int id = std::stoi(str.substr(l+1, r-l-1));
310  if (type == "cpu") {
311  ret = CPU(id);
312  } else if (type == "gpu") {
313  ret = GPU(id);
314  } else if (type == "cpu_pinned") {
315  ret = CPUPinned(id);
316  } else {
317  LOG(FATAL) << "Invalid context string " << str;
318  }
319  } catch (...) {
320  LOG(FATAL) << "Invalid context string " << str;
321  }
322  return ret;
323 }
324 
325 inline std::ostream& operator<<(std::ostream &out, const Context &ctx) {
326  if (ctx.dev_type == Context::kCPU) {
327  out << "cpu(";
328  } else if (ctx.dev_type == Context::kGPU) {
329  out << "gpu(";
330  } else if (ctx.dev_type == Context::kCPUPinned) {
331  out << "cpu_pinned(";
332  } else {
333  out << "unknown(";
334  }
335  out << ctx.dev_id << ")";
336  return out;
337 }
338 
339 // describe op registration point
340 #define STRINGIZE_DETAIL(x) #x
341 #define STRINGIZE(x) STRINGIZE_DETAIL(x)
342 #define MXNET_DESCRIBE(...) describe(__VA_ARGS__ "\n\nFrom:" __FILE__ ":" STRINGIZE(__LINE__))
343 #define ADD_FILELINE "\n\nDefined in " __FILE__ ":L" STRINGIZE(__LINE__)
344 
345 } // namespace mxnet
346 
347 #include "./tensor_blob.h"
349 #endif // MXNET_BASE_H_
static const int32_t kMaxDevID
the maximal device index
Definition: base.h:205
namespace of mxnet
Definition: base.h:126
mshadow::Stream< xpu > * get_stream() const
get mshadow stream from Context
Definition: base.h:251
bool Load(dmlc::Stream *strm)
load the content from binary stream
Definition: base.h:197
mshadow::default_real_t real_t
data type that will be used to store ndarray
Definition: base.h:134
static Context GPU(int32_t dev_id=-1)
nnvm::TShape TShape
Shape data structure used to record shape information.
Definition: base.h:136
Context ctx
base Context
Definition: base.h:240
bool operator<(const Context &b) const
Comparator, used to enable Context as std::map key.
int dev_mask() const
Get corresponding device mask.
Definition: base.h:158
static const int32_t kMaxDevType
the maximal device type
Definition: base.h:203
execution time context. The information needed in runtime for actual execution.
Definition: base.h:238
DeviceType dev_type
the device type we run the op on
Definition: base.h:149
Definition: base.h:144
int32_t dev_id
device id we are going to run it on
Definition: base.h:151
Definition: base.h:146
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:244
void Save(dmlc::Stream *strm) const
save the content into binary stream
Definition: base.h:188
mshadow::gpu gpu
mxnet gpu
Definition: base.h:130
const Context & get_ctx() const
get the base Context from RunContext
Definition: base.h:255
Definition: base.h:145
DeviceType
Type of device.
Definition: base.h:143
mshadow::cpu cpu
mxnet cpu
Definition: base.h:128
nnvm::Op Op
operator structure from NNVM
Definition: base.h:138
Context()
default constructor
Definition: base.h:153
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:181
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)
mshadow::index_t index_t
index type usually use unsigned
Definition: base.h:132
TBlob class that holds common representation of arbirary dimension tensor, can be used to transformed...
Context information about the execution environment.
Definition: base.h:141
bool operator==(const Context &b) const
check if current context equals another one
Definition: base.h:173
unsigned index_t
Definition: base.h:36