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.
7#include <kernel/base_header.hpp>
8#include <kernel/lafem/arch/matvecmult_dense_dense.hpp>
9#include <kernel/util/memory_aux.hpp>
10#include <kernel/util/exception.hpp>
11#include <kernel/util/cuda_util.hpp>
12#include <kernel/util/math.hpp>
13#include <kernel/util/half.hpp>
15#include "cusparse_v2.h"
17namespace FEAT::LAFEM::Arch
19 cublasStatus_t cublas_apply_dense(cublasOperation_t trans,
21 const float * const alpha,
22 const float * const val,
23 const float * const x, const float * const beta, float * y)
25 return cublasSgemv(Util::Intern::cublas_handle, trans, n, m, alpha, val, n, x, 1, beta, y, 1);
28 cublasStatus_t cublas_apply_dense(cublasOperation_t trans,
30 const double * const alpha,
31 const double * const val,
32 const double * const x, const double * const beta, double * y)
34 return cublasDgemv(Util::Intern::cublas_handle, trans, n, m, alpha, val, n, x, 1, beta, y, 1);
37 template <typename DT_>
38 void MatVecMultDenseDense::exec_cuda_impl(DT_ * r, const DT_ alpha, const DT_ * const y, const DT_ * const val, const DT_ * const x, const Index rows, const Index cols, bool transposed)
40 // cublas expects column major, so swap transpose and normal
41 cublasOperation_t trans = (transposed ? CUBLAS_OP_N : CUBLAS_OP_T);
44 Memory::memset_cuda(r, 0, sizeof(DT_) * (transposed ? cols : rows));
46 Memory::memcopy_cuda(r, y, sizeof(DT_) * (transposed ? cols : rows));
48 DT_ beta = (y == nullptr) ? DT_(0) : DT_(1);
49 cublasStatus_t status = cublas_apply_dense(trans, (int)rows, (int)cols, &alpha, val, x, &beta, r);
50 if (status != CUBLAS_STATUS_SUCCESS)
51 throw InternalError(__func__, __FILE__, __LINE__, "cuda error: " + stringify(cublasGetStatusString(status)));
54 cudaDeviceSynchronize();
55 cudaError_t last_error(cudaGetLastError());
56 if (cudaSuccess != last_error)
57 throw InternalError(__func__, __FILE__, __LINE__, "CUDA error occurred in execution!\n" + stringify(cudaGetErrorString(last_error)));
61 template void MatVecMultDenseDense::exec_cuda_impl(float *, const float, const float * const, const float * const, const float * const, const Index, const Index, bool);
62 template void MatVecMultDenseDense::exec_cuda_impl(double *, const double, const double * const, const double * const, const double * const, const Index, const Index, bool);
63 #ifdef FEAT_HAVE_HALFMATH
64 template void MatVecMultDenseDense::exec_cuda_impl(Half *, const Half, const Half * const, const Half * const, const Half * const, const Index, const Index, bool);
66} // namespace FEAT::LAFEM::Arch