pradyot
pradyot

Reputation: 174

Time Calculation on parallel programs

To calculate the total time taken for a c program we usually make use of functions as clock() and time() present in the header file <time.h> .
The clock() measures the number of ticks the CPU has used (CLOCKS_PER_SEC) after that we can easily divide the difference between the starting and the ending points of the code snippet we wish to calculate by CLOCKS_PER_SEC. This helps us in getting the time taken in sec
Also time(null) returns the number of whole seconds since a particular point in time. referred here

Now here is my problem. To calculate the total time taken (to document speed up operation) by a normal c program which when run using OpenMP, I make use of all the processors present in my system, what is the best approach to calculate the time taken. As clock() would just return the the cumulative time taken to run the program on each processor, basically

(actual_time_taken x omp_get_max_threads())

And time() is a very vague method to calculate as I am interested in calculation of time in milliseconds.

Upvotes: 0

Views: 2061

Answers (2)

jefflarkin
jefflarkin

Reputation: 1279

OpenMP provides omp_get_wtime() for doing exactly this. Is there some reason not to use this function in your code?

Upvotes: 5

John Zwinck
John Zwinck

Reputation: 249093

Since you are programming in C, you should use the function getrusage(). This will tell you the system and user mode times for either the entire process and its children, the current process alone, or all threads. Call it once at the start and once at the end, then take the difference.

Upvotes: -1

Related Questions