blob: b66cbb1640ad2becd4c687f048b0a3d772bfd6c8 [file] [edit]
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2015 Gael Guennebaud <gael.guennebaud@inria.fr>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// SPDX-License-Identifier: MPL-2.0
#include <cstdlib>
#include <atomic>
#include <thread>
#include <vector>
#include "main.h"
#include <Eigen/Core>
#include "SafeScalar.h"
// SafeScalar<T> is used to simulate custom Scalar types, which use a more generalized approach to generate random
// numbers
// For GCC-6, if this function is inlined then there is an optimization
// bug that triggers a failure. This failure goes away if you access `r` in
// in any way, and for any other compiler.
template <typename Scalar>
EIGEN_DONT_INLINE Scalar check_in_range(Scalar x, Scalar y) {
Scalar r = internal::random<Scalar>(x, y);
VERIFY(r >= x);
if (y >= x) {
VERIFY(r <= y);
}
return r;
}
template <typename Scalar>
void check_all_in_range(Scalar x, Scalar y) {
constexpr int repeats = 32;
Index count = static_cast<Index>(y) - static_cast<Index>(x) + 1;
ArrayX<bool> mask(count);
mask.setConstant(false);
for (Index k = 0; k < count; k++)
for (int repeat = 0; repeat < repeats; repeat++) {
Scalar r = check_in_range(x, y);
Index i = static_cast<Index>(r) - static_cast<Index>(x);
mask(i) = true;
}
for (Index i = 0; i < mask.size(); ++i)
if (mask(i) == false) std::cout << "WARNING: value " << x + i << " not reached." << std::endl;
VERIFY(mask.cwiseEqual(true).all());
}
template <typename Scalar>
void check_all_in_range() {
const Scalar x = NumTraits<Scalar>::lowest();
const Scalar y = NumTraits<Scalar>::highest();
check_all_in_range(x, y);
}
template <typename Scalar, typename EnableIf = void>
class HistogramHelper {
public:
HistogramHelper(int nbins) : HistogramHelper(Scalar(-1), Scalar(1), nbins) {}
HistogramHelper(Scalar lower, Scalar upper, int nbins) {
lower_ = static_cast<double>(lower);
upper_ = static_cast<double>(upper);
num_bins_ = nbins;
bin_width_ = (upper_ - lower_) / static_cast<double>(nbins);
}
int bin(Scalar v) {
double result = (static_cast<double>(v) - lower_) / bin_width_;
return std::min<int>(static_cast<int>(result), num_bins_ - 1);
}
double uniform_bin_probability(int bin) {
double range = upper_ - lower_;
if (bin < num_bins_ - 1) {
return bin_width_ / range;
}
return (upper_ - (lower_ + double(bin) * bin_width_)) / range;
}
private:
double lower_;
double upper_;
int num_bins_;
double bin_width_;
};
// helper class to avoid extending std:: namespace
template <typename T>
struct get_range_type {
using type = std::conditional_t<(sizeof(T) < sizeof(int)), unsigned int, std::make_unsigned_t<T>>;
};
template <typename T>
struct get_range_type<SafeScalar<T>> : std::make_unsigned<T> {};
template <typename Scalar>
class HistogramHelper<Scalar, std::enable_if_t<Eigen::NumTraits<Scalar>::IsInteger>> {
public:
using RangeType = typename get_range_type<Scalar>::type;
HistogramHelper(int nbins)
: HistogramHelper(Eigen::NumTraits<Scalar>::lowest(), Eigen::NumTraits<Scalar>::highest(), nbins) {}
HistogramHelper(Scalar lower, Scalar upper, int nbins)
: lower_{lower}, upper_{upper}, num_bins_{nbins}, bin_width_{bin_width(lower, upper, nbins)} {}
int bin(Scalar v) { return static_cast<int>(RangeType(RangeType(v) - RangeType(lower_)) / bin_width_); }
double uniform_bin_probability(int bin) {
// The full range upper - lower + 1 might overflow the RangeType by one.
// So instead, we know we have (nbins - 1) bins of width bin_width_,
// and the last bin of width:
RangeType last_bin_width =
RangeType(upper_) - (RangeType(lower_) + RangeType(num_bins_ - 1) * bin_width_) + RangeType(1);
double last_bin_ratio = static_cast<double>(last_bin_width) / static_cast<double>(bin_width_);
// Total probability = (nbins - 1) * p + last_bin_ratio * p = 1.0
// p = 1.0 / (nbins - 1 + last_bin_ratio)
double p = 1.0 / (last_bin_ratio + num_bins_ - 1);
if (bin < num_bins_ - 1) {
return p;
}
return last_bin_ratio * p;
}
private:
static constexpr RangeType bin_width(Scalar lower, Scalar upper, int nbins) {
// Avoid overflow in computing the full range.
// floor( (upper - lower + 1) / nbins) )
// = floor( (upper- nbins - lower + 1 + nbins) / nbins) )
return RangeType(RangeType(upper - nbins) - RangeType(lower) + 1) / nbins + 1;
}
Scalar lower_;
Scalar upper_;
int num_bins_;
RangeType bin_width_;
};
// A bin of probability p receives Binomial(count, p) samples, so the normalized hist(i) has
// standard deviation sqrt((1 - p) / (count * p)) about 1. Bonferroni over the bins: k solving
// erfc(k / sqrt(2)) = kFalseFailureRate / bins caps at kFalseFailureRate the probability that
// any bin strays past k sigma.
inline double histogram_k_sigma(int bins) {
constexpr double kFalseFailureRate = 1e-9;
const double target = kFalseFailureRate / double(bins);
double lo = 0.0, hi = 40.0;
for (int iter = 0; iter < 100; ++iter) {
const double mid = 0.5 * (lo + hi);
if (std::erfc(mid / std::sqrt(2.0)) > target)
lo = mid;
else
hi = mid;
}
return hi;
}
template <typename Scalar>
void check_histogram(Scalar x, Scalar y, int bins) {
constexpr int repeats = 10000;
double count = double(bins) * double(repeats);
Eigen::VectorXd hist = Eigen::VectorXd::Zero(bins);
HistogramHelper<Scalar> hist_helper(x, y, bins);
for (int k = 0; k < bins; k++)
for (int repeat = 0; repeat < repeats; repeat++) {
Scalar r = check_in_range(x, y);
int bin = hist_helper.bin(r);
hist(bin)++;
}
// Normalize bins by probability, and bound each by its own sampling standard deviation.
hist /= count;
const double k = histogram_k_sigma(bins);
Eigen::VectorXd tol(bins);
for (int i = 0; i < bins; ++i) {
const double p = hist_helper.uniform_bin_probability(i);
hist(i) = hist(i) / p;
tol(i) = k * std::sqrt((1.0 - p) / (count * p));
}
VERIFY(((hist.array() - 1.0).abs() < tol.array()).all());
}
template <typename Scalar>
void check_histogram(int bins) {
constexpr int repeats = 10000;
double count = double(bins) * double(repeats);
Eigen::VectorXd hist = Eigen::VectorXd::Zero(bins);
HistogramHelper<Scalar> hist_helper(bins);
for (int k = 0; k < bins; k++)
for (int repeat = 0; repeat < repeats; repeat++) {
Scalar r = Eigen::internal::random<Scalar>();
int bin = hist_helper.bin(r);
hist(bin)++;
}
// Normalize bins by probability, and bound each by its own sampling standard deviation.
hist /= count;
const double k = histogram_k_sigma(bins);
Eigen::VectorXd tol(bins);
for (int i = 0; i < bins; ++i) {
const double p = hist_helper.uniform_bin_probability(i);
hist(i) = hist(i) / p;
tol(i) = k * std::sqrt((1.0 - p) / (count * p));
}
VERIFY(((hist.array() - 1.0).abs() < tol.array()).all());
}
template <>
void check_histogram<bool>(int) {
constexpr int bins = 2;
constexpr int repeats = 10000;
double count = double(bins) * double(repeats);
double true_count = 0.0;
for (int k = 0; k < bins; k++)
for (int repeat = 0; repeat < repeats; repeat++) {
bool r = Eigen::internal::random<bool>();
if (r) true_count += 1.0;
}
double p = true_count / count;
VERIFY(numext::abs(p - 0.5) < 0.05);
}
template <typename Scalar>
void check_reproducibility() {
using Vector = Matrix<Scalar, Dynamic, 1>;
const uint64_t seeds[] = {0, 42, 0x10000002aULL, 0xffffffffffffffffULL};
Vector expected(100), actual(100);
for (uint64_t seed : seeds) {
Eigen::setRandomSeed(seed);
expected = Vector::Random(actual.size());
Eigen::setRandomSeed(seed);
actual.setRandom();
VERIFY((actual.array() == expected.array()).all());
}
}
void check_seed_backend() {
using Vector = Matrix<uint32_t, Dynamic, 1>;
constexpr uint64_t seed = 0x123456780000002aULL;
#if EIGEN_HAS_THREAD_LOCAL_RANDOM
static_assert(internal::eigen_random_device::Entropy == 32, "PCG supplies 32 random bits");
static_assert(internal::eigen_random_device::Highest == 0xffffffffu, "PCG spans uint32_t");
Eigen::setRandomSeed(seed);
const Vector expected = Vector::Random(100);
Eigen::setRandomSeed(seed);
std::srand(17);
const Vector actual = Vector::Random(100);
VERIFY((actual.array() == expected.array()).all());
Eigen::setRandomSeed(static_cast<unsigned>(seed));
const Vector truncated = Vector::Random(100);
VERIFY((truncated.array() != expected.array()).any());
std::srand(31);
const int expected_rand = std::rand();
std::srand(31);
Eigen::setRandomSeed(seed);
const Vector unused = Vector::Random(100);
EIGEN_UNUSED_VARIABLE(unused);
VERIFY_IS_EQUAL(std::rand(), expected_rand);
#else
static_assert(internal::eigen_random_device::Highest == RAND_MAX, "Fallback retains the std::rand range");
std::srand(static_cast<unsigned>(seed));
const Vector expected = Vector::Random(100);
Eigen::setRandomSeed(seed);
const Vector actual = Vector::Random(100);
VERIFY((actual.array() == expected.array()).all());
#endif
}
#if EIGEN_HAS_THREAD_LOCAL_RANDOM
// With inc = 2*seed+1, seeds s and s ^ 2^63 shared the increment and their states differed by exactly 2^63 at every
// step, so each XSH-RS output of one stream equaled the other's shifted by 4 bits, up to one flipped bit.
void check_seed_high_bit_streams() {
using Device = internal::eigen_random_device;
constexpr int samples = 64;
for (uint64_t seed : {uint64_t(0), uint64_t(7), uint64_t(0x123456780000002aULL)}) {
unsigned a[samples], b[samples];
Eigen::setRandomSeed(seed);
for (unsigned& x : a) x = Device::run();
Eigen::setRandomSeed(seed ^ (uint64_t(1) << 63));
for (unsigned& y : b) y = Device::run();
int shifted = 0;
for (int i = 0; i < samples; ++i) {
const int d1 = internal::popcount<unsigned>(((a[i] << 4) ^ b[i]) & 0xfffffff0u);
const int d2 = internal::popcount<unsigned>(((b[i] << 4) ^ a[i]) & 0xfffffff0u);
if (d1 <= 1 || d2 <= 1) ++shifted;
}
VERIFY(shifted <= 1);
}
}
void check_thread_safety() {
using Vector = Matrix<uint32_t, Dynamic, 1>;
constexpr int num_threads = 4;
constexpr int samples_per_thread = 10000;
const uint64_t seeds[num_threads] = {7, 7, 0x100000007ULL, 0xffffffffffffffffULL};
std::vector<Vector> expected(num_threads), results(num_threads);
for (int t = 0; t < num_threads; ++t) {
Eigen::setRandomSeed(seeds[t]);
expected[t] = Vector::Random(samples_per_thread);
results[t].resize(samples_per_thread);
}
Eigen::setRandomSeed(123);
const Vector expected_main = Vector::Random(samples_per_thread);
std::atomic<int> ready{0};
std::atomic<bool> start{false};
std::vector<std::thread> threads;
for (int t = 0; t < num_threads; ++t) {
threads.emplace_back([&, t]() {
Eigen::setRandomSeed(seeds[t]);
ready.fetch_add(1);
while (!start.load()) std::this_thread::yield();
results[t].setRandom();
});
}
while (ready.load() != num_threads) std::this_thread::yield();
// Reseeding the main thread must not reset any worker's stream.
Eigen::setRandomSeed(123);
start.store(true);
for (auto& th : threads) th.join();
for (int t = 0; t < num_threads; ++t) {
VERIFY((results[t].array() == expected[t].array()).all());
}
// Worker draws must leave the main thread's stream untouched.
const Vector actual_main = Vector::Random(samples_per_thread);
VERIFY((actual_main.array() == expected_main.array()).all());
}
#endif // EIGEN_HAS_THREAD_LOCAL_RANDOM
EIGEN_DECLARE_TEST(rand) {
int64_t int64_ref = NumTraits<int64_t>::highest() / 10;
// the minimum guarantees that these conversions are safe
int8_t int8t_offset = static_cast<int8_t>((std::min)(g_repeat, 64));
int16_t int16t_offset = static_cast<int16_t>((std::min)(g_repeat, 8000));
EIGEN_UNUSED_VARIABLE(int64_ref);
EIGEN_UNUSED_VARIABLE(int8t_offset);
EIGEN_UNUSED_VARIABLE(int16t_offset);
for (int i = 0; i < g_repeat * 10000; i++) {
CALL_SUBTEST_1(check_in_range<float>(10.0f, 11.0f));
CALL_SUBTEST_1(check_in_range<float>(1.24234523f, 1.24234523f));
CALL_SUBTEST_1(check_in_range<float>(-1.0f, 1.0f));
CALL_SUBTEST_1(check_in_range<float>(-1432.2352f, -1432.2352f));
CALL_SUBTEST_2(check_in_range<double>(10.0, 11.0));
CALL_SUBTEST_2(check_in_range<double>(1.24234523, 1.24234523));
CALL_SUBTEST_2(check_in_range<double>(-1.0, 1.0));
CALL_SUBTEST_2(check_in_range<double>(-1432.2352, -1432.2352));
CALL_SUBTEST_3(check_in_range<long double>(10.0L, 11.0L));
CALL_SUBTEST_3(check_in_range<long double>(1.24234523L, 1.24234523L));
CALL_SUBTEST_3(check_in_range<long double>(-1.0L, 1.0L));
CALL_SUBTEST_3(check_in_range<long double>(-1432.2352L, -1432.2352L));
CALL_SUBTEST_4(check_in_range<half>(half(10.0f), half(11.0f)));
CALL_SUBTEST_4(check_in_range<half>(half(1.24234523f), half(1.24234523f)));
CALL_SUBTEST_4(check_in_range<half>(half(-1.0f), half(1.0f)));
CALL_SUBTEST_4(check_in_range<half>(half(-1432.2352f), half(-1432.2352f)));
CALL_SUBTEST_5(check_in_range<bfloat16>(bfloat16(10.0f), bfloat16(11.0f)));
CALL_SUBTEST_5(check_in_range<bfloat16>(bfloat16(1.24234523f), bfloat16(1.24234523f)));
CALL_SUBTEST_5(check_in_range<bfloat16>(bfloat16(-1.0f), bfloat16(1.0f)));
CALL_SUBTEST_5(check_in_range<bfloat16>(bfloat16(-1432.2352f), bfloat16(-1432.2352f)));
CALL_SUBTEST_6(check_in_range<int32_t>(0, -1));
CALL_SUBTEST_6(check_in_range<int16_t>(0, -1));
CALL_SUBTEST_6(check_in_range<int64_t>(0, -1));
CALL_SUBTEST_6(check_in_range<int32_t>(-673456, 673456));
CALL_SUBTEST_6(check_in_range<int32_t>(-RAND_MAX + 10, RAND_MAX - 10));
CALL_SUBTEST_6(check_in_range<int16_t>(-24345, 24345));
CALL_SUBTEST_6(check_in_range<int64_t>(-int64_ref, int64_ref));
}
CALL_SUBTEST_7(check_all_in_range<int8_t>(11, 11));
CALL_SUBTEST_7(check_all_in_range<int8_t>(11, 11 + int8t_offset));
CALL_SUBTEST_7(check_all_in_range<int8_t>(-5, 5));
CALL_SUBTEST_7(check_all_in_range<int8_t>(-11 - int8t_offset, -11));
CALL_SUBTEST_7(check_all_in_range<int8_t>(-126, -126 + int8t_offset));
CALL_SUBTEST_7(check_all_in_range<int8_t>(126 - int8t_offset, 126));
CALL_SUBTEST_7(check_all_in_range<int8_t>());
CALL_SUBTEST_7(check_all_in_range<uint8_t>());
CALL_SUBTEST_8(check_all_in_range<int16_t>(11, 11));
CALL_SUBTEST_8(check_all_in_range<int16_t>(11, 11 + int16t_offset));
CALL_SUBTEST_8(check_all_in_range<int16_t>(-5, 5));
CALL_SUBTEST_8(check_all_in_range<int16_t>(-11 - int16t_offset, -11));
CALL_SUBTEST_8(check_all_in_range<int16_t>(-24345, -24345 + int16t_offset));
CALL_SUBTEST_8(check_all_in_range<int16_t>());
CALL_SUBTEST_8(check_all_in_range<uint16_t>());
CALL_SUBTEST_9(check_all_in_range<int32_t>(11, 11));
CALL_SUBTEST_9(check_all_in_range<int32_t>(11, 11 + g_repeat));
CALL_SUBTEST_9(check_all_in_range<int32_t>(-5, 5));
CALL_SUBTEST_9(check_all_in_range<int32_t>(-11 - g_repeat, -11));
CALL_SUBTEST_9(check_all_in_range<int32_t>(-673456, -673456 + g_repeat));
CALL_SUBTEST_9(check_all_in_range<int32_t>(673456, 673456 + g_repeat));
CALL_SUBTEST_10(check_all_in_range<int64_t>(11, 11));
CALL_SUBTEST_10(check_all_in_range<int64_t>(11, 11 + g_repeat));
CALL_SUBTEST_10(check_all_in_range<int64_t>(-5, 5));
CALL_SUBTEST_10(check_all_in_range<int64_t>(-11 - g_repeat, -11));
CALL_SUBTEST_10(check_all_in_range<int64_t>(-int64_ref, -int64_ref + g_repeat));
CALL_SUBTEST_10(check_all_in_range<int64_t>(int64_ref, int64_ref + g_repeat));
CALL_SUBTEST_11(check_histogram<int32_t>(-5, 5, 11));
int bins = 100;
EIGEN_UNUSED_VARIABLE(bins);
CALL_SUBTEST_11(check_histogram<int32_t>(-3333, -3333 + bins * (3333 / bins) - 1, bins));
bins = 1000;
CALL_SUBTEST_11(check_histogram<int32_t>(-RAND_MAX + 10, -RAND_MAX + 10 + bins * (RAND_MAX / bins) - 1, bins));
CALL_SUBTEST_11(check_histogram<int32_t>(-RAND_MAX + 10,
-int64_t(RAND_MAX) + 10 + bins * (2 * int64_t(RAND_MAX) / bins) - 1, bins));
CALL_SUBTEST_12(check_histogram<bool>(/*bins=*/2));
CALL_SUBTEST_12(check_histogram<uint8_t>(/*bins=*/16));
CALL_SUBTEST_12(check_histogram<uint16_t>(/*bins=*/1024));
CALL_SUBTEST_12(check_histogram<uint32_t>(/*bins=*/1024));
CALL_SUBTEST_12(check_histogram<uint64_t>(/*bins=*/1024));
CALL_SUBTEST_13(check_histogram<int8_t>(/*bins=*/16));
CALL_SUBTEST_13(check_histogram<int16_t>(/*bins=*/1024));
CALL_SUBTEST_13(check_histogram<int32_t>(/*bins=*/1024));
CALL_SUBTEST_13(check_histogram<int64_t>(/*bins=*/1024));
CALL_SUBTEST_14(check_histogram<float>(-10.0f, 10.0f, /*bins=*/1024));
CALL_SUBTEST_14(check_histogram<double>(-10.0, 10.0, /*bins=*/1024));
CALL_SUBTEST_14(check_histogram<long double>(-10.0L, 10.0L, /*bins=*/1024));
CALL_SUBTEST_14(check_histogram<half>(half(-10.0f), half(10.0f), /*bins=*/512));
CALL_SUBTEST_14(check_histogram<bfloat16>(bfloat16(-10.0f), bfloat16(10.0f), /*bins=*/64));
CALL_SUBTEST_14(check_histogram<SafeScalar<float>>(-10.0f, 10.0f, /*bins=*/1024));
CALL_SUBTEST_14(check_histogram<SafeScalar<half>>(half(-10.0f), half(10.0f), /*bins=*/512));
CALL_SUBTEST_14(check_histogram<SafeScalar<bfloat16>>(bfloat16(-10.0f), bfloat16(10.0f), /*bins=*/64));
CALL_SUBTEST_15(check_histogram<float>(/*bins=*/1024));
CALL_SUBTEST_15(check_histogram<double>(/*bins=*/1024));
CALL_SUBTEST_15(check_histogram<long double>(/*bins=*/1024));
CALL_SUBTEST_15(check_histogram<half>(/*bins=*/512));
CALL_SUBTEST_15(check_histogram<bfloat16>(/*bins=*/64));
CALL_SUBTEST_15(check_histogram<SafeScalar<float>>(/*bins=*/1024));
CALL_SUBTEST_15(check_histogram<SafeScalar<half>>(/*bins=*/512));
CALL_SUBTEST_15(check_histogram<SafeScalar<bfloat16>>(/*bins=*/64));
CALL_SUBTEST_16(check_reproducibility<float>());
CALL_SUBTEST_16(check_reproducibility<double>());
CALL_SUBTEST_16(check_reproducibility<int32_t>());
CALL_SUBTEST_16(check_reproducibility<uint64_t>());
CALL_SUBTEST_16(check_reproducibility<std::complex<double>>());
CALL_SUBTEST_16(check_seed_backend());
#if EIGEN_HAS_THREAD_LOCAL_RANDOM
CALL_SUBTEST_16(check_seed_high_bit_streams());
CALL_SUBTEST_17(check_thread_safety());
#endif
}