Skip to content

Commit

Permalink
make the macro stack clear
Browse files Browse the repository at this point in the history
  • Loading branch information
yhmtsai committed Nov 21, 2024
1 parent 8044d70 commit c14d27b
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 148 deletions.
27 changes: 12 additions & 15 deletions common/cuda_hip/solver/batch_bicgstab_launch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "core/base/batch_struct.hpp"
#include "core/matrix/batch_struct.hpp"
#include "core/solver/batch_bicgstab_kernels.hpp"
#include "core/solver/batch_dispatch.hpp"


namespace gko {
Expand Down Expand Up @@ -50,32 +51,28 @@ void launch_apply_kernel(
device_type<_vtype>* const __restrict__ workspace_data, \
const int& block_size, const size_t& shared_size)

#define GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH(...) \
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE_VARGS( \
GKO_DECLARE_BATCH_BICGSTAB_LAUNCH, __VA_ARGS__)

#define GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH_0_FALSE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH, 0, false)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_BICGSTAB_LAUNCH, 0, false)
#define GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH_1_FALSE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH, 1, false)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_BICGSTAB_LAUNCH, 1, false)
#define GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH_2_FALSE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH, 2, false)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_BICGSTAB_LAUNCH, 2, false)
#define GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH_3_FALSE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH, 3, false)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_BICGSTAB_LAUNCH, 3, false)
#define GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH_4_FALSE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH, 4, false)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_BICGSTAB_LAUNCH, 4, false)
#define GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH_5_FALSE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH, 5, false)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_BICGSTAB_LAUNCH, 5, false)
#define GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH_6_FALSE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH, 6, false)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_BICGSTAB_LAUNCH, 6, false)
#define GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH_7_FALSE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH, 7, false)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_BICGSTAB_LAUNCH, 7, false)
#define GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH_8_FALSE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH, 8, false)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_BICGSTAB_LAUNCH, 8, false)
#define GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH_9_FALSE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH, 9, false)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_BICGSTAB_LAUNCH, 9, false)
#define GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH_9_TRUE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_BICGSTAB_LAUNCH, 9, true)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_BICGSTAB_LAUNCH, 9, true)


} // namespace batch_bicgstab
Expand Down
19 changes: 8 additions & 11 deletions common/cuda_hip/solver/batch_cg_launch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "core/base/batch_struct.hpp"
#include "core/matrix/batch_struct.hpp"
#include "core/solver/batch_cg_kernels.hpp"
#include "core/solver/batch_dispatch.hpp"


namespace gko {
Expand Down Expand Up @@ -50,24 +51,20 @@ void launch_apply_kernel(
device_type<_vtype>* const __restrict__ workspace_data, \
const int& block_size, const size_t& shared_size)

#define GKO_INSTANTIATE_BATCH_CG_LAUNCH(...) \
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE_VARGS(GKO_DECLARE_BATCH_CG_LAUNCH, \
__VA_ARGS__)

#define GKO_INSTANTIATE_BATCH_CG_LAUNCH_0_FALSE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_CG_LAUNCH, 0, false)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_CG_LAUNCH, 0, false)
#define GKO_INSTANTIATE_BATCH_CG_LAUNCH_1_FALSE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_CG_LAUNCH, 1, false)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_CG_LAUNCH, 1, false)
#define GKO_INSTANTIATE_BATCH_CG_LAUNCH_2_FALSE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_CG_LAUNCH, 2, false)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_CG_LAUNCH, 2, false)
#define GKO_INSTANTIATE_BATCH_CG_LAUNCH_3_FALSE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_CG_LAUNCH, 3, false)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_CG_LAUNCH, 3, false)
#define GKO_INSTANTIATE_BATCH_CG_LAUNCH_4_FALSE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_CG_LAUNCH, 4, false)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_CG_LAUNCH, 4, false)
#define GKO_INSTANTIATE_BATCH_CG_LAUNCH_5_FALSE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_CG_LAUNCH, 5, false)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_CG_LAUNCH, 5, false)
#define GKO_INSTANTIATE_BATCH_CG_LAUNCH_5_TRUE \
GKO_BATCH_INSTANTIATE_VARGS(GKO_INSTANTIATE_BATCH_CG_LAUNCH, 5, true)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_CG_LAUNCH, 5, true)


} // namespace batch_cg
Expand Down
44 changes: 25 additions & 19 deletions core/base/batch_instantiation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,43 @@
#include <ginkgo/core/matrix/batch_identity.hpp>
#include <ginkgo/core/preconditioner/batch_jacobi.hpp>


