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

Backend-portable SHAFFT C example using config-driven FFTND initialization.

#include <shafft/shafft.h>
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
int rank = 0;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
size_t globalShape[3] = {64, 64, 32};
int rc = 0;
shafft_nd_config_t cfg = {0};
rc = shafftConfigNDInit(&cfg,
3,
globalShape,
NULL,
0,
0,
MPI_COMM_WORLD);
void* plan = NULL;
rc = shafftNDCreate(&plan);
rc = shafftNDInitFromConfig(plan, &cfg);
size_t elemCount = cfg.allocElements;
size_t localElems = cfg.initial.localElements;
void*data = NULL, *work = NULL;
rc = shafftAllocBufferF(elemCount, &data);
rc = shafftAllocBufferF(elemCount, &work);
float* host = (float*)calloc(elemCount * 2, sizeof(float));
if (rank == 0 && localElems > 0)
host[0] = 1.0f;
rc = shafftCopyToBufferF(data, host, elemCount);
rc = shafftSetBuffers(plan, data, work);
rc = shafftPlan(plan);
rc = shafftNormalize(plan);
rc = shafftGetBuffers(plan, &data, &work);
float* spectrum = (float*)malloc(elemCount * 2 * sizeof(float));
rc = shafftCopyFromBufferF(spectrum, data, elemCount);
if (rank == 0) {
printf("Spectrum[0..3] = (%g,%g) (%g,%g) (%g,%g) (%g,%g)\n",
spectrum[0],
spectrum[1],
spectrum[2],
spectrum[3],
spectrum[4],
spectrum[5],
spectrum[6],
spectrum[7]);
}
rc = shafftNormalize(plan);
rc = shafftGetBuffers(plan, &data, &work);
float* result = (float*)malloc(elemCount * 2 * sizeof(float));
rc = shafftCopyFromBufferF(result, data, elemCount);
if (rank == 0) {
printf("Result[0..3] = (%g,%g) (%g,%g) (%g,%g) (%g,%g)\n",
result[0],
result[1],
result[2],
result[3],
result[4],
result[5],
result[6],
result[7]);
}
free(result);
free(spectrum);
free(host);
rc = shafftFreeBufferF(data);
rc = shafftFreeBufferF(work);
rc = shafftDestroy(&plan);
MPI_Finalize();
return 0;
}
void shafftConfigNDRelease(shafft_nd_config_t *cfg)
Release internal resources of an N-D config object.
int shafftCopyToBufferF(void *dst, const void *src, size_t count)
Copy single-precision data from host to SHAFFT buffer.
Definition shafftc.cpp:397
int shafftNDInitFromConfig(void *planPtr, shafft_nd_config_t *cfg)
Initialize an N-D plan from a resolved config object.
Definition shafftc.cpp:434
int shafftCopyFromBufferF(void *dst, const void *src, size_t count)
Copy single-precision data from SHAFFT buffer to host.
Definition shafftc.cpp:413
int shafftNormalize(void *planPtr)
Apply normalization to data buffer.
Definition shafftc.cpp:333
int shafftGetBuffers(void *planPtr, void **data, void **work)
Retrieve current buffer pointers.
Definition shafftc.cpp:250
int shafftConfigNDInit(shafft_nd_config_t *cfg, int ndim, const size_t *globalShape, shafft_t precision, const int *commDims, int hintNda, shafft_decomposition_strategy_t strategy, shafft_transform_layout_t outputPolicy, size_t memLimit, MPI_Comm comm)
Initialize and resolve an N-D configuration object.
int shafftDestroy(void **planPtr)
Release plan resources.
Definition shafftc.cpp:226
int shafftAllocBufferF(size_t count, void **buf)
Allocate single-precision complex buffer.
Definition shafftc.cpp:357
int shafftFreeBufferF(void *buf)
Free single-precision buffer from shafftAllocBufferF().
Definition shafftc.cpp:381
int shafftPlan(void *planPtr)
Create backend FFT plans.
Definition shafftc.cpp:159
int shafftExecute(void *planPtr, shafft_direction_t direction)
Execute FFT transform.
Definition shafftc.cpp:317
int shafftNDCreate(void **outPlan)
Allocate uninitialized N-D plan object.
Definition shafftc.cpp:60
int shafftSetBuffers(void *planPtr, void *data, void *work)
Attach data and work buffers to a plan.
Definition shafftc.cpp:240
@ SHAFFT_LAYOUT_REDISTRIBUTED
Definition shafft.h:92
@ SHAFFT_BACKWARD
Definition shafft.h:63
@ SHAFFT_FORWARD
Definition shafft.h:62
@ SHAFFT_C2C
Definition shafft.h:52
@ SHAFFT_MINIMIZE_NDA
Definition shafft.h:200
N-D configuration object (C POD struct).
Definition shafft.h:244
size_t allocElements
Definition shafft.h:264
shafft_nd_layout_t initial
Definition shafft.h:267
size_t localElements
Definition shafft.h:227