123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- #include <math.h>
- #include <sys/param.h>
- #include <sys/times.h>
- #include <sys/types.h>
- #include <iostream>
- #include <gsl/gsl_rng.h>
- #include <gsl/gsl_randist.h>
- #include "rng_queue_pool.hpp"
- #include "rng_paraqueue_pool.hpp"
- using namespace std;
- int main(int argc, char** argv)
- {
- cout << "argc=" << argc << "\n";
- int ArgNum=1;
-
- bool UseThreadRng=false;
- if (argc >=ArgNum) {
- UseThreadRng = atoi(argv[ArgNum++]);
- }
- int NLoops = 100;
- if (argc >=ArgNum) {
- NLoops = atoi(argv[ArgNum++]);
- }
- int NVectors=10;
- if (argc >=ArgNum) {
- NVectors = atoi(argv[ArgNum++]);
- }
-
- int NumbersPerVector=2000;
- if (argc >=ArgNum) {
- NumbersPerVector = atoi(argv[ArgNum++]);
- }
-
- cout << "NLoops=" << NLoops << "\n";
- const double PoissonLambda=0.3;
- // initialize random number generator
- const gsl_rng_type * T;
- gsl_rng_env_setup();
- T = gsl_rng_default;
- gsl_rng* gslr = gsl_rng_alloc (T);
- // unsigned long int seed = (long)(time( NULL ));
- unsigned long int seed = (long)(42);
- gsl_rng_set(gslr, seed);
-
- IRngQueue * rnq;
- IRngQueuePool* p;
- // if (UseThreadRng) rnq = RngQueuePool::getRngQueuePool()->getRngQueue();
- if (UseThreadRng) rnq = RngParaQueuePool::getRngQueuePool()->getRngQueue(kRngPoisson, PoissonLambda, 10, 2000);
- //IRngQueue * rnq2 = RngQueuePool::getRngQueuePool()->getRngQueue();
- int NSleep=2;
- /* cout << "sleeping "; fflush(stdout);
- for (int i=0;i<NSleep;++i) {
- sleep(1);
- cout << "."; fflush(stdout);
- }
- cout << " ready\n"; fflush(stdout);*/
-
- const int NSum=10000;
- double decrease = 0.9;
- double qdec = 1./decrease;
-
- double random1=0;
- double random2=0;
-
- struct tms t_time,u_time;
- long r1,r2;
-
- r1 = times(&t_time);
- int NOutput=10;
- int Mod = NLoops/NOutput;
- if (Mod<1) Mod=1;
- int NRandOut=200;
- int Count=0;
-
- double RandomNum=0;
- for (int i=0; i<NLoops;++i)
- {
- double sum=0;
- double lastsum=0;
- for (int j=0;j<NSum;++j) {
- sum +=0.0001;
- if (UseThreadRng)
- RandomNum = rnq->getRandomNumber();
- else
- RandomNum = gsl_ran_poisson(gslr, PoissonLambda);
- /* if (Count++ < NRandOut) {
- cout << RandomNum << " ";
- if (Count % 20 == 0) cout << "\n";
- }*/
- sum += RandomNum;
- sum /= qdec;
- sum += lastsum;
- sum /= qdec;
- sum += exp(-lastsum);
- sum += exp(-sum);
- if (sum > 10) {
- sum = sum / lastsum;
- if (UseThreadRng)
- RandomNum = rnq->getRandomNumber();
- else
- RandomNum = gsl_ran_poisson(gslr, PoissonLambda);
- sum += RandomNum;
- /* if (Count++ < NRandOut) {
- cout << RandomNum << " ";
- if (Count % 20 == 0) cout << "\n";
- }*/
- }
- lastsum = sum;
- }
- if (i%Mod == 0) {
- cout << "\nNum = " << sum << "";
- Count=0;
- }
- }
-
- r2 = times(&u_time);
- printf("\nuser time = %f\n", ((float)(u_time.tms_utime-t_time.tms_utime))/(HZ));
- printf("system time = %f\n", ((float)(u_time.tms_stime-t_time.tms_stime))/(HZ));
- printf("real time = %f\n", ((float)(r2-r1))/(HZ));
- return 0;
- }
|