CUDA thrust zip_iterator tuple transform_reduce

丶灬走出姿态 提交于 2019-12-05 10:49:28
David Lerner

Solved! tup should have been thrust::tuple<float, float>, not thrust::tuple<iter, iter>. Full solution:

#include <iostream>
#include <stdlib.h>
#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include <thrust/tuple.h>
#include <thrust/transform_reduce.h>
#include <thrust/iterator/zip_iterator.h>

typedef thrust::device_vector<float> dvec;
typedef thrust::tuple<float, float> tup;

struct func
{
  __device__ float operator()(tup t) //difsq
  {
     float f = thrust::get<0>(t) - thrust::get<1>(t);
     return f*f;
  }
};

int main()
{
  dvec a(4, 3.f);
  dvec b(4, 2.f);
  auto begin = thrust::make_zip_iterator(thrust::make_tuple(a.begin(), b.begin()));
  auto end = thrust::make_zip_iterator(thrust::make_tuple(a.end(), b.end()));
  std::cout << thrust::transform_reduce(begin, end, func(), 0.0f, thrust::plus<float>()) << std::endl;
  std::cout << "done" << std::endl;
  return 0;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!