mxnet
utils.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_COMMON_UTILS_H_
25 #define MXNET_COMMON_UTILS_H_
26 
27 #if DMLC_USE_CXX11
28 #include <memory>
29 #include <vector>
30 #include <type_traits>
31 #include <utility>
32 #include <random>
33 #include <string>
34 #include <thread>
35 #include <algorithm>
36 #endif // DMLC_USE_CXX11
37 
38 #include <dmlc/logging.h>
39 #include <mxnet/engine.h>
40 
41 namespace mxnet {
42 namespace common {
43 
44 #if DMLC_USE_CXX11
45 // heuristic to dermine number of threads per GPU
46 inline int GetNumThreadPerGPU() {
47  // This is resource efficient option.
48  return dmlc::GetEnv("MXNET_GPU_WORKER_NTHREADS", 2);
49 }
50 
51 // heuristic to get number of matching colors.
52 // this decides how much parallelism we can get in each GPU.
53 inline int GetExecNumMatchColor() {
54  // This is resource efficient option.
55  int num_match_color = dmlc::GetEnv("MXNET_EXEC_NUM_TEMP", 1);
56  return std::min(num_match_color, GetNumThreadPerGPU());
57 }
58 
62 typedef std::mt19937 RANDOM_ENGINE;
63 
67 namespace helper {
68 
72 template <class T>
73 struct UniqueIf {
77  using SingleObject = std::unique_ptr<T>;
78 };
79 
83 template <class T>
84 struct UniqueIf<T[]> {
88  using UnknownBound = std::unique_ptr<T[]>;
89 };
90 
94 template <class T, size_t kSize>
95 struct UniqueIf<T[kSize]> {
99  using KnownBound = void;
100 };
101 
102 } // namespace helper
103 
115 template <class T, class... Args>
117  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
118 }
119 
129 template <class T>
131  using U = typename std::remove_extent<T>::type;
132  return std::unique_ptr<T>(new U[n]{});
133 }
134 
143 template <class T, class... Args>
144 typename helper::UniqueIf<T>::KnownBound MakeUnique(Args&&... args) = delete;
145 
146 template<typename FCompType>
147 FCompType GetFCompute(const nnvm::Op* op, const std::string& name,
148  const Context& ctx) {
149  static auto& fcompute_cpu = nnvm::Op::GetAttr<FCompType>(name + "<cpu>");
150  static auto& fcompute_gpu = nnvm::Op::GetAttr<FCompType>(name + "<gpu>");
151 
152  if (ctx.dev_mask() == cpu::kDevMask) {
153  return fcompute_cpu.get(op, nullptr);
154  } else if (ctx.dev_mask() == gpu::kDevMask) {
155  return fcompute_gpu.get(op, nullptr);
156  } else {
157  LOG(FATAL) << "Unknown device mask";
158  return nullptr;
159  }
160 }
161 
162 #endif // DMLC_USE_CXX11
163 
164 } // namespace common
165 } // namespace mxnet
166 #endif // MXNET_COMMON_UTILS_H_
Symbol min(const std::string &symbol_name, Symbol data, Shape axis=Shape(), bool keepdims=false, bool exclude=false)
Definition: op.h:2319
Engine that schedules all the operations according to dependency.
namespace of mxnet
Definition: base.h:126
void KnownBound
Type of T.
Definition: utils.h:99
FCompType GetFCompute(const nnvm::Op *op, const std::string &name, const Context &ctx)
Definition: utils.h:147
int GetNumThreadPerGPU()
Definition: utils.h:46
int dev_mask() const
Get corresponding device mask.
Definition: base.h:158
std::mt19937 RANDOM_ENGINE
Random Engine.
Definition: utils.h:62
Helper for non-array type T.
Definition: utils.h:73
std::unique_ptr< T[]> UnknownBound
Type of T.
Definition: utils.h:88
nnvm::Op Op
operator structure from NNVM
Definition: base.h:138
std::unique_ptr< T > SingleObject
Type of T.
Definition: utils.h:77
int GetExecNumMatchColor()
Definition: utils.h:53
helper::UniqueIf< T >::SingleObject MakeUnique(Args &&...args)
Constructs an object of type T and wraps it in a std::unique_ptr.
Definition: utils.h:116
Context information about the execution environment.
Definition: base.h:141