Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.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 OMP 2.0嵌套For循环_C_Multithreading_Openmp - Fatal编程技术网

C OMP 2.0嵌套For循环

C OMP 2.0嵌套For循环,c,multithreading,openmp,C,Multithreading,Openmp,由于我无法使用omp任务(使用visual studio 2015),我正在尝试为嵌套循环任务找到一种解决方法。代码如下: #pragma omp parallel { for (i = 0; i < largeNum; i++) { #pragma omp single { //Some code to be run by a single thread memset(results,

由于我无法使用omp任务(使用visual studio 2015),我正在尝试为嵌套循环任务找到一种解决方法。代码如下:

#pragma omp parallel
    {
        for (i = 0; i < largeNum; i++)
        {
#pragma omp single
        {
            //Some code to be run by a single thread
            memset(results, 0, num * sizeof(results[0]));
        }
#pragma omp for
            for (n = 0; n < num; n++) {
                //Call to my function
                largeFunc(params[n], &resulsts[n])
            }
        }
#pragma omp barrier
    }
#pragma omp并行
{
对于(i=0;i
我希望我的所有线程都执行largeNum次,但等待memset设置为零,然后我希望每个线程都执行largeFunc。我没有发现任何数据依赖项

此时,omp指令在我脑子里乱七八糟的。这个解决方案有效吗?没有任务还有更好的方法吗


谢谢

这段代码怎么样

#pragma omp parallel private( i, n )
for ( i = 0; i < largeNum; i++ ) {
    #pragma omp for
    for ( n = 0; n < num; n++ ) {
        results[n] = 0;
        largeFunc( param[n], &results[n] );
    }
}
#pragma omp parallel private(i,n)
对于(i=0;i

据我所知,如果
结果的实际类型
支持分配到0,则应在不需要
单个
指令的情况下处理初始化部分。此外,您的初始代码缺少
private(i)
声明。最后,应该不需要
屏障。

为什么希望所有线程都执行largeNUM?如果是的话,您是否以某种方式依赖于大函数中的索引i

#pragma omp parallel for
    for (int i = 0; i < largeNum; i++)
    {
#pragma omp single
    {
        //Some code to be run by a single thread
        memset(results, 0, num * sizeof(results[0]));
    }
#pragma omp barrier

// #pragma omp for  -- this is not needed since it has to be coarse on the outermost level. However if the below function does not have anything to do with the outer loop then see the next example
        for (n = 0; n < num; n++) {
            //Call to my function
            largeFunc(params[n], &resulsts[n])
        }
    }

}
#pragma omp parallel for
for(int i=0;i
如果你不依赖我的话

    for (i = 0; i < largeNum; i++)
    {
        //Some code to be run by a single thread
        memset(results, 0, num * sizeof(results[0]));

 #pragma omp parallel for
        for (int n = 0; n < num; n++) {
            //Call to my function
            largeFunc(params[n], &resulsts[n])
        }
    }
(i=0;i { //某些代码将由单个线程运行 memset(results,0,num*sizeof(results[0])); #pragma-omp并行 对于(int n=0;n

但是我觉得你想要第一个。一般来说,您可以在最外层的循环上并行。如果没有足够的工作要做,在innerloop中放置pragma会降低代码的运行速度,因为这样会产生开销。

为什么不使用
pragma omp parallel for
作为
for
for
循环,并删除剩余的omp内容?该循环似乎是唯一一个您希望并行运行的循环。我不想在线程之间拆分largeNum循环。没有依赖关系。将pragma放置在内部不会导致线程连接吗?我试图保持线程分叉以最小化开销,这样你就不会有任何依赖性了?那么您很幸运,可以使用nowait的
#pragma omp
nowait将为您消除隐含的障碍。没有依赖项,除非我希望所有线程都位于内部for之后。是的,然后将
#pragma omp parallel
放在第一个内部循环之前的最外层for循环中。正如我提到的,你也可以使用nowait。如果在下一次操作之前没有隐含的障碍物,或者如果您不需要障碍物,这将为您节省大量开销。但只要测试一下速度对你自己的好处就行了。这些建议在很大程度上取决于您在循环中所做的事情以及主要工作负载在哪里。@Giles我不希望largenum被分割到线程之间,而只是内部循环。此实现在内部for之后是否有隐含的障碍?此实现将产生一个线程团队,所有线程都执行整个
i
迭代,并在每个迭代中共享
n
循环上的工作。
omp for
指令末尾有一个隐式障碍,阻止团队中的任何线程在所有线程完成当前迭代之前启动新的
i
迭代。总而言之,我的观点是它能满足您的需求(尽管我没有看到您首先想要的原因——可能是基准测试)。