1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
#include "CLHEP/Random/DualRand.h"
#include "CLHEP/Random/Hurd160Engine.h"
#include "CLHEP/Random/Hurd288Engine.h"
#include "CLHEP/Random/JamesRandom.h"
#include "CLHEP/Random/MixMaxRng.h"
#include "CLHEP/Random/MTwistEngine.h"
#include "CLHEP/Random/RandEngine.h"
#include "CLHEP/Random/RanecuEngine.h"
#include "CLHEP/Random/Ranlux64Engine.h"
#include "CLHEP/Random/RanluxEngine.h"
#include "CLHEP/Random/RanluxppEngine.h"
#include "CLHEP/Random/RanshiEngine.h"
#include "CLHEP/Random/TripleRand.h"
#include "CLHEP/Random/RandBinomial.h"
#include "CLHEP/Random/RandChiSquare.h"
#include "CLHEP/Random/RandGauss.h"
#include "CLHEP/Random/RandGamma.h"
#include "CLHEP/Random/RandGaussZiggurat.h"
#include "CLHEP/Random/RandExpZiggurat.h"
#include "CLHEP/Utility/atomic_int.h"
#include <cmath>
#include <iostream>
#include <vector>
// Some logic to only run the test if the compiler supports
// threading
#if __cplusplus >= 201103L
// Skip threaded tests on architectures with known TLS issues
#if defined(__i386__) || defined(__arm__) || defined(__hppa__) || defined(__x32__)
#define CLHEP_RUN_THREADED_TESTS 0
#else
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 7)
#define CLHEP_RUN_THREADED_TESTS 1
#include <thread>
#elif __clang__
#if __has_feature(cxx_thread_local) && __has_feature(c_atomic)
#define CLHEP_RUN_THREADED_TESTS 1
#include <thread>
#else
#define CLHEP_RUN_THREADED_TESTS 0
#endif
#else
#define CLHEP_RUN_THREADED_TESTS 0
#endif
#endif // architectures with known TLS issues
#else
#define CLHEP_RUN_THREADED_TESTS 0
#endif
void testRandGauss(std::vector<double> const& reference, bool& result) {
// Check that the fire and two shoot methods all give the same
// random number sequence. The output of the fire method is passed
// in. fire does not use thread local variables, but instead
// nonstatic class data members for the caching. shoot should
// be using thread local variables for the caching. Random
// numbers are generated in pairs and on a subsequent call
// the second number is used. Note it is important that an
// odd number of random numbers are generated for this test,
// because that causes there to be a number in the cache from
// the first thread when the second thread executes. It proves
// that the second thread is using a different cache when it
// does not use that cached value.
long seedL1 = 100;
CLHEP::HepJamesRandom engine(seedL1);
CLHEP::RandGauss dist(engine);
result = true;
std::vector<double> v;
v.push_back(dist.fire());
v.push_back(dist.fire());
v.push_back(dist.fire());
v.push_back(dist.fire());
v.push_back(dist.fire());
// Just a sanity check first. The fire method reproduces
// itself.
if (reference[0] != v[0] ||
reference[1] != v[1] ||
reference[2] != v[2] ||
reference[3] != v[3] ||
reference[4] != v[4]) {
result = false;
}
// check the shoot method where we pass in an engine
CLHEP::HepJamesRandom engine1(seedL1);
v.clear();
v.push_back(CLHEP::RandGauss::shoot(&engine1));
v.push_back(CLHEP::RandGauss::shoot(&engine1));
v.push_back(CLHEP::RandGauss::shoot(&engine1));
v.push_back(CLHEP::RandGauss::shoot(&engine1));
v.push_back(CLHEP::RandGauss::shoot(&engine1));
if (reference[0] != v[0] ||
reference[1] != v[1] ||
reference[2] != v[2] ||
reference[3] != v[3] ||
reference[4] != v[4]) {
result = false;
}
// check the shoot method using the CLHEP thread local
// engine
CLHEP::HepJamesRandom engine2(seedL1);
CLHEP::HepRandomEngine* savedEngine = CLHEP::HepRandom::getTheEngine();
CLHEP::HepRandom::setTheEngine(&engine2);
// setFlag causes it to not use the cached value
// and generate a new pair of random numbers
CLHEP::RandGauss::setFlag(false);
v.clear();
v.push_back(CLHEP::RandGauss::shoot());
v.push_back(CLHEP::RandGauss::shoot());
v.push_back(CLHEP::RandGauss::shoot());
v.push_back(CLHEP::RandGauss::shoot());
v.push_back(CLHEP::RandGauss::shoot());
if (reference[0] != v[0] ||
reference[1] != v[1] ||
reference[2] != v[2] ||
reference[3] != v[3] ||
reference[4] != v[4]) {
result = false;
}
CLHEP::HepRandom::setTheEngine(savedEngine);
}
#if defined __GNUC__
#if __GNUC__ > 3 && __GNUC_MINOR__ > 6
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"
#endif
#if __GNUC__ > 4
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"
#endif
#endif
#if defined __INTEL_COMPILER
#pragma warning push
#pragma warning disable 1599
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshadow"
#endif
int main() {
std::ofstream output("testThreaded.cout");
#if CLHEP_RUN_THREADED_TESTS == 0
output << "Not running testThreaded.cc tests because compiler does not support needed features.\n";
return 0;
#else
output << "Running testThreaded.cc tests.\n";
long seedL = 100;
CLHEP::HepJamesRandom engine(seedL);
CLHEP::RandGauss dist(engine);
std::vector<double> generatedNumbers;
generatedNumbers.push_back(dist.fire());
generatedNumbers.push_back(dist.fire());
generatedNumbers.push_back(dist.fire());
generatedNumbers.push_back(dist.fire());
generatedNumbers.push_back(dist.fire());
bool result1 = true;
// First test that the fire and shoot methods give consistent results
std::thread t1(testRandGauss, std::cref(generatedNumbers), std::ref(result1));
t1.join();
if(!result1) {
output << "testRandGauss failed on thread 1. fire and 2 shoot functions do not give the same results.\n";
return 1;
}
bool result2 = true;
// RandGauss generates numbers in pairs and caches the second, using the
// second on a subsequent call instead of generating another. The following
// will fail if the cache is not thread local because the first random
// number generated will be the cached one from the previous thread instead
// of a new one.
std::thread t2(testRandGauss, std::cref(generatedNumbers), std::ref(result2));
t2.join();
if(!result2) {
output << "testRandGauss failed on thread 2. This might mean the cache in RandGauss is not really thread local.\n";
output << "Maybe a problem in the macro logic in CLHEP/Utility/thread_local.h.\n";
return 1;
}
output << "Verified that CLHEP_THREAD_LOCAL types are really thread local\n";
CLHEP_ATOMIC_INT_TYPE numberOfEngines(0);
if(numberOfEngines.load() == 0) {
output << "If this is printing, it means CLHEP_ATOMIC_INT_TYPE really has type atomic<int>\n";
}
// The default seed values for the engines are not supposed
// to change nor are the initial random numbers from a sequence.
// I empirically determined the values before the threading code
// changes and test that they are still the same.
double epsilon = 0.0001;
{
CLHEP::DualRand engine1;
CLHEP::DualRand engine2;
CLHEP::DualRand engine3;
if(std::fabs(engine1.flat() - 0.412678) > epsilon ||
std::fabs(engine2.flat() - 0.112937) > epsilon ||
std::fabs(engine3.flat() - 0.998563) > epsilon) {
output << "Error, default seeds changed for DualRand random engine.\n";
return 1;
}
}
{
CLHEP::Hurd160Engine engine1;
CLHEP::Hurd160Engine engine2;
CLHEP::Hurd160Engine engine3;
if(std::fabs(engine1.flat() - 0.104097) > epsilon ||
std::fabs(engine2.flat() - 0.392414) > epsilon ||
std::fabs(engine3.flat() - 0.7008) > epsilon) {
output << "Error, default seeds changed for Hurd160Engine random engine.\n";
return 1;
}
}
{
CLHEP::Hurd288Engine engine1;
CLHEP::Hurd288Engine engine2;
CLHEP::Hurd288Engine engine3;
if(std::fabs(engine1.flat() - 0.942396) > epsilon ||
std::fabs(engine2.flat() - 0.422952) > epsilon ||
std::fabs(engine3.flat() - 0.528602) > epsilon) {
output << "Error, default seeds changed for Hurd288Engine random engine.\n";
return 1;
}
}
{
// Actually engines 3, 4, and 5 because each thread above created
// a default HepJamesRandom engine. The default one whose creation
// was triggered in the CLHEP::HepRandom::setTheEngine call.
CLHEP::HepJamesRandom engine3;
CLHEP::HepJamesRandom engine4;
CLHEP::HepJamesRandom engine5;
if(std::fabs(engine3.flat() - 0.286072) > epsilon ||
std::fabs(engine4.flat() - 0.233610) > epsilon ||
std::fabs(engine5.flat() - 0.837788) > epsilon) {
output << "Error, default seeds changed for HepJamesRandom random engine.\n";
return 1;
}
}
{
CLHEP::MixMaxRng engine1;
CLHEP::MixMaxRng engine2;
CLHEP::MixMaxRng engine3;
if(std::fabs(engine1.flat() - 0.840834) > epsilon ||
std::fabs(engine2.flat() - 0.338951) > epsilon ||
std::fabs(engine3.flat() - 0.840794) > epsilon) {
output << "Error, default seeds changed for MixMaxRng random engine.\n";
return 1;
}
}
{
CLHEP::MTwistEngine engine1;
CLHEP::MTwistEngine engine2;
CLHEP::MTwistEngine engine3;
if(std::fabs(engine1.flat() - 0.350114) > epsilon ||
std::fabs(engine2.flat() - 0.575236) > epsilon ||
std::fabs(engine3.flat() - 0.143409) > epsilon) {
output << "Error, default seeds changed for MTwistEngine random engine.\n";
return 1;
}
}
{
CLHEP::RanecuEngine engine1;
CLHEP::RanecuEngine engine2;
CLHEP::RanecuEngine engine3;
if(std::fabs(engine1.flat() - 0.154707) > epsilon ||
std::fabs(engine2.flat() - 0.417668) > epsilon ||
std::fabs(engine3.flat() - 0.350542) > epsilon) {
output << "Error, default seeds changed for RanecuEngine random engine.\n";
return 1;
}
}
{
CLHEP::Ranlux64Engine engine1;
CLHEP::Ranlux64Engine engine2;
CLHEP::Ranlux64Engine engine3;
if(std::fabs(engine1.flat() - 0.943338) > epsilon ||
std::fabs(engine2.flat() - 0.175414) > epsilon ||
std::fabs(engine3.flat() - 0.965602) > epsilon) {
output << "Error, default seeds changed for Ranlux64Engine random engine.\n";
return 1;
}
}
{
CLHEP::RanluxEngine engine1;
CLHEP::RanluxEngine engine2;
CLHEP::RanluxEngine engine3;
if(std::fabs(engine1.flat() - 0.117402) > epsilon ||
std::fabs(engine2.flat() - 0.856504) > epsilon ||
std::fabs(engine3.flat() - 0.68177) > epsilon) {
output << "Error, default seeds changed for RanluxEngine random engine.\n";
return 1;
}
}
{
CLHEP::RanluxppEngine engine1;
CLHEP::RanluxppEngine engine2;
CLHEP::RanluxppEngine engine3;
if(std::fabs(engine1.flat() - 0.239639) > epsilon ||
std::fabs(engine2.flat() - 0.566275) > epsilon ||
std::fabs(engine3.flat() - 0.958136) > epsilon) {
output << "Error, default seeds changed for RanluxppEngine random engine.\n";
return 1;
}
}
{
CLHEP::RanshiEngine engine1;
CLHEP::RanshiEngine engine2;
CLHEP::RanshiEngine engine3;
if(std::fabs(engine1.flat() - 0.989873) > epsilon ||
std::fabs(engine2.flat() - 0.548671) > epsilon ||
std::fabs(engine3.flat() - 0.917996) > epsilon) {
output << "Error, default seeds changed for RanshiEngine random engine.\n";
return 1;
}
}
{
CLHEP::TripleRand engine1;
CLHEP::TripleRand engine2;
CLHEP::TripleRand engine3;
if(std::fabs(engine1.flat() - 0.838579) > epsilon ||
std::fabs(engine2.flat() - 0.212374) > epsilon ||
std::fabs(engine3.flat() - 0.84029) > epsilon) {
output << "Error, default seeds changed for TripleRand random engine.\n";
return 1;
}
}
// Test to reference values determined by running this code once
// Results should be reproducible so they should not change.
{
long seedL2 = 100;
CLHEP::HepJamesRandom engine(seedL2);
if(CLHEP::RandBinomial::shoot(&engine, 50, 0.2) != 12) {
output << "Error, results changed for RandBinomial.\n";
return 1;
}
}
{
long seedL3 = 100;
CLHEP::HepJamesRandom engine(seedL3);
if(std::fabs(CLHEP::RandChiSquare::shoot(&engine) - 0.031799) > epsilon) {
output << "Error, results changed for RandChiSquared.\n";
return 1;
}
}
{
long seedL4 = 100;
CLHEP::HepJamesRandom engine(seedL4);
if(std::fabs(CLHEP::RandExpZiggurat::shoot(&engine) - 1.59601) > epsilon) {
output << "Error, results changed for RandExpZiggurat.\n";
return 1;
}
}
{
long seedL5 = 100;
CLHEP::HepJamesRandom engine(seedL5);
if(std::fabs(CLHEP::RandGamma::shoot(&engine) - 1.25744) > epsilon) {
output << "Error, results changed for RandGamma.\n";
return 1;
}
}
{
long seedL6 = 100;
CLHEP::HepJamesRandom engine(seedL6);
if(std::fabs(CLHEP::RandGaussZiggurat::shoot(&engine) - (-0.138855)) > epsilon) {
output << "Error, results changed for RandGaussZiggurat.\n";
return 1;
}
}
return 0;
#endif
}
#if defined __GNUC__
#if __GNUC__ > 3 && __GNUC_MINOR__ > 6
#pragma GCC diagnostic pop
#endif
#if __GNUC__ > 4
#pragma GCC diagnostic pop
#endif
#endif
#if defined __INTEL_COMPILER
#pragma warning pop
#endif
#ifdef __clang__
#pragma clang diagnostic pop
#endif
|