Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Floating-point numbers comparison util for tests and benchmarks #141

Merged
merged 64 commits into from
May 15, 2019
Merged
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
d3412c3
Merge branch 'master' into 'master'
Dec 11, 2018
114d42e
Merge branch 'master' into 'master'
Dec 12, 2018
4676449
updating sdk
mehdi-goli Jan 25, 2019
a54c812
removing unused toolchain.
mehdi-goli Jan 25, 2019
26fc8fe
Initial version of SYCL-BLAS library
mehdi-goli Jan 16, 2019
ba6a2a7
Merge branch 'SYCL_LIBRARY-SYCLE-242' into 'master'
mehdi-goli Feb 19, 2019
62c23e3
Moving blas3 backend to bals3 folder.
mehdi-goli Feb 19, 2019
ea8c36e
Merge with upstream gbench
mehdi-goli Feb 19, 2019
f31ed4c
Fixing bench
mehdi-goli Feb 19, 2019
640a72f
Adding batch gemm
mehdi-goli Feb 19, 2019
2b0aaed
Fixing clblast bench
mehdi-goli Feb 19, 2019
344d987
Merge branch 'gbench' into 'master'
mehdi-goli Feb 20, 2019
39da55a
Fixing Cmake; applying the comments
mehdi-goli Feb 20, 2019
4170449
Fixing divergance issue in GEMM.
mehdi-goli Feb 20, 2019
6a6018f
renaming OpenBLAS to SYSTEM_BLAS; updating readme file.
mehdi-goli Feb 21, 2019
4c71fb3
renaming OpenBLAS to SYSTEM_BLAS; updating readme file.
mehdi-goli Feb 21, 2019
4973265
Update README.md
mehdi-goli Feb 21, 2019
3bc01ff
Adding third party variant to the cmake.
mehdi-goli Feb 21, 2019
bb74fa2
Update README.md
mehdi-goli Feb 25, 2019
1305c00
Fixing gemm with local memory when used for batch size.
mehdi-goli Feb 26, 2019
f4960f6
separating the out_block stor
mehdi-goli Feb 26, 2019
5cb99dd
Merge branch 'out_block_Store_separation' into 'master'
mehdi-goli Feb 27, 2019
4742550
Batch gemm optimisation
mehdi-goli Feb 27, 2019
52c6e96
Merge branch 'BatchGEMM_Optimisation' into 'master'
mehdi-goli Feb 27, 2019
22e9ab8
Fixing typo in backend.hpp
mehdi-goli Feb 28, 2019
ca55bf6
Optimising gemm for powerVR.
mehdi-goli Mar 4, 2019
5e6057c
Applying the comments
mehdi-goli Mar 4, 2019
0162308
Merge branch 'OptimisePVRGEMM' into 'master'
mehdi-goli Mar 4, 2019
dc64c06
Updating the library with coding guideline style
mehdi-goli Mar 8, 2019
a6b5ff3
removing temp submodule
mehdi-goli Mar 8, 2019
91ca2cf
fixing the submodule for py_Gen to point to the external git hub
mehdi-goli Mar 8, 2019
43a8e50
updating the backends
mehdi-goli Mar 8, 2019
6468916
merge with the master
mehdi-goli Mar 8, 2019
46bfe0d
final tidy up
mehdi-goli Mar 8, 2019
4eb13c3
Update blas1_copy_test.cpp
mehdi-goli Mar 11, 2019
9a726f6
Synch with upstream
mehdi-goli Mar 15, 2019
e43d503
Merge branch 'synch_with_upstream' into 'master'
mehdi-goli Mar 15, 2019
e673040
Merge with upstream master
mehdi-goli Mar 18, 2019
1052c33
Merge branch 'master' of github.com:codeplaysoftware/sycl-blas
Nyrio Mar 22, 2019
90a180f
Merge branch 'master' of github.com:codeplaysoftware/sycl-blas into HEAD
Nyrio Mar 25, 2019
8f4f880
Updating external
Nyrio Mar 25, 2019
2f69b30
Merge branch 'master' of github.com:codeplaysoftware/sycl-blas into HEAD
Nyrio Mar 25, 2019
75cf99c
Merge branch 'master' of github.com:codeplaysoftware/sycl-blas
Nyrio Apr 3, 2019
33cd86d
Merge branch 'master' of github.com:codeplaysoftware/sycl-blas
Nyrio Apr 18, 2019
34024a8
Merge branch 'master' of github.com:codeplaysoftware/sycl-blas
Nyrio Apr 19, 2019
382c85d
Merge branch 'master' of github.com:codeplaysoftware/sycl-blas
Nyrio Apr 26, 2019
55bf9f5
Updating submodules
Nyrio Apr 26, 2019
cfe3eb1
Rolling back external sdk update
Nyrio May 1, 2019
9d2b50f
Merge branch 'master' of github.com:codeplaysoftware/sycl-blas
Nyrio May 2, 2019
2755cf9
Merge branch 'master' of github.com:codeplaysoftware/sycl-blas
Nyrio May 8, 2019
5b81594
Added a utility to compare floating-point results
Nyrio May 9, 2019
d531c20
Cleanup
Nyrio May 9, 2019
224f380
Merge branch 'master' into float-comparison-util
mehdi-goli May 10, 2019
2f4e1f9
Updated description of the utils
Nyrio May 10, 2019
789a503
Merge branch 'float-comparison-util' of github.com:Nyrio/sycl-blas in…
Nyrio May 10, 2019
a321363
Merge branch 'master' into float-comparison-util
mehdi-goli May 10, 2019
e341683
Merge branch 'master' into float-comparison-util
mehdi-goli May 13, 2019
284d69f
Important fix: was using index 0 instead of i in the comparison
Nyrio May 13, 2019
a5bebd2
Changed the way the comparison is done (ULPs not reliable around zero)
Nyrio May 14, 2019
dc05407
Stricter values for double
Nyrio May 14, 2019
a977a85
Merge branch 'master' into float-comparison-util
mehdi-goli May 14, 2019
3439051
Added comments
Nyrio May 14, 2019
a81e46c
Updated limits, added explanations, better handling of the zero case
Nyrio May 14, 2019
d215cdf
Clang format
Nyrio May 14, 2019
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
139 changes: 139 additions & 0 deletions include/utils/float_comparison.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright 2019 Codeplay Software Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use these files except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* This file provides tools to compare floating-point numbers.
* The function almost_equal returns a boolean indicating if two scalars can
* be considered equal. The function compare_vectors checks if two vectors of
* the same size are almost equal, prints a message on stderr if a value
* mismatch is found, and returns a boolean as well.
* Neither of the methods raises an exception nor calls exit(1)
*/

