我已经在CUDA 3中成功使用了CUFFT库,但是相同的代码不能在CUDA 4中运行。使用CUDA 4时,我会在FFT执行时收到运行时错误(CUDA_INVALID_VALUE)。这是一个前向的实际到复杂的1D变换。我在CUFFT文档中看到的唯一一个在CUDA 3和CUDA 4之间发生变化的是增加了FFTW兼容模式。我将其设置为本机模式。
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
int Nfft, Navg, iAvg, N, n1, n2, Npsd, size[2];
float *hReal;
float *pPxx;
float *dReal;
float *dAvg, *dSum, *dWindow;
float U;
long lAvg, lSum, lWindow;
cufftHandle hPlan;
cufftComplex *dComplex;
cufftResult result;
int nBlocks, blockSize;
if (nrhs == 12)
{
Nfft = mxGetScalar(prhs[0]);
blockSize = mxGetScalar(prhs[1]);
Navg = mxGetScalar(prhs[2]);
iAvg = mxGetScalar(prhs[3]);
U = mxGetScalar(prhs[4]);
n1 = mxGetScalar(prhs[5]);
n2 = mxGetScalar(prhs[6]);
hPlan = (cufftHandle)mxGetScalar(prhs[7]);
hReal = (float *)mxGetData(prhs[8]);
lWindow = (long)mxGetScalar(prhs[9]);
lAvg = (long)mxGetScalar(prhs[10]);
lSum = (long)mxGetScalar(prhs[11]);
}
else
mexErrMsgTxt("fftcuda: Function requires 12 inputs");
// pointers to GPU arrays
dWindow = (float *)lWindow;
dAvg = (float *)lAvg;
dSum = (float *)lSum;
// size of output array
N = Nfft/2 + 1;
Npsd = n2 - n1 + 1;
size[0] = 1;
size[1] = Npsd;
/* Allocate working arrays on device */
cudaMalloc( (void**)&dReal,sizeof(float)*Nfft);
cudaMalloc( (void**)&dComplex,sizeof(cufftComplex)*N);
/* Copy input array to the device */
cudaMemcpy( (void*)dReal,(void*)hReal,sizeof(float)*Nfft,cudaMemcpyHostToDevice);
// setup for cuda functions
nBlocks = (int)(Nfft/blockSize);
/* multiply input array by window */
cudaMult <<< nBlocks, blockSize >>> (dReal,dWindow,dReal,Nfft);
/* Execute FFT on device */
result = cufftExecR2C(hPlan, (cufftReal *)dReal, dComplex);
if (result == CUFFT_SETUP_FAILED)
mexErrMsgTxt("CUFFT library failed to initialize.");
else if (result == CUFFT_INVALID_PLAN )
mexErrMsgTxt("The hPlan parameter is not a valid handle.");
else if (result == CUFFT_INVALID_VALUE )
mexErrMsgTxt("The idata or odata parameter is not valid.");
else if (result == CUFFT_EXEC_FAILED )
mexErrMsgTxt("CUFFT failed to execute the transform on GPU.");
// setup for cuda functions
nBlocks = (int)(Npsd/blockSize) + (Npsd%blockSize);
/* Compute absolute value */
cudaAbs <<< nBlocks, blockSize >>> (&dComplex[n1-1],dReal,Npsd);
if (nlhs != 1)
mexErrMsgTxt("fftcuda: Function requires 1 output: float pPxx");
plhs[0]=mxCreateNumericArray(2,size,mxSINGLE_CLASS,mxREAL);
pPxx = (float *)mxGetData(plhs[0]);
/* Copy result back to host */
cudaMemcpy( (void*)pPxx, (void*)dReal, sizeof(float)*Npsd,cudaMemcpyDeviceToHost);
/* free working arrays from gpu memory */
cudaFree((void*)dReal);
cudaFree((void*)dComplex);
return;
}
答案 0 :(得分:1)
CUFFT库不仅仅有一个版本。由于CUFFT是CUDA工具包的一部分,因此每个新版本的CUDA工具包都会发布该库的更新版本。
如果您尝试使用较新版本的CUDA来使用较旧版本的库,那几乎可以肯定是您的问题。只需使用相同版本的CUFFT作为您的CUDA工具包,它应该可以工作。