array_op.h 6.83 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*!
 *  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>
#include <vector>
11
12
#include <tuple>
#include <utility>
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

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);

template <DLDeviceType XPU, typename IdType>
IdArray HStack(IdArray arr1, IdArray arr2);

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

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

45
46
47
48
49
50
template <DLDeviceType XPU, typename DType, typename IdType>
NDArray Scatter(NDArray array, IdArray indices);

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

51
52
53
template <DLDeviceType XPU, typename IdType>
IdArray Relabel_(const std::vector<IdArray>& arrays);

54
55
56
57
58
59
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);

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// 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);

80
template <DLDeviceType XPU, typename IdType>
81
82
runtime::NDArray CSRGetRowData(CSRMatrix csr, int64_t row);

83
template <DLDeviceType XPU, typename IdType>
84
85
runtime::NDArray CSRGetData(CSRMatrix csr, int64_t row, int64_t col);

86
template <DLDeviceType XPU, typename IdType>
87
88
runtime::NDArray CSRGetData(CSRMatrix csr, runtime::NDArray rows, runtime::NDArray cols);

89
template <DLDeviceType XPU, typename IdType>
90
91
92
std::vector<runtime::NDArray> CSRGetDataAndIndices(
    CSRMatrix csr, runtime::NDArray rows, runtime::NDArray cols);

93
template <DLDeviceType XPU, typename IdType>
94
95
96
97
98
99
100
101
102
103
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);

104
template <DLDeviceType XPU, typename IdType>
105
106
CSRMatrix CSRSliceRows(CSRMatrix csr, int64_t start, int64_t end);

107
template <DLDeviceType XPU, typename IdType>
108
109
CSRMatrix CSRSliceRows(CSRMatrix csr, runtime::NDArray rows);

110
template <DLDeviceType XPU, typename IdType>
111
112
CSRMatrix CSRSliceMatrix(CSRMatrix csr, runtime::NDArray rows, runtime::NDArray cols);

113
114
115
template <DLDeviceType XPU, typename IdType>
void CSRSort_(CSRMatrix* csr);

116
117
118
template <DLDeviceType XPU, typename IdType>
CSRMatrix CSRRemove(CSRMatrix csr, IdArray entries);

119
120
121
122
123
124
125
126
127
128
// 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);

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

// FloatType is the type of weight data.
129
template <DLDeviceType XPU, typename IdType, typename DType>
130
COOMatrix CSRRowWiseTopk(
131
    CSRMatrix mat, IdArray rows, int64_t k, NDArray weight, bool ascending);
132
133

///////////////////////////////////////////////////////////////////////////////////////////
Da Zheng's avatar
Da Zheng committed
134

135
136
137
138
139
140
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);

141
142
143
template <DLDeviceType XPU, typename IdType>
bool COOHasDuplicate(COOMatrix coo);

144
145
146
147
148
149
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);

150
template <DLDeviceType XPU, typename IdType>
151
152
153
std::pair<runtime::NDArray, runtime::NDArray>
COOGetRowDataAndIndices(COOMatrix coo, int64_t row);

154
template <DLDeviceType XPU, typename IdType>
155
156
runtime::NDArray COOGetData(COOMatrix coo, int64_t row, int64_t col);

157
template <DLDeviceType XPU, typename IdType>
158
159
160
std::vector<runtime::NDArray> COOGetDataAndIndices(
    COOMatrix coo, runtime::NDArray rows, runtime::NDArray cols);

161
template <DLDeviceType XPU, typename IdType>
162
163
COOMatrix COOTranspose(COOMatrix coo);

164
template <DLDeviceType XPU, typename IdType>
165
166
CSRMatrix COOToCSR(COOMatrix coo);

167
template <DLDeviceType XPU, typename IdType>
168
169
COOMatrix COOSliceRows(COOMatrix coo, int64_t start, int64_t end);

170
template <DLDeviceType XPU, typename IdType>
171
172
COOMatrix COOSliceRows(COOMatrix coo, runtime::NDArray rows);

173
template <DLDeviceType XPU, typename IdType>
174
175
COOMatrix COOSliceMatrix(COOMatrix coo, runtime::NDArray rows, runtime::NDArray cols);

176
177
178
template <DLDeviceType XPU, typename IdType>
std::pair<COOMatrix, IdArray> COOCoalesce(COOMatrix coo);

179
180
181
template <DLDeviceType XPU, typename IdType>
COOMatrix COOSort(COOMatrix mat, bool sort_column);

182
183
184
template <DLDeviceType XPU, typename IdType>
COOMatrix COORemove(COOMatrix coo, IdArray entries);

185
186
187
188
189
190
191
192
193
194
195
196
197
// 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);

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

// 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);
198

199
200
201
202
203
}  // namespace impl
}  // namespace aten
}  // namespace dgl

#endif  // DGL_ARRAY_ARRAY_OP_H_