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/unit_filter_block_vec.hpp>
9#include <kernel/util/exception.hpp>
10#include <kernel/util/cuda_util.hpp>
11#include <kernel/util/cuda_math.cuh>
13namespace FEAT::LAFEM::Arch
15 template <typename DT_, typename IT_>
16 __global__ void cuda_unit_filter_block_vec(DT_ * v, const DT_ * f_val, const IT_ * f_idx, const Index f_nzes, const bool zero, const bool ign_nans, const Index block_size)
18 // grid strided for loop
19 for(Index i = threadIdx.x + blockDim.x * blockIdx.x; i < f_nzes; i += blockDim.x * gridDim.x)
21 for(Index j(0) ; j < block_size; ++j)
23 const Index k = block_size * i + j;
25 // skip if filter value is NaN if desired
26 if(ign_nans && CudaMath::cuda_isnan(f_val[k]))
29 // write filter value or zero
30 v[Index(block_size) * f_idx[i] + Index(j)] = zero ? DT_(0) : f_val[k];
35 template <typename DT_, typename IT_>
36 void UnitFilterBlockVec::exec_cuda_impl(DT_ * v, const DT_ * const f_val, const IT_ * const f_idx, const Index f_nzes, const bool zero, const bool ign_nans, const int block_size)
38 dim3 block(static_cast<unsigned int>(Util::cuda_blocksize_misc));
39 dim3 grid((static_cast<unsigned int>(f_nzes) + block.x - 1u) / block.x); // round up to next integer
41 cuda_unit_filter_block_vec<<<grid, block>>>(v, f_val, f_idx, f_nzes, zero, ign_nans, Index(block_size));
44 cudaDeviceSynchronize();
45 cudaError_t last_error(cudaGetLastError());
46 if (cudaSuccess != last_error)
47 throw InternalError(__func__, __FILE__, __LINE__, "CUDA error occurred in execution!\n" + stringify(cudaGetErrorString(last_error)));
51 template void UnitFilterBlockVec::exec_cuda_impl(float *, const float * const, const std::uint64_t * const, const Index, const bool, const bool, const int);
52 template void UnitFilterBlockVec::exec_cuda_impl(double *, const double * const, const std::uint64_t * const, const Index, const bool, const bool, const int);
53 template void UnitFilterBlockVec::exec_cuda_impl(float *, const float * const, const std::uint32_t * const, const Index, const bool, const bool, const int);
54 template void UnitFilterBlockVec::exec_cuda_impl(double *, const double * const, const std::uint32_t * const, const Index, const bool, const bool, const int);
55} // namespace FEAT::LAFEM::Arch