upgpkg: wordpress 6.2.1-1
[ArchLinux/community.git] / rocfft / repos / community-x86_64 / test.cpp
blobc1b0149915c203f8c9974e721d45690b1830cb0d
1 #include <rocfft/rocfft.h>
2 #include <hip/hip_runtime.h>
3 #include <hip/hip_vector_types.h>
4 #include <vector>
5 #include <numeric>
6 #include <cmath>
7 #include <iostream>
9 int main()
11 size_t size = 1024 * 1024;
13 rocfft_setup();
15 float2 *x;
16 hipMalloc((void**)&x, sizeof *x * size);
19 std::vector<float2> xin(size);
20 for(auto &xx: xin){
21 xx.x = 1.0f;
22 xx.y = 0.0f;
24 hipMemcpy(x, xin.data(), sizeof *x * size, hipMemcpyHostToDevice);
26 rocfft_plan plan = nullptr;
27 size_t len = size;
28 rocfft_plan_create(&plan, rocfft_placement_inplace,
29 rocfft_transform_type_complex_forward, rocfft_precision_single,
30 1, &len, 1, nullptr);
31 size_t work_size = 0;
32 rocfft_plan_get_work_buffer_size(plan, &work_size);
33 void *work;
34 rocfft_execution_info info = nullptr;
35 if(work_size){
36 rocfft_execution_info_create(&info);
37 hipMalloc((void**)&work, work_size);
38 rocfft_execution_info_set_work_buffer(info, work, work_size);
40 rocfft_execute(plan, (void**)&x, nullptr, info);
42 std::vector<float2> xout(size);
43 hipMemcpy(xout.data(), x, sizeof *x * size, hipMemcpyDeviceToHost);
45 std::vector<float2> xref(size);
46 for(auto &xx: xref){
47 xx.x = 0.0f;
48 xx.y = 0.0f;
50 xref[0].x = 1.0f * size;
52 float tol = 0.001f;
53 for(size_t i = 0; i < size; i++){
54 if(std::abs(xref[i].x - xout[i].x) + std::abs(xref[i].y - xout[i].y) > tol){
55 std::cout << "Element mismatch at index " << i << "\n";
56 std::cout << "Expected: " << xref[i].x << " " << xref[i].y << "\n";
57 std::cout << "Actual : " << xout[i].x << " " << xout[i].y << "\n";
58 return 1;
62 std::cout << "TESTS PASSED!" << std::endl;
64 hipFree(x);
65 rocfft_plan_destroy(plan);
66 rocfft_cleanup();