mxnet
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
utils.h
Go to the documentation of this file.
1 
6 #ifndef MXNET_COMMON_UTILS_H_
7 #define MXNET_COMMON_UTILS_H_
8 
9 #if DMLC_USE_CXX11
10 #include <memory>
11 #include <type_traits>
12 #include <utility>
13 #include <random>
14 #include <thread>
15 #include <algorithm>
16 #endif // DMLC_USE_CXX11
17 
18 #include <dmlc/logging.h>
19 
20 namespace mxnet {
21 namespace common {
22 
23 #if DMLC_USE_CXX11
24 
25 // heuristic to dermine number of threads per GPU
26 inline int GetNumThreadPerGPU() {
27  // This is resource efficient option.
28  return dmlc::GetEnv("MXNET_GPU_WORKER_NTHREADS", 2);
29 }
30 
31 // heuristic to get number of matching colors.
32 // this decides how much parallelism we can get in each GPU.
33 inline int GetExecNumMatchColor() {
34  // This is resource efficient option.
35  int num_match_color = dmlc::GetEnv("MXNET_EXEC_NUM_TEMP", 1);
36  return std::min(num_match_color, GetNumThreadPerGPU());
37 }
38 
42 typedef std::mt19937 RANDOM_ENGINE;
43 
47 namespace helper {
48 
52 template <class T>
53 struct UniqueIf {
57  using SingleObject = std::unique_ptr<T>;
58 };
59 
63 template <class T>
64 struct UniqueIf<T[]> {
68  using UnknownBound = std::unique_ptr<T[]>;
69 };
70 
74 template <class T, size_t kSize>
75 struct UniqueIf<T[kSize]> {
79  using KnownBound = void;
80 };
81 
82 } // namespace helper
83 
95 template <class T, class... Args>
97  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
98 }
99 
109 template <class T>
111  using U = typename std::remove_extent<T>::type;
112  return std::unique_ptr<T>(new U[n]{});
113 }
114 
123 template <class T, class... Args>
124 typename helper::UniqueIf<T>::KnownBound MakeUnique(Args&&... args) = delete;
125 
126 #endif // DMLC_USE_CXX11
127 
128 } // namespace common
129 } // namespace mxnet
130 #endif // MXNET_COMMON_UTILS_H_
void KnownBound
Type of T.
Definition: utils.h:79
int GetNumThreadPerGPU()
Definition: utils.h:26
std::mt19937 RANDOM_ENGINE
Random Engine.
Definition: utils.h:42
Helper for non-array type T.
Definition: utils.h:53
std::unique_ptr< T[]> UnknownBound
Type of T.
Definition: utils.h:68
std::unique_ptr< T > SingleObject
Type of T.
Definition: utils.h:57
int GetExecNumMatchColor()
Definition: utils.h:33
helper::UniqueIf< T >::SingleObject MakeUnique(Args &&...args)
Constructs an object of type T and wraps it in a std::unique_ptr.
Definition: utils.h:96