This repository has been archived on 2022-10-18. You can view files and clone it, but cannot push or open issues or pull requests.
HPC/Project2/Project2-code/hist/hist_omp.cpp

57 lines
1.2 KiB
C++
Raw Permalink Normal View History

2022-10-18 12:39:50 +00:00
#include "walltime.h"
#include <iostream>
#include <random>
#define VEC_SIZE 1000000000
#define BINS 16
using namespace std;
int main() {
double time_start, time_end;
// Initialize random number generator
unsigned int seed = 123;
float mean = BINS / 2.0;
float sigma = BINS / 12.0;
std::default_random_engine generator(seed);
std::normal_distribution<float> distribution(mean, sigma);
// Generate random sequence
// Note: normal distribution is on interval [-inf; inf]
// we want [0; BINS-1]
int *vec = new int[VEC_SIZE];
for (long i = 0; i < VEC_SIZE; ++i) {
vec[i] = int(distribution(generator));
if (vec[i] < 0)
vec[i] = 0;
if (vec[i] > BINS - 1)
vec[i] = BINS - 1;
}
// Initialize histogram
// Set all bins to zero
long dist[BINS];
for (int i = 0; i < BINS; ++i) {
dist[i] = 0;
}
time_start = wall_time();
// TODO Parallelize the histogram computation
for (long i = 0; i < VEC_SIZE; ++i) {
dist[vec[i]]++;
}
time_end = wall_time();
// Write results
for (int i = 0; i < BINS; ++i) {
cout << "dist[" << i << "]=" << dist[i] << endl;
}
cout << "Time: " << time_end - time_start << " sec" << endl;
delete[] vec;
return 0;
}