-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87e888d
commit 9293291
Showing
2 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,328 @@ | ||
/*******************************<GINKGO LICENSE>****************************** | ||
Copyright (c) 2017-2023, 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/base/block_operator.hpp> | ||
|
||
|
||
#include <vector> | ||
|
||
|
||
#include <gtest/gtest.h> | ||
|
||
|
||
#include <ginkgo/core/matrix/dense.hpp> | ||
|
||
|
||
#include "core/test/utils.hpp" | ||
|
||
|
||
struct DummyOperator : public gko::EnableLinOp<DummyOperator> { | ||
explicit DummyOperator(std::shared_ptr<const gko::Executor> exec) | ||
: DummyOperator(std::move(exec), {1, 1}) | ||
{} | ||
|
||
explicit DummyOperator(std::shared_ptr<const gko::Executor> exec, | ||
gko::dim<2> size) | ||
: gko::EnableLinOp<DummyOperator>(std::move(exec), size) | ||
{} | ||
|
||
void apply_impl(const LinOp* b, LinOp* x) const override {} | ||
|
||
void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta, | ||
LinOp* x) const override | ||
{} | ||
}; | ||
|
||
|
||
class BlockOperator : public ::testing::Test { | ||
protected: | ||
std::shared_ptr<const gko::Executor> exec = | ||
gko::ReferenceExecutor::create(); | ||
std::vector<std::vector<std::shared_ptr<gko::LinOp>>> operators = { | ||
{std::make_shared<DummyOperator>(exec), | ||
std::make_shared<DummyOperator>(exec)}, | ||
{std::make_shared<DummyOperator>(exec), | ||
std::make_shared<DummyOperator>(exec)}}; | ||
}; | ||
|
||
|
||
template <typename Fn> | ||
void enumerate_for_all_blocks(gko::ptr_param<const gko::BlockOperator> bop, | ||
Fn&& fn) | ||
{ | ||
auto size = bop->get_block_size(); | ||
for (gko::size_type row = 0; row < size[0]; ++row) { | ||
for (gko::size_type col = 0; col < size[1]; ++col) { | ||
fn(row, col); | ||
} | ||
} | ||
} | ||
|
||
|
||
template <typename Fn> | ||
void for_all_blocks(gko::ptr_param<const gko::BlockOperator> bop, Fn&& fn) | ||
{ | ||
enumerate_for_all_blocks( | ||
bop, [&](auto row, auto col) { fn(bop->block_at(row, col)); }); | ||
} | ||
|
||
|
||
TEST_F(BlockOperator, CanBeEmpty) | ||
{ | ||
auto bop = gko::BlockOperator::create(exec); | ||
|
||
ASSERT_EQ(bop->get_executor(), exec); | ||
ASSERT_FALSE(bop->get_size()); | ||
ASSERT_FALSE(bop->get_block_size()); | ||
} | ||
|
||
|
||
TEST_F(BlockOperator, CanBeConstructedFromVectors) | ||
{ | ||
auto bop = gko::BlockOperator::create(exec, operators); | ||
|
||
gko::dim<2> size{2u, 2u}; | ||
GKO_ASSERT_EQUAL_DIMENSIONS(bop->get_block_size(), size); | ||
GKO_ASSERT_EQUAL_DIMENSIONS(bop->get_size(), size); | ||
for_all_blocks(bop, [](auto block) { ASSERT_TRUE(block); }); | ||
} | ||
|
||
|
||
TEST_F(BlockOperator, CanBeConstructedFromInitializerList) | ||
{ | ||
auto bop = gko::BlockOperator::create( | ||
exec, {{std::make_shared<DummyOperator>(exec), | ||
std::make_shared<DummyOperator>(exec)}, | ||
{std::make_shared<DummyOperator>(exec), | ||
std::make_shared<DummyOperator>(exec)}}); | ||
|
||
gko::dim<2> size{2u, 2u}; | ||
GKO_ASSERT_EQUAL_DIMENSIONS(bop->get_block_size(), size); | ||
GKO_ASSERT_EQUAL_DIMENSIONS(bop->get_size(), size); | ||
for_all_blocks(bop, [](auto block) { ASSERT_TRUE(block); }); | ||
} | ||
|
||
|
||
TEST_F(BlockOperator, CanBeConstructedWithNull) | ||
{ | ||
auto bop = gko::BlockOperator::create( | ||
exec, {{std::make_shared<DummyOperator>(exec), nullptr}, | ||
{nullptr, std::make_shared<DummyOperator>(exec)}}); | ||
|
||
gko::dim<2> size{2u, 2u}; | ||
GKO_ASSERT_EQUAL_DIMENSIONS(bop->get_block_size(), size); | ||
GKO_ASSERT_EQUAL_DIMENSIONS(bop->get_size(), size); | ||
ASSERT_TRUE(bop->block_at(0, 0)); | ||
ASSERT_TRUE(bop->block_at(1, 1)); | ||
ASSERT_FALSE(bop->block_at(0, 1)); | ||
ASSERT_FALSE(bop->block_at(1, 0)); | ||
} | ||
|
||
|
||
TEST_F(BlockOperator, ThrowsOnUnequalNumberOfColumns) | ||
{ | ||
ASSERT_THROW(gko::BlockOperator::create( | ||
exec, {{std::make_shared<DummyOperator>(exec), | ||
std::make_shared<DummyOperator>(exec)}, | ||
{std::make_shared<DummyOperator>(exec)}}), | ||
gko::ValueMismatch); | ||
} | ||
|
||
|
||
TEST_F(BlockOperator, ThrowsOnEmptyRow) | ||
{ | ||
ASSERT_THROW(gko::BlockOperator::create( | ||
exec, {{std::make_shared<DummyOperator>(exec), | ||
std::make_shared<DummyOperator>(exec)}, | ||
{nullptr, nullptr}}), | ||
gko::InvalidStateError); | ||
} | ||
|
||
|
||
TEST_F(BlockOperator, ThrowsOnEmptyColumn) | ||
{ | ||
ASSERT_THROW(gko::BlockOperator::create( | ||
exec, {{std::make_shared<DummyOperator>(exec), nullptr}, | ||
{std::make_shared<DummyOperator>(exec), nullptr}}), | ||
gko::InvalidStateError); | ||
} | ||
|
||
|
||
TEST_F(BlockOperator, ThrowsOnNonMatchingBlocks) | ||
{ | ||
ASSERT_THROW( | ||
gko::BlockOperator::create( | ||
exec, | ||
{{nullptr, std::make_shared<DummyOperator>(exec, gko::dim<2>{2, 2}), | ||
std::make_shared<DummyOperator>(exec, gko::dim<2>{1, 2})}, | ||
{std::make_shared<DummyOperator>(exec, gko::dim<2>{2, 2}), nullptr, | ||
std::make_shared<DummyOperator>(exec, gko::dim<2>{2, 2})}}), | ||
gko::DimensionMismatch); | ||
} | ||
|
||
|
||
TEST_F(BlockOperator, CanBeCopied) | ||
{ | ||
using Mtx = gko::matrix::Dense<>; | ||
auto bop = gko::BlockOperator::create( | ||
exec, {{gko::initialize<Mtx>({{1, 2}, {2, 1}}, exec), nullptr}, | ||
{nullptr, gko::initialize<Mtx>({{3, 4}, {4, 3}}, exec)}}); | ||
auto bop_copy = gko::BlockOperator::create(exec); | ||
|
||
bop_copy->copy_from(bop); | ||
|
||
GKO_ASSERT_EQUAL_DIMENSIONS(bop, bop_copy); | ||
GKO_ASSERT_EQUAL_DIMENSIONS(bop->get_block_size(), | ||
bop_copy->get_block_size()); | ||
enumerate_for_all_blocks(bop, [&](auto row, auto col) { | ||
if (bop->block_at(row, col) == nullptr) { | ||
ASSERT_TRUE(bop_copy->block_at(row, col) == nullptr); | ||
} else { | ||
GKO_ASSERT_MTX_NEAR(gko::as<Mtx>(bop->block_at(row, col)), | ||
gko::as<Mtx>(bop_copy->block_at(row, col)), | ||
0.0); | ||
} | ||
}); | ||
} | ||
|
||
|
||
TEST_F(BlockOperator, CanBeMoved) | ||
{ | ||
using Mtx = gko::matrix::Dense<>; | ||
auto bop = gko::BlockOperator::create( | ||
exec, {{gko::initialize<Mtx>({{1, 2}, {2, 1}}, exec), nullptr}, | ||
{nullptr, gko::initialize<Mtx>({{3, 4}, {4, 3}}, exec)}}); | ||
auto bop_copy = gko::clone(bop); | ||
auto bop_move = gko::BlockOperator::create(exec); | ||
|
||
bop_move->move_from(bop); | ||
|
||
GKO_ASSERT_EQUAL_DIMENSIONS(bop_copy, bop_move); | ||
GKO_ASSERT_EQUAL_DIMENSIONS(bop_copy->get_block_size(), | ||
bop_move->get_block_size()); | ||
GKO_ASSERT_EQUAL_DIMENSIONS(bop, gko::dim<2>{}); | ||
GKO_ASSERT_EQUAL_DIMENSIONS(bop->get_block_size(), gko::dim<2>{}); | ||
enumerate_for_all_blocks(bop_copy, [&](auto row, auto col) { | ||
if (bop_copy->block_at(row, col) == nullptr) { | ||
ASSERT_TRUE(bop_move->block_at(row, col) == nullptr); | ||
} else { | ||
GKO_ASSERT_MTX_NEAR(gko::as<Mtx>(bop_copy->block_at(row, col)), | ||
gko::as<Mtx>(bop_move->block_at(row, col)), | ||
0.0); | ||
} | ||
}); | ||
} | ||
|
||
|
||
TEST_F(BlockOperator, CanApply) | ||
{ | ||
using vtype = double; | ||
using Mtx = gko::matrix::Dense<vtype>; | ||
auto bop = gko::BlockOperator::create( | ||
exec, {{gko::initialize<Mtx>({{1, 2}, {2, 1}}, exec), | ||
gko::initialize<Mtx>({{5, 6}, {6, 5}}, exec)}, | ||
{nullptr, gko::initialize<Mtx>({{3, 4}, {4, 3}}, exec)}}); | ||
auto x = gko::initialize<Mtx>({1, 2, 3, 4}, exec); | ||
auto y = Mtx::create_with_config_of(x); | ||
|
||
bop->apply(x, y); | ||
|
||
GKO_ASSERT_MTX_NEAR(y, l(I<vtype>{44, 42, 25, 24}), r<vtype>::value); | ||
} | ||
|
||
|
||
TEST_F(BlockOperator, CanAdvancedApply) | ||
{ | ||
using vtype = double; | ||
using Mtx = gko::matrix::Dense<vtype>; | ||
auto bop = gko::BlockOperator::create( | ||
exec, {{gko::initialize<Mtx>({{1, 2}, {2, 1}}, exec), | ||
gko::initialize<Mtx>({{5, 6}, {6, 5}}, exec)}, | ||
{nullptr, gko::initialize<Mtx>({{3, 4}, {4, 3}}, exec)}}); | ||
auto x = gko::initialize<Mtx>({1, 2, 3, 4}, exec); | ||
auto y = gko::initialize<Mtx>({-4, -3, -2, -1}, exec); | ||
auto alpha = gko::initialize<Mtx>({0.5}, exec); | ||
auto beta = gko::initialize<Mtx>({-1}, exec); | ||
|
||
bop->apply(alpha, x, beta, y); | ||
|
||
GKO_ASSERT_MTX_NEAR(y, l(I<vtype>{26, 24, 14.5, 13}), r<vtype>::value); | ||
} | ||
|
||
|
||
TEST_F(BlockOperator, CanApplyToBlockOperator) | ||
{ | ||
using vtype = double; | ||
using Mtx = gko::matrix::Dense<vtype>; | ||
auto bop = gko::BlockOperator::create( | ||
exec, {{gko::initialize<Mtx>({{1, 2}, {2, 1}}, exec), | ||
gko::initialize<Mtx>({{5, 6}, {6, 5}}, exec)}, | ||
{nullptr, gko::initialize<Mtx>({{3, 4}, {4, 3}}, exec)}}); | ||
auto x = gko::BlockOperator::create(exec, | ||
{{gko::initialize<Mtx>({1, 2}, exec)}, | ||
{gko::initialize<Mtx>({3, 4}, exec)}}); | ||
auto y = gko::clone(x); | ||
|
||
bop->apply(x, y); | ||
|
||
GKO_ASSERT_MTX_NEAR(gko::as<Mtx>(y->block_at(0, 0)), l(I<vtype>{44, 42}), | ||
r<vtype>::value); | ||
GKO_ASSERT_MTX_NEAR(gko::as<Mtx>(y->block_at(1, 0)), l(I<vtype>{25, 24}), | ||
r<vtype>::value); | ||
} | ||
|
||
|
||
TEST_F(BlockOperator, CanAdvancedApplyToBlockOperator) | ||
{ | ||
using vtype = double; | ||
using Mtx = gko::matrix::Dense<vtype>; | ||
auto bop = gko::BlockOperator::create( | ||
exec, {{gko::initialize<Mtx>({{1, 2}, {2, 1}}, exec), | ||
gko::initialize<Mtx>({{5, 6}, {6, 5}}, exec)}, | ||
{nullptr, gko::initialize<Mtx>({{3, 4}, {4, 3}}, exec)}}); | ||
auto x = gko::BlockOperator::create(exec, | ||
{{gko::initialize<Mtx>({1, 2}, exec)}, | ||
{gko::initialize<Mtx>({3, 4}, exec)}}); | ||
auto y = gko::BlockOperator::create( | ||
exec, {{gko::initialize<Mtx>({-4, -3}, exec)}, | ||
{gko::initialize<Mtx>({-2, -1}, exec)}}); | ||
auto alpha = gko::initialize<Mtx>({0.5}, exec); | ||
auto beta = gko::initialize<Mtx>({-1}, exec); | ||
|
||
bop->apply(alpha, x, beta, y); | ||
|
||
|
||
GKO_ASSERT_MTX_NEAR(gko::as<Mtx>(y->block_at(0, 0)), l(I<vtype>{26, 24}), | ||
r<vtype>::value); | ||
GKO_ASSERT_MTX_NEAR(gko::as<Mtx>(y->block_at(1, 0)), l(I<vtype>{14.5, 13}), | ||
r<vtype>::value); | ||
} |