假设我有一个键向量
thrust::device_vector<int> keys(10);
keys[0] = 51; // ----->
keys[1] = 51;
keys[2] = 72; // ----->
keys[3] = 72;
keys[4] = 72;
keys[5] = 103; //----->
keys[6] = 103;
keys[7] = 504; // ------>
keys[8] = 504
keys[9] = 504 ;
我事先就知道有
4
不同的键值这个向量。我想填充两个设备数组
pidx[4]
和 pnum[4]
.pidx
数组给了我每个不同键的第一个位置键向量,即标有
---->
的位置在上面的代码片段中。所以,在这个例子中,我应该有 pidx[4] = {0, 2, 5, 7}
. pnum
数组给了我每个键出现的次数。所以,在这个例子中,我应该有pnum[4] = {2, 3, 2, 3}
. 如何使用 CUDA Thrust 执行上述操作?
最佳答案
这不是最佳解决方案,但我想不出更好的方法。
// Use `unique` to grab the distinct values
thrust::device_vector<int> values(4);
thrust::unique_copy( keys.begin(), keys.end(), values.begin() );
// For each of the values use `count` to get the frequencies
for ( int i = 4; i != 0; --i )
pnum[i] = thrust::count( keys.begin(), keys.end(), values[i] );
// Use prefix sum to get the indices
thrust::exclusive_scan( pnum.begin(), pnum.end(), pidx.begin() );
关于cuda - 通过CUDA Thrust查找key的出现次数和key第一次出现的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8792926/