Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rocFFT and hipFFT examples (part I) #141

Merged
merged 11 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions Common/hipfft_utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// MIT License
//
// Copyright (c) 2024 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#ifndef COMMON_HIPFFT_UTILS_HPP
#define COMMON_HIPFFT_UTILS_HPP

#include <hipfft/hipfft.h>

#include <iomanip>
#include <iostream>

/// \brief Converts a \p hipfftResult_t variable to its correspondent string.
inline const char* hipfftResultToString(hipfftResult_t status)
{
switch(status)
{
case HIPFFT_SUCCESS: return "HIPFFT_SUCCESS";
case HIPFFT_INVALID_PLAN: return "HIPFFT_INVALID_PLAN";
case HIPFFT_ALLOC_FAILED: return "HIPFFT_ALLOC_FAILED";
case HIPFFT_INVALID_TYPE: return "HIPFFT_INVALID_TYPE";
case HIPFFT_INVALID_VALUE: return "HIPFFT_INVALID_VALUE";
case HIPFFT_INTERNAL_ERROR: return "HIPFFT_INTERNAL_ERROR";
case HIPFFT_EXEC_FAILED: return "HIPFFT_EXEC_FAILED";
case HIPFFT_SETUP_FAILED: return "HIPFFT_SETUP_FAILED";
case HIPFFT_INVALID_SIZE: return "HIPFFT_INVALID_SIZE";
case HIPFFT_UNALIGNED_DATA: return "HIPFFT_UNALIGNED_DATA";
case HIPFFT_INCOMPLETE_PARAMETER_LIST: return "HIPFFT_INCOMPLETE_PARAMETER_LIST";
case HIPFFT_INVALID_DEVICE: return "HIPFFT_INVALID_DEVICE";
case HIPFFT_PARSE_ERROR: return "HIPFFT_PARSE_ERROR";
case HIPFFT_NO_WORKSPACE: return "HIPFFT_NO_WORKSPACE";
case HIPFFT_NOT_IMPLEMENTED: return "HIPFFT_NOT_IMPLEMENTED";
case HIPFFT_NOT_SUPPORTED: return "HIPFFT_NOT_SUPPORTED";

// We do use default because we are not in control of these enumeration values.
// Ideally this function is something hipFFT would provide
default: return "<unknown hipfftResult_t value>";
}
}

/// \brief Checks if the provided status code is \p HIPFFT_SUCCESS and if not,
/// prints an error message to the standard error output and terminates the program
/// with an error code.
#define HIPFFT_CHECK(condition) \
{ \
const hipfftResult status = condition; \
if(status != HIPFFT_SUCCESS) \
{ \
std::cerr << "hipFFT error encountered: \"" << hipfftResultToString(status) \
<< "\" at " << __FILE__ << ':' << __LINE__ << std::endl; \
std::exit(error_exit_code); \
} \
}

/// \brief Prints an {1,2,3}-dimensional array. The last dimension (fastest-index) specified in
/// \p n will be printed horizontally.
template<class T>
void print_nd_data(const std::vector<T> data, const std::vector<int> n, const int column_width = 4)
{
// Note: we want to print the last dimension horizontally (on the x-axis)!
int size_x = n[n.size() - 1];
int size_y = n.size() > 1 ? n[n.size() - 2] : 1;
int size_z = n.size() > 2 ? n[n.size() - 3] : 1;
for(int z = 0; z < size_z; ++z)
{
for(int y = 0; y < size_y; ++y)
{
for(int x = 0; x < size_x; ++x)
{
auto index = (z * size_y + y) * size_x + x;
std::cout << std::setfill(' ') << std::setw(column_width) << data[index] << " ";
}
std::cout << "\n";
}
if(z != size_z - 1)
{
std::cout << "\n";
}
}
std::cout << std::flush;
}

#endif // COMMON_HIPFFT_UTILS_HPP
67 changes: 67 additions & 0 deletions Common/rocfft_utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// MIT License
//
// Copyright (c) 2024 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#ifndef COMMON_ROCFFT_UTILS_HPP
#define COMMON_ROCFFT_UTILS_HPP

#include "example_utils.hpp"

#include <rocfft/rocfft.h>

#include <iostream>

/// \brief Converts a \p rocfft_status variable to its correspondent string.
inline const char* rocfftStatusToString(rocfft_status status)
{
switch(status)
{
case rocfft_status_success: return "rocfft_status_success";
case rocfft_status_failure: return "rocfft_status_failure";
case rocfft_status_invalid_arg_value: return "rocfft_status_invalid_arg_value";
case rocfft_status_invalid_dimensions: return "rocfft_status_invalid_dimensions";
case rocfft_status_invalid_array_type: return "rocfft_status_invalid_array_type";
case rocfft_status_invalid_strides: return "rocfft_status_invalid_strides";
case rocfft_status_invalid_distance: return "rocfft_status_invalid_distance";
case rocfft_status_invalid_offset: return "rocfft_status_invalid_offset";
case rocfft_status_invalid_work_buffer: return "rocfft_status_invalid_work_buffer";

// We do use default because we are not in control of these enumeration values.
// Ideally this function is something rocFFT would provide
default: return "<unknown rocfft_status value>";
}
}

