FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
unit_filter_block_bcsr.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/unit_filter_block_bcsr.hpp>
9#include <kernel/util/exception.hpp>
10#include <kernel/util/cuda_util.hpp>
11#include <kernel/util/cuda_math.cuh>
12
13namespace FEAT::LAFEM::Arch
14{
15 template<typename DT_, typename IT_>
16 __global__ void cuda_unit_filter_blocked_mat(
17 DT_* __restrict__ mat, const IT_* __restrict__ const row_ptr, const IT_* __restrict__ const col_idx,
18 const DT_ * __restrict__ const f_val, const IT_ * __restrict__ const f_idx, const Index f_nzes,
19 const bool unit, const bool ign_nans, const int block_height, const int block_width)
20 {
21 // grid strided for loop
22 for(Index i = threadIdx.x + blockDim.x * blockIdx.x; i < f_nzes; i += blockDim.x * gridDim.x)
23 {
24 const IT_ ix(f_idx[i]);
25 const DT_* vx(&f_val[i*block_height]);
26
27 // replace by unit or zero row
28 for(IT_ j(row_ptr[ix]); j < row_ptr[ix + 1]; ++j)
29 {
30 // loop over rows in the block
31 for(int k(0); k < block_height; ++k)
32 {
33 // possibly skip row if filter value is NaN
34 if(ign_nans && CudaMath::cuda_isnan(vx[k]))
35 continue;
36
37 // format block row to zero
38 for(int l(0); l < block_width; ++l)
39 mat[j*block_height*block_width + k*block_width +l] = DT_(0);
40
41 // replace diagonal entry by 1 if requested
42 if(unit && (col_idx[j] == ix) && (k < block_width))
43 mat[j*block_height*block_width + k*block_width +k] = DT_(1);
44 }
45 }
46 }
47 }
48
49 template<typename DT_, typename IT_>
50 void UnitFilterBlockBCSR::exec_cuda_impl(DT_* mat, const IT_* const row_ptr, const IT_* const col_idx,
51 const DT_ * const f_val, const IT_ * const f_idx, const Index f_nzes, const bool unit,
52 const bool ign_nans, const int block_height, const int block_width)
53 {
54 dim3 block(static_cast<unsigned int>(Util::cuda_blocksize_misc));
55 dim3 grid((static_cast<unsigned int>(f_nzes) + block.x - 1u) / block.x); // round up to next integer
56
57 cuda_unit_filter_blocked_mat<<<grid, block >>>(mat, row_ptr, col_idx, f_val, f_idx, f_nzes, unit, ign_nans, block_height, block_width);
58
59 #ifdef DEBUG
60 cudaDeviceSynchronize();
61 cudaError_t last_error(cudaGetLastError());
62 if (cudaSuccess != last_error)
63 throw InternalError(__func__, __FILE__, __LINE__, "CUDA error occurred in execution!\n" + stringify(cudaGetErrorString(last_error)));
64 #endif
65 }
66
67 template void UnitFilterBlockBCSR::exec_cuda_impl(float* mat, const std::uint32_t* const row_ptr, const std::uint32_t* const col_idx, const float * const f_val, const std::uint32_t * const f_idx, const Index f_nzes, const bool unit, const bool ign_nans, const int block_height, const int block_width);
68 template void UnitFilterBlockBCSR::exec_cuda_impl(float* mat, const std::uint64_t* const row_ptr, const std::uint64_t* const col_idx, const float * const f_val, const std::uint64_t * const f_idx, const Index f_nzes, const bool unit, const bool ign_nans, const int block_height, const int block_width);
69 template void UnitFilterBlockBCSR::exec_cuda_impl(double* mat, const std::uint32_t* const row_ptr, const std::uint32_t* const col_idx, const double * const f_val, const std::uint32_t * const f_idx, const Index f_nzes, const bool unit, const bool ign_nans, const int block_height, const int block_width);
70 template void UnitFilterBlockBCSR::exec_cuda_impl(double* mat, const std::uint64_t* const row_ptr, const std::uint64_t* const col_idx, const double * const f_val, const std::uint64_t * const f_idx, const Index f_nzes, const bool unit, const bool ign_nans, const int block_height, const int block_width);
71} // namespace FEAT::LAFEM::Arch