24 #ifndef MXNET_COMMON_UTILS_H_ 25 #define MXNET_COMMON_UTILS_H_ 27 #include <dmlc/logging.h> 29 #include <nnvm/graph.h> 34 #include <nnvm/graph_attr_types.h> 38 #include <type_traits> 49 template<
typename xpu>
50 void CastStorageDispatch(
const OpContext& ctx,
const NDArray& input,
const NDArray& output);
57 if (!vstorage.empty()) {
58 for (
const auto& i : vstorage) {
59 if (i != stype)
return false;
77 if (!vstorage.empty()) {
79 for (
const auto i : vstorage) {
82 }
else if (i == stype2) {
101 if (!ndarrays.empty()) {
102 for (
const auto& nd : ndarrays) {
103 if (nd.storage_type() != stype) {
122 if (!ndarrays.empty()) {
124 for (
const auto& nd : ndarrays) {
126 if (stype == stype1) {
128 }
else if (stype == stype2) {
135 *has_both = has == 3;
148 return "fcompute_ex";
150 return "fcompute_fallback";
176 return dmlc::GetEnv(
"MXNET_GPU_WORKER_NTHREADS", 2);
183 int num_match_color = dmlc::GetEnv(
"MXNET_EXEC_NUM_TEMP", 1);
187 template<
typename T,
typename V>
190 #pragma omp parallel for reduction(+:sum) 191 for (
int i = 0; i < n; ++i) {
204 template<
typename RandomIt,
typename Compare>
206 size_t grainsize,
const Compare& comp) {
207 if (len < grainsize) {
210 std::thread thr(ParallelSortHelper<RandomIt, Compare>, first, len/2, grainsize, comp);
213 std::inplace_merge(first, first+len/2, first+len, comp);
226 template<
typename RandomIt,
typename Compare>
227 void ParallelSort(RandomIt first, RandomIt last,
size_t num_threads, Compare comp) {
228 const auto num = std::distance(first, last);
229 size_t grainsize =
std::max(num / num_threads + 5, static_cast<size_t>(1024*16));
242 template<
typename RandomIt>
245 std::less<
typename std::iterator_traits<RandomIt>::value_type>());
283 template <
class T,
size_t kSize>
304 template <
class T,
class... Args>
306 return std::unique_ptr<T>(
new T(std::forward<Args>(args)...));
320 using U =
typename std::remove_extent<T>::type;
321 return std::unique_ptr<T>(
new U[n]{});
332 template <
class T,
class... Args>
335 template<
typename FCompType>
338 static auto& fcompute_cpu = nnvm::Op::GetAttr<FCompType>(name +
"<cpu>");
339 static auto& fcompute_gpu = nnvm::Op::GetAttr<FCompType>(name +
"<gpu>");
341 if (ctx.
dev_mask() == cpu::kDevMask) {
342 return fcompute_cpu.get(op,
nullptr);
343 }
else if (ctx.
dev_mask() == gpu::kDevMask) {
344 return fcompute_gpu.get(op,
nullptr);
346 LOG(FATAL) <<
"Unknown device mask";
353 #endif // MXNET_COMMON_UTILS_H_
NDArrayStorageType
Definition: ndarray.h:58
Engine that schedules all the operations according to dependency.
Symbol sort(const std::string &symbol_name, Symbol data, dmlc::optional< int > axis=dmlc::optional< int >(-1), bool is_ascend=1)
Definition: op.h:2487
namespace of mxnet
Definition: base.h:126
Additional operator attributes beside the ones provided by NNVM.
void KnownBound
Type of T.
Definition: utils.h:288
void ParallelSortHelper(RandomIt first, size_t len, size_t grainsize, const Compare &comp)
Helper function for ParallelSort. DO NOT call this function directly. Use the interface ParallelSort ...
Definition: utils.h:205
FCompType GetFCompute(const nnvm::Op *op, const std::string &name, const Context &ctx)
Definition: utils.h:336
V ParallelAccumulate(const T *a, const int n, V start)
Definition: utils.h:188
int GetNumThreadPerGPU()
Definition: utils.h:174
int dev_mask() const
Get corresponding device mask.
Definition: base.h:158
DispatchMode
the dispatch mode of the operator
Definition: op_attr_types.h:106
std::string stype_string(const int x)
get string representation of storage_type
Definition: utils.h:161
Symbol max(const std::string &symbol_name, Symbol data, Shape axis=Shape(), bool keepdims=0, bool exclude=0)
Definition: op.h:2230
void CastStorageDispatch(const OpContext &ctx, const NDArray &input, const NDArray &output)
void ParallelSort(RandomIt first, RandomIt last, size_t num_threads, Compare comp)
Sort the elements in the range [first, last) into the ascending order defined by the comparator comp...
Definition: utils.h:227
bool ContainsOnlyStorage(const StorageTypeVector &vstorage, const NDArrayStorageType stype)
returns true if all storage types in vstorage are the same as target stype. false is returned for emp...
Definition: utils.h:55
std::mt19937 RANDOM_ENGINE
Random Engine.
Definition: utils.h:251
Symbol sum(const std::string &symbol_name, Symbol data, Shape axis=Shape(), bool keepdims=0, bool exclude=0)
Definition: op.h:2041
std::string dispatch_mode_string(const DispatchMode x)
get string representation of dispatch_mode
Definition: utils.h:143
Helper for non-array type T.
Definition: utils.h:262
Data structures that can appear in graph attributes.
std::unique_ptr< T[]> UnknownBound
Type of T.
Definition: utils.h:277
nnvm::Op Op
operator structure from NNVM
Definition: base.h:138
std::unique_ptr< T > SingleObject
Type of T.
Definition: utils.h:266
int GetExecNumMatchColor()
Definition: utils.h:181
Symbol min(const std::string &symbol_name, Symbol data, Shape axis=Shape(), bool keepdims=0, bool exclude=0)
Definition: op.h:2267
helper::UniqueIf< T >::SingleObject MakeUnique(Args &&...args)
Constructs an object of type T and wraps it in a std::unique_ptr.
Definition: utils.h:305
Context information about the execution environment.
Definition: base.h:141
std::vector< int > StorageTypeVector
The result holder of storage type of each NodeEntry in the graph.
Definition: graph_attr_types.h:45