gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:51:37 25 4
gpt4 key购买 nike

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

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

看起来像这样:

设备 vector 对 1

key 值 6 8 9

计数 1 3 2

设备 vector 对 2

key 值 6 8 8 9 9

二进制值 1 1 2 1 1

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

键值 8

熵 0.602

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

但是,我不知道如何让这部分工作。我已经尝试使用 thrust::for_each 来查找所有出现足够次数以供测试的键,但我认为不可能在第二个 vector 对中搜索键并在 for_each 函数中执行计算。

是否有人对实现此目标的其他方法有建议?

感谢您的帮助。

最佳答案

我考虑的两个想法是:

想法A:

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

想法 B:

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

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

想法 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,
$

关于c++ - CUDA Thrust - 如何使用多个不同大小的设备 vector 编写函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31175242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com