Skip to content

Commit

Permalink
implicit tolerance multiplier proposal (#192)
Browse files Browse the repository at this point in the history
* implicit tolerance multiplier
  • Loading branch information
YvanMokwinski authored Feb 25, 2021
1 parent 24fae7c commit e375572
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 27 deletions.
154 changes: 129 additions & 25 deletions clients/common/rocsparse_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ void unit_check_general(int64_t M, int64_t N, int64_t lda, const size_t* hCPU, c
UNIT_CHECK(M, N, lda, hCPU, hGPU, ASSERT_EQ);
}

#define MAX_TOL_MULTIPLIER 4

template <typename T>
void near_check_general_template(rocsparse_int M,
rocsparse_int N,
Expand All @@ -143,6 +145,7 @@ void near_check_general_template(rocsparse_int M,
const T* hGPU,
floating_data_t<T> tol = default_tolerance<T>::value)
{
int tolm = 1;
for(rocsparse_int j = 0; j < N; ++j)
{
for(rocsparse_int i = 0; i < M; ++i)
Expand All @@ -160,20 +163,51 @@ void near_check_general_template(rocsparse_int M,
}
else
{
ASSERT_NEAR(hCPU[i + j * lda], hGPU[i + j * lda], compare_val);
int k;
for(k = 1; k <= MAX_TOL_MULTIPLIER; ++k)
{
if(std::abs(hCPU[i + j * lda] - hGPU[i + j * lda]) <= compare_val * k)
{
break;
}
}

if(k > MAX_TOL_MULTIPLIER)
{
ASSERT_NEAR(hCPU[i + j * lda], hGPU[i + j * lda], compare_val);
}
tolm = std::max(tolm, k);
}
#else
if(std::abs(hCPU[i + j * lda] - hGPU[i + j * lda]) >= compare_val)

int k;
for(k = 1; k <= MAX_TOL_MULTIPLIER; ++k)
{
if(std::abs(hCPU[i + j * lda] - hGPU[i + j * lda]) <= compare_val * k)
{
break;
}
}

if(k > MAX_TOL_MULTIPLIER)
{
std::cerr.precision(12);
std::cerr << "ASSERT_NEAR(" << hCPU[i + j * lda] << ", " << hGPU[i + j * lda]
<< ") failed: " << std::abs(hCPU[i + j * lda] - hGPU[i + j * lda])
<< " exceeds compare_val " << compare_val << std::endl;
<< " exceeds permissive range [" << compare_val << ","
<< compare_val * MAX_TOL_MULTIPLIER << " ]" << std::endl;
exit(EXIT_FAILURE);
}
tolm = std::max(tolm, k);
#endif
}
}

if(tolm > 1)
{
std::cerr << "WARNING near_check has been permissive with a tolerance multiplier equal to "
<< tolm << std::endl;
}
}

template <>
Expand All @@ -184,6 +218,7 @@ void near_check_general_template(rocsparse_int M,
const rocsparse_float_complex* hGPU,
float tol)
{
int tolm = 1;
for(rocsparse_int j = 0; j < N; ++j)
{
for(rocsparse_int i = 0; i < M; ++i)
Expand All @@ -204,28 +239,62 @@ void near_check_general_template(rocsparse_int M,
}
else
{
ASSERT_NEAR(std::real(hCPU[i + j * lda]),
std::real(hGPU[i + j * lda]),
std::real(compare_val));
ASSERT_NEAR(std::imag(hCPU[i + j * lda]),
std::imag(hGPU[i + j * lda]),
std::imag(compare_val));
int k;
for(k = 1; k <= MAX_TOL_MULTIPLIER; ++k)
{
if(std::abs(std::real(hCPU[i + j * lda]) - std::real(hGPU[i + j * lda]))
<= std::real(compare_val) * k
&& std::abs(std::imag(hCPU[i + j * lda]) - std::imag(hGPU[i + j * lda]))
<= std::imag(compare_val) * k)
{
break;
}
}

if(k > MAX_TOL_MULTIPLIER)
{
ASSERT_NEAR(std::real(hCPU[i + j * lda]),
std::real(hGPU[i + j * lda]),
std::real(compare_val));
ASSERT_NEAR(std::imag(hCPU[i + j * lda]),
std::imag(hGPU[i + j * lda]),
std::imag(compare_val));
}
tolm = std::max(tolm, k);
}
#else
if(std::abs(std::real(hCPU[i + j * lda]) - std::real(hGPU[i + j * lda]))
>= std::real(compare_val)
|| std::abs(std::imag(hCPU[i + j * lda]) - std::imag(hGPU[i + j * lda]))
>= std::imag(compare_val))

int k;
for(k = 1; k <= MAX_TOL_MULTIPLIER; ++k)
{
if(std::abs(std::real(hCPU[i + j * lda]) - std::real(hGPU[i + j * lda]))
<= std::real(compare_val) * k
&& std::abs(std::imag(hCPU[i + j * lda]) - std::imag(hGPU[i + j * lda]))
<= std::imag(compare_val) * k)
{
break;
}
}

if(k > MAX_TOL_MULTIPLIER)
{
std::cerr.precision(16);
std::cerr << "ASSERT_NEAR(" << hCPU[i + j * lda] << ", " << hGPU[i + j * lda]
<< ") failed: " << std::abs(hCPU[i + j * lda] - hGPU[i + j * lda])
<< " exceeds compare_val " << compare_val << std::endl;
<< " exceeds permissive range [" << compare_val << ","
<< compare_val * MAX_TOL_MULTIPLIER << " ]" << std::endl;
exit(EXIT_FAILURE);
}
tolm = std::max(tolm, k);
#endif
}
}

if(tolm > 1)
{
std::cerr << "WARNING near_check has been permissive with a tolerance multiplier equal to "
<< tolm << std::endl;
}
}

