Skip to content

Commit

Permalink
Add hip bicgstab solver kernels
Browse files Browse the repository at this point in the history
Co-authored-by: Aditya Kashi <[email protected]>
Co-authored-by: Isha Aggarwal <[email protected]>
  • Loading branch information
3 people committed Oct 26, 2023
1 parent 0857c11 commit fb0669f
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 16 deletions.
3 changes: 3 additions & 0 deletions cuda/base/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ void CudaExecutor::set_gpu_property()
kernels::cuda::config::warp_size;
this->get_exec_info().max_subgroup_size =
kernels::cuda::config::warp_size;
GKO_ASSERT_NO_CUDA_ERRORS(cudaDeviceGetAttribute(
&this->get_exec_info().max_shared_memory_per_workgroup,
cudaDevAttrMaxSharedMemoryPerBlock, this->get_device_id()));
}
}

Expand Down
6 changes: 3 additions & 3 deletions cuda/solver/batch_bicgstab_kernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace batch_bicgstab {

template <typename StopType, typename PrecType, typename LogType,
typename BatchMatrixType, typename ValueType>
int get_num_threads_per_block(std::shared_ptr<const CudaExecutor> exec,
int get_num_threads_per_block(std::shared_ptr<const DefaultExecutor> exec,
const int num_rows)
{
int nwarps = num_rows / 4;
Expand Down Expand Up @@ -117,7 +117,7 @@ int get_num_threads_per_block(std::shared_ptr<const CudaExecutor> exec,

template <typename StopType, typename PrecType, typename LogType,
typename BatchMatrixType, typename ValueType>
int get_max_dynamic_shared_memory(std::shared_ptr<const CudaExecutor> exec,
int get_max_dynamic_shared_memory(std::shared_ptr<const DefaultExecutor> exec,
const size_type required_cache_storage)
{
int shmem_per_sm = 0;
Expand Down Expand Up @@ -178,7 +178,7 @@ public:
{
using real_type = gko::remove_complex<value_type>;
const size_type num_batch_items = mat.num_batch_items;
constexpr int align_multiple = 2;
constexpr int align_multiple = 8;
const int shared_gap =
((mat.num_rows + align_multiple - 1) / align_multiple) *
align_multiple;
Expand Down
56 changes: 56 additions & 0 deletions hip/base/exception.hip.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*******************************<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>*******************************/

#ifndef GKO_HIP_BASE_EXCEPTION_HIP_HPP_
#define GKO_HIP_BASE_EXCEPTION_HIP_HPP_


#include <ginkgo/core/base/exception.hpp>


namespace gko {


#define GKO_HIP_LAST_IF_ERROR_THROW \
hipError_t err = hipGetLastError(); \
if (err != hipSuccess) { \
printf(" Hip kernel error: %s\n", hipGetErrorString(err)); \
throw gko::HipError(__FILE__, __LINE__, __func__, err); \
} \
static_assert(true, \
"This assert is used to counter the false positive extra " \
"semi-colon warnings")


} // namespace gko

#endif // GKO_HIP_BASE_EXCEPTION_HIP_HPP_
3 changes: 3 additions & 0 deletions hip/base/executor.hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ void HipExecutor::set_gpu_property()
#endif // GINKGO_HIP_PLATFORM_NVCC
this->get_exec_info().max_subgroup_size =
kernels::hip::config::warp_size;
GKO_ASSERT_NO_HIP_ERRORS(hipDeviceGetAttribute(
&this->get_exec_info().max_shared_memory_per_workgroup,
hipDeviceAttributeMaxSharedMemoryPerBlock, this->get_device_id()));
}
}

Expand Down
192 changes: 190 additions & 2 deletions hip/solver/batch_bicgstab_kernels.hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,38 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#include <hip/hip_runtime.h>
#include <thrust/functional.h>
#include <thrust/transform.h>


#include <ginkgo/core/base/exception_helpers.hpp>
#include <ginkgo/core/base/math.hpp>


#include "core/base/batch_struct.hpp"
#include "core/matrix/batch_struct.hpp"
#include "core/solver/batch_dispatch.hpp"
#include "hip/base/batch_struct.hip.hpp"
#include "hip/base/config.hip.hpp"
#include "hip/base/exception.hip.hpp"
#include "hip/base/math.hip.hpp"
#include "hip/base/thrust.hip.hpp"
#include "hip/base/types.hip.hpp"
#include "hip/components/cooperative_groups.hip.hpp"
#include "hip/components/reduction.hip.hpp"
#include "hip/components/thread_ids.hip.hpp"
#include "hip/components/uninitialized_array.hip.hpp"
#include "hip/matrix/batch_struct.hip.hpp"


namespace gko {
namespace kernels {
namespace hip {


constexpr int default_block_size = 256;
constexpr int sm_oversubscription = 4;

/**
* @brief The batch Bicgstab solver namespace.
*
Expand All @@ -57,19 +74,190 @@ namespace hip {
namespace batch_bicgstab {


#include "common/cuda_hip/components/uninitialized_array.hpp.inc"

#include "common/cuda_hip/base/batch_multi_vector_kernels.hpp.inc"
#include "common/cuda_hip/matrix/batch_dense_kernels.hpp.inc"
#include "common/cuda_hip/matrix/batch_ell_kernels.hpp.inc"
#include "common/cuda_hip/solver/batch_bicgstab_kernels.hpp.inc"


template <typename BatchMatrixType>
int get_num_threads_per_block(std::shared_ptr<const DefaultExecutor> exec,
const int num_rows)
{
int nwarps = num_rows / 4;
if (nwarps < 2) {
nwarps = 2;
}
const int min_block_size = 2 * config::warp_size;
const int device_max_threads =
((std::max(num_rows, min_block_size)) / config::warp_size) *
config::warp_size;
const int num_regs_used_per_thread = 64;
int max_regs_blk = 0;
hipDeviceGetAttribute(&max_regs_blk, hipDeviceAttributeMaxRegistersPerBlock,
exec->get_device_id());
const int max_threads_regs = (max_regs_blk / num_regs_used_per_thread);
const int max_threads = std::min(max_threads_regs, device_max_threads);
return std::min(nwarps * static_cast<int>(config::warp_size), max_threads);
}


template <typename T>
using settings = gko::kernels::batch_bicgstab::settings<T>;


template <typename HipValueType>
class KernelCaller {
public:
using value_type = HipValueType;

KernelCaller(std::shared_ptr<const DefaultExecutor> exec,
const settings<remove_complex<value_type>> settings)
: exec_{exec}, settings_{settings}
{}

template <typename StopType, const int n_shared,
const bool prec_shared_bool, typename PrecType, typename LogType,
typename BatchMatrixType>
void launch_apply_kernel(
const gko::kernels::batch_bicgstab::storage_config& sconf,
LogType& logger, PrecType& prec, const BatchMatrixType& mat,
const value_type* const __restrict__ b_values,
value_type* const __restrict__ x_values,
value_type* const __restrict__ workspace_data, const int& block_size,
const size_t& shared_size) const
{
apply_kernel<StopType, n_shared, prec_shared_bool>
<<<mat.num_batch_items, block_size, shared_size,
exec_->get_stream()>>>(sconf, settings_.max_iterations,
settings_.residual_tol, logger, prec, mat,
b_values, x_values, workspace_data);
}


template <typename BatchMatrixType, typename PrecType, typename StopType,
typename LogType>
void call_kernel(
LogType logger, const BatchMatrixType& mat, PrecType prec,
const gko::batch::multi_vector::uniform_batch<const value_type>& b,
const gko::batch::multi_vector::uniform_batch<value_type>& x) const
{
using real_type = gko::remove_complex<value_type>;
const size_type num_batch_items = mat.num_batch_items;
constexpr int align_multiple = 8;
const int shared_gap =
((mat.num_rows + align_multiple - 1) / align_multiple) *
align_multiple;
const int shmem_per_blk = exec_->get_max_shared_memory_per_block();
const int block_size =
get_num_threads_per_block<BatchMatrixType>(exec_, mat.num_rows);
assert(block_size >= 2 * config::warp_size);

const size_t prec_size =
PrecType::dynamic_work_size(shared_gap,
mat.get_single_item_num_nnz()) *
sizeof(value_type);
const auto sconf =
gko::kernels::batch_bicgstab::compute_shared_storage<PrecType,
value_type>(
shmem_per_blk, shared_gap, mat.get_single_item_num_nnz(),
b.num_rhs);
const size_t shared_size =
sconf.n_shared * shared_gap * sizeof(value_type) +
(sconf.prec_shared ? prec_size : 0);
auto workspace = gko::array<value_type>(
exec_,
sconf.gmem_stride_bytes * num_batch_items / sizeof(value_type));
assert(sconf.gmem_stride_bytes % sizeof(value_type) == 0);

value_type* const workspace_data = workspace.get_data();

// Template parameters launch_apply_kernel<StopType, n_shared,
// prec_shared)
if (sconf.prec_shared)
launch_apply_kernel<StopType, 9, 1>(
sconf, logger, prec, mat, b.values, x.values, workspace_data,
block_size, shared_size);
else {
switch (sconf.n_shared) {
case 0:
launch_apply_kernel<StopType, 0, 0>(
sconf, logger, prec, mat, b.values, x.values,
workspace_data, block_size, shared_size);
break;
case 1:
launch_apply_kernel<StopType, 1, 0>(
sconf, logger, prec, mat, b.values, x.values,
workspace_data, block_size, shared_size);
break;
case 2:
launch_apply_kernel<StopType, 2, 0>(
sconf, logger, prec, mat, b.values, x.values,
workspace_data, block_size, shared_size);
break;
case 3:
launch_apply_kernel<StopType, 3, 0>(
sconf, logger, prec, mat, b.values, x.values,
workspace_data, block_size, shared_size);
break;
case 4:
launch_apply_kernel<StopType, 4, 0>(
sconf, logger, prec, mat, b.values, x.values,
workspace_data, block_size, shared_size);
break;
case 5:
launch_apply_kernel<StopType, 5, 0>(
sconf, logger, prec, mat, b.values, x.values,
workspace_data, block_size, shared_size);
break;
case 6:
launch_apply_kernel<StopType, 6, 0>(
sconf, logger, prec, mat, b.values, x.values,
workspace_data, block_size, shared_size);
break;
case 7:
launch_apply_kernel<StopType, 7, 0>(
sconf, logger, prec, mat, b.values, x.values,
workspace_data, block_size, shared_size);
break;
case 8:
launch_apply_kernel<StopType, 8, 0>(
sconf, logger, prec, mat, b.values, x.values,
workspace_data, block_size, shared_size);
break;
case 9:
launch_apply_kernel<StopType, 9, 0>(
sconf, logger, prec, mat, b.values, x.values,
workspace_data, block_size, shared_size);
break;
}
}

GKO_HIP_LAST_IF_ERROR_THROW;
}

private:
std::shared_ptr<const DefaultExecutor> exec_;
const settings<remove_complex<value_type>> settings_;
};


template <typename ValueType>
void apply(std::shared_ptr<const DefaultExecutor> exec,
const settings<remove_complex<ValueType>>& settings,
const batch::BatchLinOp* const a,
const batch::BatchLinOp* const mat,
const batch::BatchLinOp* const precon,
const batch::MultiVector<ValueType>* const b,
batch::MultiVector<ValueType>* const x,
batch::log::detail::log_data<remove_complex<ValueType>>& logdata)
GKO_NOT_IMPLEMENTED;
{
using hip_value_type = hip_type<ValueType>;
auto dispatcher = batch::solver::create_dispatcher<ValueType>(
KernelCaller<hip_value_type>(exec, settings), settings, mat, precon);
dispatcher.apply(b, x, logdata);
}

GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(GKO_DECLARE_BATCH_BICGSTAB_APPLY_KERNEL);

Expand Down
Loading

0 comments on commit fb0669f

Please sign in to comment.