LCOV - code coverage report
Current view: top level - solvers/linear/simd - linear_solver_simd_dispatch.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 96.2 % 53 51
Test Date: 2026-10-05 04:47:18 Functions: 90.9 % 11 10

            Line data    Source code
       1              : /**
       2              :  * @file linear_solver_simd_dispatch.c
       3              :  * @brief SIMD Poisson solver dispatcher with runtime CPU detection
       4              :  *
       5              :  * This file provides unified SIMD solvers that select the appropriate
       6              :  * architecture-specific implementation at RUNTIME:
       7              :  * - AVX2 on x86-64 (detected via CPUID)
       8              :  * - NEON on ARM64 (always available on ARM64)
       9              :  *
      10              :  * The actual implementations remain in separate files:
      11              :  * - avx2/linear_solver_jacobi_avx2.c
      12              :  * - avx2/linear_solver_redblack_avx2.c
      13              :  * - avx2/linear_solver_cg_avx2.c
      14              :  * - neon/linear_solver_jacobi_neon.c
      15              :  * - neon/linear_solver_redblack_neon.c
      16              :  * - neon/linear_solver_cg_neon.c
      17              :  *
      18              :  * This design is identical to the boundary conditions SIMD dispatcher.
      19              :  */
      20              : 
      21              : #include "../linear_solver_internal.h"
      22              : #include "cfd/core/cpu_features.h"
      23              : #include "cfd/core/logging.h"
      24              : 
      25              : #include <stdio.h>
      26              : 
      27              : /* ============================================================================
      28              :  * LOGGING
      29              :  * ============================================================================ */
      30              : 
      31              : /**
      32              :  * Record why a SIMD factory is about to return NULL.
      33              :  *
      34              :  * The error state, not just the log: a caller that gets NULL from
      35              :  * poisson_solver_create has to be able to say what went wrong, and a DEBUG line
      36              :  * is not available to it. Without this the refusal travels as a bare
      37              :  * CFD_ERROR_UNSUPPORTED with cfd_get_last_error() reading (null) -- which is
      38              :  * what the AVX2 projection reported on a CFD_ENABLE_AVX2=OFF build.
      39              :  *
      40              :  * Callers must not fall back to scalar on NULL; that is the cross-backend
      41              :  * fallback the library forbids. They return CFD_ERROR_UNSUPPORTED and let the
      42              :  * caller pick another backend.
      43              :  */
      44           35 : static void log_no_simd_available(const char* solver_type) {
      45           35 :     char msg[160];
      46           35 :     snprintf(msg, sizeof(msg),
      47              :              "No SIMD %s solver in this build (detected arch: %s); the SIMD "
      48              :              "Poisson backends need both AVX2/NEON and OpenMP compiled in "
      49              :              "(-DCFD_ENABLE_AVX2=ON), or request another backend",
      50              :              solver_type, cfd_get_simd_name());
      51           35 :     cfd_set_error(CFD_ERROR_UNSUPPORTED, msg);
      52           35 :     CFD_LOG_DEBUG("simd", "%s", msg);
      53           35 : }
      54              : 
      55              : /* ============================================================================
      56              :  * FORWARD DECLARATIONS FOR ARCHITECTURE-SPECIFIC IMPLEMENTATIONS
      57              :  *
      58              :  * These are defined in avx2/ and neon/ subdirectories.
      59              :  * If a backend is not compiled (e.g., NEON on x86), these functions return NULL.
      60              :  * ============================================================================ */
      61              : 
      62              : /* AVX2 implementations (x86-64) */
      63              : extern poisson_solver_t* create_jacobi_avx2_solver(void);
      64              : extern poisson_solver_t* create_redblack_avx2_solver(void);
      65              : extern poisson_solver_t* create_cg_avx2_solver(void);
      66              : extern poisson_solver_t* create_bicgstab_avx2_solver(void);
      67              : extern poisson_solver_t* create_sor_avx2_solver(void);
      68              : extern poisson_solver_t* create_gmres_avx2_solver(void);
      69              : 
      70              : /* NEON implementations (ARM64) */
      71              : extern poisson_solver_t* create_jacobi_neon_solver(void);
      72              : extern poisson_solver_t* create_redblack_neon_solver(void);
      73              : extern poisson_solver_t* create_cg_neon_solver(void);
      74              : extern poisson_solver_t* create_bicgstab_neon_solver(void);
      75              : extern poisson_solver_t* create_sor_neon_solver(void);
      76              : extern poisson_solver_t* create_gmres_neon_solver(void);
      77              : 
      78              : /* ============================================================================
      79              :  * SIMD BACKEND AVAILABILITY (Runtime + Compile-time detection)
      80              :  *
      81              :  * This checks BOTH:
      82              :  * 1. Compile-time: Was code compiled with SIMD support (-mavx2 or ARM64)?
      83              :  * 2. Compile-time: Was OpenMP enabled at build time?
      84              :  * 3. Runtime: Does CPU support the SIMD instructions?
      85              :  *
      86              :  * All must be true for SIMD to be available.
      87              :  * ============================================================================ */
      88              : 
      89              : /* Check if AVX2 implementation was compiled
      90              :  * CFD_HAS_AVX2 is set by CMake when -DCFD_ENABLE_AVX2=ON.
      91              :  * This works consistently across all compilers (GCC, Clang, MSVC).
      92              :  */
      93              : #if defined(CFD_HAS_AVX2) && defined(CFD_ENABLE_OPENMP)
      94              : #define HAS_AVX2_IMPL 1
      95              : #else
      96              : #define HAS_AVX2_IMPL 0
      97              : #endif
      98              : 
      99              : /* Check if NEON implementation was compiled */
     100              : #if (defined(__aarch64__) || defined(_M_ARM64) || defined(__ARM_NEON) || defined(__ARM_NEON__)) && defined(CFD_ENABLE_OPENMP)
     101              : #define HAS_NEON_IMPL 1
     102              : #else
     103              : #define HAS_NEON_IMPL 0
     104              : #endif
     105              : 
     106           31 : bool poisson_solver_simd_backend_available(void) {
     107           31 :     cfd_simd_arch_t arch = cfd_detect_simd_arch();
     108              : 
     109              :     /* Check AVX2: compiled with AVX2 AND runtime CPU supports AVX2 */
     110           31 :     if (HAS_AVX2_IMPL && arch == CFD_SIMD_AVX2) {
     111              :         return true;
     112              :     }
     113              : 
     114              :     /* Check NEON: compiled with NEON AND runtime CPU supports NEON */
     115           31 :     if (HAS_NEON_IMPL && arch == CFD_SIMD_NEON) {
     116              :         return true;
     117              :     }
     118              : 
     119           31 :     return false;
     120              : }
     121              : 
     122            1 : const char* poisson_solver_simd_get_arch_name(void) {
     123            1 :     return cfd_get_simd_name();
     124              : }
     125              : 
     126              : /* Public API wrappers */
     127            0 : bool poisson_solver_simd_available(void) {
     128            0 :     return poisson_solver_simd_backend_available();
     129              : }
     130              : 
     131            1 : const char* poisson_solver_get_simd_arch_name(void) {
     132            1 :     return poisson_solver_simd_get_arch_name();
     133              : }
     134              : 
     135              : /* ============================================================================
     136              :  * JACOBI SIMD DISPATCHER
     137              :  * ============================================================================ */
     138              : 
     139            3 : poisson_solver_t* create_jacobi_simd_solver(void) {
     140            3 :     cfd_simd_arch_t arch = cfd_detect_simd_arch();
     141              : 
     142              :     /* Check compile-time AND runtime availability before calling factory */
     143            3 :     if (HAS_AVX2_IMPL && arch == CFD_SIMD_AVX2) {
     144              :         return create_jacobi_avx2_solver();
     145              :     }
     146              : 
     147            3 :     if (HAS_NEON_IMPL && arch == CFD_SIMD_NEON) {
     148              :         return create_jacobi_neon_solver();
     149              :     }
     150              : 
     151              :     /* No SIMD backend available - report error and return NULL (no fallback) */
     152            3 :     log_no_simd_available("Jacobi");
     153            3 :     return NULL;
     154              : }
     155              : 
     156              : /* ============================================================================
     157              :  * RED-BLACK SOR SIMD DISPATCHER
     158              :  * ============================================================================ */
     159              : 
     160            3 : poisson_solver_t* create_redblack_simd_solver(void) {
     161            3 :     cfd_simd_arch_t arch = cfd_detect_simd_arch();
     162              : 
     163              :     /* Check compile-time AND runtime availability before calling factory */
     164            3 :     if (HAS_AVX2_IMPL && arch == CFD_SIMD_AVX2) {
     165              :         return create_redblack_avx2_solver();
     166              :     }
     167              : 
     168            3 :     if (HAS_NEON_IMPL && arch == CFD_SIMD_NEON) {
     169              :         return create_redblack_neon_solver();
     170              :     }
     171              : 
     172              :     /* No SIMD backend available - report error and return NULL (no fallback) */
     173            3 :     log_no_simd_available("Red-Black");
     174            3 :     return NULL;
     175              : }
     176              : 
     177              : /* ============================================================================
     178              :  * CONJUGATE GRADIENT SIMD DISPATCHER
     179              :  * ============================================================================ */
     180              : 
     181           10 : poisson_solver_t* create_cg_simd_solver(void) {
     182           10 :     cfd_simd_arch_t arch = cfd_detect_simd_arch();
     183              : 
     184              :     /* Check compile-time AND runtime availability before calling factory */
     185           10 :     if (HAS_AVX2_IMPL && arch == CFD_SIMD_AVX2) {
     186              :         return create_cg_avx2_solver();
     187              :     }
     188              : 
     189           10 :     if (HAS_NEON_IMPL && arch == CFD_SIMD_NEON) {
     190              :         return create_cg_neon_solver();
     191              :     }
     192              : 
     193              :     /* No SIMD backend available - report error and return NULL (no fallback) */
     194           10 :     log_no_simd_available("CG");
     195           10 :     return NULL;
     196              : }
     197              : 
     198              : /* ============================================================================
     199              :  * BICGSTAB SIMD DISPATCHER
     200              :  * ============================================================================ */
     201              : 
     202            6 : poisson_solver_t* create_bicgstab_simd_solver(void) {
     203            6 :     cfd_simd_arch_t arch = cfd_detect_simd_arch();
     204              : 
     205              :     /* Check compile-time AND runtime availability before calling factory */
     206            6 :     if (HAS_AVX2_IMPL && arch == CFD_SIMD_AVX2) {
     207              :         return create_bicgstab_avx2_solver();
     208              :     }
     209              : 
     210            6 :     if (HAS_NEON_IMPL && arch == CFD_SIMD_NEON) {
     211              :         return create_bicgstab_neon_solver();
     212              :     }
     213              : 
     214              :     /* No SIMD backend available - report error and return NULL (no fallback) */
     215            6 :     log_no_simd_available("BiCGSTAB");
     216            6 :     return NULL;
     217              : }
     218              : 
     219              : /* ============================================================================
     220              :  * SOR SIMD DISPATCHER (Block SOR)
     221              :  * ============================================================================ */
     222              : 
     223            7 : poisson_solver_t* create_sor_simd_solver(void) {
     224            7 :     cfd_simd_arch_t arch = cfd_detect_simd_arch();
     225              : 
     226              :     /* Check compile-time AND runtime availability before calling factory */
     227            7 :     if (HAS_AVX2_IMPL && arch == CFD_SIMD_AVX2) {
     228              :         return create_sor_avx2_solver();
     229              :     }
     230              : 
     231            7 :     if (HAS_NEON_IMPL && arch == CFD_SIMD_NEON) {
     232              :         return create_sor_neon_solver();
     233              :     }
     234              : 
     235              :     /* No SIMD backend available - report error and return NULL (no fallback) */
     236            7 :     log_no_simd_available("SOR");
     237            7 :     return NULL;
     238              : }
     239              : 
     240              : /* ============================================================================
     241              :  * GMRES SIMD DISPATCHER
     242              :  * ============================================================================ */
     243              : 
     244            6 : poisson_solver_t* create_gmres_simd_solver(void) {
     245            6 :     cfd_simd_arch_t arch = cfd_detect_simd_arch();
     246              : 
     247              :     /* Check compile-time AND runtime availability before calling factory */
     248            6 :     if (HAS_AVX2_IMPL && arch == CFD_SIMD_AVX2) {
     249              :         return create_gmres_avx2_solver();
     250              :     }
     251              : 
     252            6 :     if (HAS_NEON_IMPL && arch == CFD_SIMD_NEON) {
     253              :         return create_gmres_neon_solver();
     254              :     }
     255              : 
     256              :     /* No SIMD backend available - report error and return NULL (no fallback) */
     257            6 :     log_no_simd_available("GMRES");
     258            6 :     return NULL;
     259              : }
        

Generated by: LCOV version 2.0-1