Debian Codegraph @

fem/tmop/tools/discrete.cpp

raw
1 // Copyright (c) 2010-2025, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11
12 #include "../pa.hpp"
13 #include "../../tmop.hpp"
14 #include "../../kernels.hpp"
15 #include "../../../general/forall.hpp"
16
17 namespace mfem
18 {
19
20 template <int MD1, int MQ1, int T_D1D = 0, int T_Q1D = 0>
21 void TMOP_DatcSize_2D(const int NE,
22 const int ncomp,
23 const int sizeidx,
24 const real_t input_min_size,
25 const real_t *nc_reduce,
26 const ConstDeviceMatrix &W,
27 const real_t *b,
28 const DeviceTensor<4, const real_t> &X,
29 DeviceTensor<5> &J,
30 const int d1d = 0,
31 const int q1d = 0)
32 {
33 static constexpr int DIM = 2;
34 const int D1D = T_D1D ? T_D1D : d1d;
35 const int Q1D = T_Q1D ? T_Q1D : q1d;
36 MFEM_VERIFY(D1D <= Q1D, "");
37
38 const real_t infinity = std::numeric_limits<real_t>::infinity();
39
40 mfem::forall_2D(NE, Q1D, Q1D, [=] MFEM_HOST_DEVICE(int e)
41 {
42 MFEM_SHARED real_t sB[MD1][MQ1];
43 MFEM_SHARED real_t smem[MQ1][MQ1];
44 MFEM_SHARED real_t min_size[MFEM_CUDA_BLOCKS];
45
46 kernels::internal::v_regs2d_t<1,MQ1> r0, r1; // scalar X (component sizeidx)
47
48 kernels::internal::LoadDofs2d(e, D1D, X, r0);
49
50 DeviceTensor<2, real_t> M((real_t *)(min_size), D1D, D1D);
51 MFEM_FOREACH_THREAD(t, x, MFEM_CUDA_BLOCKS) { min_size[t] = infinity; }
52 MFEM_SYNC_THREAD;
53 MFEM_FOREACH_THREAD_DIRECT(dy, y, D1D)
54 {
55 MFEM_FOREACH_THREAD_DIRECT(dx, x, D1D)
56 {
57 M(dy, dx) = r0(sizeidx, dy, dx);
58 }
59 }
60
61 MFEM_SYNC_THREAD;
62 for (int wrk = MFEM_CUDA_BLOCKS >> 1; wrk > 0; wrk >>= 1)
63 {
64 MFEM_FOREACH_THREAD(t, x, MFEM_CUDA_BLOCKS)
65 {
66 if (t < wrk && MFEM_THREAD_ID(y) == 0 && MFEM_THREAD_ID(z) == 0)
67 {
68 min_size[t] = fmin(min_size[t], min_size[t + wrk]);
69 }
70 }
71 MFEM_SYNC_THREAD;
72 }
73 real_t min = min_size[0];
74 if (input_min_size > 0.0) { min = input_min_size; }
75
76 kernels::internal::LoadMatrix(D1D, Q1D, b, sB);
77 kernels::internal::Eval2d(D1D, Q1D, smem, sB, r0, r1);
78
79 MFEM_FOREACH_THREAD_DIRECT(qy, y, Q1D)
80 {
81 MFEM_FOREACH_THREAD_DIRECT(qx, x, Q1D)
82 {
83 const real_t T = r1(0, qy, qx);
84
85 const real_t shape_par_vals = T;
86 const real_t size = fmax(shape_par_vals, min) / nc_reduce[e];
87 const real_t alpha = std::pow(size, 1.0 / DIM);
88 for (int i = 0; i < DIM; i++)
89 {
90 for (int j = 0; j < DIM; j++)
91 {
92 J(i, j, qx, qy, e) = alpha * W(i, j);
93 }
94 }
95 }
96 }
97 });
98 }
99
100 MFEM_TMOP_MDQ_REGISTER(TMOPDatc2Size, TMOP_DatcSize_2D);
101 MFEM_TMOP_MDQ_SPECIALIZE(TMOPDatc2Size);
102
103 template <int MD1, int MQ1, int T_D1D = 0, int T_Q1D = 0>
104 void TMOP_DatcSize_3D(const int NE,
105 const int ncomp,
106 const int sizeidx,
107 const real_t input_min_size,
108 const real_t *nc_reduce,
109 const ConstDeviceMatrix &W,
110 const real_t *b,
111 const DeviceTensor<5, const real_t> &X,
112 DeviceTensor<6> &J,
113 const int d1d = 0,
114 const int q1d = 0)
115 {
116 static constexpr int DIM = 3;
117 const int D1D = T_D1D ? T_D1D : d1d;
118 const int Q1D = T_Q1D ? T_Q1D : q1d;
119
120 const real_t infinity = std::numeric_limits<real_t>::infinity();
121
122 mfem::forall_2D(NE, Q1D, Q1D, [=] MFEM_HOST_DEVICE(int e)
123 {
124 MFEM_SHARED real_t sB[MD1][MQ1];
125 MFEM_SHARED real_t smem[MQ1][MQ1];
126 MFEM_SHARED real_t min_size[MFEM_CUDA_BLOCKS];
127
128 kernels::internal::v_regs3d_t<1,MQ1> r0, r1; // scalar X (component sizeidx)
129
130 kernels::internal::LoadDofs3d(e, D1D, X, r0);
131
132 DeviceTensor<3, real_t> M((real_t *)(min_size), D1D, D1D, D1D);
133 MFEM_FOREACH_THREAD(t, x, MFEM_CUDA_BLOCKS) { min_size[t] = infinity; }
134 MFEM_SYNC_THREAD;
135 for (int dz = 0; dz < D1D; ++dz)
136 {
137 MFEM_FOREACH_THREAD_DIRECT(dy, y, D1D)
138 {
139 MFEM_FOREACH_THREAD_DIRECT(dx, x, D1D)
140 {
141 M(dz, dy, dx) = r0(sizeidx, dz, dy, dx);
142 }
143 }
144 }
145 MFEM_SYNC_THREAD;
146 for (int wrk = MFEM_CUDA_BLOCKS >> 1; wrk > 0; wrk >>= 1)
147 {
148 MFEM_FOREACH_THREAD(t, x, MFEM_CUDA_BLOCKS)
149 {
150 if (t < wrk && MFEM_THREAD_ID(y) == 0 && MFEM_THREAD_ID(z) == 0)
151 {
152 min_size[t] = fmin(min_size[t], min_size[t + wrk]);
153 }
154 }
155 MFEM_SYNC_THREAD;
156 }
157 real_t min = min_size[0];
158 if (input_min_size > 0.0) { min = input_min_size; }
159
160 kernels::internal::LoadMatrix(D1D, Q1D, b, sB);
161 kernels::internal::Eval3d(D1D, Q1D, smem, sB, r0, r1);
162
163 for (int qz = 0; qz < Q1D; ++qz)
164 {
165 MFEM_FOREACH_THREAD_DIRECT(qy, y, Q1D)
166 {
167 MFEM_FOREACH_THREAD_DIRECT(qx, x, Q1D)
168 {
169 const real_t T = r1(0, qz, qy, qx);
170
171 const real_t shape_par_vals = T;
172 const real_t size = fmax(shape_par_vals, min) / nc_reduce[e];
173 const real_t alpha = std::pow(size, 1.0 / DIM);
174 for (int i = 0; i < DIM; i++)
175 {
176 for (int j = 0; j < DIM; j++)
177 {
178 J(i, j, qx, qy, qz, e) = alpha * W(i, j);
179 }
180 }
181 }
182 }
183 }
184 });
185 }
186
187 MFEM_TMOP_MDQ_REGISTER(TMOPDatc3Size, TMOP_DatcSize_3D);
188 MFEM_TMOP_MDQ_SPECIALIZE(TMOPDatc3Size);
189
190 // PA.Jtr Size = (dim, dim, PA.ne*PA.nq);
191 void DiscreteAdaptTC::ComputeAllElementTargets(const FiniteElementSpace &pa_fes,
192 const IntegrationRule &ir,
193 const Vector &xe,
194 DenseTensor &Jtr) const
195 {
196 MFEM_VERIFY(target_type == IDEAL_SHAPE_GIVEN_SIZE ||
197 target_type == GIVEN_SHAPE_AND_SIZE, "Wrong target type.");
198
199 MFEM_VERIFY(tspec_fesv, "No target specifications have been set.");
200 const FiniteElementSpace *fes = tspec_fesv;
201
202 // The kernels are implemented only for size-adaptivity with ideal shape.
203 const bool use_kernel = (skewidx == -1 && aspectratioidx == -1 &&
204 orientationidx == -1 && sizeidx != -1 &&
205 target_type == IDEAL_SHAPE_GIVEN_SIZE);
206 if (use_kernel == false)
207 {
208 return ComputeAllElementTargets_Fallback(pa_fes, ir, xe, Jtr);
209 }
210
211 const Mesh *mesh = fes->GetMesh();
212 const int NE = mesh->GetNE();
213 const int dim = mesh->Dimension();
214 MFEM_VERIFY(mesh->GetNumGeometries(dim) <= 1,
215 "mixed meshes are not supported");
216 MFEM_VERIFY(!fes->IsVariableOrder(), "variable orders are not supported");
217 const FiniteElement &fe = *fes->GetTypicalFE();
218 if (current_W_type != fe.GetGeomType())
219 {
220 current_W_type = fe.GetGeomType();
221 current_W = Geometries.GetGeomToPerfGeomJac(current_W_type);
222 }
223
224 const DofToQuad::Mode mode = DofToQuad::TENSOR;
225 const DofToQuad &maps = fe.GetDofToQuad(ir, mode);
226 const int d = maps.ndof, q = maps.nqpt;
227 const real_t input_min_size = lim_min_size;
228
229 MFEM_VERIFY(ncomp == 1 && sizeidx == 0, "ncomp or sizeidx error.");
230 MFEM_VERIFY(d <= DeviceDofQuadLimits::Get().MAX_D1D, "");
231 MFEM_VERIFY(q <= DeviceDofQuadLimits::Get().MAX_Q1D, "");
232
233 Vector nc_size_red(NE, Device::GetDeviceMemoryType());
234 nc_size_red.UseDevice(true);
235 nc_size_red.HostWrite();
236 NCMesh *ncmesh = tspec_fesv->GetMesh()->ncmesh;
237 for (int e = 0; e < NE; e++)
238 {
239 nc_size_red(e) = (ncmesh) ? ncmesh->GetElementSizeReduction(e) : 1.0;
240 }
241 const real_t *nc_reduce = nc_size_red.Read();
242
243 auto R = fes->GetElementRestriction(ElementDofOrdering::LEXICOGRAPHIC);
244 MFEM_VERIFY(R, "Invalid ElementRestriction.");
245 const int R_height = (dim == 2) ? NE*ncomp*d*d : NE*ncomp*d*d*d;
246 MFEM_VERIFY(R->Height() == R_height, "Incorrect ElementRestriction size.");
247 Vector tspec_e(R->Height(), Device::GetDeviceMemoryType());
248 tspec_e.UseDevice(true);
249 tspec.UseDevice(true);
250 R->Mult(tspec, tspec_e);
251
252 const auto *b = maps.B.Read();
253
254 if (dim == 2)
255 {
256 const auto W = Reshape(current_W.Read(), 2, 2);
257 const auto X = Reshape(tspec_e.Read(), d, d, ncomp, NE);
258 auto J = Reshape(Jtr.Write(), 2, 2, q, q, NE);
259 TMOPDatc2Size::Run(d, q,
260 NE, ncomp, sizeidx, input_min_size, nc_reduce, W, b, X, J,
261 d, q);
262 }
263 else
264 {
265 const auto W = Reshape(current_W.Read(), 3, 3);
266 const auto X = Reshape(tspec_e.Read(), d, d, d, ncomp, NE);
267 auto J = Reshape(Jtr.Write(), 3, 3, q, q, q, NE);
268 TMOPDatc3Size::Run(d, q,
269 NE, ncomp, sizeidx, input_min_size, nc_reduce, W, b, X, J,
270 d, q);
271 }
272 }
273
274 } // namespace mfem