namespace gko {
namespace batch {


// just make the call list more consistent
#define GKO_CALL(_macro, ...) _macro(__VA_ARGS__)

#define GKO_BATCH_INSTANTIATE_PRECONDITIONER(_next, ...) \
_next(__VA_ARGS__, gko::batch::matrix::Identity); \
_next(__VA_ARGS__, gko::batch::preconditioner::Jacobi)

#define GKO_BATCH_INSTANTIATE_MATRIX(_next, ...) \
_next(__VA_ARGS__, gko::batch::matrix::Ell); \
_next(__VA_ARGS__, gko::batch::matrix::Dense); \
_next(__VA_ARGS__, gko::batch::matrix::Csr)

/**
* Instantiates a template for each valid combination of value type, batch
* matrix type, and batch preconditioner type. This only allows batch matrix
* type and preconditioner type also uses the same value type.
*
* @param _macro A macro which expands the template instantiation
* (not including the leading `template` specifier).
* Should take three arguments, where the first is replaced by
* the value type, the second by the matrix, and the third by the
* preconditioner.
* @param args the first should be a macro which expands the template
* instantiation (not including the leading `template` specifier).
* Should take three arguments, where the first is replaced by the
* value type, the second by the matrix, and the third by the
* preconditioner.
*
* @note the second and third arguments only accept the base type.s
*/
#define GKO_INSTANTIATE_FOR_BATCH_VALUE_MATRIX_PRECONDITIONER(_macro) \
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE_VARGS(_macro, gko::batch::matrix::Csr, \
gko::batch::matrix::Identity); \
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE_VARGS(_macro, gko::batch::matrix::Ell, \
gko::batch::matrix::Identity); \
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE_VARGS( \
_macro, gko::batch::matrix::Dense, gko::batch::matrix::Identity); \
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE_VARGS( \
_macro, gko::batch::matrix::Csr, gko::batch::preconditioner::Jacobi); \
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE_VARGS( \
_macro, gko::batch::matrix::Ell, gko::batch::preconditioner::Jacobi); \
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE_VARGS( \
_macro, gko::batch::matrix::Dense, gko::batch::preconditioner::Jacobi)
#define GKO_INSTANTIATE_FOR_BATCH_VALUE_MATRIX_PRECONDITIONER(...) \
GKO_CALL(GKO_BATCH_INSTANTIATE_MATRIX, \
GKO_BATCH_INSTANTIATE_PRECONDITIONER, \
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE_VARGS, __VA_ARGS__)


} // namespace batch
} // namespace gko

#endif //
#endif // GKO_PUBLIC_CORE_BASE_BATCH_INSTANTIATION_HPP_
67 changes: 25 additions & 42 deletions core/solver/batch_dispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <ginkgo/core/solver/batch_bicgstab.hpp>
#include <ginkgo/core/stop/batch_stop_enum.hpp>

#include "core/base/batch_instantiation.hpp"
#include "core/base/batch_struct.hpp"
#include "core/matrix/batch_struct.hpp"

Expand Down Expand Up @@ -164,35 +165,28 @@ enum class log_type { simple_convergence_completion };
} // namespace log


