2014-11-04 36 views
0

對於device_vector,我可以使用其rbegin()方法獲取其反向迭代器。但如何直接從device_ptr構造一個反向迭代器?從CUDA的device_ptr獲取reverse_iterator

可能,這可以通過構建device_vector與device_ptr來實現,代碼如下:

thrust::device_ptr<int> ptr = get_ptr(); 
thrust::device_vector<int> tmpVector(ptr , ptr + N) 
thrust::inclusive_scan_by_key(tmpVector.rbegin(), tmpVector.rend(), ......); 

但我不知道是否會thrust::device_vector<int> tmpVector(ptr , ptr + N)構建新的載體和PTR複製數據或者它只是保留從ptr的參考? Thrust的文檔並不是這個意思。

任何想法?

+0

'thrust :: device_vector tmpVector(ptr,ptr + N)'將構造一個新的向量並複製'ptr'中的數據。你可以使用置換迭代器在我的[你的最後一個問題]答案中演示的方法通過'device_ptr'序列反向迭代(http://stackoverflow.com/questions/26666621/thrust-fill-isolate-space )。 – 2014-11-04 11:11:25

+0

@RobertCrovella,是的,我注意到你這樣做的方式,我修改了你提供的代碼,並將第89行替換爲'thrust :: inclusive_scan_by_key(a.rbegin(),a.rend(),az.rbegin(), asr.rbegin(),head_flag_predicate ());'所以我不需要使用'ra',這爲我節省了時間和內存。但在我自己的程序中,我的'a'數組由其他人提供''device_ptr',所以我在考慮是否可以將'device_ptr'傳輸到'device_vector',以便我仍然可以使用'rbegin()' – Wood 2014-11-04 12:03:56

+0

您可以將'device_ptr'數組複製到設備向量。我不相信你可以在device_ptr數組周圍包裝設備向量。 – 2014-11-04 22:48:36

回答

0

提供一個基於Jared的評論的答案,從未得到答覆的列表中獲得此答案,併爲將來的讀者保留問題。

要從任何類型的迭代器(包括thrust::device_ptr)製作reverse iterator,請使用thrust::make_reverse_iteratorfunction

下面是一個簡單的例子:

$ cat t615.cu 
#include <thrust/device_vector.h> 
#include <thrust/iterator/reverse_iterator.h> 
#include <thrust/device_ptr.h> 
#include <thrust/sequence.h> 
#include <thrust/copy.h> 
#include <iostream> 
#define DSIZE 4 
int main(){ 

    int *data; 
    cudaMalloc(&data, DSIZE*sizeof(int)); 
    thrust::device_ptr<int> my_data = thrust::device_pointer_cast<int>(data); 
    thrust::sequence(my_data, my_data+DSIZE); 
    thrust::copy_n(my_data, DSIZE, std::ostream_iterator<int>(std::cout, ",")); 
    std::cout << std::endl; 
    typedef thrust::device_vector<int>::iterator Iterator;  
    thrust::reverse_iterator<Iterator> r_iter = make_reverse_iterator(my_data+DSIZE); // note that we point the iterator to the "end" of the device pointer area 
    thrust::copy_n(r_iter, DSIZE, std::ostream_iterator<int>(std::cout, ",")); 
    std::cout << std::endl; 
    return 0; 
} 
$ nvcc -arch=sm_35 -o t615 t615.cu 
$ ./t615 
0,1,2,3, 
3,2,1,0, 
$ 

一個反向迭代不創建任何「額外陣列」的創建。