c_api_common.cc 1.14 KB
Newer Older
1
2
3
4
5
/*!
 *  Copyright (c) 2018 by Contributors
 * \file c_runtime_api.cc
 * \brief DGL C API common implementations
 */
6
#include <dgl/graph_interface.h>
Lingfan Yu's avatar
Lingfan Yu committed
7
8
#include "c_api_common.h"

9
10
11
12
13
using dgl::runtime::DGLArgs;
using dgl::runtime::DGLArgValue;
using dgl::runtime::DGLRetValue;
using dgl::runtime::PackedFunc;
using dgl::runtime::NDArray;
Lingfan Yu's avatar
Lingfan Yu committed
14
15
16
17

namespace dgl {

PackedFunc ConvertNDArrayVectorToPackedFunc(const std::vector<NDArray>& vec) {
18
    auto body = [vec](DGLArgs args, DGLRetValue* rv) {
Da Zheng's avatar
Da Zheng committed
19
        const uint64_t which = args[0];
Lingfan Yu's avatar
Lingfan Yu committed
20
21
22
23
24
25
26
27
28
        if (which >= vec.size()) {
            LOG(FATAL) << "invalid choice";
        } else {
            *rv = std::move(vec[which]);
        }
    };
    return PackedFunc(body);
}

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
PackedFunc ConvertEdgeArrayToPackedFunc(const EdgeArray& ea) {
  auto body = [ea] (DGLArgs args, DGLRetValue* rv) {
      const int which = args[0];
      if (which == 0) {
        *rv = std::move(ea.src);
      } else if (which == 1) {
        *rv = std::move(ea.dst);
      } else if (which == 2) {
        *rv = std::move(ea.id);
      } else {
        LOG(FATAL) << "invalid choice";
      }
    };
  return PackedFunc(body);
}

45
}  // namespace dgl