registry.cc 4.5 KB
Newer Older
1 2
/*!
 *  Copyright (c) 2017 by Contributors
3
 * \file registry.cc
4 5 6
 * \brief The global registry of packed function.
 */
#include <dmlc/logging.h>
7
#include <dmlc/thread_local.h>
8
#include <tvm/runtime/registry.h>
9
#include <unordered_map>
10
#include <mutex>
11
#include <memory>
12
#include <array>
13 14 15 16 17
#include "./runtime_base.h"

namespace tvm {
namespace runtime {

18
struct Registry::Manager {
19 20 21 22 23
  // map storing the functions.
  // We delibrately used raw pointer
  // This is because PackedFunc can contain callbacks into the host languge(python)
  // and the resource can become invalid because of indeterminstic order of destruction.
  // The resources will only be recycled during program exit.
24
  std::unordered_map<std::string, Registry*> fmap;
25 26 27
  // vtable for extension type
  std::array<ExtTypeVTable, kExtEnd> ext_vtable;
  // mutex
28
  std::mutex mutex;
29

30 31 32 33 34 35
  Manager() {
    for (auto& x : ext_vtable) {
      x.destroy = nullptr;
    }
  }

36 37
  static Manager* Global() {
    static Manager inst;
38 39 40 41
    return &inst;
  }
};

42 43 44 45 46
Registry& Registry::set_body(PackedFunc f) {  // NOLINT(*)
  func_ = f;
  return *this;
}

47
Registry& Registry::Register(const std::string& name, bool override) {  // NOLINT(*)
48 49 50
  Manager* m = Manager::Global();
  std::lock_guard<std::mutex>(m->mutex);
  auto it = m->fmap.find(name);
51 52 53 54 55 56 57
  if (it == m->fmap.end()) {
    Registry* r = new Registry();
    r->name_ = name;
    m->fmap[name] = r;
    return *r;
  } else {
    CHECK(override)
58
      << "Global PackedFunc " << name << " is already registered";
59 60
    return *it->second;
  }
61 62
}

63 64 65 66 67 68 69
bool Registry::Remove(const std::string& name) {
  Manager* m = Manager::Global();
  std::lock_guard<std::mutex>(m->mutex);
  auto it = m->fmap.find(name);
  if (it == m->fmap.end()) return false;
  m->fmap.erase(it);
  return true;
70 71
}

72 73 74 75 76 77
const PackedFunc* Registry::Get(const std::string& name) {
  Manager* m = Manager::Global();
  std::lock_guard<std::mutex>(m->mutex);
  auto it = m->fmap.find(name);
  if (it == m->fmap.end()) return nullptr;
  return &(it->second->func_);
78 79
}

80 81 82
std::vector<std::string> Registry::ListNames() {
  Manager* m = Manager::Global();
  std::lock_guard<std::mutex>(m->mutex);
83
  std::vector<std::string> keys;
84 85
  keys.reserve(m->fmap.size());
  for (const auto &kv : m->fmap) {
86 87 88 89 90
    keys.push_back(kv.first);
  }
  return keys;
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
ExtTypeVTable* ExtTypeVTable::Get(int type_code) {
  CHECK(type_code > kExtBegin && type_code < kExtEnd);
  Registry::Manager* m = Registry::Manager::Global();
  ExtTypeVTable* vt = &(m->ext_vtable[type_code]);
  CHECK(vt->destroy != nullptr)
      << "Extension type not registered";
  return vt;
}

ExtTypeVTable* ExtTypeVTable::RegisterInternal(
    int type_code, const ExtTypeVTable& vt) {
  CHECK(type_code > kExtBegin && type_code < kExtEnd);
  Registry::Manager* m = Registry::Manager::Global();
  std::lock_guard<std::mutex>(m->mutex);
  ExtTypeVTable* pvt = &(m->ext_vtable[type_code]);
  pvt[0] = vt;
  return pvt;
}
109 110 111
}  // namespace runtime
}  // namespace tvm

112 113 114 115 116 117 118 119 120 121 122
/*! \brief entry to to easily hold returning information */
struct TVMFuncThreadLocalEntry {
  /*! \brief result holder for returning strings */
  std::vector<std::string> ret_vec_str;
  /*! \brief result holder for returning string pointers */
  std::vector<const char *> ret_vec_charp;
};

/*! \brief Thread local store that can be used to hold return values. */
typedef dmlc::ThreadLocalStore<TVMFuncThreadLocalEntry> TVMFuncThreadLocalStore;

123 124 125 126 127
int TVMExtTypeFree(void* handle, int type_code) {
  API_BEGIN();
  tvm::runtime::ExtTypeVTable::Get(type_code)->destroy(handle);
  API_END();
}
128

129 130
int TVMFuncRegisterGlobal(
    const char* name, TVMFunctionHandle f, int override) {
131
  API_BEGIN();
132
  tvm::runtime::Registry::Register(name, override != 0)
133
      .set_body(*static_cast<tvm::runtime::PackedFunc*>(f));
134 135 136 137 138
  API_END();
}

int TVMFuncGetGlobal(const char* name, TVMFunctionHandle* out) {
  API_BEGIN();
139 140
  const tvm::runtime::PackedFunc* fp =
      tvm::runtime::Registry::Get(name);
141 142 143 144 145
  if (fp != nullptr) {
    *out = new tvm::runtime::PackedFunc(*fp);  // NOLINT(*)
  } else {
    *out = nullptr;
  }
146 147 148 149 150 151 152
  API_END();
}

int TVMFuncListGlobalNames(int *out_size,
                           const char*** out_array) {
  API_BEGIN();
  TVMFuncThreadLocalEntry *ret = TVMFuncThreadLocalStore::Get();
153
  ret->ret_vec_str = tvm::runtime::Registry::ListNames();
154 155 156 157 158 159
  ret->ret_vec_charp.clear();
  for (size_t i = 0; i < ret->ret_vec_str.size(); ++i) {
    ret->ret_vec_charp.push_back(ret->ret_vec_str[i].c_str());
  }
  *out_array = dmlc::BeginPtr(ret->ret_vec_charp);
  *out_size = static_cast<int>(ret->ret_vec_str.size());
160 161
  API_END();
}