FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
poisson_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_csr.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 {
27
37 template<typename Space_, typename DT_, typename IT_>
38 class VoxelPoissonAssembler DOXY({});
39
40
44 namespace Kernel
45 {
46 template<typename SpaceHelp_, typename LocMatType_, int dim_, int num_verts_>
47 CUDA_HOST_DEVICE void poisson_assembly_kernel(LocMatType_& loc_mat, const Tiny::Matrix<typename SpaceHelp_::DataType, dim_, num_verts_>& local_coeffs,
48 const typename SpaceHelp_::DomainPointType* cub_pt, const typename SpaceHelp_::DataType* cub_wg, const int num_cubs)
49 {
50 typedef SpaceHelp_ SpaceHelp;
51 // constexpr int dim = SpaceHelp::dim;
52 typedef typename SpaceHelp::SpaceType SpaceType;
53 typedef typename SpaceHelp::DataType DataType;
54
55 //define local sizes
56 constexpr int num_loc_dofs = SpaceType::DofMappingType::dof_count;
57 //get number of nodes per element
58 // constexpr int num_loc_verts = SpaceType::MeshType::template IndexSet<dim, 0>::Type::num_indices;
59 // define local arrays
60 typename SpaceHelp::JacobianMatrixType loc_jac, loc_jac_inv;
61 typename SpaceHelp::DomainPointType dom_point;
62 typename SpaceHelp::EvalData basis_data;
63 DataType jac_det = DataType(0);
64
65 loc_mat.format();
66 for(int cub_ind = 0; cub_ind < num_cubs; ++cub_ind)
67 {
68 dom_point = cub_pt[cub_ind];
69 // NewSpaceHelp::map_point(img_point, dom_point, local_coeffs);
70 SpaceHelp::calc_jac_mat(loc_jac, dom_point, local_coeffs);
71 loc_jac_inv.set_inverse(loc_jac);
72 jac_det = loc_jac.det();
73
74 SpaceHelp::eval_ref_gradients(basis_data, dom_point);
75 SpaceHelp::trans_gradients(basis_data, loc_jac_inv);
76
77 for(int i = 0; i < num_loc_dofs; ++i)
78 {
79 for(int j = 0; j < num_loc_dofs; ++j)
80 {
81 //simple assembly for poisson operator
82 Tiny::axpy(loc_mat(i,j), Tiny::dot(basis_data.phi[j].grad, basis_data.phi[i].grad), jac_det * cub_wg[cub_ind]);
83 }
84 }
85
86 }
87 }
88 }
89
93 namespace Arch
94 {
95 #ifdef FEAT_HAVE_CUDA
96 template<typename Space_, typename DT_, typename IT_>
97 void assemble_poisson_csr(const Space_& space,
98 const CSRMatrixData<DT_, IT_>& matrix_data,
99 const AssemblyCubatureData<DT_>& cubature,
100 const AssemblyMappingData<DT_, IT_>& dof_mapping,
101 const std::vector<int*>& coloring_maps,
102 const std::vector<Index>& coloring_map_sizes,
103 DT_ alpha
104 );
105 #endif
106
107 template<typename Space_, typename DT_, typename IT_>
108 void assemble_poisson_csr_host(const Space_& space,
109 const CSRMatrixData<DT_, IT_>& matrix_data,
110 const AssemblyCubatureData<DT_>& cubature,
111 const AssemblyMappingData<DT_, IT_>& dof_mapping,
112 const std::vector<int*>& coloring_maps,
113 const std::vector<Index>& coloring_map_sizes,
114 DT_ alpha
115 );
116
117 }
118
119#ifndef __CUDACC__
120 //specialize for Q2Lagrange with standard Trafo
121 template<int dim_, typename DT_, typename IT_>
123 {
124 public:
129 typedef typename SpaceHelp::ShapeType ShapeType;
130 typedef typename SpaceHelp::DataType DataType;
131 typedef typename SpaceHelp::IndexType IndexType;
132
134 static constexpr int dim = SpaceHelp::dim;
135
136 typedef typename SpaceHelp::DomainPointType DomainPointType;
137 typedef typename SpaceHelp::ImagePointType ImagePointType;
138 typedef typename SpaceHelp::ValueType ValueType;
139 typedef typename SpaceHelp::JacobianMatrixType JacobianMatrixType;
140
143
144
145 public:
146 explicit VoxelPoissonAssembler() = default;
147
148 template<typename ColoringType_>
149 explicit VoxelPoissonAssembler(const SpaceType& space, const ColoringType_& coloring, int hint = -1) :
150 mesh_data(space, coloring, hint)
151 {
152 #ifdef FEAT_HAVE_CUDA
153 #ifdef DEBUG
154 const std::size_t stack_limit = Util::cuda_get_max_cache_thread();
155 const std::size_t stack_limit_target = sizeof(DataType) * (dim == 3 ? 8096u : 1012u);
156 if(stack_limit < stack_limit_target)
157 Util::cuda_set_max_cache_thread(stack_limit_target);
158 #endif
159 #endif
160 }
161
162 // rule of 5
164
165 VoxelPoissonAssembler& operator=(const VoxelPoissonAssembler&) = delete;
166
168
169 VoxelPoissonAssembler& operator=(VoxelPoissonAssembler&&) = delete;
170
172
173 template<typename CubatureFactory_>
174 void assemble_matrix1(LAFEM::SparseMatrixCSR<DataType, IndexType>& matrix, const SpaceType& space, const CubatureFactory_& cubature_factory, DataType alpha = DataType(1)) const
175 {
176 XASSERTM(matrix.rows() == space.get_num_dofs(), "invalid matrix dimensions");
177 XASSERTM(matrix.columns() == space.get_num_dofs(), "invalid matrix dimensions");
178
179 //define cubature
180 typedef Cubature::Rule<ShapeType, DataType, DataType> CubatureRuleType;
181 CubatureRuleType cubature(Cubature::ctor_factory, cubature_factory);
182
183 //get cubature points and weights
184 int num_cubs = cubature.get_num_points();
185 typename CubatureRuleType::PointType* cub_pt = cubature.get_points();
186 DataType* cub_wg = cubature.get_weights();
187
188 BACKEND_SKELETON_VOID(assemble_matrix1_cuda, assemble_matrix1_generic, assemble_matrix1_generic, matrix, space, cub_pt, cub_wg, num_cubs, alpha)
189 }
190
191
192 void assemble_matrix1_generic(LAFEM::SparseMatrixCSR<DataType, IndexType>& matrix, const SpaceType& space, typename Cubature::Rule<ShapeType, DataType, DataType>::PointType* cub_pt, const DataType* cub_wg, int num_cubs, DataType alpha) const
193 {
194 CSRMatrixData<DataType, IndexType> mat_data = {matrix.val(), matrix.row_ptr(), matrix.col_ind(), matrix.rows(), matrix.columns()};
195
196 AssemblyCubatureData<DataType> cub_data = {(void*)cub_pt, cub_wg, num_cubs};
197 AssemblyMappingData<DataType, IndexType> mapping_data = mesh_data.get_assembly_field();
198
199
200 VoxelAssembly::Arch::assemble_poisson_csr_host(space, mat_data, cub_data, mapping_data, mesh_data.get_coloring_maps(), mesh_data.get_color_map_sizes(), alpha);
201 }
202
203 #ifdef FEAT_HAVE_CUDA
204 void assemble_matrix1_cuda(LAFEM::SparseMatrixCSR<DataType, IndexType>& matrix, const SpaceType& space, typename Cubature::Rule<ShapeType, DataType, DataType>::PointType* cub_pt, const DataType* cub_wg, int num_cubs, DataType alpha) const
205 {
206 VoxelAssembly::CSRMatrixData<DataType, IndexType> mat_data = {matrix.val(), matrix.row_ptr(), matrix.col_ind(), matrix.rows(), matrix.columns()};
207
208 typedef typename Cubature::Rule<ShapeType, DataType, DataType>::PointType CubPointType;
209 //initialize all necessary pointer arrays and values //maybe more sense to specify cubature rule and set this to a const mem location?
210 void* cub_pt_device = Util::cuda_malloc(num_cubs * sizeof(CubPointType));
211 Util::cuda_copy_host_to_device(cub_pt_device, (void*)cub_pt, num_cubs * sizeof(CubPointType));
212
213 void* cub_wg_device = Util::cuda_malloc(num_cubs * sizeof(DataType));
214 Util::cuda_copy_host_to_device(cub_wg_device, (void*)cub_wg, num_cubs * sizeof(DataType));
215
216 VoxelAssembly::AssemblyCubatureData<DataType> d_cub_data = {cub_pt_device, (DataType*)cub_wg_device, num_cubs};
217 VoxelAssembly::AssemblyMappingData<DataType, IndexType> d_mapping_data = mesh_data.get_assembly_field();
218
219
220 VoxelAssembly::Arch::assemble_poisson_csr(space, mat_data, d_cub_data, d_mapping_data, mesh_data.get_coloring_maps(), mesh_data.get_color_map_sizes(), alpha);
221 //free resources
222 Util::cuda_free(cub_wg_device);
223 Util::cuda_free(cub_pt_device);
224 }
225
226 #else //FEAT_HAVE_CUDA
227 void assemble_matrix1_cuda(LAFEM::SparseMatrixCSR<DataType, IndexType>& 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
228 {
229 XABORTM("What in the nine hells are you doing here?");
230 }
231 #endif
232
233 }; //class GPUPoissonAssembler
234
235 #endif // __CUDACC__
236 }
237}
#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.
Cubature Rule class template.
Definition: rule.hpp:38
CSR based sparse matrix.
Index rows() const
Retrieve matrix row count.
Index columns() const
Retrieve matrix column count.
Standard Lagrange-2 Finite-Element space class template.
Definition: element.hpp:39
Tiny Matrix class template.
Data handler for Lagrange based FE spaces.
Poisson Voxel Assembly template.
CUDA_HOST_DEVICE T_ dot(const T_ &a, const T_ &b)
Computes the dot-product of two scalars.
CUDA_HOST_DEVICE void axpy(T_ &y, const T_ &x, const T_ &alpha)
Performs an AXPY of two scalars.
FEAT namespace.
Definition: adjactor.hpp:12
@ dom_point
specifies whether the trafo should supply domain point coordinates
@ jac_det
specifies whether the trafo should supply jacobian determinants