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?
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,...);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With