c_api_common.h 1.31 KB
Newer Older
1
2
3
4
5
/*!
 *  Copyright (c) 2018 by Contributors
 * \file c_api_common.h
 * \brief DGL C API common util functions
 */
Lingfan Yu's avatar
Lingfan Yu committed
6
7
8
9
10
11
#ifndef DGL_C_API_COMMON_H_
#define DGL_C_API_COMMON_H_

#include <dgl/runtime/ndarray.h>
#include <dgl/runtime/packed_func.h>
#include <dgl/runtime/registry.h>
12
13
#include <dgl/array.h>
#include <dgl/graph_interface.h>
GaiYu0's avatar
GaiYu0 committed
14
#include <algorithm>
Lingfan Yu's avatar
Lingfan Yu committed
15
#include <vector>
16
#include <string>
17
#include <utility>
Lingfan Yu's avatar
Lingfan Yu committed
18
19
20

namespace dgl {

21
22
// Communicator handler type
typedef void* CommunicatorHandle;
Lingfan Yu's avatar
Lingfan Yu committed
23

Chao Ma's avatar
Chao Ma committed
24
25
26
// KVstore message handler type
typedef void* KVMsgHandle;

27
28
29
/*!
 * \brief Convert a vector of NDArray to PackedFunc.
 */
30
31
dgl::runtime::PackedFunc ConvertNDArrayVectorToPackedFunc(
    const std::vector<dgl::runtime::NDArray>& vec);
Lingfan Yu's avatar
Lingfan Yu committed
32

GaiYu0's avatar
GaiYu0 committed
33
34
35
36
37
/*!
 * \brief Copy a vector to an int64_t NDArray.
 *
 * The element type of the vector must be convertible to int64_t.
 */
38
template<typename IdType, typename DType>
39
dgl::runtime::NDArray CopyVectorToNDArray(
GaiYu0's avatar
GaiYu0 committed
40
    const std::vector<DType>& vec) {
41
  using dgl::runtime::NDArray;
GaiYu0's avatar
GaiYu0 committed
42
  const int64_t len = vec.size();
43
44
  NDArray a = NDArray::Empty({len}, DLDataType{kDLInt, sizeof(IdType), 1}, DLContext{kDLCPU, 0});
  std::copy(vec.begin(), vec.end(), static_cast<IdType*>(a->data));
GaiYu0's avatar
GaiYu0 committed
45
46
47
  return a;
}

48
49
runtime::PackedFunc ConvertEdgeArrayToPackedFunc(const EdgeArray& ea);

50
}  // namespace dgl
Lingfan Yu's avatar
Lingfan Yu committed
51

52
#endif  // DGL_C_API_COMMON_H_