Skip to content
Open
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
68 changes: 68 additions & 0 deletions core/test/base/mtx_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,39 @@ TYPED_TEST(RealDummyLinOpTest, WritesLinOpToStreamDefault)
}


TYPED_TEST(RealDummyLinOpTest, WritesLinOpToStreamDefaultFromLinOpPtr)
{
using value_type = typename TestFixture::value_type;
using index_type = typename TestFixture::index_type;
std::istringstream iss(
"%%MatrixMarket matrix array real general\n"
"2 3\n"
"1.0\n"
"0.0\n"
"3.0\n"
"5.0\n"
"2.0\n"
"0.0\n");
std::unique_ptr<gko::LinOp> lin_op =
gko::read<DummyLinOp<value_type, index_type>>(
iss, gko::ReferenceExecutor::create());
std::ostringstream oss{};
std::ostringstream oss_const{};

gko::as<gko::WritableToStream>(lin_op.get())->write(oss);
auto layout =
gko::as<gko::WritableToStream>(lin_op.get())->get_default_layout();
auto const_lin_op = std::unique_ptr<const gko::LinOp>(std::move(lin_op));
gko::as<const gko::WritableToStream>(const_lin_op.get())->write(oss_const);

ASSERT_EQ(layout, gko::layout_type::coordinate);
ASSERT_EQ(oss.str(),
"%%MatrixMarket matrix coordinate real general\n2 3 6\n1 1 1\n1 "
"2 3\n1 3 2\n2 1 0\n2 2 5\n2 3 0\n");
ASSERT_EQ(oss_const.str(), oss.str());
}


TYPED_TEST(RealDummyLinOpTest, WritesAndReadsBinaryLinOpToStreamArray)
{
using value_type = typename TestFixture::value_type;
Expand Down Expand Up @@ -1225,6 +1258,41 @@ TYPED_TEST(DenseTest, WritesToStreamDefault)
}


TYPED_TEST(DenseTest, WritesToStreamDefaultFromLinOpPtr)
{
using value_type = typename TestFixture::value_type;
using index_type = typename TestFixture::index_type;
std::istringstream iss(
"%%MatrixMarket matrix array real general\n"
"2 3\n"
"1.0\n"
"0.0\n"
"3.0\n"
"5.0\n"
"2.0\n"
"0.0\n");
std::unique_ptr<gko::LinOp> lin_op =
gko::read<gko::matrix::Dense<value_type>>(
iss, gko::ReferenceExecutor::create());
std::ostringstream oss{};

gko::as<gko::WritableToStream>(lin_op.get())->write(oss);

ASSERT_EQ(
gko::as<gko::WritableToStream>(lin_op.get())->get_default_layout(),
gko::layout_type::array);
ASSERT_EQ(oss.str(),
"%%MatrixMarket matrix array real general\n"
"2 3\n"
"1\n"
"0\n"
"3\n"
"5\n"
"2\n"
"0\n");
}


template <typename ValueIndexType>
class ComplexDummyLinOpTest : public ::testing::Test {
protected:
Expand Down
39 changes: 37 additions & 2 deletions include/ginkgo/core/base/lin_op.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
//
// SPDX-License-Identifier: BSD-3-Clause

Expand All @@ -17,6 +17,7 @@
#include <ginkgo/core/base/math.hpp>
#include <ginkgo/core/base/matrix_assembly_data.hpp>
#include <ginkgo/core/base/matrix_data.hpp>
#include <ginkgo/core/base/mtx_io.hpp>
#include <ginkgo/core/base/polymorphic_object.hpp>
#include <ginkgo/core/base/types.hpp>
#include <ginkgo/core/base/utils.hpp>
Expand Down Expand Up @@ -650,14 +651,41 @@ class ReadableFromMatrixData {
};


/**
* A LinOp implmenting this interface can write its data to output stream.
*
* @ingroup LinOp
*/
class WritableToStream {
public:
virtual ~WritableToStream() = default;

/**
* Get the default layout type. If it is not override by class, it will be
* coordinate layout
*/
virtual layout_type get_default_layout() const
{
return layout_type::coordinate;
}

/**
* Writes a matrix to output stream
*
* @param os output stream
*/
virtual void write(std::ostream& os) const = 0;
};


/**
* A LinOp implementing this interface can write its data to a matrix_data
* structure.
*
* @ingroup LinOp
*/
template <typename ValueType, typename IndexType>
class WritableToMatrixData {
class WritableToMatrixData : public virtual WritableToStream {
public:
using value_type = ValueType;
using index_type = IndexType;
Expand All @@ -670,6 +698,13 @@ class WritableToMatrixData {
* @param data the matrix_data structure
*/
virtual void write(matrix_data<ValueType, IndexType>& data) const = 0;

void write(std::ostream& os) const override
{
matrix_data<ValueType, IndexType> data;
this->write(data);
write_raw(os, data, this->get_default_layout());
}
};


Expand Down
10 changes: 10 additions & 0 deletions include/ginkgo/core/matrix/dense.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ class Dense

void move_to(Dense<next_precision<ValueType>>* result) override;

layout_type get_default_layout() const override
{
return layout_type::array;
}

void write(std::ostream& os) const override
{
WritableToMatrixData<ValueType, int64>::write(os);
}

#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
friend class Dense<previous_precision<ValueType, 2>>;
using ConvertibleTo<Dense<next_precision<ValueType, 2>>>::convert_to;
Expand Down
5 changes: 5 additions & 0 deletions include/ginkgo/core/matrix/diagonal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ class Diagonal

void move_to(Diagonal<next_precision<ValueType>>* result) override;

void write(std::ostream& os) const override
{
WritableToMatrixData<ValueType, int64>::write(os);
}

#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
friend class Diagonal<previous_precision<ValueType, 2>>;
using ConvertibleTo<Diagonal<next_precision<ValueType, 2>>>::convert_to;
Expand Down
32 changes: 31 additions & 1 deletion include/ginkgo/core/matrix/fft.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
//
// SPDX-License-Identifier: BSD-3-Clause

Expand Down Expand Up @@ -70,6 +70,16 @@ class Fft : public EnableLinOp<Fft>,

void write(matrix_data<std::complex<double>, int64>& data) const override;

layout_type get_default_layout() const override
{
return layout_type::array;
}

void write(std::ostream& os) const override
{
WritableToMatrixData<value_type, int64>::write(os);
}

dim<1> get_fft_size() const;

bool is_inverse() const;
Expand Down Expand Up @@ -168,6 +178,16 @@ class Fft2 : public EnableLinOp<Fft2>,

void write(matrix_data<std::complex<double>, int64>& data) const override;

layout_type get_default_layout() const override
{
return layout_type::array;
}

void write(std::ostream& os) const override
{
WritableToMatrixData<value_type, int64>::write(os);
}

dim<2> get_fft_size() const;

bool is_inverse() const;
Expand Down Expand Up @@ -280,6 +300,16 @@ class Fft3 : public EnableLinOp<Fft3>,

void write(matrix_data<std::complex<double>, int64>& data) const override;

layout_type get_default_layout() const override
{
return layout_type::array;
}

void write(std::ostream& os) const override
{
WritableToMatrixData<value_type, int64>::write(os);
}

dim<3> get_fft_size() const;

bool is_inverse() const;
Expand Down
Loading