Skip to content

Commit

Permalink
Add conversions to Sparsity matrix format from CSR for all execs.
Browse files Browse the repository at this point in the history
  • Loading branch information
pratikvn committed Sep 12, 2019
1 parent d2e0dd5 commit a8d9b40
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 4 deletions.
21 changes: 21 additions & 0 deletions core/matrix/csr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ginkgo/core/matrix/dense.hpp>
#include <ginkgo/core/matrix/ell.hpp>
#include <ginkgo/core/matrix/sellp.hpp>
#include <ginkgo/core/matrix/sparsity.hpp>


#include "core/matrix/csr_kernels.hpp"
Expand Down Expand Up @@ -187,6 +188,26 @@ void Csr<ValueType, IndexType>::move_to(Sellp<ValueType, IndexType> *result)
}


template <typename ValueType, typename IndexType>
void Csr<ValueType, IndexType>::convert_to(
Sparsity<ValueType, IndexType> *result) const
{
auto exec = this->get_executor();
auto tmp = Sparsity<ValueType, IndexType>::create(
exec, this->get_size(), this->get_num_stored_elements());
tmp->col_idxs_ = this->col_idxs_;
tmp->row_ptrs_ = this->row_ptrs_;
tmp->move_to(result);
}


template <typename ValueType, typename IndexType>
void Csr<ValueType, IndexType>::move_to(Sparsity<ValueType, IndexType> *result)
{
this->convert_to(result);
}


template <typename ValueType, typename IndexType>
void Csr<ValueType, IndexType>::convert_to(
Ell<ValueType, IndexType> *result) const
Expand Down
1 change: 1 addition & 0 deletions core/matrix/csr_kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ginkgo/core/matrix/ell.hpp>
#include <ginkgo/core/matrix/hybrid.hpp>
#include <ginkgo/core/matrix/sellp.hpp>
#include <ginkgo/core/matrix/sparsity.hpp>


namespace gko {
Expand Down
26 changes: 26 additions & 0 deletions cuda/test/matrix/csr_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ginkgo/core/matrix/ell.hpp>
#include <ginkgo/core/matrix/hybrid.hpp>
#include <ginkgo/core/matrix/sellp.hpp>
#include <ginkgo/core/matrix/sparsity.hpp>


#include "core/test/utils.hpp"
Expand Down Expand Up @@ -388,6 +389,31 @@ TEST_F(Csr, MoveToEllIsEquivalentToRef)
GKO_ASSERT_MTX_NEAR(ell_mtx.get(), dell_mtx.get(), 1e-14);
}

TEST_F(Csr, ConvertToSparsityIsEquivalentToRef)
{
set_up_apply_data(std::make_shared<Mtx::cusparse>());
auto sparsity_mtx = gko::matrix::Sparsity<>::create(ref);
auto d_sparsity_mtx = gko::matrix::Sparsity<>::create(cuda);

mtx->convert_to(sparsity_mtx.get());
dmtx->convert_to(d_sparsity_mtx.get());

GKO_ASSERT_MTX_NEAR(sparsity_mtx.get(), d_sparsity_mtx.get(), 1e-14);
}


TEST_F(Csr, MoveToSparsityIsEquivalentToRef)
{
set_up_apply_data(std::make_shared<Mtx::cusparse>());
auto sparsity_mtx = gko::matrix::Sparsity<>::create(ref);
auto d_sparsity_mtx = gko::matrix::Sparsity<>::create(cuda);

mtx->move_to(sparsity_mtx.get());
dmtx->move_to(d_sparsity_mtx.get());

GKO_ASSERT_MTX_NEAR(sparsity_mtx.get(), d_sparsity_mtx.get(), 1e-14);
}


TEST_F(Csr, CalculateMaxNnzPerRowIsEquivalentToRef)
{
Expand Down
9 changes: 9 additions & 0 deletions include/ginkgo/core/matrix/csr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class Hybrid;
template <typename ValueType, typename IndexType>
class Sellp;

template <typename ValueType, typename IndexType>
class Sparsity;


/**
* CSR is a matrix format which stores only the nonzero coefficients by
Expand All @@ -82,6 +85,7 @@ class Csr : public EnableLinOp<Csr<ValueType, IndexType>>,
public ConvertibleTo<Ell<ValueType, IndexType>>,
public ConvertibleTo<Hybrid<ValueType, IndexType>>,
public ConvertibleTo<Sellp<ValueType, IndexType>>,
public ConvertibleTo<Sparsity<ValueType, IndexType>>,
public ReadableFromMatrixData<ValueType, IndexType>,
public WritableToMatrixData<ValueType, IndexType>,
public Transposable {
Expand All @@ -92,6 +96,7 @@ class Csr : public EnableLinOp<Csr<ValueType, IndexType>>,
friend class Ell<ValueType, IndexType>;
friend class Hybrid<ValueType, IndexType>;
friend class Sellp<ValueType, IndexType>;
friend class Sparsity<ValueType, IndexType>;

public:
using EnableLinOp<Csr>::convert_to;
Expand Down Expand Up @@ -299,6 +304,10 @@ class Csr : public EnableLinOp<Csr<ValueType, IndexType>>,

void move_to(Sellp<ValueType, IndexType> *result) override;

void convert_to(Sparsity<ValueType, IndexType> *result) const override;

void move_to(Sparsity<ValueType, IndexType> *result) override;

void read(const mat_data &data) override;

void write(mat_data &data) const override;
Expand Down
5 changes: 5 additions & 0 deletions include/ginkgo/core/matrix/sparsity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ namespace gko {
namespace matrix {


template <typename ValueType, typename IndexType>
class Csr;


/**
* SPARSITY is a matrix format which stores only the sparsity pattern of a
* sparse matrix by compressing each row of the matrix (compressed sparse row
Expand All @@ -67,6 +71,7 @@ class Sparsity : public EnableLinOp<Sparsity<ValueType, IndexType>>,
public Transposable {
friend class EnableCreateMethod<Sparsity>;
friend class EnablePolymorphicObject<Sparsity, LinOp>;
friend class Csr<ValueType, IndexType>;

public:
using EnableLinOp<Sparsity>::convert_to;
Expand Down
35 changes: 31 additions & 4 deletions omp/test/matrix/csr_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ginkgo/core/matrix/coo.hpp>
#include <ginkgo/core/matrix/csr.hpp>
#include <ginkgo/core/matrix/dense.hpp>
#include <ginkgo/core/matrix/sparsity.hpp>


#include "core/test/utils.hpp"
Expand Down Expand Up @@ -266,10 +267,10 @@ TEST_F(Csr, ConvertToDenseIsEquivalentToRef)
auto dense_mtx = gko::matrix::Dense<>::create(ref);
auto ddense_mtx = gko::matrix::Dense<>::create(omp);

mtx->convert_to(ddense_mtx.get());
mtx->convert_to(dense_mtx.get());
dmtx->convert_to(ddense_mtx.get());

GKO_ASSERT_MTX_NEAR(dense_mtx.get(), dense_mtx.get(), 1e-14);
GKO_ASSERT_MTX_NEAR(ddense_mtx.get(), dense_mtx.get(), 1e-14);
}


Expand All @@ -279,10 +280,36 @@ TEST_F(Csr, MoveToDenseIsEquivalentToRef)
auto dense_mtx = gko::matrix::Dense<>::create(ref);
auto ddense_mtx = gko::matrix::Dense<>::create(omp);

mtx->move_to(ddense_mtx.get());
mtx->move_to(dense_mtx.get());
dmtx->move_to(ddense_mtx.get());

GKO_ASSERT_MTX_NEAR(dense_mtx.get(), dense_mtx.get(), 1e-14);
GKO_ASSERT_MTX_NEAR(ddense_mtx.get(), dense_mtx.get(), 1e-14);
}


TEST_F(Csr, ConvertToSparsityIsEquivalentToRef)
{
set_up_apply_data();
auto sparsity_mtx = gko::matrix::Sparsity<>::create(ref);
auto d_sparsity_mtx = gko::matrix::Sparsity<>::create(omp);

mtx->convert_to(sparsity_mtx.get());
dmtx->convert_to(d_sparsity_mtx.get());

GKO_ASSERT_MTX_NEAR(d_sparsity_mtx.get(), sparsity_mtx.get(), 1e-14);
}


TEST_F(Csr, MoveToSparsityIsEquivalentToRef)
{
set_up_apply_data();
auto sparsity_mtx = gko::matrix::Sparsity<>::create(ref);
auto d_sparsity_mtx = gko::matrix::Sparsity<>::create(omp);

mtx->move_to(sparsity_mtx.get());
dmtx->move_to(d_sparsity_mtx.get());

GKO_ASSERT_MTX_NEAR(d_sparsity_mtx.get(), sparsity_mtx.get(), 1e-14);
}


Expand Down
40 changes: 40 additions & 0 deletions reference/test/matrix/csr_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ginkgo/core/matrix/ell.hpp>
#include <ginkgo/core/matrix/hybrid.hpp>
#include <ginkgo/core/matrix/sellp.hpp>
#include <ginkgo/core/matrix/sparsity.hpp>


#include "core/test/utils/assertions.hpp"
Expand All @@ -61,6 +62,7 @@ class Csr : public ::testing::Test {
using Coo = gko::matrix::Coo<>;
using Mtx = gko::matrix::Csr<>;
using Sellp = gko::matrix::Sellp<>;
using Sparsity = gko::matrix::Sparsity<>;
using Ell = gko::matrix::Ell<>;
using Hybrid = gko::matrix::Hybrid<>;
using ComplexMtx = gko::matrix::Csr<std::complex<double>>;
Expand Down Expand Up @@ -238,6 +240,22 @@ class Csr : public ::testing::Test {
EXPECT_EQ(v[129], 0.0);
}

void assert_equal_to_mtx(const Sparsity *m)
{
auto *c = m->get_const_col_idxs();
auto *r = m->get_const_row_ptrs();

ASSERT_EQ(m->get_size(), gko::dim<2>(2, 3));
ASSERT_EQ(m->get_num_nonzeros(), 4);
EXPECT_EQ(r[0], 0);
EXPECT_EQ(r[1], 3);
EXPECT_EQ(r[2], 4);
EXPECT_EQ(c[0], 0);
EXPECT_EQ(c[1], 1);
EXPECT_EQ(c[2], 2);
EXPECT_EQ(c[3], 1);
}

void assert_equal_to_mtx(const Ell *m)
{
auto v = m->get_const_values();
Expand Down Expand Up @@ -474,6 +492,28 @@ TEST_F(Csr, MovesToSellp)
}


TEST_F(Csr, ConvertsToSparsity)
{
auto sparsity_mtx = gko::matrix::Sparsity<>::create(mtx->get_executor());

mtx->convert_to(sparsity_mtx.get());

assert_equal_to_mtx(sparsity_mtx.get());
}


TEST_F(Csr, MovesToSparsity)
{
auto sparsity_mtx = gko::matrix::Sparsity<>::create(mtx->get_executor());
auto csr_ref = gko::matrix::Csr<>::create(mtx->get_executor());

csr_ref->copy_from(mtx.get());
csr_ref->move_to(sparsity_mtx.get());

assert_equal_to_mtx(sparsity_mtx.get());
}


TEST_F(Csr, ConvertsToHybridAutomatically)
{
auto hybrid_mtx = gko::matrix::Hybrid<>::create(mtx->get_executor());
Expand Down

0 comments on commit a8d9b40

Please sign in to comment.