Skip to content

Commit

Permalink
Merge pull request #146 from psteinb/add_selfcontained_sample
Browse files Browse the repository at this point in the history
create stub folder that contains a minimal example on how to use rocfft
  • Loading branch information
feizheng10 authored Feb 12, 2019
2 parents e31d954 + 741d07f commit 7146962
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/samples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.0)

# project name
PROJECT(rocfft_samples CXX)

find_package(hip)
find_package(rocfft)

add_executable(complex_forward_1d complex_forward_1d.cpp)
target_link_libraries( complex_forward_1d PRIVATE roc::rocfft hip::hip_hcc )
target_include_directories( complex_forward_1d PRIVATE ${rocfft_INCLUDE_DIR} )
set_target_properties( complex_forward_1d PROPERTIES
CXX_STANDARD_REQUIRED ON)
set_target_properties( complex_forward_1d PROPERTIES
CXX_STANDARD 14)

if( CMAKE_CXX_COMPILER MATCHES ".*/hcc$" )
# Remove following when hcc is fixed; hcc emits following spurious warning ROCm v1.6.1
# "clang-5.0: warning: argument unused during compilation: '-isystem /opt/rocm/include'"
target_compile_options( complex_forward_1d PRIVATE -Wno-unused-command-line-argument )
set_target_properties( complex_forward_1d PROPERTIES
CXX_EXTENSIONS OFF)

# foreach( target ${AMDGPU_TARGETS} )
# target_link_libraries( rocfft-selftest PRIVATE --amdgpu-target=${target} )
# endforeach( )
endif( )
10 changes: 10 additions & 0 deletions docs/samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Samples to demo using rocfft

## `complex_forward_1d`

``` bash
$ mkdir build
$ cmake ..
$ make
```

65 changes: 65 additions & 0 deletions docs/samples/complex_forward_1d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <stdio.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <hip/hip_runtime_api.h>
#include "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();

for( size_t i = 0;i < N;++i){
if(std::abs( cx[i].x - y[i].x ) < 1e-5){
std::cerr << "Error - unexpected matching element: observed " << y[i].x << ", expected " << cx[i].x << std::endl;
return 1;
}
}

std::cout << "complex forward 1d - OK!" << std::endl;

return 0;
}

0 comments on commit 7146962

Please sign in to comment.