Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Experiment.cpp
- *
- * Created on: 16 maj 2016
- * Author: oramus
- */
- #include <stdlib.h>
- #include <iostream>
- #include <omp.h>
- #include "Experiment.h"
- #include "Distribution.h"
- #define DEBUG_ON_
- using namespace std;
- Experiment::Experiment(int balls, int drawsNumber)
- {
- this->balls = balls;
- this->drawsNumber = drawsNumber;
- hmax = 0; // wyznaczamy maksymalna sume
- hmin = 0; // i najmniejsza sume
- for (int i = 0; i < drawsNumber; i++)
- {
- hmax += balls - i;
- hmin += i + 1; // 1 + 2 + 3 + ... liczba losowan
- }
- cout << "Histogram min: " << hmin << " max: " << hmax << endl;
- histogram = new long[hmax + 1];
- for (long i = 0; i < hmax + 1; i++)
- histogram[i] = 0;
- }
- void Experiment::clearUsed(bool *used)
- {
- for (int i = 0; i < balls; i++)
- {
- used[i] = false;
- }
- }
- long Experiment::singleExperimentResult(drand48_data *drandBuf, bool *used)
- {
- long sum = 0;
- int ball;
- double p, randomBall, randomProbability;
- clearUsed(used);
- for (int i = 0; i < drawsNumber;)
- {
- drand48_r(drandBuf, &randomBall);
- ball = 1 + (int)((double)balls * randomBall);
- if (used[ball - 1])
- continue;
- p = Distribution::getProbability(i + 1, ball); // pobieramy prawdopodobienstwo wylosowania tej kuli
- drand48_r(drandBuf, &randomProbability);
- if (randomProbability < p) // akceptacja wyboru kuli z zadanym prawdopodobienstwem
- {
- #ifdef DEBUG_ON
- cout << "Dodano kule o numerze " << ball << endl;
- #endif
- used[ball - 1] = true;
- sum += ball; // kule maja numery od 1 do balls wlacznie
- i++;
- }
- }
- /// cout << "Suma = " << sum << endl;
- return sum;
- }
- Result *Experiment::calc(long experiments)
- {
- #pragma omp parallel
- {
- long *privateHistogram = new long[hmax + 1];
- for (int i = 0; i < hmax + 1; i++)
- {
- privateHistogram[i] = 0;
- }
- bool *used = new bool[balls];
- struct drand48_data drandBuf;
- int seed = 1202107158 + omp_get_thread_num() * 1999;
- srand48_r(seed, &drandBuf);
- #pragma omp for schedule(dynamic)
- for (long l = 0; l < experiments; l++)
- {
- privateHistogram[singleExperimentResult(&drandBuf, used)]++;
- }
- #pragma omp critical
- {
- for (int i = 0; i < hmax + 1; i++)
- {
- histogram[i] += privateHistogram[i];
- }
- }
- }
- long maxID = 0;
- long maxN = 0;
- double sum = 0.0;
- long values = 0;
- #pragma omp parallel
- {
- long localMaxID = 0;
- long localMaxN = 0;
- double localSum = 0.0;
- long localValues = 0;
- #pragma omp for
- for (long idx = hmin; idx <= hmax; idx++)
- {
- if (localMaxN < histogram[idx])
- {
- localMaxN = histogram[idx];
- localMaxID = idx;
- }
- localSum += idx * histogram[idx];
- localValues += histogram[idx];
- }
- #pragma omp critical
- {
- if (localMaxN > maxN)
- {
- maxN = localMaxN;
- maxID = localMaxID;
- }
- sum += localSum;
- values += localValues;
- }
- }
- // indeks to wartosc, histogram -> liczba wystapien
- return new Result(maxID, maxN, sum / values, values);
- }
- Experiment::~Experiment()
- {
- delete[] histogram;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement