diff --git a/paddle/fluid/platform/device_context.h b/paddle/fluid/platform/device_context.h index 3c5c15a42199ab98b1d0a6dcd6d88bebb7d93bb6..13a1040dd19df23fffba5663e6b77d063704c0ab 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 21d9e8607459a484328c785242f4112cc3951263..ac98ff02035bdb28a929f436de5dab3950a01467 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 0000000000000000000000000000000000000000..2b41da541d9ae03dc93119685ed22f60bd0eb849 --- /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 0000000000000000000000000000000000000000..3f41f6978d059d35fc99a17b5bba52c7a0dfaf89 --- /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 f0a46e0818af748b37e0abce44096fe3cf73b126..37932600e7a7ea8440b15cf03e91ee9ae4d6f05c 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 9ab6dca0126bcbdd02625e2f263ad7c466b5e966..e282c033c4451b4dfeebd41d7df3d97e1492bcb5 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();