From 0c975e18088df5495bc4d63f4f3000c5415cf9a0 Mon Sep 17 00:00:00 2001 From: dmed256 Date: Sun, 9 Sep 2018 15:10:04 -0500 Subject: [PATCH] [Mode] Mode registration can now be toggled at runtime --- include/occa/mode.hpp | 7 ++++--- include/occa/mode/cuda/registration.hpp | 2 +- include/occa/mode/hip/registration.hpp | 2 +- include/occa/mode/opencl/registration.hpp | 2 +- include/occa/mode/opencl/utils.hpp | 2 ++ include/occa/mode/openmp/registration.hpp | 2 +- include/occa/mode/serial/registration.hpp | 2 +- src/mode/cuda/registration.cpp | 3 ++- src/mode/hip/registration.cpp | 3 ++- src/mode/opencl/registration.cpp | 5 ++++- src/mode/opencl/utils.cpp | 6 ++++++ src/mode/openmp/device.cpp | 7 +------ src/mode/openmp/registration.cpp | 8 +++++++- src/mode/serial/registration.cpp | 4 +++- 14 files changed, 36 insertions(+), 19 deletions(-) diff --git a/include/occa/mode.hpp b/include/occa/mode.hpp index 57f990475..5ccf03615 100644 --- a/include/occa/mode.hpp +++ b/include/occa/mode.hpp @@ -51,7 +51,7 @@ namespace occa { public: modeInfo_v(); - virtual void init() = 0; + virtual bool init() = 0; virtual styling::section& getDescription(); }; @@ -71,8 +71,9 @@ namespace occa { public: mode(std::string modeName_) { modeName = modeName_; - registerMode(this); - modeInfo_t().init(); + if (modeInfo_t().init()) { + registerMode(this); + } } styling::section &getDescription() { diff --git a/include/occa/mode/cuda/registration.hpp b/include/occa/mode/cuda/registration.hpp index 76ca842c2..1c08119e5 100644 --- a/include/occa/mode/cuda/registration.hpp +++ b/include/occa/mode/cuda/registration.hpp @@ -39,7 +39,7 @@ namespace occa { public: modeInfo(); - void init(); + bool init(); styling::section& getDescription(); }; diff --git a/include/occa/mode/hip/registration.hpp b/include/occa/mode/hip/registration.hpp index 94740df26..8eb5779fe 100644 --- a/include/occa/mode/hip/registration.hpp +++ b/include/occa/mode/hip/registration.hpp @@ -39,7 +39,7 @@ namespace occa { public: modeInfo(); - void init(); + bool init(); styling::section& getDescription(); }; diff --git a/include/occa/mode/opencl/registration.hpp b/include/occa/mode/opencl/registration.hpp index 20aa58a05..a79b952da 100644 --- a/include/occa/mode/opencl/registration.hpp +++ b/include/occa/mode/opencl/registration.hpp @@ -39,7 +39,7 @@ namespace occa { public: modeInfo(); - void init(); + bool init(); styling::section& getDescription(); }; diff --git a/include/occa/mode/opencl/utils.hpp b/include/occa/mode/opencl/utils.hpp index e98cb9f66..9f943d37e 100644 --- a/include/occa/mode/opencl/utils.hpp +++ b/include/occa/mode/opencl/utils.hpp @@ -65,6 +65,8 @@ namespace occa { std::string vendor(int type); } + bool isEnabled(); + cl_device_type deviceType(int type); int getPlatformCount(); diff --git a/include/occa/mode/openmp/registration.hpp b/include/occa/mode/openmp/registration.hpp index 036e32a49..59db6acf7 100644 --- a/include/occa/mode/openmp/registration.hpp +++ b/include/occa/mode/openmp/registration.hpp @@ -37,7 +37,7 @@ namespace occa { public: modeInfo(); - void init(); + bool init(); void setupProperties(); }; diff --git a/include/occa/mode/serial/registration.hpp b/include/occa/mode/serial/registration.hpp index 442cc673f..8b690dd09 100644 --- a/include/occa/mode/serial/registration.hpp +++ b/include/occa/mode/serial/registration.hpp @@ -37,7 +37,7 @@ namespace occa { public: modeInfo(); - void init(); + bool init(); styling::section& getDescription(); }; diff --git a/src/mode/cuda/registration.cpp b/src/mode/cuda/registration.cpp index 7905fa868..fa8d5caf7 100644 --- a/src/mode/cuda/registration.cpp +++ b/src/mode/cuda/registration.cpp @@ -31,8 +31,9 @@ namespace occa { namespace cuda { modeInfo::modeInfo() {} - void modeInfo::init() { + bool modeInfo::init() { cuda::init(); + return true; } styling::section& modeInfo::getDescription() { diff --git a/src/mode/hip/registration.cpp b/src/mode/hip/registration.cpp index 7154985f6..807ce2642 100644 --- a/src/mode/hip/registration.cpp +++ b/src/mode/hip/registration.cpp @@ -31,8 +31,9 @@ namespace occa { namespace hip { modeInfo::modeInfo() {} - void modeInfo::init() { + bool modeInfo::init() { hip::init(); + return true; } styling::section& modeInfo::getDescription() { diff --git a/src/mode/opencl/registration.cpp b/src/mode/opencl/registration.cpp index 6102d9bf8..784b28234 100644 --- a/src/mode/opencl/registration.cpp +++ b/src/mode/opencl/registration.cpp @@ -24,13 +24,16 @@ #if OCCA_OPENCL_ENABLED +#include #include namespace occa { namespace opencl { modeInfo::modeInfo() {} - void modeInfo::init() {} + bool modeInfo::init() { + return occa::opencl::isEnabled(); + } styling::section& modeInfo::getDescription() { static styling::section section("OpenCL"); diff --git a/src/mode/opencl/utils.cpp b/src/mode/opencl/utils.cpp index cbf4fa54a..9a61ab9df 100644 --- a/src/mode/opencl/utils.cpp +++ b/src/mode/opencl/utils.cpp @@ -63,6 +63,12 @@ namespace occa { } } + bool isEnabled() { + cl_uint platformCount; + cl_int error = clGetPlatformIDs(0, NULL, &platformCount); + return !error; + } + cl_device_type deviceType(int type) { cl_device_type ret = 0; diff --git a/src/mode/openmp/device.cpp b/src/mode/openmp/device.cpp index 71869d3a9..3d442eeba 100644 --- a/src/mode/openmp/device.cpp +++ b/src/mode/openmp/device.cpp @@ -24,8 +24,6 @@ #if OCCA_OPENMP_ENABLED -#include - #include #include #include @@ -34,10 +32,7 @@ namespace occa { namespace openmp { device::device(const occa::properties &properties_) : - serial::device(properties_) { - // Generate an OpenMP library dependency (so it doesn't crash when dlclose()) - omp_get_num_threads(); - } + serial::device(properties_) {} hash_t device::kernelHash(const occa::properties &props) const { return ( diff --git a/src/mode/openmp/registration.cpp b/src/mode/openmp/registration.cpp index c79019e96..0b4ab9fba 100644 --- a/src/mode/openmp/registration.cpp +++ b/src/mode/openmp/registration.cpp @@ -24,13 +24,19 @@ #if OCCA_OPENMP_ENABLED +#include + #include namespace occa { namespace openmp { modeInfo::modeInfo() {} - void modeInfo::init() {} + bool modeInfo::init() { + // Generate an OpenMP library dependency (so it doesn't crash when dlclose()) + omp_get_num_threads(); + return true; + } occa::mode mode("OpenMP"); diff --git a/src/mode/serial/registration.cpp b/src/mode/serial/registration.cpp index d29f37083..285fa2c0c 100644 --- a/src/mode/serial/registration.cpp +++ b/src/mode/serial/registration.cpp @@ -26,7 +26,9 @@ namespace occa { namespace serial { modeInfo::modeInfo() {} - void modeInfo::init() {} + bool modeInfo::init() { + return true; + } styling::section& modeInfo::getDescription() { static styling::section section("CPU(s)");