如何从数组中并行删除零值 [英] How to remove zero values from an array in parallel

查看:83
本文介绍了如何从数组中并行删除零值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用CUDA从数组中有效地删除零值. 有关零值数量的信息是预先提供的, 应该可以简化此任务.

How can I efficiently remove zero values from an array in parallel using CUDA. The information about the number of zero values is available in advance, which should simplify this task.

重要的是,数字应保持与源数组中的顺序相同, 复制到结果数组时.

It is important that the numbers remain ordered as in the source array, when being copied to the resulting array.

示例:

该数组例如包含以下值: [0,0,19,7,0,3,5,0,0,1] 还有5个值为零的附加信息. 所需的最终结果将是另一个包含以下内容的数组: [19,7,3,5,1]

The array would e.g. contain the following values: [0, 0, 19, 7, 0, 3, 5, 0, 0, 1] with the additional information that 5 values are zeros. The desired end result would then be another array containing: [19, 7, 3, 5, 1]

推荐答案

要消除数组中的某些元素,可以使用推力库的重新排序操作.给定谓词is_not_zero,该谓词返回零值的false,返回其他值的true,您可以这样编写操作

To eliminate some elements from an array you may use Thrust Library's reordering operations. Given a predicate is_not_zero, which returns false for zero values, and true for others, you may write the operation like this

thrust::copy_if(in_array, in_array + size, out_array, is_not_zero);

输出数组将仅包含非零值,因为该谓词表明如此.

the output array will include only the values which are non-zero, because the predicate indicates so.

您还可以将"remove_if"函数与反向谓词一起使用,该谓词返回true代表零,返回false代表其他..

You may also use "remove_if" function with a reverse predicate which return true for zeros, and false for others..

thrust::remove_if(in_array, in_array + size, is_zero);

我建议您看一下Thrust库的压缩示例或一般压缩概念.

I suggest you taking a look at compaction examples of Thrust library, or general compaction concept.

https://github.com/thrust/thrust/blob/master/examples/stream_compaction.cu

这篇关于如何从数组中并行删除零值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