0

I'm running a very simple routine in C++ with openMP and measuring the elapsed time... the code goes at reads,

#include <iostream>
#include <math.h>
#include "timer.h"
#include <omp.h>



int main ()
{
    double start,finish;
    int i;
    int n=8000;
    double a[n];
    double b[n];
    double c[n];



    GET_TIME(start);
#pragma omp parallel private(i,a) shared(b,c,n)
    {
#pragma omp for 
        for (i=0; i<n-1; i++)
        b[i] += (a[i] + a[i+1])/2;
#pragma omp for
        for (i=0; i<n-1; i++)
            c[i] += (a[i] + a[i+1])/2;
    } 
    GET_TIME(finish);
    std::cout<< "Elapsed time is" <<(finish-start)<<"seconds";
    return 0;
}

Code with I'm compiling with the following bash script (observe that threads are defined in the environment variable OMP_NUM_THREADS=$n):

#!/bin/bash

clear

g++ -O3 -o test test.cpp -fopenmp 

for n in $(seq 1 8); do
  export OMP_NUM_THREADS=$n
   ./test
    echo threads=$n
done

As a result, a general trend of decreasing the performance with increasing the number of threads is observed as follows: (Of course the numbers can change)...

Elapsed time is0.000161886secondsthreads=1
Elapsed time is0.00019002secondsthreads=2
Elapsed time is0.00226498secondsthreads=3
Elapsed time is0.000210047secondsthreads=4
Elapsed time is0.000212908secondsthreads=5
Elapsed time is0.00920105secondsthreads=6
Elapsed time is0.00937104secondsthreads=7
Elapsed time is0.000834942secondsthreads=8

Any suggestions for increasing the performance (instead of decreasing it)? Thank you very much!.

2
  • 1
    If I read your numbers correctly the execution speed increases by a factor of more than 10 when going from 7 threads to 8 -- a significant improvement in performance. I suggest you re-time with much larger and longer loops, and take the average of 3 - 5 runs before trying to draw any conclusions. Sep 4, 2015 at 12:28
  • Thank you Mark.. Perhaps what I get most often is: 8.39233e-05secondsthreads=1 Elapsed time is0.000119925secondsthreads=2 Elapsed time is0.000138044secondsthreads=3 Elapsed time is0.000138044secondsthreads=4 Elapsed time is0.000123978secondsthreads=5 Elapsed time is0.000133991secondsthreads=6 Elapsed time is0.00356102secondsthreads=7 Elapsed time is0.00615597secondsthreads=8.... Is always getting worst when I increase the threads... I know that Loop is not big enough, however is there a way to improve it with other technique as SIMD f.e.? Thank you!.
    – uom0
    Sep 5, 2015 at 12:45

1 Answer 1

2

You can do this instead, it will increase the operation done by each thread. This is to overcome the overhead needed to start a new thread by actually having the thread do some more work. Also, there is no need to declare the b, c or n as shared.

#pragma omp parallel private(i,a,b,c,n)
{
#pragma omp for schedule(static)
    for (i=0; i<n-1; i++){
        b[i] += (a[i] + a[i+1])/2;
        c[i] += (a[i] + a[i+1])/2;}
}
4
  • 1
    Thank you, but still decreasing the performance... look... Elapsed time is3.38554e-05secondsthreads=1 Elapsed time is0.000103951secondsthreads=2 Elapsed time is0.000139952secondsthreads=3 Elapsed time is0.000194788secondsthreads=4 Elapsed time is0.000201225secondsthreads=5 Elapsed time is0.000176191secondsthreads=6 Elapsed time is0.000298023secondsthreads=7 Elapsed time is0.000277996secondsthreads=8
    – uom0
    Sep 4, 2015 at 11:48
  • Try using the static scheduling. I have modified the answer to include it. Your test is a small one, results highly vary between trials.
    – Mido
    Sep 4, 2015 at 12:15
  • Thank you again! Results, infact are varying a bit because is a small problem, but always with one core is much more faster that with more cores ... I tried what your post ans as reads, is getting wors with increasing threads... Thank you!. Elapsed time is8.39233e-05secondsthreads=1 Elapsed time is0.000119925secondsthreads=2 Elapsed time is0.000138044secondsthreads=3 Elapsed time is0.000138044secondsthreads=4 Elapsed time is0.000123978secondsthreads=5 Elapsed time is0.000133991secondsthreads=6 Elapsed time is0.00356102secondsthreads=7 Elapsed time is0.00615597secondsthreads=8
    – uom0
    Sep 4, 2015 at 13:05
  • 2
    This is a problem often seen in HPC. There are advantages to doing computation in parallel but you need to be aware of how well your code scales to larger numbers of processors. With short runs your performance gains are often eaten away by the overhead of creating the additional threads/processes. communication between the threads/processes, and additional load on a shared resource like memory or disk. Come up with a run that takes multiple seconds to complete using Mido's optimized code and see if the numbers look better as thread count increases.
    – chuck
    Sep 4, 2015 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.