Prefix-Sums
并行的前追求和,也叫scan操作,与压实流、基数排序等都是并行算法的重要模块。下面的源码将举例说明使用默认加法的inclusive scan:
# include <thrust / scan .h>
int data [6] = {1, 0, 2, 2, 1, 3};
thrust :: inclusive_scan (data , data + 6, data ); // in - place scan
// data is now {1, 1, 3, 5, 6, 9}
int data [6] = {1, 0, 2, 2, 1, 3};
thrust :: inclusive_scan (data , data + 6, data ); // in - place scan
// data is now {1, 1, 3, 5, 6, 9}
Inclusive scan的每个输出元素为输入数列的相应部分和。例如,data[2] = data[0] + data[1] + data[2]。Exclusive scan类似,但是右移一个位置:
# include <thrust / scan .h>
int data [6] = {1, 0, 2, 2, 1, 3};
thrust :: exclusive_scan (data , data + 6, data ); // in - place scan
// data is now {0, 1, 1, 3, 5, 6}
int data [6] = {1, 0, 2, 2, 1, 3};
thrust :: exclusive_scan (data , data + 6, data ); // in - place scan
// data is now {0, 1, 1, 3, 5, 6}
现在为data[2] = data[0] + data[1]。由例子可见,inclusive_sacn与exclusive_scan允许原址操作。Thrust也提供了函数transform_inclusive_scan与transform_exclusive_scan可以实现在scan操作前对输入数列进行一元操作。完整的scan变体说明请参见documentation。
以上译文来自忆幽梦的博客,请参见:http://blog.cudachina.org/dreampursue/
Reordering
Thrust通过下面的算法为partitioning和stream compaction提供支持:
1.copy_if:复制通过谓词测试的元素。
2. partition(分区):根据谓词重新排列元素(真值在假值前)。
3. remove and remove_if:删除谓词测试失败的元素。
4. unique:在一系列参考文档中删除连续的副本,这些参考文档是关于函数重排和用法实例的完整列表。