OpenSWMM Engine  6.0.0-alpha.1
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.1)
Loading...
Searching...
No Matches
SIMD.hpp File Reference

SIMD abstraction layer for vectorized arithmetic operations. More...

#include <cstddef>
#include <cmath>
#include <algorithm>
#include <cassert>
Include dependency graph for SIMD.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  openswmm
 
namespace  openswmm::simd
 

Macros

#define OPENSWMM_RESTRICT   __restrict__
 
#define OPENSWMM_SIMD_SCALAR   1
 
#define OPENSWMM_SIMD_WIDTH   1
 Scalar fallback.
 

Functions

void openswmm::simd::add (const double *OPENSWMM_RESTRICT a, const double *OPENSWMM_RESTRICT b, double *OPENSWMM_RESTRICT dst, std::size_t n) noexcept
 Element-wise addition: dst[i] = a[i] + b[i].
 
void openswmm::simd::multiply (const double *OPENSWMM_RESTRICT a, const double *OPENSWMM_RESTRICT b, double *OPENSWMM_RESTRICT dst, std::size_t n) noexcept
 Element-wise multiplication: dst[i] = a[i] * b[i].
 
double openswmm::simd::min (const double *a, std::size_t n) noexcept
 Find the minimum value in an array.
 
double openswmm::simd::max (const double *a, std::size_t n) noexcept
 Find the maximum value in an array.
 
void openswmm::simd::clamp (double *a, double lo, double hi, std::size_t n) noexcept
 Clamp all elements of an array to [lo, hi].
 
double openswmm::simd::dot (const double *OPENSWMM_RESTRICT a, const double *OPENSWMM_RESTRICT b, std::size_t n) noexcept
 Dot product of two arrays: sum(a[i] * b[i]).
 
constexpr std::size_t openswmm::simd::lane_width () noexcept
 Returns the SIMD lane width (doubles per register on this platform).
 

Detailed Description

SIMD abstraction layer for vectorized arithmetic operations.

Provides a platform-neutral interface over:

  • x86_64: AVX2 (256-bit, 4 doubles per register)
  • arm64: NEON (128-bit, 2 doubles per register)
  • Other: Scalar fallback (auto-vectorized by the compiler)

The goal is to express numerical loops in a way that the compiler can vectorize, with explicit SIMD intrinsics as hints for the most performance-critical paths.

Usage philosophy

Do NOT pepper the solver code with raw intrinsics. Instead:

  1. Write the algorithm using the SIMD helpers in this file.
  2. Let the compiler auto-vectorize as much as possible.
  3. Profile; only use explicit intrinsics for hot loops that the compiler fails to vectorize.
Note
The scalar fallback is always available. If SIMD intrinsics are unavailable at compile time, the scalar path is used automatically.
See also
tests/unit/test_simd_math.cpp
tests/benchmarks/bench_hydraulics.cpp
Author
Caleb Buahin caleb.nosp@m..bua.nosp@m.hin@g.nosp@m.mail.nosp@m..com
License\n MIT License

Macro Definition Documentation

◆ OPENSWMM_RESTRICT

#define OPENSWMM_RESTRICT   __restrict__

◆ OPENSWMM_SIMD_SCALAR

#define OPENSWMM_SIMD_SCALAR   1

◆ OPENSWMM_SIMD_WIDTH

#define OPENSWMM_SIMD_WIDTH   1

Scalar fallback.