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.
6#include <kernel/base_header.hpp>
7#include <kernel/util/cuda_util.hpp>
8#include <kernel/voxel_assembly/defo_assembler.hpp>
9#include <kernel/lafem/matrix_gather_scatter_helper.hpp>
10#include <kernel/voxel_assembly/helper/space_helper.hpp>
11#include <kernel/util/tiny_algebra.hpp>
15 namespace VoxelAssembly
19 /**************************************************************************************************************/
21 /**************************************************************************************************************/
23 template<typename Space_, typename DT_, typename IT_, FEAT::Intern::MatrixGatherScatterPolicy pol_ = FEAT::Intern::MatrixGatherScatterPolicy::useLocalOps>
24 __global__ void defo_assembler_matrix1_bcsr(DT_* __restrict__ matrix_data,
25 const IT_* __restrict__ matrix_row_ptr, const IT_* __restrict__ matrix_col_idx, Index matrix_num_rows, Index matrix_num_cols,
26 const Tiny::Vector<DT_, Space_::world_dim>* __restrict__ cub_pt,
27 const DT_* __restrict__ cub_wg, int num_cubs, DT_ alpha,
28 const IT_* __restrict__ cell_to_dof, [[maybe_unused]] Index cell_num,
29 const Tiny::Vector<DT_, Space_::world_dim>* __restrict__ nodes, [[maybe_unused]] Index node_size,
30 const int* __restrict__ coloring_map, Index coloring_size, const IT_* __restrict__ cell_to_dof_sorter, DT_ nu)
32 Index idx = threadIdx.x + blockDim.x * blockIdx.x;
33 if(idx >= coloring_size)
36 typedef Space_ SpaceType;
38 typedef IT_ IndexType;
40 typedef VoxelAssembly::SpaceHelper<SpaceType, DT_, IT_> SpaceHelp;
42 constexpr int dim = SpaceHelp::dim;
45 constexpr int num_loc_dofs = SpaceType::DofMappingType::dof_count;
46 // get number of nodes per element
47 constexpr int num_loc_verts = SpaceType::MeshType::template IndexSet<dim, 0>::Type::num_indices;
49 // typedef Tiny::Vector<DataType, dim> VecValueType;
50 typedef Tiny::Matrix<DataType, dim, dim> MatValueType;
51 // typedef Tiny::Vector<VecValueType, num_loc_dofs> LocalVectorType;
52 typedef Tiny::Matrix<MatValueType, num_loc_dofs, num_loc_dofs> LocalMatrixType;
54 // define local coefficients
55 Tiny::Matrix<DataType, dim, num_loc_verts> local_coeffs;
57 LocalMatrixType loc_mat;
60 //now do work for this cell
61 Index cell = Index(coloring_map[idx]);
62 const IndexSetWrapper<IndexType> local_dofs_w{cell_to_dof, IndexType(num_loc_dofs)};
63 const IndexType* local_dofs = cell_to_dof + cell*num_loc_dofs;
64 const IndexType* local_dof_sorter = cell_to_dof_sorter + cell*num_loc_dofs;
65 SpaceHelp::set_coefficients(local_coeffs, local_dofs_w, nodes, cell);
67 // To minimize code repetition, we call the same kernel from cuda and non cuda build...
68 VoxelAssembly::Kernel::defo_assembly_kernel<SpaceHelp, LocalMatrixType>(loc_mat, local_coeffs, cub_pt, cub_wg, num_cubs, nu);
71 LAFEM::template MatrixGatherScatterHelper<SpaceType, DataType, IndexType, pol_>::scatter_matrix_csr(loc_mat, (MatValueType*)matrix_data, local_dofs, local_dofs, IndexType(matrix_num_rows), IndexType(matrix_num_cols), matrix_row_ptr, matrix_col_idx, alpha, local_dof_sorter);
76 /**************************************************************************************************************/
77 /* CUDA Host OMP Kernels */
78 /**************************************************************************************************************/
80 template<typename Space_, typename DT_, typename IT_, FEAT::Intern::MatrixGatherScatterPolicy pol_ = FEAT::Intern::MatrixGatherScatterPolicy::useLocalOps>
81 void defo_assembler_matrix1_csr_host(DT_* matrix_data,
82 const IT_* matrix_row_ptr, const IT_* matrix_col_idx, Index matrix_num_rows, Index matrix_num_cols,
83 const Tiny::Vector<DT_, Space_::world_dim>* cub_pt,
84 const DT_* cub_wg, int num_cubs, DT_ alpha,
85 const IT_* cell_to_dof, [[maybe_unused]] Index cell_num,
86 const Tiny::Vector<DT_, Space_::world_dim>* nodes, [[maybe_unused]] Index node_size,
87 const int* coloring_map, Index coloring_size,
88 const IT_* cell_to_dof_sorter, DT_ nu)
91 typedef Space_ SpaceType;
93 typedef IT_ IndexType;
95 typedef VoxelAssembly::SpaceHelper<SpaceType, DT_, IT_> SpaceHelp;
97 constexpr int dim = SpaceHelp::dim;
100 constexpr int num_loc_dofs = SpaceType::DofMappingType::dof_count;
101 // get number of nodes per element
102 constexpr int num_loc_verts = SpaceType::MeshType::template IndexSet<dim, 0>::Type::num_indices;
103 //our local datatypes
104 // typedef Tiny::Vector<DataType, dim> VecValueType;
105 typedef Tiny::Matrix<DataType, dim, dim> MatValueType;
106 // typedef Tiny::Vector<VecValueType, num_loc_dofs> LocalVectorType;
107 typedef Tiny::Matrix<MatValueType, num_loc_dofs, num_loc_dofs> LocalMatrixType;
111 FEAT_PRAGMA_OMP(parallel for)
112 for(Index idx = 0; idx < coloring_size; ++idx)
114 // define local coefficients
115 Tiny::Matrix<DataType, dim, num_loc_verts> local_coeffs;
117 LocalMatrixType loc_mat;
118 // now do work for this cell
119 int cell = coloring_map[idx];
120 // std::cout << "Starting with cell " << cell << std::endl;
121 const IndexSetWrapper<IndexType> local_dofs_w{cell_to_dof, IndexType(num_loc_dofs)};
122 const IndexType* local_dofs = cell_to_dof + IndexType(cell)*num_loc_dofs;
123 const IndexType* local_dof_sorter = cell_to_dof_sorter + IndexType(cell)*num_loc_dofs;
125 SpaceHelp::set_coefficients(local_coeffs, local_dofs_w, nodes, cell);
127 // To minimize code repetition, we call the same kernel from cuda and non cuda build...
128 VoxelAssembly::Kernel::defo_assembly_kernel<SpaceHelp, LocalMatrixType>(loc_mat, local_coeffs, cub_pt, cub_wg, num_cubs, nu);
131 LAFEM::template MatrixGatherScatterHelper<SpaceType, DataType, IndexType, pol_>::scatter_matrix_csr(loc_mat, (MatValueType*)matrix_data, local_dofs, local_dofs, IndexType(matrix_num_rows), IndexType(matrix_num_cols), matrix_row_ptr, matrix_col_idx, alpha, local_dof_sorter);
137 /**************************************************************************************************************/
138 /* CUDA kernel wrapper */
139 /**************************************************************************************************************/
143 template<typename Space_, typename DT_, typename IT_>
144 void assemble_defo_csr([[maybe_unused]] const Space_& space,
145 const CSRMatrixData<DT_, IT_>& matrix_data,
146 const AssemblyCubatureData<DT_>& cubature,
147 const AssemblyMappingData<DT_, IT_>& dof_mapping,
148 const Adjacency::ColoringDataHandler& coloring_data,
152 const Memory::TypedView<int> color_view = coloring_data.color_map_view(Memory::Location::cuda, Memory::Access::read);
153 const Memory::TypedView<IT_> cell_to_dof_view(dof_mapping.cell_to_dof.view(Memory::Location::cuda, Memory::Access::read));
154 const Memory::TypedView<IT_> cell_to_dof_sorter_view(dof_mapping.cell_to_dof_sorter.view(Memory::Location::cuda, Memory::Access::read));
155 const Memory::TypedView<Tiny::Vector<DT_, Space_::world_dim>> nodes_view(dof_mapping.nodes.view(Memory::Location::cuda, Memory::Access::read));
157 for(Index i = 0; i < coloring_data.get_num_colors(); ++i)
159 dim3 block(static_cast<unsigned int>(Util::cuda_blocksize_spmv));
160 dim3 grid((static_cast<unsigned int>(coloring_data.get_color_size(i)) + block.x - 1u) / block.x); // round up to next integer
162 //kernel call, since this uses the standard stream, sync before next call is enforced:
163 VoxelAssembly::Kernel::template defo_assembler_matrix1_bcsr<Space_, DT_, IT_, FEAT::Intern::MatrixGatherScatterPolicy::useLocalSortHelper><<< grid, block >>>(
164 matrix_data.data, matrix_data.row_ptr, matrix_data.col_idx, matrix_data.num_rows, matrix_data.num_cols,
165 (const typename Tiny::Vector<DT_, Space_::world_dim>*) cubature.cub_pt,
166 cubature.cub_wg, cubature.num_cubs, alpha,
167 cell_to_dof_view.get_r(), dof_mapping.cell_num,
168 nodes_view.get_r(), dof_mapping.node_size,
169 &color_view.get_r()[coloring_data.get_color_offset(i)], coloring_data.get_color_size(i),
170 cell_to_dof_sorter_view.get_r(), nu
174 //check for cuda error in our kernel
175 Util::cuda_check_last_error();
178 template<typename Space_, typename DT_, typename IT_>
179 void assemble_defo_csr_host([[maybe_unused]] const Space_& space,
180 const CSRMatrixData<DT_, IT_>& matrix_data,
181 const AssemblyCubatureData<DT_>& cubature,
182 const AssemblyMappingData<DT_, IT_>& dof_mapping,
183 const Adjacency::ColoringDataHandler& coloring_data,
187 const Memory::TypedView<int> color_view = coloring_data.color_map_view(Memory::Location::main, Memory::Access::read);
188 const Memory::TypedView<IT_> cell_to_dof_view(dof_mapping.cell_to_dof.view(Memory::Location::main, Memory::Access::read));
189 const Memory::TypedView<IT_> cell_to_dof_sorter_view(dof_mapping.cell_to_dof_sorter.view(Memory::Location::main, Memory::Access::read));
190 const Memory::TypedView<Tiny::Vector<DT_, Space_::world_dim>> nodes_view(dof_mapping.nodes.view(Memory::Location::main, Memory::Access::read));
191 for(Index col = 0; col < coloring_data.get_num_colors(); ++col)
193 VoxelAssembly::Kernel::template defo_assembler_matrix1_csr_host<Space_, DT_, IT_, FEAT::Intern::MatrixGatherScatterPolicy::useLocalSortHelper>(
194 matrix_data.data, matrix_data.row_ptr, matrix_data.col_idx, matrix_data.num_rows, matrix_data.num_cols,
195 (const typename Tiny::Vector<DT_, Space_::world_dim>*) cubature.cub_pt,
196 cubature.cub_wg, cubature.num_cubs, alpha, cell_to_dof_view.get_r(), dof_mapping.cell_num,
197 nodes_view.get_r(), dof_mapping.node_size,
198 &color_view.get_r()[coloring_data.get_color_offset(col)], coloring_data.get_color_size(col),
199 cell_to_dof_sorter_view.get_r(), nu
209using namespace FEAT::VoxelAssembly;
211template void Arch::assemble_defo_csr(const Q2StandardQuad&, const CSRMatrixData<double, std::uint32_t>&, const AssemblyCubatureData<double>&, const AssemblyMappingData<double, std::uint32_t>&,
212 const Adjacency::ColoringDataHandler&, double, double);
213template void Arch::assemble_defo_csr(const Q2StandardQuad&, const CSRMatrixData<float, std::uint32_t>&, const AssemblyCubatureData<float>&, const AssemblyMappingData<float, std::uint32_t>&,
214 const Adjacency::ColoringDataHandler&, float, float);
215template void Arch::assemble_defo_csr(const Q2StandardQuad&, const CSRMatrixData<double, std::uint64_t>&, const AssemblyCubatureData<double>&, const AssemblyMappingData<double, std::uint64_t>&,
216 const Adjacency::ColoringDataHandler&, double, double);
217template void Arch::assemble_defo_csr(const Q2StandardQuad&, const CSRMatrixData<float, std::uint64_t>&, const AssemblyCubatureData<float>&, const AssemblyMappingData<float, std::uint64_t>&,
218 const Adjacency::ColoringDataHandler&, float, float);
219// #ifdef FEAT_HAVE_HALFMATH
220// template void Arch::assemble_defo_csr(const Q2StandardQuad&, const CSRMatrixData<Half, std::uint32_t>&, const AssemblyCubatureData<Half>&, const AssemblyMappingData<Half, std::uint32_t>&,
221// const Adjacency::ColoringDataHandler&, Half, Half);
222// template void Arch::assemble_defo_csr(const Q2StandardQuad&, const CSRMatrixData<Half, std::uint64_t>&, const AssemblyCubatureData<Half>&, const AssemblyMappingData<Half, std::uint64_t>&,
223// const Adjacency::ColoringDataHandler&, Half, Half);
226template void Arch::assemble_defo_csr_host(const Q2StandardQuad&, const CSRMatrixData<double, std::uint32_t>&, const AssemblyCubatureData<double>&, const AssemblyMappingData<double, std::uint32_t>&,
227 const Adjacency::ColoringDataHandler&, double, double);
228template void Arch::assemble_defo_csr_host(const Q2StandardQuad&, const CSRMatrixData<float, std::uint32_t>&, const AssemblyCubatureData<float>&, const AssemblyMappingData<float, std::uint32_t>&,
229 const Adjacency::ColoringDataHandler&, float, float);
230template void Arch::assemble_defo_csr_host(const Q2StandardQuad&, const CSRMatrixData<double, std::uint64_t>&, const AssemblyCubatureData<double>&, const AssemblyMappingData<double, std::uint64_t>&,
231 const Adjacency::ColoringDataHandler&, double, double);
232template void Arch::assemble_defo_csr_host(const Q2StandardQuad&, const CSRMatrixData<float, std::uint64_t>&, const AssemblyCubatureData<float>&, const AssemblyMappingData<float, std::uint64_t>&,
233 const Adjacency::ColoringDataHandler&, float, float);
236template void Arch::assemble_defo_csr(const Q2StandardHexa&, const CSRMatrixData<double, std::uint32_t>&, const AssemblyCubatureData<double>&, const AssemblyMappingData<double, std::uint32_t>&,
237 const Adjacency::ColoringDataHandler&, double, double);
238template void Arch::assemble_defo_csr(const Q2StandardHexa&, const CSRMatrixData<float, std::uint32_t>&, const AssemblyCubatureData<float>&, const AssemblyMappingData<float, std::uint32_t>&,
239 const Adjacency::ColoringDataHandler&, float, float);
240template void Arch::assemble_defo_csr(const Q2StandardHexa&, const CSRMatrixData<double, std::uint64_t>&, const AssemblyCubatureData<double>&, const AssemblyMappingData<double, std::uint64_t>&,
241 const Adjacency::ColoringDataHandler&, double, double);
242template void Arch::assemble_defo_csr(const Q2StandardHexa&, const CSRMatrixData<float, std::uint64_t>&, const AssemblyCubatureData<float>&, const AssemblyMappingData<float, std::uint64_t>&,
243 const Adjacency::ColoringDataHandler&, float, float);
244// #ifdef FEAT_HAVE_HALFMATH
245// template void Arch::assemble_defo_csr(const Q2StandardHexa&, const CSRMatrixData<Half, std::uint32_t>&, const AssemblyCubatureData<Half>&, const AssemblyMappingData<Half, std::uint32_t>&,
246// const Adjacency::ColoringDataHandler&, Half, Half);
247// template void Arch::assemble_defo_csr(const Q2StandardHexa&, const CSRMatrixData<Half, std::uint64_t>&, const AssemblyCubatureData<Half>&, const AssemblyMappingData<Half, std::uint64_t>&,
248// const Adjacency::ColoringDataHandler&, Half, Half);
251template void Arch::assemble_defo_csr_host(const Q2StandardHexa&, const CSRMatrixData<double, std::uint32_t>&, const AssemblyCubatureData<double>&, const AssemblyMappingData<double, std::uint32_t>&,
252 const Adjacency::ColoringDataHandler&, double, double);
253template void Arch::assemble_defo_csr_host(const Q2StandardHexa&, const CSRMatrixData<float, std::uint32_t>&, const AssemblyCubatureData<float>&, const AssemblyMappingData<float, std::uint32_t>&,
254 const Adjacency::ColoringDataHandler&, float, float);
255template void Arch::assemble_defo_csr_host(const Q2StandardHexa&, const CSRMatrixData<double, std::uint64_t>&, const AssemblyCubatureData<double>&, const AssemblyMappingData<double, std::uint64_t>&,
256 const Adjacency::ColoringDataHandler&, double, double);
257template void Arch::assemble_defo_csr_host(const Q2StandardHexa&, const CSRMatrixData<float, std::uint64_t>&, const AssemblyCubatureData<float>&, const AssemblyMappingData<float, std::uint64_t>&,
258 const Adjacency::ColoringDataHandler&, float, float);