我试图通过在GPU的线程上实现for循环来优化我的代码。我正在尝试使用using::transform消除两个for循环。C++中的代码如下所示:
ka_index = 0;
for (int i = 0; i < N_gene; i++)
{
for (int j = 0; j < n_ka_d[i]; j++ )
{
co0 = get_coeff0(ka_vec_d[ka_index]);
act[i] += (co0*ka_val_d[ka_index]);
ka_index++;
}
act[i] = pow(act[i],n);
}我正在估计上述循环中常微分方程(ODE)的系数,并已使用推力将所有数据传输到设备上。考虑这样的情况,其中基因的数量由N_gene表示。第一个for循环必须运行N_gene多次。第二个for循环受每个基因的激活子(基因库中的其他友好基因)数量的限制。每个基因都有许多激活因子(友好的基因,它们的存在增加了基因I的浓度),由n_ka载体的元件代表。n_kai的值可以从0到N_gene - 1。ka_val表示每个激活剂ka的激活度量。ka_vec_d具有激活基因i的基因索引。
我正在尝试使用迭代器来表示这些循环,但无法做到。我很熟悉在单个for循环中使用thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple)),但是很难想出一种使用counting_iterator或转换迭代器实现两个for循环的方法。任何指针或帮助转换这两个for循环将不胜感激。耽误您时间,实在对不起!
发布于 2013-04-06 19:38:02
这看起来像是一个reduce问题。我认为你可以通过zip迭代器和thrust::reduce_by_key来使用thrust::transform。此解决方案的概要如下:
// generate indices
std::vector< int > hindices;
for( size_t i=0 ; i<N_gene ; ++i )
for( size_t j=0 ; j<n_ka_d[i] ; ++j )
hindices.push_back( i );
thrust::device_vector< int > indices = hindices;
// generate tmp
// trafo1 implements get_coeff0( get< 0 >( t ) ) * get< 1 >( t);
thrust::device_vector< double > tmp( N );
thrust::transform(
thrust::make_zip_iterator(
thrust::make_tuple( ka_vec_d.begin() , ka_val_d.begin() ) ) ,
thrust::make_zip_iterator(
thrust::make_tuple( ka_vec_d.end() , ka_val_d.end() ) ) ,
tmp.begin() , trafo1 );
// do the reduction for each ac[i]
thrust::device_vector< int > indices_out( N );
thrust::reduce_by_key( indices.begin() , indices.end() , tmp.begin() ,
ac.begin() , indices_out.begin() );
// do the pow transformation
thrust::transform( ac.begin() , ac.end() , ac.begin() , pow_trafo );这也可以通过transform_iterators进行优化,以减少thrust::transform和thrust::recuce_by_key的调用次数。
https://stackoverflow.com/questions/15823015
复制相似问题