Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
36 changes: 36 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,42 @@ jobs:
call "C:\Program Files (x86)\Intel\oneAPI\setvars.bat" >nul 2>&1
ctest --test-dir build-sycl --output-on-failure -R test_derivatives_sycl

hip-tests:
runs-on: ubuntu-22.04
container:
image: rocm/dev-ubuntu-24.04:7.2
name: Test HIP on ubuntu-22.04 (ROCm container)
steps:
- uses: actions/checkout@v3

- name: install build tools
run: |
apt-get update
apt-get install -y cmake build-essential git

- name: record ROCm inventory
run: |
hipcc --version || true
rocminfo || true

- name: configure HIP build
run: |
cmake -B build-hip -S . \
-DSPHERICART_BUILD_TESTS=ON \
-DSPHERICART_ENABLE_HIP=ON \
-DCMAKE_PREFIX_PATH=/opt/rocm

- name: build HIP tests
run: cmake --build build-hip --parallel

# GitHub-hosted runners have no AMD GPU: the test compiles here and
# skips its device checks; full execution runs on self-hosted HIP
# runners (e.g. the gfx1151 qualification host used by symmetrix-xl)
- name: run HIP ctest
# matches test_derivatives_hip and test_hip_async; both skip without a
# device, example_hip (which does not skip) stays out
run: ctest --test-dir build-hip --output-on-failure -R "test_.*hip"

# check that we can build Python wheels on any Python version
python-build:
runs-on: ubuntu-24.04
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ The support for GPU offload could be controled with the following CMake variable
- `-DSPHERICART_ENABLE_CUDA=ON/OFF`: build with CUDA support also set `CUDA_HOME` environement variable.
- `-DSPHERICART_ENABLE_SYCL=ON/OFF`: build with SYCL support, configure tool will search for `sycl/sycl.h` header.
- `-DSPHERICART_SYCL_DEVICE=all/cpu/gpu`: target architecute for SYCL support, check which devices are available with `sycl-ls`, for `all` (default) is possible to control at execution with `export ONEAPI_DEVICE_SELECTOR=opencl:gpu` or `export ONEAPI_DEVICE_SELECTOR=opencl:cpu`.
- `-DSPHERICART_ENABLE_HIP=ON/OFF`: build the HIP backend with AMD ROCm support, the configure tool will search for the HIP runtime (e.g. `-DCMAKE_PREFIX_PATH=/opt/rocm`). The kernels are compiled at runtime with hipRTC, targeting the GCN architecture of the installed GPU, so no HIP compiler is needed at build time. HIP can be enabled alongside the CUDA and SYCL backends. Published wheels do not include the HIP backend; build from source on a system with ROCm.

The following flags have been tested with Intel OneAPI 2025.3 for enabling SYCL support:
- `-DCMAKE_CXX_COMPILER=icpx`
Expand Down
5 changes: 3 additions & 2 deletions docs/src/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ spherical harmonics and their derivatives in Cartesian coordinates.
The theory behind this efficient implementation is detailed in
`this paper <https://doi.org/10.1063/5.0156307>`_.

The core library is implemented in C++ (with OpenMP parallelism), CUDA and SYCL.
The core library is implemented in C++ (with OpenMP parallelism), CUDA, HIP and SYCL.
It also provides APIs for C, Python (NumPy), PyTorch and JAX. The torch and JAX
implementations provide fast spherical harmonics on Nvidia GPUs.
implementations provide fast spherical harmonics on Nvidia GPUs; the HIP backend
targets AMD GPUs through ROCm.

A native Julia package is also available, contributed by
`Christoph Ortner <https://personal.math.ubc.ca/~ortner/>`_.
Expand Down
1 change: 1 addition & 0 deletions docs/src/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ The following cmake configuration options are available:
- ``-DSPHERICART_ENABLE_CUDA=ON/OFF``: build the CUDA backend (OFF by default)
- ``-DSPHERICART_ENABLE_SYCL=ON/OFF``: build the SYCL backend (OFF by default)
- ``-DSPHERICART_SYCL_DEVICE=all/cpu/gpu/accelerator``: select the device type used by the SYCL backend (``all`` by default)
- ``-DSPHERICART_ENABLE_HIP=ON/OFF``: build the HIP backend with AMD ROCm (OFF by default). The backend requires a ROCm installation (e.g. ``-DCMAKE_PREFIX_PATH=/opt/rocm``); kernels are compiled at runtime with hipRTC, so no HIP compiler is needed at build time. HIP can be enabled alongside the CUDA and SYCL backends.
- ``-DCMAKE_INSTALL_PREFIX=where/you/want/to/install``: set the root path for installation (``/usr/local`` by default)