#define GKO_BATCH_INSTANTIATE_STOP(macro, ...) \
macro(__VA_ARGS__, \
#define GKO_BATCH_INSTANTIATE_STOP(_next, ...) \
_next(__VA_ARGS__, \
::gko::batch::solver::device::batch_stop::SimpleAbsResidual); \
macro(__VA_ARGS__, \
_next(__VA_ARGS__, \
::gko::batch::solver::device::batch_stop::SimpleRelResidual)

#define GKO_BATCH_INSTANTIATE_PRECONDITIONER(macro, ...) \
GKO_BATCH_INSTANTIATE_STOP( \
macro, __VA_ARGS__, \
::gko::batch::solver::device::batch_preconditioner::Identity); \
GKO_BATCH_INSTANTIATE_STOP( \
macro, __VA_ARGS__, \
::gko::batch::solver::device::batch_preconditioner::ScalarJacobi); \
GKO_BATCH_INSTANTIATE_STOP( \
macro, __VA_ARGS__, \
::gko::batch::solver::device::batch_preconditioner::BlockJacobi)

#define GKO_BATCH_INSTANTIATE_LOGGER(macro, ...) \
GKO_BATCH_INSTANTIATE_PRECONDITIONER( \
macro, __VA_ARGS__, \
::gko::batch::solver::device::batch_log::SimpleFinalLogger)

#define GKO_BATCH_INSTANTIATE_MATRIX_VARGS(macro, ...) \
GKO_BATCH_INSTANTIATE_LOGGER(macro, __VA_ARGS__, \
batch::matrix::ell::uniform_batch); \
GKO_BATCH_INSTANTIATE_LOGGER(macro, __VA_ARGS__, \
batch::matrix::dense::uniform_batch); \
GKO_BATCH_INSTANTIATE_LOGGER(macro, __VA_ARGS__, \
batch::matrix::csr::uniform_batch)
#define GKO_BATCH_INSTANTIATE_DEVICE_PRECONDITIONER(_next, ...) \
_next(__VA_ARGS__, \
::gko::batch::solver::device::batch_preconditioner::Identity); \
_next(__VA_ARGS__, \
::gko::batch::solver::device::batch_preconditioner::ScalarJacobi); \
_next(__VA_ARGS__, \
::gko::batch::solver::device::batch_preconditioner::BlockJacobi)

#define GKO_BATCH_INSTANTIATE_LOGGER(_next, ...) \
_next(__VA_ARGS__, \
::gko::batch::solver::device::batch_log::SimpleFinalLogger)

#define GKO_BATCH_INSTANTIATE_MATRIX_BATCH(_next, ...) \
_next(__VA_ARGS__, batch::matrix::ell::uniform_batch); \
_next(__VA_ARGS__, batch::matrix::dense::uniform_batch); \
_next(__VA_ARGS__, batch::matrix::csr::uniform_batch)

/**
* Passes each valid configuration of batch solver template parameter to a
Expand All @@ -201,22 +195,11 @@ enum class log_type { simple_convergence_completion };
* GKO_BATCH_INSTANTIATE will be prepended to the batch solver template
* parameters.
*/
#define GKO_BATCH_INSTANTIATE_VARGS(macro, ...) \
GKO_BATCH_INSTANTIATE_MATRIX_VARGS(macro, __VA_ARGS__)


/**
* Passes each valid configuration of batch solver template parameter to a
* macro. The order of template parameters is: macro(<matrix>, <logger>,
* <precond>, <stop>)
*/
#define GKO_BATCH_INSTANTIATE_MATRIX(macro, ...) \
GKO_BATCH_INSTANTIATE_LOGGER(macro, batch::matrix::ell::uniform_batch); \
GKO_BATCH_INSTANTIATE_LOGGER(macro, batch::matrix::dense::uniform_batch); \
GKO_BATCH_INSTANTIATE_LOGGER(macro, batch::matrix::csr::uniform_batch)

#define GKO_BATCH_INSTANTIATE(macro) GKO_BATCH_INSTANTIATE_MATRIX(macro)

#define GKO_BATCH_INSTANTIATE(...) \
GKO_CALL(GKO_BATCH_INSTANTIATE_MATRIX_BATCH, GKO_BATCH_INSTANTIATE_LOGGER, \
GKO_BATCH_INSTANTIATE_DEVICE_PRECONDITIONER, \
GKO_BATCH_INSTANTIATE_STOP, \
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE_VARGS, __VA_ARGS__)

/**
* Handles dispatching to the correct instantiation of a batched solver
Expand Down
18 changes: 4 additions & 14 deletions cuda/solver/batch_bicgstab_launch.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,32 @@ template <typename StopType, typename PrecType, typename LogType,
int get_num_threads_per_block(std::shared_ptr<const DefaultExecutor> exec,
const int num_rows);

#define GKO_DECLARE_BATCH_BICGSTAB_GET_NUM_THREADS_PER_BLOCK_( \
#define GKO_DECLARE_BATCH_BICGSTAB_GET_NUM_THREADS_PER_BLOCK( \
_vtype, mat_t, log_t, pre_t, stop_t) \
int get_num_threads_per_block< \
stop_t<cuda_type<_vtype>>, pre_t<cuda_type<_vtype>>, \
log_t<gko::remove_complex<_vtype>>, mat_t<const cuda_type<_vtype>>, \
cuda_type<_vtype>>(std::shared_ptr<const DefaultExecutor> exec, \
const int num_rows)

#define GKO_INSTANTIATE_BATCH_BICGSTAB_GET_NUM_THREADS_PER_BLOCK_(...) \
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE_VARGS( \
GKO_DECLARE_BATCH_BICGSTAB_GET_NUM_THREADS_PER_BLOCK_, __VA_ARGS__)

#define GKO_INSTANTIATE_BATCH_BICGSTAB_GET_NUM_THREADS_PER_BLOCK \
GKO_BATCH_INSTANTIATE( \
GKO_INSTANTIATE_BATCH_BICGSTAB_GET_NUM_THREADS_PER_BLOCK_)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_BICGSTAB_GET_NUM_THREADS_PER_BLOCK)


template <typename StopType, typename PrecType, typename LogType,
typename BatchMatrixType, typename ValueType>
int get_max_dynamic_shared_memory(std::shared_ptr<const DefaultExecutor> exec);

#define GKO_DECLARE_BATCH_BICGSTAB_GET_MAX_DYNAMIC_SHARED_MEMORY_( \
#define GKO_DECLARE_BATCH_BICGSTAB_GET_MAX_DYNAMIC_SHARED_MEMORY( \
_vtype, mat_t, log_t, pre_t, stop_t) \
int get_max_dynamic_shared_memory< \
stop_t<cuda_type<_vtype>>, pre_t<cuda_type<_vtype>>, \
log_t<gko::remove_complex<_vtype>>, mat_t<const cuda_type<_vtype>>, \
cuda_type<_vtype>>(std::shared_ptr<const DefaultExecutor> exec)

#define GKO_INSTANTIATE_BATCH_BICGSTAB_GET_MAX_DYNAMIC_SHARED_MEMORY_(...) \
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE_VARGS( \
GKO_DECLARE_BATCH_BICGSTAB_GET_MAX_DYNAMIC_SHARED_MEMORY_, \
__VA_ARGS__)

#define GKO_INSTANTIATE_BATCH_BICGSTAB_GET_MAX_DYNAMIC_SHARED_MEMORY \
GKO_BATCH_INSTANTIATE( \
GKO_INSTANTIATE_BATCH_BICGSTAB_GET_MAX_DYNAMIC_SHARED_MEMORY_)
GKO_DECLARE_BATCH_BICGSTAB_GET_MAX_DYNAMIC_SHARED_MEMORY)


} // namespace batch_bicgstab
Expand Down
28 changes: 9 additions & 19 deletions cuda/solver/batch_cg_launch.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,31 @@ template <typename StopType, typename PrecType, typename LogType,
int get_num_threads_per_block(std::shared_ptr<const DefaultExecutor> exec,
const int num_rows);

#define GKO_DECLARE_BATCH_CG_GET_NUM_THREADS_PER_BLOCK_(_vtype, mat_t, log_t, \
pre_t, stop_t) \
int get_num_threads_per_block< \
stop_t<cuda_type<_vtype>>, pre_t<cuda_type<_vtype>>, \
log_t<gko::remove_complex<cuda_type<_vtype>>>, \
mat_t<const cuda_type<_vtype>>, cuda_type<_vtype>>( \
#define GKO_DECLARE_BATCH_CG_GET_NUM_THREADS_PER_BLOCK(_vtype, mat_t, log_t, \
pre_t, stop_t) \
int get_num_threads_per_block< \
stop_t<cuda_type<_vtype>>, pre_t<cuda_type<_vtype>>, \
log_t<gko::remove_complex<cuda_type<_vtype>>>, \
mat_t<const cuda_type<_vtype>>, cuda_type<_vtype>>( \
std::shared_ptr<const DefaultExecutor> exec, const int num_rows)

#define GKO_INSTANTIATE_BATCH_CG_GET_NUM_THREADS_PER_BLOCK_(...) \
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE_VARGS( \
GKO_DECLARE_BATCH_CG_GET_NUM_THREADS_PER_BLOCK_, __VA_ARGS__)

#define GKO_INSTANTIATE_BATCH_CG_GET_NUM_THREADS_PER_BLOCK \
GKO_BATCH_INSTANTIATE(GKO_INSTANTIATE_BATCH_CG_GET_NUM_THREADS_PER_BLOCK_)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_CG_GET_NUM_THREADS_PER_BLOCK)


template <typename StopType, typename PrecType, typename LogType,
typename BatchMatrixType, typename ValueType>
int get_max_dynamic_shared_memory(std::shared_ptr<const DefaultExecutor> exec);

#define GKO_DECLARE_BATCH_CG_GET_MAX_DYNAMIC_SHARED_MEMORY_( \
#define GKO_DECLARE_BATCH_CG_GET_MAX_DYNAMIC_SHARED_MEMORY( \
_vtype, mat_t, log_t, pre_t, stop_t) \
int get_max_dynamic_shared_memory< \
stop_t<cuda_type<_vtype>>, pre_t<cuda_type<_vtype>>, \
log_t<gko::remove_complex<_vtype>>, mat_t<const cuda_type<_vtype>>, \
cuda_type<_vtype>>(std::shared_ptr<const DefaultExecutor> exec)


#define GKO_INSTANTIATE_BATCH_CG_GET_MAX_DYNAMIC_SHARED_MEMORY_(...) \
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE_VARGS( \
GKO_DECLARE_BATCH_CG_GET_MAX_DYNAMIC_SHARED_MEMORY_, __VA_ARGS__)

#define GKO_INSTANTIATE_BATCH_CG_GET_MAX_DYNAMIC_SHARED_MEMORY \
GKO_BATCH_INSTANTIATE( \
GKO_INSTANTIATE_BATCH_CG_GET_MAX_DYNAMIC_SHARED_MEMORY_)
GKO_BATCH_INSTANTIATE(GKO_DECLARE_BATCH_CG_GET_MAX_DYNAMIC_SHARED_MEMORY)


} // namespace batch_cg
Expand Down
Loading

0 comments on commit c14d27b

Please sign in to comment.