Created
October 30, 2019 19:13
-
-
Save antholzer/d746dc946261400f2612015e6337325b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include "hip/hip_runtime_api.h" | |
#include "hip/hip_vector_types.h" | |
#include "rocfft.h" | |
int main() | |
{ | |
// rocFFT gpu compute | |
// ======================================== | |
rocfft_setup(); | |
size_t N = 16; | |
size_t N2 = 9; | |
size_t Nbytes = N * sizeof(float); | |
size_t Nbytes2 = N2 * sizeof(float2); | |
// Create HIP device buffer | |
float *x; | |
hipMalloc(&x, Nbytes); | |
float *y; | |
hipMalloc(&y, Nbytes2); | |
// Initialize data | |
std::vector<float> cx(N); | |
for (size_t i = 0; i < N; i++) | |
{ | |
cx[i] = 1; | |
} | |
std::cout << std::endl; | |
for (size_t i = 0; i < N; i++) | |
{ | |
std::cout << cx[i] << ", "; | |
} | |
std::cout << std::endl; | |
// Copy data to device | |
hipMemcpy(x, cx.data(), Nbytes, hipMemcpyHostToDevice); | |
// Create rocFFT plan | |
rocfft_plan plan = NULL; | |
size_t length = N; | |
rocfft_plan_create(&plan, rocfft_placement_notinplace, rocfft_transform_type_real_forward, rocfft_precision_single, 1, &length, 1, NULL); | |
// Execute plan | |
rocfft_execute(plan, (void**) &x, (void **) &y, NULL); | |
// Wait for execution to finish | |
hipDeviceSynchronize(); | |
// Destroy plan | |
rocfft_plan_destroy(plan); | |
// Copy result back to host | |
std::vector<float2> cy(N2); | |
hipMemcpy(cy.data(), y, Nbytes2, hipMemcpyDeviceToHost); | |
hipMemcpy(cx.data(), x, Nbytes, hipMemcpyDeviceToHost); | |
// Print results | |
// for (size_t i = 0; i < N2; i++) | |
// { | |
// std::cout << cy[i].x << ", " << cy[i].y << std::endl; | |
// } | |
// std::cout << std::endl; | |
for (size_t i = 0; i < N; i++) | |
{ | |
std::cout << cx[i] << ", "; | |
} | |
std::cout << std::endl; | |
// Free device buffer | |
hipFree(x); | |
hipFree(y); | |
rocfft_cleanup(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment