array_arith.cc 7.25 KB
Newer Older
1
/**
2
 *  Copyright (c) 2019 by Contributors
3
4
 * @file array/array_aritch.cc
 * @brief DGL array arithmetic operations
5
6
7
 */
#include <dgl/packed_func_ext.h>
#include <dgl/runtime/container.h>
8
9
#include <dgl/runtime/ndarray.h>

10
11
#include "../c_api_common.h"
#include "./arith.h"
12
#include "./array_op.h"
13
14
15
16
17
18
19

using namespace dgl::runtime;

namespace dgl {
namespace aten {

// Generate operators with both operations being NDArrays.
20
21
22
23
24
25
26
27
28
29
30
#define BINARY_ELEMENT_OP(name, op)                                  \
  IdArray name(IdArray lhs, IdArray rhs) {                           \
    IdArray ret;                                                     \
    CHECK_SAME_DTYPE(lhs, rhs);                                      \
    CHECK_SAME_CONTEXT(lhs, rhs);                                    \
    ATEN_XPU_SWITCH_CUDA(lhs->ctx.device_type, XPU, #name, {         \
      ATEN_ID_TYPE_SWITCH(lhs->dtype, IdType, {                      \
        ret = impl::BinaryElewise<XPU, IdType, arith::op>(lhs, rhs); \
      });                                                            \
    });                                                              \
    return ret;                                                      \
31
32
33
  }

// Generate operators with only lhs being NDArray.
34
35
36
37
38
39
40
41
42
#define BINARY_ELEMENT_OP_L(name, op)                                \
  IdArray name(IdArray lhs, int64_t rhs) {                           \
    IdArray ret;                                                     \
    ATEN_XPU_SWITCH_CUDA(lhs->ctx.device_type, XPU, #name, {         \
      ATEN_ID_TYPE_SWITCH(lhs->dtype, IdType, {                      \
        ret = impl::BinaryElewise<XPU, IdType, arith::op>(lhs, rhs); \
      });                                                            \
    });                                                              \
    return ret;                                                      \
43
44
45
  }

// Generate operators with only lhs being NDArray.
46
47
48
49
50
51
52
53
54
#define BINARY_ELEMENT_OP_R(name, op)                                \
  IdArray name(int64_t lhs, IdArray rhs) {                           \
    IdArray ret;                                                     \
    ATEN_XPU_SWITCH_CUDA(rhs->ctx.device_type, XPU, #name, {         \
      ATEN_ID_TYPE_SWITCH(rhs->dtype, IdType, {                      \
        ret = impl::BinaryElewise<XPU, IdType, arith::op>(lhs, rhs); \
      });                                                            \
    });                                                              \
    return ret;                                                      \
55
56
57
  }

// Generate operators with only lhs being NDArray.
58
59
60
61
62
63
64
65
66
#define UNARY_ELEMENT_OP(name, op)                             \
  IdArray name(IdArray lhs) {                                  \
    IdArray ret;                                               \
    ATEN_XPU_SWITCH_CUDA(lhs->ctx.device_type, XPU, #name, {   \
      ATEN_ID_TYPE_SWITCH(lhs->dtype, IdType, {                \
        ret = impl::UnaryElewise<XPU, IdType, arith::op>(lhs); \
      });                                                      \
    });                                                        \
    return ret;                                                \
67
68
69
70
71
72
  }

BINARY_ELEMENT_OP(Add, Add)
BINARY_ELEMENT_OP(Sub, Sub)
BINARY_ELEMENT_OP(Mul, Mul)
BINARY_ELEMENT_OP(Div, Div)
73
BINARY_ELEMENT_OP(Mod, Mod)
74
75
76
77
78
79
80
81
82
83
84
BINARY_ELEMENT_OP(GT, GT)
BINARY_ELEMENT_OP(LT, LT)
BINARY_ELEMENT_OP(GE, GE)
BINARY_ELEMENT_OP(LE, LE)
BINARY_ELEMENT_OP(EQ, EQ)
BINARY_ELEMENT_OP(NE, NE)

BINARY_ELEMENT_OP_L(Add, Add)
BINARY_ELEMENT_OP_L(Sub, Sub)
BINARY_ELEMENT_OP_L(Mul, Mul)
BINARY_ELEMENT_OP_L(Div, Div)
85
BINARY_ELEMENT_OP_L(Mod, Mod)
86
87
88
89
90
91
92
93
94
95
96
BINARY_ELEMENT_OP_L(GT, GT)
BINARY_ELEMENT_OP_L(LT, LT)
BINARY_ELEMENT_OP_L(GE, GE)
BINARY_ELEMENT_OP_L(LE, LE)
BINARY_ELEMENT_OP_L(EQ, EQ)
BINARY_ELEMENT_OP_L(NE, NE)

BINARY_ELEMENT_OP_R(Add, Add)
BINARY_ELEMENT_OP_R(Sub, Sub)
BINARY_ELEMENT_OP_R(Mul, Mul)
BINARY_ELEMENT_OP_R(Div, Div)
97
BINARY_ELEMENT_OP_R(Mod, Mod)
98
99
100
101
102
103
104
105
106
107
108
109
110
BINARY_ELEMENT_OP_R(GT, GT)
BINARY_ELEMENT_OP_R(LT, LT)
BINARY_ELEMENT_OP_R(GE, GE)
BINARY_ELEMENT_OP_R(LE, LE)
BINARY_ELEMENT_OP_R(EQ, EQ)
BINARY_ELEMENT_OP_R(NE, NE)

