Skip to content

Commit

Permalink
Merge Add AmgxPgm cuda/hip/omp implementation, matrix/array utils
Browse files Browse the repository at this point in the history
This PR adds AmgxPgm cuda/hip/omp implementation and matrix/array utils

Summary:
- AmgxPgm device kernels except for dpcpp
- matrix/array generator
- symmetric/Hermitian/diag_dominant/hpd(spd)

Related PR: #695
  • Loading branch information
yhmtsai authored May 5, 2021
2 parents bd33473 + 8b5901b commit f36ac48
Show file tree
Hide file tree
Showing 49 changed files with 2,487 additions and 692 deletions.
228 changes: 228 additions & 0 deletions common/multigrid/amgx_pgm_kernels.hpp.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
/*******************************<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>*******************************/

namespace kernel {


template <typename IndexType>
__global__ __launch_bounds__(default_block_size) void match_edge_kernel(
size_type num, const IndexType *__restrict__ strongest_neighbor_vals,
IndexType *__restrict__ agg_vals)
{
auto tidx = thread::get_thread_id_flat<IndexType>();
if (tidx >= num) {
return;
}
if (agg_vals[tidx] != -1) {
return;
}
auto neighbor = strongest_neighbor_vals[tidx];
if (neighbor != -1 && strongest_neighbor_vals[neighbor] == tidx &&
tidx < neighbor) {
// Use the smaller index as agg point
agg_vals[tidx] = tidx;
agg_vals[neighbor] = tidx;
}
}


template <typename IndexType>
__global__ __launch_bounds__(default_block_size) void activate_kernel(
size_type num, const IndexType *__restrict__ agg,
IndexType *__restrict__ active_agg)
{
auto tidx = thread::get_thread_id_flat();
if (tidx >= num) {
return;
}
active_agg[tidx] = agg[tidx] == -1;
}


template <typename IndexType>
__global__ __launch_bounds__(default_block_size) void fill_agg_kernel(
size_type num, const IndexType *__restrict__ index,
IndexType *__restrict__ result)
{
auto tidx = thread::get_thread_id_flat();
if (tidx >= num) {
return;
}
// agg_vals[i] == i always holds in the aggregated group whose identifier is
// i because we use the index of element as the aggregated group identifier.
result[tidx] = (index[tidx] == tidx);
}


template <typename IndexType>
__global__ __launch_bounds__(default_block_size) void renumber_kernel(
size_type num, const IndexType *__restrict__ map,
IndexType *__restrict__ result)
{
auto tidx = thread::get_thread_id_flat();
if (tidx >= num) {
return;
}
result[tidx] = map[result[tidx]];
}


template <typename ValueType, typename IndexType>
__global__
__launch_bounds__(default_block_size) void find_strongest_neighbor_kernel(
const size_type num, const IndexType *__restrict__ row_ptrs,
const IndexType *__restrict__ col_idxs,
const ValueType *__restrict__ weight_vals,
const ValueType *__restrict__ diag, IndexType *__restrict__ agg,
IndexType *__restrict__ strongest_neighbor)
{
auto row = thread::get_thread_id_flat();
if (row >= num) {
return;
}

auto max_weight_unagg = zero<ValueType>();
auto max_weight_agg = zero<ValueType>();
IndexType strongest_unagg = -1;
IndexType strongest_agg = -1;
if (agg[row] != -1) {
return;
}
for (auto idx = row_ptrs[row]; idx < row_ptrs[row + 1]; idx++) {
auto col = col_idxs[idx];
if (col == row) {
continue;
}
auto weight = weight_vals[idx] / max(abs(diag[row]), abs(diag[col]));
if (agg[col] == -1 &&
thrust::tie(weight, col) >
thrust::tie(max_weight_unagg, strongest_unagg)) {
max_weight_unagg = weight;
strongest_unagg = col;
} else if (agg[col] != -1 &&
thrust::tie(weight, col) >
thrust::tie(max_weight_agg, strongest_agg)) {
max_weight_agg = weight;
strongest_agg = col;
}
}

if (strongest_unagg == -1 && strongest_agg != -1) {
// all neighbor is agg, connect to the strongest agg
// Also, no others will use this item as their strongest_neighbor
// because they are already aggregated. Thus, it is determinstic
// behavior
agg[row] = agg[strongest_agg];
} else if (strongest_unagg != -1) {
// set the strongest neighbor in the unagg group
strongest_neighbor[row] = strongest_unagg;
} else {
// no neighbor
strongest_neighbor[row] = row;
}
}


template <typename ValueType, typename IndexType>
__global__
__launch_bounds__(default_block_size) void assign_to_exist_agg_kernel(
const size_type num, const IndexType *__restrict__ row_ptrs,
const IndexType *__restrict__ col_idxs,
const ValueType *__restrict__ weight_vals,
const ValueType *__restrict__ diag,
const IndexType *__restrict__ agg_const_val,
IndexType *__restrict__ agg_val)
{
auto row = thread::get_thread_id_flat();
if (row >= num || agg_val[row] != -1) {
return;
}
ValueType max_weight_agg = zero<ValueType>();
IndexType strongest_agg = -1;
for (auto idx = row_ptrs[row]; idx < row_ptrs[row + 1]; idx++) {
auto col = col_idxs[idx];
if (col == row) {
continue;
}
auto weight = weight_vals[idx] / max(abs(diag[row]), abs(diag[col]));
if (agg_const_val[col] != -1 &&
thrust::tie(weight, col) >
thrust::tie(max_weight_agg, strongest_agg)) {
max_weight_agg = weight;
strongest_agg = col;
}
}
if (strongest_agg != -1) {
agg_val[row] = agg_const_val[strongest_agg];
} else {
agg_val[row] = row;
}
}

// This is the undeterminstic implementation which is the same implementation of
// the previous one but agg_val == agg_const_val.
template <typename ValueType, typename IndexType>
__global__
__launch_bounds__(default_block_size) void assign_to_exist_agg_kernel(
const size_type num, const IndexType *__restrict__ row_ptrs,
const IndexType *__restrict__ col_idxs,
const ValueType *__restrict__ weight_vals,
const ValueType *__restrict__ diag, IndexType *__restrict__ agg_val)
{
auto row = thread::get_thread_id_flat();
if (row >= num || agg_val[row] != -1) {
return;
}
ValueType max_weight_agg = zero<ValueType>();
IndexType strongest_agg = -1;
for (auto idx = row_ptrs[row]; idx < row_ptrs[row + 1]; idx++) {
auto col = col_idxs[idx];
if (col == row) {
continue;
}
auto weight = weight_vals[idx] / max(abs(diag[row]), abs(diag[col]));
if (agg_val[col] != -1 &&
thrust::tie(weight, col) >
thrust::tie(max_weight_agg, strongest_agg)) {
max_weight_agg = weight;
strongest_agg = col;
}
}
if (strongest_agg != -1) {
agg_val[row] = agg_val[strongest_agg];
} else {
agg_val[row] = row;
}
}


} // namespace kernel
5 changes: 0 additions & 5 deletions core/device_hooks/common_kernels.inc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1328,11 +1328,6 @@ GKO_NOT_COMPILED(GKO_HOOK_MODULE);
GKO_INSTANTIATE_FOR_EACH_NON_COMPLEX_VALUE_AND_INDEX_TYPE(
GKO_DECLARE_AMGX_PGM_ASSIGN_TO_EXIST_AGG);

