From 87ca8bcba27523d26539c2dc14ce9693fcc3fa26 Mon Sep 17 00:00:00 2001 From: Jiafeng-Liu Date: Thu, 31 Mar 2022 21:52:36 +0800 Subject: [PATCH 1/9] cusparse loaded --- taichi/backends/cuda/cuda_context.cpp | 3 ++- taichi/backends/cuda/cuda_context.h | 2 ++ taichi/backends/cuda/cuda_driver.cpp | 30 +++++++++++++++++++++++++++ taichi/backends/cuda/cuda_driver.h | 11 ++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/taichi/backends/cuda/cuda_context.cpp b/taichi/backends/cuda/cuda_context.cpp index a138c3d555395..892a17b50e6de 100644 --- a/taichi/backends/cuda/cuda_context.cpp +++ b/taichi/backends/cuda/cuda_context.cpp @@ -13,7 +13,8 @@ TLANG_NAMESPACE_BEGIN CUDAContext::CUDAContext() - : profiler_(nullptr), driver_(CUDADriver::get_instance_without_context()) { + : profiler_(nullptr), driver_(CUDADriver::get_instance_without_context()), + cusparse_driver_(CUSPARSEDriver::get_instance()) { // CUDA initialization dev_count_ = 0; driver_.init(0); diff --git a/taichi/backends/cuda/cuda_context.h b/taichi/backends/cuda/cuda_context.h index 69a02adf6f082..31c14b47f695c 100644 --- a/taichi/backends/cuda/cuda_context.h +++ b/taichi/backends/cuda/cuda_context.h @@ -15,6 +15,7 @@ TLANG_NAMESPACE_BEGIN // cases such as unit testing where many Taichi programs are created/destroyed. class CUDADriver; +class CUSPARSEDriver; class CUDAContext { private: @@ -26,6 +27,7 @@ class CUDAContext { std::mutex lock_; KernelProfilerBase *profiler_; CUDADriver &driver_; + CUSPARSEDriver & cusparse_driver_; bool debug_; public: diff --git a/taichi/backends/cuda/cuda_driver.cpp b/taichi/backends/cuda/cuda_driver.cpp index e01e1c0fe5bf2..c61bcc05ffc65 100644 --- a/taichi/backends/cuda/cuda_driver.cpp +++ b/taichi/backends/cuda/cuda_driver.cpp @@ -78,4 +78,34 @@ CUDADriver &CUDADriver::get_instance() { return get_instance_without_context(); } + +CUSPARSEDriver::CUSPARSEDriver() { + auto disabled_by_env_ = (get_environ_config("TI_ENABLE_CUDA", 1) == 0); + if (disabled_by_env_) { + TI_TRACE("CUDA driver disabled by enviroment variable \"TI_ENABLE_CUDA\"."); + return; + } + +#if defined(TI_PLATFORM_LINUX) + auto loader_ = std::make_unique("libcusparse.so"); +#elif defined(TI_PLATFORM_WINDOWS) + loader_ = std::make_unique("cusparse.dll"); +#else + static_assert(false, "Taichi CUDA driver supports only Windows and Linux."); +#endif + + if (!loader_->loaded()) { + TI_WARN("CUSPARSE driver not found."); + return; + } + else { + TI_TRACE("cusparse loaded!"); + } +} + +CUSPARSEDriver& CUSPARSEDriver::get_instance() { + static CUSPARSEDriver* instance = new CUSPARSEDriver(); + return *instance; +} + TLANG_NAMESPACE_END diff --git a/taichi/backends/cuda/cuda_driver.h b/taichi/backends/cuda/cuda_driver.h index 40e3eff765c63..87b11e1479b7a 100644 --- a/taichi/backends/cuda/cuda_driver.h +++ b/taichi/backends/cuda/cuda_driver.h @@ -127,4 +127,15 @@ class CUDADriver { bool cuda_version_valid_{false}; }; +class CUSPARSEDriver { +public: + static CUSPARSEDriver &get_instance(); + +private: + CUSPARSEDriver* instance; + CUSPARSEDriver(); + +}; + + TLANG_NAMESPACE_END From e3b1c7c5a7a3ab3961ca94ee1e8d0caf980e46e7 Mon Sep 17 00:00:00 2001 From: Jiafeng-Liu Date: Wed, 4 May 2022 15:50:51 +0800 Subject: [PATCH 2/9] load cusolver --- taichi/backends/cuda/cuda_context.cpp | 3 ++- taichi/backends/cuda/cuda_context.h | 2 ++ taichi/backends/cuda/cuda_driver.cpp | 35 +++++++++++++++++++++++++-- taichi/backends/cuda/cuda_driver.h | 15 +++++++++++- 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/taichi/backends/cuda/cuda_context.cpp b/taichi/backends/cuda/cuda_context.cpp index 892a17b50e6de..c4cb302570144 100644 --- a/taichi/backends/cuda/cuda_context.cpp +++ b/taichi/backends/cuda/cuda_context.cpp @@ -14,7 +14,8 @@ TLANG_NAMESPACE_BEGIN CUDAContext::CUDAContext() : profiler_(nullptr), driver_(CUDADriver::get_instance_without_context()), - cusparse_driver_(CUSPARSEDriver::get_instance()) { + cusparse_driver_(CUSPARSEDriver::get_instance()), + cusolver_driver_(CUSOLVERDriver::get_instance()) { // CUDA initialization dev_count_ = 0; driver_.init(0); diff --git a/taichi/backends/cuda/cuda_context.h b/taichi/backends/cuda/cuda_context.h index 31c14b47f695c..4a8cca6755799 100644 --- a/taichi/backends/cuda/cuda_context.h +++ b/taichi/backends/cuda/cuda_context.h @@ -16,6 +16,7 @@ TLANG_NAMESPACE_BEGIN class CUDADriver; class CUSPARSEDriver; +class CUSOLVERDriver; class CUDAContext { private: @@ -28,6 +29,7 @@ class CUDAContext { KernelProfilerBase *profiler_; CUDADriver &driver_; CUSPARSEDriver & cusparse_driver_; + CUSOLVERDriver & cusolver_driver_; bool debug_; public: diff --git a/taichi/backends/cuda/cuda_driver.cpp b/taichi/backends/cuda/cuda_driver.cpp index c61bcc05ffc65..5804f7f956758 100644 --- a/taichi/backends/cuda/cuda_driver.cpp +++ b/taichi/backends/cuda/cuda_driver.cpp @@ -80,6 +80,7 @@ CUDADriver &CUDADriver::get_instance() { CUSPARSEDriver::CUSPARSEDriver() { + // TODO: enable cusparse and cusolver flag env variable. auto disabled_by_env_ = (get_environ_config("TI_ENABLE_CUDA", 1) == 0); if (disabled_by_env_) { TI_TRACE("CUDA driver disabled by enviroment variable \"TI_ENABLE_CUDA\"."); @@ -87,7 +88,7 @@ CUSPARSEDriver::CUSPARSEDriver() { } #if defined(TI_PLATFORM_LINUX) - auto loader_ = std::make_unique("libcusparse.so"); + loader_ = std::make_unique("libcusparse.so"); #elif defined(TI_PLATFORM_WINDOWS) loader_ = std::make_unique("cusparse.dll"); #else @@ -95,7 +96,7 @@ CUSPARSEDriver::CUSPARSEDriver() { #endif if (!loader_->loaded()) { - TI_WARN("CUSPARSE driver not found."); + TI_WARN("CUSPARSE lib not found."); return; } else { @@ -108,4 +109,34 @@ CUSPARSEDriver& CUSPARSEDriver::get_instance() { return *instance; } +CUSOLVERDriver::CUSOLVERDriver() { + // TODO: enable cusparse and cusolver flag env variable. + auto disabled_by_env_ = (get_environ_config("TI_ENABLE_CUDA", 1) == 0); + if (disabled_by_env_) { + TI_TRACE("CUDA driver disabled by enviroment variable \"TI_ENABLE_CUDA\"."); + return; + } + +#if defined(TI_PLATFORM_LINUX) + loader_ = std::make_unique("libcusolver.so"); +#elif defined(TI_PLATFORM_WINDOWS) + loader_ = std::make_unique("cusolver.dll"); +#else + static_assert(false, "Taichi CUDA driver supports only Windows and Linux."); +#endif + + if (!loader_->loaded()) { + TI_WARN("cusolver lib not found."); + return; + } + else { + TI_TRACE("cusolver loaded!"); + } +} + +CUSOLVERDriver& CUSOLVERDriver::get_instance() { + static CUSOLVERDriver* instance = new CUSOLVERDriver(); + return *instance; +} + TLANG_NAMESPACE_END diff --git a/taichi/backends/cuda/cuda_driver.h b/taichi/backends/cuda/cuda_driver.h index 87b11e1479b7a..770b98db743a0 100644 --- a/taichi/backends/cuda/cuda_driver.h +++ b/taichi/backends/cuda/cuda_driver.h @@ -127,15 +127,28 @@ class CUDADriver { bool cuda_version_valid_{false}; }; + + class CUSPARSEDriver { public: static CUSPARSEDriver &get_instance(); private: - CUSPARSEDriver* instance; + std::unique_ptr loader_; CUSPARSEDriver(); }; +class CUSOLVERDriver { +public: + static CUSOLVERDriver &get_instance(); + +private: + std::unique_ptr loader_; + CUSOLVERDriver(); + +}; + + TLANG_NAMESPACE_END From 1b8a8c755e2d10b4a24cc6256976ac52ca29b495 Mon Sep 17 00:00:00 2001 From: Jiafeng-Liu Date: Wed, 4 May 2022 16:55:00 +0800 Subject: [PATCH 3/9] add driver base class --- taichi/backends/cuda/cuda_driver.cpp | 36 ++++++++-------------------- taichi/backends/cuda/cuda_driver.h | 14 +++++++---- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/taichi/backends/cuda/cuda_driver.cpp b/taichi/backends/cuda/cuda_driver.cpp index 5804f7f956758..9ebb15af33a82 100644 --- a/taichi/backends/cuda/cuda_driver.cpp +++ b/taichi/backends/cuda/cuda_driver.cpp @@ -78,19 +78,20 @@ CUDADriver &CUDADriver::get_instance() { return get_instance_without_context(); } - -CUSPARSEDriver::CUSPARSEDriver() { +CUDADriverBase::CUDADriverBase() { // TODO: enable cusparse and cusolver flag env variable. auto disabled_by_env_ = (get_environ_config("TI_ENABLE_CUDA", 1) == 0); if (disabled_by_env_) { TI_TRACE("CUDA driver disabled by enviroment variable \"TI_ENABLE_CUDA\"."); return; } +} +void CUDADriverBase::load_lib(std::string lib_linux, std::string lib_windows) { #if defined(TI_PLATFORM_LINUX) - loader_ = std::make_unique("libcusparse.so"); + loader_ = std::make_unique(lib_linux); #elif defined(TI_PLATFORM_WINDOWS) - loader_ = std::make_unique("cusparse.dll"); + loader_ = std::make_unique(lib_windows); #else static_assert(false, "Taichi CUDA driver supports only Windows and Linux."); #endif @@ -104,34 +105,17 @@ CUSPARSEDriver::CUSPARSEDriver() { } } +CUSPARSEDriver::CUSPARSEDriver(){ + load_lib("libcusparse.so", "cusparse.dll"); +} + CUSPARSEDriver& CUSPARSEDriver::get_instance() { static CUSPARSEDriver* instance = new CUSPARSEDriver(); return *instance; } CUSOLVERDriver::CUSOLVERDriver() { - // TODO: enable cusparse and cusolver flag env variable. - auto disabled_by_env_ = (get_environ_config("TI_ENABLE_CUDA", 1) == 0); - if (disabled_by_env_) { - TI_TRACE("CUDA driver disabled by enviroment variable \"TI_ENABLE_CUDA\"."); - return; - } - -#if defined(TI_PLATFORM_LINUX) - loader_ = std::make_unique("libcusolver.so"); -#elif defined(TI_PLATFORM_WINDOWS) - loader_ = std::make_unique("cusolver.dll"); -#else - static_assert(false, "Taichi CUDA driver supports only Windows and Linux."); -#endif - - if (!loader_->loaded()) { - TI_WARN("cusolver lib not found."); - return; - } - else { - TI_TRACE("cusolver loaded!"); - } + load_lib("libcusolver.so", "cusolver.dll"); } CUSOLVERDriver& CUSOLVERDriver::get_instance() { diff --git a/taichi/backends/cuda/cuda_driver.h b/taichi/backends/cuda/cuda_driver.h index 770b98db743a0..e26ab156d297d 100644 --- a/taichi/backends/cuda/cuda_driver.h +++ b/taichi/backends/cuda/cuda_driver.h @@ -127,24 +127,30 @@ class CUDADriver { bool cuda_version_valid_{false}; }; +class CUDADriverBase { +protected: + std::unique_ptr loader_; + CUDADriverBase(); + + void load_lib(std::string lib_linux, std::string lib_windows); + +}; -class CUSPARSEDriver { +class CUSPARSEDriver: protected CUDADriverBase { public: static CUSPARSEDriver &get_instance(); private: - std::unique_ptr loader_; CUSPARSEDriver(); }; -class CUSOLVERDriver { +class CUSOLVERDriver: protected CUDADriverBase { public: static CUSOLVERDriver &get_instance(); private: - std::unique_ptr loader_; CUSOLVERDriver(); }; From e53279ae2965bd8cf2232109837ec4bc5f4931fa Mon Sep 17 00:00:00 2001 From: Jiafeng-Liu Date: Wed, 4 May 2022 17:19:35 +0800 Subject: [PATCH 4/9] update comments --- taichi/backends/cuda/cuda_driver.h | 1 + 1 file changed, 1 insertion(+) diff --git a/taichi/backends/cuda/cuda_driver.h b/taichi/backends/cuda/cuda_driver.h index e26ab156d297d..b63887205fda2 100644 --- a/taichi/backends/cuda/cuda_driver.h +++ b/taichi/backends/cuda/cuda_driver.h @@ -96,6 +96,7 @@ class CUDADriverFunction { }; class CUDADriver { + // TODO: make CUDADriver a derived class of CUDADriverBase. public: #define PER_CUDA_FUNCTION(name, symbol_name, ...) \ CUDADriverFunction<__VA_ARGS__> name; From 4082d57d7b26e350a1c29a29db2075827dde74f3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 May 2022 09:19:57 +0000 Subject: [PATCH 5/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- taichi/backends/cuda/cuda_context.cpp | 7 ++++--- taichi/backends/cuda/cuda_context.h | 4 ++-- taichi/backends/cuda/cuda_driver.cpp | 13 ++++++------- taichi/backends/cuda/cuda_driver.h | 20 +++++++------------- 4 files changed, 19 insertions(+), 25 deletions(-) diff --git a/taichi/backends/cuda/cuda_context.cpp b/taichi/backends/cuda/cuda_context.cpp index c4cb302570144..f1cb1ba5bba3c 100644 --- a/taichi/backends/cuda/cuda_context.cpp +++ b/taichi/backends/cuda/cuda_context.cpp @@ -13,9 +13,10 @@ TLANG_NAMESPACE_BEGIN CUDAContext::CUDAContext() - : profiler_(nullptr), driver_(CUDADriver::get_instance_without_context()), - cusparse_driver_(CUSPARSEDriver::get_instance()), - cusolver_driver_(CUSOLVERDriver::get_instance()) { + : profiler_(nullptr), + driver_(CUDADriver::get_instance_without_context()), + cusparse_driver_(CUSPARSEDriver::get_instance()), + cusolver_driver_(CUSOLVERDriver::get_instance()) { // CUDA initialization dev_count_ = 0; driver_.init(0); diff --git a/taichi/backends/cuda/cuda_context.h b/taichi/backends/cuda/cuda_context.h index 4a8cca6755799..0834de1120d74 100644 --- a/taichi/backends/cuda/cuda_context.h +++ b/taichi/backends/cuda/cuda_context.h @@ -28,8 +28,8 @@ class CUDAContext { std::mutex lock_; KernelProfilerBase *profiler_; CUDADriver &driver_; - CUSPARSEDriver & cusparse_driver_; - CUSOLVERDriver & cusolver_driver_; + CUSPARSEDriver &cusparse_driver_; + CUSOLVERDriver &cusolver_driver_; bool debug_; public: diff --git a/taichi/backends/cuda/cuda_driver.cpp b/taichi/backends/cuda/cuda_driver.cpp index 9ebb15af33a82..d703337421fee 100644 --- a/taichi/backends/cuda/cuda_driver.cpp +++ b/taichi/backends/cuda/cuda_driver.cpp @@ -99,18 +99,17 @@ void CUDADriverBase::load_lib(std::string lib_linux, std::string lib_windows) { if (!loader_->loaded()) { TI_WARN("CUSPARSE lib not found."); return; - } - else { + } else { TI_TRACE("cusparse loaded!"); } } -CUSPARSEDriver::CUSPARSEDriver(){ +CUSPARSEDriver::CUSPARSEDriver() { load_lib("libcusparse.so", "cusparse.dll"); } -CUSPARSEDriver& CUSPARSEDriver::get_instance() { - static CUSPARSEDriver* instance = new CUSPARSEDriver(); +CUSPARSEDriver &CUSPARSEDriver::get_instance() { + static CUSPARSEDriver *instance = new CUSPARSEDriver(); return *instance; } @@ -118,8 +117,8 @@ CUSOLVERDriver::CUSOLVERDriver() { load_lib("libcusolver.so", "cusolver.dll"); } -CUSOLVERDriver& CUSOLVERDriver::get_instance() { - static CUSOLVERDriver* instance = new CUSOLVERDriver(); +CUSOLVERDriver &CUSOLVERDriver::get_instance() { + static CUSOLVERDriver *instance = new CUSOLVERDriver(); return *instance; } diff --git a/taichi/backends/cuda/cuda_driver.h b/taichi/backends/cuda/cuda_driver.h index b63887205fda2..6d0469fb1f415 100644 --- a/taichi/backends/cuda/cuda_driver.h +++ b/taichi/backends/cuda/cuda_driver.h @@ -129,33 +129,27 @@ class CUDADriver { }; class CUDADriverBase { - -protected: + protected: std::unique_ptr loader_; CUDADriverBase(); void load_lib(std::string lib_linux, std::string lib_windows); - }; -class CUSPARSEDriver: protected CUDADriverBase { -public: +class CUSPARSEDriver : protected CUDADriverBase { + public: static CUSPARSEDriver &get_instance(); -private: + private: CUSPARSEDriver(); - }; -class CUSOLVERDriver: protected CUDADriverBase { -public: +class CUSOLVERDriver : protected CUDADriverBase { + public: static CUSOLVERDriver &get_instance(); -private: + private: CUSOLVERDriver(); - }; - - TLANG_NAMESPACE_END From 39e0563253582e838012ef2f7c82b1cf62c1692a Mon Sep 17 00:00:00 2001 From: Jiafeng-Liu Date: Tue, 17 May 2022 11:08:58 +0800 Subject: [PATCH 6/9] make CUDADriver a derived class from CUDADriverBase --- taichi/backends/cuda/cuda_driver.cpp | 60 ++++++++++++++++------------ taichi/backends/cuda/cuda_driver.h | 30 +++++++++----- 2 files changed, 54 insertions(+), 36 deletions(-) diff --git a/taichi/backends/cuda/cuda_driver.cpp b/taichi/backends/cuda/cuda_driver.cpp index d703337421fee..c4a002e6f8f20 100644 --- a/taichi/backends/cuda/cuda_driver.cpp +++ b/taichi/backends/cuda/cuda_driver.cpp @@ -20,24 +20,26 @@ bool CUDADriver::detected() { } CUDADriver::CUDADriver() { - disabled_by_env_ = (get_environ_config("TI_ENABLE_CUDA", 1) == 0); - if (disabled_by_env_) { - TI_TRACE("CUDA driver disabled by enviroment variable \"TI_ENABLE_CUDA\"."); - return; - } - -#if defined(TI_PLATFORM_LINUX) - loader_ = std::make_unique("libcuda.so"); -#elif defined(TI_PLATFORM_WINDOWS) - loader_ = std::make_unique("nvcuda.dll"); -#else - static_assert(false, "Taichi CUDA driver supports only Windows and Linux."); -#endif - - if (!loader_->loaded()) { - TI_WARN("CUDA driver not found."); - return; - } +// disabled_by_env_ = (get_environ_config("TI_ENABLE_CUDA", 1) == 0); +// if (disabled_by_env_) { +// TI_TRACE("CUDA driver disabled by enviroment variable \"TI_ENABLE_CUDA\"."); +// return; +// } + +// #if defined(TI_PLATFORM_LINUX) +// loader_ = std::make_unique("libcuda.so"); +// #elif defined(TI_PLATFORM_WINDOWS) +// loader_ = std::make_unique("nvcuda.dll"); +// #else +// static_assert(false, "Taichi CUDA driver supports only Windows and Linux."); +// #endif + + // if (!loader_->loaded()) { + // TI_WARN("CUDA driver not found."); + // return; + // } + + load_lib("libcuda.so", "nvcuda.dll"); loader_->load_function("cuGetErrorName", get_error_name); loader_->load_function("cuGetErrorString", get_error_string); @@ -80,7 +82,7 @@ CUDADriver &CUDADriver::get_instance() { CUDADriverBase::CUDADriverBase() { // TODO: enable cusparse and cusolver flag env variable. - auto disabled_by_env_ = (get_environ_config("TI_ENABLE_CUDA", 1) == 0); + disabled_by_env_ = (get_environ_config("TI_ENABLE_CUDA", 1) == 0); if (disabled_by_env_) { TI_TRACE("CUDA driver disabled by enviroment variable \"TI_ENABLE_CUDA\"."); return; @@ -89,18 +91,26 @@ CUDADriverBase::CUDADriverBase() { void CUDADriverBase::load_lib(std::string lib_linux, std::string lib_windows) { #if defined(TI_PLATFORM_LINUX) - loader_ = std::make_unique(lib_linux); + auto lib_name = lib_linux; #elif defined(TI_PLATFORM_WINDOWS) - loader_ = std::make_unique(lib_windows); + auto lib_name = lib_windows; #else static_assert(false, "Taichi CUDA driver supports only Windows and Linux."); #endif - +// #if defined(TI_PLATFORM_LINUX) +// loader_ = std::make_unique(lib_linux); +// #elif defined(TI_PLATFORM_WINDOWS) +// loader_ = std::make_unique(lib_windows); +// #else +// static_assert(false, "Taichi CUDA driver supports only Windows and Linux."); +// #endif + loader_ = std::make_unique(lib_name); if (!loader_->loaded()) { - TI_WARN("CUSPARSE lib not found."); + TI_WARN("{} lib not found.", lib_name); return; - } else { - TI_TRACE("cusparse loaded!"); + } + else { + TI_TRACE("{} loaded!", lib_name); } } diff --git a/taichi/backends/cuda/cuda_driver.h b/taichi/backends/cuda/cuda_driver.h index 6d0469fb1f415..5b65a8671c8d0 100644 --- a/taichi/backends/cuda/cuda_driver.h +++ b/taichi/backends/cuda/cuda_driver.h @@ -95,7 +95,22 @@ class CUDADriverFunction { std::mutex *driver_lock_{nullptr}; }; -class CUDADriver { + +class CUDADriverBase { + +public: + ~CUDADriverBase() = default; + +protected: + std::unique_ptr loader_; + CUDADriverBase(); + + void load_lib(std::string lib_linux, std::string lib_windows); + + bool disabled_by_env_{false}; +}; + +class CUDADriver: protected CUDADriverBase { // TODO: make CUDADriver a derived class of CUDADriverBase. public: #define PER_CUDA_FUNCTION(name, symbol_name, ...) \ @@ -111,7 +126,7 @@ class CUDADriver { bool detected(); - ~CUDADriver() = default; + // ~CUDADriver() = default; static CUDADriver &get_instance(); @@ -120,21 +135,14 @@ class CUDADriver { private: CUDADriver(); - std::unique_ptr loader_; + // std::unique_ptr loader_; std::mutex lock_; - bool disabled_by_env_{false}; + // bool disabled_by_env_{false}; bool cuda_version_valid_{false}; }; -class CUDADriverBase { - protected: - std::unique_ptr loader_; - CUDADriverBase(); - - void load_lib(std::string lib_linux, std::string lib_windows); -}; class CUSPARSEDriver : protected CUDADriverBase { public: From 933589eb2ac25d83af896148742c12ce3223ea4e Mon Sep 17 00:00:00 2001 From: Jiafeng-Liu Date: Tue, 17 May 2022 13:44:46 +0800 Subject: [PATCH 7/9] clean code --- taichi/backends/cuda/cuda_driver.cpp | 27 +-------------------------- taichi/backends/cuda/cuda_driver.h | 12 ++++-------- 2 files changed, 5 insertions(+), 34 deletions(-) diff --git a/taichi/backends/cuda/cuda_driver.cpp b/taichi/backends/cuda/cuda_driver.cpp index c4a002e6f8f20..9e8ec44fe6d60 100644 --- a/taichi/backends/cuda/cuda_driver.cpp +++ b/taichi/backends/cuda/cuda_driver.cpp @@ -20,25 +20,6 @@ bool CUDADriver::detected() { } CUDADriver::CUDADriver() { -// disabled_by_env_ = (get_environ_config("TI_ENABLE_CUDA", 1) == 0); -// if (disabled_by_env_) { -// TI_TRACE("CUDA driver disabled by enviroment variable \"TI_ENABLE_CUDA\"."); -// return; -// } - -// #if defined(TI_PLATFORM_LINUX) -// loader_ = std::make_unique("libcuda.so"); -// #elif defined(TI_PLATFORM_WINDOWS) -// loader_ = std::make_unique("nvcuda.dll"); -// #else -// static_assert(false, "Taichi CUDA driver supports only Windows and Linux."); -// #endif - - // if (!loader_->loaded()) { - // TI_WARN("CUDA driver not found."); - // return; - // } - load_lib("libcuda.so", "nvcuda.dll"); loader_->load_function("cuGetErrorName", get_error_name); @@ -97,13 +78,7 @@ void CUDADriverBase::load_lib(std::string lib_linux, std::string lib_windows) { #else static_assert(false, "Taichi CUDA driver supports only Windows and Linux."); #endif -// #if defined(TI_PLATFORM_LINUX) -// loader_ = std::make_unique(lib_linux); -// #elif defined(TI_PLATFORM_WINDOWS) -// loader_ = std::make_unique(lib_windows); -// #else -// static_assert(false, "Taichi CUDA driver supports only Windows and Linux."); -// #endif + loader_ = std::make_unique(lib_name); if (!loader_->loaded()) { TI_WARN("{} lib not found.", lib_name); diff --git a/taichi/backends/cuda/cuda_driver.h b/taichi/backends/cuda/cuda_driver.h index 5b65a8671c8d0..b29e32d52c834 100644 --- a/taichi/backends/cuda/cuda_driver.h +++ b/taichi/backends/cuda/cuda_driver.h @@ -111,7 +111,6 @@ class CUDADriverBase { }; class CUDADriver: protected CUDADriverBase { - // TODO: make CUDADriver a derived class of CUDADriverBase. public: #define PER_CUDA_FUNCTION(name, symbol_name, ...) \ CUDADriverFunction<__VA_ARGS__> name; @@ -126,8 +125,6 @@ class CUDADriver: protected CUDADriverBase { bool detected(); - // ~CUDADriver() = default; - static CUDADriver &get_instance(); static CUDADriver &get_instance_without_context(); @@ -135,25 +132,24 @@ class CUDADriver: protected CUDADriverBase { private: CUDADriver(); - // std::unique_ptr loader_; - std::mutex lock_; - // bool disabled_by_env_{false}; bool cuda_version_valid_{false}; }; -class CUSPARSEDriver : protected CUDADriverBase { +class CUSPARSEDriver: protected CUDADriverBase { public: + // TODO: Add cusparse function APIs static CUSPARSEDriver &get_instance(); private: CUSPARSEDriver(); }; -class CUSOLVERDriver : protected CUDADriverBase { +class CUSOLVERDriver: protected CUDADriverBase { public: + // TODO: Add cusolver function APIs static CUSOLVERDriver &get_instance(); private: From 8d468266854d99a6c8cee68f298925279a338850 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 May 2022 05:52:34 +0000 Subject: [PATCH 8/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- taichi/backends/cuda/cuda_driver.cpp | 3 +-- taichi/backends/cuda/cuda_driver.h | 13 +++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/taichi/backends/cuda/cuda_driver.cpp b/taichi/backends/cuda/cuda_driver.cpp index 9e8ec44fe6d60..17aec896722b1 100644 --- a/taichi/backends/cuda/cuda_driver.cpp +++ b/taichi/backends/cuda/cuda_driver.cpp @@ -83,8 +83,7 @@ void CUDADriverBase::load_lib(std::string lib_linux, std::string lib_windows) { if (!loader_->loaded()) { TI_WARN("{} lib not found.", lib_name); return; - } - else { + } else { TI_TRACE("{} loaded!", lib_name); } } diff --git a/taichi/backends/cuda/cuda_driver.h b/taichi/backends/cuda/cuda_driver.h index b29e32d52c834..c0569d05402e2 100644 --- a/taichi/backends/cuda/cuda_driver.h +++ b/taichi/backends/cuda/cuda_driver.h @@ -95,13 +95,11 @@ class CUDADriverFunction { std::mutex *driver_lock_{nullptr}; }; - class CUDADriverBase { - -public: + public: ~CUDADriverBase() = default; -protected: + protected: std::unique_ptr loader_; CUDADriverBase(); @@ -110,7 +108,7 @@ class CUDADriverBase { bool disabled_by_env_{false}; }; -class CUDADriver: protected CUDADriverBase { +class CUDADriver : protected CUDADriverBase { public: #define PER_CUDA_FUNCTION(name, symbol_name, ...) \ CUDADriverFunction<__VA_ARGS__> name; @@ -137,8 +135,7 @@ class CUDADriver: protected CUDADriverBase { bool cuda_version_valid_{false}; }; - -class CUSPARSEDriver: protected CUDADriverBase { +class CUSPARSEDriver : protected CUDADriverBase { public: // TODO: Add cusparse function APIs static CUSPARSEDriver &get_instance(); @@ -147,7 +144,7 @@ class CUSPARSEDriver: protected CUDADriverBase { CUSPARSEDriver(); }; -class CUSOLVERDriver: protected CUDADriverBase { +class CUSOLVERDriver : protected CUDADriverBase { public: // TODO: Add cusolver function APIs static CUSOLVERDriver &get_instance(); From 222abeffeade6eac5b8c0f07453be0fcb1735721 Mon Sep 17 00:00:00 2001 From: Jiafeng-Liu Date: Tue, 17 May 2022 14:56:53 +0800 Subject: [PATCH 9/9] clean code --- taichi/backends/cuda/cuda_driver.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/taichi/backends/cuda/cuda_driver.cpp b/taichi/backends/cuda/cuda_driver.cpp index 17aec896722b1..16370be1543ad 100644 --- a/taichi/backends/cuda/cuda_driver.cpp +++ b/taichi/backends/cuda/cuda_driver.cpp @@ -62,7 +62,6 @@ CUDADriver &CUDADriver::get_instance() { } CUDADriverBase::CUDADriverBase() { - // TODO: enable cusparse and cusolver flag env variable. disabled_by_env_ = (get_environ_config("TI_ENABLE_CUDA", 1) == 0); if (disabled_by_env_) { TI_TRACE("CUDA driver disabled by enviroment variable \"TI_ENABLE_CUDA\".");