-
Couldn't load subscription status.
- Fork 5.9k
port dyload to platform #2722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
QiJune
merged 7 commits into
PaddlePaddle:develop
from
QiJune:feature/port_dyload_to_platform
Jul 4, 2017
Merged
port dyload to platform #2722
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3ba7a73
add dynamic_load
QiJune a30754b
test device_context
QiJune a77fcef
fix cuda compile error
QiJune ed18647
finish test
QiJune 76b7be4
add deps for dyload cc_library
QiJune 9eeabe9
follow comments
QiJune 3567ea6
move to dynload directory
QiJune File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| add_subdirectory(dynload) | ||
|
|
||
| nv_test(cuda_test SRCS cuda_test.cu) | ||
|
|
||
| cc_library(place SRCS place.cc) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| cc_library(dynamic_loader SRCS dynamic_loader.cc DEPS glog gflags) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
|
||
| 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 <cublas_v2.h> | ||
| #include "paddle/platform/dynamic_loader.h" | ||
|
|
||
| namespace paddle { | ||
| namespace platform { | ||
| namespace dynload { | ||
|
|
||
| std::once_flag cublas_dso_flag; | ||
| void *cublas_dso_handle = nullptr; | ||
|
|
||
| /** | ||
| * The following macro definition can generate structs | ||
| * (for each function) to dynamic load cublas routine | ||
| * via operator overloading. | ||
| * | ||
| * note: default dynamic linked libs | ||
| */ | ||
| #ifdef PADDLE_USE_DSO | ||
| #define DYNAMIC_LOAD_CUBLAS_WRAP(__name) \ | ||
| struct DynLoad__##__name { \ | ||
| template <typename... Args> \ | ||
| cublasStatus_t operator()(Args... args) { \ | ||
| typedef cublasStatus_t (*cublasFunc)(Args...); \ | ||
| std::call_once(cublas_dso_flag, \ | ||
| paddle::platform::dynload::GetCublasDsoHandle, \ | ||
| &cublas_dso_handle); \ | ||
| void *p_##__name = dlsym(cublas_dso_handle, #__name); \ | ||
| return reinterpret_cast<cublasFunc>(p_##__name)(args...); \ | ||
| } \ | ||
| } __name; // struct DynLoad__##__name | ||
| #else | ||
| #define DYNAMIC_LOAD_CUBLAS_WRAP(__name) \ | ||
| struct DynLoad__##__name { \ | ||
| template <typename... Args> \ | ||
| cublasStatus_t operator()(Args... args) { \ | ||
| return __name(args...); \ | ||
| } \ | ||
| } __name; // struct DynLoad__##__name | ||
| #endif | ||
|
|
||
| #define DYNAMIC_LOAD_CUBLAS_V2_WRAP(__name) DYNAMIC_LOAD_CUBLAS_WRAP(__name) | ||
|
|
||
| // include all needed cublas functions in HPPL | ||
| // clang-format off | ||
| #define CUBLAS_BLAS_ROUTINE_EACH(__macro) \ | ||
| __macro(cublasSgemv) \ | ||
| __macro(cublasDgemv) \ | ||
| __macro(cublasSgemm) \ | ||
| __macro(cublasDgemm) \ | ||
| __macro(cublasSgeam) \ | ||
| __macro(cublasDgeam) \ | ||
|
|
||
| DYNAMIC_LOAD_CUBLAS_V2_WRAP(cublasCreate) | ||
| DYNAMIC_LOAD_CUBLAS_V2_WRAP(cublasDestroy) | ||
| DYNAMIC_LOAD_CUBLAS_V2_WRAP(cublasSetStream) | ||
| DYNAMIC_LOAD_CUBLAS_V2_WRAP(cublasSetPointerMode) | ||
| DYNAMIC_LOAD_CUBLAS_V2_WRAP(cublasGetPointerMode) | ||
| DYNAMIC_LOAD_CUBLAS_WRAP(cublasSgemmBatched) | ||
| DYNAMIC_LOAD_CUBLAS_WRAP(cublasDgemmBatched) | ||
| DYNAMIC_LOAD_CUBLAS_WRAP(cublasCgemmBatched) | ||
| DYNAMIC_LOAD_CUBLAS_WRAP(cublasZgemmBatched) | ||
| DYNAMIC_LOAD_CUBLAS_WRAP(cublasSgetrfBatched) | ||
| DYNAMIC_LOAD_CUBLAS_WRAP(cublasSgetriBatched) | ||
| DYNAMIC_LOAD_CUBLAS_WRAP(cublasDgetrfBatched) | ||
| DYNAMIC_LOAD_CUBLAS_WRAP(cublasDgetriBatched) | ||
| CUBLAS_BLAS_ROUTINE_EACH(DYNAMIC_LOAD_CUBLAS_V2_WRAP) | ||
|
|
||
| #undef DYNAMIC_LOAD_CUBLAS_WRAP | ||
| #undef DYNAMIC_LOAD_CUBLAS_V2_WRAP | ||
| #undef CUBLAS_BLAS_ROUTINE_EACH | ||
|
|
||
| // clang-format on | ||
| #ifndef PADDLE_TYPE_DOUBLE | ||
| #define CUBLAS_GEAM paddle::platform::dynload::cublasSgeam | ||
| #define CUBLAS_GEMV paddle::platform::dynload::cublasSgemv | ||
| #define CUBLAS_GEMM paddle::platform::dynload::cublasSgemm | ||
| #define CUBLAS_GETRF paddle::platform::dynload::cublasSgetrfBatched | ||
| #define CUBLAS_GETRI paddle::platform::dynload::cublasSgetriBatched | ||
| #else | ||
| #define CUBLAS_GEAM paddle::platform::dynload::cublasDgeam | ||
| #define CUBLAS_GEMV paddle::platform::dynload::cublasDgemv | ||
| #define CUBLAS_GEMM paddle::platform::dynload::cublasDgemm | ||
| #define CUBLAS_GETRF paddle::platform::dynload::cublasDgetrfBatched | ||
| #define CUBLAS_GETRI paddle::platform::dynload::cublasDgetriBatched | ||
| #endif | ||
| } // namespace dynload | ||
| } // namespace platform | ||
| } // namespace paddle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
|
||
| 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 <cudnn.h> | ||
| #include "paddle/platform/dynamic_loader.h" | ||
|
|
||
| namespace paddle { | ||
| namespace platform { | ||
| namespace dynload { | ||
|
|
||
| std::once_flag cudnn_dso_flag; | ||
| void* cudnn_dso_handle = nullptr; | ||
|
|
||
| #ifdef PADDLE_USE_DSO | ||
|
|
||
| #define DYNAMIC_LOAD_CUDNN_WRAP(__name) \ | ||
| struct DynLoad__##__name { \ | ||
| template <typename... Args> \ | ||
| auto operator()(Args... args) -> decltype(__name(args...)) { \ | ||
| using cudnn_func = decltype(__name(args...)) (*)(Args...); \ | ||
| std::call_once(cudnn_dso_flag, \ | ||
| paddle::platform::dynload::GetCudnnDsoHandle, \ | ||
| &cudnn_dso_handle); \ | ||
| void* p_##__name = dlsym(cudnn_dso_handle, #__name); \ | ||
| return reinterpret_cast<cudnn_func>(p_##__name)(args...); \ | ||
| } \ | ||
| } __name; /* struct DynLoad__##__name */ | ||
|
|
||
| #else | ||
|
|
||
| #define DYNAMIC_LOAD_CUDNN_WRAP(__name) \ | ||
| struct DynLoad__##__name { \ | ||
| template <typename... Args> \ | ||
| auto operator()(Args... args) -> decltype(__name(args...)) { \ | ||
| return __name(args...); \ | ||
| } \ | ||
| } __name; /* struct DynLoad__##__name */ | ||
|
|
||
| #endif | ||
|
|
||
| /** | ||
| * include all needed cudnn functions in HPPL | ||
| * different cudnn version has different interfaces | ||
| **/ | ||
| // clang-format off | ||
| #define CUDNN_DNN_ROUTINE_EACH(__macro) \ | ||
| __macro(cudnnSetTensor4dDescriptor) \ | ||
| __macro(cudnnSetTensor4dDescriptorEx) \ | ||
| __macro(cudnnGetConvolutionNdForwardOutputDim) \ | ||
| __macro(cudnnGetConvolutionForwardAlgorithm) \ | ||
| __macro(cudnnCreateTensorDescriptor) \ | ||
| __macro(cudnnDestroyTensorDescriptor) \ | ||
| __macro(cudnnCreateFilterDescriptor) \ | ||
| __macro(cudnnSetFilter4dDescriptor) \ | ||
| __macro(cudnnSetPooling2dDescriptor) \ | ||
| __macro(cudnnDestroyFilterDescriptor) \ | ||
| __macro(cudnnCreateConvolutionDescriptor) \ | ||
| __macro(cudnnCreatePoolingDescriptor) \ | ||
| __macro(cudnnDestroyPoolingDescriptor) \ | ||
| __macro(cudnnSetConvolution2dDescriptor) \ | ||
| __macro(cudnnDestroyConvolutionDescriptor) \ | ||
| __macro(cudnnCreate) \ | ||
| __macro(cudnnDestroy) \ | ||
| __macro(cudnnSetStream) \ | ||
| __macro(cudnnActivationForward) \ | ||
| __macro(cudnnConvolutionForward) \ | ||
| __macro(cudnnConvolutionBackwardBias) \ | ||
| __macro(cudnnGetConvolutionForwardWorkspaceSize) \ | ||
| __macro(cudnnTransformTensor) \ | ||
| __macro(cudnnPoolingForward) \ | ||
| __macro(cudnnPoolingBackward) \ | ||
| __macro(cudnnSoftmaxBackward) \ | ||
| __macro(cudnnSoftmaxForward) \ | ||
| __macro(cudnnGetVersion) \ | ||
| __macro(cudnnGetErrorString) | ||
| CUDNN_DNN_ROUTINE_EACH(DYNAMIC_LOAD_CUDNN_WRAP) | ||
|
|
||
| #define CUDNN_DNN_ROUTINE_EACH_R2(__macro) \ | ||
| __macro(cudnnAddTensor) \ | ||
| __macro(cudnnConvolutionBackwardData) \ | ||
| __macro(cudnnConvolutionBackwardFilter) | ||
| CUDNN_DNN_ROUTINE_EACH_R2(DYNAMIC_LOAD_CUDNN_WRAP) | ||
|
|
||
| // APIs available after R3: | ||
| #if CUDNN_VERSION >= 3000 | ||
| #define CUDNN_DNN_ROUTINE_EACH_AFTER_R3(__macro) \ | ||
| __macro(cudnnGetConvolutionBackwardFilterWorkspaceSize) \ | ||
| __macro(cudnnGetConvolutionBackwardDataAlgorithm) \ | ||
| __macro(cudnnGetConvolutionBackwardFilterAlgorithm) \ | ||
| __macro(cudnnGetConvolutionBackwardDataWorkspaceSize) | ||
| CUDNN_DNN_ROUTINE_EACH_AFTER_R3(DYNAMIC_LOAD_CUDNN_WRAP) | ||
| #undef CUDNN_DNN_ROUTINE_EACH_AFTER_R3 | ||
| #endif | ||
|
|
||
|
|
||
| // APIs available after R4: | ||
| #if CUDNN_VERSION >= 4007 | ||
| #define CUDNN_DNN_ROUTINE_EACH_AFTER_R4(__macro) \ | ||
| __macro(cudnnBatchNormalizationForwardTraining) \ | ||
| __macro(cudnnBatchNormalizationForwardInference) \ | ||
| __macro(cudnnBatchNormalizationBackward) | ||
| CUDNN_DNN_ROUTINE_EACH_AFTER_R4(DYNAMIC_LOAD_CUDNN_WRAP) | ||
| #undef CUDNN_DNN_ROUTINE_EACH_AFTER_R4 | ||
| #endif | ||
|
|
||
| // APIs in R5 | ||
| #if CUDNN_VERSION >= 5000 | ||
| #define CUDNN_DNN_ROUTINE_EACH_R5(__macro) \ | ||
| __macro(cudnnCreateActivationDescriptor) \ | ||
| __macro(cudnnSetActivationDescriptor) \ | ||
| __macro(cudnnGetActivationDescriptor) \ | ||
| __macro(cudnnDestroyActivationDescriptor) | ||
| CUDNN_DNN_ROUTINE_EACH_R5(DYNAMIC_LOAD_CUDNN_WRAP) | ||
| #undef CUDNN_DNN_ROUTINE_EACH_R5 | ||
| #endif | ||
|
|
||
| #undef CUDNN_DNN_ROUTINE_EACH | ||
| // clang-format on | ||
| } // namespace dynload | ||
| } // namespace platform | ||
| } // namespace paddle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
|
||
| 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 <curand.h> | ||
| #include "paddle/platform/dynamic_loader.h" | ||
|
|
||
| namespace paddle { | ||
| namespace platform { | ||
| namespace dynload { | ||
| std::once_flag curand_dso_flag; | ||
| void *curand_dso_handle = nullptr; | ||
| #ifdef PADDLE_USE_DSO | ||
| #define DYNAMIC_LOAD_CURAND_WRAP(__name) \ | ||
| struct DynLoad__##__name { \ | ||
| template <typename... Args> \ | ||
| curandStatus_t operator()(Args... args) { \ | ||
| typedef curandStatus_t (*curandFunc)(Args...); \ | ||
| std::call_once(curand_dso_flag, \ | ||
| paddle::platform::dynload::GetCurandDsoHandle, \ | ||
| &curand_dso_handle); \ | ||
| void *p_##__name = dlsym(curand_dso_handle, #__name); \ | ||
| return reinterpret_cast<curandFunc>(p_##__name)(args...); \ | ||
| } \ | ||
| } __name; /* struct DynLoad__##__name */ | ||
| #else | ||
| #define DYNAMIC_LOAD_CURAND_WRAP(__name) \ | ||
| struct DynLoad__##__name { \ | ||
| template <typename... Args> \ | ||
| curandStatus_t operator()(Args... args) { \ | ||
| return __name(args...); \ | ||
| } \ | ||
| } __name; /* struct DynLoad__##__name */ | ||
| #endif | ||
|
|
||
| /* include all needed curand functions in HPPL */ | ||
| // clang-format off | ||
| #define CURAND_RAND_ROUTINE_EACH(__macro) \ | ||
| __macro(curandCreateGenerator) \ | ||
| __macro(curandSetStream) \ | ||
| __macro(curandSetPseudoRandomGeneratorSeed)\ | ||
| __macro(curandGenerateUniform) \ | ||
| __macro(curandGenerateUniformDouble) \ | ||
| __macro(curandDestroyGenerator) | ||
| // clang-format on | ||
|
|
||
| CURAND_RAND_ROUTINE_EACH(DYNAMIC_LOAD_CURAND_WRAP) | ||
|
|
||
| #undef CURAND_RAND_ROUTINE_EACH | ||
| #undef DYNAMIC_LOAD_CURAND_WRAP | ||
| } // namespace dynload | ||
| } // namespace platform | ||
| } // namespace paddle |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we only use
PADDLE_ENFORCEand not throwthrust::system_error?But it seems that we need to move
enforcetoplatformif we want to usePADDLE_ENFORCEinplatformThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UNLIKELY can not handle the following situation since different libs have a different return struct.