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/diagonal_bcsr.hpp>
9#include <kernel/util/exception.hpp>
10#include <kernel/util/cuda_util.hpp>
12namespace FEAT::LAFEM::Arch
14 template <typename DT_, typename IT_>
15 __global__ void cuda_diagonal_bcsr(DT_ * diag, const DT_ * val, const IT_ * col_idx,
16 const IT_ * row_ptr, const Index rows, const int block_size)
18 // grid strided for loop
19 for(Index i = threadIdx.x + blockDim.x * blockIdx.x; i < rows * block_size; i += blockDim.x * gridDim.x)
21 Index csr_row = i / Index(block_size);
22 Index block_row = i % Index(block_size);
26 const Index end(row_ptr[csr_row + 1]);
27 for (Index j(row_ptr[csr_row]) ; j < end ; ++j)
29 if(col_idx[j] == csr_row)
31 diag[i] = val[block_size*block_size*j + block_row*block_size + block_row];
38 template <typename DT_, typename IT_>
39 void DiagonalBCSR::exec_cuda_impl(DT_ * diag, const DT_ * const val, const IT_ * const col_idx, const IT_ * const row_ptr, const Index rows, const int block_size)
41 dim3 block(static_cast<unsigned int>(Util::cuda_blocksize_spmv));
42 dim3 grid((static_cast<unsigned int>(rows) + block.x - 1u) / block.x); // round up to next integer
44 cuda_diagonal_bcsr<<<grid, block>>>(diag, val, col_idx, row_ptr, rows, block_size);
47 cudaDeviceSynchronize();
48 cudaError_t last_error(cudaGetLastError());
49 if (cudaSuccess != last_error)
50 throw InternalError(__func__, __FILE__, __LINE__, "CUDA error occurred in execution!\n" + stringify(cudaGetErrorString(last_error)));
54 template void DiagonalBCSR::exec_cuda_impl(float *, const float * const, const std::uint64_t * const, const std::uint64_t * const, const Index, const int);
55 template void DiagonalBCSR::exec_cuda_impl(double *, const double * const, const std::uint64_t * const, const std::uint64_t * const, const Index, const int);
56 template void DiagonalBCSR::exec_cuda_impl(float *, const float * const, const std::uint32_t * const, const std::uint32_t * const, const Index, const int);
57 template void DiagonalBCSR::exec_cuda_impl(double *, const double * const, const std::uint32_t * const, const std::uint32_t * const, const Index, const int);
58} // namespace FEAT::LAFEM::Arch