SHARE
TWEET

Untitled

a guest Dec 8th, 2019 113 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Experiment.cpp
  3.  *
  4.  *  Created on: 16 maj 2016
  5.  *      Author: oramus
  6.  */
  7.  
  8. #include <stdlib.h>
  9. #include <iostream>
  10. #include <omp.h>
  11.  
  12. #include "Experiment.h"
  13. #include "Distribution.h"
  14.  
  15. #define DEBUG_ON_
  16.  
  17. using namespace std;
  18.  
  19. Experiment::Experiment(int balls, int drawsNumber)
  20. {
  21.     this->balls = balls;
  22.     this->drawsNumber = drawsNumber;
  23.  
  24.     hmax = 0; // wyznaczamy maksymalna sume
  25.     hmin = 0; // i najmniejsza sume
  26.     for (int i = 0; i < drawsNumber; i++)
  27.     {
  28.         hmax += balls - i;
  29.         hmin += i + 1; // 1 + 2 + 3 + ... liczba losowan
  30.     }
  31.  
  32.     cout << "Histogram min: " << hmin << " max: " << hmax << endl;
  33.  
  34.     histogram = new long[hmax + 1];
  35.  
  36.     for (long i = 0; i < hmax + 1; i++)
  37.         histogram[i] = 0;
  38. }
  39.  
  40. void Experiment::clearUsed(bool *used)
  41. {
  42.     for (int i = 0; i < balls; i++)
  43.     {
  44.         used[i] = false;
  45.     }
  46. }
  47.  
  48. long Experiment::singleExperimentResult(drand48_data *drandBuf, bool *used)
  49. {
  50.     long sum = 0;
  51.     int ball;
  52.     double p, randomBall, randomProbability;
  53.  
  54.     clearUsed(used);
  55.     for (int i = 0; i < drawsNumber;)
  56.     {
  57.         drand48_r(drandBuf, &randomBall);
  58.         ball = 1 + (int)((double)balls * randomBall);
  59.  
  60.         if (used[ball - 1])
  61.             continue;
  62.  
  63.         p = Distribution::getProbability(i + 1, ball); // pobieramy prawdopodobienstwo wylosowania tej kuli
  64.  
  65.         drand48_r(drandBuf, &randomProbability);
  66.         if (randomProbability < p) // akceptacja wyboru kuli z zadanym prawdopodobienstwem
  67.         {
  68.             #ifdef DEBUG_ON
  69.             cout << "Dodano kule o numerze " << ball << endl;
  70.             #endif
  71.             used[ball - 1] = true;
  72.             sum += ball; // kule maja numery od 1 do balls wlacznie
  73.             i++;
  74.         }
  75.     }
  76.  
  77.     /// cout << "Suma = " << sum << endl;
  78.  
  79.     return sum;
  80. }
  81.  
  82. Result *Experiment::calc(long experiments)
  83. {
  84.     #pragma omp parallel
  85.     {
  86.         long *privateHistogram = new long[hmax + 1];
  87.         for (int i = 0; i < hmax + 1; i++)
  88.         {
  89.             privateHistogram[i] = 0;
  90.         }
  91.         bool *used = new bool[balls];
  92.  
  93.         struct drand48_data drandBuf;
  94.         int seed = 1202107158 + omp_get_thread_num() * 1999;
  95.         srand48_r(seed, &drandBuf);
  96.  
  97.         #pragma omp for schedule(dynamic)
  98.         for (long l = 0; l < experiments; l++)
  99.         {
  100.             privateHistogram[singleExperimentResult(&drandBuf, used)]++;
  101.         }
  102.  
  103.         #pragma omp critical
  104.         {
  105.             for (int i = 0; i < hmax + 1; i++)
  106.             {
  107.                 histogram[i] += privateHistogram[i];
  108.             }
  109.         }
  110.     }
  111.  
  112.     long maxID = 0;
  113.     long maxN = 0;
  114.     double sum = 0.0;
  115.     long values = 0;
  116.  
  117.     #pragma omp parallel
  118.     {
  119.         long localMaxID = 0;
  120.         long localMaxN = 0;
  121.         double localSum = 0.0;
  122.         long localValues = 0;
  123.  
  124.         #pragma omp for
  125.         for (long idx = hmin; idx <= hmax; idx++)
  126.         {
  127.             if (localMaxN < histogram[idx])
  128.             {
  129.                 localMaxN = histogram[idx];
  130.                 localMaxID = idx;
  131.             }
  132.             localSum += idx * histogram[idx];
  133.             localValues += histogram[idx];
  134.         }
  135.  
  136.         #pragma omp critical
  137.         {
  138.             if (localMaxN > maxN)
  139.             {
  140.                 maxN = localMaxN;
  141.                 maxID = localMaxID;
  142.             }
  143.             sum += localSum;
  144.             values += localValues;
  145.         }
  146.     }
  147.  
  148.     // indeks to wartosc, histogram -> liczba wystapien
  149.     return new Result(maxID, maxN, sum / values, values);
  150. }
  151.  
  152. Experiment::~Experiment()
  153. {
  154.     delete[] histogram;
  155. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top