rand.cpp File Reference
#include <cstdlib>
#include "main.h"
#include "SafeScalar.h"

Classes

class  HistogramHelper< Scalar, EnableIf >
 
struct  get_range_type< T >
 
struct  get_range_type< SafeScalar< T > >
 
class  HistogramHelper< Scalar, std::enable_if_t< Eigen::NumTraits< Scalar >::IsInteger > >
 

Functions

template<typename Scalar >
EIGEN_DONT_INLINE Scalar check_in_range (Scalar x, Scalar y)
 
template<typename Scalar >
void check_all_in_range (Scalar x, Scalar y)
 
template<typename Scalar >
void check_all_in_range ()
 
template<typename Scalar >
void check_histogram (Scalar x, Scalar y, int bins)
 
template<typename Scalar >
void check_histogram (int bins)
 
template<>
void check_histogram< bool > (int)
 
 EIGEN_DECLARE_TEST (rand)
 

Function Documentation

◆ check_all_in_range() [1/2]

template<typename Scalar >
void check_all_in_range ( )
50  {
54 }
SCALAR Scalar
Definition: bench_gemm.cpp:45
Scalar * y
Definition: level1_cplx_impl.h:128
list x
Definition: plotDoE.py:28
void check_all_in_range(Scalar x, Scalar y)
Definition: rand.cpp:31
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217

References check_all_in_range(), plotDoE::x, and y.

◆ check_all_in_range() [2/2]

