NCCL and cuFFTMp#
cuFFTMp can use NCCL instead of NVSHMEM to move data between GPUs.
To use NCCL, give CUFFT_COMM_NCCL and a pointer to an NCCL communicator (ncclComm_t) to the plan or reshape creation function.
For a comparison of the two backends, refer to Communication backends.
Requirements#
NCCL 2.30.7 or later, for CUDA 13.
One GPU for each process. Each process in the NCCL communicator must use a different GPU.
A blocking NCCL communicator. This is the default (
blocking = 1inncclConfig_t). If the communicator is not blocking, plan and reshape creation returnCUFFT_NOT_SUPPORTED.
The standalone cuFFTMp packages (archive, Linux packages and Python wheel) do not include NCCL. Install NCCL separately. For the installation procedures, refer to the NCCL documentation.
The NCCL backend does not use NVSHMEM.
An application that uses only CUFFT_COMM_NCCL does not need NVSHMEM at compile time, at link time or at run time.
How cuFFTMp loads NCCL#
libcufftMp.so does not link to NCCL.
cuFFTMp loads libnccl.so.2 at run time, when the application creates the first plan or reshape with CUFFT_COMM_NCCL:
If the process already contains
libnccl.so.2, cuFFTMp uses this library. This is the usual case, because the application links to NCCL to create the communicator.If not, the dynamic loader searches for
libnccl.so.2, for example in the directories ofLD_LIBRARY_PATH.
cuFFTMp and the application must use the same NCCL library.
Link the application to the shared NCCL library (-lnccl). Do not link it to the static NCCL library.
If cuFFTMp cannot load NCCL, or if the NCCL version is earlier than 2.30.7, plan and reshape creation return CUFFT_INTERNAL_ERROR.
For more information, refer to Communication backends.
Create the NCCL communicator#
The application creates the NCCL communicator before it creates the cuFFTMp plan. The example that follows uses MPI to send the NCCL unique ID to all processes.
#include <cufftMp.h>
#include <nccl.h>
#include <mpi.h>
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Select one GPU for each process before you create the NCCL communicator.
// local_rank is the rank of the process in its node.
MPI_Comm node_comm;
MPI_Comm_split_type(MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, rank, MPI_INFO_NULL, &node_comm);
int local_rank;
MPI_Comm_rank(node_comm, &local_rank);
cudaSetDevice(local_rank);
// Rank 0 gets the NCCL unique ID, and MPI sends it to all ranks
ncclUniqueId id;
if (rank == 0) ncclGetUniqueId(&id);
MPI_Bcast(&id, sizeof(id), MPI_BYTE, 0, MPI_COMM_WORLD);
ncclComm_t comm;
ncclCommInitRank(&comm, size, id, rank);
// Create the plan with the NCCL communicator
cufftHandle plan;
cufftCreate(&plan);
size_t workspace;
cufftMpMakePlan3d(plan, nx, ny, nz, CUFFT_C2C, &comm, CUFFT_COMM_NCCL, &workspace);
// Allocate memory, copy data, execute the plan, etc.
// ...
// Destroy the plan before the communicator
cufftDestroy(plan);
ncclCommDestroy(comm);
MPI_Finalize();
The code does these steps:
Initialize MPI and get the rank and the number of processes.
Select a GPU with
cudaSetDevice. Do this before you callncclCommInitRank, becausencclCommInitRankuses the current device.Create the NCCL communicator. Rank 0 gets a unique ID,
MPI_Bcastsends it to all processes, and all processes callncclCommInitRank.Create the plan with
cufftMpMakePlan3d,CUFFT_COMM_NCCLand a pointer to the communicator.Use the plan as with NVSHMEM. Refer to Usage with built-in slab data decompositions.
Destroy the plan, then destroy the communicator.
cuFFTMp does not use MPI with the NCCL backend. The application uses MPI only to send the NCCL unique ID. You can use a different method to send the ID.
With the built-in slab data distributions, the rank of a process in the NCCL communicator identifies the slab that the process owns.
The c2c_no_descriptors_nccl sample shows the complete procedure.
Communicator ownership and lifetime#
The application owns the NCCL communicator. cuFFTMp does not destroy it.
Obey these rules:
Keep the communicator and the pointer to it valid until you destroy the plan or the reshape handle.
Before you call
ncclCommDestroy, free all the descriptors withcufftXtFree, then destroy all the plans and reshape handles.The application can also use the communicator for its own NCCL operations. Do not run these operations at the same time as cuFFTMp operations. For example, use the same stream, or synchronize the streams.
Do not call cuFFTMp functions between
ncclGroupStartandncclGroupEnd.On all processes, call the cuFFTMp functions and the NCCL functions on the communicator in the same order.
All the plans and reshape handles that exist at the same time in a process must use the same NCCL communicator.
If you give a different communicator, plan and reshape creation return CUFFT_INVALID_VALUE.
Memory#
With NCCL, the application does not allocate memory from a symmetric heap.
Data buffers#
The data buffers that you give to
cufftXtExec,cufftExecC2Cand the othercufftExec*functions can be any device memory, for example fromcudaMalloc.The input, output and workspace buffers that you give to
cufftMpExecReshapeAsynccan be any device memory.A transform is in place. The data buffer of a transform must hold the local input data and the local output data of the process. With the built-in slab distributions, allocate the larger of these two sizes. This size can be different on each process. With a custom data distribution, allocate the same size on all processes: the largest local input size or local output size of all the processes. With a custom data distribution, cuFFTMp can also use the data buffer as temporary memory. This temporary data can be larger than the local data of the process.
The reshape input buffer has the size of the local input data. The reshape output buffer has the size of the local output data. These sizes can be different on each process. The reshape workspace has the size that
cufftMpGetReshapeSizereturns.It is not necessary to synchronize the processes before an execution.
Descriptors#
cufftXtMalloc allocates the descriptor memory.
This memory can be NCCL symmetric memory.
All processes must call cufftXtMalloc together, because the allocation is collective.
If the memory is NCCL symmetric memory, cufftXtFree waits until the GPU completes all its work, then releases the memory.
With CUFFT_COPY_DEVICE_TO_DEVICE, the destination of cufftXtMemcpy must be a descriptor from cufftXtMalloc.
Work area#
By default, cuFFTMp allocates the work area of the plan. To supply your own work area, do these steps on all processes:
Call
cufftSetAutoAllocation(plan, 0)before you make the plan.Make the plan, for example with
cufftMpMakePlan3d.Allocate device memory of
workSizebytes, for example withcudaMalloc.Call
cufftSetWorkArea(plan, work_area).
All processes must call cufftSetAutoAllocation with the same value.
If the values are different, plan creation can return CUFFT_INVALID_VALUE on all processes, or plan creation or a later cuFFTMp call can wait forever.
A call to cufftSetWorkArea before you make the plan has the same effect as cufftSetAutoAllocation(plan, 0).
A plan that you make after cufftSetAutoAllocation(plan, 0) can need a larger work area than a plan with its own work area, and it can be slower.
Refer to Memory requirements.
If the plan allocates its own work area and you call cufftSetWorkArea after you make the plan, give memory from cuFFTMp, such as a cufftXtMalloc buffer.
With other memory, cufftSetWorkArea can return CUFFT_INVALID_VALUE. The plan then keeps its own work area.
A cufftXtMalloc buffer has desc->descriptor->size[0] bytes.
This size can be smaller than workSize.
Use the buffer as a work area only if it has at least workSize bytes.
cuFFTMp does not examine the size.
The r2c_c2r_shared_scratch_nccl sample shows two plans that share one work area. For the work area sizes, refer to Memory requirements.
Standalone reshape#
With NCCL, the standalone reshape API always needs a workspace.
Thus cufftMpGetReshapeSize returns a workspace size that is not zero.
The environment variable CUFFT_RESHAPE_USE_PACKING has no effect with NCCL.
The reshape_nccl sample shows a reshape with NCCL.