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/scale.hpp>
9#include <kernel/util/exception.hpp>
10#include <kernel/util/cuda_util.hpp>
11#include <kernel/util/half.hpp>
14using namespace FEAT::LAFEM;
15using namespace FEAT::LAFEM::Arch;
17template <typename DT_>
18void Scale::value_cuda(DT_ * r, const DT_ * const x, const DT_ s, const Index size)
22 if (typeid(DT_) == typeid(double))
27 else if (typeid(DT_) == typeid(float))
32#ifdef FEAT_HAVE_HALFMATH
33 else if (typeid(DT_) == typeid(Half))
40 throw InternalError(__func__, __FILE__, __LINE__, "unsupported data type!");
43 ///\todo cuse cublasCopyEx when available
44 cudaMemcpy(r, x, size * sizeof(DT_), cudaMemcpyDefault);
46 cublasStatus_t status;
50 // NOTE(mmuegge): cublasAxpyEx expects a to be a floating point value, but DT_ might be Half.
51 // Cast to float to ensure the type matches
52 const float s_tmp = float(s);
53 status = cublasScalEx(Util::Intern::cublas_handle, int(size), &s_tmp, et, r, dt, 1, et);
57 status = cublasScalEx(Util::Intern::cublas_handle, int(size), &s, et, r, dt, 1, et);
59 if (status != CUBLAS_STATUS_SUCCESS)
60 throw InternalError(__func__, __FILE__, __LINE__, "cuda error: " + stringify(cublasGetStatusString(status)));
62 cudaDeviceSynchronize();
64 cudaError_t last_error(cudaGetLastError());
65 if (cudaSuccess != last_error)
66 throw InternalError(__func__, __FILE__, __LINE__, "CUDA error occurred in execution!\n" + stringify(cudaGetErrorString(last_error)));
69#ifdef FEAT_HAVE_HALFMATH
70template void Scale::value_cuda(Half *, const Half * const, const Half, const Index);
72template void Scale::value_cuda(float *, const float * const, const float, const Index);
73template void Scale::value_cuda(double *, const double * const, const double, const Index);