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

Backend-portable SHAFFT C example.

#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);
int dims[3] = {64, 64, 32};
int commDims[3] = {0, 0, 0};
size_t subsize[3], offset[3];
int nda = 0, commSize, rc;
dims,
commDims,
&nda,
subsize,
offset,
&commSize,
0,
MPI_COMM_WORLD);
void* plan = NULL;
rc = shafftNDCreate(&plan);
plan, 3, commDims, dims, SHAFFT_C2C, MPI_COMM_WORLD, SHAFFT_LAYOUT_REDISTRIBUTED);
size_t elemCount = 0;
rc = shafftGetAllocSize(plan, &elemCount);
size_t localElems = subsize[0] * subsize[1] * subsize[2];
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;
}
int shafftGetAllocSize(void *plan, size_t *localAllocSize)
Get required buffer size in complex elements.
Definition shafftc.cpp:281
int shafftConfigurationND(int ndim, int *size, shafft_t precision, int *commDims, int *nda, size_t *subsize, size_t *offset, int *commSize, shafft_decomposition_strategy_t strategy, size_t memLimit, MPI_Comm cComm)
Compute local layout and process grid for N-D distributed FFT.
Definition shafftc.cpp:104
int shafftCopyToBufferF(void *dst, const void *src, size_t count)
Copy single-precision data from host to SHAFFT buffer.
Definition shafftc.cpp:397
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 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 shafftNDInit(void *planPtr, int ndim, int commDims[], int dimensions[], shafft_t precision, MPI_Comm cComm, shafft_transform_layout_t output_policy)
Initialize N-D plan from process grid.
Definition shafftc.cpp:74
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