CUDA初学者-Thrust - Algorithms - Searching(6)

本人 CUDA小白一枚,要是有什么不对,还望各位大佬指点。
本文及后面的几篇将分别从几个方面来大概阐述一下Thrust的一些接口。原来的网址在这里

1.Algorithms

1.6 Searching
1.6.1 find

template <typename DerivedPolicy, typename InputIterator, typename T>
__host__ __device__ InputIterator thrust::find(
	const thrust::detail::execution_policy_base< DerivedPolicy > & exec,
  	InputIterator first,
  	InputIterator last,
  	const T & value
);

template <typename InputIterator, typename T>
InputIterator thrust::find(
	InputIterator first,
  	InputIterator last,
  	const T & value
);

在[first, end)中找value,如果能找到,返回对应的位置,找不到则返回last。
例子:

thrust::device_vector<int> input(4);

input[0] = 0;
input[1] = 5;
input[2] = 3;
input[3] = 7;

thrust::device_vector<int>::iterator iter;

iter = thrust::find(thrust::device, input.begin(), input.end(), 3);
// iter = thrust::find(input.begin(), input.end(), 3);
// iter = input.first + 2

1.6.2 find_if

template <typename DerivedPolicy, typename InputIterator, typename Predicate>
__host__ __device__ InputIterator thrust::find_if(
	const thrust::detail::execution_policy_base< DerivedPolicy > & exec,
  	InputIterator first,
  	InputIterator last,
  	Predicate pred
);

template <typename InputIterator, typename Predicate>
InputIterator thrust::find_if(
	InputIterator first,
  	InputIterator last,
  	Predicate pred
);

在[first, last)中找到符合ped的第一个元素的位置,如果能找到,则返回对应的位置,如果找不到则返回last。
例子:

struct greater_than_four {
  __host__ __device__
  bool operator()(int x) {
    return x > 4;
  }
};
thrust::device_vector<int> input(4);

input[0] = 0;
input[1] = 5;
input[2] = 3;
input[3] = 7;

thrust::device_vector<int>::iterator iter;
iter = thrust::find_if(thrust::device, input.begin(), input.end(), greater_than_four()); 
// iter = thrust::find_if(input.begin(), input.end(), greater_than_four()); 
// iter = input.first() + 1

1.6.3 find_if_not

template <typename DerivedPolicy, typename InputIterator, typename Predicate>
__host__ __device__ InputIterator thrust::find_if_not(
	const thrust::detail::execution_policy_base< DerivedPolicy > & exec,
  	InputIterator first,
  	InputIterator last,
  	Predicate pred
);

template <typename InputIterator, typename Predicate>
InputIterator thrust::find_if_not(
	InputIterator first,
  	InputIterator last,
  	Predicate pred
);

在[first, last)中找到第一个不符合pred的,如果能找到,则返回对应的位置,找不到则返回last。
例子:

struct greater_than_four {
  __host__ __device__
  bool operator()(int x) {
    return x > 4;
  }
};
thrust::device_vector<int> input(4);

input[0] = 0;
input[1] = 5;
input[2] = 3;
input[3] = 7;

thrust::device_vector<int>::iterator iter;
iter = thrust::find_if_not(thrust::device, input.begin(), input.end(), greater_than_four()); 
// iter = thrust::find_if_not(input.begin(), input.end(), greater_than_four()); 
// iter = input.first()

1.6.4 mismatch

template <typename DerivedPolicy, typename InputIterator1, typename InputIterator2>
__host__ __device__ thrust::pair< InputIterator1, InputIterator2 > thrust::mismatch(
	const thrust::detail::execution_policy_base< DerivedPolicy > & exec,
  	InputIterator1 first1,
  	InputIterator1 last1,
  	InputIterator2 first2
);

template <typename InputIterator1, typename InputIterator2>
thrust::pair< InputIterator1, InputIterator2 > thrust::mismatch(
	InputIterator1 first1,
  	InputIterator1 last1,
  	InputIterator2 first2
);

template <typename DerivedPolicy, typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
__host__ __device__ thrust::pair< InputIterator1, InputIterator2 > thrust::mismatch(
	const thrust::detail::execution_policy_base< DerivedPolicy > & exec,
  	InputIterator1 first1,
  	InputIterator1 last1,
  	InputIterator2 first2,
  	BinaryPredicate pred
);

template <typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
thrust::pair< InputIterator1, InputIterator2 > thrust::mismatch(
	InputIterator1 first1,
  	InputIterator1 last1,
  	InputIterator2 first2,
  	BinaryPredicate pred
);

依次比较[first1, last1)和[first2, last2)中的元素的,返回第一个不相等的元素的位置。
例子:

thrust::device_vector<int> vec1(4);
thrust::device_vector<int> vec2(4);

vec1[0] = 0;  vec2[0] = 0; 
vec1[1] = 5;  vec2[1] = 5;
vec1[2] = 3;  vec2[2] = 8;
vec1[3] = 7;  vec2[3] = 7;

typedef thrust::device_vector<int>::iterator Iterator;
thrust::pair<Iterator,Iterator> result;

result = thrust::mismatch(thrust::device, vec1.begin(), vec1.end(), vec2.begin());
// result = thrust::mismatch(vec1.begin(), vec1.end(), vec2.begin());
// result = thrust::mismatch(thrust::device, vec1.begin(), vec1.end(), vec2.begin(), thrust::equal_to<int>());
// result = thrust::mismatch(vec1.begin(), vec1.end(), vec2.begin(), thrust::equal_to<int>());
// result 结果 vec1.begin()

1.6.5 partition_point

template <typename DerivedPolicy, typename ForwardIterator, typename Predicate>
__host__ __device__ ForwardIterator thrust::partition_point(
	const thrust::detail::execution_policy_base< DerivedPolicy > & exec,
  	ForwardIterator first,
  	ForwardIterator last,
  	Predicate pred
);

template <typename ForwardIterator, typename Predicate>
ForwardIterator thrust::partition_point(
	ForwardIterator first,
  	ForwardIterator last,
  	Predicate pred
);

例子:

struct is_even {
  __host__ __device__
  bool operator()(const int &x) {
    return (x % 2) == 0;
  }
};

...

int A[] = {2, 4, 6, 8, 10, 1, 3, 5, 7, 9};
int * B = thrust::partition_point(thrust::host, A, A + 10, is_even());
// int * B = thrust::partition_point(A, A + 10, is_even());
// A 中的结果为 {1, 3, 5, 7, 9}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值