tensordispatch.cc 1.68 KB
Newer Older
1
2
3
4
5
6
7
/*!
 *  Copyright (c) 2019 by Contributors
 * \file runtime/tensordispatch.cc
 * \brief Adapter library caller
 */

#include <dgl/packed_func_ext.h>
8
9
#include <dgl/runtime/registry.h>
#include <dgl/runtime/tensordispatch.h>
10
11
#if defined(WIN32) || defined(_WIN32)
#include <windows.h>
12
#else  // !WIN32
13
14
15
16
17
18
19
20
21
#include <dlfcn.h>
#endif  // WIN32
#include <cstring>

namespace dgl {
namespace runtime {

constexpr const char *TensorDispatcher::names_[];

22
bool TensorDispatcher::Load(const char *path) {
23
24
25
  CHECK(!available_) << "The tensor adapter can only load once.";

  if (path == nullptr || strlen(path) == 0)
26
27
    // does not have dispatcher library; all operators fall back to DGL's
    // implementation
28
    return false;
29
30

#if defined(WIN32) || defined(_WIN32)
31
  handle_ = LoadLibrary(path);
32

33
  if (!handle_) return false;
34

35
  for (int i = 0; i < num_entries_; ++i) {
36
37
    entrypoints_[i] =
        reinterpret_cast<void *>(GetProcAddress(handle_, names_[i]));
38
39
    CHECK(entrypoints_[i]) << "cannot locate symbol " << names_[i];
  }
40
#else   // !WIN32
41
42
  handle_ = dlopen(path, RTLD_LAZY);

43
  if (!handle_) {
44
45
46
    DLOG(WARNING)
        << "Could not open file: " << dlerror()
        << ". This does not affect DGL's but might impact its performance.";
47
    return false;
48
  }
49
50

  for (int i = 0; i < num_entries_; ++i) {
51
    entrypoints_[i] = dlsym(handle_, names_[i]);
52
53
    CHECK(entrypoints_[i]) << "cannot locate symbol " << names_[i];
  }
54
55
56
#endif  // WIN32

  available_ = true;
57
  return true;
58
59
60
61
62
63
64
65
66
67
68
69
70
71
}

TensorDispatcher::~TensorDispatcher() {
  if (handle_) {
#if defined(WIN32) || defined(_WIN32)
    FreeLibrary(handle_);
#else   // !WIN32
    dlclose(handle_);
#endif  // WIN32
  }
}

};  // namespace runtime
};  // namespace dgl