-
Notifications
You must be signed in to change notification settings - Fork 8
Clean up CMake, add option for SYCL reference. #55
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
Draft
BenBrock
wants to merge
12
commits into
main
Choose a base branch
from
dev/brock/sycl-reference
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.
Draft
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
442ca6d
Clean up CMake, add option for SYCL reference.
BenBrock d87abf6
Intermediate commit: initial implementation, no tests yet.
BenBrock d55847d
Merge branch 'main' into dev/brock/sycl-reference
BenBrock 59d015c
Implement basic SpMM in SYCL.
BenBrock 1b82474
Implement basic benchmark.
BenBrock d468b7d
Add `spmm_benchmark` for GPU backends.
BenBrock 5a825e8
Update SYCL SpMM benchmark as well as general SpMM benchmark.
BenBrock 9fc0538
Use `thrust::device` to avoid any potential overhead from detecting
BenBrock 4cde552
Add plotting
BenBrock e089f8a
Implement simple reordering of split k method.
BenBrock 66d899f
Update
BenBrock adaf75f
Implement split-k algorithm with smem.
BenBrock 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
|
|
||
| function(add_sycl_example example_name) | ||
| add_executable(${example_name} ${example_name}.cpp) | ||
| target_link_libraries(${example_name} spblas fmt sycl_thrust) | ||
| endfunction() | ||
|
|
||
| add_sycl_example(sycl_spmm) |
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,167 @@ | ||
| #include <iostream> | ||
| #include <spblas/spblas.hpp> | ||
|
|
||
| #include <spblas/backend/sycl/spmm_impl.hpp> | ||
|
|
||
| #include <thrust/device_vector.h> | ||
|
|
||
| #include <cassert> | ||
|
|
||
| #include <cmath> | ||
| #include <fmt/core.h> | ||
| #include <fmt/ranges.h> | ||
|
|
||
| int main(int argc, char** argv) { | ||
| using value_t = float; | ||
| using index_t = int32_t; | ||
| using offset_t = int32_t; | ||
| namespace md = spblas::__mdspan; | ||
|
|
||
| offset_t nnz_row = 100; | ||
|
|
||
| index_t m = 100000; | ||
| index_t n = 1; | ||
| index_t k = 100000; | ||
|
|
||
| char method = 'k'; | ||
|
|
||
| std::size_t wg_size = 32; | ||
|
|
||
| if (argc >= 2) { | ||
| m = std::atoll(argv[1]); | ||
| } | ||
|
|
||
| if (argc >= 3) { | ||
| k = std::atoll(argv[2]); | ||
| } | ||
|
|
||
| if (argc >= 4) { | ||
| n = std::atoll(argv[3]); | ||
| } | ||
|
|
||
| if (argc >= 5) { | ||
| nnz_row = std::atoll(argv[4]); | ||
| } | ||
|
|
||
| if (argc >= 6) { | ||
| method = argv[5][0]; | ||
| } | ||
|
|
||
| if (argc >= 7) { | ||
| wg_size = std::atoll(argv[6]); | ||
| } | ||
|
|
||
| assert(method == 'k' || method == 'j'); | ||
|
|
||
| fmt::print("Multiplying {} x {} matrix with {} nnz/row by {} columns.\n", m, | ||
| k, nnz_row, n); | ||
| fmt::print("Using method {} with WG size {}\n", method, wg_size); | ||
|
|
||
| offset_t nnz_in = m * nnz_row; | ||
|
|
||
| auto&& [values, rowptr, colind, shape, nnz] = | ||
| spblas::generate_csr<value_t, index_t, offset_t>(m, k, nnz_in); | ||
|
|
||
| thrust::device_vector<value_t> d_values(values); | ||
| thrust::device_vector<offset_t> d_rowptr(rowptr); | ||
| thrust::device_vector<index_t> d_colind(colind); | ||
|
|
||
| spblas::csr_view<value_t, index_t, offset_t> a( | ||
| d_values.data().get(), d_rowptr.data().get(), d_colind.data().get(), | ||
| shape, nnz); | ||
|
|
||
| std::vector<value_t> b_values(k * n, 1); | ||
| std::vector<value_t> c_values(m * n, 0); | ||
|
|
||
| thrust::device_vector<value_t> d_b(b_values); | ||
| thrust::device_vector<value_t> d_c(c_values); | ||
|
|
||
| md::mdspan b(d_b.data().get(), k, n); | ||
| md::mdspan c(d_c.data().get(), m, n); | ||
|
|
||
| sycl::queue q(sycl::gpu_selector_v); | ||
|
|
||
| if (method == 'k') { | ||
| spblas::spmm_wgsplitk(q, a, b, c, wg_size); | ||
| } else { | ||
| spblas::spmm_wgsplitj(q, a, b, c, wg_size); | ||
| } | ||
|
|
||
| thrust::copy(d_c.begin(), d_c.end(), c_values.begin()); | ||
|
|
||
| std::vector<value_t> c_ref(m * n, 0); | ||
|
|
||
| spblas::csr_view<value_t, index_t, offset_t> a_view( | ||
| values.data(), rowptr.data(), colind.data(), shape, nnz); | ||
| md::mdspan b_view(b_values.data(), k, n); | ||
| md::mdspan c_view(c_ref.data(), m, n); | ||
|
|
||
| spblas::multiply(a_view, b_view, c_view); | ||
|
|
||
| // Compare results | ||
| const float epsilon = 64 * std::numeric_limits<float>::epsilon(); | ||
| const float abs_th = std::numeric_limits<float>::min(); | ||
| bool results_match = true; | ||
|
|
||
| for (std::size_t i = 0; i < c_ref.size(); ++i) { | ||
| float diff = std::abs(c_ref[i] - c_values[i]); | ||
| float norm = std::min(std::abs(c_ref[i]) + std::abs(c_values[i]), | ||
| std::numeric_limits<float>::max()); | ||
| float abs_error = std::max(abs_th, epsilon * norm); | ||
|
|
||
| if (diff > abs_error) { | ||
| results_match = false; | ||
| std::cout << "Mismatch at index " << i << ": " | ||
| << "SYCL result = " << c_values[i] | ||
| << ", Reference = " << c_ref[i] << "\n"; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (results_match) { | ||
| fmt::print("OK!\n"); | ||
| } else { | ||
| fmt::print("Error!\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| double gb = 1e-9 * (nnz * sizeof(value_t) + nnz * sizeof(index_t) + | ||
| (m + 1) * sizeof(offset_t) + k * n * sizeof(value_t) + | ||
| m * n * sizeof(value_t)); | ||
|
|
||
| std::size_t n_iterations = 10; | ||
|
|
||
| std::vector<double> durations; | ||
| durations.reserve(n_iterations); | ||
|
|
||
| double max_bw = 456; | ||
|
|
||
| for (std::size_t i = 0; i < n_iterations; i++) { | ||
| auto begin = std::chrono::high_resolution_clock::now(); | ||
| if (method == 'k') { | ||
| spblas::spmm_wgsplitk(q, a, b, c, wg_size); | ||
| } else { | ||
| spblas::spmm_wgsplitj(q, a, b, c, wg_size); | ||
| } | ||
| auto end = std::chrono::high_resolution_clock::now(); | ||
| double duration = std::chrono::duration<double>(end - begin).count(); | ||
| double gb_s = gb / duration; | ||
|
|
||
| fmt::print("Completed in {} s (achieved {} GB/s)\n", duration, gb_s); | ||
|
|
||
| durations.push_back(duration); | ||
| } | ||
|
|
||
| fmt::print("Durations: {}\n", durations); | ||
|
|
||
| std::sort(durations.begin(), durations.end()); | ||
|
|
||
| double median_duration = durations[durations.size() / 2]; | ||
|
|
||
| double median_gb_s = gb / median_duration; | ||
|
|
||
| fmt::print("Median duration {} ({} GB/s) {}% of peak.\n", median_duration, | ||
| median_gb_s, 100 * (median_gb_s / max_bw)); | ||
|
|
||
| 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
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,3 @@ | ||
| #pragma once | ||
|
|
||
| #include <spblas/backend/sycl/spmm_impl.hpp> |
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.
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.
spmm is one of the few sparse algorithms that has potential of getting into compute bound region instead of just memory bound, so calculating gflops is also helpful. all others should just be looked at compared to the gb memory limits.
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.
it can happen because of the potential reuse of B dense matrix if we are careful from cache, while streaming A matrix and limiting accesses to C (along with trying to not cache C at all)
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.
In my opinion, for measuring peak perf of a kernel, it is a good idea to have a warmup loop with several iterations untimed, then a timed run loop that in aggregate takes on order of seconds or at least ms to run, with average time per run computed and recorded. this increases the change of repeatability and stability of measurement and runs over time and makes them much more comparable.
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.
I've made a few updates that do both things: compute GFLOPs in addition to BW achieved, and do up to a 2 second warmup before timing.