CUDA Thrust - 如何使用多个不同大小的设备向量编写函数?

CUDA Thrust - How can I write a function using multiple device vectors with different sizes?

我一直在尝试找出如何使用四个推力设备矢量执行简单的熵计算。

我有四个设备向量,代表两个键值对。第一对向量包含键和该键出现的次数。第二对包含与用于计算熵的箱配对的键。在第二个向量对中,键出现多次,每个实例代表不同的 bin。

看起来像这样:

设备向量对 1

键值 6 8 9

计数 1 3 2

设备向量对 2

键值 6 8 8 9 9

二进制值 1 1 2 1 1

Result Vector(包含计算出的熵结果)

键值 8

熵 0.602

我打算做的是使用第一个向量对来检查一个键是否出现了足够的次数来计算熵。如果计数足够大,第二个向量对将用于计算该键的 bin 值的熵。我将需要使用该特定键的所有 bin 值。例如,如果我想计算至少出现 3 次的密钥的熵,我会在第一个向量对中发现 KeyVal 8 已准备就绪。然后,我会在第二对中搜索 KeyVal 8 的所有实例,并使用它们对应的 BinVal 计算熵。熵计算很简单,只需将每个相关值的 BinVal*Log(BinVal) 相加即可。在我的示例中,它将是 entropy = 1*log(1) + 2*log(2)。

但是,我不知道如何让这部分工作。我试过使用 thrust::for_each 来查找所有出现次数足以进行测试的键,但我认为不可能在第二个向量对中搜索键并在 [=43] 中执行计算=] 函数。

有人对完成此任务的其他方法有建议吗?

感谢您的帮助。

我考虑的两个想法是:

想法一:

  1. 计算所有的熵
  2. Select符合条件的

想法 B:

  1. Select符合条件的传入数据
  2. 计算熵。

想法 A 似乎在做不必要的工作 - 计算需要或可能不需要的熵。然而,当我完成想法 B 的过程时,我最终添加了很多步骤(例如计算前缀和)来完成想法 B 的步骤 1,这似乎并没有更好。所以我现在将介绍想法 A。也许 m.s。否则其他人会出现 post 更好的东西。

想法 A 的第 1 步由 thrust::reduce_by_key 处理,并使用适当的函子计算特定的熵函数

想法 A 的第 2 步由 thrust::copy_if

处理
$ cat t827.cu
#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/copy.h>
#include <thrust/reduce.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/iterator/discard_iterator.h>
#include <math.h>

// THRESH determines the minimum Counts value required for a KeyVal Entropy calculation to occur
#define THRESH 2

using namespace thrust::placeholders;


struct my_entropy : public thrust::unary_function<float, float>
{
  __host__ __device__
  float operator()(float val){
    return val*log10f(val);}  // if you want napierian log, change this to logf
};

int main(){

  int KeyVal1[]={6, 8, 9};
  int Counts[] ={1, 3, 2};
  int KeyVal2[]={6, 8, 8, 9, 9};
  float BinVal[] ={1, 1, 2, 1, 1};

  int dsize1 = sizeof(KeyVal1)/sizeof(int);
  int dsize2 = sizeof(KeyVal2)/sizeof(int);

  thrust::device_vector<int> d_KeyVal1(KeyVal1, KeyVal1+dsize1);
  thrust::device_vector<int> d_Counts(Counts, Counts+dsize1);
  thrust::device_vector<int> d_KeyVal2(KeyVal2, KeyVal2+dsize2);
  thrust::device_vector<float> d_BinVal(BinVal, BinVal+dsize2);


  // method 1 - just compute all entropies, then select the desired ones
  thrust::device_vector<float> entropies(dsize2);
  thrust::reduce_by_key(d_KeyVal2.begin(), d_KeyVal2.end(), thrust::make_transform_iterator(d_BinVal.begin(), my_entropy()), thrust::make_discard_iterator(), entropies.begin());
  thrust::device_vector<int> res_keys(dsize1);
  thrust::device_vector<float>res_ent(dsize1);
  int res_size = thrust::copy_if(thrust::make_zip_iterator(thrust::make_tuple(d_KeyVal1.begin(), entropies.begin())), thrust::make_zip_iterator(thrust::make_tuple(d_KeyVal1.end(), entropies.end())), d_Counts.begin(), thrust::make_zip_iterator(thrust::make_tuple(res_keys.begin(), res_ent.begin())), _1 >= THRESH) - thrust::make_zip_iterator(thrust::make_tuple(res_keys.begin(), res_ent.begin()));
  std::cout << "Counts threshold: " << THRESH << std::endl <<  "selected keys: " << std::endl;
  thrust::copy_n(res_keys.begin(), res_size, std::ostream_iterator<int>(std::cout, ","));
  std::cout << std::endl << "calculated entropies: " << std::endl;
  thrust::copy_n(res_ent.begin(), res_size, std::ostream_iterator<float>(std::cout, ","));
  std::cout << std::endl;

  return 0;
}
[bob@cluster1 misc]$ nvcc -o t827 t827.cu
$ ./t827
Counts threshold: 2
selected keys:
8,9,
calculated entropies:
0.60206,0,
$