FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
min_max_value_dense.cu
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
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>
11
12// includes, CUDA
13#include <cublas_v2.h>
14
15namespace FEAT::LAFEM::Arch
16{
17 float cuda_min_max_value(const float * x, const Index size, const bool min_, const bool abs_)
18 {
19 XASSERTM(abs_, "only absolute min/max implemented for cuda!");
20 int result;
21 cublasStatus_t status;
22 if(min_)
23 status = cublasIsamin(Util::Intern::cublas_handle, int(size), x, 1, &result);
24 else
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)));
28
29 float retval(0.0f);
30 cudaMemcpy(&retval, &x[result - 1], sizeof(float), cudaMemcpyDeviceToHost);
31 return std::abs(retval);
32 }
33
34 double cuda_min_max_value(const double * x, const Index size, const bool min_, const bool abs_)
35 {
36 XASSERTM(abs_, "only absolute min/max implemented for cuda!");
37 int result;
38 cublasStatus_t status;
39 if(min_)
40 status = cublasIdamin(Util::Intern::cublas_handle, int(size), x, 1, &result);
41 else
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)));
45
46 double retval(0.0);
47 cudaMemcpy(&retval, &x[result - 1], sizeof(double), cudaMemcpyDeviceToHost);
48 return std::abs(retval);
49 }
50
51 template <typename DT_>
52 DT_ MinMaxValueDense::exec_cuda_impl(const DT_ * const x, const Index size, const bool min_, const bool abs_)
53 {
54 DT_ result = cuda_min_max_value(x, size, min_, abs_);
55
56 #ifdef DEBUG
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)));
61 #endif
62 return result;
63 }
64
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