大佬教程收集整理的这篇文章主要介绍了c – 减少时push :: tuple的自定义最小运算符,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
但是,我无法使其工作,并且当前正在计算向量中存在的结果
以下代码重现了该问题:
#include <thrust/device_vector.h> #include <thrust/iterator/zip_iterator.h> #include <thrust/tuple.h> #include <thrust/sequence.h> typedef thrust::tuple<unsigned int,unsigned int> DereferencedIteratorTuple; struct tuple_snd_min{ __host__ __device__ bool operator()(const DereferencedIteratorTuple& lhs,const DereferencedIteratorTuple& rhs){ return (thrust::get<1>(lhs) < thrust::get<1>(rhs)); } }; void f(){ thrust::device_vector<unsigned int> X(10); thrust::device_vector<unsigned int> Y(10); thrust::sequence(X.begin(),X.end()); thrust::sequence(Y.begin(),Y.end()); X[0] = 5; Y[0] = 5; X[1] = 50; // X: 5 50 2 3 4 5 6 7 8 9 // Y: 5 1 2 3 4 5 6 7 8 9 typedef thrust::device_vector<unsigned int>::iterator UIntIterator; typedef thrust::tuple<UIntIterator,UIntIterator> IteratorTuple; thrust::zip_iterator<IteratorTuple> first = thrust::make_zip_iterator(thrust::make_tuple(X.begin(),Y.begin())); thrust::tuple<unsigned int,unsigned int> init = first[0]; thrust::tuple<unsigned int,unsigned int> min = thrust::reduce(first,first + 10,init,tuple_snd_min()); printf("(%d,%d)\n",thrust::get<0>(min),thrust::get<1>(min)); // should return (50,1) // returns (0,0) }
感谢Jared Hoberock的评论,我能够解决这个问题.
typedef thrust::tuple<unsigned int,unsigned int> DereferencedIteratorTuple; struct tuple_snd_min{ __host__ __device__ const DereferencedIteratorTuple& operator()(const DereferencedIteratorTuple& lhs,const DereferencedIteratorTuple& rhs) { if(thrust::get<1>(lhs) < thrust::get<1>(rhs)) return lhs; else return rhs; } };
struct tuple_snd_min{ __host__ __device__ bool operator()(const DereferencedIteratorTuple& lhs,const DereferencedIteratorTuple& rhs){ return (thrust::get<1>(lhs) < thrust::get<1>(rhs)); } };
您的仿函数需要定义如下:
struct tuple_snd_min{ __host__ __device__ int operator()(const DereferencedIteratorTuple& lhs,const DereferencedIteratorTuple& rhs){ return (thrust::get<1>(lhs) < thrust::get<1>(rhs)) ? thrust::get<1>(lhs) : thrust::get<1>(rhs); } };
以上是大佬教程为你收集整理的c – 减少时push :: tuple的自定义最小运算符全部内容,希望文章能够帮你解决c – 减少时push :: tuple的自定义最小运算符所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。