What exactly is a cufftHandle

i find cufftHandle is typedef int, how a int can do the fft? i think it should be a struct at least so that can store some data to do fft, like the size、direction and dimension of the transformation and so on…when i assign a int value to the handle before cufftExec, the fft will be error sometimes. so what exactly is the cufftHandle? any help is expected thanks

cufft can use that int to look up the associated data.

cufftHandle is an example of a handle, which is an abstract reference to an object or a resource. Other examples you may know are stderr, stdout, stdin. The important thing to keep in mind about handles is that they are opaque, which means programmers do not need to know, and should not rely on, any specific way they may be implemented.

When an API creates an object, it hands the programmer a handle as a way to refer to the object in future API calls. When the programmer later calls the API to perform a particular action, the handle identifies to the API which object or resource should be acted upon.

In terms of implementation, in the simplest cases handles are often integers representing an index into an internal table, less frequently they are pointers that point directly to an entry in some internal data structure (a list, for example). But more elaborate schemes for generating handles are certainly possible, for example to add some form of validity checking.

1 Like

Here is the thing, i also want use a handle-method to program, but i do not know how to actually relate handle to the associated data.
otherwise, if i make it a class, the associated data to be member property, the function to be the Member methods. it can also do the fft. compared to class, what is the advantages of handle-method. best wishes! thanks

When you use a class with data members and member functions, the user code needs to be recompiled whenever the class is modified. And with a class, you expose internal details to the user. You avoid this by using a handle. (similar to the PIMPL idiom)
(By the way, cudaStream_t and cudaEvent_t are also handles, pointers to an internal struct. typedef __device_builtin__ struct CUstream_st *cudaStream_t; )

A simple handle implementation may look like the following (written in browser).


using MyHandle = int;

void createHandle(MyHandle* handle);
void destroyHandle(MyHandle handle);

#include <map>
#include <mutex>
#include "myapi.h"

struct MyInternalData{


int globalHandleCounter = 0;
std::mutex globalMutex;
std::map<MyHandle, MyInternalData> globalMap;

void createHandle(MyHandle* handle){
    std::lock_guard<std::mutex> lg(globalMutex);
    *handle = globalHandleCounter;
    globalMap.insert(std::make_pair(*handle, MyInternalData{}));

void destroyHandle(MyHandle handle){
     std::lock_guard<std::mutex> lg(globalMutex);

1 Like