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;
}
3
  • 1
    possible duplicate of OpenMP time and clock() calculates two different results Commented May 21, 2015 at 9: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. Commented May 21, 2015 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. Commented May 21, 2015 at 10:31

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.