|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <cublasXt.h> |
| 18 | +#include <cublas_v2.h> |
| 19 | +#include <cuda.h> |
| 20 | +#if CUDA_VERSION >= 12030 && defined(__linux__) |
| 21 | +#include <cublas_api.h> |
| 22 | +#endif |
| 23 | + |
| 24 | +#include <mutex> // NOLINT |
| 25 | +#include <type_traits> |
| 26 | + |
| 27 | +#include "paddle/phi/backends/dynload/dynamic_loader.h" |
| 28 | +#include "paddle/phi/common/port.h" |
| 29 | + |
| 30 | +namespace phi { |
| 31 | +namespace dynload { |
| 32 | + |
| 33 | +extern std::once_flag cublas_dso_flag; |
| 34 | +extern void* cublas_dso_handle; |
| 35 | + |
| 36 | +/** |
| 37 | + * The following macro definition can generate structs |
| 38 | + * (for each function) to dynamic load cublas routine |
| 39 | + * via operator overloading. |
| 40 | + * |
| 41 | + * note: default dynamic linked libs |
| 42 | + */ |
| 43 | +#define DECLARE_DYNAMIC_LOAD_CUBLAS_WRAP(__name) \ |
| 44 | + struct DynLoad__##__name { \ |
| 45 | + template <typename... Args> \ |
| 46 | + inline auto operator()(Args... args) -> DECLARE_TYPE(__name, args...) { \ |
| 47 | + using cublas_func = \ |
| 48 | + decltype(::__name(std::declval<Args>()...)) (*)(Args...); \ |
| 49 | + std::call_once(cublas_dso_flag, []() { \ |
| 50 | + cublas_dso_handle = phi::dynload::GetCublasDsoHandle(); \ |
| 51 | + }); \ |
| 52 | + std::string replaced_name = #__name; \ |
| 53 | + replaced_name = replaced_name.replace(0, 2, "mc"); \ |
| 54 | + int index = replaced_name.find("_", 0); \ |
| 55 | + if (index != -1) replaced_name = replaced_name.substr(0, index); \ |
| 56 | + static void* p_##__name = \ |
| 57 | + dlsym(cublas_dso_handle, replaced_name.c_str()); \ |
| 58 | + return reinterpret_cast<cublas_func>(p_##__name)(args...); \ |
| 59 | + } \ |
| 60 | + }; \ |
| 61 | + extern DynLoad__##__name __name |
| 62 | + |
| 63 | +#define CUBLAS_BLAS_ROUTINE_EACH(__macro) \ |
| 64 | + __macro(cublasSaxpy_v2); \ |
| 65 | + __macro(cublasDaxpy_v2); \ |
| 66 | + __macro(cublasCaxpy_v2); \ |
| 67 | + __macro(cublasZaxpy_v2); \ |
| 68 | + __macro(cublasSscal_v2); \ |
| 69 | + __macro(cublasDscal_v2); \ |
| 70 | + __macro(cublasScopy_v2); \ |
| 71 | + __macro(cublasDcopy_v2); \ |
| 72 | + __macro(cublasSgemv_v2); \ |
| 73 | + __macro(cublasDgemv_v2); \ |
| 74 | + __macro(cublasCgemv_v2); \ |
| 75 | + __macro(cublasZgemv_v2); \ |
| 76 | + __macro(cublasSgemm_v2); \ |
| 77 | + __macro(cublasDgemm_v2); \ |
| 78 | + __macro(cublasCgemm_v2); \ |
| 79 | + __macro(cublasZgemm_v2); \ |
| 80 | + __macro(cublasHgemm); \ |
| 81 | + __macro(cublasSgemmEx); \ |
| 82 | + __macro(cublasSgeam); \ |
| 83 | + __macro(cublasDgeam); \ |
| 84 | + __macro(cublasStrsm_v2); \ |
| 85 | + __macro(cublasDtrsm_v2); \ |
| 86 | + __macro(cublasCtrsm_v2); \ |
| 87 | + __macro(cublasZtrsm_v2); \ |
| 88 | + __macro(cublasCreate_v2); \ |
| 89 | + __macro(cublasDestroy_v2); \ |
| 90 | + __macro(cublasSetStream_v2); \ |
| 91 | + __macro(cublasSetPointerMode_v2); \ |
| 92 | + __macro(cublasGetPointerMode_v2); \ |
| 93 | + __macro(cublasSgemmBatched); \ |
| 94 | + __macro(cublasDgemmBatched); \ |
| 95 | + __macro(cublasCgemmBatched); \ |
| 96 | + __macro(cublasZgemmBatched); \ |
| 97 | + __macro(cublasStrsmBatched); \ |
| 98 | + __macro(cublasDtrsmBatched); \ |
| 99 | + __macro(cublasCtrsmBatched); \ |
| 100 | + __macro(cublasZtrsmBatched); \ |
| 101 | + __macro(cublasSgetrfBatched); \ |
| 102 | + __macro(cublasSgetriBatched); \ |
| 103 | + __macro(cublasDgetrfBatched); \ |
| 104 | + __macro(cublasDgetriBatched); \ |
| 105 | + __macro(cublasCgetrfBatched); \ |
| 106 | + __macro(cublasCgetriBatched); \ |
| 107 | + __macro(cublasZgetrfBatched); \ |
| 108 | + __macro(cublasZgetriBatched); \ |
| 109 | + __macro(cublasSmatinvBatched); \ |
| 110 | + __macro(cublasDmatinvBatched); \ |
| 111 | + __macro(cublasCmatinvBatched); \ |
| 112 | + __macro(cublasZmatinvBatched); \ |
| 113 | + __macro(cublasSgetrsBatched); \ |
| 114 | + __macro(cublasDgetrsBatched); \ |
| 115 | + __macro(cublasSdot_v2); \ |
| 116 | + __macro(cublasDdot_v2); \ |
| 117 | + __macro(cublasCdotc_v2); \ |
| 118 | + __macro(cublasZdotc_v2); \ |
| 119 | + __macro(cublasCdotu_v2); \ |
| 120 | + __macro(cublasZdotu_v2); \ |
| 121 | + __macro(cublasDotEx); \ |
| 122 | + __macro(cublasGemmEx); \ |
| 123 | + __macro(cublasSgemmStridedBatched); \ |
| 124 | + __macro(cublasDgemmStridedBatched); \ |
| 125 | + __macro(cublasCgemmStridedBatched); \ |
| 126 | + __macro(cublasZgemmStridedBatched); \ |
| 127 | + __macro(cublasHgemmStridedBatched); \ |
| 128 | + __macro(cublasSetMathMode); \ |
| 129 | + __macro(cublasGetMathMode); \ |
| 130 | + __macro(cublasCgeam); \ |
| 131 | + __macro(cublasZgeam); \ |
| 132 | + __macro(cublasGemmBatchedEx); \ |
| 133 | + __macro(cublasGemmStridedBatchedEx); |
| 134 | + |
| 135 | +CUBLAS_BLAS_ROUTINE_EACH(DECLARE_DYNAMIC_LOAD_CUBLAS_WRAP) |
| 136 | + |
| 137 | +#if CUDA_VERSION >= 12030 && defined(__linux__) |
| 138 | +#define CUBLAS_BLAS_ROUTINE_EACH_R5(__macro) \ |
| 139 | + __macro(cublasGemmStridedBatchedEx_64); \ |
| 140 | + __macro(cublasGemmEx_64); \ |
| 141 | + __macro(cublasSgemmEx_64); |
| 142 | + |
| 143 | +CUBLAS_BLAS_ROUTINE_EACH_R5(DECLARE_DYNAMIC_LOAD_CUBLAS_WRAP) |
| 144 | +#endif |
| 145 | + |
| 146 | +#undef DECLARE_DYNAMIC_LOAD_CUBLAS_WRAP |
| 147 | +} // namespace dynload |
| 148 | +} // namespace phi |
0 commit comments