Reputation: 580
I want to measure the time that each thread is performing a task in OpenMP. How can I do that?
Upvotes: 1
Views: 54
Reputation: 51393
A rudimentary way of doing this for a parallel region would be as follows:
int total_threads = ...;
double time_taken_by_threads[total_threads];
#pragma omp parallel region num_threads(total_threads)
{
int threadID = omp_get_thread_num();
double start = omp_get_wtime();
// parallel task
double end = omp_get_wtime();
time_taken_by_threads[threadID] = end - start;
}
for(int i = 0; i < total_threads; i++)
printf("Thread %d took, %ld (s)", time_taken_by_threads[i];
You create an array to store the time taken per thread. You use omp_get_wtime before and after the parallel task performed by the thread. Finally, you save the result in the array. And outside the parallel region you check the time that each thread has taken.
This works fine with the parallel region but might not be so great for other OpenMP parallel constructors such as tasks for instead.
The most robust solution is to use a profiler for OpenM for instance VTune.
Upvotes: 3