Without specifying any options, the commands above will attempt to install
Expand Down
8 changes: 8 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ if (SPHERICART_ENABLE_SYCL)
add_test(NAME example_sycl COMMAND ./example_sycl)
endif()

if (SPHERICART_ENABLE_HIP)
add_executable(example_hip hip/example.cpp)
target_link_libraries(example_hip sphericart ${ROCM_HIP_LIBRARY})
target_include_directories(example_hip PRIVATE ${ROCM_HIP_INCLUDE_DIR})
target_compile_definitions(example_hip PRIVATE __HIP_PLATFORM_AMD__=1)
add_test(NAME example_hip COMMAND ./example_hip)
endif()

find_package(Kokkos CONFIG)
if(Kokkos_FOUND)
add_executable(example_kokkos kokkos/example.cpp)
Expand Down
155 changes: 155 additions & 0 deletions examples/hip/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/** @file example.cpp
* @brief Usage example for the HIP C++ API
*/

#include "sphericart_hip.hpp"
#include <cmath>
#include <hip/hip_runtime.h>
#include <iomanip>
#include <iostream>
#include <vector>

#ifndef DTYPE
#define DTYPE double
#endif

#include "sphericart.hpp"

#define CHECK_HIP(call) \
do { \
hipError_t status_ = (call); \
if (status_ != hipSuccess) { \
std::cout << "HIP error at " << __FILE__ << ":" << __LINE__ << ": " \
<< hipGetErrorString(status_) << std::endl; \
return 1; \
} \
} while (0)

#define TOLERANCE 1e-4 // High tolerance: finite differences are inaccurate for second

