Reputation: 23
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;
}
Upvotes: 1
Views: 138
Reputation: 23
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.
Upvotes: 1