api_test.cc 1.55 KB
Newer Older
1
/**
2
 *  Copyright (c) 2022 by Contributors
3
4
 * @file api/api_test.cc
 * @brief C APIs for testing FFI
5
 */
6
#include <dgl/packed_func_ext.h>
7
#include <dgl/runtime/container.h>
8
#include <dgl/runtime/ndarray.h>
9
#include <dgl/runtime/registry.h>
10

11
12
13
14
15
16
17
18
19
20
21
#include <thread>

namespace dgl {
namespace runtime {

// Register an internal API for testing python callback.
// It receives two arguments:
//   - The python callback function.
//   - The argument to pass to the python callback
// It returns what python callback returns
DGL_REGISTER_GLOBAL("_TestPythonCallback")
22
23
24
25
26
27
    .set_body([](DGLArgs args, DGLRetValue* rv) {
      LOG(INFO) << "Inside C API";
      PackedFunc fn = args[0];
      DGLArgs cb_args(args.values + 1, args.type_codes + 1, 1);
      fn.CallPacked(cb_args, rv);
    });
28
29
30
31
32
33
34
35
36
37

// Register an internal API for testing python callback.
// It receives two arguments:
//   - The python callback function.
//   - The argument to pass to the python callback
// It returns what python callback returns
//
// The API runs the python callback in a separate thread to test
// python GIL is properly released.
DGL_REGISTER_GLOBAL("_TestPythonCallbackThread")
38
39
40
41
    .set_body([](DGLArgs args, DGLRetValue* rv) {
      LOG(INFO) << "Inside C API";
      PackedFunc fn = args[0];
      auto thr = std::make_shared<std::thread>([fn, args, rv]() {
42
43
44
45
        LOG(INFO) << "Callback thread " << std::this_thread::get_id();
        DGLArgs cb_args(args.values + 1, args.type_codes + 1, 1);
        fn.CallPacked(cb_args, rv);
      });
46
47
      thr->join();
    });
48
49
50

}  // namespace runtime
}  // namespace dgl