我正在尝试开始在C中使用基本的OpenMP功能。我对'omp parallel for'的基本理解使我相信以下应该在线程之间分配以下循环迭代并且应该并发执行。我得到的输出如下。代码如下。我的hello world例子中是否有一些微妙的东西?
来自omp线程0的Hello World 来自omp线程0的Hello World 来自omp线程0的Hello World 来自omp线程0的Hello World 来自omp线程0的Hello World 来自omp线程0的Hello World 等。
int HelloFunc()
{
int i;
int numthreads = 8;
#pragma omp parallel for default(none) num_threads(numthreads) private(i)
for (i = 0; i < 100; i++)
{
int tid = omp_get_thread_num();
printf("Hello world from omp thread %d\n", tid);
}
return -1;
}
int main()
{
int result = HelloFunc();
}
答案 0 :(得分:4)
#include <omp.h>
#include <stdio.h>
int HelloFunc()
{
int i;
int numthreads = 8;
#pragma omp parallel for default(none) num_threads(numthreads) private(i)
for (i = 0; i < 100; i++)
{
int tid = omp_get_thread_num();
printf("Hello world from omp thread %d\n", tid);
}
return -1;
}
int main()
{
HelloFunc();
return 0;
}
然后编译:
gcc t.c -fopenmp -Wall
并运行:
./a.out
输出:
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 2
Hello world from omp thread 2
Hello world from omp thread 4
Hello world from omp thread 4
Hello world from omp thread 4
Hello world from omp thread 4
Hello world from omp thread 2
Hello world from omp thread 1
Hello world from omp thread 4
Hello world from omp thread 2
Hello world from omp thread 1
Hello world from omp thread 4
Hello world from omp thread 2
Hello world from omp thread 2
Hello world from omp thread 4
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 2
Hello world from omp thread 4
Hello world from omp thread 1
Hello world from omp thread 2
Hello world from omp thread 4
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 4
Hello world from omp thread 2
Hello world from omp thread 1
Hello world from omp thread 4
Hello world from omp thread 4
Hello world from omp thread 2
Hello world from omp thread 2
Hello world from omp thread 2
Hello world from omp thread 2
答案 1 :(得分:2)
您的计算机可能只使用一个线程来运行此程序。 OMP不强制它与多个线程一起运行,它只是告诉编译器它可以并设置必要的环境来实现它。
没有办法强迫OMP在更多线程中执行某些操作。你不会想要,因为OMP会自动设置所有内容以使其尽可能快地运行。