Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure execution time of each thread in openmp?

I'd like to measure the time that each thread spends doing a chunk of code. I'd like to see if my load balancing strategy equally divides chunks among workers. Typically, my code looks like the following:

#pragma omp parallel for schedule(dynamic,chunk) private(i)
for(i=0;i<n;i++){
//loop code here
}

UPDATE I am using openmp 3.1 with gcc

like image 919
Marouen Avatar asked Jan 16 '17 14:01

Marouen


People also ask

How is thread execution time calculated?

How do you calculate: Log time intervals when the thread starts and at swap-in and swap-out. Aggregate all of them and you'll have the execution time of your thread.

How do I find the number of threads on OMP?

omp_get_num_threads() The omp_get_num_threads function returns the number of threads in the team currently executing the parallel region from which it is called. The function binds to the closest enclosing PARALLEL directive.

How many threads does OpenMP use?

The obvious drawback of the baseline implementation that we have is that it only uses one thread, and hence only one CPU core. To exploit all CPU cores, we must somehow create multiple threads of execution.


1 Answers

You can just print the per-thread time this way (not tested, not even compiled):

#pragma omp parallel
{
    double wtime = omp_get_wtime();
    #pragma omp for schedule( dynamic, 1 ) nowait
    for ( int i=0; i<n; i++ ) {
        // whatever
    }
    wtime = omp_get_wtime() - wtime;
    printf( "Time taken by thread %d is %f\n", omp_get_thread_num(), wtime );
}

NB the nowaitthan removes the barrier at the end of the for loop, otherwise this wouldn't have any interest.

And of couse, using a proper profiling tool is a way better approach...

like image 75
Gilles Avatar answered Nov 14 '22 22:11

Gilles