Reputation: 923
I'm trying to point out the difference between serial and parallel program with OpenMP. I work on scientific Linux. This is a simple program that performs a sum and a multiplication, then sum the results.
I used the function omp_get_wtime()
to check the execution time and the result is that the part parallelized runs more slowly than the serial part.
I think the problem is in the cases, because both threads enter in the switch, and this increases the execution time.
I wish that the first thread makes the sum and the second makes the multiplication, in order to speed the final result. Can you help me?
At the beginning of the program i use:
#define NUM_THREADS 2
...
omp_set_num_threads(NUM_THREADS);
This is the program:
double a,b,c,d,g,timer,timer2;
printf("Insert 2 numbers \n"); cin >> a; cin >> b;
timer = omp_get_wtime();
c = a+b;
d = a*b;
g = c+d;
printf("the result is: %f\n\n",g);
printf("time: %f seconds\n\n",omp_get_wtime()-timer);
timer2 = omp_get_wtime();
#pragma omp parallel
{
switch ( omp_get_thread_num() )
{
case 1: c=a+b;
case 2: d=a*b;
}
}
g = c+d;
printf("parallel time: %f seconds\n\n",g,omp_get_wtime()-timer2);
Upvotes: 1
Views: 145
Reputation: 181705
There is some constant overhead to spinning up the threads and distributing the work. For a single trivial operation like a multiplication, this overhead far outweighs the time saved by performing the operations in parallel.
You're more likely to see benefits if you give the threads some actual work to do.
Upvotes: 4