Skip to content

Commit

Permalink
Merge parallel Jacobi build
Browse files Browse the repository at this point in the history
This PR compiles the individual Jacobi block sizes as separate TUs to improve memory usage
and total runtime of the compilation.

Related PR: #871
  • Loading branch information
upsj authored Sep 4, 2021
2 parents f5de29b + 1a065eb commit 3bc2ce3
Show file tree
Hide file tree
Showing 17 changed files with 858 additions and 244 deletions.
30 changes: 30 additions & 0 deletions cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,33 @@ target_sources(ginkgo_cuda
foreach(source_file IN LISTS GKO_CUDA_COMMON_SOURCES)
set_source_files_properties(${source_file} PROPERTIES LANGUAGE CUDA)
endforeach(source_file)
if(GINKGO_JACOBI_FULL_OPTIMIZATIONS)
set(GKO_CUDA_JACOBI_BLOCK_SIZES)
foreach(blocksize RANGE 1 32)
list(APPEND GKO_CUDA_JACOBI_BLOCK_SIZES ${blocksize})
endforeach()
else()
set(GKO_CUDA_JACOBI_BLOCK_SIZES 1 2 4 8 13 16 32)
endif()
set(GKO_CUDA_JACOBI_SOURCES)
foreach(GKO_JACOBI_BLOCK_SIZE IN LISTS GKO_CUDA_JACOBI_BLOCK_SIZES)
configure_file(
preconditioner/jacobi_generate_instantiate.inc.cu
preconditioner/jacobi_generate_instantiate.${GKO_JACOBI_BLOCK_SIZE}.cu)
configure_file(
preconditioner/jacobi_simple_apply_instantiate.inc.cu
preconditioner/jacobi_simple_apply_instantiate.${GKO_JACOBI_BLOCK_SIZE}.cu)
configure_file(
preconditioner/jacobi_advanced_apply_instantiate.inc.cu
preconditioner/jacobi_advanced_apply_instantiate.${GKO_JACOBI_BLOCK_SIZE}.cu)
list(APPEND GKO_CUDA_JACOBI_SOURCES
${CMAKE_CURRENT_BINARY_DIR}/preconditioner/jacobi_generate_instantiate.${GKO_JACOBI_BLOCK_SIZE}.cu
${CMAKE_CURRENT_BINARY_DIR}/preconditioner/jacobi_simple_apply_instantiate.${GKO_JACOBI_BLOCK_SIZE}.cu
${CMAKE_CURRENT_BINARY_DIR}/preconditioner/jacobi_advanced_apply_instantiate.${GKO_JACOBI_BLOCK_SIZE}.cu)
endforeach()
target_sources(ginkgo_cuda PRIVATE ${GKO_CUDA_JACOBI_SOURCES})
string(REPLACE ";" "," GKO_CUDA_JACOBI_BLOCK_SIZES_CODE "${GKO_CUDA_JACOBI_BLOCK_SIZES}")
configure_file(preconditioner/jacobi_common.hpp.in preconditioner/jacobi_common.hpp)

if(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA")
# remove false positive CUDA warnings when calling one<T>() and zero<T>()
Expand Down Expand Up @@ -177,6 +204,9 @@ ginkgo_compile_features(ginkgo_cuda)
target_compile_definitions(ginkgo_cuda PRIVATE GKO_COMPILING_CUDA)
target_include_directories(ginkgo_cuda
SYSTEM PRIVATE ${CUDA_INCLUDE_DIRS})
# include path for generated headers like jacobi_common.hpp
target_include_directories(ginkgo_cuda
PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/..)
target_link_libraries(ginkgo_cuda PRIVATE ${CUDA_RUNTIME_LIBS} ${CUBLAS} ${CUSPARSE} ${CURAND})
target_link_libraries(ginkgo_cuda PUBLIC ginkgo_device)
target_compile_options(ginkgo_cuda
Expand Down
125 changes: 125 additions & 0 deletions cuda/preconditioner/jacobi_advanced_apply_instantiate.inc.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2021, 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>*******************************/

#include "core/preconditioner/jacobi_kernels.hpp"


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


#include "core/base/extended_float.hpp"
#include "core/matrix/dense_kernels.hpp"
#include "core/preconditioner/jacobi_utils.hpp"
#include "core/synthesizer/implementation_selection.hpp"
#include "cuda/base/config.hpp"
#include "cuda/base/math.hpp"
#include "cuda/base/types.hpp"
#include "cuda/components/cooperative_groups.cuh"
#include "cuda/components/thread_ids.cuh"
#include "cuda/components/warp_blas.cuh"
#include "cuda/preconditioner/jacobi_common.hpp"


namespace gko {
namespace kernels {
namespace cuda {
/**
* @brief The Jacobi preconditioner namespace.
* @ref Jacobi
* @ingroup jacobi
*/
namespace jacobi {


#include "common/cuda_hip/preconditioner/jacobi_advanced_apply_kernel.hpp.inc"


// clang-format off
#cmakedefine GKO_JACOBI_BLOCK_SIZE @GKO_JACOBI_BLOCK_SIZE@
// clang-format on
// make things easier for IDEs
#ifndef GKO_JACOBI_BLOCK_SIZE
#define GKO_JACOBI_BLOCK_SIZE 1
#endif


template <int warps_per_block, int max_block_size, typename ValueType,
typename IndexType>
void advanced_apply(
syn::value_list<int, max_block_size>, size_type num_blocks,
const precision_reduction* block_precisions,
const IndexType* block_pointers, const ValueType* blocks,
const preconditioner::block_interleaved_storage_scheme<IndexType>&
storage_scheme,
const ValueType* alpha, const ValueType* b, size_type b_stride,
ValueType* x, size_type x_stride)
{
constexpr int subwarp_size = get_larger_power(max_block_size);
constexpr int blocks_per_warp = config::warp_size / subwarp_size;
const dim3 grid_size(ceildiv(num_blocks, warps_per_block * blocks_per_warp),
1, 1);
const dim3 block_size(subwarp_size, blocks_per_warp, warps_per_block);

if (block_precisions) {
kernel::advanced_adaptive_apply<max_block_size, subwarp_size,
warps_per_block>
<<<grid_size, block_size, 0, 0>>>(
as_cuda_type(blocks), storage_scheme, block_precisions,
block_pointers, num_blocks, as_cuda_type(alpha),
as_cuda_type(b), b_stride, as_cuda_type(x), x_stride);
} else {
kernel::advanced_apply<max_block_size, subwarp_size, warps_per_block>
<<<grid_size, block_size, 0, 0>>>(
as_cuda_type(blocks), storage_scheme, block_pointers,
num_blocks, as_cuda_type(alpha), as_cuda_type(b), b_stride,
as_cuda_type(x), x_stride);
}
}


#define DECLARE_JACOBI_ADVANCED_APPLY_INSTANTIATION(ValueType, IndexType) \
void advanced_apply<config::min_warps_per_block, GKO_JACOBI_BLOCK_SIZE, \
ValueType, IndexType>( \
syn::value_list<int, GKO_JACOBI_BLOCK_SIZE>, size_type, \
const precision_reduction*, const IndexType* block_pointers, \
const ValueType*, \
const preconditioner::block_interleaved_storage_scheme<IndexType>&, \
const ValueType*, const ValueType*, size_type, ValueType*, size_type)

GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(
DECLARE_JACOBI_ADVANCED_APPLY_INSTANTIATION);


} // namespace jacobi
} // namespace cuda
} // namespace kernels
} // namespace gko
41 changes: 1 addition & 40 deletions cuda/preconditioner/jacobi_advanced_apply_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ginkgo/core/base/exception_helpers.hpp>


#include "core/base/extended_float.hpp"
#include "core/matrix/dense_kernels.hpp"
#include "core/preconditioner/jacobi_utils.hpp"
#include "core/synthesizer/implementation_selection.hpp"
#include "cuda/base/config.hpp"
#include "cuda/base/math.hpp"
#include "cuda/base/types.hpp"
#include "cuda/components/cooperative_groups.cuh"
#include "cuda/components/thread_ids.cuh"
#include "cuda/components/warp_blas.cuh"
#include "cuda/preconditioner/jacobi_common.hpp"


Expand All @@ -60,12 +52,6 @@ namespace cuda {
namespace jacobi {


#include "common/cuda_hip/preconditioner/jacobi_advanced_apply_kernel.hpp.inc"


namespace {


template <int warps_per_block, int max_block_size, typename ValueType,
typename IndexType>
void advanced_apply(
Expand All @@ -75,36 +61,11 @@ void advanced_apply(
const preconditioner::block_interleaved_storage_scheme<IndexType>&
storage_scheme,
const ValueType* alpha, const ValueType* b, size_type b_stride,
ValueType* x, size_type x_stride)
{
constexpr int subwarp_size = get_larger_power(max_block_size);
constexpr int blocks_per_warp = config::warp_size / subwarp_size;
const dim3 grid_size(ceildiv(num_blocks, warps_per_block * blocks_per_warp),
1, 1);
const dim3 block_size(subwarp_size, blocks_per_warp, warps_per_block);

if (block_precisions) {
kernel::advanced_adaptive_apply<max_block_size, subwarp_size,
warps_per_block>
<<<grid_size, block_size, 0, 0>>>(
as_cuda_type(blocks), storage_scheme, block_precisions,
block_pointers, num_blocks, as_cuda_type(alpha),
as_cuda_type(b), b_stride, as_cuda_type(x), x_stride);
} else {
kernel::advanced_apply<max_block_size, subwarp_size, warps_per_block>
<<<grid_size, block_size, 0, 0>>>(
as_cuda_type(blocks), storage_scheme, block_pointers,
num_blocks, as_cuda_type(alpha), as_cuda_type(b), b_stride,
as_cuda_type(x), x_stride);
}
}
ValueType* x, size_type x_stride);

GKO_ENABLE_IMPLEMENTATION_SELECTION(select_advanced_apply, advanced_apply);


} // namespace


template <typename ValueType, typename IndexType>
void apply(std::shared_ptr<const CudaExecutor> exec, size_type num_blocks,
uint32 max_block_size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@ namespace jacobi {
* A compile-time list of block sizes for which dedicated generate and apply
* kernels should be compiled.
*/
#ifdef GINKGO_JACOBI_FULL_OPTIMIZATIONS
using compiled_kernels = syn::as_list<syn::range<1, config::warp_size + 1, 1>>;
#else
using compiled_kernels =
syn::value_list<int, 1, 2, 4, 8, 13, 16, 32, config::warp_size>;
// clang-format off
#cmakedefine GKO_CUDA_JACOBI_BLOCK_SIZES_CODE @GKO_CUDA_JACOBI_BLOCK_SIZES_CODE@
// clang-format on
// make things easier for IDEs
#ifndef GKO_CUDA_JACOBI_BLOCK_SIZES_CODE
#define GKO_CUDA_JACOBI_BLOCK_SIZES_CODE 1
#endif


using compiled_kernels = syn::value_list<int, GKO_CUDA_JACOBI_BLOCK_SIZES_CODE>;


constexpr int get_larger_power(int value, int guess = 1)
{
return guess >= value ? guess : get_larger_power(value, guess << 1);
Expand Down
132 changes: 132 additions & 0 deletions cuda/preconditioner/jacobi_generate_instantiate.inc.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2021, 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>*******************************/

#include "core/preconditioner/jacobi_kernels.hpp"


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


#include "core/base/extended_float.hpp"
#include "core/components/fill_array.hpp"
#include "core/preconditioner/jacobi_utils.hpp"
#include "core/synthesizer/implementation_selection.hpp"
#include "cuda/base/config.hpp"
#include "cuda/base/math.hpp"
#include "cuda/base/types.hpp"
#include "cuda/components/cooperative_groups.cuh"
#include "cuda/components/diagonal_block_manipulation.cuh"
#include "cuda/components/thread_ids.cuh"
#include "cuda/components/uninitialized_array.hpp"
#include "cuda/components/warp_blas.cuh"
#include "cuda/preconditioner/jacobi_common.hpp"


namespace gko {
namespace kernels {
namespace cuda {
/**
* @brief The Jacobi preconditioner namespace.
* @ref Jacobi
* @ingroup jacobi
*/
namespace jacobi {


#include "common/cuda_hip/preconditioner/jacobi_generate_kernel.hpp.inc"


// clang-format off
#cmakedefine GKO_JACOBI_BLOCK_SIZE @GKO_JACOBI_BLOCK_SIZE@
// clang-format on
// make things easier for IDEs
#ifndef GKO_JACOBI_BLOCK_SIZE
#define GKO_JACOBI_BLOCK_SIZE 1
#endif


template <int warps_per_block, int max_block_size, typename ValueType,
typename IndexType>
void generate(syn::value_list<int, max_block_size>,
const matrix::Csr<ValueType, IndexType>* mtx,
remove_complex<ValueType> accuracy, ValueType* block_data,
const preconditioner::block_interleaved_storage_scheme<IndexType>&
storage_scheme,
remove_complex<ValueType>* conditioning,
precision_reduction* block_precisions,
const IndexType* block_ptrs, size_type num_blocks)
{
constexpr int subwarp_size = get_larger_power(max_block_size);
constexpr int blocks_per_warp = config::warp_size / subwarp_size;
const dim3 grid_size(ceildiv(num_blocks, warps_per_block * blocks_per_warp),
1, 1);
const dim3 block_size(subwarp_size, blocks_per_warp, warps_per_block);

if (block_precisions) {
kernel::adaptive_generate<max_block_size, subwarp_size, warps_per_block>
<<<grid_size, block_size, 0, 0>>>(
mtx->get_size()[0], mtx->get_const_row_ptrs(),
mtx->get_const_col_idxs(),
as_cuda_type(mtx->get_const_values()), as_cuda_type(accuracy),
as_cuda_type(block_data), storage_scheme,
as_cuda_type(conditioning), block_precisions, block_ptrs,
num_blocks);
} else {
kernel::generate<max_block_size, subwarp_size, warps_per_block>
<<<grid_size, block_size, 0, 0>>>(
mtx->get_size()[0], mtx->get_const_row_ptrs(),
mtx->get_const_col_idxs(),
as_cuda_type(mtx->get_const_values()), as_cuda_type(block_data),
storage_scheme, block_ptrs, num_blocks);
}
}


#define DECLARE_JACOBI_GENERATE_INSTANTIATION(ValueType, IndexType) \
void generate<config::min_warps_per_block, GKO_JACOBI_BLOCK_SIZE, \
ValueType, IndexType>( \
syn::value_list<int, GKO_JACOBI_BLOCK_SIZE>, \
const matrix::Csr<ValueType, IndexType>*, remove_complex<ValueType>, \
ValueType*, \
const preconditioner::block_interleaved_storage_scheme<IndexType>&, \
remove_complex<ValueType>*, precision_reduction*, const IndexType*, \
size_type)

GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(
DECLARE_JACOBI_GENERATE_INSTANTIATION);


} // namespace jacobi
} // namespace cuda
} // namespace kernels
} // namespace gko
Loading

0 comments on commit 3bc2ce3

Please sign in to comment.