LCOV - code coverage report
Current view: top level - solvers/linear/omp - linear_solver_jacobi_omp.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 57 57
Test Date: 2026-10-05 04:47:18 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /**
       2              :  * @file linear_solver_jacobi_omp.c
       3              :  * @brief Jacobi iteration solver - OpenMP parallelized implementation
       4              :  *
       5              :  * Same algorithm as scalar Jacobi (double-buffered: reads p_old, writes p_new),
       6              :  * with the interior update loop parallelized across OpenMP threads. Jacobi is
       7              :  * embarrassingly parallel — each interior update reads only the previous
       8              :  * iterate, so there is no cross-cell dependency within a sweep.
       9              :  */
      10              : 
      11              : #include "../linear_solver_internal.h"
      12              : 
      13              : #include "cfd/core/indexing.h"
      14              : #include "cfd/core/memory.h"
      15              : 
      16              : #include <string.h>
      17              : 
      18              : #ifdef CFD_ENABLE_OPENMP
      19              : 
      20              : #include <omp.h>
      21              : 
      22              : /* ============================================================================
      23              :  * JACOBI CONTEXT
      24              :  * ============================================================================ */
      25              : 
      26              : typedef struct {
      27              :     double dx2;        /* dx^2 */
      28              :     double dy2;        /* dy^2 */
      29              :     double inv_dz2;    /* 1/dz^2 (0 for 2D) */
      30              :     double inv_factor; /* 1 / (2 * (1/dx^2 + 1/dy^2 + inv_dz2)) */
      31              :     size_t stride_z;   /* nx*ny for 3D, 0 for 2D */
      32              :     size_t k_start;    /* first interior k index */
      33              :     size_t k_end;      /* one-past-last interior k index */
      34              :     size_t n;          /* nx*ny*nz, validated at init */
      35              :     int initialized;
      36              : } jacobi_omp_context_t;
      37              : 
      38              : /* ============================================================================
      39              :  * JACOBI OMP IMPLEMENTATION
      40              :  * ============================================================================ */
      41              : 
      42            8 : static cfd_status_t jacobi_omp_init(
      43              :     poisson_solver_t* solver,
      44              :     size_t nx, size_t ny, size_t nz,
      45              :     double dx, double dy, double dz,
      46              :     const poisson_solver_params_t* params)
      47              : {
      48            8 :     (void)params;
      49              : 
      50              :     /* The sweep loops over int bounds and copies nx*ny*nz doubles; oversized
      51              :      * grids would empty the loops or wrap the copy size, so reject them. */
      52            8 :     size_t n = 0;
      53            8 :     cfd_status_t size_status = poisson_solver_validate_grid_size(nx, ny, nz, 1, &n);
      54            8 :     if (size_status != CFD_SUCCESS) {
      55              :         return size_status;
      56              :     }
      57              : 
      58            5 :     jacobi_omp_context_t* ctx = (jacobi_omp_context_t*)cfd_calloc(1, sizeof(jacobi_omp_context_t));
      59            5 :     if (!ctx) {
      60              :         return CFD_ERROR_NOMEM;
      61              :     }
      62              : 
      63            5 :     ctx->dx2 = dx * dx;
      64            5 :     ctx->dy2 = dy * dy;
      65            5 :     ctx->inv_dz2 = poisson_solver_compute_inv_dz2(dz);
      66            5 :     ctx->n = n;
      67            5 :     poisson_solver_compute_3d_bounds(nz, nx, ny,
      68              :         &ctx->stride_z, &ctx->k_start, &ctx->k_end);
      69              : 
      70            5 :     double factor = 2.0 * (1.0 / ctx->dx2 + 1.0 / ctx->dy2 + ctx->inv_dz2);
      71            5 :     ctx->inv_factor = 1.0 / factor;
      72            5 :     ctx->initialized = 1;
      73              : 
      74            5 :     solver->context = ctx;
      75            5 :     return CFD_SUCCESS;
      76              : }
      77              : 
      78            9 : static void jacobi_omp_destroy(poisson_solver_t* solver) {
      79            9 :     if (solver && solver->context) {
      80            5 :         cfd_free(solver->context);
      81            5 :         solver->context = NULL;
      82              :     }
      83            9 : }
      84              : 
      85         3225 : static cfd_status_t jacobi_omp_iterate(
      86              :     poisson_solver_t* solver,
      87              :     double* x,
      88              :     double* x_temp,
      89              :     const double* rhs,
      90              :     double* residual)
      91              : {
      92         3225 :     if (!x_temp) {
      93              :         return CFD_ERROR_INVALID;  /* Jacobi requires temp buffer */
      94              :     }
      95              : 
      96         3225 :     jacobi_omp_context_t* ctx = (jacobi_omp_context_t*)solver->context;
      97         3225 :     size_t nx = solver->nx;
      98         3225 :     size_t ny = solver->ny;
      99         3225 :     double dx2 = ctx->dx2;
     100         3225 :     double dy2 = ctx->dy2;
     101         3225 :     double inv_dz2 = ctx->inv_dz2;
     102         3225 :     double inv_factor = ctx->inv_factor;
     103         3225 :     size_t stride_z = ctx->stride_z;
     104              : 
     105         3225 :     const double* p_old = x;
     106         3225 :     double* p_new = x_temp;
     107              : 
     108              :     /* Jacobi update: reads from p_old, writes to p_new (no in-place dependency) */
     109         6450 :     for (size_t k = ctx->k_start; k < ctx->k_end; k++) {
     110         3225 :         int j;
     111         3225 : #pragma omp parallel for schedule(static)
     112              :         for (j = 1; j < (int)ny - 1; j++) {
     113              :             for (int i = 1; i < (int)nx - 1; i++) {
     114              :                 size_t idx = k * stride_z + IDX_2D((size_t)i, (size_t)j, nx);
     115              : 
     116              :                 double p_result = -(rhs[idx]
     117              :                     - (p_old[idx + 1] + p_old[idx - 1]) / dx2
     118              :                     - (p_old[idx + nx] + p_old[idx - nx]) / dy2
     119              :                     - (p_old[idx + stride_z] + p_old[idx - stride_z]) * inv_dz2
     120              :                     ) * inv_factor;
     121              : 
     122              :                 p_new[idx] = p_result;
     123              :             }
     124              :         }
     125              :     }
     126              : 
     127              :     /* Copy result back to x */
     128         3225 :     memcpy(x, x_temp, ctx->n * sizeof(double));
     129              : 
     130              :     /* Apply boundary conditions */
     131         3225 :     poisson_solver_apply_bc(solver, x);
     132              : 
     133              :     /* Compute residual if requested */
     134         3225 :     if (residual) {
     135         3225 :         *residual = poisson_solver_compute_residual(solver, x, rhs);
     136              :     }
     137              : 
     138              :     return CFD_SUCCESS;
     139              : }
     140              : 
     141              : /* ============================================================================
     142              :  * FACTORY FUNCTION
     143              :  * ============================================================================ */
     144              : 
     145            9 : poisson_solver_t* create_jacobi_omp_solver(void) {
     146            9 :     poisson_solver_t* solver = (poisson_solver_t*)cfd_calloc(1, sizeof(poisson_solver_t));
     147            9 :     if (!solver) {
     148              :         return NULL;
     149              :     }
     150              : 
     151            9 :     solver->name = POISSON_SOLVER_TYPE_JACOBI_OMP;
     152            9 :     solver->description = "Jacobi iteration (OpenMP)";
     153            9 :     solver->method = POISSON_METHOD_JACOBI;
     154            9 :     solver->backend = POISSON_BACKEND_OMP;
     155            9 :     solver->params = poisson_solver_params_default();
     156            9 :     solver->params.max_iterations = 2000;  /* Jacobi needs more iterations */
     157            9 :     solver->params.check_interval = 10;    /* Check less frequently for speed */
     158              : 
     159            9 :     solver->init = jacobi_omp_init;
     160            9 :     solver->destroy = jacobi_omp_destroy;
     161            9 :     solver->solve = NULL;  /* Use common solve loop */
     162            9 :     solver->iterate = jacobi_omp_iterate;
     163            9 :     solver->apply_bc = NULL;  /* Use default Neumann */
     164              : 
     165            9 :     return solver;
     166              : }
     167              : 
     168              : #endif /* CFD_ENABLE_OPENMP */
        

Generated by: LCOV version 2.0-1