Skip to content

Commit

Permalink
Merge: Reverse Cuthill-McKee reordering
Browse files Browse the repository at this point in the history
This PR adds a reordering framework and the RCM algorithm for reordering a matrix.

+ All reordering is done on the CPU and if reordering is called on the GPU, it is copied to the host and the reordering is copied back to the GPU.

Related PR: #500
  • Loading branch information
pratikvn committed Oct 16, 2020
2 parents fd66b6b + e1f949c commit a0e1050
Show file tree
Hide file tree
Showing 35 changed files with 5,669 additions and 1 deletion.
1 change: 1 addition & 0 deletions contributors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ Maier Matthias <matthias@43-1.org> Texas A&M University
Nayak Pratik <pratik.nayak@kit.edu> Karlsruhe Institute of Technology
Olenik Gregor <go@hpsim.de> HPSim
Ribizel Tobias <mail@upsj.de> Karlsruhe Institute of Technology
Riemer Lukas <lksriemer@gmail.com> Karlsruhe Institute of Technology
Tsai Yuhsiang <yhmtsai@gmail.com> National Taiwan University
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ target_sources(ginkgo
matrix/sparsity_csr.cpp
preconditioner/isai.cpp
preconditioner/jacobi.cpp
reorder/rcm.cpp
solver/bicg.cpp
solver/bicgstab.cpp
solver/cg.cpp
Expand Down
2 changes: 1 addition & 1 deletion core/base/allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ExecutorAllocator {
* @tparam U the element type of the allocator to be constructed.
*/
template <typename U>
explicit ExecutorAllocator(const ExecutorAllocator<U> &other)
ExecutorAllocator(const ExecutorAllocator<U> &other)
: exec_{other.get_executor()}
{}

Expand Down
17 changes: 17 additions & 0 deletions core/device_hooks/common_kernels.inc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "core/matrix/sparsity_csr_kernels.hpp"
#include "core/preconditioner/isai_kernels.hpp"
#include "core/preconditioner/jacobi_kernels.hpp"
#include "core/reorder/rcm_kernels.hpp"
#include "core/solver/bicg_kernels.hpp"
#include "core/solver/bicgstab_kernels.hpp"
#include "core/solver/cg_kernels.hpp"
Expand Down Expand Up @@ -1050,6 +1051,22 @@ GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(


} // namespace par_ilut_factorization
namespace rcm {


template <typename IndexType>
GKO_DECLARE_RCM_GET_PERMUTATION_KERNEL(IndexType)
GKO_NOT_COMPILED(GKO_HOOK_MODULE);
GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(GKO_DECLARE_RCM_GET_PERMUTATION_KERNEL);


template <typename IndexType>
GKO_DECLARE_RCM_GET_DEGREE_OF_NODES_KERNEL(IndexType)
GKO_NOT_COMPILED(GKO_HOOK_MODULE);
GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(GKO_DECLARE_RCM_GET_DEGREE_OF_NODES_KERNEL);


} // namespace rcm


namespace set_all_statuses {
Expand Down
91 changes: 91 additions & 0 deletions core/reorder/rcm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2020, 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 <ginkgo/core/reorder/rcm.hpp>


#include <memory>


#include <ginkgo/core/base/array.hpp>
#include <ginkgo/core/base/exception_helpers.hpp>
#include <ginkgo/core/base/executor.hpp>
#include <ginkgo/core/base/polymorphic_object.hpp>
#include <ginkgo/core/base/types.hpp>
#include <ginkgo/core/base/utils.hpp>
#include <ginkgo/core/matrix/csr.hpp>
#include <ginkgo/core/matrix/permutation.hpp>
#include <ginkgo/core/matrix/sparsity_csr.hpp>


#include "core/matrix/csr_kernels.hpp"
#include "core/reorder/rcm_kernels.hpp"


namespace gko {
namespace reorder {
namespace rcm {


GKO_REGISTER_OPERATION(get_permutation, rcm::get_permutation);
GKO_REGISTER_OPERATION(get_degree_of_nodes, rcm::get_degree_of_nodes);


} // namespace rcm


template <typename ValueType, typename IndexType>
void Rcm<ValueType, IndexType>::generate(
std::shared_ptr<const Executor> &exec,
std::unique_ptr<SparsityMatrix> adjacency_matrix) const
{
const IndexType num_rows = adjacency_matrix->get_size()[0];
const auto mtx = adjacency_matrix.get();
auto degrees = Array<IndexType>(exec, num_rows);
// RCM is only valid for symmetric matrices. Need to add an expensive check
// for symmetricity here ?
exec->run(rcm::make_get_degree_of_nodes(num_rows, mtx->get_const_row_ptrs(),
degrees.get_data()));
exec->run(rcm::make_get_permutation(
num_rows, mtx->get_const_row_ptrs(), mtx->get_const_col_idxs(),
degrees.get_const_data(), permutation_->get_permutation(),
inv_permutation_.get() ? inv_permutation_->get_permutation() : nullptr,
parameters_.strategy));
}


#define GKO_DECLARE_RCM(ValueType, IndexType) class Rcm<ValueType, IndexType>
GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(GKO_DECLARE_RCM);


} // namespace reorder
} // namespace gko
117 changes: 117 additions & 0 deletions core/reorder/rcm_kernels.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2020, 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_CORE_REORDER_RCM_KERNELS_HPP_
#define GKO_CORE_REORDER_RCM_KERNELS_HPP_


#include <ginkgo/core/reorder/rcm.hpp>


#include <memory>


#include <ginkgo/core/base/executor.hpp>
#include <ginkgo/core/base/lin_op.hpp>
#include <ginkgo/core/base/types.hpp>
#include <ginkgo/core/matrix/csr.hpp>
#include <ginkgo/core/matrix/permutation.hpp>
#include <ginkgo/core/matrix/sparsity_csr.hpp>


namespace gko {
namespace kernels {


#define GKO_DECLARE_RCM_GET_PERMUTATION_KERNEL(IndexType) \
void get_permutation(std::shared_ptr<const DefaultExecutor> exec, \
IndexType num_vertices, const IndexType *row_ptrs, \
const IndexType *col_idxs, const IndexType *degrees, \
IndexType *permutation, IndexType *inv_permutation, \
gko::reorder::starting_strategy strategy)

#define GKO_DECLARE_RCM_GET_DEGREE_OF_NODES_KERNEL(IndexType) \
void get_degree_of_nodes(std::shared_ptr<const DefaultExecutor> exec, \
IndexType num_vertices, \
const IndexType *row_ptrs, IndexType *degrees)

#define GKO_DECLARE_ALL_AS_TEMPLATES \
template <typename IndexType> \
GKO_DECLARE_RCM_GET_DEGREE_OF_NODES_KERNEL(IndexType); \
template <typename IndexType> \
GKO_DECLARE_RCM_GET_PERMUTATION_KERNEL(IndexType)


namespace omp {
namespace rcm {

GKO_DECLARE_ALL_AS_TEMPLATES;

} // namespace rcm
} // namespace omp


namespace cuda {
namespace rcm {

GKO_DECLARE_ALL_AS_TEMPLATES;

} // namespace rcm
} // namespace cuda


namespace hip {
namespace rcm {

GKO_DECLARE_ALL_AS_TEMPLATES;

} // namespace rcm
} // namespace hip


namespace reference {
namespace rcm {

GKO_DECLARE_ALL_AS_TEMPLATES;

} // namespace rcm
} // namespace reference


#undef GKO_DECLARE_ALL_AS_TEMPLATES


} // namespace kernels
} // namespace gko


#endif // GKO_CORE_REORDER_RCM_KERNELS_HPP_
1 change: 1 addition & 0 deletions core/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_subdirectory(factorization)
add_subdirectory(log)
add_subdirectory(matrix)
add_subdirectory(preconditioner)
add_subdirectory(reorder)
add_subdirectory(solver)
add_subdirectory(stop)
add_subdirectory(utils)
1 change: 1 addition & 0 deletions core/test/reorder/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ginkgo_create_test(rcm)
70 changes: 70 additions & 0 deletions core/test/reorder/rcm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2019, 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 <ginkgo/core/reorder/rcm.hpp>


#include <memory>


#include <gtest/gtest.h>


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


#include "core/test/utils.hpp"


namespace {

class Rcm : public ::testing::Test {
protected:
using v_type = double;
using i_type = int;
using reorder_type = gko::reorder::Rcm<v_type, i_type>;

Rcm()
: exec(gko::ReferenceExecutor::create()),
rcm_factory(reorder_type::build().on(exec))
{}

std::shared_ptr<const gko::Executor> exec;
std::unique_ptr<reorder_type::Factory> rcm_factory;
};

TEST_F(Rcm, RcmFactoryKnowsItsExecutor)
{
ASSERT_EQ(this->rcm_factory->get_executor(), this->exec);
}

} // namespace
1 change: 1 addition & 0 deletions cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ target_sources(ginkgo_cuda
preconditioner/jacobi_generate_kernel.cu
preconditioner/jacobi_kernels.cu
preconditioner/jacobi_simple_apply_kernel.cu
reorder/rcm_kernels.cu
solver/bicg_kernels.cu
solver/bicgstab_kernels.cu
solver/cg_kernels.cu
Expand Down
Loading

0 comments on commit a0e1050

Please sign in to comment.