FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
row_norm2_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/row_norm2_bcsr.hpp>
9#include <kernel/util/exception.hpp>
10#include <kernel/util/cuda_util.hpp>
11
12namespace FEAT::LAFEM::Arch
13{
14 template <typename DT_, typename IT_>
15 __global__ void cuda_norm2_bcsr(DT_ * row_norms, const DT_ * val, const IT_ * col_idx,
16 const IT_ * row_ptr, const Index rows, const int block_height, bool block_width, const bool squared)
17 {
18 // grid strided for loop
19 for(Index i = threadIdx.x + blockDim.x * blockIdx.x; i < rows * block_height; i += blockDim.x * gridDim.x)
20 {
21 Index csr_row = i / block_height;
22 Index block_row = i % block_height;
23
24 DT_ norm(0);
25 const Index end(row_ptr[csr_row + 1]);
26 for (Index j(row_ptr[csr_row]) ; j < end ; ++j)
27 {
28 for (Index w(0) ; w < block_width ; ++w)
29 {
30 DT_ ival = val[block_height*block_width*j + block_row*block_width + w];
31 norm += ival * ival;
32 }
33 }
34 if(!squared)
35 row_norms[i] = sqrt(norm);
36 }
37 }
38
39 template <typename DT_, typename IT_>
40 void RowNorm2BCSR::exec_cuda_impl(DT_ * row_norms, const DT_ * const val, const IT_ * const col_idx, const IT_ * const row_ptr, const Index rows, const int block_height, const int block_width, const bool squared)
41 {
42 dim3 block(static_cast<unsigned int>(Util::cuda_blocksize_spmv));
43 dim3 grid((static_cast<unsigned int>(rows) + block.x - 1u) / block.x); // round up to next integer
44
45 cuda_norm2_bcsr<<<grid, block>>>(row_norms, val, col_idx, row_ptr, rows, block_height, block_width, squared);
46
47 #ifdef DEBUG
48 cudaDeviceSynchronize();
49 cudaError_t last_error(cudaGetLastError());
50 if (cudaSuccess != last_error)
51 throw InternalError(__func__, __FILE__, __LINE__, "CUDA error occurred in execution!\n" + stringify(cudaGetErrorString(last_error)));
52 #endif
53 }
54
55 template void RowNorm2BCSR::exec_cuda_impl(float *, const float * const, const std::uint64_t * const, const std::uint64_t * const, const Index, const int, const int, const bool);
56 template void RowNorm2BCSR::exec_cuda_impl(double *, const double * const, const std::uint64_t * const, const std::uint64_t * const, const Index, const int, const int, const bool);
57 template void RowNorm2BCSR::exec_cuda_impl(float *, const float * const, const std::uint32_t * const, const std::uint32_t * const, const Index, const int, const int, const bool);
58 template void RowNorm2BCSR::exec_cuda_impl(double *, const double * const, const std::uint32_t * const, const std::uint32_t * const, const Index, const int, const int, const bool);
59} // namespace FEAT::LAFEM::Arch