array_op.h 10.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*!
 *  Copyright (c) 2019 by Contributors
 * \file array/array_op.h
 * \brief Array operator templates
 */
#ifndef DGL_ARRAY_ARRAY_OP_H_
#define DGL_ARRAY_ARRAY_OP_H_

#include <dgl/array.h>
10
#include <dgl/graph_traversal.h>
11
#include <vector>
12
13
#include <tuple>
#include <utility>
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

namespace dgl {
namespace aten {
namespace impl {

template <DLDeviceType XPU, typename IdType>
IdArray Full(IdType val, int64_t length, DLContext ctx);

template <DLDeviceType XPU, typename IdType>
IdArray Range(IdType low, IdType high, DLContext ctx);

template <DLDeviceType XPU, typename IdType>
IdArray AsNumBits(IdArray arr, uint8_t bits);

template <DLDeviceType XPU, typename IdType, typename Op>
IdArray BinaryElewise(IdArray lhs, IdArray rhs);

template <DLDeviceType XPU, typename IdType, typename Op>
IdArray BinaryElewise(IdArray lhs, IdType rhs);

template <DLDeviceType XPU, typename IdType, typename Op>
IdArray BinaryElewise(IdType lhs, IdArray rhs);

37
38
39
template <DLDeviceType XPU, typename IdType, typename Op>
IdArray UnaryElewise(IdArray array);

40
41
template <DLDeviceType XPU, typename DType, typename IdType>
NDArray IndexSelect(NDArray array, IdArray index);
42

43
template <DLDeviceType XPU, typename DType>
44
DType IndexSelect(NDArray array, int64_t index);
45

Jinjing Zhou's avatar
Jinjing Zhou committed
46
47
48
template <DLDeviceType XPU, typename DType>
IdArray NonZero(BoolArray bool_arr);

49
template <DLDeviceType XPU, typename DType>
50
std::pair<IdArray, IdArray> Sort(IdArray array, int num_bits);
51

52
53
54
template <DLDeviceType XPU, typename DType, typename IdType>
NDArray Scatter(NDArray array, IdArray indices);

55
56
57
template <DLDeviceType XPU, typename DType, typename IdType>
void Scatter_(IdArray index, NDArray value, NDArray out);

58
59
60
template <DLDeviceType XPU, typename DType, typename IdType>
NDArray Repeat(NDArray array, IdArray repeats);

61
62
63
template <DLDeviceType XPU, typename IdType>
IdArray Relabel_(const std::vector<IdArray>& arrays);

64
65
66
template <DLDeviceType XPU, typename IdType>
NDArray Concat(const std::vector<IdArray>& arrays);

67
68
69
70
71
72
template <DLDeviceType XPU, typename DType>
std::tuple<NDArray, IdArray, IdArray> Pack(NDArray array, DType pad_value);

template <DLDeviceType XPU, typename DType, typename IdType>
std::pair<NDArray, IdArray> ConcatSlices(NDArray array, IdArray lengths);

73
74
75
template <DLDeviceType XPU, typename IdType>
IdArray CumSum(IdArray array, bool prepend_zero);

76
77
78
template <DLDeviceType XPU, typename IdType>
IdArray NonZero(NDArray array);

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// sparse arrays

template <DLDeviceType XPU, typename IdType>
bool CSRIsNonZero(CSRMatrix csr, int64_t row, int64_t col);

template <DLDeviceType XPU, typename IdType>
runtime::NDArray CSRIsNonZero(CSRMatrix csr, runtime::NDArray row, runtime::NDArray col);

template <DLDeviceType XPU, typename IdType>
bool CSRHasDuplicate(CSRMatrix csr);

template <DLDeviceType XPU, typename IdType>
int64_t CSRGetRowNNZ(CSRMatrix csr, int64_t row);

template <DLDeviceType XPU, typename IdType>
runtime::NDArray CSRGetRowNNZ(CSRMatrix csr, runtime::NDArray row);

template <DLDeviceType XPU, typename IdType>
runtime::NDArray CSRGetRowColumnIndices(CSRMatrix csr, int64_t row);

99
template <DLDeviceType XPU, typename IdType>
100
101
runtime::NDArray CSRGetRowData(CSRMatrix csr, int64_t row);

102
103
104
template <DLDeviceType XPU, typename IdType>
bool CSRIsSorted(CSRMatrix csr);

105
106
template <DLDeviceType XPU, typename IdType, typename DType>
runtime::NDArray CSRGetData(
107
    CSRMatrix csr, runtime::NDArray rows, runtime::NDArray cols, bool return_eids,
108
109
    runtime::NDArray weights, DType filler);

110
111
112
113
114
115
116
template <DLDeviceType XPU, typename IdType, typename DType>
runtime::NDArray CSRGetData(
    CSRMatrix csr, runtime::NDArray rows, runtime::NDArray cols,
    runtime::NDArray weights, DType filler) {
  return CSRGetData<XPU, IdType, DType>(csr, rows, cols, false, weights, filler);
}

117
template <DLDeviceType XPU, typename IdType>
118
NDArray CSRGetData(CSRMatrix csr, NDArray rows, NDArray cols) {
119
  return CSRGetData<XPU, IdType, IdType>(csr, rows, cols, true, NullArray(rows->dtype), -1);
120
}
121

122
template <DLDeviceType XPU, typename IdType>
123
124
125
std::vector<runtime::NDArray> CSRGetDataAndIndices(
    CSRMatrix csr, runtime::NDArray rows, runtime::NDArray cols);

126
template <DLDeviceType XPU, typename IdType>
127
128
129
130
131
132
133
134
135
136
CSRMatrix CSRTranspose(CSRMatrix csr);

// Convert CSR to COO
template <DLDeviceType XPU, typename IdType>
COOMatrix CSRToCOO(CSRMatrix csr);

// Convert CSR to COO using data array as order
template <DLDeviceType XPU, typename IdType>
COOMatrix CSRToCOODataAsOrder(CSRMatrix csr);

137
template <DLDeviceType XPU, typename IdType>
138
139
CSRMatrix CSRSliceRows(CSRMatrix csr, int64_t start, int64_t end);

140
template <DLDeviceType XPU, typename IdType>
141
142
CSRMatrix CSRSliceRows(CSRMatrix csr, runtime::NDArray rows);

143
template <DLDeviceType XPU, typename IdType>
144
145
CSRMatrix CSRSliceMatrix(CSRMatrix csr, runtime::NDArray rows, runtime::NDArray cols);

146
147
148
template <DLDeviceType XPU, typename IdType>
void CSRSort_(CSRMatrix* csr);

149
150
151
152
template <DLDeviceType XPU, typename IdType, typename TagType>
std::pair<CSRMatrix, NDArray> CSRSortByTag(
    const CSRMatrix &csr, IdArray tag_array, int64_t num_tags);

Da Zheng's avatar
Da Zheng committed
153
154
155
156
157
158
template <DLDeviceType XPU, typename IdType>
CSRMatrix CSRReorder(CSRMatrix csr, runtime::NDArray new_row_ids, runtime::NDArray new_col_ids);

template <DLDeviceType XPU, typename IdType>
COOMatrix COOReorder(COOMatrix coo, runtime::NDArray new_row_ids, runtime::NDArray new_col_ids);

159
160
161
template <DLDeviceType XPU, typename IdType>
CSRMatrix CSRRemove(CSRMatrix csr, IdArray entries);

162
163
164
165
166
// FloatType is the type of probability data.
template <DLDeviceType XPU, typename IdType, typename FloatType>
COOMatrix CSRRowWiseSampling(
    CSRMatrix mat, IdArray rows, int64_t num_samples, FloatArray prob, bool replace);

167
168
169
170
// FloatType is the type of probability data.
template <DLDeviceType XPU, typename IdType, typename FloatType>
COOMatrix CSRRowWisePerEtypeSampling(
    CSRMatrix mat, IdArray rows, IdArray etypes,
171
172
    const std::vector<int64_t>& num_samples, FloatArray prob, bool replace,
    bool etype_sorted);
173

174
175
176
177
template <DLDeviceType XPU, typename IdType>
COOMatrix CSRRowWiseSamplingUniform(
    CSRMatrix mat, IdArray rows, int64_t num_samples, bool replace);

178
179
template <DLDeviceType XPU, typename IdType>
COOMatrix CSRRowWisePerEtypeSamplingUniform(
180
    CSRMatrix mat, IdArray rows, IdArray etypes, const std::vector<int64_t>& num_samples,
181
    bool replace, bool etype_sorted);
182

183
// FloatType is the type of weight data.
184
template <DLDeviceType XPU, typename IdType, typename DType>
185
COOMatrix CSRRowWiseTopk(
186
    CSRMatrix mat, IdArray rows, int64_t k, NDArray weight, bool ascending);
187

188
189
190
191
192
193
194
195
196
197
template <DLDeviceType XPU, typename IdType, typename FloatType>
COOMatrix CSRRowWiseSamplingBiased(
    CSRMatrix mat,
    IdArray rows,
    int64_t num_samples,
    NDArray tag_offset,
    FloatArray bias,
    bool replace
);

198
199
200
201
// Union CSRMatrixes
template <DLDeviceType XPU, typename IdType>
CSRMatrix UnionCsr(const std::vector<CSRMatrix>& csrs);

202
203
template <DLDeviceType XPU, typename IdType>
std::tuple<CSRMatrix, IdArray, IdArray> CSRToSimple(CSRMatrix csr);
204

205
///////////////////////////////////////////////////////////////////////////////////////////
Da Zheng's avatar
Da Zheng committed
206

207
208
209
210
211
212
template <DLDeviceType XPU, typename IdType>
bool COOIsNonZero(COOMatrix coo, int64_t row, int64_t col);

template <DLDeviceType XPU, typename IdType>
runtime::NDArray COOIsNonZero(COOMatrix coo, runtime::NDArray row, runtime::NDArray col);

213
214
215
template <DLDeviceType XPU, typename IdType>
bool COOHasDuplicate(COOMatrix coo);

216
217
218
219
220
221
template <DLDeviceType XPU, typename IdType>
int64_t COOGetRowNNZ(COOMatrix coo, int64_t row);

template <DLDeviceType XPU, typename IdType>
runtime::NDArray COOGetRowNNZ(COOMatrix coo, runtime::NDArray row);

222
template <DLDeviceType XPU, typename IdType>
223
224
225
std::pair<runtime::NDArray, runtime::NDArray>
COOGetRowDataAndIndices(COOMatrix coo, int64_t row);

226
template <DLDeviceType XPU, typename IdType>
227
228
229
std::vector<runtime::NDArray> COOGetDataAndIndices(
    COOMatrix coo, runtime::NDArray rows, runtime::NDArray cols);

230
231
232
template <DLDeviceType XPU, typename IdType>
runtime::NDArray COOGetData(COOMatrix mat, runtime::NDArray rows, runtime::NDArray cols);

233
template <DLDeviceType XPU, typename IdType>
234
235
COOMatrix COOTranspose(COOMatrix coo);

236
template <DLDeviceType XPU, typename IdType>
237
238
CSRMatrix COOToCSR(COOMatrix coo);

239
template <DLDeviceType XPU, typename IdType>
240
241
COOMatrix COOSliceRows(COOMatrix coo, int64_t start, int64_t end);

242
template <DLDeviceType XPU, typename IdType>
243
244
COOMatrix COOSliceRows(COOMatrix coo, runtime::NDArray rows);

245
template <DLDeviceType XPU, typename IdType>
246
247
COOMatrix COOSliceMatrix(COOMatrix coo, runtime::NDArray rows, runtime::NDArray cols);

248
249
250
template <DLDeviceType XPU, typename IdType>
std::pair<COOMatrix, IdArray> COOCoalesce(COOMatrix coo);

251
template <DLDeviceType XPU, typename IdType>
252
253
254
255
void COOSort_(COOMatrix* mat, bool sort_column);

template <DLDeviceType XPU, typename IdType>
std::pair<bool, bool> COOIsSorted(COOMatrix coo);
256

257
258
259
template <DLDeviceType XPU, typename IdType>
COOMatrix COORemove(COOMatrix coo, IdArray entries);

260
261
262
263
264
// FloatType is the type of probability data.
template <DLDeviceType XPU, typename IdType, typename FloatType>
COOMatrix COORowWiseSampling(
    COOMatrix mat, IdArray rows, int64_t num_samples, FloatArray prob, bool replace);

265
266
267
268
// FloatType is the type of probability data.
template <DLDeviceType XPU, typename IdType, typename FloatType>
COOMatrix COORowWisePerEtypeSampling(
    COOMatrix mat, IdArray rows, IdArray etypes,
269
    const std::vector<int64_t>& num_samples, FloatArray prob, bool replace, bool etype_sorted);
270

271
272
273
274
template <DLDeviceType XPU, typename IdType>
COOMatrix COORowWiseSamplingUniform(
    COOMatrix mat, IdArray rows, int64_t num_samples, bool replace);

275
276
template <DLDeviceType XPU, typename IdType>
COOMatrix COORowWisePerEtypeSamplingUniform(
277
    COOMatrix mat, IdArray rows, IdArray etypes, const std::vector<int64_t>& num_samples,
278
    bool replace, bool etype_sorted);
279

280
281
282
283
// FloatType is the type of weight data.
template <DLDeviceType XPU, typename IdType, typename FloatType>
COOMatrix COORowWiseTopk(
    COOMatrix mat, IdArray rows, int64_t k, FloatArray weight, bool ascending);
284

285
286
///////////////////////// Graph Traverse routines //////////////////////////

287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
template <DLDeviceType XPU, typename IdType>
Frontiers BFSNodesFrontiers(const CSRMatrix& csr, IdArray source);

template <DLDeviceType XPU, typename IdType>
Frontiers BFSEdgesFrontiers(const CSRMatrix& csr, IdArray source);

template <DLDeviceType XPU, typename IdType>
Frontiers TopologicalNodesFrontiers(const CSRMatrix& csr);

template <DLDeviceType XPU, typename IdType>
Frontiers DGLDFSEdges(const CSRMatrix& csr, IdArray source);

template <DLDeviceType XPU, typename IdType>
Frontiers DGLDFSLabeledEdges(const CSRMatrix& csr,
                             IdArray source,
                             const bool has_reverse_edge,
                             const bool has_nontree_edge,
                             const bool return_labels);

306
307
template <DLDeviceType XPU, typename IdType>
COOMatrix COOLineGraph(const COOMatrix &coo, bool backtracking);
308

309
310
311
312
313
}  // namespace impl
}  // namespace aten
}  // namespace dgl

#endif  // DGL_ARRAY_ARRAY_OP_H_