-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkernels.hpp
More file actions
415 lines (355 loc) · 13.9 KB
/
kernels.hpp
File metadata and controls
415 lines (355 loc) · 13.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#pragma once
#include <utility>
#ifndef PRECOND_ITERS
#define PRECOND_ITERS 1
#endif
#ifndef PRECOND_INNER_ITERS
#define PRECOND_INNER_ITERS 1
#endif
#include "common.hpp"
#include "sparse_matrix.hpp"
#ifdef USE_LIKWID
#include <likwid-marker.h>
#endif
#ifdef USE_SMAX
#include "utilities/smax_helpers.hpp"
#endif
inline void native_spmv(const MatrixCRS *A, const double *x, double *y) {
#pragma omp parallel
{
#ifdef USE_LIKWID
LIKWID_MARKER_START("spmv");
#endif
#pragma omp for schedule(static)
for (int row = 0; row < A->n_rows; ++row) {
double tmp = 0.0;
#pragma omp simd reduction(+ : tmp)
for (int nz_idx = A->row_ptr[row]; nz_idx < A->row_ptr[row + 1];
++nz_idx) {
tmp += A->val[nz_idx] * x[A->col[nz_idx]];
}
y[row] = tmp;
}
#ifdef USE_LIKWID
LIKWID_MARKER_STOP("spmv");
#endif
}
}
inline void spmv(const MatrixCRS *A, const double *x, double *y, int offset = 0,
Interface *smax = nullptr,
const std::string kernel_name = "") {
#if USE_SMAX
smax->kernel(kernel_name.c_str())->run(0, offset, 0);
#else
native_spmv(A, x, y);
#endif
}
inline void native_sptrsv(const MatrixCRS *L, double *x, const double *D,
const double *b) {
#ifdef USE_LIKWID
LIKWID_MARKER_START("sptrsv");
#endif
double sum;
for (int row = 0; row < L->n_rows; ++row) {
sum = 0.0;
int row_start = L->row_ptr[row];
int row_stop = L->row_ptr[row + 1];
for (int nz_idx = row_start; nz_idx < row_stop; ++nz_idx) {
sum += L->val[nz_idx] * x[L->col[nz_idx]];
}
x[row] = (b[row] - sum) / D[row];
}
#ifdef USE_LIKWID
LIKWID_MARKER_STOP("sptrsv");
#endif
}
inline void sptrsv(const MatrixCRS *L, double *x, const double *D,
const double *b, int offset = 0, Interface *smax = nullptr,
const std::string kernel_name = "") {
#ifdef USE_SMAX
smax->kernel(kernel_name.c_str())->run(0, offset, 0);
#else
native_sptrsv(L, x, D, b);
#endif
}
inline void native_bsptrsv(const MatrixCRS *U, double *x, const double *D,
const double *b) {
#ifdef USE_LIKWID
LIKWID_MARKER_START("backwards-sptrsv");
#endif
for (int row = U->n_rows - 1; row >= 0; --row) {
double sum = 0.0;
int row_start = U->row_ptr[row];
int row_stop = U->row_ptr[row + 1];
for (int nz_idx = row_start; nz_idx < row_stop; ++nz_idx) {
sum += U->val[nz_idx] * x[U->col[nz_idx]];
}
x[row] = (b[row] - sum) / D[row];
}
#ifdef USE_LIKWID
LIKWID_MARKER_STOP("backwards-sptrsv");
#endif
}
inline void bsptrsv(const MatrixCRS *U, double *x, const double *D,
const double *b, int offset = 0, Interface *smax = nullptr,
const std::string kernel_name = "") {
#if USE_SMAX
smax->kernel(kernel_name.c_str())->run(0, offset, 0);
#else
native_bsptrsv(U, x, D, b);
#endif
}
inline void subtract_vectors(double *result_vec, const double *vec1,
const double *vec2, const int N,
const double scale = 1.0) {
#pragma omp parallel for schedule(static)
for (int i = 0; i < N; ++i) {
result_vec[i] = vec1[i] - scale * vec2[i];
}
}
inline void sum_vectors(double *result_vec, const double *vec1,
const double *vec2, const int N,
const double scale = 1.0) {
#pragma omp parallel for schedule(static)
for (int i = 0; i < N; ++i) {
result_vec[i] = vec1[i] + scale * vec2[i];
}
}
inline void elemwise_mult_vectors(double *result_vec, const double *vec1,
const double *vec2, const int N,
const double scale = 1.0) {
#pragma omp parallel for schedule(static)
for (int i = 0; i < N; ++i) {
result_vec[i] = vec1[i] * scale * vec2[i];
}
}
inline void elemwise_div_vectors(double *result_vec, const double *vec1,
const double *vec2, const int N,
const double scale = 1.0) {
#pragma omp parallel for schedule(static)
for (int i = 0; i < N; ++i) {
result_vec[i] = vec1[i] / (scale * vec2[i]);
}
}
inline void compute_residual(const MatrixCRS *A, const double *x,
const double *b, double *residual, double *tmp,
Interface *smax = nullptr,
const std::string kernel_name = "") {
spmv(A, x, tmp SMAX_ARGS(0, smax, kernel_name));
subtract_vectors(residual, b, tmp, A->n_cols);
}
inline double infty_vec_norm(const double *vec, const int N) {
double max_abs = 0.0;
double curr_abs;
for (int i = 0; i < N; ++i) {
// TODO:: Hmmm...
// curr_abs = std::abs(static_cast<double>(vec[i]));
curr_abs = (vec[i] >= 0) ? vec[i] : -1 * vec[i];
if (curr_abs > max_abs) {
max_abs = curr_abs;
}
}
return max_abs;
}
inline double infty_mat_norm(const MatrixCRS *A) {
double max_row_sum = 0.0;
#pragma omp parallel for reduction(max : max_row_sum) schedule(static)
for (int row = 0; row < A->n_rows; ++row) {
double row_sum = 0.0;
for (int idx = A->row_ptr[row]; idx < A->row_ptr[row + 1]; ++idx) {
row_sum += std::abs(A->val[idx]);
}
max_row_sum = std::max(max_row_sum, row_sum);
}
return max_row_sum;
}
inline double euclidean_vec_norm(const double *vec, int N) {
double tmp = 0.0;
#pragma omp parallel for reduction(+ : tmp) schedule(static)
for (int i = 0; i < N; ++i) {
tmp += vec[i] * vec[i];
}
return std::sqrt(tmp);
}
inline double dot(const double *vec1, const double *vec2, const int N) {
double sum = 0.0;
#pragma omp parallel for reduction(+ : sum) schedule(static)
for (int i = 0; i < N; ++i) {
sum += vec1[i] * vec2[i];
}
return sum;
}
inline void scale(double *result_vec, const double *vec, const double scalar,
const int N) {
#pragma omp parallel for schedule(static)
for (int i = 0; i < N; ++i) {
result_vec[i] = vec[i] * scalar;
}
}
inline void init_dense_identity_matrix(double *mat, const int n_rows,
const int n_cols) {
#pragma omp parallel for schedule(static)
for (int i = 0; i < n_rows; ++i) {
for (int j = 0; j < n_cols; ++j) {
if (i == j) {
mat[n_cols * i + j] = 1.0;
} else {
mat[n_cols * i + j] = 0.0;
}
}
}
}
inline void init_vector(double *vec, double val, long size) {
#pragma omp parallel for schedule(static)
for (int i = 0; i < size; ++i) {
vec[i] = val;
}
}
inline void copy_dense_matrix(double *new_mat, const double *old_mat,
const int n_rows, const int n_cols) {
for (int row = 0; row < n_rows; ++row) {
for (int col_idx = 0; col_idx < n_cols; ++col_idx) {
new_mat[n_cols * row + col_idx] = old_mat[n_cols * row + col_idx];
}
}
}
inline void copy_vector(double *output, const double *input, const int n_rows) {
#pragma omp parallel for schedule(static)
for (int row = 0; row < n_rows; ++row) {
output[row] = input[row];
}
}
inline void dgemm_transpose1(double *A, double *B, double *C, int n_rows_A,
int n_cols_A, int n_cols_B) {
#pragma omp parallel for
for (int i = 0; i < n_rows_A; ++i) {
for (int j = 0; j < n_cols_B; ++j) {
double tmp = 0.0;
for (int k = 0; k < n_cols_A; ++k) {
tmp += A[k * n_rows_A + i] * B[k * n_cols_B + j];
}
C[i * n_cols_B + j] = tmp;
}
}
}
inline void dgemm_transpose2(double *A, double *B, double *C, int n_rows_A,
int n_cols_A, int n_cols_B) {
for (int i = 0; i < n_rows_A; ++i) {
for (int j = 0; j < n_cols_B; ++j) {
double tmp = 0.0;
for (int k = 0; k < n_cols_A; ++k) {
tmp += A[i * n_cols_A + k] * B[k * n_cols_B + j];
}
C[i * n_cols_B + j] = tmp;
}
}
}
inline void dgemm(double *A, double *B, double *C, int n_rows_A, int n_cols_A,
int n_cols_B) {
for (int i = 0; i < n_rows_A; ++i) {
for (int j = 0; j < n_cols_B; ++j) {
double tmp = 0.0;
for (int k = 0; k < n_cols_A; ++k) {
tmp += A[i * n_cols_A + k] * B[k * n_cols_B + j];
}
C[i * n_cols_B + j] = tmp;
}
}
}
inline void dgemv(const double *A, const double *x, double *y, int n_rows_A,
int n_cols_A, double alpha = 1.0
// double beta = 1.0
) {
for (int i = 0; i < n_rows_A; ++i) {
// y[i] *= beta;
y[i] = 0.0;
for (int j = 0; j < n_cols_A; ++j) {
y[i] += alpha * A[i * n_cols_A + j] * x[j];
}
}
}
inline void two_stage_gauss_seidel(const MatrixCRS *strict, double *tmp,
double *work, double *D_inv, double *input,
double *output, const int N, int offset = 0,
Interface *smax = nullptr,
const std::string &kernel_name = "") {
elemwise_mult_vectors(work, D_inv, input, N);
copy_vector(output, work, N);
for (int inner = 1; inner <= PRECOND_INNER_ITERS; ++inner) {
spmv(strict, work, tmp SMAX_ARGS(0, smax, kernel_name));
elemwise_mult_vectors(tmp, D_inv, tmp, N, -1.0);
std::swap(work, tmp);
#ifdef USE_SMAX
smax->kernel(kernel_name)->swap_operands();
#endif
sum_vectors(output, output, work, N);
}
}
// Computes z <- M^{-1}y
inline void apply_preconditioner(const PrecondType preconditioner, const int N,
const MatrixCRS *L_strict,
const MatrixCRS *U_strict, double *A_D,
double *A_D_inv, double *L_D, double *U_D,
double *output, double *input, double *tmp,
double *work, int offset = 0,
Interface *smax = nullptr,
const std::string kernel_name = "") {
IF_DEBUG_MODE_FINE(
SanityChecker::print_vector(input, N, "before precond:"));
double *input_storage = nullptr;
if (PRECOND_OUTER_ITERS > 1) {
input_storage = new double[N];
copy_vector(input_storage, input, N);
}
// clang-format off
for (int i = 0; i < PRECOND_OUTER_ITERS; ++i) {
if (preconditioner == PrecondType::Jacobi) {
elemwise_div_vectors(output, input, A_D, N);
} else if (preconditioner == PrecondType::GaussSeidel) {
sptrsv(L_strict, output, A_D, input SMAX_ARGS(0, smax, kernel_name));
} else if (preconditioner == PrecondType::BackwardsGaussSeidel) {
bsptrsv(U_strict, output, A_D, input SMAX_ARGS(0, smax, kernel_name));
} else if (preconditioner == PrecondType::SymmetricGaussSeidel) {
// tmp <- (L+D)^{-1}*r
IF_DEBUG_MODE_FINE(SanityChecker::print_vector(tmp, N, "tmp before lower solve"));
sptrsv(L_strict, tmp, A_D, input SMAX_ARGS(0, smax, std::string(kernel_name + "_lower")));
// tmp <- D(L+D)^{-1}*r
IF_DEBUG_MODE_FINE(SanityChecker::print_vector(tmp, N, "tmp before divide"));
elemwise_mult_vectors(tmp, tmp, A_D, N);
// z <- (L+U)^{-1}*tmp
IF_DEBUG_MODE_FINE(SanityChecker::print_vector(tmp, N, "tmp before upper solve"));
bsptrsv(U_strict, output, A_D, tmp SMAX_ARGS(0, smax, std::string(kernel_name + "_upper")));
} else if (preconditioner == PrecondType::TwoStageGS) {
two_stage_gauss_seidel(L_strict, tmp, work, A_D_inv, input,
output, N SMAX_ARGS(0, smax, std::string(kernel_name)));
} else if (preconditioner == PrecondType::SymmetricTwoStageGS) {
two_stage_gauss_seidel(L_strict, tmp, work, A_D_inv, input,
output, N SMAX_ARGS(0, smax, std::string(kernel_name + "_lower")));
elemwise_mult_vectors(output, output, A_D, N);
two_stage_gauss_seidel(U_strict, tmp, work, A_D_inv, output,
output, N SMAX_ARGS(0, smax, std::string(kernel_name + "_upper")));
} else if (preconditioner == PrecondType::ILU0) {
// tmp <- L^{-1}*r
// NOTE: L_D := ones(N), since we don't divide by anything
IF_DEBUG_MODE_FINE(SanityChecker::print_vector(tmp, N, "tmp before lower solve"));
sptrsv(L_strict, tmp, L_D, input SMAX_ARGS(0, smax, std::string(kernel_name + "_lower")));
// z <- U^{-1}*tmp
IF_DEBUG_MODE_FINE(SanityChecker::print_vector(tmp, N, "tmp before upper solve"));
bsptrsv(U_strict, output, U_D, tmp SMAX_ARGS(0, smax, std::string(kernel_name + "_upper")));
}
else {
// TODO: Would be great to think of a way around this
copy_vector(output, input, N);
}
if (PRECOND_OUTER_ITERS > 1 && i != PRECOND_OUTER_ITERS - 1) {
copy_vector(input, output, N);
}
}
if ( PRECOND_OUTER_ITERS > 1 ) {
copy_vector(input, input_storage, N);
}
delete[] input_storage;
// clang-format on
IF_DEBUG_MODE_FINE(
SanityChecker::print_vector(output, N, "after precond:"));
}