forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparse_arith.hpp
More file actions
225 lines (187 loc) · 6.47 KB
/
sparse_arith.hpp
File metadata and controls
225 lines (187 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*******************************************************
* Copyright (c) 2015, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#pragma once
#include <Param.hpp>
#include <math.hpp>
#include <cmath>
namespace cpu {
namespace kernel {
template<typename T, af_op_t op>
struct arith_op {
T operator()(T v1, T v2) {
UNUSED(v1);
UNUSED(v2);
return scalar<T>(0);
}
};
template<typename T>
struct arith_op<T, af_add_t> {
T operator()(T v1, T v2) { return v1 + v2; }
};
template<typename T>
struct arith_op<T, af_sub_t> {
T operator()(T v1, T v2) { return v1 - v2; }
};
template<typename T>
struct arith_op<T, af_mul_t> {
T operator()(T v1, T v2) { return v1 * v2; }
};
template<typename T>
struct arith_op<T, af_div_t> {
T operator()(T v1, T v2) { return v1 / v2; }
};
template<typename T, af_op_t op, af_storage type>
void sparseArithOpD(Param<T> output, CParam<T> values, CParam<int> rowIdx,
CParam<int> colIdx, CParam<T> rhs,
const bool reverse = false) {
T *oPtr = output.get();
const T *hPtr = rhs.get();
const T *vPtr = values.get();
const int *rPtr = rowIdx.get();
const int *cPtr = colIdx.get();
dim4 odims = output.dims();
dim4 ostrides = output.strides();
;
dim4 hstrides = rhs.strides();
;
std::vector<int> temp;
if (type == AF_STORAGE_CSR) {
temp.resize(values.dims().elements());
for (int i = 0; i < rowIdx.dims(0) - 1; i++) {
for (int ii = rPtr[i]; ii < rPtr[i + 1]; ii++) { temp[ii] = i; }
}
//} else if(type == AF_STORAGE_CSC) { // For future
}
const int *xx = (type == AF_STORAGE_CSR) ? temp.data() : rPtr;
const int *yy = (type == AF_STORAGE_CSC) ? temp.data() : cPtr;
for (int i = 0; i < (int)values.dims().elements(); i++) {
// Bad index data
if (xx[i] >= odims[0] || yy[i] >= odims[1]) continue;
int offset = xx[i] + yy[i] * ostrides[1];
int hoff = xx[i] + yy[i] * hstrides[1];
if (reverse)
oPtr[offset] = arith_op<T, op>()(hPtr[hoff], vPtr[i]);
else
oPtr[offset] = arith_op<T, op>()(vPtr[i], hPtr[hoff]);
}
}
template<typename T, af_op_t op, af_storage type>
void sparseArithOpS(Param<T> values, Param<int> rowIdx, Param<int> colIdx,
CParam<T> rhs, const bool reverse = false) {
T *vPtr = values.get();
const int *rPtr = rowIdx.get();
const int *cPtr = colIdx.get();
const T *hPtr = rhs.get();
dim4 dims = rhs.dims();
dim4 hstrides = rhs.strides();
std::vector<int> temp;
if (type == AF_STORAGE_CSR) {
temp.resize(values.dims().elements());
for (int i = 0; i < rowIdx.dims(0) - 1; i++) {
for (int ii = rPtr[i]; ii < rPtr[i + 1]; ii++) { temp[ii] = i; }
}
//} else if(type == AF_STORAGE_CSC) { // For future
}
const int *xx = (type == AF_STORAGE_CSR) ? temp.data() : rPtr;
const int *yy = (type == AF_STORAGE_CSC) ? temp.data() : cPtr;
for (int i = 0; i < (int)values.dims().elements(); i++) {
// Bad index data
if (xx[i] >= dims[0] || yy[i] >= dims[1]) continue;
int hoff = xx[i] + yy[i] * hstrides[1];
if (reverse)
vPtr[i] = arith_op<T, op>()(hPtr[hoff], vPtr[i]);
else
vPtr[i] = arith_op<T, op>()(vPtr[i], hPtr[hoff]);
}
}
// The following functions can handle CSR
// storage format only as of now.
static void calcOutNNZ(Param<int> outRowIdx, const uint M, const uint N,
CParam<int> lRowIdx, CParam<int> lColIdx,
CParam<int> rRowIdx, CParam<int> rColIdx) {
UNUSED(N);
int *orPtr = outRowIdx.get();
const int *lrPtr = lRowIdx.get();
const int *lcPtr = lColIdx.get();
const int *rrPtr = rRowIdx.get();
const int *rcPtr = rColIdx.get();
unsigned csrOutCount = 0;
for (uint row = 0; row < M; ++row) {
const int lEnd = lrPtr[row + 1];
const int rEnd = rrPtr[row + 1];
uint rowNNZ = 0;
int l = lrPtr[row];
int r = rrPtr[row];
while (l < lEnd && r < rEnd) {
int lci = lcPtr[l];
int rci = rcPtr[r];
l += (lci <= rci);
r += (lci >= rci);
rowNNZ++;
}
// Elements from lhs or rhs are exhausted.
// Just count left over elements
rowNNZ += (lEnd - l);
rowNNZ += (rEnd - r);
orPtr[row] = csrOutCount;
csrOutCount += rowNNZ;
}
// Write out the Rows+1 entry
orPtr[M] = csrOutCount;
}
template<typename T, af_op_t op>
void sparseArithOp(Param<T> oVals, Param<int> oColIdx, CParam<int> oRowIdx,
const uint Rows, CParam<T> lvals, CParam<int> lRowIdx,
CParam<int> lColIdx, CParam<T> rvals, CParam<int> rRowIdx,
CParam<int> rColIdx) {
const int *orPtr = oRowIdx.get();
const T *lvPtr = lvals.get();
const int *lrPtr = lRowIdx.get();
const int *lcPtr = lColIdx.get();
const T *rvPtr = rvals.get();
const int *rrPtr = rRowIdx.get();
const int *rcPtr = rColIdx.get();
arith_op<T, op> binOp;
auto ZERO = scalar<T>(0);
for (uint row = 0; row < Rows; ++row) {
const int lEnd = lrPtr[row + 1];
const int rEnd = rrPtr[row + 1];
const int offs = orPtr[row];
T *ovPtr = oVals.get() + offs;
int *ocPtr = oColIdx.get() + offs;
uint rowNNZ = 0;
int l = lrPtr[row];
int r = rrPtr[row];
while (l < lEnd && r < rEnd) {
int lci = lcPtr[l];
int rci = rcPtr[r];
T lhs = (lci <= rci ? lvPtr[l] : ZERO);
T rhs = (lci >= rci ? rvPtr[r] : ZERO);
ovPtr[rowNNZ] = binOp(lhs, rhs);
ocPtr[rowNNZ] = (lci <= rci) ? lci : rci;
l += (lci <= rci);
r += (lci >= rci);
rowNNZ++;
}
while (l < lEnd) {
ovPtr[rowNNZ] = binOp(lvPtr[l], ZERO);
ocPtr[rowNNZ] = lcPtr[l];
l++;
rowNNZ++;
}
while (r < rEnd) {
ovPtr[rowNNZ] = binOp(ZERO, rvPtr[r]);
ocPtr[rowNNZ] = rcPtr[r];
r++;
rowNNZ++;
}
}
}
} // namespace kernel
} // namespace cpu