/// \brief Checks if the provided status code is \p rocfft_status_success and if not,
/// prints an error message to the standard error output and terminates the program
/// with an error code.
#define ROCFFT_CHECK(condition) \
{ \
const rocfft_status status = condition; \
if(status != rocfft_status_success) \
{ \
std::cerr << "rocFFT error encountered: \"" << rocfftStatusToString(status) \
<< "\" at " << __FILE__ << ':' << __LINE__ << std::endl; \
std::exit(error_exit_code); \
} \
}

#endif // COMMON_ROCFFT_UTILS_HPP
2 changes: 2 additions & 0 deletions Libraries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/${folder_bin})
if(NOT (CMAKE_SYSTEM_NAME MATCHES Windows AND "${GPU_RUNTIME}" STREQUAL "CUDA"))
add_subdirectory(hipBLAS)
add_subdirectory(hipCUB)
add_subdirectory(hipFFT)
add_subdirectory(hipSOLVER)
add_subdirectory(rocBLAS)
add_subdirectory(rocFFT)
add_subdirectory(rocPRIM)
add_subdirectory(rocRAND)
add_subdirectory(rocSOLVER)
Expand Down
2 changes: 2 additions & 0 deletions Libraries/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
LIBRARIES := \
hipBLAS \
hipCUB \
hipFFT \
hipSOLVER \
rocRAND

ifneq ($(GPU_RUNTIME), CUDA)
LIBRARIES += \
rocBLAS \
rocFFT \
rocPRIM \
rocSOLVER \
rocSPARSE \
Expand Down
52 changes: 52 additions & 0 deletions Libraries/hipFFT/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# MIT License
#
# Copyright (c) 2024 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
project(hipFFT_examples LANGUAGES CXX)

file(RELATIVE_PATH folder_bin ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/${folder_bin})

if(WIN32)
set(ROCM_ROOT
"$ENV{HIP_PATH}"
CACHE PATH
"Root directory of the ROCm installation"
)
else()
set(ROCM_ROOT
"/opt/rocm"
CACHE PATH
"Root directory of the ROCm installation"
)
endif()

list(APPEND CMAKE_PREFIX_PATH "${ROCM_ROOT}")

find_package(hipfft)
if(NOT hipfft_FOUND)
message(STATUS "hipFFT could not be found, not building hipFFT examples")
return()
endif()

add_subdirectory(plan_d2z)
add_subdirectory(plan_z2z)
35 changes: 35 additions & 0 deletions Libraries/hipFFT/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# MIT License
#
# Copyright (c) 2024 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

EXAMPLES := \
plan_d2z \
plan_z2z

all: $(EXAMPLES)

clean: TARGET=clean
clean: all

$(EXAMPLES):
$(MAKE) -C $@ $(TARGET)

.PHONY: all clean $(EXAMPLES)
56 changes: 56 additions & 0 deletions Libraries/hipFFT/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# hipFFT Examples

## Summary

The examples in this subdirectory showcase the functionality of [hipFFT](https://github.com/ROCm/hipFFT), a (Fast Fourier Transform) FFT marshalling library for rocFFT and cuFFT.

## Prerequisites

### Linux

- [CMake](https://cmake.org/download/) (at least version 3.21)
- OR GNU Make - available via the distribution's package manager
- [ROCm](https://docs.amd.com/bundle/ROCm-Installation-Guide-v5.1.3/page/Overview_of_ROCm_Installation_Methods.html) (at least version 5.x.x)
- [hipFFT](https://github.com/ROCm/hipFFT)

### Windows

- [Visual Studio](https://visualstudio.microsoft.com/) 2019 or 2022 with the "Desktop Development with C++" workload
- ROCm toolchain for Windows (No public release yet)
- The Visual Studio ROCm extension needs to be installed to build with the solution files.
- [hipFFT](https://github.com/ROCm/hipFFT)
- [CMake](https://cmake.org/download/) (optional, to build with CMake. Requires at least version 3.21)
- [Ninja](https://ninja-build.org/) (optional, to build with CMake)

## Building

### Linux

Make sure that the dependencies are installed, or use one of the [provided Dockerfiles](../../Dockerfiles/) to build and run the examples in a containerized environment.

#### Using CMake

All examples in the `hipFFT` subdirectory can either be built by a single CMake project or be built independently.

- `$ cd Libraries/hipFFT`
- `$ cmake -S . -B build` (on ROCm) or `$ cmake -S . -B build -D GPU_RUNTIME=CUDA` (on CUDA)
- `$ cmake --build build`

#### Using Make

All examples can be built by a single invocation to Make or be built independently.

- `$ cd Libraries/hipFFT`
- `$ make` (on ROCm) or `$ make GPU_RUNTIME=CUDA` (on CUDA)

### Windows

#### Visual Studio

Visual Studio solution files are available for the individual examples. To build all examples for hipFFT open the top level solution file [ROCm-Examples-VS2019.sln](../../ROCm-Examples-VS2019.sln) and filter for hipFFT.

For more detailed build instructions refer to the top level [README.md](../../README.md#visual-studio).

#### CMake

All examples in the `hipFFT` subdirectory can either be built by a single CMake project or be built independently. For build instructions refer to the top-level [README.md](../../README.md#cmake-2).
1 change: 1 addition & 0 deletions Libraries/hipFFT/plan_d2z/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hipfft_plan_d2z
Loading
Loading