#ifndef UTILS_FLOAT_COMPARISON_H_
#define UTILS_FLOAT_COMPARISON_H_

#include <cmath>

namespace utils {

/**
* Indicates the tolerated margin for relative differences
*/
template <typename scalar_t>
inline scalar_t getRelativeErrorMargin() {
/* Measured empirically with gemm. The dimensions of the matrices (even k)
* don't seem to have an impact on the observed relative differences
* In the cases where the relative error is relevant (non close to zero),
* relative differences of up to 0.002 were observed for float
*/
return static_cast<scalar_t>(0.005);
}

template <>
inline double getRelativeErrorMargin<double>() {
/* Measured empirically with gemm. The dimensions of the matrices (even k)
* don't seem to have an impact on the observed relative differences
* In the cases where the relative error is relevant (non close to zero),
* relative differences of up to 10^-12 were observed for double
*/
return 0.0000000001; // 10^-10
}

/**
* Indicates the tolerated margin for absolute differences (used in case the
* scalars are close to 0)
*/
template <typename scalar_t>
inline scalar_t getAbsoluteErrorMargin() {
/* Measured empirically with gemm.
* In the cases where the relative error is irrelevant (close to zero),
* absolute differences of up to 0.00008 were observed for float
*/
return 0.0002;
}

template <>
inline double getAbsoluteErrorMargin<double>() {
/* Measured empirically with gemm.
* In the cases where the relative error is irrelevant (close to zero),
* absolute differences of up to 10^-12 were observed for double
*/
return 0.0000000001; // 10^-10
}

/**
* Indicates if a scalar is close to zero
*/
template <typename scalar_t>
inline bool closeToZero(scalar_t scalar) {
// Measured empirically with gemm. The relative difference becomes too big
// for values smaller than this
return (std::fabs(scalar) < 0.01);
}

/**
* Compare two scalars and returns false if the difference is not acceptable.
*/
template <typename scalar_t>
inline bool almost_equal(scalar_t const& scalar1, scalar_t const& scalar2) {
// Shortcut, also handles case where both are zero
if (scalar1 == scalar2) {
return true;
}
// Handle cases where both values are NaN or inf
if ((std::isnan(scalar1) && std::isnan(scalar2)) ||
(std::isinf(scalar1) && std::isinf(scalar2))) {
return true;
}

const auto absolute_diff = std::fabs(scalar1 - scalar2);

// Close to zero, the relative error doesn't work, use absolute error
if (closeToZero(scalar1) || closeToZero(scalar2)) {
return (absolute_diff < getAbsoluteErrorMargin<scalar_t>());
}
// Use relative error
const auto absolute_sum = std::fabs(scalar1) + std::fabs(scalar2);
return (absolute_diff / absolute_sum) < getRelativeErrorMargin<scalar_t>();
}

/**
* Compare two vectors and returns false if the difference is not acceptable.
* The second vector is considered the reference.
*/
template <typename scalar_t>
inline bool compare_vectors(std::vector<scalar_t>& vec,
std::vector<scalar_t>& ref) {
if (vec.size() != ref.size()) {
std::cerr << "Error: tried to compare vectors of different sizes"
<< std::endl;
return false;
}

for (int i = 0; i < vec.size(); ++i) {
if (!almost_equal(vec[i], ref[i])) {
std::cerr << "Value mismatch at index " << i << ": " << vec[i]
<< "; expected " << ref[i] << std::endl;
return false;
}
}
return true;
}

} // namespace utils

#endif // UTILS_FLOAT_COMPARISON_H_