-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmask_hash_mult.h
241 lines (208 loc) · 5.67 KB
/
mask_hash_mult.h
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* @file
* hash_mult.h
*
* @author
*
* @date
*
* @brief
* Masked SpGEMM with hash tables as accumulators
*
* @todo
*
* @note
*
*/
#include <algorithm>
#include <omp.h>
#include "CSR.h"
#include "mask_hash.h"
#include "utility.h"
// With bin
template <typename IT,
typename NT,
typename MultiplyOperation,
typename AddOperation>
void
mxm_hash_mask
(
const CSR<IT, NT> &A,
const CSR<IT, NT> &B,
CSR<IT, NT> &C,
const CSR<IT, NT> &M,
MultiplyOperation multop,
AddOperation addop,
unsigned threadCount
)
{
C.rows = A.rows;
C.cols = B.cols;
C.zerobased = true;
C.rowptr = my_malloc<IT>(C.rows + 1);
IT *row_nz = my_malloc<IT>(C.rows);
int numThreads;
#pragma omp parallel num_threads(threadCount)
{
numThreads = omp_get_num_threads();
}
BIN<IT, NT> bin(A.rows, IMB_PWMIN, numThreads);
/* Set max bin */
bin.set_max_bin(A.rowptr, A.colids, B.rowptr, C.rows, C.cols);
/* Create hash table (thread local) */
bin.create_local_hash_table(C.cols);
IT rowPerThread = (M.rows + numThreads -1) / numThreads;
#pragma omp parallel num_threads(threadCount)
{
IT i, tid, start_row, end_row, max_row = 0, ra;
tid = omp_get_thread_num();
start_row = bin.rows_offset[tid];
end_row = bin.rows_offset[tid + 1];
// start_row = rowPerThread * tid;
// end_row = min(rowPerThread * (tid+1), M.rows);
for (ra = start_row; ra < end_row; ++ra){
max_row = max(max_row, M.rowptr[ra+1] - M.rowptr[ra]);
}
map_lp<IT, bool> ht(max_row + 1);
for (ra = start_row; ra < end_row; ++ra)
// for (IT ra = 0; ra < A.rows; ++ra)
{
// map_lp<IT, bool> ht(M.rowptr[ra+1] - M.rowptr[ra] + 1);
for (IT cmptr = M.rowptr[ra]; cmptr < M.rowptr[ra+1]; ++cmptr)
ht.insert(M.colids[cmptr], false);
row_nz[ra] = 0;
for (IT captr = A.rowptr[ra]; captr < A.rowptr[ra+1]; ++captr)
{
IT rb = A.colids[captr];
for (IT cbptr = B.rowptr[rb]; cbptr < B.rowptr[rb+1]; ++cbptr)
{
auto hv = ht.find(B.colids[cbptr]);
if (hv != -1 && !ht[hv])
{
++row_nz[ra];
ht[hv] = true;
}
}
}
ht.reset();
}
}
scan(row_nz, C.rowptr, C.rows + 1);
my_free<IT>(row_nz);
C.nnz = C.rowptr[C.rows];
C.colids = my_malloc<IT>(C.nnz);
C.values = my_malloc<NT>(C.nnz);
#pragma omp parallel num_threads(threadCount)
{
IT i, tid, start_row, end_row, max_row = 0, ra;
tid = omp_get_thread_num();
start_row = bin.rows_offset[tid];
end_row = bin.rows_offset[tid + 1];
// start_row = rowPerThread * tid;
// end_row = min(rowPerThread * (tid+1), M.rows);
for (ra = start_row; ra < end_row; ++ra){
max_row = max(max_row, M.rowptr[ra+1] - M.rowptr[ra]);
}
map_lp<IT, NT, bool> ht(max_row + 1);
for (ra = start_row; ra < end_row; ++ra)
{
// map_lp<IT, NT, bool> ht(M.rowptr[ra+1] - M.rowptr[ra] + 1);
for (IT cmptr = M.rowptr[ra]; cmptr < M.rowptr[ra+1]; ++cmptr)
ht.insert(M.colids[cmptr], NT(), false);
for (IT captr = A.rowptr[ra]; captr < A.rowptr[ra+1]; ++captr)
{
IT rb = A.colids[captr];
for (IT cbptr = B.rowptr[rb]; cbptr < B.rowptr[rb+1]; ++cbptr)
{
auto hv = ht.find(B.colids[cbptr]);
if (hv != -1 && !ht.get2(hv))
{
ht.get1(hv) = multop(A.values[captr], B.values[cbptr]);
ht.get2(hv) = true;
}
else if (hv != -1 && ht.get2(hv))
ht.get1(hv) =
addop(ht.get1(hv),
multop(A.values[captr], B.values[cbptr]));
}
}
ht.gather(C.colids + C.rowptr[ra], C.values + C.rowptr[ra]);
}
}
return;
}
// notes (IN): parallelism over rows, HT allocation for each row
template <typename IT,
typename NT,
typename MultiplyOperation,
typename AddOperation>
void
mxm_hash_mask_wobin
(
const CSR<IT, NT> &A,
const CSR<IT, NT> &B,
CSR<IT, NT> &C,
const CSR<IT, NT> &M,
MultiplyOperation multop,
AddOperation addop,
unsigned threadCount
)
{
C.rows = A.rows;
C.cols = B.cols;
C.zerobased = true;
C.rowptr = my_malloc<IT>(C.rows + 1);
IT *row_nz = my_malloc<IT>(C.rows);
#pragma omp parallel for num_threads(threadCount)
for (IT ra = 0; ra < A.rows; ++ra)
{
map_lp<IT, bool> ht(M.rowptr[ra+1] - M.rowptr[ra] + 1);
for (IT cmptr = M.rowptr[ra]; cmptr < M.rowptr[ra+1]; ++cmptr)
ht.insert(M.colids[cmptr], false);
row_nz[ra] = 0;
for (IT captr = A.rowptr[ra]; captr < A.rowptr[ra+1]; ++captr)
{
IT rb = A.colids[captr];
for (IT cbptr = B.rowptr[rb]; cbptr < B.rowptr[rb+1]; ++cbptr)
{
auto hv = ht.find(B.colids[cbptr]);
if (hv != -1 && !ht[hv])
{
++row_nz[ra];
ht[hv] = true;
}
}
}
}
scan(row_nz, C.rowptr, C.rows + 1);
my_free<IT>(row_nz);
C.nnz = C.rowptr[C.rows];
C.colids = my_malloc<IT>(C.nnz);
C.values = my_malloc<NT>(C.nnz);
#pragma omp parallel for num_threads(threadCount)
for (IT ra = 0; ra < A.rows; ++ra)
{
map_lp<IT, NT, bool> ht(M.rowptr[ra+1] - M.rowptr[ra] + 1);
for (IT cmptr = M.rowptr[ra]; cmptr < M.rowptr[ra+1]; ++cmptr)
ht.insert(M.colids[cmptr], NT(), false);
for (IT captr = A.rowptr[ra]; captr < A.rowptr[ra+1]; ++captr)
{
IT rb = A.colids[captr];
for (IT cbptr = B.rowptr[rb]; cbptr < B.rowptr[rb+1]; ++cbptr)
{
auto hv = ht.find(B.colids[cbptr]);
if (hv != -1 && !ht.get2(hv))
{
ht.get1(hv) = multop(A.values[captr], B.values[cbptr]);
ht.get2(hv) = true;
}
else if (hv != -1 && ht.get2(hv))
ht.get1(hv) =
addop(ht.get1(hv),
multop(A.values[captr], B.values[cbptr]));
}
}
ht.gather(C.colids + C.rowptr[ra], C.values + C.rowptr[ra]);
}
return;
}