The*_*ist 2 c++ parallel-processing multithreading openmp
我正在使用以下命令在程序的可用线程上并行化单个循环:
#pragma omp parallel for num_threads(threads)
for(long i = 0; i < threads; i++)
{
array[i] = calculateStuff(i,...);
}
Run Code Online (Sandbox Code Playgroud)
由于技术原因,我想保证线程号 0 执行i=0,线程号 1 执行i=1。换句话说,我总是想要i=omp_get_thread_num(). 这可能吗?
在这种特殊情况下,使用循环是一种资源浪费。只需使用omp_get_thread_num():
#include <omp.h>
#pragma omp parallel num_threads(threads)
{
int i = omp_get_thread_num();
array[i] = calculateStuff(i,...);
}
Run Code Online (Sandbox Code Playgroud)