我正在使用CUDA进行一些数组操作/计算(通过Cudafy.NET库,尽管我同样对CUDA/C++方法感兴趣),需要计算数组中的最小值和最大值。其中一个核函数如下:
[Cudafy]
public static void UpdateEz(GThread thread, float time, float ca, float cb, float[,] hx, float[,] hy, float[,] ez)
{
var i = thread.blockIdx.x;
var j = thread.blockIdx.y;
if (i > 0 && i < ez.GetLength(0) - 1 && j > 0 && j < ez.GetLength(1) - 1)
ez[i, j] =
ca * ez[i, j]
+ cb * (hx[i, j] - hx[i - 1, j])
+ cb * (hy[i, j - 1] - hy[i, j])
;
}
我希望能够做到如下操作:
[Cudafy]
public static void UpdateEz(GThread thread, float time, float ca, float cb, float[,] hx, float[,] hy, float[,] ez, out float min, out float max)
{
var i = thread.blockIdx.x;
var j = thread.blockIdx.y;
min = float.MaxValue;
max = float.MinValue;
if (i > 0 && i < ez.GetLength(0) - 1 && j > 0 && j < ez.GetLength(1) - 1)
{
ez[i, j] =
ca * ez[i, j]
+ cb * (hx[i, j] - hx[i - 1, j])
+ cb * (hy[i, j - 1] - hy[i, j])
;
min = Math.Min(ez[i, j], min);
max = Math.Max(ez[i, j], max);
}
}
有没有方便的方法来返回数组的最小值和最大值(针对整个数组,而不仅是每个线程或块)?
thrust
或npp
。 - sgarizvithrust
,可以参考这个例子。 - BenC