template <>
Expand All @@ -236,6 +305,7 @@ void near_check_general_template(rocsparse_int M,
const rocsparse_double_complex* hGPU,
double tol)
{
int tolm = 1;
for(rocsparse_int j = 0; j < N; ++j)
{
for(rocsparse_int i = 0; i < M; ++i)
Expand All @@ -256,28 +326,62 @@ void near_check_general_template(rocsparse_int M,
}
else
{
ASSERT_NEAR(std::real(hCPU[i + j * lda]),
std::real(hGPU[i + j * lda]),
std::real(compare_val));
ASSERT_NEAR(std::imag(hCPU[i + j * lda]),
std::imag(hGPU[i + j * lda]),
std::imag(compare_val));
int k;
for(k = 1; k <= MAX_TOL_MULTIPLIER; ++k)
{
if(std::abs(std::real(hCPU[i + j * lda]) - std::real(hGPU[i + j * lda]))
<= std::real(compare_val) * k
&& std::abs(std::imag(hCPU[i + j * lda]) - std::imag(hGPU[i + j * lda]))
<= std::imag(compare_val) * k)
{
break;
}
}

if(k > MAX_TOL_MULTIPLIER)
{
ASSERT_NEAR(std::real(hCPU[i + j * lda]),
std::real(hGPU[i + j * lda]),
std::real(compare_val));
ASSERT_NEAR(std::imag(hCPU[i + j * lda]),
std::imag(hGPU[i + j * lda]),
std::imag(compare_val));
}
tolm = std::max(tolm, k);
}
#else
if(std::abs(std::real(hCPU[i + j * lda]) - std::real(hGPU[i + j * lda]))
>= std::real(compare_val)
|| std::abs(std::imag(hCPU[i + j * lda]) - std::imag(hGPU[i + j * lda]))
>= std::imag(compare_val))

int k;
for(k = 1; k <= MAX_TOL_MULTIPLIER; ++k)
{
if(std::abs(std::real(hCPU[i + j * lda]) - std::real(hGPU[i + j * lda]))
<= std::real(compare_val) * k
&& std::abs(std::imag(hCPU[i + j * lda]) - std::imag(hGPU[i + j * lda]))
<= std::imag(compare_val) * k)
{
break;
}
}

if(k > MAX_TOL_MULTIPLIER)
{
std::cerr.precision(16);
std::cerr << "ASSERT_NEAR(" << hCPU[i + j * lda] << ", " << hGPU[i + j * lda]
<< ") failed: " << std::abs(hCPU[i + j * lda] - hGPU[i + j * lda])
<< " exceeds compare_val " << compare_val << std::endl;
<< " exceeds permissive range [" << compare_val << ","
<< compare_val * MAX_TOL_MULTIPLIER << " ]" << std::endl;
exit(EXIT_FAILURE);
}
tolm = std::max(tolm, k);
#endif
}
}

if(tolm > 1)
{
std::cerr << "WARNING near_check has been permissive with a tolerance multiplier equal to "
<< tolm << std::endl;
}
}

template <typename T>
Expand Down
1 change: 0 additions & 1 deletion clients/tests/test_csrmv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ Tests:
baseA: [rocsparse_index_base_zero, rocsparse_index_base_one]
matrix: [rocsparse_matrix_random]
algo: [0, 1]
tolm: [2]

- name: csrmv_file
category: quick
Expand Down
1 change: 0 additions & 1 deletion clients/tests/test_gebsrmv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ Tests:
baseA: [rocsparse_index_base_zero]
matrix: [rocsparse_matrix_file_rocalution]
filename: [nos7]
tolm: [2]

#
# More tests for code coverage
Expand Down

0 comments on commit e375572

Please sign in to comment.