东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 1533|回复: 0
打印 上一主题 下一主题

[C/C++] thrust/functional.h中的算子:negate、modulus

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14450
QQ
跳转到指定楼层
楼主
发表于 2021-6-16 11:36:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式



thrust/functional.h中的算子:negate、modulus


  1. #include <thrust/device_vector.h>
  2. #include <thrust/transform.h>
  3. #include <thrust/sequence.h>
  4. #include <thrust/copy.h>
  5. #include <thrust/fill.h>
  6. #include <thrust/replace.h>
  7. #include <thrust/functional.h>
  8. #include <iostream>


  9. //Thrust提供了大量常见的并行算法。其中许多算法在C ++标准库中都有直接类比
  10. //Thrust中的所有算法都具有主机和设备的实现
  11. //除了thrust::copy可以在主机和设备之间复制数据之外,
  12. //Thrust算法的所有迭代器参数都应该位于同一位置:要么全部在主机上,要么全部在设备上。违反此要求时,编译器将生成错误消息
  13. //thrust::fill,它将范围的所有元素设置为指定值。其他转变包括thrust::sequence,thrust::replace当然thrust::transform

  14. //转换是将操作应用于一组(零个或多个)输入范围中的每个元素,然后将结果存储在目标范围内的算法


  15. //thrust/functional.h中的算子:negate、modulus


  16. int main(void)
  17. {
  18.   // allocate three device_vectors with 10 elements
  19.   thrust::device_vector<int> X(10);
  20.   thrust::device_vector<int> Y(10);
  21.   thrust::device_vector<int> Z(10);
  22.   //文件中thrust/functional.h:negate、modulus

  23.   // initialize X to 0,1,2,3, ....
  24.   thrust::sequence(X.begin(), X.end());

  25.   // compute Y = -X  negate负数
  26.   thrust::transform(X.begin(), X.end(), Y.begin(), thrust::negate<int>());

  27.   // fill Z with twos
  28.   thrust::fill(Z.begin(), Z.end(), 2);

  29.   // compute Y = X mod 2
  30.   thrust::transform(X.begin(), X.end(), Z.begin(), Y.begin(), thrust::modulus<int>());

  31.   // replace all the ones in Y with tens
  32.   thrust::replace(Y.begin(), Y.end(), 1, 10);

  33.   // print Y  拷贝还可以这样用啊?
  34.   thrust::copy(Y.begin(), Y.end(), std::ostream_iterator<int>(std::cout, "\n"));
  35.    
  36.   return 0;   
  37. }



复制代码


让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-6-18 01:27 , Processed in 0.175106 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表