c_api_common.h 1.83 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>
Lingfan Yu's avatar
Lingfan Yu committed
17

18
19
using dgl::runtime::operator<<;

20
/*! \brief Output the string representation of device context.*/
21
22
23
24
25
26
27
28
29
30
31
32
33
inline std::ostream& operator<<(std::ostream& os, const DLContext& ctx) {
  std::string device_name;
  switch (ctx.device_type) {
    case kDLCPU:
      device_name = "CPU";
      break;
    case kDLGPU:
      device_name = "GPU";
      break;
    default:
      device_name = "Unknown device";
  }
  return os << device_name << ":" << ctx.device_id;
34
35
}

Lingfan Yu's avatar
Lingfan Yu committed
36
37
namespace dgl {

38
39
// Communicator handler type
typedef void* CommunicatorHandle;
Lingfan Yu's avatar
Lingfan Yu committed
40

Chao Ma's avatar
Chao Ma committed
41
42
43
// KVstore message handler type
typedef void* KVMsgHandle;

44
45
46
47
48
49
50
/*! \brief Enum type for bool value with unknown */
enum BoolFlag {
  kBoolUnknown = -1,
  kBoolFalse = 0,
  kBoolTrue = 1
};

51
52
53
/*!
 * \brief Convert a vector of NDArray to PackedFunc.
 */
54
55
dgl::runtime::PackedFunc ConvertNDArrayVectorToPackedFunc(
    const std::vector<dgl::runtime::NDArray>& vec);
Lingfan Yu's avatar
Lingfan Yu committed
56

GaiYu0's avatar
GaiYu0 committed
57
58
59
60
61
62
/*!
 * \brief Copy a vector to an int64_t NDArray.
 *
 * The element type of the vector must be convertible to int64_t.
 */
template<typename DType>
63
dgl::runtime::NDArray CopyVectorToNDArray(
GaiYu0's avatar
GaiYu0 committed
64
    const std::vector<DType>& vec) {
65
  using dgl::runtime::NDArray;
GaiYu0's avatar
GaiYu0 committed
66
67
68
69
70
71
  const int64_t len = vec.size();
  NDArray a = NDArray::Empty({len}, DLDataType{kDLInt, 64, 1}, DLContext{kDLCPU, 0});
  std::copy(vec.begin(), vec.end(), static_cast<int64_t*>(a->data));
  return a;
}

72
73
runtime::PackedFunc ConvertEdgeArrayToPackedFunc(const EdgeArray& ea);

74
}  // namespace dgl
Lingfan Yu's avatar
Lingfan Yu committed
75

76
#endif  // DGL_C_API_COMMON_H_