"vscode:/vscode.git/clone" did not exist on "b8ed4d9ba348c9104cab7b75910b930cb48fc5ce"
dso_module.cc 3.12 KB
Newer Older
Minjie Wang's avatar
Minjie Wang committed
1
2
3
4
5
6
7
/*!
 *  Copyright (c) 2017 by Contributors
 * \file dso_dll_module.cc
 * \brief Module to load from dynamic shared library.
 */
#include <dgl/runtime/module.h>
#include <dgl/runtime/packed_func.h>
8
9
#include <dgl/runtime/registry.h>

Minjie Wang's avatar
Minjie Wang committed
10
11
12
13
14
15
16
17
#include "module_util.h"

#if defined(_WIN32)
#include <windows.h>
#else
#include <dlfcn.h>
#endif

18
namespace dgl {
Minjie Wang's avatar
Minjie Wang committed
19
20
21
namespace runtime {

// Module to load from dynamic shared libary.
22
// This is the default module DGL used for host-side AOT
Minjie Wang's avatar
Minjie Wang committed
23
24
25
26
27
28
class DSOModuleNode final : public ModuleNode {
 public:
  ~DSOModuleNode() {
    if (lib_handle_) Unload();
  }

29
  const char* type_key() const final { return "dso"; }
Minjie Wang's avatar
Minjie Wang committed
30
31
32
33
34

  PackedFunc GetFunction(
      const std::string& name,
      const std::shared_ptr<ModuleNode>& sptr_to_self) final {
    BackendPackedCFunc faddr;
35
    if (name == runtime::symbol::dgl_module_main) {
Minjie Wang's avatar
Minjie Wang committed
36
      const char* entry_name = reinterpret_cast<const char*>(
37
          GetSymbol(runtime::symbol::dgl_module_main));
38
39
40
      CHECK(entry_name != nullptr)
          << "Symbol " << runtime::symbol::dgl_module_main
          << " is not presented";
Minjie Wang's avatar
Minjie Wang committed
41
42
43
44
45
46
47
48
49
50
      faddr = reinterpret_cast<BackendPackedCFunc>(GetSymbol(entry_name));
    } else {
      faddr = reinterpret_cast<BackendPackedCFunc>(GetSymbol(name.c_str()));
    }
    if (faddr == nullptr) return PackedFunc();
    return WrapPackedFunc(faddr, sptr_to_self);
  }

  void Init(const std::string& name) {
    Load(name);
51
52
    if (auto* ctx_addr = reinterpret_cast<void**>(
            GetSymbol(runtime::symbol::dgl_module_ctx))) {
Minjie Wang's avatar
Minjie Wang committed
53
54
      *ctx_addr = this;
    }
55
56
    InitContextFunctions(
        [this](const char* fname) { return GetSymbol(fname); });
Minjie Wang's avatar
Minjie Wang committed
57
    // Load the imported modules
58
59
    const char* dev_mblob = reinterpret_cast<const char*>(
        GetSymbol(runtime::symbol::dgl_dev_mblob));
Minjie Wang's avatar
Minjie Wang committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    if (dev_mblob != nullptr) {
      ImportModuleBlob(dev_mblob, &imports_);
    }
  }

 private:
  // Platform dependent handling.
#if defined(_WIN32)
  // library handle
  HMODULE lib_handle_{nullptr};
  // Load the library
  void Load(const std::string& name) {
    // use wstring version that is needed by LLVM.
    std::wstring wname(name.begin(), name.end());
    lib_handle_ = LoadLibraryW(wname.c_str());
    CHECK(lib_handle_ != nullptr)
        << "Failed to load dynamic shared library " << name;
  }
  void* GetSymbol(const char* name) {
    return reinterpret_cast<void*>(
80
        GetProcAddress(lib_handle_, (LPCSTR)name));  // NOLINT(*)
Minjie Wang's avatar
Minjie Wang committed
81
  }
82
  void Unload() { FreeLibrary(lib_handle_); }
Minjie Wang's avatar
Minjie Wang committed
83
84
85
86
87
88
89
#else
  // Library handle
  void* lib_handle_{nullptr};
  // load the library
  void Load(const std::string& name) {
    lib_handle_ = dlopen(name.c_str(), RTLD_LAZY | RTLD_LOCAL);
    CHECK(lib_handle_ != nullptr)
90
        << "Failed to load dynamic shared library " << name << " " << dlerror();
Minjie Wang's avatar
Minjie Wang committed
91
  }
92
93
  void* GetSymbol(const char* name) { return dlsym(lib_handle_, name); }
  void Unload() { dlclose(lib_handle_); }
Minjie Wang's avatar
Minjie Wang committed
94
95
96
#endif
};

97
DGL_REGISTER_GLOBAL("module.loadfile_so")
98
99
100
101
102
    .set_body([](DGLArgs args, DGLRetValue* rv) {
      std::shared_ptr<DSOModuleNode> n = std::make_shared<DSOModuleNode>();
      n->Init(args[0]);
      *rv = runtime::Module(n);
    });
Minjie Wang's avatar
Minjie Wang committed
103
}  // namespace runtime
104
}  // namespace dgl