template <typename ValueType, typename IndexType>
GKO_DECLARE_AMGX_PGM_GENERATE(ValueType, IndexType)
GKO_NOT_COMPILED(GKO_HOOK_MODULE);
GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(GKO_DECLARE_AMGX_PGM_GENERATE);


} // namespace amgx_pgm

Expand Down
38 changes: 11 additions & 27 deletions core/multigrid/amgx_pgm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,13 @@ GKO_REGISTER_OPERATION(renumber, amgx_pgm::renumber);
GKO_REGISTER_OPERATION(find_strongest_neighbor,
amgx_pgm::find_strongest_neighbor);
GKO_REGISTER_OPERATION(assign_to_exist_agg, amgx_pgm::assign_to_exist_agg);
GKO_REGISTER_OPERATION(amgx_pgm_generate, amgx_pgm::amgx_pgm_generate);
GKO_REGISTER_OPERATION(fill_array, components::fill_array);
GKO_REGISTER_OPERATION(fill_seq_array, components::fill_seq_array);


} // namespace amgx_pgm


namespace {


template <typename ValueType, typename IndexType>
std::unique_ptr<LinOp> amgx_pgm_generate(
std::shared_ptr<const Executor> exec,
const matrix::Csr<ValueType, IndexType> *source, const size_type num_agg,
const Array<IndexType> &agg)
{
auto coarse = matrix::Csr<ValueType, IndexType>::create(
exec, dim<2>{num_agg, num_agg}, 0, source->get_strategy());
exec->run(amgx_pgm::make_amgx_pgm_generate(source, agg, coarse.get()));
return std::move(coarse);
}


} // namespace


