arith.h 1.83 KB
Newer Older
1
2
3
4
5
6
7
8
/*!
 *  Copyright (c) 2019 by Contributors
 * \file array/arith.h
 * \brief Arithmetic functors
 */
#ifndef DGL_ARRAY_ARITH_H_
#define DGL_ARRAY_ARITH_H_

9
10
11
12
13
14
15
16
#ifdef __CUDACC__
#define DGLDEVICE __device__
#define DGLINLINE __forceinline__
#else
#define DGLDEVICE
#define DGLINLINE inline
#endif  // __CUDACC__

17
18
19
20
21
22
namespace dgl {
namespace aten {
namespace arith {

struct Add {
  template <typename T>
23
  static DGLINLINE DGLDEVICE T Call(const T& t1, const T& t2) {
24
25
26
27
28
29
    return t1 + t2;
  }
};

struct Sub {
  template <typename T>
30
  static DGLINLINE DGLDEVICE T Call(const T& t1, const T& t2) {
31
32
33
34
35
36
    return t1 - t2;
  }
};

struct Mul {
  template <typename T>
37
  static DGLINLINE DGLDEVICE T Call(const T& t1, const T& t2) {
38
39
40
41
42
43
    return t1 * t2;
  }
};

struct Div {
  template <typename T>
44
  static DGLINLINE DGLDEVICE T Call(const T& t1, const T& t2) {
45
46
47
48
    return t1 / t2;
  }
};

49
50
51
52
53
54
55
struct GT {
  template <typename T>
  static DGLINLINE DGLDEVICE bool Call(const T& t1, const T& t2) {
    return t1 > t2;
  }
};

56
57
struct LT {
  template <typename T>
58
  static DGLINLINE DGLDEVICE bool Call(const T& t1, const T& t2) {
59
60
61
62
    return t1 < t2;
  }
};

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
struct GE {
  template <typename T>
  static DGLINLINE DGLDEVICE bool Call(const T& t1, const T& t2) {
    return t1 >= t2;
  }
};

struct LE {
  template <typename T>
  static DGLINLINE DGLDEVICE bool Call(const T& t1, const T& t2) {
    return t1 <= t2;
  }
};

struct EQ {
  template <typename T>
  static DGLINLINE DGLDEVICE bool Call(const T& t1, const T& t2) {
    return t1 == t2;
  }
};

struct NE {
  template <typename T>
  static DGLINLINE DGLDEVICE bool Call(const T& t1, const T& t2) {
    return t1 != t2;
  }
};

struct Neg {
  template <typename T>
  static DGLINLINE DGLDEVICE T Call(const T& t1) {
    return -t1;
  }
};

98
99
100
101
102
}  // namespace arith
}  // namespace aten
}  // namespace dgl

#endif  // DGL_ARRAY_ARITH_H_