registry.cc 5.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

20 21
/*!
 *  Copyright (c) 2017 by Contributors
22
 * \file registry.cc
23 24 25
 * \brief The global registry of packed function.
 */
#include <dmlc/logging.h>
26
#include <dmlc/thread_local.h>
27
#include <tvm/runtime/registry.h>
28
#include <unordered_map>
29
#include <mutex>
30
#include <memory>
31
#include <array>
32
#include "runtime_base.h"
33 34 35 36

namespace tvm {
namespace runtime {

37
struct Registry::Manager {
38 39 40 41 42
  // 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.
43
  std::unordered_map<std::string, Registry*> fmap;
44 45
  // vtable for extension type
  std::array<ExtTypeVTable, kExtEnd> ext_vtable;
46
  // mutex
47
  std::mutex mutex;
48

49
  Manager() {
50 51 52
    for (auto& x : ext_vtable) {
      x.destroy = nullptr;
    }
53 54
  }

55
  static Manager* Global() {
56 57 58 59 60
    // We deliberately leak the Manager instance, to avoid leak sanitizers
    // complaining about the entries in Manager::fmap being leaked at program
    // exit.
    static Manager* inst = new Manager();
    return inst;
61 62 63
  }
};

64 65 66 67 68
Registry& Registry::set_body(PackedFunc f) {  // NOLINT(*)
  func_ = f;
  return *this;
}

69
Registry& Registry::Register(const std::string& name, bool override) {  // NOLINT(*)
70
  Manager* m = Manager::Global();
71
  std::lock_guard<std::mutex> lock(m->mutex);
72
  auto it = m->fmap.find(name);
73 74 75 76 77 78 79
  if (it == m->fmap.end()) {
    Registry* r = new Registry();
    r->name_ = name;
    m->fmap[name] = r;
    return *r;
  } else {
    CHECK(override)
80
      << "Global PackedFunc " << name << " is already registered";
81 82
    return *it->second;
  }
83 84
}

85 86
bool Registry::Remove(const std::string& name) {
  Manager* m = Manager::Global();
87
  std::lock_guard<std::mutex> lock(m->mutex);
88 89 90 91
  auto it = m->fmap.find(name);
  if (it == m->fmap.end()) return false;
  m->fmap.erase(it);
  return true;
92 93
}

94 95
const PackedFunc* Registry::Get(const std::string& name) {
  Manager* m = Manager::Global();
96
  std::lock_guard<std::mutex> lock(m->mutex);
97 98 99
  auto it = m->fmap.find(name);
  if (it == m->fmap.end()) return nullptr;
  return &(it->second->func_);
100 101
}

102 103
std::vector<std::string> Registry::ListNames() {
  Manager* m = Manager::Global();
104
  std::lock_guard<std::mutex> lock(m->mutex);
105
  std::vector<std::string> keys;
106 107
  keys.reserve(m->fmap.size());
  for (const auto &kv : m->fmap) {
108 109 110 111 112
    keys.push_back(kv.first);
  }
  return keys;
}

113 114 115 116 117 118 119 120 121 122 123 124 125
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();
126
  std::lock_guard<std::mutex> lock(m->mutex);
127 128 129 130
  ExtTypeVTable* pvt = &(m->ext_vtable[type_code]);
  pvt[0] = vt;
  return pvt;
}
131 132 133
}  // namespace runtime
}  // namespace tvm

134 135 136 137 138 139 140 141 142 143 144
/*! \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;

145 146 147 148 149 150
int TVMExtTypeFree(void* handle, int type_code) {
  API_BEGIN();
  tvm::runtime::ExtTypeVTable::Get(type_code)->destroy(handle);
  API_END();
}

151 152
int TVMFuncRegisterGlobal(
    const char* name, TVMFunctionHandle f, int override) {
153
  API_BEGIN();
154
  tvm::runtime::Registry::Register(name, override != 0)
155
      .set_body(*static_cast<tvm::runtime::PackedFunc*>(f));
156 157 158 159 160
  API_END();
}

int TVMFuncGetGlobal(const char* name, TVMFunctionHandle* out) {
  API_BEGIN();
161 162
  const tvm::runtime::PackedFunc* fp =
      tvm::runtime::Registry::Get(name);
163 164 165 166 167
  if (fp != nullptr) {
    *out = new tvm::runtime::PackedFunc(*fp);  // NOLINT(*)
  } else {
    *out = nullptr;
  }
168 169 170 171 172 173 174
  API_END();
}

int TVMFuncListGlobalNames(int *out_size,
                           const char*** out_array) {
  API_BEGIN();
  TVMFuncThreadLocalEntry *ret = TVMFuncThreadLocalStore::Get();
175
  ret->ret_vec_str = tvm::runtime::Registry::ListNames();
176 177 178 179 180 181
  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());
182 183
  API_END();
}