MCPcopy Create free account
hub / github.com/NVIDIA/cuda-samples / test0

Function test0

cpp/5_Domain_Specific/convolutionFFT2D/main.cpp:83–216  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

81float getRand(void) { return (float)(rand() % 16); }
82
83bool test0(void)
84{
85 float *h_Data, *h_Kernel, *h_ResultCPU, *h_ResultGPU;
86
87 float *d_Data, *d_PaddedData, *d_Kernel, *d_PaddedKernel;
88
89 fComplex *d_DataSpectrum, *d_KernelSpectrum;
90
91 cufftHandle fftPlanFwd, fftPlanInv;
92
93 bool bRetVal;
94 StopWatchInterface *hTimer = NULL;
95 sdkCreateTimer(&hTimer);
96
97 printf("Testing built-in R2C / C2R FFT-based convolution\n");
98 const int kernelH = 7;
99 const int kernelW = 6;
100 const int kernelY = 3;
101 const int kernelX = 4;
102 const int dataH = 2000;
103 const int dataW = 2000;
104 const int fftH = snapTransformSize(dataH + kernelH - 1);
105 const int fftW = snapTransformSize(dataW + kernelW - 1);
106
107 printf("...allocating memory\n");
108 h_Data = (float *)malloc(dataH * dataW * sizeof(float));
109 h_Kernel = (float *)malloc(kernelH * kernelW * sizeof(float));
110 h_ResultCPU = (float *)malloc(dataH * dataW * sizeof(float));
111 h_ResultGPU = (float *)malloc(fftH * fftW * sizeof(float));
112
113 checkCudaErrors(cudaMalloc((void **)&d_Data, dataH * dataW * sizeof(float)));
114 checkCudaErrors(cudaMalloc((void **)&d_Kernel, kernelH * kernelW * sizeof(float)));
115
116 checkCudaErrors(cudaMalloc((void **)&d_PaddedData, fftH * fftW * sizeof(float)));
117 checkCudaErrors(cudaMalloc((void **)&d_PaddedKernel, fftH * fftW * sizeof(float)));
118
119 checkCudaErrors(cudaMalloc((void **)&d_DataSpectrum, fftH * (fftW / 2 + 1) * sizeof(fComplex)));
120 checkCudaErrors(cudaMalloc((void **)&d_KernelSpectrum, fftH * (fftW / 2 + 1) * sizeof(fComplex)));
121 checkCudaErrors(cudaMemset(d_KernelSpectrum, 0, fftH * (fftW / 2 + 1) * sizeof(fComplex)));
122
123 printf("...generating random input data\n");
124 srand(2010);
125
126 for (int i = 0; i < dataH * dataW; i++) {
127 h_Data[i] = getRand();
128 }
129
130 for (int i = 0; i < kernelH * kernelW; i++) {
131 h_Kernel[i] = getRand();
132 }
133
134 printf("...creating R2C & C2R FFT plans for %i x %i\n", fftH, fftW);
135 checkCudaErrors(cufftPlan2d(&fftPlanFwd, fftH, fftW, CUFFT_R2C));
136 checkCudaErrors(cufftPlan2d(&fftPlanInv, fftH, fftW, CUFFT_C2R));
137
138 printf("...uploading to GPU and padding convolution kernel and input data\n");
139 checkCudaErrors(cudaMemcpy(d_Kernel, h_Kernel, kernelH * kernelW * sizeof(float), cudaMemcpyHostToDevice));
140 checkCudaErrors(cudaMemcpy(d_Data, h_Data, dataH * dataW * sizeof(float), cudaMemcpyHostToDevice));

Callers 1

mainFunction · 0.85

Calls 10

sdkCreateTimerFunction · 0.85
snapTransformSizeFunction · 0.85
getRandFunction · 0.85
sdkResetTimerFunction · 0.85
sdkStartTimerFunction · 0.85
sdkStopTimerFunction · 0.85
sdkGetTimerValueFunction · 0.85
sqrtFunction · 0.85
sdkDeleteTimerFunction · 0.85

Tested by

no test coverage detected