Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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#_Arrays_Optimization_Parallel.for - Fatal编程技术网

C# 二维数组的空间列中是否可能有一个常量?

C# 二维数组的空间列中是否可能有一个常量?,c#,arrays,optimization,parallel.for,C#,Arrays,Optimization,Parallel.for,我正在努力提高并行性。因为在我的主要代码部分。这个循环有主要的计算,每个输出需要完成100多万次(我需要8000万次输出)。因此,任何轻微的改进都可能对执行时间产生严重影响。 我知道IF条件会降低并行计算速度。另外,我知道,对于特殊位置,可能主要变量(U[I,j]和V[I,j])总是零。所以,如果我可以为这些数组的特殊列指定常量零(不想在计算中改变),我就可以从代码中消除if条件 Before calculation: | 1 1 1 0 1| | 1 1 1 0 1| | 1 1 1 0 1|

我正在努力提高并行性。因为在我的主要代码部分。这个循环有主要的计算,每个输出需要完成100多万次(我需要8000万次输出)。因此,任何轻微的改进都可能对执行时间产生严重影响。 我知道IF条件会降低并行计算速度。另外,我知道,对于特殊位置,可能主要变量(U[I,j]和V[I,j])总是零。所以,如果我可以为这些数组的特殊列指定常量零(不想在计算中改变),我就可以从代码中消除if条件

Before calculation:
| 1 1 1 0 1|
| 1 1 1 0 1|
| 1 1 1 0 1|
| 1 1 1 0 1|
After calculation:
| 3 1 8 0 5|
| 1 4 4 0 1|
| 7 3 1 0 8|
| 1 1 5 0 7|
我想要一个列,它的值始终保持为零

如何为二维数组的空间列指定常量(零)

作为示例,上述部分如下所示:

double[,] U= new double[nx,ny];
double[,] V= new double[nx,ny];

