Skip to content

Commit

Permalink
Try to test all template values for residual_norm_reduction_kernels.
Browse files Browse the repository at this point in the history
  • Loading branch information
tcojean committed Mar 4, 2020
1 parent 5bc28c9 commit a752469
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cmake/CTestScript.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ ctest_build(BUILD "${CTEST_BINARY_DIRECTORY}" APPEND)
ctest_submit(PARTS Build)


if (CTEST_MEMORYCHECK_TYPE STREQUAL "None")
if (CTEST_MEMORYCHECK_TYPE STREQUAL "None" OR CTEST_MEMORYCHECK_TYPE STREQUAL "COVERAGE")
ctest_test(BUILD "${CTEST_BINARY_DIRECTORY}" APPEND)
ctest_submit(PARTS Test)
endif()
Expand Down
74 changes: 47 additions & 27 deletions reference/test/stop/residual_norm_reduction_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,81 +33,98 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ginkgo/core/stop/residual_norm_reduction.hpp>


#include <gtest/gtest.h>
#include <type_traits>


namespace {
#include <gtest/gtest-typed-test.h>
#include <gtest/gtest.h>


constexpr double reduction_factor = 1.0e-14;
namespace {


template <typename T>
class ResidualNormReduction : public ::testing::Test {
protected:
using Mtx = gko::matrix::Dense<>;
using Mtx = gko::matrix::Dense<T>;
static constexpr gko::remove_complex<T> reduction_factor =
std::is_same<gko::remove_complex<T>, float>::value ? 1.0e-7 : 1.0e-14;

ResidualNormReduction()
{
exec_ = gko::ReferenceExecutor::create();
factory_ = gko::stop::ResidualNormReduction<>::build()
factory_ = gko::stop::ResidualNormReduction<T>::build()
.with_reduction_factor(reduction_factor)
.on(exec_);
}

std::unique_ptr<gko::stop::ResidualNormReduction<>::Factory> factory_;
std::unique_ptr<typename gko::stop::ResidualNormReduction<T>::Factory>
factory_;
std::shared_ptr<const gko::Executor> exec_;
};


TEST_F(ResidualNormReduction, CanCreateFactory)
template <typename T>
constexpr gko::remove_complex<T> ResidualNormReduction<T>::reduction_factor;


using ValueTypes =
::testing::Types<float, double, std::complex<float>, std::complex<double>>;
TYPED_TEST_CASE(ResidualNormReduction, ValueTypes);


TYPED_TEST(ResidualNormReduction, CanCreateFactory)
{
ASSERT_NE(factory_, nullptr);
ASSERT_EQ(factory_->get_parameters().reduction_factor, reduction_factor);
ASSERT_EQ(factory_->get_executor(), exec_);
ASSERT_NE(this->factory_, nullptr);
ASSERT_EQ(this->factory_->get_parameters().reduction_factor,
TestFixture::reduction_factor);
ASSERT_EQ(this->factory_->get_executor(), this->exec_);
}


TEST_F(ResidualNormReduction, CannotCreateCriterionWithoutB)
TYPED_TEST(ResidualNormReduction, CannotCreateCriterionWithoutB)
{
ASSERT_THROW(factory_->generate(nullptr, nullptr, nullptr, nullptr),
ASSERT_THROW(this->factory_->generate(nullptr, nullptr, nullptr, nullptr),
gko::NotSupported);
}


TEST_F(ResidualNormReduction, CanCreateCriterionWithB)
TYPED_TEST(ResidualNormReduction, CanCreateCriterionWithB)
{
using Mtx = typename TestFixture::Mtx;
std::shared_ptr<gko::LinOp> scalar =
gko::initialize<gko::matrix::Dense<>>({1.0}, exec_);
gko::initialize<Mtx>({1.0}, this->exec_);
auto criterion =
factory_->generate(nullptr, nullptr, nullptr, scalar.get());
this->factory_->generate(nullptr, nullptr, nullptr, scalar.get());
ASSERT_NE(criterion, nullptr);
}


TEST_F(ResidualNormReduction, WaitsTillResidualGoal)
TYPED_TEST(ResidualNormReduction, WaitsTillResidualGoal)
{
auto scalar = gko::initialize<Mtx>({1.0}, exec_);
using Mtx = typename TestFixture::Mtx;
auto scalar = gko::initialize<Mtx>({1.0}, this->exec_);
auto criterion =
factory_->generate(nullptr, nullptr, nullptr, scalar.get());
this->factory_->generate(nullptr, nullptr, nullptr, scalar.get());
bool one_changed{};
constexpr gko::uint8 RelativeStoppingId{1};
gko::Array<gko::stopping_status> stop_status(exec_, 1);
gko::Array<gko::stopping_status> stop_status(this->exec_, 1);
stop_status.get_data()[0].reset();

ASSERT_FALSE(
criterion->update()
.residual_norm(scalar.get())
.check(RelativeStoppingId, true, &stop_status, &one_changed));

scalar->at(0) = reduction_factor * 1.0e+2;
scalar->at(0) = TestFixture::reduction_factor * 1.0e+2;
ASSERT_FALSE(
criterion->update()
.residual_norm(scalar.get())
.check(RelativeStoppingId, true, &stop_status, &one_changed));
ASSERT_EQ(stop_status.get_data()[0].has_converged(), false);
ASSERT_EQ(one_changed, false);

scalar->at(0) = reduction_factor * 1.0e-2;
scalar->at(0) = TestFixture::reduction_factor * 1.0e-2;
ASSERT_TRUE(
criterion->update()
.residual_norm(scalar.get())
Expand All @@ -117,13 +134,16 @@ TEST_F(ResidualNormReduction, WaitsTillResidualGoal)
}


TEST_F(ResidualNormReduction, WaitsTillResidualGoalMultipleRHS)
TYPED_TEST(ResidualNormReduction, WaitsTillResidualGoalMultipleRHS)
{
auto mtx = gko::initialize<Mtx>({{1.0, 1.0}}, exec_);
auto criterion = factory_->generate(nullptr, nullptr, nullptr, mtx.get());
using Mtx = typename TestFixture::Mtx;
auto one = gko::one<TypeParam>();
auto mtx = gko::initialize<Mtx>({{one, one}}, this->exec_);
auto criterion =
this->factory_->generate(nullptr, nullptr, nullptr, mtx.get());
bool one_changed{};
constexpr gko::uint8 RelativeStoppingId{1};
gko::Array<gko::stopping_status> stop_status(exec_, 2);
gko::Array<gko::stopping_status> stop_status(this->exec_, 2);
// Array only does malloc, it *does not* construct the object
// therefore you get undefined values in your objects whatever you do.
// Proper fix is not easy, we can't just call memset. We can probably not
Expand All @@ -134,14 +154,14 @@ TEST_F(ResidualNormReduction, WaitsTillResidualGoalMultipleRHS)
ASSERT_FALSE(criterion->update().residual_norm(mtx.get()).check(
RelativeStoppingId, true, &stop_status, &one_changed));

mtx->at(0, 0) = reduction_factor * 1.0e-2;
mtx->at(0, 0) = TestFixture::reduction_factor * 1.0e-2;
ASSERT_FALSE(criterion->update().residual_norm(mtx.get()).check(
RelativeStoppingId, true, &stop_status, &one_changed));
ASSERT_EQ(stop_status.get_data()[0].has_converged(), true);
ASSERT_EQ(one_changed, true);
one_changed = false;

mtx->at(0, 1) = reduction_factor * 1.0e-2;
mtx->at(0, 1) = TestFixture::reduction_factor * 1.0e-2;
ASSERT_TRUE(criterion->update().residual_norm(mtx.get()).check(
RelativeStoppingId, true, &stop_status, &one_changed));
ASSERT_EQ(stop_status.get_data()[1].has_converged(), true);
Expand Down

0 comments on commit a752469

Please sign in to comment.