|
| 1 | +#include <sycl/sycl.hpp> |
| 2 | +#include <dpct/dpct.hpp> |
| 3 | +#include <iostream> |
| 4 | +#include <dpct/fft_utils.hpp> |
| 5 | + |
| 6 | +#include <cmath> |
| 7 | + |
| 8 | +// Function to compare two floating-point numbers with a tolerance |
| 9 | +int almostEqual(float a, float b, float tolerance) { |
| 10 | + return fabs(a - b) < tolerance; |
| 11 | +} |
| 12 | + |
| 13 | +// Function to compare two cufftComplex variables |
| 14 | +int compareCufftComplex(sycl::float2 a, sycl::float2 b, float tolerance) { |
| 15 | + return almostEqual(a.x(), b.x(), tolerance) && |
| 16 | + almostEqual(a.y(), b.y(), tolerance); |
| 17 | +} |
| 18 | + |
| 19 | +std::string strigifyCufftComplex(sycl::float2 a) { |
| 20 | + return std::string("(" + std::to_string(a.x()) + ", " + |
| 21 | + std::to_string(a.y()) + ")"); |
| 22 | +} |
| 23 | + |
| 24 | +int main() { |
| 25 | + dpct::device_ext &dev_ct1 = dpct::get_current_device(); |
| 26 | + sycl::queue &q_ct1 = dev_ct1.in_order_queue(); |
| 27 | + const int n = 8; // Size of the input array |
| 28 | + |
| 29 | + // Allocate memory on the host for input and output arrays |
| 30 | + sycl::float2 *h_input = (sycl::float2 *)malloc(sizeof(sycl::float2) * n); |
| 31 | + sycl::float2 *h_output = (sycl::float2 *)malloc(sizeof(sycl::float2) * n); |
| 32 | + |
| 33 | + // Initialize the input array with random values |
| 34 | + for (int i = 0; i < n; ++i) { |
| 35 | + h_input[i].x() = static_cast<float>(rand()) / RAND_MAX; |
| 36 | + h_input[i].y() = static_cast<float>(rand()) / RAND_MAX; |
| 37 | + } |
| 38 | + |
| 39 | + // Allocate memory on the host for reference output array |
| 40 | + sycl::float2 *h_ref_output = |
| 41 | + (sycl::float2 *)malloc(sizeof(sycl::float2) * n); |
| 42 | + |
| 43 | + // Initialize h_ref_output with expected output |
| 44 | + h_ref_output[0] = sycl::float2(4.94234f, 4.77104f); |
| 45 | + h_ref_output[1] = sycl::float2(0.914293f, -0.261793f); |
| 46 | + h_ref_output[2] = sycl::float2(-0.415583f, 0.264357f); |
| 47 | + h_ref_output[3] = sycl::float2(0.241085f, 0.382871f); |
| 48 | + h_ref_output[4] = sycl::float2(-0.153554f, -1.45243f); |
| 49 | + h_ref_output[5] = sycl::float2(-0.421167f, -1.15111f); |
| 50 | + h_ref_output[6] = sycl::float2(0.0986443f, 0.210444f); |
| 51 | + h_ref_output[7] = sycl::float2(1.51544f, 0.391681f); |
| 52 | + |
| 53 | + // Allocate memory on the device (GPU) |
| 54 | + sycl::float2 *d_input, *d_output; |
| 55 | + d_input = sycl::malloc_device<sycl::float2>(n, q_ct1); |
| 56 | + d_output = sycl::malloc_device<sycl::float2>(n, q_ct1); |
| 57 | + |
| 58 | + // Copy input data from host to device |
| 59 | + q_ct1.memcpy(d_input, h_input, sizeof(sycl::float2) * n).wait(); |
| 60 | + |
| 61 | + // Create a cuFFT plan |
| 62 | + dpct::fft::fft_engine_ptr plan; |
| 63 | + plan = dpct::fft::fft_engine::create( |
| 64 | + &q_ct1, n, dpct::fft::fft_type::complex_float_to_complex_float, 1); |
| 65 | + |
| 66 | + // Perform forward FFT |
| 67 | + plan->compute<sycl::float2, sycl::float2>( |
| 68 | + d_input, d_output, dpct::fft::fft_direction::forward); |
| 69 | + |
| 70 | + // Copy output data from device to host |
| 71 | + q_ct1.memcpy(h_output, d_output, sizeof(sycl::float2) * n).wait(); |
| 72 | + |
| 73 | + // Verify the result |
| 74 | + bool failed = false; |
| 75 | + |
| 76 | + float tolerance = 5e-6f; |
| 77 | + for (int i = 0; i < n; ++i) { |
| 78 | + if (!compareCufftComplex(h_output[i], h_ref_output[i], tolerance)) { |
| 79 | + std::cout << "Failed: at index - " << i; |
| 80 | + std::cout << ": " << strigifyCufftComplex(h_output[i]); |
| 81 | + std::cout << " != " << strigifyCufftComplex(h_ref_output[i]); |
| 82 | + std::cout << std::endl; |
| 83 | + failed = true; |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if(!failed) std::cout << "Verification successful" << std::endl; |
| 89 | + |
| 90 | + // Destroy the cuFFT plan and free allocated memory |
| 91 | + dpct::fft::fft_engine::destroy(plan); |
| 92 | + sycl::free(d_input, q_ct1); |
| 93 | + sycl::free(d_output, q_ct1); |
| 94 | + free(h_input); |
| 95 | + free(h_output); |
| 96 | + |
| 97 | + if(failed) |
| 98 | + return 1; |
| 99 | + |
| 100 | + return 0; |
| 101 | +} |
0 commit comments