template <typename ValueType, typename IndexType>
void AmgxPgm<ValueType, IndexType>::generate()
{
Expand Down Expand Up @@ -155,13 +135,8 @@ void AmgxPgm<ValueType, IndexType>::generate()
// Renumber the index
exec->run(amgx_pgm::make_renumber(agg_, &num_agg));

// Construct the coarse matrix
auto coarse_matrix =
share(amgx_pgm_generate(exec, amgxpgm_op, num_agg, agg_));
// this->set_multigrid_level(system_matrix_, coarse_matrix);
auto coarse_dim = coarse_matrix->get_size()[0];
gko::dim<2>::dimension_type coarse_dim = num_agg;
auto fine_dim = system_matrix_->get_size()[0];

// TODO: prolong_op can be done with lightway format
auto prolong_op = share(
matrix_type::create(exec, gko::dim<2>{fine_dim, coarse_dim}, fine_dim));
Expand All @@ -172,7 +147,16 @@ void AmgxPgm<ValueType, IndexType>::generate()
exec->run(amgx_pgm::make_fill_array(prolong_op->get_values(), fine_dim,
one<ValueType>()));
// TODO: implement the restrict_op from aggregation.
auto restrict_op = share(prolong_op->transpose());
auto restrict_op = gko::as<matrix_type>(share(prolong_op->transpose()));

// Construct the coarse matrix
// TODO: use less memory footprint to improve it
auto coarse_matrix =
share(matrix_type::create(exec, gko::dim<2>{coarse_dim, coarse_dim}));
auto tmp = matrix_type::create(exec, gko::dim<2>{coarse_dim, fine_dim});
restrict_op->apply(amgxpgm_op, tmp.get());
tmp->apply(prolong_op.get(), coarse_matrix.get());

this->set_multigrid_level(prolong_op, coarse_matrix, restrict_op);
}

Expand Down
10 changes: 1 addition & 9 deletions core/multigrid/amgx_pgm_kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@ namespace amgx_pgm {
const matrix::Diagonal<ValueType> *diag, Array<IndexType> &agg, \
Array<IndexType> &intermediate_agg)

#define GKO_DECLARE_AMGX_PGM_GENERATE(ValueType, IndexType) \
void amgx_pgm_generate(std::shared_ptr<const DefaultExecutor> exec, \
const matrix::Csr<ValueType, IndexType> *source, \
const Array<IndexType> &agg, \
matrix::Csr<ValueType, IndexType> *coarse)

#define GKO_DECLARE_ALL_AS_TEMPLATES \
template <typename IndexType> \
GKO_DECLARE_AMGX_PGM_MATCH_EDGE_KERNEL(IndexType); \
Expand All @@ -94,9 +88,7 @@ namespace amgx_pgm {
template <typename ValueType, typename IndexType> \
GKO_DECLARE_AMGX_PGM_FIND_STRONGEST_NEIGHBOR(ValueType, IndexType); \
template <typename ValueType, typename IndexType> \
GKO_DECLARE_AMGX_PGM_ASSIGN_TO_EXIST_AGG(ValueType, IndexType); \
template <typename ValueType, typename IndexType> \
GKO_DECLARE_AMGX_PGM_GENERATE(ValueType, IndexType)
GKO_DECLARE_AMGX_PGM_ASSIGN_TO_EXIST_AGG(ValueType, IndexType)


} // namespace amgx_pgm
Expand Down
4 changes: 3 additions & 1 deletion core/test/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#include "core/base/extended_float.hpp"
#include "core/test/utils/array_generator.hpp"
#include "core/test/utils/assertions.hpp"
#include "core/test/utils/matrix_generator.hpp"
#include "core/test/utils/matrix_utils.hpp"
#include "core/test/utils/value_generator.hpp"


namespace gko {
Expand All @@ -63,7 +66,6 @@ using ValueTypes =
::testing::Types<float, double, std::complex<float>, std::complex<double>>;
#endif


using ComplexValueTypes =
#if GINKGO_DPCPP_SINGLE_MODE
::testing::Types<std::complex<float>>;
Expand Down
3 changes: 3 additions & 0 deletions core/test/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
ginkgo_create_test(array_generator_test)
ginkgo_create_test(assertions_test)
ginkgo_create_test(matrix_generator_test)
ginkgo_create_test(matrix_utils_test)
ginkgo_create_test(unsort_matrix_test)
ginkgo_create_test(value_generator_test)
Loading

0 comments on commit f36ac48

Please sign in to comment.