-
Notifications
You must be signed in to change notification settings - Fork 1
/
local_kernel_benchmark.cpp
306 lines (250 loc) · 7 KB
/
local_kernel_benchmark.cpp
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
295
296
297
298
299
300
301
302
303
304
305
#include <iostream>
#include <algorithm>
#include <mkl_spblas.h>
#include <Eigen/Dense>
#include <mpi.h>
#include <omp.h>
#include "SpmatLocal.hpp"
#include "common.h"
using namespace std;
using namespace Eigen;
void generateRowStart(MKL_INT* rows, MKL_INT* rowStart, int numCoords, int numRows) {
#pragma omp parallel for
for(int i = 0; i < numRows; i++) {
rowStart[i] = 0;
}
rowStart[0] = 0;
rowStart[numRows] = numCoords;
/*#pragma omp parallel for
for(int i = 1; i < numCoords; i++) {
if(rows[i] != rows[i - 1]) {
rowStart[rows[i]] = i;
}
}
#pragma omp parallel for
for(int i = 0; i < numRows; i++) {
int idx = i + 1;
if(rowStart[i] != -1) {
while(rowStart[idx] == -1) {
rowStart[idx] = rowStart[i];
idx++;
}
}
}*/
}
void sddmm(double* ptrB, double* ptrC, double* values, MKL_INT* rows, MKL_INT* cols, int num_coords, int R) {
#pragma omp parallel for
for(int t = 0; t < num_coords; t++) {
double* Brow = ptrB + rows[t];
double* Crow = ptrC + cols[t];
double value = 0.0;
#pragma ivdep
for(int k = 0; k < R; k++) {
value += Brow[k] * Crow[k];
}
values[t] = value;
}
}
void sddmm(double* ptrB,
double* ptrC,
double* values,
MKL_INT* rowStart,
MKL_INT* col_idx,
MKL_INT num_coords,
int R,
int num_rows) {
#pragma omp parallel
{
int num_threads = omp_get_num_threads();
int current_thread = omp_get_thread_num();
MKL_INT share = divideAndRoundUp(num_coords, num_threads);
MKL_INT* lb = std::lower_bound(rowStart, rowStart + num_rows, share + num_rows);
MKL_INT row = *lb;
for(MKL_INT i = share * current_thread; i < std::min(share * (current_thread + 1), num_coords); i++) {
while(rowStart[row + 1] <= i) {
row++;
}
double* Brow = ptrB + row;
double* Crow = ptrC + col_idx[i];
double value = 0.0;
#pragma ivdep
for(int k = 0; k < R; k++) {
value += Brow[k] * Crow[k];
}
values[i] = value;
}
}
}
void spmm(double* ptrB, double* ptrC, sparse_matrix_t &A, int num_coords, int R) {
struct matrix_descr descr;
descr.type = SPARSE_MATRIX_TYPE_GENERAL;
mkl_sparse_d_mm (
SPARSE_OPERATION_NON_TRANSPOSE,
1.0,
A,
descr,
SPARSE_LAYOUT_ROW_MAJOR,
ptrB,
R,
R, // ldb
1.0,
ptrC,
R); // ldc
}
void benchmark(int logM, int nnz_per_row, vector<int> &rValues, double min_time, bool benchmark_sddmm) {
SpmatLocal erdos_renyi, er_prime;
erdos_renyi.loadTuples(false, logM, nnz_per_row, "");
std::sort(erdos_renyi.coords.begin(), erdos_renyi.coords.end(), column_major);
int num_coords = erdos_renyi.coords.size();
MKL_INT* rows = new MKL_INT[num_coords];
MKL_INT* cols = new MKL_INT[num_coords];
double* values = new double[num_coords];
for(int i = 0; i < num_coords; i++) {
rows[i] = erdos_renyi.coords[i].r;
cols[i] = erdos_renyi.coords[i].c;
values[i] = 1.0;
}
er_prime.loadTuples(false, logM, nnz_per_row / 2, "");
int num_coords_prime = er_prime.coords.size();
MKL_INT* rows_prime = new MKL_INT[num_coords_prime];
MKL_INT* cols_prime = new MKL_INT[num_coords_prime];
double* values_prime = new double[num_coords_prime];
for(int i = 0; i < num_coords_prime; i++) {
rows_prime[i] = er_prime.coords[i].r;
cols_prime[i] = er_prime.coords[i].c;
values[i] = 1.0;
}
sparse_matrix_t A_coo, A, A_coo_prime, A_prime;
mkl_sparse_d_create_coo (
&A_coo,
SPARSE_INDEX_BASE_ZERO,
erdos_renyi.M,
erdos_renyi.N,
erdos_renyi.coords.size(),
rows,
cols,
values);
mkl_sparse_d_create_coo (
&A_coo_prime,
SPARSE_INDEX_BASE_ZERO,
er_prime.M,
er_prime.N,
num_coords_prime,
rows_prime,
cols_prime,
values_prime);
mkl_sparse_convert_csr(A_coo, SPARSE_OPERATION_NON_TRANSPOSE, &A);
mkl_sparse_convert_csr(A_coo_prime, SPARSE_OPERATION_NON_TRANSPOSE, &A_prime);
// Just testing the handle creation
sparse_index_base_t indexing;
MKL_INT rows_export, cols_export, re_prime, ce_prime;
MKL_INT* rows_start, *rows_end, *col_indx, *r_start_prime, *r_end_prime, *c_indx_prime;
double* values_export, *values_export_prime;
mkl_sparse_d_export_csr(A, &indexing, &rows_export, &cols_export, &rows_start,
&rows_end, &col_indx, &values_export);
mkl_sparse_d_export_csr(A_prime, &indexing, &re_prime, &ce_prime, &r_start_prime,
&r_end_prime, &c_indx_prime, &values_export_prime);
memcpy(rows_start, r_start_prime, sizeof(MKL_INT) * rows_export);
memcpy(rows_end, r_end_prime, sizeof(MKL_INT) * rows_export);
memcpy(col_indx, c_indx_prime, sizeof(MKL_INT) * num_coords_prime);
memcpy(values_export, values_export_prime, sizeof(double) * num_coords_prime);
for(int i = 0; i < rValues.size(); i++) {
int R = rValues[i];
DenseMatrix B = DenseMatrix::Constant(erdos_renyi.N, R, 1.0);
DenseMatrix C = DenseMatrix::Constant(erdos_renyi.M, R, 1.0);
double* ptrB = B.data();
double* ptrC = C.data();
my_timer_t t = start_clock();
int num_trials = 0;
//#pragma omp parallel
//{
//#pragma omp single
//{
do {
num_trials++;
//if(benchmark_sddmm) {
// sddmm(ptrB, ptrC, values, rows, cols, num_coords, R);
//}
//else {
//}
//spmm(ptrB, ptrC, A, num_coords, R);
//sddmm(ptrB, ptrC, values, rows, cols, num_coords, R);
sddmm(ptrB,
ptrC,
values_export,
rows_start,
rows_end,
num_coords,
R,
rows_export);
char transa = 'N';
double alpha = 1.0;
double beta = 0.0;
MKL_INT R_mkl;
R_mkl = R;
char matdescra[4] = {'G', 'O', 'N', 'F'};
/*mkl_dcsrmm(&transa,
&rows_export,
&R_mkl,
&cols_export,
&alpha,
matdescra,
values_export,
col_indx,
rows_start,
rows_end,
ptrB,
&R_mkl,
&beta,
ptrC,
&R_mkl);*/
} while(stop_clock_get_elapsed(t) < min_time);
//}
//}
double elapsed = stop_clock_get_elapsed(t);
double throughput = er_prime.coords.size() * 2 * R * num_trials / elapsed;
throughput /= 1.0e9;
cout << erdos_renyi.M << "\t"
<< erdos_renyi.N << "\t"
<< erdos_renyi.coords.size() << "\t"
<< R << "\t"
<< throughput << "\t"
<< num_trials
<< endl;
}
}
void print_header() {
cout << "M\tN\tNNZ\tR\tGFLOPs\tTrials" << endl;
cout << "==========================================" << endl;
}
void print_footer() {
cout << "==========================================" << endl;
}
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
vector<int> logMValues {13, 14, 15, 16};
vector<int> nnz_per_row_values {8, 16, 32, 64, 128};
vector<int> rValues {8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096};
double min_time = 5.0;
print_header();
for(int i = 0; i < logMValues.size(); i++) {
for(int j = 0; j < nnz_per_row_values.size(); j++) {
cout << "SDDMM Benchmark" << endl;
benchmark(logMValues[i],
nnz_per_row_values[j],
rValues,
min_time,
true
);
cout << "SpMM Benchmark" << endl;
benchmark(logMValues[i],
nnz_per_row_values[j],
rValues,
min_time,
false
);
}
}
print_footer();
MPI_Finalize();
}