C OMP omp_get_wtime () return time 0.00

I used 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); } 
+4
source share
7 answers

Make sure you include the omp.h library in the file header.

 #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 time will tell correctly.

+12
source

Try typing with "% g", which leaves it as a scientific notation.

+2
source

Declare the end time of the program, after which you take the difference between the start time and the end time, print the difference. this should solve it, as I did something similar a few 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 
0
source

Declare the end time of the program, after which you take the difference between the start time and the end time, print the difference. this should solve it, as I did something similar a few months ago.

-1
source

I had the same problem, and while setprecision did the trick in C ++, you can use the following code in c. To see the difference, you need to print the results with high accuracy.

 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); 
-1
source

Your program may be too fast to allow omp_get_wtime . If you only want to measure time and not care about the final content of mZ, you can repeat the test several 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 can also disable printf's inside a parallel block, as this can be a serious cause of slowdown.

-1
source

@mcleod_ideafix was right when he wrote about suppressing printf. You should definitely remove the printf function call from the loop, as this can greatly distort the result. Keep in mind that a call to printf will at some stage involve switching to kernel mode, and this in itself is an expensive operation in terms of processor cycles.

-1
source

Source: https://habr.com/ru/post/1483003/


All Articles