int main() {
/* ===== set up the calculation ===== */

// hard-coded parameters for the example
size_t n_samples = 1000;
size_t l_max = 6;

// initializes samples
auto xyz = std::vector<DTYPE>(n_samples * 3, 0.0);
for (size_t i = 0; i < n_samples * 3; ++i) {
xyz[i] = (DTYPE)rand() / (DTYPE)RAND_MAX * 2.0 - 1.0;
}

// host-side results, used as a reference
auto sph = std::vector<DTYPE>(n_samples * (l_max + 1) * (l_max + 1), 0.0);
auto dsph = std::vector<DTYPE>(n_samples * 3 * (l_max + 1) * (l_max + 1), 0.0);

// device-side results
auto sph_f = std::vector<DTYPE>(n_samples * (l_max + 1) * (l_max + 1), 0.0);
auto dsph_f = std::vector<DTYPE>(n_samples * 3 * (l_max + 1) * (l_max + 1), 0.0);

DTYPE* xyz_device = nullptr;
DTYPE* sph_device = nullptr;
DTYPE* dsph_device = nullptr;
CHECK_HIP(hipMalloc(&xyz_device, xyz.size() * sizeof(DTYPE)));
CHECK_HIP(hipMalloc(&sph_device, sph.size() * sizeof(DTYPE)));
CHECK_HIP(hipMalloc(&dsph_device, dsph.size() * sizeof(DTYPE)));
CHECK_HIP(hipMemcpy(xyz_device, xyz.data(), xyz.size() * sizeof(DTYPE), hipMemcpyHostToDevice));

/* ===== API calls ===== */

// internal buffers and numerical factors are initialized at construction
// HIP calculator
sphericart::hip::SphericalHarmonics<DTYPE> calculator_hip(l_max);
// CPU calculator
auto calculator = sphericart::SphericalHarmonics<DTYPE>(l_max);

std::cout << "ROUND 1 \n";
std::cout << " Computing SPH only \n";
calculator.compute(xyz, sph);
calculator_hip.compute(xyz_device, n_samples, sph_device);

// Get results from device to host
CHECK_HIP(
hipMemcpy(sph_f.data(), sph_device, sph_f.size() * sizeof(DTYPE), hipMemcpyDeviceToHost)
);

/* ===== check results ===== */
DTYPE sph_error = 0.0, sph_norm = 0.0, sph_error_max = 0.0;
for (size_t i = 0; i < sph.size(); ++i) {
DTYPE error = (sph_f[i] - sph[i]) * (sph_f[i] - sph[i]);
sph_error += error;
if (error > sph_error_max) {
sph_error_max = error;
}
sph_norm += sph[i] * sph[i];
}

std::cout << "CPU vs GPU relative error SPH: " << std::scientific << std::setprecision(8)
<< sqrt(sph_error / sph_norm) << std::endl;
std::cout << "Maximum squared error SPH: " << std::scientific << std::setprecision(8)
<< sph_error_max << std::endl;
std::cout << std::endl;

std::cout << "ROUND 2 \n";
std::cout << " Computing gradients (SPH and DSPH) \n";
calculator.compute_with_gradients(xyz, sph, dsph);
calculator_hip.compute_with_gradients(xyz_device, n_samples, sph_device, dsph_device);

CHECK_HIP(
hipMemcpy(sph_f.data(), sph_device, sph_f.size() * sizeof(DTYPE), hipMemcpyDeviceToHost)
);
CHECK_HIP(
hipMemcpy(dsph_f.data(), dsph_device, dsph_f.size() * sizeof(DTYPE), hipMemcpyDeviceToHost)
);

/* ===== check results ===== */
sph_error = 0.0, sph_norm = 0.0, sph_error_max = 0.0;
for (size_t i = 0; i < sph.size(); ++i) {
DTYPE error = (sph_f[i] - sph[i]) * (sph_f[i] - sph[i]);
sph_error += error;
if (error > sph_error_max) {
sph_error_max = error;
}
sph_norm += sph[i] * sph[i];
}

std::cout << "CPU vs GPU relative error SPH: " << std::scientific << std::setprecision(8)
<< sqrt(sph_error / sph_norm) << std::endl;
std::cout << "Maximum squared error SPH: " << std::scientific << std::setprecision(8)
<< sph_error_max << std::endl;
std::cout << std::endl;

DTYPE dsph_error = 0.0, dsph_norm = 0.0, dsph_error_max = 0.0;
int n_sph = (l_max + 1) * (l_max + 1);
for (size_t alpha = 0; alpha < 3; alpha++) {
for (size_t i_sample = 0; i_sample < n_samples; i_sample++) {
for (int i_sph = 0; i_sph < n_sph; i_sph++) {
DTYPE d0 = dsph[3 * n_sph * i_sample + n_sph * alpha + i_sph];
DTYPE d0_f = dsph_f[3 * n_sph * i_sample + n_sph * alpha + i_sph];
DTYPE error = (d0 - d0_f) * (d0 - d0_f);
dsph_error += error;
if (error > dsph_error_max) {
dsph_error_max = error;
}
dsph_norm += d0 * d0;
}
}
}
std::cout << "CPU vs GPU relative error DSPH: " << std::scientific << std::setprecision(8)
<< sqrt(dsph_error / dsph_norm) << std::endl;
std::cout << "Maximum squared error DSPH: " << std::scientific << std::setprecision(8)
<< dsph_error_max << std::endl;
std::cout << std::endl;

CHECK_HIP(hipFree(xyz_device));
CHECK_HIP(hipFree(sph_device));
CHECK_HIP(hipFree(dsph_device));

if (sqrt(sph_error / sph_norm) > TOLERANCE || sqrt(dsph_error / dsph_norm) > TOLERANCE) {
std::cout << "Example failed: GPU results do not match the CPU reference" << std::endl;
return 1;
}

return 0;
}
63 changes: 60 additions & 3 deletions sphericart/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ OPTION(SPHERICART_OPENMP "Try to use OpenMP when compiling Sphericart" ON)
OPTION(SPHERICART_ARCH_NATIVE "Try to use -march=native when compiling Sphericart" ON)
OPTION(SPHERICART_ENABLE_CUDA "Are we building the CUDA backend of Sphericart?" OFF)
OPTION(SPHERICART_ENABLE_SYCL "Are we building the SYCL backend of Sphericart?" OFF)
OPTION(SPHERICART_ENABLE_HIP "Are we building the HIP backend with AMD ROCm?" OFF)

if (SPHERICART_ENABLE_SYCL)
# SYCL device type: "gpu", "cpu", "accelerator", or "all"
Expand Down Expand Up @@ -241,19 +242,62 @@ else()
list(APPEND COMMON_SOURCES "src/sphericart_cuda_stub.cpp")
endif()

# Find HIP / ROCm. Kernels are compiled at runtime with hipRTC (a dlopen'd
# library), so the backend only needs the HIP runtime headers and library at
# build time - no HIP compiler is required, mirroring how the CUDA backend
# avoids a CUDA toolkit dependency through gpu-lite.
if (SPHERICART_ENABLE_HIP)
if (WIN32 OR APPLE)
message(FATAL_ERROR "The HIP backend of Sphericart is only supported on Linux")
endif()

find_path(ROCM_HIP_INCLUDE_DIR
NAMES hip/hip_runtime.h
PATHS
"${ROCM_ROOT}/include"
"$ENV{ROCM_ROOT}/include"
"${HIP_PATH}/include"
"$ENV{HIP_PATH}/include"
/opt/rocm/include
)
find_library(ROCM_HIP_LIBRARY
NAMES amdhip64
PATHS
"${ROCM_ROOT}/lib"
"$ENV{ROCM_ROOT}/lib"
"${HIP_PATH}/lib"
"$ENV{HIP_PATH}/lib"
/opt/rocm/lib
)
if (NOT ROCM_HIP_INCLUDE_DIR OR NOT ROCM_HIP_LIBRARY)
message(FATAL_ERROR
"SPHERICART_ENABLE_HIP=ON but the ROCm HIP runtime was not found. "
"Point CMake at your ROCm installation, e.g. "
"-DCMAKE_PREFIX_PATH=/opt/rocm")
endif()
message(STATUS "Found HIP runtime: ${ROCM_HIP_LIBRARY}")

list(APPEND COMMON_SOURCES "include/hip_base.hpp")
list(APPEND COMMON_SOURCES "include/sphericart_hip.hpp")
list(APPEND COMMON_SOURCES "src/hiprtc_lite.cpp")
list(APPEND COMMON_SOURCES "src/hip_base.cpp")
list(APPEND COMMON_SOURCES "src/sphericart_hip.cpp")
endif()

add_library(sphericart ${COMMON_SOURCES})

if (SPHERICART_ENABLE_CUDA)
if (SPHERICART_ENABLE_CUDA OR SPHERICART_ENABLE_HIP)
#include the build/generated folder for compilation
target_include_directories(sphericart PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(sphericart PRIVATE gpulite)

include_headers(
"${CMAKE_CURRENT_SOURCE_DIR}/src/sphericart_impl.cu"
"${CMAKE_CURRENT_BINARY_DIR}/generated/tmp.cu"
)

# Make the source file includeable by converting it to a string literal
# Make the source file includeable by converting it to a string literal.
# The same CUDA-dialect kernel source is embedded for the CUDA (NVRTC) and
# HIP (hipRTC) backends.
make_includeable(
"${CMAKE_CURRENT_BINARY_DIR}/generated/tmp.cu"
"${CMAKE_CURRENT_BINARY_DIR}/generated/wrapped_sphericart_impl.cu"
Expand All @@ -262,6 +306,19 @@ if (SPHERICART_ENABLE_CUDA)
file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/generated/tmp.cuh")
endif()

if (SPHERICART_ENABLE_CUDA)
target_link_libraries(sphericart PRIVATE gpulite)
endif()

if (SPHERICART_ENABLE_HIP)
# __HIP_PLATFORM_AMD__ selects the AMD flavor of the HIP headers; hipcc
# defines it implicitly, this backend compiles with the host compiler
target_compile_definitions(sphericart PRIVATE SPHERICART_USE_HIP=1 __HIP_PLATFORM_AMD__=1)
Comment thread
Luthaf marked this conversation as resolved.
Resolved
target_include_directories(sphericart PRIVATE ${ROCM_HIP_INCLUDE_DIR})
# ${CMAKE_DL_LIBS} is needed to dlopen libhiprtc at runtime
target_link_libraries(sphericart PRIVATE ${ROCM_HIP_LIBRARY} ${CMAKE_DL_LIBS})
endif()

if (SYCL_COMPILER_FOUND AND NOT SPHERICART_ENABLE_SYCL)
message(STATUS "Found a SYCL compiler but SPHERICART_ENABLE_SYCL=OFF, set SPHERICART_ENABLE_SYCL=ON in order to compile with CUDA support.")
endif()
Expand Down
Loading
Loading