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/min_max_value_dense.hpp>
9#include <kernel/util/exception.hpp>
10#include <kernel/util/cuda_util.hpp>
15namespace FEAT::LAFEM::Arch
17 float cuda_min_max_value(const float * x, const Index size, const bool min_, const bool abs_)
19 XASSERTM(abs_, "only absolute min/max implemented for cuda!");
21 cublasStatus_t status;
23 status = cublasIsamin(Util::Intern::cublas_handle, int(size), x, 1, &result);
25 status = cublasIsamax(Util::Intern::cublas_handle, int(size), x, 1, &result);
26 if (status != CUBLAS_STATUS_SUCCESS)
27 throw InternalError(__func__, __FILE__, __LINE__, "cuda error: " + stringify(cublasGetStatusString(status)));
30 cudaMemcpy(&retval, &x[result - 1], sizeof(float), cudaMemcpyDeviceToHost);
31 return std::abs(retval);
34 double cuda_min_max_value(const double * x, const Index size, const bool min_, const bool abs_)
36 XASSERTM(abs_, "only absolute min/max implemented for cuda!");
38 cublasStatus_t status;
40 status = cublasIdamin(Util::Intern::cublas_handle, int(size), x, 1, &result);
42 status = cublasIdamax(Util::Intern::cublas_handle, int(size), x, 1, &result);
43 if (status != CUBLAS_STATUS_SUCCESS)
44 throw InternalError(__func__, __FILE__, __LINE__, "cuda error: " + stringify(cublasGetStatusString(status)));
47 cudaMemcpy(&retval, &x[result - 1], sizeof(double), cudaMemcpyDeviceToHost);
48 return std::abs(retval);
51 template <typename DT_>
52 DT_ MinMaxValueDense::exec_cuda_impl(const DT_ * const x, const Index size, const bool min_, const bool abs_)
54 DT_ result = cuda_min_max_value(x, size, min_, abs_);
57 cudaDeviceSynchronize();
58 cudaError_t last_error(cudaGetLastError());
59 if (cudaSuccess != last_error)
60 throw InternalError(__func__, __FILE__, __LINE__, "CUDA error occurred in execution!\n" + stringify(cudaGetErrorString(last_error)));
65 template float MinMaxValueDense::exec_cuda_impl(const float * const, const Index, const bool, const bool);
66 template double MinMaxValueDense::exec_cuda_impl(const double * const, const Index, const bool, const bool);
67} // namespace FEAT::LAFEM::Arch