c_api_common.h 2.32 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
16
#include <vector>

17
18
19
20
21
22
23
24
25
26
/*! \brief Check whether two data types are the same.*/
inline bool operator == (const DLDataType& ty1, const DLDataType& ty2) {
  return ty1.code == ty2.code && ty1.bits == ty2.bits && ty1.lanes == ty2.lanes;
}

/*! \brief Output the string representation of device context.*/
inline std::ostream& operator << (std::ostream& os, const DLDataType& ty) {
  return os << "code=" << ty.code << ",bits=" << ty.bits << "lanes=" << ty.lanes;
}

27
28
29
30
31
32
33
/*! \brief Check whether two device contexts are the same.*/
inline bool operator == (const DLContext& ctx1, const DLContext& ctx2) {
  return ctx1.device_type == ctx2.device_type && ctx1.device_id == ctx2.device_id;
}

/*! \brief Output the string representation of device context.*/
inline std::ostream& operator << (std::ostream& os, const DLContext& ctx) {
34
  return os << ctx.device_type << ":" << ctx.device_id;
35
36
}

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

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

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

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

GaiYu0's avatar
GaiYu0 committed
55
/*!\brief Return whether the array is a valid 1D int array*/
56
inline bool IsValidIdArray(const dgl::runtime::NDArray& arr) {
57
  return arr->ndim == 1 && arr->dtype.code == kDLInt;
GaiYu0's avatar
GaiYu0 committed
58
59
60
61
62
63
64
65
}

/*!
 * \brief Copy a vector to an int64_t NDArray.
 *
 * The element type of the vector must be convertible to int64_t.
 */
template<typename DType>
66
dgl::runtime::NDArray CopyVectorToNDArray(
GaiYu0's avatar
GaiYu0 committed
67
    const std::vector<DType>& vec) {
68
  using dgl::runtime::NDArray;
GaiYu0's avatar
GaiYu0 committed
69
70
71
72
73
74
  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;
}

75
76
runtime::PackedFunc ConvertEdgeArrayToPackedFunc(const EdgeArray& ea);

77
}  // namespace dgl
Lingfan Yu's avatar
Lingfan Yu committed
78

79
#endif  // DGL_C_API_COMMON_H_