有没有办法使用Thrust来设置基于索引向量的标志

时间:2015-03-26 13:09:30

标签: c++ cuda gpu thrust

假设我有一个数组A [0,2,4,5,6,7]

我想将其转换为B:

B [1,0,1,0,1,1,1,1]

所以A代表B中哪个元素需要设置为1的索引.A是有序的。

阵列A中的最大数量是已知的,因此事先知道B的大小。

是否有一种简单的方法可以调用Thrust库来完成此任务?

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

这可以在推力的一行中完成:

#include <thrust/iterator/constant_iterator.h>
#include <thrust/iterator/permutation_iterator.h>
#include <thrust/copy.h>
#include <iostream>

int main()
{

  int A[6] = {0,2,4,5,6,7};
  int B[7] = {0};

  const int count_A = 6;

  thrust::copy(thrust::make_constant_iterator(1), thrust::make_constant_iterator(1)+count_A, thrust::make_permutation_iterator(B,A));

  thrust::copy(B, B+7, std::ostream_iterator<int>(std::cout, " "));
  return 0;
}

输出:

1 0 1 0 1 1 1