• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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_spotrf)            \
67   __macro(rocsolver_dpotrf)            \
68   __macro(rocsolver_cpotrf)            \
69   __macro(rocsolver_zpotrf)
70 
71 // clang-format on
72 
73 FOREACH_ROCSOLVER_API(ROCSOLVER_API_WRAPPER)
74 
75 #undef TO_STR_
76 #undef TO_STR
77 #undef FOREACH_ROCSOLVER_API
78 #undef ROCSOLVER_API_WRAPPER
79 
80 }  // namespace wrap
81 }  // namespace tensorflow
82 
83 #endif  // TENSORFLOW_STREAM_EXECUTOR_ROCM_ROCSOLVER_WRAPPER_H_
84