如何让 OpenMP 与 #pragma omp 任务一起工作?
How to get OpenMP to work with #pragma omp task?
我在 Linux 终端上使用 OpenMP。我用标志 -fopenmp
编译我的程序。我使用的环境确实支持多线程。
#pragma omp parallel num_threads(3)
{
#pragma omp task
R11Inverted = compute_inverse(r11, half);
#pragma omp task
printf("5 I am thread # %d\n", omp_get_thread_num());
R22Inverted = compute_inverse(r22, half);
}
作为一个简单的最小测试,我想做的就是得到printf("5 I am thread # %d\n", omp_get_thread_num());
来表明程序是运行三个线程。基本上 omp_get_thread_num()
应该 return 值 0、1 或 2。
The Output
上面的代码块被重复调用了很多次。然而每次的结果都显示线程没有工作,程序只使用一个线程(线程 0)。
您缺少代码块周围的 {} 以及单个子句。
尝试以下操作:
#pragma omp parallel num_threads(3)
{
#pragma omp single
{
#pragma omp task
R11Inverted = compute_inverse(r11, half);
#pragma omp task
{
printf("5 I am thread # %d\n", omp_get_thread_num());
R22Inverted = compute_inverse(r22, half);
}
}
}
However the results every time is showing that threading is not
working and the program is only using a single thread (Thread 0).
如果您想检查它是否被多线程调用,请改用 omp_get_num_threads,returns 团队的线程数。
#pragma omp parallel num_threads(3)
{
#pragma omp master
{
printf("Total Threads # %d\n", omp_get_num_threads());
}
#pragma omp single
{
#pragma omp task
R11Inverted = compute_inverse(r11, half);
#pragma omp task
{
printf("5 I am thread # %d\n", omp_get_thread_num());
R22Inverted = compute_inverse(r22, half);
}
}
}
如果上面的代码是递归调用的,那么需要适配如下:
#pragma omp parallel num_threads(3)
{
#pragma omp master
printf("Total Threads # %d\n", omp_get_num_threads());
#pragma omp single
compute_inverse(...); // First call to the recursive function
}
和 compute_inverse 函数内部:
#pragma omp task
R11Inverted = compute_inverse(r11, half);
#pragma omp task
{
printf("5 I am thread # %d\n", omp_get_thread_num());
R22Inverted = compute_inverse(r22, half);
}
我在 Linux 终端上使用 OpenMP。我用标志 -fopenmp
编译我的程序。我使用的环境确实支持多线程。
#pragma omp parallel num_threads(3)
{
#pragma omp task
R11Inverted = compute_inverse(r11, half);
#pragma omp task
printf("5 I am thread # %d\n", omp_get_thread_num());
R22Inverted = compute_inverse(r22, half);
}
作为一个简单的最小测试,我想做的就是得到printf("5 I am thread # %d\n", omp_get_thread_num());
来表明程序是运行三个线程。基本上 omp_get_thread_num()
应该 return 值 0、1 或 2。
The Output 上面的代码块被重复调用了很多次。然而每次的结果都显示线程没有工作,程序只使用一个线程(线程 0)。
您缺少代码块周围的 {} 以及单个子句。
尝试以下操作:
#pragma omp parallel num_threads(3)
{
#pragma omp single
{
#pragma omp task
R11Inverted = compute_inverse(r11, half);
#pragma omp task
{
printf("5 I am thread # %d\n", omp_get_thread_num());
R22Inverted = compute_inverse(r22, half);
}
}
}
However the results every time is showing that threading is not working and the program is only using a single thread (Thread 0).
如果您想检查它是否被多线程调用,请改用 omp_get_num_threads,returns 团队的线程数。
#pragma omp parallel num_threads(3)
{
#pragma omp master
{
printf("Total Threads # %d\n", omp_get_num_threads());
}
#pragma omp single
{
#pragma omp task
R11Inverted = compute_inverse(r11, half);
#pragma omp task
{
printf("5 I am thread # %d\n", omp_get_thread_num());
R22Inverted = compute_inverse(r22, half);
}
}
}
如果上面的代码是递归调用的,那么需要适配如下:
#pragma omp parallel num_threads(3)
{
#pragma omp master
printf("Total Threads # %d\n", omp_get_num_threads());
#pragma omp single
compute_inverse(...); // First call to the recursive function
}
和 compute_inverse 函数内部:
#pragma omp task
R11Inverted = compute_inverse(r11, half);
#pragma omp task
{
printf("5 I am thread # %d\n", omp_get_thread_num());
R22Inverted = compute_inverse(r22, half);
}