common.h 1.54 KB
Newer Older
1
2
3
4
5
#ifndef TEST_COMMON_H_
#define TEST_COMMON_H_

#include <dgl/runtime/ndarray.h>

6
7
static constexpr DGLContext CTX = DGLContext{kDGLCPU, 0};
static constexpr DGLContext CPU = DGLContext{kDGLCPU, 0};
8
#ifdef DGL_USE_CUDA
9
static constexpr DGLContext GPU = DGLContext{kDGLCUDA, 0};
10
11
#endif

12
13
14
15
16
17
18
19
20
21
22
23
24
template <typename T>
inline T* Ptr(dgl::runtime::NDArray nd) {
  return static_cast<T*>(nd->data);
}

inline int64_t* PI64(dgl::runtime::NDArray nd) {
  return static_cast<int64_t*>(nd->data);
}

inline int32_t* PI32(dgl::runtime::NDArray nd) {
  return static_cast<int32_t*>(nd->data);
}

25
inline int64_t Len(dgl::runtime::NDArray nd) { return nd->shape[0]; }
26
27
28
29

template <typename T>
inline bool ArrayEQ(dgl::runtime::NDArray a1, dgl::runtime::NDArray a2) {
  if (a1->ndim != a2->ndim) return false;
30
31
32
33
  if (a1->dtype != a2->dtype) return false;
  if (a1->ctx != a2->ctx) return false;
  if (a1.NumElements() != a2.NumElements()) return false;
  if (a1.NumElements() == 0) return true;
34
35
  int64_t num = 1;
  for (int i = 0; i < a1->ndim; ++i) {
36
    if (a1->shape[i] != a2->shape[i]) return false;
37
38
    num *= a1->shape[i];
  }
39
40
  a1 = a1.CopyTo(CPU);
  a2 = a2.CopyTo(CPU);
41
42
43
44
45
46
  for (int64_t i = 0; i < num; ++i)
    if (static_cast<T*>(a1->data)[i] != static_cast<T*>(a2->data)[i])
      return false;
  return true;
}

47
48
template <typename T>
inline bool IsInArray(dgl::runtime::NDArray a, T x) {
49
  if (!a.defined() || a->shape[0] == 0) return false;
50
  for (int64_t i = 0; i < a->shape[0]; ++i) {
51
    if (x == static_cast<T*>(a->data)[i]) return true;
52
53
54
55
  }
  return false;
}

56
#endif  // TEST_COMMON_H_