FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
matvecmult_csr_dense_mkl.cpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6// includes, FEAT
8#include <kernel/lafem/arch/matvecmult_csr_dense.hpp>
9#include <kernel/util/memory_aux.hpp>
11
12FEAT_DISABLE_WARNINGS
13#include <mkl.h>
14#include <mkl_spblas.h>
15FEAT_RESTORE_WARNINGS
16
17namespace FEAT::LAFEM::Arch
18{
19 // the code below casts Index* to MKL_INT*
20 static_assert(sizeof(Index) == sizeof(MKL_INT), "size mismatch between MKL_INT and FEAT::Index");
21
22 void MatVecMultCSRDense::exec_mkl_impl(float * r, const float alpha, const float * const x, const float * const y, const float * const val, const Index * const col_idx, const Index * const row_ptr, const Index rows, const Index cols, const Index, const bool transposed)
23 {
24 MKL_INT mrows = (MKL_INT)rows;
25 MKL_INT mcolumns = (MKL_INT)cols;
26
27 if(y == nullptr)
28 Memory::memset_main(r, 0, sizeof(float) * (transposed ? cols : rows));
29 else if(r != y)
30 {
31 MKL_INT one = 1;
32 MKL_INT n = transposed ? mcolumns : mrows;
33 scopy(&n, y, &one, r, &one);
34 }
35
36 float beta = 1.0f;
37
38 #ifndef FEAT_USE_MKL_LEGACY_SPMV
39
40 sparse_operation_t opt = (transposed ? SPARSE_OPERATION_TRANSPOSE : SPARSE_OPERATION_NON_TRANSPOSE);
41
42 sparse_matrix_t A;
43 FEAT_DISABLE_WARNINGS
44 sparse_status_t status = mkl_sparse_s_create_csr(&A, SPARSE_INDEX_BASE_ZERO, mrows, mcolumns, (MKL_INT*)row_ptr, (MKL_INT*)(row_ptr + 1), (MKL_INT*)col_idx, (float*) val);
45 FEAT_RESTORE_WARNINGS
46 if (status != SPARSE_STATUS_SUCCESS)
47 XABORTM("MKL Sparse Error occurred in execution!\n");
48 matrix_descr md;
49 md.type = SPARSE_MATRIX_TYPE_GENERAL;
50 status = mkl_sparse_s_mv(opt, alpha, A, md, x, beta, r);
51 if (status != SPARSE_STATUS_SUCCESS)
52 XABORTM("MKL Sparse Error occurred in execution!\n");
53 mkl_sparse_destroy(A);
54
55 #else
56
57 char matdescra[6];
58 matdescra[0] = 'G';
59 matdescra[1] = 0; //ingored by mkl, but valgrind complains otherwise
60 matdescra[2] = 0; //ingored by mkl, but valgrind complains otherwise
61 matdescra[3] = 'C';
62
63 char trans = (transposed ? 'T' : 'N');
64
65 FEAT_DISABLE_WARNINGS
66 mkl_scsrmv(&trans, &mrows, &mcolumns, (const float*)&alpha, matdescra, (const float*) val, (const MKL_INT*)col_idx, (const MKL_INT*)row_ptr, (const MKL_INT*)(row_ptr) + 1, (const float*)x, (const float*)&beta, r);
67 FEAT_RESTORE_WARNINGS
68
69 #endif
70 }
71
72 void MatVecMultCSRDense::exec_mkl_impl(double * r, const double alpha, const double * const x, const double * const y, const double * const val, const Index * const col_idx, const Index * const row_ptr, const Index rows, const Index cols, const Index, const bool transposed)
73 {
74 MKL_INT mrows = (MKL_INT)rows;
75 MKL_INT mcolumns = (MKL_INT)cols;
76
77 if(y == nullptr)
78 Memory::memset_main(r, 0, sizeof(double) * (transposed ? cols : rows));
79 else if(r != y)
80 {
81 MKL_INT one = 1;
82 MKL_INT n = transposed ? mcolumns : mrows;
83 dcopy(&n, y, &one, r, &one);
84 }
85
86 double beta = 1.0;
87
88 #ifndef FEAT_USE_MKL_LEGACY_SPMV
89
90 sparse_operation_t opt = (transposed ? SPARSE_OPERATION_TRANSPOSE : SPARSE_OPERATION_NON_TRANSPOSE);
91
92 sparse_matrix_t A;
93 FEAT_DISABLE_WARNINGS
94 sparse_status_t status = mkl_sparse_d_create_csr(&A, SPARSE_INDEX_BASE_ZERO, mrows, mcolumns, (MKL_INT*)row_ptr, (MKL_INT*)(row_ptr + 1), (MKL_INT*)col_idx, (double*) val);
95 FEAT_RESTORE_WARNINGS
96 if (status != SPARSE_STATUS_SUCCESS)
97 XABORTM("MKL Sparse Error occurred in execution!\n");
98 matrix_descr md;
99 md.type = SPARSE_MATRIX_TYPE_GENERAL;
100 status = mkl_sparse_d_mv(opt, alpha, A, md, x, beta, r);
101 if (status != SPARSE_STATUS_SUCCESS)
102 XABORTM("MKL Sparse Error occurred in execution!\n");
103 mkl_sparse_destroy(A);
104
105 #else
106
107 char matdescra[6];
108 matdescra[0] = 'G';
109 matdescra[1] = 0; //ingored by mkl, but valgrind complains otherwise
110 matdescra[2] = 0; //ingored by mkl, but valgrind complains otherwise
111 matdescra[3] = 'C';
112
113 char trans = (transposed ? 'T' : 'N');
114
115 FEAT_DISABLE_WARNINGS
116 mkl_dcsrmv(&trans, &mrows, &mcolumns, (const double*)&alpha, matdescra, (const double*) val, (const MKL_INT*)col_idx, (const MKL_INT*)row_ptr, (const MKL_INT*)(row_ptr) + 1, (const double*)x, (const double*)&beta, r);
117 FEAT_RESTORE_WARNINGS
118
119 #endif
120 }
121} // namespace FEAT::LAFEM::Arch
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
FEAT Kernel base header.
void memset_main(void *ptr, int value, std::size_t bytes)
Formats all bytes of a memory block in main memory to a given value.
std::uint64_t Index
Index data type.