template<typename Scalar >
void check_all_in_range ( Scalar  x,
Scalar  y 
)
31  {
32  constexpr int repeats = 32;
33  uint64_t count = static_cast<uint64_t>(y) - static_cast<uint64_t>(x) + 1;
34  ArrayX<bool> mask(count);
35  // ensure that `count` does not overflow the return type of `mask.size()`
36  VERIFY(count == static_cast<uint64_t>(mask.size()));
37  mask.setConstant(false);
38  for (uint64_t k = 0; k < count; k++)
39  for (int repeat = 0; repeat < repeats; repeat++) {
41  Index i = static_cast<Index>(r) - static_cast<Index>(x);
42  mask(i) = true;
43  }
44  for (Index i = 0; i < mask.size(); ++i)
45  if (mask(i) == false) std::cout << "WARNING: value " << x + i << " not reached." << std::endl;
46  VERIFY(mask.cwiseEqual(true).all());
47 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
char char char int int * k
Definition: level2_impl.h:374
#define VERIFY(a)
Definition: main.h:362
constexpr array< t, n > repeat(t v)
Definition: MoreMeta.h:583
std::uint64_t uint64_t
Definition: Meta.h:42
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
r
Definition: UniformPSDSelfTest.py:20
EIGEN_DONT_INLINE Scalar check_in_range(Scalar x, Scalar y)
Definition: rand.cpp:21

References check_in_range(), i, k, UniformPSDSelfTest::r, Eigen::internal::repeat(), VERIFY, plotDoE::x, and y.

Referenced by check_all_in_range().

◆ check_histogram() [1/2]

template<typename Scalar >
void check_histogram ( int  bins)
154  {
155  constexpr int repeats = 10000;
156  double count = double(bins) * double(repeats);
157  Eigen::VectorXd hist = Eigen::VectorXd::Zero(bins);
158  HistogramHelper<Scalar> hist_helper(bins);
159  for (int k = 0; k < bins; k++)
160  for (int repeat = 0; repeat < repeats; repeat++) {
161  Scalar r = Eigen::internal::random<Scalar>();
162  int bin = hist_helper.bin(r);
163  hist(bin)++;
164  }
165  // Normalize bins by probability.
166  hist /= count;
167  for (int i = 0; i < bins; ++i) {
168  hist(i) = hist(i) / hist_helper.uniform_bin_probability(i);
169  }
170  VERIFY(((hist.array() - 1.0).abs() < 0.05).all());
171 }
Definition: rand.cpp:57
bins
Definition: UniformPSDSelfTest.py:19
double Zero
Definition: pseudosolid_node_update_elements.cc:35

References HistogramHelper< Scalar, EnableIf >::bin(), UniformPSDSelfTest::bins, i, k, UniformPSDSelfTest::r, Eigen::internal::repeat(), HistogramHelper< Scalar, EnableIf >::uniform_bin_probability(), VERIFY, and oomph::PseudoSolidHelper::Zero.

◆ check_histogram() [2/2]

template<typename Scalar >
void check_histogram ( Scalar  x,
Scalar  y,
int  bins 
)
134  {
135  constexpr int repeats = 10000;
136  double count = double(bins) * double(repeats);
137  Eigen::VectorXd hist = Eigen::VectorXd::Zero(bins);
138  HistogramHelper<Scalar> hist_helper(x, y, bins);
139  for (int k = 0; k < bins; k++)
140  for (int repeat = 0; repeat < repeats; repeat++) {
141  Scalar r = check_in_range(x, y);
142  int bin = hist_helper.bin(r);
143  hist(bin)++;
144  }
145  // Normalize bins by probability.
146  hist /= count;
147  for (int i = 0; i < bins; ++i) {
148  hist(i) = hist(i) / hist_helper.uniform_bin_probability(i);
149  }
150  VERIFY(((hist.array() - 1.0).abs() < 0.05).all());
151 }

References HistogramHelper< Scalar, EnableIf >::bin(), UniformPSDSelfTest::bins, check_in_range(), i, k, UniformPSDSelfTest::r, Eigen::internal::repeat(), HistogramHelper< Scalar, EnableIf >::uniform_bin_probability(), VERIFY, plotDoE::x, y, and oomph::PseudoSolidHelper::Zero.

Referenced by EIGEN_DECLARE_TEST().

◆ check_histogram< bool >()

template<>
void check_histogram< bool > ( int  )
174  {
175  constexpr int bins = 2;
176  constexpr int repeats = 10000;
177  double count = double(bins) * double(repeats);
178  double true_count = 0.0;
179  for (int k = 0; k < bins; k++)
180  for (int repeat = 0; repeat < repeats; repeat++) {
181  bool r = Eigen::internal::random<bool>();
182  if (r) true_count += 1.0;
183  }
184  double p = true_count / count;
185  VERIFY(numext::abs(p - 0.5) < 0.05);
186 }
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
float * p
Definition: Tutorial_Map_using.cpp:9

References abs(), UniformPSDSelfTest::bins, k, p, UniformPSDSelfTest::r, Eigen::internal::repeat(), and VERIFY.

Referenced by EIGEN_DECLARE_TEST().

◆ check_in_range()

template<typename Scalar >
EIGEN_DONT_INLINE Scalar check_in_range ( Scalar  x,
Scalar  y 
)
21  {
22  Scalar r = internal::random<Scalar>(x, y);
23  VERIFY(r >= x);
24  if (y >= x) {
25  VERIFY(r <= y);
26  }
27  return r;
28 }

References UniformPSDSelfTest::r, VERIFY, plotDoE::x, and y.

Referenced by check_all_in_range(), and check_histogram().

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( rand  )
188  {
189  int64_t int64_ref = NumTraits<int64_t>::highest() / 10;
190  // the minimum guarantees that these conversions are safe
191  int8_t int8t_offset = static_cast<int8_t>((std::min)(g_repeat, 64));
192  int16_t int16t_offset = static_cast<int16_t>((std::min)(g_repeat, 8000));
193  EIGEN_UNUSED_VARIABLE(int64_ref);
194  EIGEN_UNUSED_VARIABLE(int8t_offset);
195  EIGEN_UNUSED_VARIABLE(int16t_offset);
196 
197  for (int i = 0; i < g_repeat * 10000; i++) {
198  CALL_SUBTEST_1(check_in_range<float>(10.0f, 11.0f));
199  CALL_SUBTEST_1(check_in_range<float>(1.24234523f, 1.24234523f));
200  CALL_SUBTEST_1(check_in_range<float>(-1.0f, 1.0f));
201  CALL_SUBTEST_1(check_in_range<float>(-1432.2352f, -1432.2352f));
202 
203  CALL_SUBTEST_2(check_in_range<double>(10.0, 11.0));
204  CALL_SUBTEST_2(check_in_range<double>(1.24234523, 1.24234523));
205  CALL_SUBTEST_2(check_in_range<double>(-1.0, 1.0));
206  CALL_SUBTEST_2(check_in_range<double>(-1432.2352, -1432.2352));
207 
208  CALL_SUBTEST_3(check_in_range<long double>(10.0L, 11.0L));
209  CALL_SUBTEST_3(check_in_range<long double>(1.24234523L, 1.24234523L));
210  CALL_SUBTEST_3(check_in_range<long double>(-1.0L, 1.0L));
211  CALL_SUBTEST_3(check_in_range<long double>(-1432.2352L, -1432.2352L));
212 
213  CALL_SUBTEST_4(check_in_range<half>(half(10.0f), half(11.0f)));
214  CALL_SUBTEST_4(check_in_range<half>(half(1.24234523f), half(1.24234523f)));
215  CALL_SUBTEST_4(check_in_range<half>(half(-1.0f), half(1.0f)));
216  CALL_SUBTEST_4(check_in_range<half>(half(-1432.2352f), half(-1432.2352f)));
217 
218  CALL_SUBTEST_5(check_in_range<bfloat16>(bfloat16(10.0f), bfloat16(11.0f)));
219  CALL_SUBTEST_5(check_in_range<bfloat16>(bfloat16(1.24234523f), bfloat16(1.24234523f)));
220  CALL_SUBTEST_5(check_in_range<bfloat16>(bfloat16(-1.0f), bfloat16(1.0f)));
221  CALL_SUBTEST_5(check_in_range<bfloat16>(bfloat16(-1432.2352f), bfloat16(-1432.2352f)));
222 
223  CALL_SUBTEST_6(check_in_range<int32_t>(0, -1));
224  CALL_SUBTEST_6(check_in_range<int16_t>(0, -1));
225  CALL_SUBTEST_6(check_in_range<int64_t>(0, -1));
226  CALL_SUBTEST_6(check_in_range<int32_t>(-673456, 673456));
227  CALL_SUBTEST_6(check_in_range<int32_t>(-RAND_MAX + 10, RAND_MAX - 10));
228  CALL_SUBTEST_6(check_in_range<int16_t>(-24345, 24345));
229  CALL_SUBTEST_6(check_in_range<int64_t>(-int64_ref, int64_ref));
230  }
231 
232  CALL_SUBTEST_7(check_all_in_range<int8_t>(11, 11));
233  CALL_SUBTEST_7(check_all_in_range<int8_t>(11, 11 + int8t_offset));
234  CALL_SUBTEST_7(check_all_in_range<int8_t>(-5, 5));
235  CALL_SUBTEST_7(check_all_in_range<int8_t>(-11 - int8t_offset, -11));
236  CALL_SUBTEST_7(check_all_in_range<int8_t>(-126, -126 + int8t_offset));
237  CALL_SUBTEST_7(check_all_in_range<int8_t>(126 - int8t_offset, 126));
238  CALL_SUBTEST_7(check_all_in_range<int8_t>());
239  CALL_SUBTEST_7(check_all_in_range<uint8_t>());
240 
241  CALL_SUBTEST_8(check_all_in_range<int16_t>(11, 11));
242  CALL_SUBTEST_8(check_all_in_range<int16_t>(11, 11 + int16t_offset));
243  CALL_SUBTEST_8(check_all_in_range<int16_t>(-5, 5));
244  CALL_SUBTEST_8(check_all_in_range<int16_t>(-11 - int16t_offset, -11));
245  CALL_SUBTEST_8(check_all_in_range<int16_t>(-24345, -24345 + int16t_offset));
246  CALL_SUBTEST_8(check_all_in_range<int16_t>());
247  CALL_SUBTEST_8(check_all_in_range<uint16_t>());
248 
249  CALL_SUBTEST_9(check_all_in_range<int32_t>(11, 11));
250  CALL_SUBTEST_9(check_all_in_range<int32_t>(11, 11 + g_repeat));
251  CALL_SUBTEST_9(check_all_in_range<int32_t>(-5, 5));
252  CALL_SUBTEST_9(check_all_in_range<int32_t>(-11 - g_repeat, -11));
253  CALL_SUBTEST_9(check_all_in_range<int32_t>(-673456, -673456 + g_repeat));
254  CALL_SUBTEST_9(check_all_in_range<int32_t>(673456, 673456 + g_repeat));
255 
256  CALL_SUBTEST_10(check_all_in_range<int64_t>(11, 11));
257  CALL_SUBTEST_10(check_all_in_range<int64_t>(11, 11 + g_repeat));
258  CALL_SUBTEST_10(check_all_in_range<int64_t>(-5, 5));
259  CALL_SUBTEST_10(check_all_in_range<int64_t>(-11 - g_repeat, -11));
260  CALL_SUBTEST_10(check_all_in_range<int64_t>(-int64_ref, -int64_ref + g_repeat));
261  CALL_SUBTEST_10(check_all_in_range<int64_t>(int64_ref, int64_ref + g_repeat));
262 
263  CALL_SUBTEST_11(check_histogram<int32_t>(-5, 5, 11));
264  int bins = 100;
266  CALL_SUBTEST_11(check_histogram<int32_t>(-3333, -3333 + bins * (3333 / bins) - 1, bins));
267  bins = 1000;
268  CALL_SUBTEST_11(check_histogram<int32_t>(-RAND_MAX + 10, -RAND_MAX + 10 + bins * (RAND_MAX / bins) - 1, bins));
269  CALL_SUBTEST_11(check_histogram<int32_t>(-RAND_MAX + 10,
270  -int64_t(RAND_MAX) + 10 + bins * (2 * int64_t(RAND_MAX) / bins) - 1, bins));
271 
273  CALL_SUBTEST_12(check_histogram<uint8_t>(/*bins=*/16));
274  CALL_SUBTEST_12(check_histogram<uint16_t>(/*bins=*/1024));
275  CALL_SUBTEST_12(check_histogram<uint32_t>(/*bins=*/1024));
276  CALL_SUBTEST_12(check_histogram<uint64_t>(/*bins=*/1024));
277 
278  CALL_SUBTEST_13(check_histogram<int8_t>(/*bins=*/16));
279  CALL_SUBTEST_13(check_histogram<int16_t>(/*bins=*/1024));
280  CALL_SUBTEST_13(check_histogram<int32_t>(/*bins=*/1024));
281  CALL_SUBTEST_13(check_histogram<int64_t>(/*bins=*/1024));
282 
283  CALL_SUBTEST_14(check_histogram<float>(-10.0f, 10.0f, /*bins=*/1024));
284  CALL_SUBTEST_14(check_histogram<double>(-10.0, 10.0, /*bins=*/1024));
285  CALL_SUBTEST_14(check_histogram<long double>(-10.0L, 10.0L, /*bins=*/1024));
286  CALL_SUBTEST_14(check_histogram<half>(half(-10.0f), half(10.0f), /*bins=*/512));
287  CALL_SUBTEST_14(check_histogram<bfloat16>(bfloat16(-10.0f), bfloat16(10.0f), /*bins=*/64));
288  CALL_SUBTEST_14(check_histogram<SafeScalar<float>>(-10.0f, 10.0f, /*bins=*/1024));
289  CALL_SUBTEST_14(check_histogram<SafeScalar<half>>(half(-10.0f), half(10.0f), /*bins=*/512));
290  CALL_SUBTEST_14(check_histogram<SafeScalar<bfloat16>>(bfloat16(-10.0f), bfloat16(10.0f), /*bins=*/64));
291 
292  CALL_SUBTEST_15(check_histogram<float>(/*bins=*/1024));
293  CALL_SUBTEST_15(check_histogram<double>(/*bins=*/1024));
294  CALL_SUBTEST_15(check_histogram<long double>(/*bins=*/1024));
295  CALL_SUBTEST_15(check_histogram<half>(/*bins=*/512));
296  CALL_SUBTEST_15(check_histogram<bfloat16>(/*bins=*/64));
300 }
#define EIGEN_UNUSED_VARIABLE(var)
Definition: Macros.h:966
Definition: SafeScalar.h:4
#define min(a, b)
Definition: datatypes.h:22
std::int8_t int8_t
Definition: Meta.h:37
std::int16_t int16_t
Definition: Meta.h:39
std::int64_t int64_t
Definition: Meta.h:43
static int g_repeat
Definition: main.h:191
void check_histogram(Scalar x, Scalar y, int bins)
Definition: rand.cpp:134
void check_histogram< bool >(int)
Definition: rand.cpp:174
#define CALL_SUBTEST_6(FUNC)
Definition: split_test_helper.h:34
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
#define CALL_SUBTEST_13(FUNC)
Definition: split_test_helper.h:76
#define CALL_SUBTEST_14(FUNC)
Definition: split_test_helper.h:82
#define CALL_SUBTEST_8(FUNC)
Definition: split_test_helper.h:46
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
#define CALL_SUBTEST_11(FUNC)
Definition: split_test_helper.h:64
#define CALL_SUBTEST_12(FUNC)
Definition: split_test_helper.h:70
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
#define CALL_SUBTEST_7(FUNC)
Definition: split_test_helper.h:40
#define CALL_SUBTEST_15(FUNC)
Definition: split_test_helper.h:88
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
#define CALL_SUBTEST_9(FUNC)
Definition: split_test_helper.h:52
#define CALL_SUBTEST_10(FUNC)
Definition: split_test_helper.h:58
Definition: BFloat16.h:101
Definition: Half.h:139

References UniformPSDSelfTest::bins, CALL_SUBTEST_1, CALL_SUBTEST_10, CALL_SUBTEST_11, CALL_SUBTEST_12, CALL_SUBTEST_13, CALL_SUBTEST_14, CALL_SUBTEST_15, CALL_SUBTEST_2, CALL_SUBTEST_3, CALL_SUBTEST_4, CALL_SUBTEST_5, CALL_SUBTEST_6, CALL_SUBTEST_7, CALL_SUBTEST_8, CALL_SUBTEST_9, check_histogram(), check_histogram< bool >(), EIGEN_UNUSED_VARIABLE, Eigen::g_repeat, i, and min.