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];
- bool* used = new bool[balls];
- int threadId = omp_get_thread_num();
- int totalThreads = omp_get_num_threads();
- int chunkSize = (int)(experiments / totalThreads);
- int startIdx = threadId * chunkSize;
- int endIdx;
- if (threadId == totalThreads - 1) {
- endIdx = experiments;
- } else {
- endIdx = (threadId + 1) * chunkSize;
- }
- struct drand48_data drandBuf;
- int seed = 1202107158 + threadId * 1999;
- srand48_r(seed, &drandBuf);
- #pragma omp for
- for (long l = startIdx; l < endIdx; l++) {
- privateHistogram[singleExperimentResult(&drandBuf, used)]++;
- }
- #pragma omp critical
- {
- for (int i = 0; i < hmax + 1; i++) {
- histogram[i] += privateHistogram[i];
- }
- }
- }
- long maxID = 0;
- long minID = 0;
- long maxN = 0;
- long minN = experiments;
- double sum = 0.0;
- long values = 0;
- for (long idx = hmin; idx <= hmax; idx++) {
- if (maxN < histogram[idx]) {
- maxN = histogram[idx];
- maxID = idx;
- }
- sum += idx * histogram[idx];
- values += histogram[idx];
- }
- // 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