我正在玩omp任务功能,并遇到了问题。我有以下代码:
void function1(int n, int j)
{
//Serial part
#pragma omp parallel
{
#pragma omp single nowait
{
//execcute function1() for every n
for (i=0; i<n; i++)
{
//create a task for computing only large numbers of n
if (i <= 10)
//execute serial
else
#pragma omp task
//call function again
function1(n, j+1);
printf("task id:\n", omp_get_thread_num());
}
}
}
}
现在代码将生成正确的结果,但性能比原始串行版本慢得多。经过一些调查后,我发现所有任务都在线程0中执行,无论总共有4个线程在运行。有谁知道这里发生了什么?提前谢谢!
答案 0 :(得分:0)
omp single nowait
pragma意味着以下块将由单个线程执行。在这种情况下,这意味着您的整个循环由一个线程执行。这应该可以解决您的问题:
void function1(int n, int j)
{
//Serial part
#pragma omp parallel
{
//execcute function1() for every n
for (i=0; i<n; i++) //Notice this is outside the pragma
{
#pragma omp single nowait //Notice this is inside the loop
{
//create a task for computing only large numbers of n
if (i <= 10)
//execute serial
else
#pragma omp task
//call function again
function1(n, j+1);
printf("task id:\n", omp_get_thread_num());
}
}
}
}