-
Notifications
You must be signed in to change notification settings - Fork 109
Added exclusive prefix sum to examples #1825
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
SavannaSpayd
wants to merge
3
commits into
LLNL:develop
Choose a base branch
from
SavannaSpayd:prefix_sum_example
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,160 @@ | ||
| //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// | ||
| // Copyright (c) 2016-25, Lawrence Livermore National Security, LLC | ||
| // and RAJA project contributors. See the RAJA/LICENSE file for details. | ||
| // | ||
| // SPDX-License-Identifier: (BSD-3-Clause) | ||
| //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// | ||
|
|
||
| #include <cstdlib> | ||
|
|
||
| #include "RAJA/RAJA.hpp" | ||
| #include "RAJA/util/Timer.hpp" | ||
|
|
||
| #include "memoryManager.hpp" | ||
|
|
||
| /* | ||
| * Prefix Sum Example | ||
| * | ||
| * Computes an exclusive prefix sum (scan) on an array of integers using | ||
| * multiple execution policies. | ||
| * | ||
| * RAJA features shown: | ||
| * - `exclusive_scan` operation with different execution backends | ||
| * - Use of `make_span` to create RAJA-compatible views | ||
| * - Sequential, OpenMP, and CUDA execution variants | ||
| * - CUDA device memory allocation and transfer | ||
| * | ||
| * If CUDA is enabled, device memory is allocated manually with `cudaMalloc` | ||
| * and the results are copied back to host memory for validation. | ||
| */ | ||
|
|
||
|
|
||
| /* | ||
| CUDA_BLOCK_SIZE - specifies the number of threads in a CUDA thread block | ||
| */ | ||
| #if defined(RAJA_ENABLE_CUDA) | ||
| const int CUDA_BLOCK_SIZE = 256; | ||
| #endif | ||
|
|
||
| /* | ||
| N - the length of the series to perform the prefix sum | ||
| */ | ||
| const int N = 100000000; | ||
|
|
||
| bool check_equal(const std::vector<int>& a, const std::vector<int>& b) | ||
| { | ||
| if (a.size() != b.size()) return false; | ||
| for (size_t i = 0; i < a.size(); ++i) { | ||
| if (a[i] != b[i]) return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| int main(int RAJA_UNUSED_ARG(argc), char** RAJA_UNUSED_ARG(argv)) | ||
| { | ||
| std::cout << "\n\nRAJA prefix sum (exclusive_scan) example using a series of " << N << " length...\n"; | ||
|
|
||
| std::vector<int> input(N, 1); | ||
| std::vector<int> reference_output(N, 0); | ||
| std::vector<int> test_output(N, 0); | ||
|
|
||
| auto timer = RAJA::Timer(); | ||
| double elapsed_us; | ||
|
|
||
| //---------------------------------------------------------------------------- | ||
| std::cout << "\n Running C-style prefix sum...\n"; | ||
|
|
||
| timer.start(); | ||
|
|
||
| reference_output[0] = 0; | ||
| for (int i = 1; i < N; ++i) { | ||
| reference_output[i] = reference_output[i - 1] + input[i - 1]; | ||
| } | ||
|
|
||
| timer.stop(); | ||
| elapsed_us = timer.elapsed(); | ||
|
|
||
| std::cout << " Reference complete. Time: " << elapsed_us * 1e6 << " us\n"; | ||
| timer.reset(); | ||
|
|
||
| //---------------------------------------------------------------------------- | ||
| std::cout << "\n Running RAJA::exclusive_scan with seq_exec...\n"; | ||
| std::fill(test_output.begin(), test_output.end(), 0); | ||
|
|
||
| timer.start(); | ||
|
|
||
| RAJA::exclusive_scan<RAJA::seq_exec>( | ||
| RAJA::make_span(input), | ||
| RAJA::make_span(test_output), | ||
| RAJA::operators::plus<int>()); | ||
|
|
||
| timer.stop(); | ||
| elapsed_us = timer.elapsed(); | ||
|
|
||
| std::cout << " Result: " | ||
| << (check_equal(reference_output, test_output) ? "PASS" : "FAIL") | ||
| << " | Time: " << elapsed_us * 1e6 << " us\n"; | ||
| timer.reset(); | ||
|
|
||
|
|
||
| //---------------------------------------------------------------------------- | ||
| #if defined(RAJA_ENABLE_OPENMP) | ||
| std::cout << "\n Running RAJA::exclusive_scan with omp_parallel_for_exec...\n"; | ||
| std::fill(test_output.begin(), test_output.end(), 0); | ||
|
|
||
| timer.start(); | ||
|
|
||
| RAJA::exclusive_scan<RAJA::omp_parallel_for_exec>( | ||
| RAJA::make_span(input), | ||
| RAJA::make_span(test_output), | ||
| RAJA::operators::plus<int>()); | ||
|
|
||
| timer.stop(); | ||
| elapsed_us = timer.elapsed(); | ||
|
|
||
| std::cout << " Result: " | ||
| << (check_equal(reference_output, test_output) ? "PASS" : "FAIL") | ||
| << " | Time: " << elapsed_us * 1e6 << " us\n"; | ||
| timer.reset(); | ||
| #endif | ||
|
|
||
|
|
||
| //---------------------------------------------------------------------------- | ||
| #if defined(RAJA_ENABLE_CUDA) | ||
| std::cout << "\n Running RAJA::exclusive_scan with cuda_exec...\n"; | ||
|
|
||
| int* d_input; | ||
| int* d_output; | ||
|
|
||
| cudaMalloc((void**)&d_input, N * sizeof(int)); | ||
| cudaMalloc((void**)&d_output, N * sizeof(int)); | ||
|
|
||
| cudaMemcpy(d_input, input.data(), N * sizeof(int), cudaMemcpyHostToDevice); | ||
| cudaMemset(d_output, 0, N * sizeof(int)); | ||
|
|
||
| cudaDeviceSynchronize(); | ||
| timer.start(); | ||
|
|
||
| RAJA::exclusive_scan<RAJA::cuda_exec<CUDA_BLOCK_SIZE>>( | ||
| RAJA::make_span(d_input, N), | ||
| RAJA::make_span(d_output, N), | ||
| RAJA::operators::plus<int>{}); | ||
|
|
||
| cudaDeviceSynchronize(); // Make sure the scan finishes before timing ends | ||
| timer.stop(); | ||
| elapsed_us = timer.elapsed(); | ||
|
|
||
| cudaMemcpy(test_output.data(), d_output, N * sizeof(int), cudaMemcpyDeviceToHost); | ||
|
|
||
| std::cout << " Result: " | ||
| << (check_equal(reference_output, test_output) ? "PASS" : "FAIL") | ||
| << " | Time: " << elapsed_us * 1e6 << " us\n"; | ||
| timer.reset(); | ||
| cudaFree(d_input); | ||
| cudaFree(d_output); | ||
| #endif | ||
|
|
||
|
|
||
| std::cout << "\n DONE!...\n"; | ||
| return 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add HIP variant as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, I don't have a HIP-compatible environment. Since I wouldn’t be able to verify the results or debug any issues, I’ve opted to leave it out to avoid introducing untested code.