Backend-portable SHAFFT C++ example using config-driven FFTND initialization.
#include <shafft/shafft.hpp>
#include <cstdio>
#include <mpi.h>
#include <vector>
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
int rank = 0;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
{
[[maybe_unused]] int rc;
constexpr int printCount = 4;
std::vector<size_t> dims = {64, 64, 32};
{},
0,
0,
MPI_COMM_WORLD);
rc = fft.
init(cfg.cStruct());
rc = fft.plan();
size_t allocSize = cfg.cStruct().allocElements;
size_t localElems = cfg.cStruct().initial.localElements;
std::vector<shafft::complexf> host(allocSize, {0.0f, 0.0f});
if (rank == 0 && localElems > 0)
host[0] = {1.0f, 0.0f};
rc = fft.setBuffers(data, work);
rc = fft.normalize();
rc = fft.getBuffers(&curData, &curWork);
std::vector<shafft::complexf> spectrum(allocSize);
if (rank == 0) {
std::printf("Spectrum[0..%d] =", printCount - 1);
for (int i = 0; i < printCount; ++i)
std::printf(" (%g,%g)", spectrum[i].real(), spectrum[i].imag());
std::printf("\n");
}
rc = fft.normalize();
rc = fft.getBuffers(&curData, &curWork);
std::vector<shafft::complexf> result(allocSize);
if (rank == 0) {
std::printf("Result[0..%d] =", printCount - 1);
for (int i = 0; i < printCount; ++i)
std::printf(" (%g,%g)", result[i].real(), result[i].imag());
std::printf("\n");
}
fft.release();
}
MPI_Finalize();
return 0;
}
RAII wrapper for N-D configuration objects.
Definition shafft.hpp:1027
N-dimensional distributed FFT plan with RAII semantics.
Definition shafft.hpp:51
int init(const std::vector< int > &commDims, const std::vector< size_t > &dimensions, FFTType type, MPI_Comm comm, TransformLayout output=TransformLayout::REDISTRIBUTED) noexcept
Initialize plan with Cartesian process grid.
int freeBuffer(complexf *buf) noexcept
Free buffer allocated with allocBuffer().
int copyToBuffer(complexf *dst, const complexf *src, size_t count) noexcept
Copy from host to SHAFFT buffer.
int allocBuffer(size_t count, complexf **buf) noexcept
Allocate buffer for the current backend.
int copyFromBuffer(complexf *dst, const complexf *src, size_t count) noexcept
Copy from SHAFFT buffer to host.
std::complex< float > complexf
Single-precision complex type (std::complex<float>).
Definition shafft_types.hpp:71
@ C2C
Single-precision complex-to-complex (float).
@ REDISTRIBUTED
Keep post-forward redistributed layout (no final redistribution).
@ MINIMIZE_NDA
Minimize distributed axes.
@ BACKWARD
Backward/inverse transform (frequency to time domain).
@ FORWARD
Forward transform (time to frequency domain).