diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b8bbd2e0272..5c457b662a84 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -408,12 +408,14 @@ if(USE_OPENMP) endif() set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") + add_definitions(-DMXNET_USE_OPENMP=1) else() if(OPENMP_FOUND) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}") + add_definitions(-DMXNET_USE_OPENMP=1) endif() endif() elseif(UNIX AND NOT ANDROID) diff --git a/include/mxnet/base.h b/include/mxnet/base.h index f773139d6c3e..7b02fa1c8939 100644 --- a/include/mxnet/base.h +++ b/include/mxnet/base.h @@ -25,6 +25,7 @@ #ifndef MXNET_BASE_H_ #define MXNET_BASE_H_ +#include "mxfeatures.h" #include #include #include @@ -36,36 +37,6 @@ #include #include -/*! - *\brief whether to use opencv support - */ -#ifndef MXNET_USE_OPENCV -#define MXNET_USE_OPENCV 1 -#endif - -/*! - *\brief whether to use cuda support - */ -#ifndef MXNET_USE_CUDA -#define MXNET_USE_CUDA MSHADOW_USE_CUDA -#endif - -/*! - *\brief whether to use cudnn library for convolution - */ -#ifndef MXNET_USE_CUDNN -#define MXNET_USE_CUDNN MSHADOW_USE_CUDNN -#endif - -/*! - *\brief whether to use cusolver library - */ -#ifndef MXNET_USE_CUSOLVER -#define MXNET_USE_CUSOLVER MSHADOW_USE_CUSOLVER -#endif - -/*! \brief Error message for using gpu when MXNET_USE_CUDA==0 */ -#define MXNET_GPU_NOT_ENABLED_ERROR "GPU is not enabled" /*! * \brief define compatible keywords in g++ @@ -412,6 +383,7 @@ inline std::ostream& operator<<(std::ostream &out, const Context &ctx) { #define MXNET_DESCRIBE(...) describe(__VA_ARGS__ "\n\nFrom:" __FILE__ ":" STRINGIZE(__LINE__)) #define ADD_FILELINE "\n\nDefined in " __FILE__ ":L" STRINGIZE(__LINE__) + #if MXNET_USE_MKLDNN == 1 constexpr size_t kMKLDNNAlign = 64; #endif diff --git a/include/mxnet/mxfeatures.h b/include/mxnet/mxfeatures.h new file mode 100644 index 000000000000..a06bb0a1d85e --- /dev/null +++ b/include/mxnet/mxfeatures.h @@ -0,0 +1,142 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * Copyright (c) 2018 by Contributors + * \file mxfeatures.h + * \brief check MXNet features including compile time support + */ + +#pragma once + +#include +#include + +/*! + *\brief whether to use opencv support + */ +#ifndef MXNET_USE_OPENCV +#define MXNET_USE_OPENCV 1 +#endif + +/*! + *\brief whether to use cuda support + */ +#ifndef MXNET_USE_CUDA +#define MXNET_USE_CUDA MSHADOW_USE_CUDA +#endif + +/*! + *\brief whether to use cudnn library for convolution + */ +#ifndef MXNET_USE_CUDNN +#define MXNET_USE_CUDNN MSHADOW_USE_CUDNN +#endif + +/*! + *\brief whether to use cusolver library + */ +#ifndef MXNET_USE_CUSOLVER +#define MXNET_USE_CUSOLVER MSHADOW_USE_CUSOLVER +#endif + +#ifndef MXNET_ENABLE_CUDA_RTC +#define MXNET_ENABLE_CUDA_RTC 0 +#endif + +/*! \brief Error message for using gpu when MXNET_USE_CUDA==0 */ +#define MXNET_GPU_NOT_ENABLED_ERROR "GPU is not enabled" + + +#ifndef MXNET_USE_TENSORRT +#define MXNET_USE_TENSORRT 0 +#endif + +#ifndef MXNET_USE_OPENMP +#define MXNET_USE_OPENMP 0 +#endif + +#ifndef MXNET_USE_F16C +#define MXNET_USE_F16C MSHADOW_USE_F16C +#endif + +#ifndef MXNET_USE_CAFFE +#define MXNET_USE_CAFFE 0 +#endif + +#ifndef MXNET_USE_DIST_KVSTORE +#define MXNET_USE_DIST_KVSTORE 0 +#endif + +#ifndef MXNET_USE_SIGNAL_HANDLER +#define MXNET_USE_SIGNAL_HANDLER 0 +#endif + +#ifndef MXNET_USE_NCCL +#define MXNET_USE_NCCL 0 +#endif + + + +namespace mxnet { +namespace features { +// Check compile flags such as CMakeLists.txt + +/// Compile time features +enum : uint32_t { + // NVIDIA, CUDA + CUDA=0, + CUDNN, + NCCL, + CUDA_RTC, + TENSORRT, + + // Multiprocessing / CPU / System + OPENMP, + SSE, + F16C, + JEMALLOC, + + // Math libraries + LAPACK, + MKLDNN, + + // Image processing + OPENCV, + + // Misc + CAFFE, + PROFILER, + DIST_KVSTORE, + CXX14, + SIGNAL_HANDLER, + DEBUG, + + // size indicator + MAX_FEATURES +}; + + +/*! + * \return true if the given feature is supported + */ +bool is_enabled(uint32_t feat); + +} // namespace features +} // namespace mxnet diff --git a/src/mxfeatures.cc b/src/mxfeatures.cc new file mode 100644 index 000000000000..a2ba1dc4de3e --- /dev/null +++ b/src/mxfeatures.cc @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * Copyright (c) 2018 by Contributors + * \file mxfeatures.cc + * \brief check MXNet features including compile time support + */ + +#include "mxnet/mxfeatures.h" +#include "dmlc/logging.h" +#include + + +namespace mxnet { +namespace features { + +class Storage { +public: + Storage(): + feature_bits() + { + if (MXNET_USE_CUDA) + feature_bits.set(CUDA); + if (MXNET_USE_CUDNN) + feature_bits.set(CUDNN); + if (MXNET_USE_NCCL) + feature_bits.set(NCCL); + if (MXNET_USE_OPENCV) + feature_bits.set(OPENCV); + if (MXNET_ENABLE_CUDA_RTC) + feature_bits.set(CUDA_RTC); + if (MXNET_USE_TENSORRT) + feature_bits.set(TENSORRT); + if (MXNET_USE_OPENMP) + feature_bits.set(OPENMP); + if (MXNET_USE_F16C) + feature_bits.set(F16C); + if (MXNET_USE_LAPACK) + feature_bits.set(LAPACK); + if (MXNET_USE_MKLDNN) + feature_bits.set(MKLDNN); + if (MXNET_USE_OPENCV) + feature_bits.set(OPENCV); + if (MXNET_USE_CAFFE) + feature_bits.set(CAFFE); + if (MXNET_USE_DIST_KVSTORE) + feature_bits.set(DIST_KVSTORE); + if (MXNET_USE_SIGNAL_HANDLER) + feature_bits.set(SIGNAL_HANDLER); +#ifndef NDEBUG + feature_bits.set(DEBUG); +#endif + + +#ifdef USE_JEMALLOC + feature_bits.set(JEMALLOC); +#endif + } + bool is_enabled(unsigned feat) { + CHECK_LT(feat, MAX_FEATURES); + return feature_bits.test(feat); + } +private: + std::bitset feature_bits; +}; + +static Storage storage; + +bool is_enabled(unsigned feat) { + return storage.is_enabled(feat); +} + +} // namespace features +} // namespace mxnet