Parallel.For(0,nx,i =>
{
   For (j=0; j<ny ; j++)
   {
     if (i!=a && i!=b &&i!=c &&i!=d &&)
     {
       U[i,j]= ...; // A big chunk of calculations
       V[i,j]=... ;// A big chunk of calculations
     }
  }
}
double[,]U=新的双精度[nx,ny];
双精度[,]V=新双精度[nx,ny];
对于(0,nx,i=>
{

对于(j=0;j而言,这难道不是某种改善吗

Parallel.For(0,nx,i =>
{
   if (i!=a && i!=b &&i!=c &&i!=d &&)
   {
       For (j=0; j<ny ; j++)
       {
           U[i,j]= ...; // A big chunk of calculations
           V[i,j]=... ;// A big chunk of calculations
       }
   }
}
Parallel.For(0,nx,i=>
{
如果(i!=a&&i!=b&&i!=c&&i!=d&)
{

对于(j=0;j而言,这难道不是某种改善吗

Parallel.For(0,nx,i =>
{
   if (i!=a && i!=b &&i!=c &&i!=d &&)
   {
       For (j=0; j<ny ; j++)
       {
           U[i,j]= ...; // A big chunk of calculations
           V[i,j]=... ;// A big chunk of calculations
       }
   }
}
Parallel.For(0,nx,i=>
{
如果(i!=a&&i!=b&&i!=c&&i!=d&)
{

对于(j=0;j在一个单独的内核中计算边界,因为只有它们有“if”子句。然后计算内部,不带任何if条件。期望2倍的加速

//interior (dont include borders)
Parallel.For(1,nx-1,i =>
{

       For (j=1; j<ny-1 ; j++)
       {
           U[i,j]= ...; // A big chunk of calculations
           V[i,j]=... ;// A big chunk of calculations
       } 

}

//exterior 1
Parallel.For(xx,xx1,i =>
{
   //another calculation
}

//exterior 2
Parallel.For(xx1,yy,i =>
{
   //another calculation
}

//exterior 3
Parallel.For(yy,yy1,i =>
{
   //another calculation
}

//exterior 4
Parallel.For(yy1,xx,i =>
{
   //another calculation
}
//内部(不包括边框)
对于(1,nx-1,i=>
{
对于(j=1;j
{
//另一个计算
}
//外部2
对于(xx1,yy,i=>
{
//另一个计算
}
//外部3
Parallel.For(yy,yy1,i=>
{
//另一个计算
}
//外部4
对于(yy1,xx,i=>
{
//另一个计算
}

使用内环的C++ DLL,对于GPGPU->30X加速,有10倍以上的加速(SIMD)甚至OpenCL。

< P>计算单独的内核中的边界,因为只有它们有“if”子句。然后计算内部没有任何Cnodidiices。期望2X加速。
//interior (dont include borders)
Parallel.For(1,nx-1,i =>
{

       For (j=1; j<ny-1 ; j++)
       {
           U[i,j]= ...; // A big chunk of calculations
           V[i,j]=... ;// A big chunk of calculations
       } 

}

//exterior 1
Parallel.For(xx,xx1,i =>
{
   //another calculation
}

//exterior 2
Parallel.For(xx1,yy,i =>
{
   //another calculation
}

//exterior 3
Parallel.For(yy,yy1,i =>
{
   //another calculation
}

//exterior 4
Parallel.For(yy1,xx,i =>
{
   //another calculation
}
//内部(不包括边框)
对于(1,nx-1,i=>
{
对于(j=1;j
{
//另一个计算
}
//外部2
对于(xx1,yy,i=>
{
//另一个计算
}
//外部3
Parallel.For(yy,yy1,i=>
{
//另一个计算
}
//外部4
对于(yy1,xx,i=>
{
//另一个计算
}


< P>使用内部循环的C++ DLL,对于GPGPU->30X加速,有10倍的加速比(SIMD)甚至OpenCL。< /P>我已经编辑了你的标题。请看,“一致”是“不,不应该”。.Location of reference?你听说过数组有特殊列吗?@JohnSaunders:我们能给列赋值吗?好的,没有办法让数组有特殊列。你必须更改算法以跳过你认为应该跳过的列。但是,我强烈建议你将代码配置为fi找出花费时间的位置。很可能您试图解决错误的问题。特别是,您可能存在线程争用问题,而不是CPU时间太长的问题。我已编辑了您的标题。请参阅“”,其中的共识是“不,他们不应该”.Location of reference?你听说过数组有特殊列吗?@JohnSaunders:我们能给列赋值吗?好的,没有办法让数组有特殊列。你必须更改算法以跳过你认为应该跳过的列。但是,我强烈建议你将代码配置为fi找出花在哪里的时间。很可能你试图解决错误的问题。特别是,你可能有线程争用问题,而不是花太多CPU时间的问题。+1:我是个白痴。这可能是他的大问题,也可能不是,我肯定是被数组中特殊列的想法所抛弃的,b但是,嘿,
i
在这个内循环中是不变的,所以如果
在这个内循环中是不变的,那么,当然,把它从内循环中去掉!@JohnSaunders是的,我不确定这是否会有帮助。我想编译器可能已经在优化它了。另外,我想知道
a
b
c
d
是OP想要排除的唯一索引吗?如果有一个更大的集合,那么可以想象,
并行。使用迭代器返回索引值的ForEach
可能更好。不过,真正的答案是,“profile,profile,profile”@JohnSaunders:你说得对。这没有帮助。但这是一个有趣的观点。谢谢你+1:我是个白痴。这可能不是他的大问题,我肯定是被数组中的特殊列的想法所抛弃的,但是,嘿,
I
在那个内循环中是不变的,所以
如果
在那个内循环中是不变的,那么,当然,采取行动吧它脱离了内部循环!@JohnSaunders是的,我不确定这是否会有帮助。我想编译器可能已经在优化它了。另外,我想知道
a
b
c
d
是否是OP想要排除的唯一索引?如果有更大的索引集,那么可以想象
并行。ForEach
带有迭代器返回索引值可能更好。不过,真正的答案是,“profile,profile,profile”@JohnSaunders:你说得对。这没有帮助。但这是一个有趣的问题。谢谢你。问题是计算区域中的例外点不在边界内。因为你熟悉物理概念,我在空腔中有一堵墙,空腔的左侧和右侧有速度,但在壁区域中速度为零。因此,我应该始终在C++中,你可以用C++中的DLL输出来加速,在C++中使用DLLIMPLE,在C空间中使用适当的C空间名称,问题是计算区域中的异常点不在边界。你对物理c熟悉吗
//interior (dont include borders)
Parallel.For(1,nx-1,i =>
{

       For (j=1; j<ny-1 ; j++)
       {
           U[i,j]= ...; // A big chunk of calculations
           V[i,j]=... ;// A big chunk of calculations
       } 

}

//exterior 1
Parallel.For(xx,xx1,i =>
{
   //another calculation
}

//exterior 2
Parallel.For(xx1,yy,i =>
{
   //another calculation
}

//exterior 3
Parallel.For(yy,yy1,i =>
{
   //another calculation
}

//exterior 4
Parallel.For(yy1,xx,i =>
{
   //another calculation
}