This is an archive of the discontinued LLVM Phabricator instance.

Fix random number generation and floating point comparison in matrix-types-spec.cpp
ClosedPublic

Authored by jsji on Jul 15 2020, 2:36 PM.

Details

Summary

The testsuite is failing on PowerPC buildbots due to https://reviews.llvm.org/D72770.
Looks like we are using wrong type generator for double.

In file included from ...SingleSource/UnitTests/matrix-types-spec.cpp:8:
In file included from /usr/lib/gcc/powerpc64le-linux-gnu/7.3.0/../../../../include/c++/7.3.0/random:49:
In file included from /usr/lib/gcc/powerpc64le-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/random.h:35:
/usr/lib/gcc/powerpc64le-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/uniform_int_dist.h:63:7: error: static_assert failed due to requirement 'std::is_integral<double>::value' "template argument must be an integral type"

static_assert(std::is_integral<_IntType>::value,
^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

...SingleSource/UnitTests/matrix-types-spec.cpp:38:41: note: in instantiation of template class 'std::uniform_int_distribution<double>' requested here

std::uniform_int_distribution<double> distribution(-10.0, 10.0);
                                      ^

...SingleSource/UnitTests/matrix-types-spec.cpp:39:29: error: no member named 'bind' in namespace 'std'; did you mean 'find'?

auto random_double = std::bind(distribution, generator);
                     ~~~~~^~~~
                          find

Event Timeline

jsji created this revision.Jul 15 2020, 2:36 PM
jsji edited the summary of this revision. (Show Details)Jul 15 2020, 2:40 PM
jsji edited the summary of this revision. (Show Details)Jul 15 2020, 2:43 PM
jsji added reviewers: anemet, rjmccall.
fhahn added a comment.Jul 16 2020, 1:41 AM

Thanks for taking a look at the issue!

SingleSource/UnitTests/matrix-types-spec.cpp
90

I think we need 2 version of initRandom, one for floating point and one for integral element types, like in D83692, otherwise we might run into the same issue for integer element types.

For example

Floating point specialization:

template <typename ElementTy, 
          typename std::enable_if_t<std::is_floating_point<ElementTy>::value,
                                    int> = 0>
 void initRandom(ElementTy *A, unsigned Rows, unsigned Cols) {
  std::default_random_engine generator;
  std::uniform_real_distribution<ElementTy> distribution(-10.0, 10.0);
  ....
}

Integer specialization

template <
    typename ElementTy,
    typename std::enable_if_t<std::is_integral<ElementTy>::value, int> = 0>
 void initRandom(ElementTy *A, unsigned Rows, unsigned Cols) {
  std::default_random_engine generator;
  std::uniform_int_distribution<ElementTy> distribution(-10, 10);
  ....
}
jsji updated this revision to Diff 278476.Jul 16 2020, 7:38 AM
jsji edited the summary of this revision. (Show Details)
  • Add int specification version

    This will pass compilation, but runtime will still fail on PowerPC.

    The problem is we are using EXPECT_MATRIX_EQ to do binary comparision of floating point values before and afer multiplication. And there might be small differences but within tolerance allowance.

    I think we should output the data, fpcmp-target will handle the compare and tolerance.
jsji marked an inline comment as done.Jul 16 2020, 7:38 AM
fhahn added a comment.Jul 16 2020, 7:46 AM
  • Add int specification version

    This will pass compilation, but runtime will still fail on PowerPC.

    The problem is we are using EXPECT_MATRIX_EQ to do binary comparision of floating point values before and afer multiplication. And there might be small differences but within tolerance allowance.

    I think we should output the data, fpcmp-target will handle the compare and tolerance.

Oh right I see. I thought that the low input range would make that unlikely, but it would probably be best to check that the difference of both values is below a certain epsilon value. I think this could easily be done in the EXPECT_MATRIX_EQ macro, without needing to dump the matrixes and generate a large comparison file. I think it is an advantage to have a self-checking binary here, without the need of updating/maintaining a comparison file. What do you think?

jsji updated this revision to Diff 278492.Jul 16 2020, 8:20 AM
  • Use fpcmp to do floating point comparision

I copied the comparision code from fpcmp.c.

jsji updated this revision to Diff 278493.Jul 16 2020, 8:22 AM
  • Rename the function name
jsji added a comment.Jul 16 2020, 8:30 AM

With this, the case is passing on PowerPC. @fhahn Can you have a look and also try on your platform? Thanks.

fhahn accepted this revision.Jul 16 2020, 8:55 AM

LGTM, thank you very much! Looks good on both X86 and AArch64. The commit message could potentially be a bit more descriptive, .e.g something like Fix random number generation in matrix-types-spec.cpp. I don't think you need to include [test-suite], as the code lives in the llvm-test-suite repo, so the tag would be redundant.

This revision is now accepted and ready to land.Jul 16 2020, 8:55 AM
jsji retitled this revision from [test-suite] Fix random generator to Fix random number generation and floating point comparison in matrix-types-spec.cpp .Jul 16 2020, 9:01 AM
jsji closed this revision.Jul 16 2020, 9:08 AM