1

I've write a C++ code that compute some number. I'm trying to use the OpenMP library to parallelize it. It has 3 nested for loops, and the parallelized one is the outer. I work on Linux with G++ compiler. The code works, but the problem is that the parallelization hasn't improoved performance. Launching it with with 2 threads in my dual core laptop (intel i5 processor with multithreading switched off) takes a bit more time that executing it with just one.

Here it is the code (launched with: g++ source.cpp -fopenmp):

#include <time.h>
clock_t tStart = clock();
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <omp.h>
using namespace std;

int nThreads= 2; // switch to change the number of threads.

int main () {
    int i, j, k, nz= 10;
    double T= 1000, deltaT= 0.005, g= 9.80665, h=10, dz= h/nz, t, z, V, A;
    int nT= T/deltaT;

    #pragma omp parallel for private (j, k, t, z, V, A) num_threads(nThreads)
    for (i=0; i<=nT; i++) {
        t= i*deltaT;
        for (j=0; j<=nz; ++j) {
            z= dz*j;
            for (k=0; k<=1000; k++) {
                V= t*z*g*k;
                A= z*g*k;
            }
        }
    }

    cout << "Time taken: " << setprecision(5) <<  (double)(clock() - tStart)/CLOCKS_PER_SEC << endl;
    return 0;
}
teo2070
  • 23
  • 4
  • 1
    possible duplicate of [OpenMP time and clock() calculates two different results](http://stackoverflow.com/questions/10673732/openmp-time-and-clock-calculates-two-different-results) – High Performance Mark May 21 '15 at 09:49
  • 1
    Pretty hard to say anything meaningful about this code, you are doing it wrong if you don't get 0. There's is nothing to parallelize, the compiler will remove the for-loops and simply compute V = nT * deltaT * dz * nz * g * 1000 since that is all that is needed. – Hans Passant May 21 '15 at 10:11
  • how do you measure the execution time? If you measure the cpu time, what you got is to be expected. openmp adds some overhead and the calculations you are doing take in total roughly the same time as on one cpu. You should measure the real time. – 463035818_is_not_an_ai May 21 '15 at 10:31

1 Answers1

1

Ok, perfect. I was measuring cpu time, and the result was coherent. time() function gives me the number i need to know. Problem solved. Thanks everyone.

teo2070
  • 23
  • 4