2011-09-29 37 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

我嘗試的第一個,並沒有得到正確的結果: