问题
I have used a omp_get_wtime() but when i want to print the time i always get 0.00, where is the problem ?
#define SIZE 500
#define nthreads 10
(...)
void sumTab(int mX[][SIZE], int mY[][SIZE], int mZ[][SIZE]) {
int i,k;
double start = omp_get_wtime();
#pragma omp parallel for schedule(dynamic,3) private(i) num_threads(nthreads)
for(i=0 ; i<SIZE ; i++)
{
for(k=0 ; k<SIZE ; k++)
{
mZ[i][k]=mX[i][k]+mY[i][k];
printf("Thread no %d \t [%d] [%d] result: %d\n", omp_get_thread_num(),i,k, mZ[i][k]);
}
}
printf("Time: \t %f \n", omp_get_wtime()-start);
}
回答1:
Make sure you include the omp.h library in the header of the file.
#include <omp.h>
double start_time = omp_get_wtime();
#pragma omp parallel [...]
// code
double time = omp_get_wtime() - start_time;
This library will remove this warning in compilation:
warning: implicit declaration of function ‘omp_get_wtime’ [-Wimplicit-function-declaration]
And the time will show correctly.
回答2:
Try to print with "% g" that leaves it as scientific notation.
回答3:
Declare the time where the program ends, after which you take the difference between the start time and the end time, output the difference. that should solve it as i did something similar some months ago
THis is what your code should look like:
double dif;
double start = omp_get_wtime( ); //start the timer
//beginning of computation
..
...
//end of computation
double end = omp_get_wtime();// end the timer
dif = end - start // stores the difference in dif
printf("the time of dif is %f", dif);
//this should point you in the way
回答4:
This is because of precision loss in converting the double to float.
Try to print time with format specifier "%ld" for double insted of "%f".
printf("the time of dif is %lf", dif);
The program execution takes time in milliseconds or maybe less than that.
回答5:
Declare the time where the program ends, after which you take the difference between the start time and the end time, output the difference. that should solve it as i did something similar some months ago
回答6:
Your routine could be too fast for the resolution of omp_get_wtime
. If you only want to measure the time and don't care about the final contents of mZ, you can repeat the test a number of times and divide the final number by the number of repetitions:
#define REPS 1024
...
...
double acumtime = 0.0;
for (rep = 0; rep < REPS; rep++)
{
double start = omp_get_wtime();
#pragma omp parallel for schedule(dynamic,3) private(i) num_threads(nthreads)
for(i=0 ; i<SIZE ; i++)
{
for(k=0 ; k<SIZE ; k++)
{
mZ[i][k]=mX[i][k]+mY[i][k];
printf("Thread no %d \t [%d] [%d] result: %d\n", omp_get_thread_num(),i,k, mZ[i][k]);
}
}
acumtime += omp_get_wtime()-start;
}
printf ("Elapsed time is: %f\n", acumtime/REPS);
You may also want to supress printf's
inside the parallel block, as this might be a severe cause of slowdown.
回答7:
I had this same problem and while setprecision did the trick in c++, you can use the following code in c. In order to see the difference you have to print the results with high precision.
double exec_time;
double start = omp_get_wtime();
//beginning of computation
...
//end of computation
double end = omp_get_wtime();
exec_time = end - start;
printf("the time difference is %15.15f", exec_time);
回答8:
@mcleod_ideafix was right when he wrote about the suppression of printf. You should definitely remove the call to printf function from within the loop as it may skew the result greatly. Bear in mind that call to printf will at some stage involve transition to kernel mode and this itself is costly operation in terms of CPU cycles.
来源:https://stackoverflow.com/questions/16777810/c-omp-omp-get-wtime-returning-time-0-00