c_api_common.h 1.41 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
 * \brief Copy a vector to an NDArray.
GaiYu0's avatar
GaiYu0 committed
35
 *
36
37
 * 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
38
 */
39
template<typename IdType, typename DType>
40
dgl::runtime::NDArray CopyVectorToNDArray(
GaiYu0's avatar
GaiYu0 committed
41
    const std::vector<DType>& vec) {
42
  using dgl::runtime::NDArray;
GaiYu0's avatar
GaiYu0 committed
43
  const int64_t len = vec.size();
44
45
  NDArray a = NDArray::Empty({len}, DLDataType{kDLInt, sizeof(IdType) * 8, 1},
                             DLContext{kDLCPU, 0});
46
  std::copy(vec.begin(), vec.end(), static_cast<IdType*>(a->data));
GaiYu0's avatar
GaiYu0 committed
47
48
49
  return a;
}

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

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

54
#endif  // DGL_C_API_COMMON_H_