rngpooltest.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include <math.h>
  2. #include <sys/param.h>
  3. #include <sys/times.h>
  4. #include <sys/types.h>
  5. #include <iostream>
  6. #include <gsl/gsl_rng.h>
  7. #include <gsl/gsl_randist.h>
  8. #include "rng_queue_pool.hpp"
  9. #include "rng_paraqueue_pool.hpp"
  10. using namespace std;
  11. int main(int argc, char** argv)
  12. {
  13. cout << "argc=" << argc << "\n";
  14. int ArgNum=1;
  15. bool UseThreadRng=false;
  16. if (argc >=ArgNum) {
  17. UseThreadRng = atoi(argv[ArgNum++]);
  18. }
  19. int NLoops = 100;
  20. if (argc >=ArgNum) {
  21. NLoops = atoi(argv[ArgNum++]);
  22. }
  23. int NVectors=10;
  24. if (argc >=ArgNum) {
  25. NVectors = atoi(argv[ArgNum++]);
  26. }
  27. int NumbersPerVector=2000;
  28. if (argc >=ArgNum) {
  29. NumbersPerVector = atoi(argv[ArgNum++]);
  30. }
  31. cout << "NLoops=" << NLoops << "\n";
  32. const double PoissonLambda=0.3;
  33. // initialize random number generator
  34. const gsl_rng_type * T;
  35. gsl_rng_env_setup();
  36. T = gsl_rng_default;
  37. gsl_rng* gslr = gsl_rng_alloc (T);
  38. // unsigned long int seed = (long)(time( NULL ));
  39. unsigned long int seed = (long)(42);
  40. gsl_rng_set(gslr, seed);
  41. IRngQueue * rnq;
  42. IRngQueuePool* p;
  43. // if (UseThreadRng) rnq = RngQueuePool::getRngQueuePool()->getRngQueue();
  44. if (UseThreadRng) rnq = RngParaQueuePool::getRngQueuePool()->getRngQueue(kRngPoisson, PoissonLambda, 10, 2000);
  45. //IRngQueue * rnq2 = RngQueuePool::getRngQueuePool()->getRngQueue();
  46. int NSleep=2;
  47. /* cout << "sleeping "; fflush(stdout);
  48. for (int i=0;i<NSleep;++i) {
  49. sleep(1);
  50. cout << "."; fflush(stdout);
  51. }
  52. cout << " ready\n"; fflush(stdout);*/
  53. const int NSum=10000;
  54. double decrease = 0.9;
  55. double qdec = 1./decrease;
  56. double random1=0;
  57. double random2=0;
  58. struct tms t_time,u_time;
  59. long r1,r2;
  60. r1 = times(&t_time);
  61. int NOutput=10;
  62. int Mod = NLoops/NOutput;
  63. if (Mod<1) Mod=1;
  64. int NRandOut=200;
  65. int Count=0;
  66. double RandomNum=0;
  67. for (int i=0; i<NLoops;++i)
  68. {
  69. double sum=0;
  70. double lastsum=0;
  71. for (int j=0;j<NSum;++j) {
  72. sum +=0.0001;
  73. if (UseThreadRng)
  74. RandomNum = rnq->getRandomNumber();
  75. else
  76. RandomNum = gsl_ran_poisson(gslr, PoissonLambda);
  77. /* if (Count++ < NRandOut) {
  78. cout << RandomNum << " ";
  79. if (Count % 20 == 0) cout << "\n";
  80. }*/
  81. sum += RandomNum;
  82. sum /= qdec;
  83. sum += lastsum;
  84. sum /= qdec;
  85. sum += exp(-lastsum);
  86. sum += exp(-sum);
  87. if (sum > 10) {
  88. sum = sum / lastsum;
  89. if (UseThreadRng)
  90. RandomNum = rnq->getRandomNumber();
  91. else
  92. RandomNum = gsl_ran_poisson(gslr, PoissonLambda);
  93. sum += RandomNum;
  94. /* if (Count++ < NRandOut) {
  95. cout << RandomNum << " ";
  96. if (Count % 20 == 0) cout << "\n";
  97. }*/
  98. }
  99. lastsum = sum;
  100. }
  101. if (i%Mod == 0) {
  102. cout << "\nNum = " << sum << "";
  103. Count=0;
  104. }
  105. }
  106. r2 = times(&u_time);
  107. printf("\nuser time = %f\n", ((float)(u_time.tms_utime-t_time.tms_utime))/(HZ));
  108. printf("system time = %f\n", ((float)(u_time.tms_stime-t_time.tms_stime))/(HZ));
  109. printf("real time = %f\n", ((float)(r2-r1))/(HZ));
  110. return 0;
  111. }