1 /* Copyright 2020 The TensorFlow 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 16 // This file wraps rocsolver API calls with dso loader so that we don't need to 17 // have explicit linking to librocsolver. All TF hipsarse API usage should route 18 // through this wrapper. 19 20 #ifndef TENSORFLOW_STREAM_EXECUTOR_ROCM_ROCSOLVER_WRAPPER_H_ 21 #define TENSORFLOW_STREAM_EXECUTOR_ROCM_ROCSOLVER_WRAPPER_H_ 22 23 #include "rocm/include/rocsolver/rocsolver.h" 24 #include "tensorflow/stream_executor/lib/env.h" 25 #include "tensorflow/stream_executor/platform/dso_loader.h" 26 #include "tensorflow/stream_executor/platform/port.h" 27 28 namespace tensorflow { 29 namespace wrap { 30 31 #ifdef PLATFORM_GOOGLE 32 33 #define ROCSOLVER_API_WRAPPER(api_name) \ 34 template <typename... Args> \ 35 auto api_name(Args... args)->decltype(::api_name(args...)) { \ 36 return ::api_name(args...); \ 37 } 38 39 #else 40 41 #define TO_STR_(x) #x 42 #define TO_STR(x) TO_STR_(x) 43 44 #define ROCSOLVER_API_WRAPPER(api_name) \ 45 template <typename... Args> \ 46 auto api_name(Args... args)->decltype(::api_name(args...)) { \ 47 using FuncPtrT = std::add_pointer<decltype(::api_name)>::type; \ 48 static FuncPtrT loaded = []() -> FuncPtrT { \ 49 static const char* kName = TO_STR(api_name); \ 50 void* f; \ 51 auto s = stream_executor::port::Env::Default()->GetSymbolFromLibrary( \ 52 stream_executor::internal::CachedDsoLoader::GetRocsolverDsoHandle() \ 53 .ValueOrDie(), \ 54 kName, &f); \ 55 CHECK(s.ok()) << "could not find " << kName \ 56 << " in rocsolver lib; dlerror: " << s.error_message(); \ 57 return reinterpret_cast<FuncPtrT>(f); \ 58 }(); \ 59 return loaded(args...); \ 60 } 61 62 #endif 63 64 // clang-format off 65 #define FOREACH_ROCSOLVER_API(__macro) \ 66 __macro(rocsolver_cgetrf) \ 67 __macro(rocsolver_dgetrf) \ 68 __macro(rocsolver_sgetrf) \ 69 __macro(rocsolver_zgetrf) \ 70 __macro(rocsolver_cgetrs) \ 71 __macro(rocsolver_dgetrs) \ 72 __macro(rocsolver_sgetrs) \ 73 __macro(rocsolver_zgetrs) \ 74 __macro(rocsolver_cgetrf_batched) \ 75 __macro(rocsolver_dgetrf_batched) \ 76 __macro(rocsolver_sgetrf_batched) \ 77 __macro(rocsolver_zgetrf_batched) \ 78 __macro(rocsolver_cgetrs_batched) \ 79 __macro(rocsolver_dgetrs_batched) \ 80 __macro(rocsolver_sgetrs_batched) \ 81 __macro(rocsolver_zgetrs_batched) \ 82 __macro(rocsolver_cgetri_batched) \ 83 __macro(rocsolver_dgetri_batched) \ 84 __macro(rocsolver_sgetri_batched) \ 85 __macro(rocsolver_zgetri_batched) \ 86 __macro(rocsolver_cpotrf) \ 87 __macro(rocsolver_dpotrf) \ 88 __macro(rocsolver_spotrf) \ 89 __macro(rocsolver_zpotrf) \ 90 __macro(rocsolver_cpotrf_batched) \ 91 __macro(rocsolver_dpotrf_batched) \ 92 __macro(rocsolver_spotrf_batched) \ 93 __macro(rocsolver_zpotrf_batched) \ 94 __macro(rocsolver_cgeqrf) \ 95 __macro(rocsolver_dgeqrf) \ 96 __macro(rocsolver_sgeqrf) \ 97 __macro(rocsolver_zgeqrf) \ 98 __macro(rocsolver_cunmqr) \ 99 __macro(rocsolver_zunmqr) \ 100 __macro(rocsolver_cungqr) \ 101 __macro(rocsolver_zungqr) 102 // clang-format on 103 104 FOREACH_ROCSOLVER_API(ROCSOLVER_API_WRAPPER) 105 106 #undef TO_STR_ 107 #undef TO_STR 108 #undef FOREACH_ROCSOLVER_API 109 #undef ROCSOLVER_API_WRAPPER 110 111 } // namespace wrap 112 } // namespace tensorflow 113 114 #endif // TENSORFLOW_STREAM_EXECUTOR_ROCM_ROCSOLVER_WRAPPER_H_ 115