amrex icon indicating copy to clipboard operation
amrex copied to clipboard

Better SIMD Complex Support

Open ax3l opened this issue 4 months ago • 2 comments

At the moment, compiling amrex::Complex<> as a ad-hoc SIMD fails on MSVC with

 │ │ %PREFIX%\Library\include\AMReX_GpuComplex.H(28,8): error: requested alignment is not a power of 2
 │ │    28 | struct alignas(2*sizeof(T)) GpuComplex
 │ │       |        ^       ~~~~~~~~~~~

It is possible this is actually an alignment requirement in general (not only MSVC) and magic happens.

We might need to wait for https://github.com/mattkretz/vir-simd/issues/42 / C++26 with a proper implementation, which will likely not be 2xSIMD but SIMDx2 layouted.

User code: this is currently used in ImpactX elements like Multipole.H where the complex math is simple. For more involved complex math, we cannot yet vectorize.

ax3l avatar Aug 14 '25 03:08 ax3l

The alignment requirement is needed for GPUs to get coalesced memory accesses into an array of GPU complexes. If T is a SIMD type, these should not be stored in an array, so you could for example check if the size of T is not 4 or 8 and then not have a stricter alignment.

AlexanderSinn avatar Aug 14 '25 09:08 AlexanderSinn

Thanks! Yes, I think the idea is to ultimately have an array of fixed-width SIMD of complex<double/float>, so the fundamental layout in memory stays the same and we just load a few adjacent complex at once.

ax3l avatar Aug 18 '25 05:08 ax3l