DPC++ Runtime
Runtime libraries for oneAPI DPC++
posix_pi.cpp
Go to the documentation of this file.
1 //==---------------- posix_pi.cpp ------------------------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
11 #include <sycl/detail/pi.hpp>
12 
13 #include <dlfcn.h>
14 #include <string>
15 
16 namespace sycl {
17 inline namespace _V1 {
18 namespace detail::pi {
19 
20 void *loadOsLibrary(const std::string &LibraryPath) {
21  // TODO: Check if the option RTLD_NOW is correct. Explore using
22  // RTLD_DEEPBIND option when there are multiple plugins.
23  void *so = dlopen(LibraryPath.c_str(), RTLD_NOW);
24  if (!so && trace(TraceLevel::PI_TRACE_ALL)) {
25  char *Error = dlerror();
26  std::cerr << "SYCL_PI_TRACE[-1]: dlopen(" << LibraryPath
27  << ") failed with <" << (Error ? Error : "unknown error") << ">"
28  << std::endl;
29  }
30  return so;
31 }
32 
33 void *loadOsPluginLibrary(const std::string &PluginPath) {
34  return loadOsLibrary(PluginPath);
35 }
36 
37 int unloadOsLibrary(void *Library) { return dlclose(Library); }
38 
39 int unloadOsPluginLibrary(void *Library) {
40  // The mock plugin does not have an associated library, so we allow nullptr
41  // here to avoid it trying to free a non-existent library.
42  if (!Library)
43  return 0;
44  return dlclose(Library);
45 }
46 
47 void *getOsLibraryFuncAddress(void *Library, const std::string &FunctionName) {
48  return dlsym(Library, FunctionName.c_str());
49 }
50 
51 // Load plugins corresponding to provided list of plugin names.
52 std::vector<std::tuple<std::string, backend, void *>>
53 loadPlugins(const std::vector<std::pair<std::string, backend>> &&PluginNames) {
54  std::vector<std::tuple<std::string, backend, void *>> LoadedPlugins;
55  const std::string LibSYCLDir =
56  sycl::detail::OSUtil::getCurrentDSODir() + sycl::detail::OSUtil::DirSep;
57 
58  for (auto &PluginName : PluginNames) {
59  void *Library = loadOsPluginLibrary(LibSYCLDir + PluginName.first);
60  LoadedPlugins.push_back(std::make_tuple(
61  std::move(PluginName.first), std::move(PluginName.second), Library));
62  }
63 
64  return LoadedPlugins;
65 }
66 
67 } // namespace detail::pi
68 } // namespace _V1
69 } // namespace sycl
__SYCL_EXTERN_STREAM_ATTRS ostream cerr
Linked to standard error (unbuffered)
int unloadOsLibrary(void *Library)
Definition: posix_pi.cpp:37
void * loadOsPluginLibrary(const std::string &Library)
Definition: posix_pi.cpp:33
bool trace(TraceLevel level)
Definition: pi.cpp:366
void * loadOsLibrary(const std::string &Library)
Definition: posix_pi.cpp:20
void * getOsLibraryFuncAddress(void *Library, const std::string &FunctionName)
Definition: posix_pi.cpp:47
int unloadOsPluginLibrary(void *Library)
Definition: posix_pi.cpp:39
std::vector< std::tuple< std::string, backend, void * > > loadPlugins(const std::vector< std::pair< std::string, backend >> &&PluginNames)
Definition: posix_pi.cpp:53
constexpr tuple< Ts... > make_tuple(Ts... Args)
Definition: tuple.hpp:35
Definition: access.hpp:18
C++ wrapper of extern "C" PI interfaces.