FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
defo_assembler.hpp
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#pragma once
7
9#include <kernel/backend.hpp>
10#include <kernel/voxel_assembly/voxel_assembly_common.hpp>
11#include <kernel/voxel_assembly/helper/data_handler.hpp>
12#include <kernel/voxel_assembly/helper/space_helper.hpp>
13#include <kernel/cubature/rule.hpp>
14#include <kernel/lafem/sparse_matrix_bcsr.hpp>
15#include <kernel/trafo/standard/mapping.hpp>
16#include <kernel/geometry/conformal_mesh.hpp>
17#include <kernel/util/tiny_algebra.hpp>
18
19#ifdef FEAT_HAVE_CUDA
20#include <kernel/util/cuda_util.hpp>
21#endif
22
23namespace FEAT
24{
25 namespace VoxelAssembly
26 {
36 template<typename Space_, typename DT_, typename IT_>
37 class VoxelDefoAssembler DOXY({});
38
39 namespace Kernel
40 {
41 template<typename SpaceHelp_, typename LocMatType_, int dim_, int num_verts_>
42 CUDA_HOST_DEVICE void defo_assembly_kernel(LocMatType_& loc_mat, const Tiny::Matrix<typename SpaceHelp_::DataType, dim_, num_verts_>& local_coeffs,
43 const typename SpaceHelp_::DomainPointType* cub_pt, const typename SpaceHelp_::DataType* cub_wg, const int num_cubs,
44 const typename SpaceHelp_::DataType nu)
45 {
46 typedef SpaceHelp_ SpaceHelp;
47 // constexpr int dim = SpaceHelp::dim;
48 typedef typename SpaceHelp::SpaceType SpaceType;
49 typedef typename SpaceHelp::DataType DataType;
50
51 //define local sizes
52 constexpr int num_loc_dofs = SpaceType::DofMappingType::dof_count;
53 //get number of nodes per element
54 // constexpr int num_loc_verts = SpaceType::MeshType::template IndexSet<dim, 0>::Type::num_indices;
55 // define local arrays
56 typename SpaceHelp::JacobianMatrixType loc_jac, loc_jac_inv;
57 typename SpaceHelp::DomainPointType dom_point;
58 typename SpaceHelp::EvalData basis_data;
59
60 loc_mat.format();
61 for(int cub_ind = 0; cub_ind < num_cubs; ++cub_ind)
62 {
63 dom_point = cub_pt[cub_ind];
64 // NewSpaceHelp::map_point(img_point, dom_point, local_coeffs);
65 SpaceHelp::calc_jac_mat(loc_jac, dom_point, local_coeffs);
66 loc_jac_inv.set_inverse(loc_jac);
67
68 SpaceHelp::eval_ref_gradients(basis_data, dom_point);
69 SpaceHelp::trans_gradients(basis_data, loc_jac_inv);
70
71 const DataType weight = loc_jac.vol() * cub_wg[cub_ind];
72 for(int i = 0; i < num_loc_dofs; ++i)
73 {
74 for(int j = 0; j < num_loc_dofs; ++j)
75 {
76 //now assemble outer and inner products
77 loc_mat[i][j].add_scalar_main_diag(nu * weight * Tiny::dot(basis_data.phi[i].grad, basis_data.phi[j].grad));
78 loc_mat[i][j].add_outer_product(basis_data.phi[j].grad, basis_data.phi[i].grad, nu * weight);
79 }
80 }
81
82 }
83
84 }
85 }
86
87 namespace Arch
88 {
89 #ifdef FEAT_HAVE_CUDA
90 template<typename Space_, typename DT_, typename IT_>
91 void assemble_defo_csr(const Space_& space,
92 const CSRMatrixData<DT_, IT_>& matrix_data,
93 const AssemblyCubatureData<DT_>& cubature,
94 const AssemblyMappingData<DT_, IT_>& dof_mapping,
95 const Adjacency::ColoringDataHandler& coloring_data,
96 DT_ alpha, DT_ nu
97 );
98 #endif
99
100 template<typename Space_, typename DT_, typename IT_>
101 void assemble_defo_csr_host(const Space_& space,
102 const CSRMatrixData<DT_, IT_>& matrix_data,
103 const AssemblyCubatureData<DT_>& cubature,
104 const AssemblyMappingData<DT_, IT_>& dof_mapping,
105 const Adjacency::ColoringDataHandler& coloring_data,
106 DT_ alpha, DT_ nu
107 );
108 }
109
110#ifndef __CUDACC__
111 //specialize for Q2Lagrange with standard Trafo
112 template<int dim_, typename DT_, typename IT_>
114 {
115 public:
120 typedef typename SpaceHelp::ShapeType ShapeType;
121 typedef typename SpaceHelp::DataType DataType;
122 typedef typename SpaceHelp::IndexType IndexType;
123
125 static constexpr int dim = SpaceHelp::dim;
126
127 typedef typename SpaceHelp::DomainPointType DomainPointType;
128 typedef typename SpaceHelp::ImagePointType ImagePointType;
129 typedef typename SpaceHelp::ValueType ValueType;
130 typedef typename SpaceHelp::JacobianMatrixType JacobianMatrixType;
131
134
136 DataType nu;
137
138 #ifdef FEAT_HAVE_CUDA
142 #endif
143
144
145 public:
146 explicit VoxelDefoAssembler() = default;
147
148 template<typename ColoringType_>
149 explicit VoxelDefoAssembler(const SpaceType& space, const ColoringType_& coloring, int hint = -1) :
150 mesh_data(space, coloring, hint), nu(DataType(0.))
151 {
152 #ifdef FEAT_HAVE_CUDA
153 // this breaks on windows vm, but is, for now, required since the defo assembler cant handle smaller
154 // stack sizes. For this reason, debug tests are turned of, for now
155 #ifdef DEBUG
156 const std::size_t stack_limit = Util::cuda_get_max_cache_thread();
157 const std::size_t stack_limit_target = sizeof(DataType) * (dim == 3 ? 8096u : 1012u);
158 if(stack_limit < stack_limit_target)
159 Util::cuda_set_max_cache_thread(stack_limit_target);
160 #endif
161 #endif
162 }
163
164 // rule of 5
165 VoxelDefoAssembler(const VoxelDefoAssembler&) = delete;
166
167 VoxelDefoAssembler& operator=(const VoxelDefoAssembler&) = delete;
168
170
171 VoxelDefoAssembler& operator=(VoxelDefoAssembler&&) = delete;
172
174
175
176 template<typename CubatureFactory_>
177 void assemble_matrix1(LAFEM::SparseMatrixBCSR<DataType, IndexType, dim, dim>& matrix, const SpaceType& space, const CubatureFactory_& cubature_factory, DataType alpha = DataType(1)) const
178 {
179 XASSERTM(matrix.num_rows() == space.get_num_dofs(), "invalid matrix dimensions");
180 XASSERTM(matrix.num_cols() == space.get_num_dofs(), "invalid matrix dimensions");
181
182 //define cubature
183 typedef Cubature::Rule<ShapeType, DataType, DataType> CubatureRuleType;
184 CubatureRuleType cubature(Cubature::ctor_factory, cubature_factory);
185
186 //get cubature points and weights
187 int num_cubs = cubature.get_num_points();
188 typename CubatureRuleType::PointType* cub_pt = cubature.get_points();
189 DataType* cub_wg = cubature.get_weights();
190
192 assemble_matrix1_cuda(matrix, space, cub_pt, cub_wg, num_cubs, alpha);
193 else
194 assemble_matrix1_generic(matrix, space, cub_pt, cub_wg, num_cubs, alpha);
195 }
196
197
198 void assemble_matrix1_generic(LAFEM::SparseMatrixBCSR<DataType, IndexType, dim, dim>& matrix, const SpaceType& space, typename Cubature::Rule<ShapeType, DataType, DataType>::PointType* cub_pt, const DataType* cub_wg, int num_cubs, DataType alpha) const
199 {
200 Memory::TypedView<DT_> val_view(matrix.val_view_raw_rw());
201 const Memory::TypedView<IT_> row_ptr_view(matrix.row_ptr_view_r());
202 const Memory::TypedView<IT_> col_idx_view(matrix.col_idx_view_r());
203 VoxelAssembly::CSRMatrixData<DataType, IndexType> mat_data = {val_view.get_w(), row_ptr_view.get_r(), col_idx_view.get_r(), matrix.num_rows(), matrix.num_cols()};
204
205 AssemblyCubatureData<DataType> cub_data = {(void*)cub_pt, cub_wg, num_cubs};
206 AssemblyMappingData<DataType, IndexType> mapping_data = mesh_data.get_assembly_field();
207
208
209 VoxelAssembly::Arch::assemble_defo_csr_host(space, mat_data, cub_data, mapping_data, mesh_data.coloring_data, alpha, nu);
210 }
211
212 #ifdef FEAT_HAVE_CUDA
213 void assemble_matrix1_cuda(LAFEM::SparseMatrixBCSR<DataType, IndexType, dim, dim>& matrix, const SpaceType& space, typename Cubature::Rule<ShapeType, DataType, DataType>::PointType* cub_pt, const DataType* cub_wg, int num_cubs, DataType alpha) const
214 {
215 //VoxelAssembly::CSRMatrixData<DataType, IndexType> mat_data = {matrix.template val<LAFEM::Perspective::pod>(), matrix.row_ptr(), matrix.col_idx(), matrix.num_rows(), matrix.num_cols()};
216 Memory::TypedView<DT_> val_view(matrix.val_view_raw_rw(Memory::Location::cuda));
217 const Memory::TypedView<IT_> row_ptr_view(matrix.row_ptr_view_r(Memory::Location::cuda));
218 const Memory::TypedView<IT_> col_idx_view(matrix.col_idx_view_r(Memory::Location::cuda));
219 VoxelAssembly::CSRMatrixData<DataType, IndexType> mat_data = {val_view.get_w(), row_ptr_view.get_r(), col_idx_view.get_r(), matrix.num_rows(), matrix.num_cols()};
220
221 // upload the cubature into the cached device buffers (if it changed) and fetch the device view;
222 // the arbiter sources the host->device copy from its own persistent host memory, so this is
223 // safe even when the assembly runs asynchronously (no end-of-kernel sync / stream-ordered free)
224 this->_cub_cache.update(cub_pt, cub_wg, num_cubs);
225 VoxelAssembly::AssemblyCubatureData<DataType> d_cub_data = this->_cub_cache.device_data();
226 VoxelAssembly::AssemblyMappingData<DataType, IndexType> d_mapping_data = mesh_data.get_assembly_field();
227
228
229 VoxelAssembly::Arch::assemble_defo_csr(space, mat_data, d_cub_data, d_mapping_data, mesh_data.coloring_data, alpha, nu);
230 }
231
232 #else //FEAT_HAVE_CUDA
233 void assemble_matrix1_cuda(LAFEM::SparseMatrixBCSR<DataType, IndexType, dim, dim>& DOXY(matrix), const SpaceType& DOXY(space), typename Cubature::Rule<ShapeType, DataType, DataType>::PointType* DOXY(cub_pt), const DataType* DOXY(cub_wg), int DOXY(num_cubs), DataType DOXY(alpha)) const
234 {
235 XABORTM("What in the nine hells are you doing here?");
236 }
237 #endif
238
239 }; //class GPUPoissonAssembler
240
241 #endif // __CUDACC__
242
243
244 }
245}
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
FEAT Kernel base header.
static PreferredBackend get_preferred_backend()
get current preferred backend
Definition: backend.cpp:32
Cubature Rule class template.
Definition: rule.hpp:38
CSR based blocked sparse matrix.
Index num_rows() const
Retrieve matrix row count.
Index num_cols() const
Retrieve matrix column count.
Standard Lagrange-2 Finite-Element space class template.
Definition: element.hpp:39
Tiny Matrix class template.
Owns the device-resident cubature (points + weights) of a voxel assembler.
AssemblyCubatureData< DT_ > device_data() const
Returns the device-side view of the cached cubature.
void update(const PointType_ *pts, const DT_ *wgs, int num_cubs)
Uploads the cubature into the cached host buffers if it changed.
Data handler for Lagrange based FE spaces.
Adjacency::ColoringDataHandler coloring_data
Datahandler for the coloring data.
Deformation Voxel Assembly template.
CUDA_HOST_DEVICE T_ dot(const T_ &a, const T_ &b)
Computes the dot-product of two scalars.
FEAT namespace.
Definition: adjactor.hpp:12
@ dom_point
specifies whether the trafo should supply domain point coordinates