From 7e18106a29d4ebcfdb921994901fe0e9f23dfb94 Mon Sep 17 00:00:00 2001 From: Liu-xiandong <85323580+Liu-xiandong@users.noreply.github.com> Date: Wed, 15 Sep 2021 17:09:31 +0800 Subject: [PATCH] add nvidia cusparse library, test=develop (#35675) Put Nvidia's cusparse library into paddle. --- paddle/fluid/platform/device_context.h | 1 + paddle/fluid/platform/dynload/CMakeLists.txt | 2 +- paddle/fluid/platform/dynload/cusparse.cc | 31 +++++++++ paddle/fluid/platform/dynload/cusparse.h | 64 +++++++++++++++++++ .../fluid/platform/dynload/dynamic_loader.cc | 17 +++++ .../fluid/platform/dynload/dynamic_loader.h | 1 + 6 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 paddle/fluid/platform/dynload/cusparse.cc create mode 100644 paddle/fluid/platform/dynload/cusparse.h diff --git a/paddle/fluid/platform/device_context.h b/paddle/fluid/platform/device_context.h index 3c5c15a4219..13a1040dd19 100644 --- a/paddle/fluid/platform/device_context.h +++ b/paddle/fluid/platform/device_context.h @@ -24,6 +24,7 @@ limitations under the License. */ #include "paddle/fluid/platform/dynload/cublas.h" #include "paddle/fluid/platform/dynload/cudnn.h" #include "paddle/fluid/platform/dynload/cusolver.h" +#include "paddle/fluid/platform/dynload/cusparse.h" #if !defined(__APPLE__) && defined(PADDLE_WITH_NCCL) #include "paddle/fluid/platform/dynload/nccl.h" #endif diff --git a/paddle/fluid/platform/dynload/CMakeLists.txt b/paddle/fluid/platform/dynload/CMakeLists.txt index 21d9e860745..ac98ff02035 100644 --- a/paddle/fluid/platform/dynload/CMakeLists.txt +++ b/paddle/fluid/platform/dynload/CMakeLists.txt @@ -1,6 +1,6 @@ cc_library(dynamic_loader SRCS dynamic_loader.cc DEPS glog gflags enforce) -list(APPEND CUDA_SRCS cublas.cc cudnn.cc curand.cc cusolver.cc nvtx.cc) +list(APPEND CUDA_SRCS cublas.cc cudnn.cc curand.cc cusolver.cc cusparse.cc nvtx.cc) if (NOT WITH_NV_JETSON) list(APPEND CUDA_SRCS nvjpeg.cc) diff --git a/paddle/fluid/platform/dynload/cusparse.cc b/paddle/fluid/platform/dynload/cusparse.cc new file mode 100644 index 00000000000..2b41da541d9 --- /dev/null +++ b/paddle/fluid/platform/dynload/cusparse.cc @@ -0,0 +1,31 @@ +/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. + +Licensed 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. */ + +#include "paddle/fluid/platform/dynload/cusparse.h" + +namespace paddle { +namespace platform { +namespace dynload { + +std::once_flag cusparse_dso_flag; +void *cusparse_dso_handle; + +#define DEFINE_WRAP(__name) DynLoad__##__name __name + +#ifdef CUSPARSE_ROUTINE_EACH +CUSPARSE_ROUTINE_EACH(DEFINE_WRAP); +#endif +} // namespace dynload +} // namespace platform +} // namespace paddle diff --git a/paddle/fluid/platform/dynload/cusparse.h b/paddle/fluid/platform/dynload/cusparse.h new file mode 100644 index 00000000000..3f41f6978d0 --- /dev/null +++ b/paddle/fluid/platform/dynload/cusparse.h @@ -0,0 +1,64 @@ +/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. + +Licensed 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. */ +#pragma once + +#include +#include +#include // NOLINT + +#include "paddle/fluid/platform/dynload/dynamic_loader.h" +#include "paddle/fluid/platform/port.h" + +namespace paddle { +namespace platform { +namespace dynload { +extern std::once_flag cusparse_dso_flag; +extern void *cusparse_dso_handle; + +#define DECLARE_DYNAMIC_LOAD_CUSPARSE_WRAP(__name) \ + struct DynLoad__##__name { \ + template \ + cusparseStatus_t operator()(Args... args) { \ + using cusparseFunc = decltype(&::__name); \ + std::call_once(cusparse_dso_flag, []() { \ + cusparse_dso_handle = \ + paddle::platform::dynload::GetCusparseDsoHandle(); \ + }); \ + static void *p_##__name = dlsym(cusparse_dso_handle, #__name); \ + return reinterpret_cast(p_##__name)(args...); \ + } \ + }; \ + extern DynLoad__##__name __name + +#if CUDA_VERSION >= 11020 +#define CUSPARSE_ROUTINE_EACH(__macro) \ + __macro(cusparseCreate); \ + __macro(cusparseCreateCsr); \ + __macro(cusparseCreateDnMat); \ + __macro(cusparseSpMM_bufferSize); \ + __macro(cusparseSpMM); \ + __macro(cusparseDestroySpMat); \ + __macro(cusparseDestroyDnMat); \ + __macro(cusparseDestroy); \ + __macro(cusparseSDDMM_bufferSize); \ + __macro(cusparseSDDMM_preprocess); \ + __macro(cusparseSDDMM); + +CUSPARSE_ROUTINE_EACH(DECLARE_DYNAMIC_LOAD_CUSPARSE_WRAP); +#endif + +#undef DECLARE_DYNAMIC_LOAD_CUSPARSE_WRAP +} // namespace dynload +} // namespace platform +} // namespace paddle diff --git a/paddle/fluid/platform/dynload/dynamic_loader.cc b/paddle/fluid/platform/dynload/dynamic_loader.cc index f0a46e0818a..37932600e7a 100644 --- a/paddle/fluid/platform/dynload/dynamic_loader.cc +++ b/paddle/fluid/platform/dynload/dynamic_loader.cc @@ -106,6 +106,9 @@ static constexpr char* win_nvjpeg_lib = static constexpr char* win_cusolver_lib = "cusolver64_" CUDA_VERSION_MAJOR CUDA_VERSION_MINOR ".dll;cusolver64_" CUDA_VERSION_MAJOR ".dll;cusolver64_10.dll"; +static constexpr char* win_cusparse_lib = + "cusparse64_" CUDA_VERSION_MAJOR CUDA_VERSION_MINOR + ".dll;cusparse64_" CUDA_VERSION_MAJOR ".dll;cusparse64_10.dll"; #else static constexpr char* win_curand_lib = "curand64_" CUDA_VERSION_MAJOR CUDA_VERSION_MINOR @@ -116,6 +119,9 @@ static constexpr char* win_nvjpeg_lib = static constexpr char* win_cusolver_lib = "cusolver64_" CUDA_VERSION_MAJOR CUDA_VERSION_MINOR ".dll;cusolver64_" CUDA_VERSION_MAJOR ".dll"; +static constexpr char* win_cusparse_lib = + "cusparse64_" CUDA_VERSION_MAJOR CUDA_VERSION_MINOR + ".dll;cusparse64_" CUDA_VERSION_MAJOR ".dll"; #endif // CUDA_VERSION #endif @@ -358,6 +364,17 @@ void* GetCusolverDsoHandle() { #endif } +void* GetCusparseDsoHandle() { +#if defined(__APPLE__) || defined(__OSX__) + return GetDsoHandleFromSearchPath(FLAGS_cuda_dir, "libcusparse.dylib"); +#elif defined(_WIN32) && defined(PADDLE_WITH_CUDA) + return GetDsoHandleFromSearchPath(FLAGS_cuda_dir, win_cusparse_lib, true, + {cuda_lib_path}); +#else + return GetDsoHandleFromSearchPath(FLAGS_cuda_dir, "libcusparse.so"); +#endif +} + void* GetNVRTCDsoHandle() { #if defined(__APPLE__) || defined(__OSX__) return GetDsoHandleFromSearchPath(FLAGS_cuda_dir, "libnvrtc.dylib", false); diff --git a/paddle/fluid/platform/dynload/dynamic_loader.h b/paddle/fluid/platform/dynload/dynamic_loader.h index 9ab6dca0126..e282c033c44 100644 --- a/paddle/fluid/platform/dynload/dynamic_loader.h +++ b/paddle/fluid/platform/dynload/dynamic_loader.h @@ -31,6 +31,7 @@ void* GetCUPTIDsoHandle(); void* GetCurandDsoHandle(); void* GetNvjpegDsoHandle(); void* GetCusolverDsoHandle(); +void* GetCusparseDsoHandle(); void* GetNVRTCDsoHandle(); void* GetCUDADsoHandle(); void* GetWarpCTCDsoHandle(); -- GitLab