-
Notifications
You must be signed in to change notification settings - Fork 114
Add generic device policies #2036
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
artv3
wants to merge
15
commits into
develop
Choose a base branch
from
artv3/generic-device-policies
base: develop
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 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f3e4836
add generic policies
artv3 d40be98
add launch example
artv3 3031a97
Merge branch 'develop' into artv3/generic-device-policies
artv3 b7d3fdd
update the docs
artv3 dab7394
clean up pass
artv3 625015a
mark as important
artv3 f095eb5
try to better connect x/y/z to sycl in example
artv3 e22e1e3
add table and more device_* alias
artv3 dd926f2
Merge branch 'develop' into artv3/generic-device-policies
artv3 c8e7300
take up browser width
artv3 7634e6e
Merge branch 'develop' into artv3/generic-device-policies
artv3 a27892f
use a macro to reduce code
artv3 f684c2f
merge develop fix conflicts
artv3 ca77c82
new place for macros
artv3 548f1d1
Merge branch 'develop' into artv3/generic-device-policies
artv3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// | ||
| // Copyright (c) Lawrence Livermore National Security, LLC and other | ||
| // RAJA Project Developers. See top-level LICENSE and COPYRIGHT | ||
| // files for dates and other details. No copyright assignment is required | ||
| // to contribute to RAJA. | ||
| // | ||
| // SPDX-License-Identifier: (BSD-3-Clause) | ||
| //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// | ||
|
|
||
| // | ||
| // Device policy aliases example | ||
| // | ||
| // This example demonstrates: | ||
| // - Backend-generic GPU execution policies via RAJA::device_* | ||
| // - CUDA-like x/y/z naming for SYCL (x->2, y->1, z->0) in those aliases | ||
| // - RAJA::Teams::sycl_order / RAJA::Threads::sycl_order for expressing a | ||
| // launch grid in SYCL's (dim0,dim1,dim2) ordering. | ||
| // | ||
| // Notes: | ||
| // - RAJA::device_* aliases are defined only in a GPU device compile pass | ||
| // (RAJA_GPU_ACTIVE). If you compile this file as host-only, it will fall | ||
| // back to a sequential path. | ||
| // | ||
|
|
||
| #include <iostream> | ||
|
|
||
| #include "RAJA/RAJA.hpp" | ||
| #include "RAJA/util/resource.hpp" | ||
|
|
||
| static void printDims(const char* label, const int v[3]) | ||
| { | ||
| std::cout << label << " (x,y,z)=(" << v[0] << "," << v[1] << "," << v[2] | ||
| << ")\n"; | ||
| } | ||
|
|
||
| int main(int, char**) | ||
| { | ||
| constexpr int N = 1024; | ||
|
|
||
| // Demonstrate the (dim0,dim1,dim2)->(x,y,z) mapping helpers for RAJA::launch. | ||
| { | ||
| const auto threads_xyz = RAJA::Threads(8, 4, 2); | ||
| const auto threads_sycl = RAJA::Threads::sycl_order(/*dim0=*/2, | ||
| /*dim1=*/4, | ||
| /*dim2=*/8); | ||
| printDims("Threads(x,y,z)", threads_xyz.value); | ||
| printDims("Threads::sycl_order(dim0,dim1,dim2)", threads_sycl.value); | ||
| } | ||
|
|
||
| RAJA::resources::Host host {}; | ||
| int* a = host.allocate<int>(N); | ||
| int* b = host.allocate<int>(N); | ||
| int* c = host.allocate<int>(N); | ||
|
|
||
| for (int i = 0; i < N; ++i) | ||
| { | ||
| a[i] = i; | ||
| b[i] = 2 * i; | ||
| c[i] = 0; | ||
| } | ||
|
|
||
| #if defined(RAJA_GPU_ACTIVE) | ||
| // Use RAJA::device_exec to select the active GPU backend (CUDA/HIP/SYCL). | ||
| // | ||
| // The matching resource type is inferred from the execution policy. | ||
| auto device_res = RAJA::resources::get_default_resource<RAJA::device_exec<256>>(); | ||
|
|
||
| int* d_a = device_res.allocate<int>(N); | ||
| int* d_b = device_res.allocate<int>(N); | ||
| int* d_c = device_res.allocate<int>(N); | ||
|
|
||
| device_res.memcpy(d_a, a, sizeof(int) * N); | ||
| device_res.memcpy(d_b, b, sizeof(int) * N); | ||
|
|
||
| RAJA::forall<RAJA::device_exec<256>>(device_res, RAJA::RangeSegment(0, N), | ||
| [=] RAJA_DEVICE(int i) { | ||
| d_c[i] = d_a[i] + d_b[i]; | ||
| }); | ||
|
|
||
| device_res.memcpy(c, d_c, sizeof(int) * N); | ||
| device_res.wait(); | ||
|
|
||
| device_res.deallocate(d_a); | ||
| device_res.deallocate(d_b); | ||
| device_res.deallocate(d_c); | ||
|
|
||
| std::cout << "Computed on device using RAJA::device_exec<256>\n"; | ||
| #else | ||
| RAJA::forall<RAJA::seq_exec>(host, RAJA::RangeSegment(0, N), [=](int i) { | ||
| c[i] = a[i] + b[i]; | ||
| }); | ||
| std::cout << "Computed on host (no RAJA_GPU_ACTIVE)\n"; | ||
| #endif | ||
|
|
||
| // Quick correctness check. | ||
| bool ok = true; | ||
| for (int i = 0; i < N; ++i) | ||
| { | ||
| if (c[i] != a[i] + b[i]) | ||
| { | ||
| ok = false; | ||
| std::cerr << "Mismatch at i=" << i << " got " << c[i] | ||
| << " expected " << (a[i] + b[i]) << "\n"; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| host.deallocate(a); | ||
| host.deallocate(b); | ||
| host.deallocate(c); | ||
|
|
||
| return ok ? 0 : 1; | ||
| } | ||
|
|
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,188 @@ | ||
| //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// | ||
| // Copyright (c) Lawrence Livermore National Security, LLC and other | ||
| // RAJA Project Developers. See top-level LICENSE and COPYRIGHT | ||
| // files for dates and other details. No copyright assignment is required | ||
| // to contribute to RAJA. | ||
| // | ||
| // SPDX-License-Identifier: (BSD-3-Clause) | ||
| //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// | ||
|
|
||
| // | ||
| // RAJA::launch with device policy aliases | ||
| // | ||
| // This example shows how to write a single RAJA::launch kernel using the | ||
| // RAJA::device_* aliases so the same source can target CUDA, HIP, or SYCL | ||
| // without downstream #if/#ifdef in user code. | ||
| // | ||
| // It also demonstrates Teams/Threads ordering helpers: | ||
| // - RAJA::Teams(x,y,z) and RAJA::Threads(x,y,z) are RAJA's canonical | ||
| // ordering. | ||
| // - RAJA::Teams::sycl_order(dim0,dim1,dim2) and Threads::sycl_order(...) | ||
| // express SYCL's (dim0,dim1,dim2) ordering and map to RAJA (x,y,z). | ||
|
MrBurmark marked this conversation as resolved.
Outdated
|
||
| // | ||
|
|
||
| #include <iostream> | ||
| #include <string> | ||
|
|
||
| #include "RAJA/RAJA.hpp" | ||
| #include "RAJA/util/resource.hpp" | ||
|
|
||
| /* | ||
| * Define host/device launch policies. | ||
| * | ||
| * When compiling for a GPU backend, the RAJA::device_* aliases resolve to the | ||
| * active backend (CUDA/HIP/SYCL). When compiling host-only, we fall back to | ||
| * pure host policies. | ||
| */ | ||
| using host_launch = RAJA::seq_launch_t; | ||
|
|
||
| #if defined(RAJA_GPU_ACTIVE) | ||
| using device_launch = RAJA::device_launch_t<false>; | ||
| #endif | ||
|
|
||
| using launch_policy = RAJA::LaunchPolicy< | ||
| host_launch | ||
| #if defined(RAJA_GPU_ACTIVE) | ||
| , device_launch | ||
| #endif | ||
| >; | ||
|
|
||
| /* | ||
| * Define team and thread loop policies. | ||
| * | ||
| * These use RAJA::device_* aliases on GPU and expand to seq_exec on host. | ||
| */ | ||
| using teams_x = RAJA::LoopPolicy< | ||
| RAJA::seq_exec | ||
| #if defined(RAJA_GPU_ACTIVE) | ||
| , RAJA::device_block_x_direct | ||
| #endif | ||
| >; | ||
|
|
||
| using teams_y = RAJA::LoopPolicy< | ||
| RAJA::seq_exec | ||
| #if defined(RAJA_GPU_ACTIVE) | ||
| , RAJA::device_block_y_direct | ||
| #endif | ||
| >; | ||
|
|
||
| using threads_x = RAJA::LoopPolicy< | ||
| RAJA::seq_exec | ||
| #if defined(RAJA_GPU_ACTIVE) | ||
| , RAJA::device_thread_x_direct | ||
| #endif | ||
| >; | ||
|
|
||
| using threads_y = RAJA::LoopPolicy< | ||
| RAJA::seq_exec | ||
| #if defined(RAJA_GPU_ACTIVE) | ||
| , RAJA::device_thread_y_direct | ||
| #endif | ||
| >; | ||
|
|
||
| int main(int argc, char** argv) | ||
| { | ||
| constexpr int nteams_x = 2; | ||
| constexpr int nteams_y = 3; | ||
| constexpr int nthreads_x = 4; | ||
| constexpr int nthreads_y = 5; | ||
|
|
||
| constexpr int len = nteams_y * nteams_x * nthreads_y * nthreads_x; | ||
|
|
||
| RAJA::ExecPlace place = RAJA::ExecPlace::HOST; | ||
|
|
||
| #if defined(RAJA_GPU_ACTIVE) | ||
| if (argc != 2) { | ||
| RAJA_ABORT_OR_THROW("Usage: launch-device-policy-aliases host|device"); | ||
| } | ||
|
|
||
| std::string exec_space = argv[1]; | ||
| if (!(exec_space.compare("host") == 0 || exec_space.compare("device") == 0)) { | ||
| RAJA_ABORT_OR_THROW("Usage: launch-device-policy-aliases host|device"); | ||
| } | ||
|
|
||
| if (exec_space.compare("host") == 0) { place = RAJA::ExecPlace::HOST; } | ||
| if (exec_space.compare("device") == 0) { place = RAJA::ExecPlace::DEVICE; } | ||
| #else | ||
| (void)argc; | ||
| (void)argv; | ||
| #endif | ||
|
|
||
| // Default host resource for host allocations/checks. | ||
| RAJA::resources::Host host_res {}; | ||
| int* out_host = host_res.allocate<int>(len); | ||
|
|
||
| for (int i = 0; i < len; ++i) { | ||
| out_host[i] = -1; | ||
| } | ||
|
|
||
| #if defined(RAJA_GPU_ACTIVE) | ||
| auto device_res = RAJA::resources::get_default_resource<device_launch>(); | ||
| int* out_device = device_res.allocate<int>(len); | ||
| device_res.memcpy(out_device, out_host, sizeof(int) * len); | ||
| device_res.wait(); | ||
|
|
||
| int* out_ptr = (place == RAJA::ExecPlace::DEVICE) ? out_device : out_host; | ||
| #else | ||
| int* out_ptr = out_host; | ||
| #endif | ||
|
|
||
| // RAJA launch grid configuration is specified in (x,y,z). For SYCL-minded | ||
| // users, sycl_order(dim0,dim1,dim2) provides an explicit mapping. | ||
| RAJA::LaunchParams params_raja(RAJA::Teams(nteams_x, nteams_y, 1), | ||
| RAJA::Threads(nthreads_x, nthreads_y, 1)); | ||
|
|
||
| RAJA::LaunchParams params_sycl( | ||
| RAJA::Teams::sycl_order(/*dim0=*/1, /*dim1=*/nteams_y, /*dim2=*/nteams_x), | ||
| RAJA::Threads::sycl_order(/*dim0=*/1, /*dim1=*/nthreads_y, | ||
| /*dim2=*/nthreads_x)); | ||
|
|
||
| // Use the RAJA-ordered params for execution (it is backend-independent). | ||
| // params_sycl is included to demonstrate equivalent spelling. | ||
| (void)params_sycl; | ||
|
|
||
| RAJA::launch<launch_policy>(place, | ||
| params_raja, | ||
| [=] RAJA_HOST_DEVICE(RAJA::LaunchContext ctx) { | ||
|
|
||
| RAJA::loop<teams_y>(ctx, RAJA::RangeSegment(0, nteams_y), [&] (int by) { | ||
| RAJA::loop<teams_x>(ctx, RAJA::RangeSegment(0, nteams_x), [&] (int bx) { | ||
|
|
||
| RAJA::loop<threads_y>(ctx, RAJA::RangeSegment(0, nthreads_y), [&] (int ty) { | ||
| RAJA::loop<threads_x>(ctx, RAJA::RangeSegment(0, nthreads_x), [&] (int tx) { | ||
|
|
||
| int i = (((by * nteams_x) + bx) * nthreads_y + ty) * nthreads_x + tx; | ||
| out_ptr[i] = i; | ||
|
|
||
| }); | ||
| }); | ||
|
|
||
| }); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
| #if defined(RAJA_GPU_ACTIVE) | ||
| if (place == RAJA::ExecPlace::DEVICE) { | ||
| device_res.memcpy(out_host, out_device, sizeof(int) * len); | ||
| device_res.wait(); | ||
| } | ||
|
|
||
| device_res.deallocate(out_device); | ||
| #endif | ||
|
|
||
| bool ok = true; | ||
| for (int i = 0; i < len; ++i) { | ||
| if (out_host[i] != i) { | ||
| ok = false; | ||
| std::cerr << "Mismatch at i=" << i << " got " << out_host[i] | ||
| << " expected " << i << "\n"; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| host_res.deallocate(out_host); | ||
|
|
||
| std::cout << (ok ? "PASS\n" : "FAIL\n"); | ||
| return ok ? 0 : 1; | ||
| } | ||
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.