Guest User

clFFT 2D in-place transform test

a guest
Aug 4th, 2022
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <clFFT.h>
  3. #include <CL/cl.h>
  4.  
  5. int main(){
  6.     // Test data parameters
  7.     size_t xRes = 8192;
  8.     size_t yRes = 8192;
  9.  
  10.     // OpenCL parameters
  11.     size_t clLengths[2] = {xRes, yRes};
  12.     size_t clRealStride[2]   = {1, xRes + 2};
  13.     size_t clComplexStride[2] = {1, static_cast<size_t>(floor(static_cast<float>(xRes) / 2) + 1)};
  14.     size_t n_pixels          = yRes * (xRes + 2);
  15.     size_t buffer_size       = n_pixels * sizeof(float);
  16.  
  17.     std::cout << "Buffer size: " << buffer_size / (1024*1024) << " MB" << std::endl;
  18.  
  19.     // Initialize OpenCL
  20.     cl_platform_id platform = 0;
  21.     cl_device_id device = 0;
  22.     clGetPlatformIDs(1, &platform, NULL);
  23.     clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
  24.     cl_context_properties props[3] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0};
  25.     cl_context ctx = clCreateContext(props, 1, &device, NULL, NULL, NULL);
  26.     cl_command_queue queue = clCreateCommandQueue(ctx, device, 0, NULL);
  27.     cl_mem buffer = clCreateBuffer(ctx, CL_MEM_READ_WRITE, buffer_size, NULL, NULL);
  28.  
  29.     char platform_name[128];
  30.     clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(platform_name), platform_name,0);
  31.     std::cout << "Platform: " << platform_name << std::endl;
  32.  
  33.     // Setup clFFT
  34.     clfftSetupData fftSetup;
  35.     clfftInitSetupData(&fftSetup);
  36.     clfftSetup(&fftSetup);
  37.  
  38.     // Create plan for in-place real to hermitian forward FFT
  39.     clfftPlanHandle planHandle;
  40.     clfftCreateDefaultPlan(&planHandle, ctx, CLFFT_2D, clLengths);
  41.     clfftSetPlanPrecision(planHandle, CLFFT_SINGLE);
  42.     clfftSetLayout(planHandle, CLFFT_REAL, CLFFT_HERMITIAN_INTERLEAVED);
  43.     clfftSetPlanInStride(planHandle, CLFFT_2D, clRealStride);
  44.     clfftSetPlanOutStride(planHandle, CLFFT_2D, clComplexStride);
  45.     clfftSetResultLocation(planHandle, CLFFT_INPLACE);
  46.     clfftSetPlanScale(planHandle, CLFFT_FORWARD, 1.0);
  47.     clfftBakePlan(planHandle, 1, &queue, NULL, NULL);
  48.  
  49.     //Wait until user has regisered VRAM utilization
  50.     std::cout << "Press enter to run transform: ";
  51.     std::cin.get();
  52.  
  53.     // Perform forward FFT
  54.     clfftEnqueueTransform(planHandle, CLFFT_FORWARD, 1, &queue, 0, NULL, NULL, &buffer, NULL, NULL);
  55.     clFinish(queue);
  56.  
  57.     //Wait until user has regisered VRAM utilization
  58.     std::cout << "Press enter to clean up: ";
  59.     std::cin.get();
  60.  
  61.     // Clean up
  62.     clReleaseMemObject(buffer);
  63.     clfftDestroyPlan(&planHandle);
  64.     clfftTeardown();
  65.     clReleaseCommandQueue(queue);
  66.     clReleaseContext(ctx);
  67.  
  68.     //Wait until user has regisered VRAM utilization
  69.     std::cout << "Press enter to end: ";
  70.     std::cin.get();
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment