Skip to content

Commit

Permalink
Flexible error metric (#21)
Browse files Browse the repository at this point in the history
Allows to use a user-defined error metric.

The old hard-coded error_metric is replaced by an interface but shipped as the default metric.
  • Loading branch information
havogt authored Feb 23, 2018
1 parent 4a70be9 commit a152251
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ project(gridtools-verification CXX C)

include(ExternalProject)

set(GRIDTOOLS_VERIFICATION_VERSION_STRING "0.3")
set(GRIDTOOLS_VERIFICATION_VERSION_STRING "0.4")
set(SERIALBOX_VERSION_REQUIRED "2.2.1")

#----------------- CMake options
Expand Down
5 changes: 3 additions & 2 deletions src/verification/error_metric.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include <cmath>
#include "../common.h"
#include "error_metric_interface.h"

namespace gt_verification {

Expand All @@ -54,7 +55,7 @@ namespace gt_verification {
* @ingroup DycoreUnittestVerificationLibrary
*/
template < typename T >
class error_metric {
class error_metric : public error_metric_interface< T > {
public:
error_metric(const error_metric &) = default;
error_metric &operator=(const error_metric &) = default;
Expand All @@ -72,7 +73,7 @@ namespace gt_verification {
*
* @return true iff absolute(a - b) <= (atol + rtol * absolute(b))
*/
inline bool equal(T a, T b) const noexcept { return (std::fabs(a - b) <= (atol_ + rtol_ * std::fabs(b))); }
bool equal(T a, T b) const noexcept override { return (std::fabs(a - b) <= (atol_ + rtol_ * std::fabs(b))); }

private:
T rtol_;
Expand Down
51 changes: 51 additions & 0 deletions src/verification/error_metric_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
GridTools Libraries
Copyright (c) 2016, GridTools Consortium
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.
For information: http://eth-cscs.github.io/gridtools/
*/
#pragma once

#include <cmath>
#include "../common.h"

namespace gt_verification {

template < typename T >
class error_metric_interface {
public:
/**
* @brief Check if two real numbers @c a and @c b are equal within a tolerance
*/
virtual bool equal(T a, T b) const noexcept = 0;
};
}
8 changes: 4 additions & 4 deletions src/verification/field_collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#include "../core/logger.h"
#include "../verification_exception.h"
#include "verification_reporter.h"
#include "error_metric.h"
#include "error_metric_interface.h"
#include "boundary_extent.h"
#include "verification.h"
#include "verification_result.h"
Expand Down Expand Up @@ -206,18 +206,18 @@ namespace gt_verification {
*
* @return VerificationResult
*/
verification_result verify(error_metric< T > errorMetric) {
verification_result verify(const error_metric_interface< T > &error_metric) {
verifications_.clear();

verification_result totalResult(true, "\n");

// Iterate over output fields and compare them to the reference fields
for (std::size_t i = 0; i < outputFields_.size(); ++i) {
verifications_.emplace_back(
outputFields_[i].second, referenceFields_[i].second.to_view(), errorMetric, boundaries_[i]);
outputFields_[i].second, referenceFields_[i].second.to_view(), boundaries_[i]);

// Perform actual verification and merge results
totalResult.merge(verifications_.back().verify());
totalResult.merge(verifications_.back().verify(error_metric));
}
return totalResult;
}
Expand Down
9 changes: 6 additions & 3 deletions src/verification/unittest_environment.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "field_collection.h"
#include "error_metric.h"
#include "../core.h"
#include <gtest/gtest.h>
#include <string>
Expand All @@ -16,14 +17,16 @@ namespace gt_verification {
private boost::noncopyable /* singleton */
{
public:
unittest_environment(command_line &cl, std::string data_name, std::string archive_type="Binary") : cl_(cl), data_path_("./") {
unittest_environment(command_line &cl, std::string data_name, std::string archive_type = "Binary")
: cl_(cl), data_path_("./") {
if (cl_.has("path"))
data_path_ = cl_.as< std::string >("path");

VERIFICATION_LOG() << "Using serializer data-path: '" << data_path_ << "'" << logger_action::endl;

// Initialize the serializer
reference_serializer_ = std::make_shared< ser::serializer >(ser::open_mode::Read, data_path_, data_name, archive_type);
reference_serializer_ =
std::make_shared< ser::serializer >(ser::open_mode::Read, data_path_, data_name, archive_type);

// Initialize error serializer
error_serializer_ = std::make_shared< ser::serializer >(ser::open_mode::Write, ".", "Error");
Expand Down Expand Up @@ -125,7 +128,7 @@ namespace gt_verification {
*/
template < typename T >
testing::AssertionResult verify_collection(
field_collection< T > &fieldCollection, error_metric< T > errorMetric) {
field_collection< T > &fieldCollection, const error_metric_interface< T > &errorMetric) {
verification_result result = fieldCollection.verify(errorMetric);
if (!result.passed())
fieldCollection.report_failures();
Expand Down
9 changes: 3 additions & 6 deletions src/verification/verification.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ namespace gt_verification {
*/
verification(type_erased_field_view< T > outputField,
type_erased_field_view< T > referenceField,
error_metric< T > errorMetric,
boundary_extent boundary = boundary_extent())
: outputField_(outputField), referenceField_(referenceField), errorMetric_(errorMetric),
boundary_(boundary) {}
: outputField_(outputField), referenceField_(referenceField), boundary_(boundary) {}

/**
* @brief Verify that outputField is equal to refrenceField within the given error metric
Expand All @@ -98,7 +96,7 @@ namespace gt_verification {
*
* @return VerificationResult
*/
verification_result verify() noexcept {
verification_result verify(const error_metric_interface< T > &error_metric) noexcept {
// Sync fields with Host
outputField_.sync();
referenceField_.sync();
Expand Down Expand Up @@ -130,7 +128,7 @@ namespace gt_verification {
for (int k = boundary_.k_minus(); k < (kSizeOut + boundary_.k_plus()); ++k)
for (int j = boundary_.j_minus(); j < (jSizeOut + boundary_.j_plus()); ++j)
for (int i = boundary_.i_minus(); i < (iSizeOut + boundary_.i_plus()); ++i)
if (!errorMetric_.equal(outputField_(i, j, k), referenceField_(i, j, k)))
if (!error_metric.equal(outputField_(i, j, k), referenceField_(i, j, k)))
failures_.push_back(failure{i, j, k, outputField_(i, j, k), referenceField_(i, j, k)});

if (failures_.empty())
Expand Down Expand Up @@ -184,7 +182,6 @@ namespace gt_verification {
private:
type_erased_field_view< T > outputField_;
type_erased_field_view< T > referenceField_;
error_metric< T > errorMetric_;
boundary_extent boundary_;

std::vector< failure > failures_;
Expand Down
1 change: 0 additions & 1 deletion src/verification/verification_reporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
#include "../core/command_line.h"
#include "../core/error.h"
#include "../core/utility.h"
#include "error_metric.h"

namespace gt_verification {

Expand Down
1 change: 0 additions & 1 deletion src/verification/verification_specification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include "../core/error.h"
#include "../core/utility.h"
#include "../core/logger.h"
#include "error_metric.h"
#include "verification_specification.h"
#include <cstdlib>
#include <string>
Expand Down
8 changes: 4 additions & 4 deletions unittest/verification/test_verification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class test_Verification : public ::testing::Test {
TEST_F(test_Verification, UnmodifiedFieldShouldPass) {
error_metric< Real > errorMetric(1e-6, 1e-8);

verification< Real > test(outView, refView, errorMetric);
ASSERT_TRUE(test.verify().passed());
verification< Real > test(outView, refView);
ASSERT_TRUE(test.verify(errorMetric).passed());
}

TEST_F(test_Verification, ModifiedFieldShouldFail) {
Expand All @@ -87,8 +87,8 @@ TEST_F(test_Verification, ModifiedFieldShouldFail) {
outField.h2d_update();
#endif

verification< Real > test(outView, refView, errorMetric);
ASSERT_FALSE(test.verify().passed());
verification< Real > test(outView, refView);
ASSERT_FALSE(test.verify(errorMetric).passed());
}

#endif

0 comments on commit a152251

Please sign in to comment.