UNARY_ELEMENT_OP(Neg, Neg)

}  // namespace aten
}  // namespace dgl

///////////////// Operator overloading for NDArray /////////////////
111
NDArray operator+(const NDArray& lhs, const NDArray& rhs) {
112
113
  return dgl::aten::Add(lhs, rhs);
}
114
NDArray operator-(const NDArray& lhs, const NDArray& rhs) {
115
116
  return dgl::aten::Sub(lhs, rhs);
}
117
NDArray operator*(const NDArray& lhs, const NDArray& rhs) {
118
119
  return dgl::aten::Mul(lhs, rhs);
}
120
NDArray operator/(const NDArray& lhs, const NDArray& rhs) {
121
122
  return dgl::aten::Div(lhs, rhs);
}
123
NDArray operator%(const NDArray& lhs, const NDArray& rhs) {
124
125
  return dgl::aten::Mod(lhs, rhs);
}
126
NDArray operator+(const NDArray& lhs, int64_t rhs) {
127
128
  return dgl::aten::Add(lhs, rhs);
}
129
NDArray operator-(const NDArray& lhs, int64_t rhs) {
130
131
  return dgl::aten::Sub(lhs, rhs);
}
132
NDArray operator*(const NDArray& lhs, int64_t rhs) {
133
134
  return dgl::aten::Mul(lhs, rhs);
}
135
NDArray operator/(const NDArray& lhs, int64_t rhs) {
136
137
  return dgl::aten::Div(lhs, rhs);
}
138
NDArray operator%(const NDArray& lhs, int64_t rhs) {
139
140
  return dgl::aten::Mod(lhs, rhs);
}
141
NDArray operator+(int64_t lhs, const NDArray& rhs) {
142
143
  return dgl::aten::Add(lhs, rhs);
}
144
NDArray operator-(int64_t lhs, const NDArray& rhs) {
145
146
  return dgl::aten::Sub(lhs, rhs);
}
147
NDArray operator*(int64_t lhs, const NDArray& rhs) {
148
149
  return dgl::aten::Mul(lhs, rhs);
}
150
NDArray operator/(int64_t lhs, const NDArray& rhs) {
151
152
  return dgl::aten::Div(lhs, rhs);
}
153
NDArray operator%(int64_t lhs, const NDArray& rhs) {
154
155
  return dgl::aten::Mod(lhs, rhs);
}
156
NDArray operator-(const NDArray& array) { return dgl::aten::Neg(array); }
157

158
NDArray operator>(const NDArray& lhs, const NDArray& rhs) {
159
160
  return dgl::aten::GT(lhs, rhs);
}
161
NDArray operator<(const NDArray& lhs, const NDArray& rhs) {
162
163
  return dgl::aten::LT(lhs, rhs);
}
164
NDArray operator>=(const NDArray& lhs, const NDArray& rhs) {
165
166
  return dgl::aten::GE(lhs, rhs);
}
167
NDArray operator<=(const NDArray& lhs, const NDArray& rhs) {
168
169
  return dgl::aten::LE(lhs, rhs);
}
170
NDArray operator==(const NDArray& lhs, const NDArray& rhs) {
171
172
  return dgl::aten::EQ(lhs, rhs);
}
173
NDArray operator!=(const NDArray& lhs, const NDArray& rhs) {
174
175
  return dgl::aten::NE(lhs, rhs);
}
176
NDArray operator>(const NDArray& lhs, int64_t rhs) {
177
178
  return dgl::aten::GT(lhs, rhs);
}
179
NDArray operator<(const NDArray& lhs, int64_t rhs) {
180
181
  return dgl::aten::LT(lhs, rhs);
}
182
NDArray operator>=(const NDArray& lhs, int64_t rhs) {
183
184
  return dgl::aten::GE(lhs, rhs);
}
185
NDArray operator<=(const NDArray& lhs, int64_t rhs) {
186
187
  return dgl::aten::LE(lhs, rhs);
}
188
NDArray operator==(const NDArray& lhs, int64_t rhs) {
189
190
  return dgl::aten::EQ(lhs, rhs);
}
191
NDArray operator!=(const NDArray& lhs, int64_t rhs) {
192
193
  return dgl::aten::NE(lhs, rhs);
}
194
NDArray operator>(int64_t lhs, const NDArray& rhs) {
195
196
  return dgl::aten::GT(lhs, rhs);
}
197
NDArray operator<(int64_t lhs, const NDArray& rhs) {
198
199
  return dgl::aten::LT(lhs, rhs);
}
200
NDArray operator>=(int64_t lhs, const NDArray& rhs) {
201
202
  return dgl::aten::GE(lhs, rhs);
}
203
NDArray operator<=(int64_t lhs, const NDArray& rhs) {
204
205
  return dgl::aten::LE(lhs, rhs);
}
206
NDArray operator==(int64_t lhs, const NDArray& rhs) {
207
208
  return dgl::aten::EQ(lhs, rhs);
}
209
NDArray operator!=(int64_t lhs, const NDArray& rhs) {
210
211
  return dgl::aten::NE(lhs, rhs);
}