Skip to content

Example cpp code calling rocFFT routine with gnu compiler

tingxingdong edited this page Apr 18, 2018 · 7 revisions
#include <stdio.h>
#include <vector>
#include <iostream>
#include <math.h>
#include <hip/hip_runtime_api.h>
#include "rocfft/rocfft.h"


int main()
{
    // For size N <= 4096
    const size_t N = 16;
       
    std::vector<float2> cx(N);

    for (size_t i = 0; i < N; i++)
    {
        cx[i].x = (i%3) - (i%7);
        cx[i].y = 0;
    }

    // rocfft gpu compute
    // ========================================

    rocfft_setup();

    size_t Nbytes = N * sizeof(float2);

    // Create HIP device object.
    float2 *x;
    hipMalloc(&x, Nbytes);

    //  Copy data to device
    hipMemcpy(x, &cx[0], Nbytes, hipMemcpyHostToDevice);

    // Create plan
    rocfft_plan plan = NULL;
    size_t length = N;
    rocfft_plan_create(&plan, rocfft_placement_inplace, rocfft_transform_type_complex_forward, rocfft_precision_single, 1, &length, 1, NULL);

    // Execute plan
    rocfft_execute(plan, (void**)&x, NULL, NULL);

    // Destroy plan
    rocfft_plan_destroy(plan);

    // Copy result back to host
    std::vector<float2> y(N);
    hipMemcpy(&y[0], x, Nbytes, hipMemcpyDeviceToHost);

    hipFree(x);

    rocfft_cleanup();

    return 0;
}



=================================
## Use standard g++ Compiler:

Add  "-D__HIP_PLATFORM_HCC__ -I/opt/rocm/include" into your compiler flag.
Tell g++  your librocfft.so & librocfft-device.so (notice you need both *.so, mine are in /opt/rocm/lib64 ) and hip_hcc path (By default in /opt/rocm/lib)
Tell your rocfft.h header (mine in /opt/rocm/include)

The whole command is 

"g++ -std=c++11 rocfft_example.cpp -o rocfft_example -D__HIP_PLATFORM_HCC__ -L/opt/rocm/lib -lhip_hcc -L/opt/rocm/lib64 -lrocfft -lrocfft-device -I/opt/rocm/include"
Clone this wiki locally