2011-09-29 59 views
1

我怎么能把这个简单的代码转换成推力码?如何在推力转换过程中重新排序矢量?

for (i=0;i<cA-rA;i++) 
    sn[i]=c[n_index[i]]-sn[i]; 

更多信息: CA和RA是常量的整数,所以我们可以海外商品会有作为 'n' 个= CA-RA SN:浮子的阵列(N) n_index:整型阵列(N) Ç :浮点数组(cA)

我的问题是与n_index [i]指向C数组的元素。 谢谢!

#include <thrust/device_vector.h> 
#include <thrust/iterator/permutation_iterator.h> 
#include <thrust/transform.h> 
#include <thrust/sequence.h> 
#include <thrust/functional.h> 

int main() 
{ 
    size_t n = 100; 

    // declare storage 
    thrust::device_vector<int> sn(n); 
    thrust::device_vector<int> n_index(n); 
    thrust::device_vector<int> c(n); 

    // initialize vectors with some sequential values for demonstrative purposes 
    thrust::sequence(sn.begin(), sn.end()); 
    thrust::sequence(n_index.begin(), n_index.end()); 
    thrust::sequence(c.begin(), c.end()); 

    // sn[i] = c[n_index[i]] - sn[i] 
    thrust::transform(thrust::make_permutation_iterator(c.begin(), n_index.begin()), 
        thrust::make_permutation_iterator(c.end(), n_index.end()), 
        sn.begin(), 
        sn.begin(), 
        thrust::minus<int>()); 

    return 0; 
} 

回答

3

您可以通过使用permutation_iterator融合thrust::transform与“聚”操作实现这一点。第二个permutation_iterator需要在的末尾向量。

请尝试以下修正:

// sn[i] = c[n_index[i]] - sn[i] 
thrust::transform(thrust::make_permutation_iterator(c.begin(), n_index.begin()), 
      thrust::make_permutation_iterator(c.end(), n_index.end()), 
      sn.begin(), 
      sn.begin(), 
      thrust::minus<int>()); 
+0

其实我不是得到相同的结果,因为它应该任何想法,为什么? –

+0

如果您编辑您的原始文章以包含您尝试移植的代码的更多详细信息,则可以调整解决方案。特别是,解决方案取决于你的文章中未知的'''''''','''''''','''''''''''n_index''的值。 –

+0

只是编辑它,再次感谢。 如果您尝试取消注释// sn [i] = c [n_index [i]] - sn [i]并将其保存到sn2中作为示例,并交叉检查它们是不同的两个数组。 –

2

我尝试的第一个,并没有得到正确的结果: