Repository navigation
Add HIP (AMD ROCm) backend #257
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
Open
zhubonan
wants to merge
7
commits into
lab-cosmo:main
Choose a base branch
from
bonan-group:HIP-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4ce25a3
Add HIP (AMD ROCm) backend ported from symmetrix-xl
zhubonan 957091e
HIP: opt-in async gradients and explicit device resource release
zhubonan f45fee1
HIP: reject shared-memory requests above the device opt-in limit
zhubonan 7b89e16
Align HIP async timing assertion with the CUDA test and note wheel scope
zhubonan 6a1b013
Address CMake portability and backend coexistence review
zhubonan d22480b
Apply clang-format to HIP sources for CI lint
zhubonan b60d65f
Merge branch 'main' into HIP-support
Luthaf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| 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; | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.