Line data Source code
1 : /**
2 : * @file energy_solver_avx2.c
3 : * @brief AVX2-vectorized energy equation solver (advection-diffusion step)
4 : *
5 : * Solves: dT/dt + u*nabla(T) = alpha * nabla^2(T) + Q
6 : * using explicit Euler time integration, central differences for diffusion, and
7 : * central or first-order upwind differences for advection
8 : * (params->convection_scheme).
9 : *
10 : * Same numerics as the scalar reference (energy/cpu/energy_solver.c). The
11 : * interior stencil is vectorized along the unit-stride i direction (4 doubles
12 : * per AVX2 register) with a scalar remainder tail; the j loop is parallelized
13 : * with OpenMP. When AVX2 is unavailable at compile time the whole interior is
14 : * processed with the scalar path, so this file always provides a correct
15 : * implementation. The optional heat source Q is applied in a unified scalar
16 : * pass (only when a callback is configured), keeping the vector path branchless.
17 : *
18 : * Branch-free 3D: when nz==1, stride_z=0 and inv_2dz/inv_dz2=0.0 cause all
19 : * z-terms to vanish, producing identical results to a 2D code path.
20 : */
21 :
22 : #define _USE_MATH_DEFINES
23 :
24 : #include "../energy_solver_internal.h"
25 :
26 : #include "cfd/core/indexing.h"
27 : #include "cfd/core/memory.h"
28 : #include "cfd/math/stencils.h"
29 : #include "../../navier_stokes/avx2/upwind_avx2.h"
30 :
31 : #include <math.h>
32 : #include <stddef.h>
33 : #include <string.h>
34 :
35 : #ifdef _OPENMP
36 : #include <omp.h>
37 : #endif
38 :
39 : #if defined(CFD_HAS_AVX2)
40 : #include <immintrin.h>
41 : #define USE_AVX 1
42 : #else
43 : #define USE_AVX 0
44 : #endif
45 :
46 0 : cfd_status_t energy_step_explicit_avx2_with_workspace(
47 : flow_field* field, const grid* grid,
48 : const ns_solver_params_t* params,
49 : double dt, double time,
50 : double* T_workspace, size_t workspace_size) {
51 0 : if (!field || !grid || !params) {
52 0 : cfd_set_error(CFD_ERROR_INVALID,
53 : "energy_solver_avx2: field, grid, and params must be non-NULL");
54 0 : return CFD_ERROR_INVALID;
55 : }
56 0 : if (!field->T) {
57 0 : cfd_set_error(CFD_ERROR_INVALID,
58 : "energy_solver_avx2: missing temperature field");
59 0 : return CFD_ERROR_INVALID;
60 : }
61 :
62 : /* Skip when energy equation is disabled */
63 0 : if (params->alpha <= 0.0) {
64 : return CFD_SUCCESS;
65 : }
66 :
67 0 : size_t nx = field->nx;
68 0 : size_t ny = field->ny;
69 0 : size_t nz = field->nz;
70 :
71 0 : if (!grid->dx || !grid->dy || nx < 3 || ny < 3) {
72 0 : cfd_set_error(CFD_ERROR_INVALID,
73 : "energy_solver_avx2: grid too small or missing dx/dy");
74 0 : return CFD_ERROR_INVALID;
75 : }
76 0 : size_t plane = nx * ny;
77 0 : size_t total = plane * nz;
78 0 : double alpha = params->alpha;
79 :
80 : /* Validate uniform spacing (central-difference stencil assumes it) */
81 0 : const double dx0 = grid->dx[0];
82 0 : const double dy0 = grid->dy[0];
83 : {
84 0 : const double tol_x = 1e-12 * fmax(1.0, fabs(dx0));
85 0 : for (size_t i = 1; i < nx - 1; i++) {
86 0 : if (fabs(grid->dx[i] - dx0) > tol_x) {
87 0 : cfd_set_error(CFD_ERROR_UNSUPPORTED,
88 : "energy_solver_avx2: non-uniform dx not supported");
89 0 : return CFD_ERROR_UNSUPPORTED;
90 : }
91 : }
92 0 : const double tol_y = 1e-12 * fmax(1.0, fabs(dy0));
93 0 : for (size_t j = 1; j < ny - 1; j++) {
94 0 : if (fabs(grid->dy[j] - dy0) > tol_y) {
95 0 : cfd_set_error(CFD_ERROR_UNSUPPORTED,
96 : "energy_solver_avx2: non-uniform dy not supported");
97 0 : return CFD_ERROR_UNSUPPORTED;
98 : }
99 : }
100 : }
101 0 : if (nz > 1) {
102 0 : if (!grid->dz) {
103 0 : cfd_set_error(CFD_ERROR_INVALID,
104 : "energy_solver_avx2: missing dz for 3D energy solve");
105 0 : return CFD_ERROR_INVALID;
106 : }
107 0 : const double dz0 = grid->dz[0];
108 0 : const double tol_z = 1e-12 * fmax(1.0, fabs(dz0));
109 0 : for (size_t k = 1; k < nz - 1; k++) {
110 0 : if (fabs(grid->dz[k] - dz0) > tol_z) {
111 0 : cfd_set_error(CFD_ERROR_UNSUPPORTED,
112 : "energy_solver_avx2: non-uniform dz not supported");
113 0 : return CFD_ERROR_UNSUPPORTED;
114 : }
115 : }
116 : }
117 :
118 : /* Precomputed constants for uniform-grid stencil */
119 0 : double inv_2dx = 1.0 / (2.0 * dx0);
120 0 : double inv_2dy = 1.0 / (2.0 * dy0);
121 0 : double inv_dx2 = 1.0 / (dx0 * dx0);
122 0 : double inv_dy2 = 1.0 / (dy0 * dy0);
123 :
124 : /* Branch-free 3D constants */
125 0 : size_t stride_z = (nz > 1) ? plane : 0;
126 0 : size_t k_start = (nz > 1) ? 1 : 0;
127 0 : size_t k_end = (nz > 1) ? (nz - 1) : 1;
128 0 : double inv_2dz = (nz > 1 && grid->dz) ? 1.0 / (2.0 * grid->dz[0]) : 0.0;
129 0 : double inv_dz2 = (nz > 1 && grid->dz) ? 1.0 / (grid->dz[0] * grid->dz[0]) : 0.0;
130 0 : double inv_dz = 2.0 * inv_2dz;
131 0 : const int upwind = (params->convection_scheme == NS_CONVECTION_SCHEME_UPWIND);
132 :
133 : /* Use caller's workspace or allocate internally */
134 0 : int owns_buffer = 0;
135 0 : double* T_new;
136 0 : if (T_workspace && workspace_size >= total) {
137 : T_new = T_workspace;
138 : } else {
139 0 : T_new = (double*)cfd_calloc(total, sizeof(double));
140 0 : if (!T_new) {
141 : return CFD_ERROR_NOMEM;
142 : }
143 : owns_buffer = 1;
144 : }
145 0 : memcpy(T_new, field->T, total * sizeof(double));
146 :
147 0 : const double* T = field->T;
148 0 : const double* u = field->u;
149 0 : const double* v = field->v;
150 0 : const double* w = field->w;
151 :
152 : /* Number of interior columns processed in 4-wide vector groups (i=1..nx-2) */
153 0 : size_t n_interior = nx - 2;
154 0 : size_t i_tail = 1 + (n_interior / 4) * 4;
155 :
156 : #if USE_AVX
157 : const __m256d inv_2dx_v = _mm256_set1_pd(inv_2dx);
158 : const __m256d inv_2dy_v = _mm256_set1_pd(inv_2dy);
159 : const __m256d inv_2dz_v = _mm256_set1_pd(inv_2dz);
160 : const __m256d inv_dx_v = _mm256_set1_pd(1.0 / dx0);
161 : const __m256d inv_dy_v = _mm256_set1_pd(1.0 / dy0);
162 : const __m256d inv_dz_v = _mm256_set1_pd(inv_dz);
163 : const __m256d inv_dx2_v = _mm256_set1_pd(inv_dx2);
164 : const __m256d inv_dy2_v = _mm256_set1_pd(inv_dy2);
165 : const __m256d inv_dz2_v = _mm256_set1_pd(inv_dz2);
166 : const __m256d alpha_v = _mm256_set1_pd(alpha);
167 : const __m256d dt_v = _mm256_set1_pd(dt);
168 : const __m256d two_v = _mm256_set1_pd(2.0);
169 : #endif
170 :
171 0 : for (size_t k = k_start; k < k_end; k++) {
172 0 : size_t k_offset = k * stride_z;
173 0 : int j;
174 : #ifdef _OPENMP
175 0 : #pragma omp parallel for schedule(static)
176 : #endif
177 : for (j = 1; j < (int)ny - 1; j++) {
178 : size_t row = k_offset + (size_t)j * nx;
179 :
180 : #if USE_AVX
181 : for (size_t i = 1; i < i_tail; i += 4) {
182 : size_t idx = row + i;
183 :
184 : __m256d T_c = _mm256_loadu_pd(&T[idx]);
185 : __m256d uc = _mm256_loadu_pd(&u[idx]);
186 : __m256d vc = _mm256_loadu_pd(&v[idx]);
187 : __m256d wc = _mm256_loadu_pd(&w[idx]);
188 :
189 : __m256d T_xp = _mm256_loadu_pd(&T[idx + 1]);
190 : __m256d T_xm = _mm256_loadu_pd(&T[idx - 1]);
191 : __m256d T_yp = _mm256_loadu_pd(&T[idx + nx]);
192 : __m256d T_ym = _mm256_loadu_pd(&T[idx - nx]);
193 : __m256d T_zp = _mm256_loadu_pd(&T[idx + stride_z]);
194 : __m256d T_zm = _mm256_loadu_pd(&T[idx - stride_z]);
195 :
196 : /* Advection: u*dT/dx + v*dT/dy + w*dT/dz, with central or
197 : * first-order upwind derivatives */
198 : __m256d dT_dx, dT_dy, dT_dz;
199 : if (upwind) {
200 : dT_dx = upwind_deriv_avx2(uc, T_xm, T_c, T_xp, inv_dx_v);
201 : dT_dy = upwind_deriv_avx2(vc, T_ym, T_c, T_yp, inv_dy_v);
202 : dT_dz = upwind_deriv_avx2(wc, T_zm, T_c, T_zp, inv_dz_v);
203 : } else {
204 : dT_dx = _mm256_mul_pd(_mm256_sub_pd(T_xp, T_xm), inv_2dx_v);
205 : dT_dy = _mm256_mul_pd(_mm256_sub_pd(T_yp, T_ym), inv_2dy_v);
206 : dT_dz = _mm256_mul_pd(_mm256_sub_pd(T_zp, T_zm), inv_2dz_v);
207 : }
208 : __m256d adv = _mm256_add_pd(
209 : _mm256_add_pd(_mm256_mul_pd(uc, dT_dx), _mm256_mul_pd(vc, dT_dy)),
210 : _mm256_mul_pd(wc, dT_dz));
211 :
212 : /* Diffusion: alpha * (d2T/dx2 + d2T/dy2 + d2T/dz2) */
213 : __m256d d2x = _mm256_mul_pd(
214 : _mm256_sub_pd(_mm256_add_pd(T_xp, T_xm), _mm256_mul_pd(two_v, T_c)), inv_dx2_v);
215 : __m256d d2y = _mm256_mul_pd(
216 : _mm256_sub_pd(_mm256_add_pd(T_yp, T_ym), _mm256_mul_pd(two_v, T_c)), inv_dy2_v);
217 : __m256d d2z = _mm256_mul_pd(
218 : _mm256_sub_pd(_mm256_add_pd(T_zp, T_zm), _mm256_mul_pd(two_v, T_c)), inv_dz2_v);
219 : __m256d diff = _mm256_mul_pd(alpha_v,
220 : _mm256_add_pd(_mm256_add_pd(d2x, d2y), d2z));
221 :
222 : /* Explicit Euler update (heat source added in the scalar pass) */
223 : __m256d dT = _mm256_mul_pd(dt_v, _mm256_sub_pd(diff, adv));
224 : _mm256_storeu_pd(&T_new[idx], _mm256_add_pd(T_c, dT));
225 : }
226 : #endif
227 : /* Scalar remainder (and the full interior when AVX2 is disabled) */
228 : for (size_t i = (USE_AVX ? i_tail : 1); i < nx - 1; i++) {
229 : size_t idx = row + i;
230 :
231 : double T_cs = T[idx];
232 : double dT_dx, dT_dy, dT_dz;
233 : if (upwind) {
234 : dT_dx = stencil_upwind_deriv_x(T[idx + 1], T_cs, T[idx - 1], dx0, u[idx]);
235 : dT_dy = stencil_upwind_deriv_y(T[idx + nx], T_cs, T[idx - nx], dy0, v[idx]);
236 : dT_dz = stencil_upwind_diff(T[idx + stride_z], T_cs, T[idx - stride_z],
237 : w[idx]) * inv_dz;
238 : } else {
239 : dT_dx = (T[idx + 1] - T[idx - 1]) * inv_2dx;
240 : dT_dy = (T[idx + nx] - T[idx - nx]) * inv_2dy;
241 : dT_dz = (T[idx + stride_z] - T[idx - stride_z]) * inv_2dz;
242 : }
243 : double adv = u[idx] * dT_dx + v[idx] * dT_dy + w[idx] * dT_dz;
244 :
245 : double d2x = (T[idx + 1] - 2.0 * T_cs + T[idx - 1]) * inv_dx2;
246 : double d2y = (T[idx + nx] - 2.0 * T_cs + T[idx - nx]) * inv_dy2;
247 : double d2z = (T[idx + stride_z] - 2.0 * T_cs + T[idx - stride_z]) * inv_dz2;
248 : double diff = alpha * (d2x + d2y + d2z);
249 :
250 : T_new[idx] = T_cs + dt * (diff - adv);
251 : }
252 :
253 : /* Heat source term (scalar; only when a callback is configured) */
254 : if (params->heat_source_func) {
255 : double y = grid->y[j];
256 : double z = (nz > 1 && grid->z) ? grid->z[k] : 0.0;
257 : for (size_t i = 1; i < nx - 1; i++) {
258 : size_t idx = row + i;
259 : double Q = params->heat_source_func(grid->x[i], y, z, time,
260 : params->heat_source_context);
261 : T_new[idx] += dt * Q;
262 : }
263 : }
264 : }
265 : }
266 :
267 : /* Check for NaN/Inf */
268 0 : int has_nan = 0;
269 0 : ptrdiff_t total_int = (ptrdiff_t)total;
270 0 : ptrdiff_t n;
271 : #ifdef _OPENMP
272 0 : #pragma omp parallel for reduction(| : has_nan) schedule(static)
273 : #endif
274 : for (n = 0; n < total_int; n++) {
275 : if (!isfinite(T_new[n])) {
276 : has_nan = 1;
277 : }
278 : }
279 0 : if (has_nan) {
280 0 : cfd_set_error(CFD_ERROR_DIVERGED,
281 : "NaN/Inf detected in energy_step_explicit_avx2");
282 0 : if (owns_buffer) cfd_free(T_new);
283 0 : return CFD_ERROR_DIVERGED;
284 : }
285 :
286 0 : memcpy(field->T, T_new, total * sizeof(double));
287 0 : if (owns_buffer) cfd_free(T_new);
288 :
289 : return CFD_SUCCESS;
290 : }
|