gpt4 book ai didi

c++ - OpenMP 似乎没有并行运行

转载 作者:搜寻专家 更新时间:2023-10-31 01:31:48 24 4
gpt4 key购买 nike

我正在用 C++ 尝试我的第一个 openmp 程序,我的代码是:

#pragma omp parallel for num_threads(2)
for (int i=0;i<16;++i)
{
printf( "Thread %d works with idx %d\n", omp_get_thread_num(), i);
}

我用 g++ -O3 -fopenmp -std=c++11 编译

但是,当我运行程序时,我得到:

Thread 0 works with idx 0
Thread 0 works with idx 1
Thread 0 works with idx 2
Thread 0 works with idx 3
Thread 0 works with idx 4
Thread 0 works with idx 5
Thread 0 works with idx 6
Thread 0 works with idx 7
Thread 1 works with idx 8
Thread 1 works with idx 9
Thread 1 works with idx 10
Thread 1 works with idx 11
Thread 1 works with idx 12
Thread 1 works with idx 13
Thread 1 works with idx 14
Thread 1 works with idx 15

它是并行运行的吗?我期待的是 thread0 - thread1 - thread0 - thread1,一个接一个。

这里会出现什么问题?

最佳答案

是的,它是并行运行的。您可以看到正在打印的两个不同线程的 ID。

I was expecting something like thread0 - thread1 - thread0 - thread1, one after another.

由于您没有定义并行循环的调度,默认情况下它将循环迭代分成 block 并将它们分配给线程。在您的情况下,thread 0 具有 from 0 to 7 block ,thread 1 具有 8 - 15 block 。

如果您希望线程以循环方式工作(一个接一个),您必须声明一个静态并行 for with scheduling of chunk = 1,例如:#pragma omp parallel for schedule (static, 1)

#pragma omp parallel for schedule (static,1) num_threads(2)
for (int i=0;i<16;++i)
{
printf( "Thread %d works with idx %d\n", omp_get_thread_num(), i);
}

有关 openMP 循环静态调度的更多信息:

Divide the loop into equal-sized chunks or as equal as possible in the case where the number of loop iterations is not evenly divisible by the number of threads multiplied by the chunk size. By default, chunk size is loop_count/number_of_threads.Set chunk to 1 to interleave the iterations.

来源:OpenMP* Loop Scheduling

关于c++ - OpenMP 似乎没有并行运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44993234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com