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

9
10
#include <dgl/array.h>
#include <dgl/graph_interface.h>
Lingfan Yu's avatar
Lingfan Yu committed
11
12
13
#include <dgl/runtime/ndarray.h>
#include <dgl/runtime/packed_func.h>
#include <dgl/runtime/registry.h>
14

GaiYu0's avatar
GaiYu0 committed
15
#include <algorithm>
16
#include <string>
17
#include <utility>
18
#include <vector>
Lingfan Yu's avatar
Lingfan Yu committed
19
20
21

namespace dgl {

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

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

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

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

51
52
runtime::PackedFunc ConvertEdgeArrayToPackedFunc(const EdgeArray& ea);

53
}  // namespace dgl
Lingfan Yu's avatar
Lingfan Yu committed
54

55
#endif  // DGL_C_API_COMMON_H_