我有以下简单程序
#include <iostream>
#include <omp>
int main() {
std::cout << "max threads: " << omp_get_max_threads() << "\n";
#pragma parallel num_threads(4)
{
int tid = omp_get_thread_num();
std::cout << "Hello from " << tid << " of " << omp_get_num_threads() << "\n";
#pragma omp for
for (int i = 0; i < 5; i++) {
std::cout << "(" << tid << ", " << i << ")\n";
}
}
}
我正在使用clang++ -fopenmp=libomp main.cpp
进行编译。我能够编译和运行以这种方式编译的其他OpenMP程序。
我希望num_threads(4)
能够使并行区域跨越4个线程运行。相反,我会遇到以下输出:
max threads: 4
Hello from 0 of 1
(0, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
为什么并行区域没有跨越4个线程运行?
答案 0 :(得分:0)
您已将omp
从parallel
pragma中删除。
#pragma omp parallel num_threads(4)