Skip to content

Commit

Permalink
add ResidualCheckNumCheck
Browse files Browse the repository at this point in the history
Co-authored-by: Tobias Ribizel <ribizel@kit.edu>
  • Loading branch information
yhmtsai and upsj committed Apr 7, 2023
1 parent e1b5951 commit 859b657
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion core/test/solver/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#include <ginkgo/core/base/executor.hpp>
#include <ginkgo/core/log/stream.hpp>
#include <ginkgo/core/matrix/dense.hpp>
#include <ginkgo/core/stop/combined.hpp>
#include <ginkgo/core/stop/iteration.hpp>
Expand All @@ -52,6 +53,22 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace {


int count_occurrance(const std::string& str, const std::string& substr)
{
int occurrance = 0;
std::string::size_type pos = 0;
while (pos < str.length()) {
// no overlapped cases
pos = str.find(substr, pos);
if (pos != std::string::npos) {
pos += substr.length();
occurrance++;
}
}
return occurrance;
}


template <typename T>
class Ir : public ::testing::Test {
protected:
Expand All @@ -74,7 +91,7 @@ class Ir : public ::testing::Test {
solver(ir_factory->generate(mtx))
{}

std::shared_ptr<const gko::Executor> exec;
std::shared_ptr<gko::Executor> exec;
std::shared_ptr<Mtx> mtx;
std::shared_ptr<typename Solver::Factory> ir_factory;
std::unique_ptr<gko::LinOp> solver;
Expand Down Expand Up @@ -458,4 +475,25 @@ TYPED_TEST(Ir, SmootherBuildWithFactory)
}


TYPED_TEST(Ir, RunResidualNormCheckCorrectTimes)
{
using value_type = typename TestFixture::value_type;
using Solver = typename TestFixture::Solver;
using Mtx = typename TestFixture::Mtx;
auto b = gko::initialize<Mtx>({2, -1.0, 1.0}, this->exec);
auto x = gko::initialize<Mtx>({0.0, 0.0, 0.0}, this->exec);
std::stringstream out;
auto logger = gko::share(gko::log::Stream<TypeParam>::create(
gko::log::Logger::operation_launched_mask, out));
this->exec->add_logger(logger);

// solver reaches the iteration limit
this->solver->apply(b, x);

// Contains make_residual_norm 3 times: check in initialization and two
// iterations. The last iteration exits due to iteration limit.
ASSERT_EQ(count_occurrance(out.str(), "make_residual_norm"), 3);
}


} // namespace

0 comments on commit 859b657

Please sign in to comment.