Apr 26, 2019 at 2:19am UTC
Im writing a program that demonstrates /openmp configuration However the following code only outputs "Generating Numbers..." Could anyone help in any way?
#include <iostream>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <time.h>
#include <Windows.h>
using namespace std;
const int NSIZE = INT_MAX / 5;
void gen_numbers(float numbers[], int how_many);
float gen_rand(int min, int max);
float sum(float array[], int num_elements);
float numbers[NSIZE];
int main()
{
unsigned int seed = time(0);
srand(seed);
__int64 start1 = GetTickCount();
cout << "Generating numbers..." << flush;
gen_numbers(numbers, NSIZE);
cout << "done." << endl;
cout << "summing the numbers..." << flush;
float answer = sum(numbers, NSIZE);
cout << "done" << endl;
__int64 end1 = GetTickCount();
cout << "total time: " << end1-start1 << endl;
cout << "Answer: " << answer << endl;
system("pause");
return 0;
}
float sum(float array[], int num_elements)
{
float nsum = 0.0;
//#pragma omp parallel for reduction(+:nsum)
for (int i = 0; i < num_elements; i++)
{
nsum += array[i];
}
return nsum;
}
void gen_numbers(float numbers[], int how_many)
{
//#pragma omp parallel for
for (int i = 0; i < how_many; i++) {
numbers[i] = gen_rand(0, 10);
}
}
float gen_rand(int min, int max)
{
return (min + static_cast <float> (rand()) / (static_cast <float>
(RAND_MAX / (max - min))));
}
Apr 26, 2019 at 5:21am UTC
Please edit your post to put [co de][/code] tags around the code.
Apr 26, 2019 at 6:22am UTC
It takes a few seconds to generate that many numbers so if you don't wait for it to finish you won't see anything more than "Generating numbers...".
Apr 26, 2019 at 7:58am UTC
As @Peter87 says, it's not instantaneous. However, it does seem to run ...
Generating numbers...done.
summing the numbers...done
total time: 10483
Answer: 2.68435e+08
Press any key to continue . . .
And here it is with threads turned on (remove the comments before #pragma omp) - sheesh!
g++ -fopenmp temp.cpp
set OMP_NUM_THREADS=12
a.exe
Generating numbers...done.
summing the numbers...done
total time: 1435
Answer: 2.08363e+09
Press any key to continue . . .
Last edited on Apr 26, 2019 at 8:29am UTC