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_weak_bcsr.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_weak_bcsr(
17 DT_ * __restrict__ mat_a, const DT_ * __restrict__ const mat_m, const IT_* __restrict__ const row_ptr,
18 const DT_ * __restrict__ const f_val, const IT_ * __restrict__ const f_idx, const Index f_nzes,
19 const bool ign_nans, const int block_height, const int block_width)
21 // grid strided for loop
22 for(Index i = threadIdx.x + blockDim.x * blockIdx.x; i < f_nzes; i += blockDim.x * gridDim.x)
24 const IT_ ix(f_idx[i]);
25 const DT_* vx(&f_val[i*block_height]);
27 for(IT_ j(row_ptr[ix]); j < row_ptr[ix + 1]; ++j)
29 // loop over rows in the block
30 for(int k(0); k < block_height; ++k)
32 // possibly skip row if filter value is NaN
33 if(ign_nans && CudaMath::cuda_isnan(vx[k]))
36 // copy mat_m row into mat_a
37 for(int l(0); l < block_width; ++l)
39 mat_a[j*block_height*block_width + k*block_width + l] = vx[k] * mat_m[j*block_height*block_width + k*block_width + l];
46 template<typename DT_, typename IT_>
47 void UnitFilterBlockWeakBCSR::exec_cuda_impl(DT_* mat_a, const DT_ * const mat_m, const IT_* const row_ptr,
48 const DT_ * const f_val, const IT_ * const f_idx, const Index f_nzes,
49 const bool ign_nans, const int block_height, const int block_width)
51 dim3 block(static_cast<unsigned int>(Util::cuda_blocksize_misc));
52 dim3 grid((static_cast<unsigned int>(f_nzes) + block.x - 1u) / block.x); // round up to next integer
54 cuda_unit_filter_block_weak_bcsr<<<grid, block>>>(mat_a, mat_m, row_ptr, f_val, f_idx, f_nzes, ign_nans, block_height, block_width);
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)));
64 template void UnitFilterBlockWeakBCSR::exec_cuda_impl<float, std::uint64_t>(float *, const float* const, const std::uint64_t * const, const float * const, const std::uint64_t * const, const Index, const bool, const int, const int);
65 template void UnitFilterBlockWeakBCSR::exec_cuda_impl<float, std::uint32_t>(float *, const float* const, const std::uint32_t * const, const float * const, const std::uint32_t * const, const Index, const bool, const int, const int);
66 template void UnitFilterBlockWeakBCSR::exec_cuda_impl<double, std::uint64_t>(double *, const double* const, const std::uint64_t * const, const double * const, const std::uint64_t * const, const Index, const bool, const int, const int);
67 template void UnitFilterBlockWeakBCSR::exec_cuda_impl<double, std::uint32_t>(double *, const double* const, const std::uint32_t * const, const double * const, const std::uint32_t * const, const Index, const bool, const int, const int);
68} // namespace FEAT::LAFEM::Arch