Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CsrBuilder and CooBuilder for intrusive access to matrix arrays #437

Merged
merged 6 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions core/matrix/coo_builder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*******************************<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_MATRIX_COO_BUILDER_HPP_
#define GKO_CORE_MATRIX_COO_BUILDER_HPP_


#include <ginkgo/core/matrix/coo.hpp>


namespace gko {
namespace matrix {


/**
* @internal
*
* Allows intrusive access to the arrays stored within a @ref Coo matrix.
*
* @tparam ValueType the value type of the matrix
* @tparam IndexType the index type of the matrix
*/
template <typename ValueType = default_precision, typename IndexType = int32>
class CooBuilder {
public:
/**
* Returns the row index array of the COO matrix.
*/
Array<IndexType> &get_row_idx_array() { return matrix_->row_idxs_; }

/**
* Returns the column index array of the COO matrix.
*/
Array<IndexType> &get_col_idx_array() { return matrix_->col_idxs_; }

/**
* Returns the value array of the COO matrix.
*/
Array<ValueType> &get_value_array() { return matrix_->values_; }

/**
* Initializes a CooBuilder from an existing COO matrix.
*/
explicit CooBuilder(Coo<ValueType, IndexType> *matrix) : matrix_{matrix} {}

// make this type non-movable
CooBuilder(const CooBuilder &) = delete;
CooBuilder(CooBuilder &&) = delete;
CooBuilder &operator=(const CooBuilder &) = delete;
CooBuilder &operator=(CooBuilder &&) = delete;

private:
Coo<ValueType, IndexType> *matrix_;
};


} // namespace matrix
} // namespace gko

