SHAFFT 1.1.0-alpha
A Scalable High-dimensional Accelerated FFT Library
Loading...
Searching...
No Matches
example_FFT1D.cpp

Backend-portable SHAFFT C++ example using FFT1D.

#include <shafft/shafft.hpp>
#include <cstdio>
#include <mpi.h>
#include <vector>
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
// MPI setup
int rank = 0;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
[[maybe_unused]] int rc;
constexpr size_t globalN = 256;
constexpr int printCount = 4;
// Get configuration
size_t localN, localStart;
rc = shafft::configuration1D(globalN, localN, localStart, shafft::FFTType::C2C, MPI_COMM_WORLD);
// Create and plan FFT
rc = fft.init(globalN, localN, localStart, shafft::FFTType::C2C, MPI_COMM_WORLD);
rc = fft.plan();
size_t allocSize = fft.allocSize();
size_t localSize = fft.localSize();
// Allocate buffers
shafft::complexf *data = nullptr, *work = nullptr;
rc = shafft::allocBuffer(allocSize, &data);
rc = shafft::allocBuffer(allocSize, &work);
// Initialize: delta function at origin (index 0)
std::vector<shafft::complexf> host(allocSize, {0.0f, 0.0f});
if (localStart == 0 && localSize > 0)
host[0] = {1.0f, 0.0f};
rc = shafft::copyToBuffer(data, host.data(), allocSize);
rc = fft.setBuffers(data, work);
// Forward FFT
rc = fft.normalize();
// Retrieve spectrum
std::vector<shafft::complexf> spectrum(allocSize);
rc = shafft::copyFromBuffer(spectrum.data(), work, 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");
}
// Backward FFT
rc = fft.setBuffers(work, data);
rc = fft.normalize();
// Retrieve result
shafft::complexf *curData, *curWork;
rc = fft.getBuffers(&curData, &curWork);
std::vector<shafft::complexf> result(allocSize);
rc = shafft::copyFromBuffer(result.data(), curWork, 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");
}
// Cleanup
fft.release();
rc = shafft::freeBuffer(data);
rc = shafft::freeBuffer(work);
MPI_Finalize();
return 0;
}
1-dimensional distributed FFT plan with RAII semantics.
Definition shafft.hpp:341
int execute(FFTDirection direction) noexcept override
Execute the transform.
int setBuffers(complexf *data, complexf *work) noexcept
Attach data and work buffers.
int init(size_t globalN, size_t localN, size_t localStart, FFTType precision, MPI_Comm comm) noexcept
Initialize plan.
size_t localSize() const noexcept
Get local element count (before padding).
int getBuffers(complexf **data, complexf **work) noexcept
Retrieve current buffer pointers.
int normalize() noexcept override
Apply symmetric normalization (1/sqrt(N) per transform).
int plan() noexcept override
Create backend FFT plans.
void release() noexcept override
Release all internal resources.
size_t allocSize() const noexcept override
Get required buffer size in complex elements.
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 configuration1D(size_t globalN, size_t &localN, size_t &localStart, FFTType precision, MPI_Comm comm)
Compute local layout for 1D distributed FFT.
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).
@ BACKWARD
Backward/inverse transform (frequency to time domain).
@ FORWARD
Forward transform (time to frequency domain).