So, in the aftermath of my previous attempt with openMP, I have realized that I don't have any example of a piece of code which actually runs faster on my system when parallelized compared to serial. Below is a short example of an attempt (which fails), first showing that there are indeed two cores, and that openMP is putting them use, and then timing two brain-dead tasks, one using openMP and the other not. It is very possible that there is something wrong about the task I'm testing on, so I'd appreciate it if someone could come up with another sanity-test, just so I can see with my own eyes that multi-threading CAN work :)
#include <iostream>
#include <vector>
#include <ctime>
#include <cmath>
using namespace std;
#include <omp.h>
int main(int argc, char *argv[])
{
//Below code will be run once for each processor (there are two)
#pragma omp parallel
{
cout << omp_get_thread_num() << endl; //this should output 1 and 0, in random order
}
//The parallel example:
vector <double> a(50000,0);
clock_t start = clock();
#pragma omp parallel for shared(a)
for (int i=0; i < 50000; i++)
{
double StartVal=i;
for (int j=0; j<2000; ++j)
a[i]=(StartVal + log(exp(exp((double) i))));
}
cout<< "Time: " << ( (double) ( clock() - start ) / (double)CLOCKS_PER_SEC ) <<endl;
//The serial example:
start = clock();
for (int i=0; i < 50000; i++)
{
double StartVal=i;
for (int j=0; j<2000; ++j)
a[i]=(StartVal + log(exp(exp((double) i))));
}
cout<< "Time: " << ( (double) ( clock() - start ) / (double)CLOCKS_PER_SEC ) <<endl;
return 0;
}
the output is:
1
0
Time: 4.07
Time: 3.84
Can it be something to do with forloop-optimization that openMP is missing out on? Or is there something wrong with how I measure time? In that case, do you have any ideas for a different test?
Thank you in advance :)
EDIT:
it did it indeed turn out that I was measuring time in a bad way. Using omp_get_wtime()
, the output becomes:
1
0
Time: 4.40776
Time: 7.77676
I guess I'd better go back and have another look at my old question then...