Skip to content
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

Add a Dense batched class and kernels #1413

Merged
merged 28 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5f2379f
Add batch dense base class, core and kernels
pratikvn Oct 1, 2023
e9f2aa1
add reference kernels WIP
pratikvn Oct 2, 2023
2f6bd77
Generalize batch utilities
pratikvn Oct 3, 2023
c9d5b44
MultiVector to BatchDense conversion
pratikvn Oct 3, 2023
4053a95
Add tests for BatchDense core
pratikvn Oct 3, 2023
e4928b2
Add reference kernel tests
pratikvn Oct 3, 2023
ecc7e51
Add OMP tests and fix kernel
pratikvn Oct 3, 2023
ccbbb40
Format files
ginkgo-bot Oct 4, 2023
691201a
circ dep and typo fixes
pratikvn Oct 4, 2023
a4b82ea
Add CUDA, HIP kernels and tests
pratikvn Oct 4, 2023
b23dbfa
Add SYCL kernels and tests WIP
pratikvn Oct 4, 2023
07578dd
HIP and CUDA thrust fixes
pratikvn Oct 5, 2023
81bcf74
SYCL kernel fixes
pratikvn Oct 5, 2023
2483667
BatchDense -> batch::Dense
pratikvn Oct 5, 2023
b402b94
Doc updates and multivector view
pratikvn Oct 5, 2023
3ca9fb4
Format files
ginkgo-bot Oct 6, 2023
e5b8813
Use CommonTestFixture value_type
pratikvn Oct 6, 2023
c00b6d9
Review updates
pratikvn Oct 9, 2023
fe21d65
Review updates
pratikvn Oct 9, 2023
f210ea9
dpcpp Jacobi needs ranlux
pratikvn Oct 9, 2023
fb74b71
Remove create_multivector_view
pratikvn Oct 9, 2023
660ec7c
Format files
ginkgo-bot Oct 9, 2023
76726e9
const_array_view needs to be in gko::
pratikvn Oct 9, 2023
09b7574
Review updates
pratikvn Oct 10, 2023
94452e9
Move apply validation to BatchLinOp
pratikvn Oct 10, 2023
4a18c40
Add to test_install
pratikvn Oct 10, 2023
927e8c8
Format files
ginkgo-bot Oct 10, 2023
190a010
Review updates
pratikvn Oct 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions common/cuda_hip/matrix/batch_dense_kernel_launcher.hpp.inc
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);
164 changes: 164 additions & 0 deletions common/cuda_hip/matrix/batch_dense_kernels.hpp.inc
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::
tcojean marked this conversation as resolved.
Show resolved Hide resolved
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) {
MarcelKoch marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ target_sources(ginkgo
log/vtune.cpp
log/record.cpp
log/stream.cpp
matrix/batch_dense.cpp
matrix/coo.cpp
matrix/csr.cpp
matrix/dense.cpp
Expand Down
28 changes: 25 additions & 3 deletions core/base/batch_multi_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ginkgo/core/base/math.hpp>
#include <ginkgo/core/base/matrix_data.hpp>
#include <ginkgo/core/base/utils.hpp>
#include <ginkgo/core/matrix/batch_dense.hpp>


#include "core/base/batch_multi_vector_kernels.hpp"
Expand Down Expand Up @@ -72,7 +73,7 @@ namespace detail {

template <typename ValueType>
batch_dim<2> compute_batch_size(
const std::vector<matrix::Dense<ValueType>*>& matrices)
const std::vector<gko::matrix::Dense<ValueType>*>& matrices)
{
auto common_size = matrices[0]->get_size();
for (size_type i = 1; i < matrices.size(); ++i) {
Expand All @@ -86,7 +87,7 @@ batch_dim<2> compute_batch_size(


template <typename ValueType>
std::unique_ptr<matrix::Dense<ValueType>>
std::unique_ptr<gko::matrix::Dense<ValueType>>
MultiVector<ValueType>::create_view_for_item(size_type item_id)
{
auto exec = this->get_executor();
Expand All @@ -102,7 +103,7 @@ MultiVector<ValueType>::create_view_for_item(size_type item_id)


template <typename ValueType>
std::unique_ptr<const matrix::Dense<ValueType>>
std::unique_ptr<const gko::matrix::Dense<ValueType>>
MultiVector<ValueType>::create_const_view_for_item(size_type item_id) const
{
auto exec = this->get_executor();
Expand Down Expand Up @@ -290,6 +291,27 @@ void MultiVector<ValueType>::move_to(
}


template <typename ValueType>
void MultiVector<ValueType>::convert_to(matrix::Dense<ValueType>* result) const
{
auto exec = result->get_executor() == nullptr ? this->get_executor()
: result->get_executor();
auto tmp = gko::batch::matrix::Dense<ValueType>::create_const(
exec, this->get_size(),
make_const_array_view(this->get_executor(),
this->get_num_stored_elements(),
this->get_const_values()));
result->copy_from(tmp);
}


template <typename ValueType>
void MultiVector<ValueType>::move_to(matrix::Dense<ValueType>* result)
{
this->convert_to(result);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should do an actual move here. It would be the same as above, except that the value array of this is moved.

}


#define GKO_DECLARE_BATCH_MULTI_VECTOR(_type) class MultiVector<_type>
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(GKO_DECLARE_BATCH_MULTI_VECTOR);

Expand Down
1 change: 0 additions & 1 deletion core/base/batch_multi_vector_kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <ginkgo/core/base/math.hpp>
#include <ginkgo/core/base/types.hpp>
#include <ginkgo/core/matrix/diagonal.hpp>


#include "core/base/kernel_declaration.hpp"
Expand Down
16 changes: 8 additions & 8 deletions core/base/batch_struct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ template <typename ValueType>
struct batch_item {
using value_type = ValueType;
ValueType* values;
int stride;
int num_rows;
int num_rhs;
int32 stride;
int32 num_rows;
int32 num_rhs;
};


Expand All @@ -67,9 +67,9 @@ struct uniform_batch {

ValueType* values;
size_type num_batch_items;
int stride;
int num_rows;
int num_rhs;
int32 stride;
int32 num_rows;
int32 num_rhs;
Comment on lines +70 to +72
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think int->int32 is not neccessary


size_type get_entry_storage() const
{
Expand Down Expand Up @@ -117,8 +117,8 @@ extract_batch_item(const multi_vector::uniform_batch<ValueType>& batch,

template <typename ValueType>
GKO_ATTRIBUTES GKO_INLINE multi_vector::batch_item<ValueType>
extract_batch_item(ValueType* const batch_values, const int stride,
const int num_rows, const int num_rhs,
extract_batch_item(ValueType* const batch_values, const int32 stride,
const int32 num_rows, const int32 num_rhs,
const size_type batch_idx)
{
return {batch_values + batch_idx * stride * num_rows, stride, num_rows,
Expand Down
Loading