-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge (#1413): Add a batch::Dense matrix format and kernels
This PR adds support for the Batch Dense matrix format and currently adds very minimal functionality that is necessary along with kernels for different backends. Currently on a simple spmv and an advanced_spmv are supported. Related PR: #1413
- Loading branch information
Showing
49 changed files
with
3,223 additions
and
145 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
common/cuda_hip/matrix/batch_dense_kernel_launcher.hpp.inc
This file contains 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,78 @@ | ||
/*******************************<GINKGO LICENSE>****************************** | ||
Copyright (c) 2017-2023, the Ginkgo authors | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions | ||
are met: | ||
1. Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
2. Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in the | ||
documentation and/or other materials provided with the distribution. | ||
3. Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
******************************<GINKGO LICENSE>*******************************/ | ||
|
||
|
||
template <typename ValueType> | ||
void simple_apply(std::shared_ptr<const DefaultExecutor> exec, | ||
const batch::matrix::Dense<ValueType>* mat, | ||
const batch::MultiVector<ValueType>* b, | ||
batch::MultiVector<ValueType>* x) | ||
{ | ||
const auto num_blocks = mat->get_num_batch_items(); | ||
const auto b_ub = get_batch_struct(b); | ||
const auto x_ub = get_batch_struct(x); | ||
const auto mat_ub = get_batch_struct(mat); | ||
if (b->get_common_size()[1] > 1) { | ||
GKO_NOT_IMPLEMENTED; | ||
} | ||
simple_apply_kernel<<<num_blocks, default_block_size, 0, | ||
exec->get_stream()>>>(mat_ub, b_ub, x_ub); | ||
} | ||
|
||
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE( | ||
GKO_DECLARE_BATCH_DENSE_SIMPLE_APPLY_KERNEL); | ||
|
||
|
||
template <typename ValueType> | ||
void advanced_apply(std::shared_ptr<const DefaultExecutor> exec, | ||
const batch::MultiVector<ValueType>* alpha, | ||
const batch::matrix::Dense<ValueType>* mat, | ||
const batch::MultiVector<ValueType>* b, | ||
const batch::MultiVector<ValueType>* beta, | ||
batch::MultiVector<ValueType>* x) | ||
{ | ||
const auto num_blocks = mat->get_num_batch_items(); | ||
const auto b_ub = get_batch_struct(b); | ||
const auto x_ub = get_batch_struct(x); | ||
const auto mat_ub = get_batch_struct(mat); | ||
const auto alpha_ub = get_batch_struct(alpha); | ||
const auto beta_ub = get_batch_struct(beta); | ||
if (b->get_common_size()[1] > 1) { | ||
GKO_NOT_IMPLEMENTED; | ||
} | ||
advanced_apply_kernel<<<num_blocks, default_block_size, 0, | ||
exec->get_stream()>>>(alpha_ub, mat_ub, b_ub, | ||
beta_ub, x_ub); | ||
} | ||
|
||
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE( | ||
GKO_DECLARE_BATCH_DENSE_ADVANCED_APPLY_KERNEL); |
This file contains 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,164 @@ | ||
/*******************************<GINKGO LICENSE>****************************** | ||
Copyright (c) 2017-2023, the Ginkgo authors | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions | ||
are met: | ||
1. Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
2. Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in the | ||
documentation and/or other materials provided with the distribution. | ||
3. Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
******************************<GINKGO LICENSE>*******************************/ | ||
|
||
|
||
template <typename ValueType> | ||
__device__ __forceinline__ void simple_apply( | ||
const gko::batch::matrix::dense::batch_item<const ValueType>& mat, | ||
const ValueType* const __restrict__ b, ValueType* const __restrict__ x) | ||
{ | ||
constexpr auto tile_size = config::warp_size; | ||
|
||
auto thread_block = group::this_thread_block(); | ||
auto subgroup = group::tiled_partition<tile_size>(thread_block); | ||
const auto subgroup_id = static_cast<int>(threadIdx.x / tile_size); | ||
const int num_subgroups_per_block = ceildiv(blockDim.x, tile_size); | ||
|
||
for (int row = subgroup_id; row < mat.num_rows; | ||
row += num_subgroups_per_block) { | ||
ValueType temp = zero<ValueType>(); | ||
for (int j = subgroup.thread_rank(); j < mat.num_cols; | ||
j += subgroup.size()) { | ||
const ValueType val = mat.values[row * mat.stride + j]; | ||
temp += val * b[j]; | ||
} | ||
|
||
// subgroup level reduction | ||
temp = reduce(subgroup, temp, thrust::plus<ValueType>{}); | ||
|
||
if (subgroup.thread_rank() == 0) { | ||
x[row] = temp; | ||
} | ||
} | ||
} | ||
|
||
template <typename ValueType> | ||
__global__ __launch_bounds__( | ||
default_block_size, | ||
sm_oversubscription) void simple_apply_kernel(const gko::batch::matrix:: | ||
dense::uniform_batch< | ||
const ValueType> | ||
mat, | ||
const gko::batch:: | ||
multi_vector:: | ||
uniform_batch< | ||
const ValueType> | ||
b, | ||
const gko::batch:: | ||
multi_vector:: | ||
uniform_batch< | ||
ValueType> | ||
x) | ||
{ | ||
for (size_type batch_id = blockIdx.x; batch_id < mat.num_batch_items; | ||
batch_id += gridDim.x) { | ||
const auto mat_b = | ||
gko::batch::matrix::extract_batch_item(mat, batch_id); | ||
const auto b_b = gko::batch::extract_batch_item(b, batch_id); | ||
const auto x_b = gko::batch::extract_batch_item(x, batch_id); | ||
simple_apply(mat_b, b_b.values, x_b.values); | ||
} | ||
} | ||
|
||
|
||
template <typename ValueType> | ||
__device__ __forceinline__ void advanced_apply( | ||
const ValueType alpha, | ||
const gko::batch::matrix::dense::batch_item<const ValueType>& mat, | ||
const ValueType* const __restrict__ b, const ValueType beta, | ||
ValueType* const __restrict__ x) | ||
{ | ||
constexpr auto tile_size = config::warp_size; | ||
|
||
auto thread_block = group::this_thread_block(); | ||
auto subgroup = group::tiled_partition<tile_size>(thread_block); | ||
const auto subgroup_id = static_cast<int>(threadIdx.x / tile_size); | ||
const int num_subgroups_per_block = ceildiv(blockDim.x, tile_size); | ||
|
||
for (int row = subgroup_id; row < mat.num_rows; | ||
row += num_subgroups_per_block) { | ||
ValueType temp = zero<ValueType>(); | ||
for (int j = subgroup.thread_rank(); j < mat.num_cols; | ||
j += subgroup.size()) { | ||
const ValueType val = mat.values[row * mat.stride + j]; | ||
temp += alpha * val * b[j]; | ||
} | ||
|
||
// subgroup level reduction | ||
temp = reduce(subgroup, temp, thrust::plus<ValueType>{}); | ||
|
||
if (subgroup.thread_rank() == 0) { | ||
x[row] = temp + beta * x[row]; | ||
} | ||
} | ||
} | ||
|
||
template <typename ValueType> | ||
__global__ __launch_bounds__( | ||
default_block_size, | ||
sm_oversubscription) void advanced_apply_kernel(const gko::batch:: | ||
multi_vector:: | ||
uniform_batch< | ||
const ValueType> | ||
alpha, | ||
const gko::batch::matrix:: | ||
dense::uniform_batch< | ||
const ValueType> | ||
mat, | ||
const gko::batch:: | ||
multi_vector:: | ||
uniform_batch< | ||
const ValueType> | ||
b, | ||
const gko::batch:: | ||
multi_vector:: | ||
uniform_batch< | ||
const ValueType> | ||
beta, | ||
const gko::batch:: | ||
multi_vector:: | ||
uniform_batch< | ||
ValueType> | ||
x) | ||
{ | ||
for (size_type batch_id = blockIdx.x; batch_id < mat.num_batch_items; | ||
batch_id += gridDim.x) { | ||
const auto mat_b = | ||
gko::batch::matrix::extract_batch_item(mat, batch_id); | ||
const auto b_b = gko::batch::extract_batch_item(b, batch_id); | ||
const auto x_b = gko::batch::extract_batch_item(x, batch_id); | ||
const auto alpha_b = gko::batch::extract_batch_item(alpha, batch_id); | ||
const auto beta_b = gko::batch::extract_batch_item(beta, batch_id); | ||
advanced_apply(alpha_b.values[0], mat_b, b_b.values, beta_b.values[0], | ||
x_b.values); | ||
} | ||
} |
This file contains 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 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 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 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.