Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何摆脱循环';谁的任务团队?_C_Loops_Parallel Processing_Openmp - Fatal编程技术网

C 如何摆脱循环';谁的任务团队?

C 如何摆脱循环';谁的任务团队?,c,loops,parallel-processing,openmp,C,Loops,Parallel Processing,Openmp,我有两种工作要做,foobar1要并行做3次,foobar2要并行做5次。 我的想法是让主线程创建这两个工作组。但我面临一个困难 有可能使线程从并行循环中转义吗?我的意思是在主任务可以执行的地方实现这样一个代码 逃避第一个团队的工作,创建第二个团队 #pragma omp parallel num_threads(8) { // first team of task which will execute the foobar1 function in parallel #pra

我有两种工作要做,
foobar1
要并行做3次,
foobar2
要并行做5次。 我的想法是让主线程创建这两个工作组。但我面临一个困难

有可能使线程从并行循环中转义吗?我的意思是在主任务可以执行的地方实现这样一个代码 逃避第一个团队的工作,创建第二个团队

#pragma omp parallel num_threads(8)
{
    // first team of task which will execute the foobar1 function in parallel

    #pragma omp for schedule(static,1) nowait 
    for(i = 0; i < 3; i++)
    {
        #pragma omp master
        {
            //escape here to create a second team in parallel
        }

        foobar1();
    }

    // second team of task which will execute the foobar2
    #pragma omp for schedule(static,1) nowait
    for(j = 0; j < 5; j++)
    {
        foobar2();
    }
}
#pragma omp并行num_线程(8)
{
//将并行执行foobar1函数的第一组任务
#计划的pragma omp(静态,1)nowait
对于(i=0;i<3;i++)
{
#pragma-omp-master
{
//逃离这里,创建第二个平行团队
}
foobar1();
}
//执行foobar2的第二组任务
#计划的pragma omp(静态,1)nowait
对于(j=0;j<5;j++)
{
foobar2();
}
}
这里有一种(干净的)方法可以完全绕过您的问题:

#pragma omp parallel for num_threads(8)
for(i = 0; i < 8; i++)
{
    if (i < 3){
        foobar1();
    }else{
        foobar2();
    }
}
用于num_线程的pragma omp并行(8) 对于(i=0;i<8;i++) { 如果(i<3){ foobar1(); }否则{ foobar2(); } } 如果这不合适,那么我想到的另一个解决方案是使用嵌套并行。但这太麻烦了。

这里有一个(干净的)方法可以完全绕过您的问题:

#pragma omp parallel for num_threads(8)
for(i = 0; i < 8; i++)
{
    if (i < 3){
        foobar1();
    }else{
        foobar2();
    }
}
用于num_线程的pragma omp并行(8) 对于(i=0;i<8;i++) { 如果(i<3){ foobar1(); }否则{ foobar2(); } }
如果这不合适,那么我想到的另一个解决方案是使用嵌套并行。但这太麻烦了。

你是在尝试拆分8条线程,让3条运行第一个循环,5条运行第二个循环吗?@Mystical,是的,这正是我想要的。你是在尝试拆分8条线程,让3条运行第一个循环,5条运行第二个循环吗?@Mystical,是的,这正是我想要的。@Mystical,谢谢你的回答,现在想象一下
foobar2
contents嵌套循环的情况。这里可用的8个线程中的某些线程是否可能在
foobar2
函数的嵌套循环中工作?非常感谢您的时间,您可能会让它的一些线程“通过”到内部循环。但是,如果没有一个例子,我不太清楚你的意思-作为一个单独的问题可能更好。@Mystical,谢谢你的回答,现在想象一下
foobar2
contents嵌套循环的情况。这里可用的8个线程中的某些线程是否可能在
foobar2
函数的嵌套循环中工作?非常感谢您的时间,您可能会让它的一些线程“通过”到内部循环。但如果没有一个例子,我不太清楚你的意思——作为一个单独的问题可能更好。