|
thrust/functional.h中的算子:negate、modulus
- #include <thrust/device_vector.h>
- #include <thrust/transform.h>
- #include <thrust/sequence.h>
- #include <thrust/copy.h>
- #include <thrust/fill.h>
- #include <thrust/replace.h>
- #include <thrust/functional.h>
- #include <iostream>
- //Thrust提供了大量常见的并行算法。其中许多算法在C ++标准库中都有直接类比
- //Thrust中的所有算法都具有主机和设备的实现
- //除了thrust::copy可以在主机和设备之间复制数据之外,
- //Thrust算法的所有迭代器参数都应该位于同一位置:要么全部在主机上,要么全部在设备上。违反此要求时,编译器将生成错误消息
- //thrust::fill,它将范围的所有元素设置为指定值。其他转变包括thrust::sequence,thrust::replace当然thrust::transform
- //转换是将操作应用于一组(零个或多个)输入范围中的每个元素,然后将结果存储在目标范围内的算法
- //thrust/functional.h中的算子:negate、modulus
- int main(void)
- {
- // allocate three device_vectors with 10 elements
- thrust::device_vector<int> X(10);
- thrust::device_vector<int> Y(10);
- thrust::device_vector<int> Z(10);
- //文件中thrust/functional.h:negate、modulus
- // initialize X to 0,1,2,3, ....
- thrust::sequence(X.begin(), X.end());
- // compute Y = -X negate负数
- thrust::transform(X.begin(), X.end(), Y.begin(), thrust::negate<int>());
- // fill Z with twos
- thrust::fill(Z.begin(), Z.end(), 2);
- // compute Y = X mod 2
- thrust::transform(X.begin(), X.end(), Z.begin(), Y.begin(), thrust::modulus<int>());
- // replace all the ones in Y with tens
- thrust::replace(Y.begin(), Y.end(), 1, 10);
- // print Y 拷贝还可以这样用啊?
- thrust::copy(Y.begin(), Y.end(), std::ostream_iterator<int>(std::cout, "\n"));
-
- return 0;
- }
复制代码
|
|