【发布时间】:2012-05-09 19:42:35
【问题描述】:
我是在 Visual Studio C# 中使用 OpenCL(带有 OpenCL.NET 库)的新手,目前正在开发一个计算大型 3D 矩阵的应用程序。在矩阵中的每个像素处,计算 192 个唯一值,然后求和以产生该像素的最终值。所以,在功能上,它就像一个 4-D 矩阵,(161 x 161 x 161) x 192。
现在我正在从我的主机代码中调用内核,如下所示:
//C# host code
...
float[] BigMatrix = new float[161*161*161]; //1-D result array
CLCalc.Program.Variable dev_BigMatrix = new CLCalc.Program.Variable(BigMatrix);
CLCalc.Program.Variable dev_OtherArray = new CLCalc.Program.Variable(otherArray);
//...load some other variables here too.
CLCalc.Program.Variable[] args = new CLCalc.Program.Variable[7] {//stuff...}
//Here, I execute the kernel, with a 2-dimensional worker pool:
BigMatrixCalc.Execute(args, new int[2]{N*N*N,192});
dev_BigMatrix.ReadFromDeviceTo(BigMatrix);
示例内核代码发布在下面。
__kernel void MyKernel(
__global float * BigMatrix
__global float * otherArray
//various other variables...
)
{
int N = 161; //Size of matrix edges
int pixel_id = get_global_id(0); //The location of the pixel in the 1D array
int array_id = get_global_id(1); //The location within the otherArray
//Finding the x,y,z values of the pixel_id.
float3 p;
p.x = pixel_id % N;
p.y = ((pixel_id % (N*N))-p.x)/N;
p.z = (pixel_id - p.x - p.y*N)/(N*N);
float result;
//...
//Some long calculation for 'result' involving otherArray and p...
//...
BigMatrix[pixel_id] += result;
}
我的代码目前可以工作,但是我正在寻找此应用程序的速度,我不确定我的工作人员/组设置是否是最佳方法(即工作人员池的尺寸为 161*161*161 和 192 )。
我已经看到了将全局工作池组织成本地工作组以提高效率的其他示例,但我不太确定如何在 OpenCL.NET 中实现它。我也不确定这与在工作池中创建另一个维度有何不同。
所以,我的问题是:我可以在这里使用本地组吗?如果可以,我将如何组织它们?一般来说,使用本地组与仅调用 n 维工作池有何不同? (即调用 Execute(args, new int[]{(N*N*N),192}),而不是本地工作组大小为 192?)
感谢大家的帮助!
【问题讨论】:
-
BigMatrix 中的值是否根据 BigMatrix 中的任何其他值计算得出?在计算中如何使用“p”?您能否提供有关您尝试进行的计算的更多信息?
-
当然。计算中不使用 BigMatrix 的值,仅使用索引。 BigMatrix 的值最初为 0,并设置为计算结果。该计算使用 BigMatrix 中当前像素的索引 (p.x,p.y,p.z) 来找到指向由 otherArray 中的值指定的另一个点的向量。因此,每个计算都是唯一的,因为每个像素对于 otherArray 中的 192 个点中的每一个都有唯一的向量。该向量的大小和距离用于 BigMatrix 中最终值的最终计算。
标签: c# opencl gpu opencl.net