c_api_common.h 2.59 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>
GaiYu0's avatar
GaiYu0 committed
12
#include <algorithm>
Lingfan Yu's avatar
Lingfan Yu committed
13
14
#include <vector>

15
16
17
18
19
20
21
22
23
24
/*! \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) {
  return os << "" << ctx.device_type << ":" << ctx.device_id << "";
}

Lingfan Yu's avatar
Lingfan Yu committed
25
26
27
28
29
namespace dgl {

// Graph handler type
typedef void* GraphHandle;

30
31
// Communicator handler type
typedef void* CommunicatorHandle;
Lingfan Yu's avatar
Lingfan Yu committed
32

33
34
35
36
37
38
39
/*! \brief Enum type for bool value with unknown */
enum BoolFlag {
  kBoolUnknown = -1,
  kBoolFalse = 0,
  kBoolTrue = 1
};

40
41
42
/*!
 * \brief Convert a vector of NDArray to PackedFunc.
 */
43
44
dgl::runtime::PackedFunc ConvertNDArrayVectorToPackedFunc(
    const std::vector<dgl::runtime::NDArray>& vec);
Lingfan Yu's avatar
Lingfan Yu committed
45

GaiYu0's avatar
GaiYu0 committed
46
/*!\brief Return whether the array is a valid 1D int array*/
47
inline bool IsValidIdArray(const dgl::runtime::NDArray& arr) {
GaiYu0's avatar
GaiYu0 committed
48
49
50
51
52
53
54
55
56
57
  return arr->ctx.device_type == kDLCPU && arr->ndim == 1
    && arr->dtype.code == kDLInt && arr->dtype.bits == 64;
}

/*!
 * \brief Copy a vector to an int64_t NDArray.
 *
 * The element type of the vector must be convertible to int64_t.
 */
template<typename DType>
58
dgl::runtime::NDArray CopyVectorToNDArray(
GaiYu0's avatar
GaiYu0 committed
59
    const std::vector<DType>& vec) {
60
  using dgl::runtime::NDArray;
GaiYu0's avatar
GaiYu0 committed
61
62
63
64
65
66
  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;
}

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* A structure used to return a vector of void* pointers. */
struct CAPIVectorWrapper {
  // The pointer vector.
  std::vector<void*> pointers;
};

/*!
 * \brief A helper function used to return vector of pointers from C to frontend.
 *
 * Note that the function will move the given vector memory into the returned
 * wrapper object.
 * 
 * \param vec The given pointer vectors.
 * \return A wrapper object containing the given data.
 */
template<typename PType>
CAPIVectorWrapper* WrapVectorReturn(std::vector<PType*> vec) {
  CAPIVectorWrapper* wrapper = new CAPIVectorWrapper;
  wrapper->pointers.reserve(vec.size());
  wrapper->pointers.insert(wrapper->pointers.end(), vec.begin(), vec.end());
  return wrapper;
}

90
}  // namespace dgl
Lingfan Yu's avatar
Lingfan Yu committed
91

92
#endif  // DGL_C_API_COMMON_H_