"tests/implicitron/vscode:/vscode.git/clone" did not exist on "c54e04866652dbb9eaaf5e7d0206bdf62fe9f091"
system_lib_module.cc 2.75 KB
Newer Older
Minjie Wang's avatar
Minjie Wang committed
1
2
3
4
5
6
7
8
9
10
/*!
 *  Copyright (c) 2017 by Contributors
 * \file system_lib_module.cc
 * \brief SystemLib module.
 */
#include <dgl/runtime/registry.h>
#include <dgl/runtime/c_backend_api.h>
#include <mutex>
#include "module_util.h"

11
namespace dgl {
Minjie Wang's avatar
Minjie Wang committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
namespace runtime {

class SystemLibModuleNode : public ModuleNode {
 public:
  SystemLibModuleNode() = default;

  const char* type_key() const final {
    return "system_lib";
  }

  PackedFunc GetFunction(
      const std::string& name,
      const std::shared_ptr<ModuleNode>& sptr_to_self) final {
    std::lock_guard<std::mutex> lock(mutex_);

    if (module_blob_ != nullptr) {
      // If we previously recorded submodules, load them now.
      ImportModuleBlob(reinterpret_cast<const char*>(module_blob_), &imports_);
      module_blob_ = nullptr;
    }

    auto it = tbl_.find(name);
    if (it != tbl_.end()) {
      return WrapPackedFunc(
          reinterpret_cast<BackendPackedCFunc>(it->second), sptr_to_self);
    } else {
      return PackedFunc();
    }
  }

  void RegisterSymbol(const std::string& name, void* ptr) {
    std::lock_guard<std::mutex> lock(mutex_);
44
    if (name == symbol::dgl_module_ctx) {
Minjie Wang's avatar
Minjie Wang committed
45
46
      void** ctx_addr = reinterpret_cast<void**>(ptr);
      *ctx_addr = this;
47
    } else if (name == symbol::dgl_dev_mblob) {
Minjie Wang's avatar
Minjie Wang committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
      // Record pointer to content of submodules to be loaded.
      // We defer loading submodules to the first call to GetFunction().
      // The reason is that RegisterSymbol() gets called when initializing the
      // syslib (i.e. library loading time), and the registeries aren't ready
      // yet. Therefore, we might not have the functionality to load submodules
      // now.
      CHECK(module_blob_ == nullptr) << "Resetting mobule blob?";
      module_blob_ = ptr;
    } else {
      auto it = tbl_.find(name);
      if (it != tbl_.end() && ptr != it->second) {
        LOG(WARNING) << "SystemLib symbol " << name
                     << " get overriden to a different address "
                     << ptr << "->" << it->second;
      }
      tbl_[name] = ptr;
    }
  }

  static const std::shared_ptr<SystemLibModuleNode>& Global() {
    static std::shared_ptr<SystemLibModuleNode> inst =
        std::make_shared<SystemLibModuleNode>();
    return inst;
  }

 private:
  // Internal mutex
  std::mutex mutex_;
  // Internal symbol table
  std::unordered_map<std::string, void*> tbl_;
  // Module blob to be imported
  void* module_blob_{nullptr};
};

82
83
DGL_REGISTER_GLOBAL("module._GetSystemLib")
.set_body([](DGLArgs args, DGLRetValue* rv) {
Minjie Wang's avatar
Minjie Wang committed
84
85
86
    *rv = runtime::Module(SystemLibModuleNode::Global());
  });
}  // namespace runtime
87
}  // namespace dgl
Minjie Wang's avatar
Minjie Wang committed
88

89
90
int DGLBackendRegisterSystemLibSymbol(const char* name, void* ptr) {
  dgl::runtime::SystemLibModuleNode::Global()->RegisterSymbol(name, ptr);
Minjie Wang's avatar
Minjie Wang committed
91
92
  return 0;
}