Repository navigation
Expand file tree
/
Copy pathkernel_mkl.cpp
More file actions
151 lines (128 loc) · 3.87 KB
/
Copy pathkernel_mkl.cpp
File metadata and controls
151 lines (128 loc) · 3.87 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
#include <stdio.h>
#include <stdlib.h>
#include "macros/cpp_defines.h"
#include "bench_common.h"
#include "kernel.h"
#ifdef __cplusplus
extern "C"{
#endif
#include "macros/macrolib.h"
#include "time_it.h"
#include "parallel_util.h"
#ifdef __cplusplus
}
#endif
#include <mkl.h>
struct CSRArrays : Matrix_Format
{
INT_T * ia; // the usual rowptr (of size m+1)
INT_T * ja; // the colidx of each NNZ (of size nnz)
ValueType * a; // the values (of size NNZ)
ValueType * x = NULL;
ValueType * y = NULL;
ValueType * out = NULL;
sparse_matrix_t A;
matrix_descr descr;
const sparse_operation_t operation = SPARSE_OPERATION_NON_TRANSPOSE;
const sparse_layout_t layout = SPARSE_LAYOUT_ROW_MAJOR;
CSRArrays(INT_T * ia, INT_T * ja, ValueType * a, long m, long n, long nnz, int k) : Matrix_Format(m, n, nnz, k), ia(ia), ja(ja), a(a)
{
const sparse_index_base_t indexing = SPARSE_INDEX_BASE_ZERO;
const sparse_memory_usage_t policy = SPARSE_MEMORY_NONE;
const int expected_calls = 128;
#ifdef MKL_IE_COLIND0
#pragma omp parallel for schedule(dynamic, 1024)
for (long i = 0; i < m; i++) {
for (long j = ia[i]; j < ia[i+1]; j++) {
ja[j] = ja[0];
}
}
#endif
descr.type = SPARSE_MATRIX_TYPE_GENERAL;
mkl_verbose(1);
#if DOUBLE == 0
mkl_sparse_s_create_csr(&A, indexing, m, n, ia, ia+1, ja, a);
#elif DOUBLE == 1
mkl_sparse_d_create_csr(&A, indexing, m, n, ia, ia+1, ja, a);
#endif
// Sort the columns
mkl_sparse_order(A);
// execute preprocess
mkl_sparse_set_mv_hint(A, operation, descr, expected_calls);
mkl_sparse_set_memory_hint(A, policy);
mkl_sparse_optimize(A);
}
~CSRArrays()
{
free(a);
free(ia);
free(ja);
mkl_sparse_destroy(A);
}
void spmm(ValueType * x, ValueType * y, int k);
void sddmm(ValueType * x, ValueType * y, ValueType * out, int k);
};
void compute_spmm(CSRArrays * restrict csr, ValueType * restrict x, ValueType * restrict y, int k);
void compute_sddmm(CSRArrays * restrict csr, ValueType * restrict x, ValueType * restrict y, ValueType * restrict out, int k);
void
CSRArrays::spmm(ValueType * x, ValueType * y, int k)
{
compute_spmm(this, x, y, k);
}
void
CSRArrays::sddmm(ValueType * x, ValueType * y, ValueType * out, int k)
{
compute_sddmm(this, x, y, out, k);
}
struct Matrix_Format *
csr_to_format(INT_T * row_ptr, INT_T * col_ind, ValueType * values, long m, long n, long nnz, int k)
{
struct CSRArrays * csr = new CSRArrays(row_ptr, col_ind, values, m, n, nnz, k);
csr->mem_footprint = nnz * (sizeof(ValueType) + sizeof(INT_T)) + (m+1) * sizeof(INT_T);
#ifdef MKL_IE_COLIND0
csr->format_name = (char *) "MKL_IE_COLIND0";
#else
csr->format_name = (char *) "MKL_IE";
#endif
return csr;
}
//==========================================================================================================================================
//= Computation
//==========================================================================================================================================
void
compute_spmm(CSRArrays * restrict csr, ValueType * restrict x, ValueType * restrict y, int k)
{
const ValueType alpha = 1.0;
const ValueType beta = 0.0;
if (csr->x == NULL)
{
csr->x = x;
// for (long i = 0; i < 100; i++) {
// printf("x[%ld]: %lf\n", i, x[i]);
// }
}
#if DOUBLE == 0
mkl_sparse_s_mm(csr->operation, alpha, csr->A, csr->descr, csr->layout, x, k, k, beta, y, k);
#elif DOUBLE == 1
mkl_sparse_d_mm(csr->operation, alpha, csr->A, csr->descr, csr->layout, x, k, k, beta, y, k);
#endif
if (csr->y == NULL)
{
csr->y = y;
}
}
void
compute_sddmm(CSRArrays * restrict csr, ValueType * restrict x, ValueType * restrict y, ValueType * restrict out, __attribute__((unused)) int k)
{
__attribute__((unused)) const ValueType alpha = 1.0;
__attribute__((unused)) const ValueType beta = 0.0;
if (csr->x == NULL)
{
csr->x = x;
csr->y = y;
}
if (csr->out == NULL)
{
csr->out = out;
}
}