#endif // GKO_CORE_MATRIX_COO_BUILDER_HPP_
26 changes: 5 additions & 21 deletions core/matrix/csr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,8 @@ void Csr<ValueType, IndexType>::apply_impl(const LinOp *b, LinOp *x) const
using TCsr = Csr<ValueType, IndexType>;
if (auto b_csr = dynamic_cast<const TCsr *>(b)) {
// if b is a CSR matrix, we compute a SpGeMM
auto exec = this->get_executor();
Array<IndexType> x_rows(exec);
Array<IndexType> x_cols(exec);
Array<ValueType> x_vals(exec);
auto x_csr = as<TCsr>(x);
this->get_executor()->run(
csr::make_spgemm(this, b_csr, x_rows, x_cols, x_vals));
auto new_x = TCsr::create(x_csr->get_executor(), x->get_size(),
std::move(x_vals), std::move(x_cols),
std::move(x_rows), x_csr->get_strategy());
new_x->move_to(x_csr);
this->get_executor()->run(csr::make_spgemm(this, b_csr, x_csr));
} else {
// otherwise we assume that b is dense and compute a SpMV/SpMM
this->get_executor()->run(
Expand All @@ -115,18 +106,11 @@ void Csr<ValueType, IndexType>::apply_impl(const LinOp *alpha, const LinOp *b,
using TCsr = Csr<ValueType, IndexType>;
if (auto b_csr = dynamic_cast<const TCsr *>(b)) {
// if b is a CSR matrix, we compute a SpGeMM
auto exec = this->get_executor();
Array<IndexType> x_rows(exec);
Array<IndexType> x_cols(exec);
Array<ValueType> x_vals(exec);
auto x_csr = as<TCsr>(x);
this->get_executor()->run(csr::make_advanced_spgemm(
as<Dense>(alpha), this, b_csr, as<Dense>(beta), x_csr, x_rows,
x_cols, x_vals));
auto new_x = TCsr::create(x_csr->get_executor(), x->get_size(),
std::move(x_vals), std::move(x_cols),
std::move(x_rows), x_csr->get_strategy());
new_x->move_to(x_csr);
auto x_copy = x_csr->clone();
this->get_executor()->run(
csr::make_advanced_spgemm(as<Dense>(alpha), this, b_csr,
as<Dense>(beta), x_copy.get(), x_csr));
} else {
// otherwise we assume that b is dense and compute a SpMV/SpMM
this->get_executor()->run(
Expand Down
89 changes: 89 additions & 0 deletions core/matrix/csr_builder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*******************************<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_MATRIX_CSR_BUILDER_HPP_
#define GKO_CORE_MATRIX_CSR_BUILDER_HPP_


#include <ginkgo/core/matrix/csr.hpp>


namespace gko {
namespace matrix {


/**
* @internal
*
* Allows intrusive access to the arrays stored within a @ref Csr matrix.
*
* @tparam ValueType the value type of the matrix
* @tparam IndexType the index type of the matrix
*/
template <typename ValueType = default_precision, typename IndexType = int32>
class CsrBuilder {
public:
/**
* Returns the column index array of the CSR matrix.
*/
Array<IndexType> &get_col_idx_array() { return matrix_->col_idxs_; }

/**
* Returns the value array of the CSR matrix.
*/
Array<ValueType> &get_value_array() { return matrix_->values_; }

/**
* Initializes a CsrBuilder from an existing CSR matrix.
*/
explicit CsrBuilder(Csr<ValueType, IndexType> *matrix) : matrix_{matrix} {}

/**
* Updates the internal matrix data structures at destruction.
*/
~CsrBuilder() { matrix_->make_srow(); }

// make this type non-movable
CsrBuilder(const CsrBuilder &) = delete;
CsrBuilder(CsrBuilder &&) = delete;
CsrBuilder &operator=(const CsrBuilder &) = delete;
CsrBuilder &operator=(CsrBuilder &&) = delete;

private:
Csr<ValueType, IndexType> *matrix_;
};


} // namespace matrix
} // namespace gko

#endif // GKO_CORE_MATRIX_CSR_BUILDER_HPP_
15 changes: 6 additions & 9 deletions core/matrix/csr_kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@ namespace kernels {
const matrix::Dense<ValueType> *beta, \
matrix::Dense<ValueType> *c)

#define GKO_DECLARE_CSR_SPGEMM_KERNEL(ValueType, IndexType) \
void spgemm(std::shared_ptr<const DefaultExecutor> exec, \
const matrix::Csr<ValueType, IndexType> *a, \
const matrix::Csr<ValueType, IndexType> *b, \
Array<IndexType> &c_row_ptrs, Array<IndexType> &c_col_idxs, \
Array<ValueType> &c_vals)
#define GKO_DECLARE_CSR_SPGEMM_KERNEL(ValueType, IndexType) \
void spgemm(std::shared_ptr<const DefaultExecutor> exec, \
const matrix::Csr<ValueType, IndexType> *a, \
const matrix::Csr<ValueType, IndexType> *b, \
matrix::Csr<ValueType, IndexType> *c)

#define GKO_DECLARE_CSR_ADVANCED_SPGEMM_KERNEL(ValueType, IndexType) \
void advanced_spgemm(std::shared_ptr<const DefaultExecutor> exec, \
Expand All @@ -76,9 +75,7 @@ namespace kernels {
const matrix::Csr<ValueType, IndexType> *b, \
const matrix::Dense<ValueType> *beta, \
const matrix::Csr<ValueType, IndexType> *d, \
Array<IndexType> &c_row_ptrs, \
Array<IndexType> &c_col_idxs, \
Array<ValueType> &c_vals)
matrix::Csr<ValueType, IndexType> *c)

#define GKO_DECLARE_CSR_CONVERT_TO_DENSE_KERNEL(ValueType, IndexType) \
void convert_to_dense(std::shared_ptr<const DefaultExecutor> exec, \
Expand Down
2 changes: 2 additions & 0 deletions core/test/matrix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ ginkgo_create_test(identity)
ginkgo_create_test(permutation)
ginkgo_create_test(sellp)
ginkgo_create_test(sparsity_csr)
ginkgo_create_test(csr_builder)
ginkgo_create_test(coo_builder)
76 changes: 76 additions & 0 deletions core/test/matrix/coo_builder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*******************************<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 "core/matrix/coo_builder.hpp"


#include <memory>


#include <gtest/gtest.h>


namespace {


class CooBuilder : public ::testing::Test {
protected:
using Mtx = gko::matrix::Coo<>;

CooBuilder()
: exec(gko::ReferenceExecutor::create()),
mtx(Mtx::create(exec, gko::dim<2>{2, 3}, 4))
{}

std::shared_ptr<const gko::Executor> exec;
std::unique_ptr<Mtx> mtx;
};


TEST_F(CooBuilder, ReturnsCorrectArrays)
{
gko::matrix::CooBuilder<> builder{mtx.get()};

auto builder_row_idxs = builder.get_row_idx_array().get_data();
auto builder_col_idxs = builder.get_col_idx_array().get_data();
auto builder_values = builder.get_value_array().get_data();
auto ref_row_idxs = mtx->get_row_idxs();
auto ref_col_idxs = mtx->get_col_idxs();
auto ref_values = mtx->get_values();

ASSERT_EQ(builder_row_idxs, ref_row_idxs);
ASSERT_EQ(builder_col_idxs, ref_col_idxs);
ASSERT_EQ(builder_values, ref_values);
}


} // namespace
Loading