FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
defo_assembler.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#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>
12
13namespace FEAT
14{
15 namespace VoxelAssembly
16 {
17 namespace Kernel
18 {
19 /**************************************************************************************************************/
20 /* CUDA Kernel */
21 /**************************************************************************************************************/
22
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)
31 {
32 Index idx = threadIdx.x + blockDim.x * blockIdx.x;
33 if(idx >= coloring_size)
34 return;
35 // define types
36 typedef Space_ SpaceType;
37 typedef DT_ DataType;
38 typedef IT_ IndexType;
39
40 typedef VoxelAssembly::SpaceHelper<SpaceType, DT_, IT_> SpaceHelp;
41
42 constexpr int dim = SpaceHelp::dim;
43
44 // define local sizes
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;
48 //our local datatypes
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;
53
54 // define local coefficients
55 Tiny::Matrix<DataType, dim, num_loc_verts> local_coeffs;
56 //define local array
57 LocalMatrixType loc_mat;
58
59
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);
66
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);
69
70 //scatter
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);
72
73 }
74
75
76 /**************************************************************************************************************/
77 /* CUDA Host OMP Kernels */
78 /**************************************************************************************************************/
79
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)
89 {
90 // define types
91 typedef Space_ SpaceType;
92 typedef DT_ DataType;
93 typedef IT_ IndexType;
94
95 typedef VoxelAssembly::SpaceHelper<SpaceType, DT_, IT_> SpaceHelp;
96
97 constexpr int dim = SpaceHelp::dim;
98
99 // define local sizes
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;
108
109
110
111 FEAT_PRAGMA_OMP(parallel for)
112 for(Index idx = 0; idx < coloring_size; ++idx)
113 {
114 // define local coefficients
115 Tiny::Matrix<DataType, dim, num_loc_verts> local_coeffs;
116 // and local matrix
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;
124
125 SpaceHelp::set_coefficients(local_coeffs, local_dofs_w, nodes, cell);
126
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);
129
130 // scatter
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);
132 }
133 }
134 }
135
136
137 /**************************************************************************************************************/
138 /* CUDA kernel wrapper */
139 /**************************************************************************************************************/
140
141 namespace Arch
142 {
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,
149 DT_ alpha, DT_ nu
150 )
151 {
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));
156
157 for(Index i = 0; i < coloring_data.get_num_colors(); ++i)
158 {
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
161
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
171 );
172 }
173
174 //check for cuda error in our kernel
175 Util::cuda_check_last_error();
176 }
177
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,
184 DT_ alpha, DT_ nu
185 )
186 {
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)
192 {
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
200 );
201 }
202 }
203 }
204 }
205}
206
207
208using namespace FEAT;
209using namespace FEAT::VoxelAssembly;
210
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);
224// #endif
225
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);
234
235
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);
249// #endif
250
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);