Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenMP specify thread number of a for loop iteration

I'm using the following command to parallelize a single loop over the available threads of the program:

#pragma omp parallel for num_threads(threads)
for(long i = 0; i < threads; i++)
{
    array[i] = calculateStuff(i,...);
}

For technical reasons, I would like to guarantee that thread number 0 executes i=0, and thread number 1 executes i=1. In other words, I want always i=omp_get_thread_num(). Is this possible?

like image 211
The Quantum Physicist Avatar asked Nov 29 '25 12:11

The Quantum Physicist


1 Answers

Using a loop is a waste of resources in that particular case. Simply use omp_get_thread_num():

#include <omp.h>

#pragma omp parallel num_threads(threads)
{
    int i = omp_get_thread_num();
    array[i] = calculateStuff(i,...);
}
like image 113
Hristo Iliev Avatar answered Dec 02 '25 02:12

Hristo Iliev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!