我有一个方法,我想在多个线程上运行,但每个线程将返回不同数量的结果。是否可以声明一个私有的、线程特定的变量,即一个列表,然后我可以将其传回主机并合并所有结果?
假设我有一个数组如下:
int[,] arr1 = new int[3,3] {{ 3, 4, 5 }, {4, 5, 6}, {1, 6, 4}};
int[] arr2 = new int[] { 3, 4, 1 };
每个线程将给出 3 个值来分析并记录 arr2 中的值与 arr1 中特定行的值之间的差异。
[Cudafy]
public static void CountAbove(GThread thread, int[] a, int[,] b, list<int> c)
{
int tid = thread.blockIdx.x;
int threshold = a[tid];
for(int i = 0; i < b.GetLength(0); i++)
{
if (threshold < b[tid,i]) c.add(b[tid,i] - threshold);
}
}