-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhash_mult_flengthcluster.h
More file actions
294 lines (258 loc) · 11.9 KB
/
hash_mult_flengthcluster.h
File metadata and controls
294 lines (258 loc) · 11.9 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <immintrin.h>
#include <algorithm>
#include <fstream>
#include <x86intrin.h>
#include "utility.h"
#include "CSR.h"
#include "CSR_FlengthCluster.h"
#include "BIN_FlengthCluster.h"
/* SpGEMM Specific Parameters */
#define HASH_SCAL 107 // Set disjoint number to hash table size (=2^n)
#define SMALL_THRESHOLD 100
#define MIN_HT_S 8 // minimum hash table size per row in symbolic phase
#define MIN_HT_N 8 // minimum hash table size per row in numeric phase
/*
* Symbolic phase for HashSpGEMMCluster.
*/
template<class IT, class NT>
inline void hash_symbolic_kernel_cluster(const IT *arpt, const IT *acol,
const IT *brpt, const IT *bcol,
BIN_FlengthCluster<IT, NT> &bin) {
#pragma omp parallel
{
IT tid = omp_get_thread_num();
IT start_row = bin.clusters_offset[tid]; // start cluster
IT end_row = bin.clusters_offset[tid + 1]; // end cluster
IT *check = bin.local_hash_table_id[tid];
IT t_acol, key, hash;
for (IT i = start_row; i < end_row; ++i) { // loop over clusters
IT nz = 0;
IT bid = bin.bin_id_cluster[i];
if (bid > 0) {
IT ht_size = MIN_HT_S << (bid - 1); // determine hash table size for i-th cluster
for (IT j = 0; j < ht_size; ++j) { // initialize hash table
check[j] = -1;
}
for (IT j = arpt[i]; j < arpt[i + 1]; ++j) { // loop over the columns of cluster[i]
t_acol = acol[j];
for (IT k = brpt[t_acol]; k < brpt[t_acol + 1]; ++k) { // loop over columns of B (B.row selected by columns of cluster[i])
key = bcol[k];
hash = (key * HASH_SCAL) & (ht_size - 1);
while (1) { // loop for hash probing
if (check[hash] == key) { // if the key is already inserted, it's ok
break;
} else if (check[hash] == -1) { // if the key has not been inserted yet, then it's added.
check[hash] = key;
nz++;
break;
} else { // linear probing: check next entry
hash = (hash + 1) & (ht_size - 1); // hash = (hash + 1) % ht_size
}
}
}
}
}
bin.cluster_nz[i] = nz; // updating cluster_nz by row[i] nnz; previously it was set to flops of row[i]
}
}
}
// Reference function for Symbolic phase of HashSpGEMMCluster
template<class IT, class NT>
inline void hash_symbolic_cluster(const IT *arpt, const IT *acol, const IT *brpt, const IT *bcol,
IT *crpt, BIN_FlengthCluster<IT, NT> &bin, const IT nrow, IT *nnz) {
hash_symbolic_kernel_cluster(arpt, acol, brpt, bcol, bin);
/* Set row pointer of matrix C */
scan(bin.cluster_nz, crpt, nrow + 1);
*nnz = crpt[nrow];
}
/*
* Used for sort function.
* Elements are sorted in ascending order.
*/
template<typename IT, typename NT>
bool sort_less_V1(const pair<IT, NT> &left, const pair<IT, NT> &right) {
return left.first < right.first;
}
/*
* After calculating on each hash table, sort them in ascending order if necessary, and then store them as output matrix
* This function is used in hash_numeric* function.
* the actual indices of colids and values of output matrix are rpt[rowid];
*/
template<bool sortOutput, typename IT, typename NT>
inline void sort_and_store_table2mat_cluster(IT *ht_check, NT *ht_value,
IT *colids, NT *values,
IT nz, IT ht_size, IT offset, IT cluster_sz,
const IT cluster_id, const IT csr_rows) {
IT index = 0;
IT val_idx, ht_idx;
// Sort elements in ascending order if necessary, and store them as output matrix
if (sortOutput) {
vector <pair<IT, IT>> p_vec(nz); // <col-id, position-in-hashtable>
for (IT j = 0; j < ht_size; ++j) { // accumulate non-zero entry from hash table
if (ht_check[j] != -1) {
p_vec[index++] = make_pair(ht_check[j], j);
}
}
// assert(index <= nz && "Index goes beyond p_vector limit");
// assert(index < offset && "Index goes beyond output limit");
sort(p_vec.begin(), p_vec.end(), sort_less_V1<IT, IT>); // sort only non-zero elements
// store the results
for (IT j = 0; j < index; ++j) {
colids[j] = p_vec[j].first;
val_idx = (j * cluster_sz);
ht_idx = (p_vec[j].second * cluster_sz);
for (IT l = 0; l < cluster_sz; l += 1) {
values[val_idx + l] = ht_value[ht_idx + l];
}
}
} else {
// store the results
for (IT j = 0; j < ht_size; ++j) {
if (ht_check[j] != -1) {
colids[index] = ht_check[j];
val_idx = (index * cluster_sz);
ht_idx = (j * cluster_sz);
for (IT l = 0; l < cluster_sz; l += 1) {
values[val_idx + l] = ht_value[ht_idx + l];
}
index++;
}
}
}
}
/*
* Numeric phase in HashSpGEMMCluster.
*/
template<bool sortOutput, typename IT, typename NT, typename MultiplyOperation, typename AddOperation>
inline void hash_numeric_cluster(const IT *arpt, const IT *acol, const NT *aval,
const IT *brpt, const IT *bcol, const NT *bval,
const IT *crpt, IT *ccol, NT *cval, const BIN_FlengthCluster<IT, NT> &bin,
const MultiplyOperation multop, const AddOperation addop, IT cnnz,
const int csr_rows, const IT cluster_sz, const NT eps = 0.000001f) {
#pragma omp parallel
{
IT tid = omp_get_thread_num();
IT start_row = bin.clusters_offset[tid];
IT end_row = bin.clusters_offset[tid + 1];
IT *ht_check = bin.local_hash_table_id[tid];
NT *ht_value = bin.local_hash_table_val[tid];
IT t_acol;
NT t_aval, t_val;
for (IT i = start_row; i < end_row; ++i) { // A.clusters
// note: BIN class is based on the cluster (not in row order of the original CSR)
IT bid = bin.bin_id_cluster[i];
if (bid > 0) {
IT offset = crpt[i];
IT ht_size = MIN_HT_N << (bid - 1);
for (IT j = 0; j < ht_size; ++j) {
ht_check[j] = -1;
}
for (IT j = arpt[i]; j < arpt[i + 1]; ++j) { // union of col-ids of cluster A.cluster[i]
t_acol = acol[j];
for (IT k = brpt[t_acol]; k < brpt[t_acol + 1]; ++k) { // B.cols
IT key = bcol[k];
IT hash = (key * HASH_SCAL) & (ht_size - 1);
while (1) { // loop for hash probing
if (ht_check[hash] == key) { // key is already inserted
for (IT l = 0; l < cluster_sz; l += 1) { // loop over all the rows of A.cluster[i].col[j]
t_aval = aval[(j * cluster_sz) + l]; // value from A
// note: maybe we can use a bitmap to find whether [(j * cluster_sz) + l]-pos is valid
if (fabs(t_aval - 0.0f) >= eps) { // avoid flop when (A.value[] == 0.0)
t_val = multop(t_aval, bval[k]); // value for C
ht_value[(hash * cluster_sz) + l] = addop(t_val, ht_value[(hash * cluster_sz) + l]);
}
}
break;
} else if (ht_check[hash] == -1) { // insert new entry
ht_check[hash] = key;
for (IT l = 0; l < cluster_sz; l += 1) { // loop over all the rows of A.cluster[i].col[j]
t_aval = aval[(j * cluster_sz) + l]; // value from A
t_val = multop(t_aval, bval[k]); // value for C
// ht_value will be automatically initialized by 0.0 if t_val is zero
ht_value[(hash * cluster_sz) + l] = t_val;
}
break;
} else {
hash = (hash + 1) & (ht_size - 1); // (hash + 1) % ht_size
}
}
}
}
// copy results from ht to the C_csr
sort_and_store_table2mat_cluster<sortOutput, IT, NT>(ht_check, ht_value,
ccol + offset, cval + (offset * cluster_sz),
(crpt[i + 1] - offset), ht_size, cnnz - offset,
cluster_sz, i, csr_rows);
}
}
}
}
/*
* Executing HashSpGEMMCluster
* The function starts with initialization of hash table followed by symbolic phase and numeric phase with hash table.
* Hash table also stores data in clustered format
*/
template<bool sortOutput, typename IT, typename NT, typename MultiplyOperation, typename AddOperation>
void HashSpGEMMCluster(const CSR_FlengthCluster<IT, NT> &a, const CSR<IT, NT> &b, CSR_FlengthCluster<IT, NT> &c,
MultiplyOperation multop, AddOperation addop) {
// initialize bin
BIN_FlengthCluster<IT, NT> bin(a.rows, MIN_HT_S, a.cluster_sz);
c.csr_rows = a.csr_rows;
c.rows = a.rows;
c.cols = b.cols;
c.cluster_sz = a.cluster_sz;
/* Set max bin */
bin.set_max_bin(a.rowptr, a.colids, b.rowptr, c.cols);
/* Create hash table (thread local) */
bin.create_local_hash_table(c.cols);
/* Symbolic Phase */
c.rowptr = my_malloc<IT>(c.rows + 1);
hash_symbolic_cluster(a.rowptr, a.colids, b.rowptr, b.colids,
c.rowptr, bin, c.rows, &(c.nnzc));
// resetting (reduce) hash table size
// hash table size is initialized by flops count
// after the symbolic phase, we can reset it by nnz count
bin.set_bin_id(c.cols, bin.min_ht_size);
c.colids = my_malloc<IT>(c.nnzc);
c.values = my_malloc<NT>(c.nnzc * c.cluster_sz);
/* Numeric Phase */
hash_numeric_cluster<sortOutput>(a.rowptr, a.colids, a.values,
b.rowptr, b.colids, b.values,
c.rowptr, c.colids, c.values,
bin, multop, addop, c.nnzc,
c.csr_rows, c.cluster_sz);
}
/*
* Executing HashSpGEMMCluster
* The function starts with initialization of hash table followed by symbolic phase and numeric phase with hash table.
* Hash table also stores data in clustered format
*/
template<bool sortOutput, typename IT, typename NT, typename MultiplyOperation, typename AddOperation>
void HashSpGEMMClustertoCheckMemoryRequirement(const CSR_FlengthCluster<IT, NT> &a, const CSR<IT, NT> &b, CSR_FlengthCluster<IT, NT> &c,
MultiplyOperation multop, AddOperation addop) {
// initialize bin
BIN_FlengthCluster<IT, NT> bin(a.rows, MIN_HT_S, a.cluster_sz);
c.csr_rows = a.csr_rows;
c.rows = a.rows;
c.cols = b.cols;
c.cluster_sz = a.cluster_sz;
/* Set max bin */
bin.set_max_bin(a.rowptr, a.colids, b.rowptr, c.cols);
/* Create hash table (thread local) */
bin.create_local_hash_table(c.cols);
/* Symbolic Phase */
c.rowptr = my_malloc<IT>(c.rows + 1);
hash_symbolic_cluster(a.rowptr, a.colids, b.rowptr, b.colids,
c.rowptr, bin, c.rows, &(c.nnzc));
// resetting (reduce) hash table size
// hash table size is initialized by flops count
// after the symbolic phase, we can reset it by nnz count
bin.set_bin_id(c.cols, bin.min_ht_size);
c.colids = my_malloc<IT>(c.nnzc);
c.values = my_malloc<NT>(c.nnzc * c.cluster_sz);
printf("BIN size: %.2f bytes\n", bin.calculate_size_in_gb());
printf("C size: %.2f bytes\n", c.calculate_size());
}