-
Notifications
You must be signed in to change notification settings - Fork 408
[HIP] Add execution test for setcc f64 -> setcc i32 reduction #378
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
zGoldthorpe
wants to merge
5
commits into
llvm:main
Choose a base branch
from
zGoldthorpe:pr188356
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.
+162
−0
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a199b3e
[HIP] Add execution test for setcc f64 -> setcc i32 reduction
zGoldthorpe 028b7a0
use `unique_ptr`
zGoldthorpe 529c9e5
Added more tests.
zGoldthorpe 0b2add8
Remove `std::memcpy` in favour of unqualified.
zGoldthorpe f6021e8
Add more constant test cases
zGoldthorpe 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| #include <iostream> | ||
|
|
||
| #include <hip/hip_runtime.h> | ||
|
|
||
| #define HIP_CHECK(r) \ | ||
| do { \ | ||
| if (r != hipSuccess) { \ | ||
| std::cerr << hipGetErrorString(r) << '\n'; \ | ||
| abort(); \ | ||
| } \ | ||
| } while (0) | ||
|
|
||
| static constexpr size_t N = 1024 * 500; | ||
| static constexpr size_t Iterations = 4096; | ||
|
|
||
| __host__ __device__ void fold(uint8_t sel, double x, double *y) { | ||
| double split = sel ? 1.0 : 4.0; | ||
| double abs_y = fabs(*y); | ||
|
|
||
| // lower 32 bits of split are always zero, so comparison can be reduced to an | ||
| // integral comparison of upper 32 bits | ||
| if (abs_y < split) { | ||
| *y += x; | ||
| } else { | ||
| *y /= 2.; | ||
| } | ||
| } | ||
|
|
||
| __global__ void f64_cmp_chain(const uint8_t *sel, const double *x, double *y) { | ||
| size_t tid = blockIdx.x * blockDim.x + threadIdx.x; | ||
| if (tid < N) { | ||
| for (size_t it = 0; it < Iterations; ++it) | ||
| fold(sel[tid], x[tid], &y[tid]); | ||
| } | ||
| } | ||
|
|
||
| void f64_cmp_chain_host(const uint8_t *sel, const double *x, double *y) { | ||
| for (size_t i = 0; i < N; ++i) | ||
| for (size_t it = 0; it < Iterations; ++it) | ||
| fold(sel[i], x[i], &y[i]); | ||
| } | ||
|
|
||
| int main(void) { | ||
| uint8_t *sel = (uint8_t *)malloc(N * sizeof(uint8_t)); | ||
| double *x = (double *)malloc(N * sizeof(double)); | ||
| double *y = (double *)malloc(N * sizeof(double)); | ||
|
zGoldthorpe marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Initialize inputs | ||
| for (size_t i = 0; i < N; ++i) { | ||
| sel[i] = i & 8; | ||
| x[i] = static_cast<double>(i); | ||
| y[i] = static_cast<double>(i) * -2.; | ||
| } | ||
|
|
||
| uint8_t *d_sel; | ||
| double *d_x; | ||
| double *d_y; | ||
| HIP_CHECK(hipMalloc((void **)&d_sel, N * sizeof(uint8_t))); | ||
| HIP_CHECK(hipMemcpy(d_sel, sel, N * sizeof(uint8_t), hipMemcpyHostToDevice)); | ||
| HIP_CHECK(hipMalloc((void **)&d_x, N * sizeof(double))); | ||
| HIP_CHECK(hipMemcpy(d_x, x, N * sizeof(double), hipMemcpyHostToDevice)); | ||
| HIP_CHECK(hipMalloc((void **)&d_y, N * sizeof(double))); | ||
| HIP_CHECK(hipMemcpy(d_y, y, N * sizeof(double), hipMemcpyHostToDevice)); | ||
|
|
||
| // CPU implementation | ||
| f64_cmp_chain_host(sel, x, y); | ||
|
|
||
| // GPU implementation | ||
| f64_cmp_chain<<<(N + 255) / 256, 256>>>(d_sel, d_x, d_y); | ||
|
|
||
| // Copy GPU result | ||
| double *h_y = (double *)malloc(N * sizeof(double)); | ||
| HIP_CHECK(hipDeviceSynchronize()); | ||
| HIP_CHECK(hipMemcpy(h_y, d_y, N * sizeof(double), hipMemcpyDeviceToHost)); | ||
|
|
||
| // Verify results | ||
| int errs = 0; | ||
| for (size_t i = 0; i < N; ++i) | ||
| if (fabs(y[i] - h_y[i]) > fabs(y[i] * 0.0001)) | ||
| ++errs; | ||
|
|
||
| if (errs != 0) | ||
| std::cout << "FAILED (errors: " << errs << ")\n"; | ||
| else | ||
| std::cout << "PASSED!\n"; | ||
|
|
||
| free(sel); | ||
| free(x); | ||
| free(y); | ||
| free(h_y); | ||
| HIP_CHECK(hipFree(d_sel)); | ||
| HIP_CHECK(hipFree(d_x)); | ||
| HIP_CHECK(hipFree(d_y)); | ||
|
|
||
| return errs; | ||
| } | ||
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,2 @@ | ||
| PASSED! | ||
| exit 0 |
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.