c_api_common.h 1.5 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
15
16
17
18
19
#include <vector>

namespace dgl {

// Graph handler type
typedef void* GraphHandle;

20
21
22
23
24
25
26
/*!
 * \brief Convert the given DLTensor to DLManagedTensor.
 *
 * Return a temporary DLManagedTensor that does not own memory.
 */
DLManagedTensor* CreateTmpDLManagedTensor(
    const tvm::runtime::TVMArgValue& arg);
Lingfan Yu's avatar
Lingfan Yu committed
27

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

GaiYu0's avatar
GaiYu0 committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*!\brief Return whether the array is a valid 1D int array*/
inline bool IsValidIdArray(const tvm::runtime::NDArray& arr) {
  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>
tvm::runtime::NDArray CopyVectorToNDArray(
    const std::vector<DType>& vec) {
  using tvm::runtime::NDArray;
  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;
}

55
}  // namespace dgl
Lingfan Yu's avatar
Lingfan Yu committed
56

57
#endif  // DGL_C_API_COMMON_H_