C# 在GPU全局内存中存储选择性元素

C# 在GPU全局内存中存储选择性元素,c#,cuda,parallel-processing,cudafy.net,C#,Cuda,Parallel Processing,Cudafy.net,我们正在使用CUDAfy.NET在GPU上进行比较。为此,我们将传递两个数组,其中一个包含数据,另一个存储结果。 我只想在结果数组中存储那些满足一定条件的元素。但数组最终会在条件不满足的情况下生成不需要的条目。 如何从结果数组中过滤这些不需要的条目,并将过滤后的数组返回主函数 [Cudafy] public static void Comparisons(GThread thread, int[] a,int[] c, int iter) { int tx = thread.thread

我们正在使用CUDAfy.NET在GPU上进行比较。为此,我们将传递两个数组,其中一个包含数据,另一个存储结果。 我只想在结果数组中存储那些满足一定条件的元素。但数组最终会在条件不满足的情况下生成不需要的条目。 如何从结果数组中过滤这些不需要的条目,并将过滤后的数组返回主函数

[Cudafy]
public static void Comparisons(GThread thread, int[] a,int[] c, int iter)
{
    int tx = thread.threadIdx.x;
    if(tx < iter)
    {
        if(a[tx] < tolerance)  //tolerance is some user defined number
        {
            c[tx] = a[tx];
        }
    }
}
[Cudafy]
公共静态void比较(GThread线程,int[]a,int[]c,int iter)
{
int tx=thread.threadIdx.x;
if(tx
您必须在多个内核过程中执行此操作

示例:
a=[1,2,1,2,1,2]
公差=2

第一关:
创建一个数组,其中包含1表示“保留元素”,0表示“放弃元素”
p=[1,0,1,0,1,0]

第二遍:
在p数组上执行并行前缀和。
i=[0,1,1,2,2,3]
(关于这个话题有很多白皮书)

第三遍:
使用a、p和i.
每个元素一个线程。
如果p[threadIdx.x]等于1,则将[threadIdx.x]放入c[i[threadIdx.x]]
(您可以在这里使用共享内存来更好地合并对c数组的写入)

结果数组c将包含[1,1,1]