日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Thrust快速入门教程(三)——迭代器与静态调度

發(fā)布時間:2025/5/22 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Thrust快速入门教程(三)——迭代器与静态调度 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在這節(jié)中我們曾使用了這樣的表達式,H.begin() 、H.end()、D.begin() + 7。begin()與end()的返回值在C++中被稱為迭代器。vector的迭代器類似于數(shù)組的指針,用于指向數(shù)組的某個元素。H.begin()是指向H容器中數(shù)組第一個元素的迭代器。類似,H.end()指向H容器中的最后一個元素。

雖然說迭代器類似于指針,但它有著更豐富的作用。可以注意到在使用thrust::fill的時候我們并不需要指明這是device_vector的迭代器。這些信息包含在了D.begin()的返回值的迭代器類型中,其類型不同于H.begin()的返回值。當Thrust中的函數(shù)調用時,將根據(jù)迭代器的類型選擇使用主機端還是設備端的算法實現(xiàn)。因為主機/設備調度是在編譯時解析,所以這一過程被稱為被稱為靜態(tài)調度。請注意,這意味著運行時沒有調度進程的開銷。

你可能想知道當“原始”指針作為Thrust函數(shù)的參數(shù)會如何。和STL一樣,Thrust允許這種用法,它會調度主機端算法。如果傳入的指針是指向設備端內存的指針,那么在調用函數(shù)之前需要用thrust::device_ptr封裝。例如:

?

[cpp] view plaincopyprint?
  • size_t?N?=?10;??
  • //?raw?pointer?to?device?memory ??
  • int?*?raw_ptr?;??
  • cudaMalloc?((?void?**)?&?raw_ptr?,?N?*?sizeof?(?int?));??
  • //?wrap?raw?pointer?with?a?device_ptr ??
  • thrust?::?device_ptr?<int?>?dev_ptr?(?raw_ptr?);??
  • //?use?device_ptr?in?thrust?algorithms ??
  • thrust?::?fill?(?dev_ptr?,?dev_ptr?+?N,?(?int?)?0);??
  • size_t N = 10; // raw pointer to device memory int * raw_ptr ; cudaMalloc (( void **) & raw_ptr , N * sizeof ( int )); // wrap raw pointer with a device_ptr thrust :: device_ptr <int > dev_ptr ( raw_ptr ); // use device_ptr in thrust algorithms thrust :: fill ( dev_ptr , dev_ptr + N, ( int ) 0);?

    ?

    如需從 device_ptr中提取“原始”指針需要使用raw_pointer_cast,用法如下:

    ?

    [cpp] view plaincopyprint?
  • size_t?N?=?10;??
  • //?create?a?device_ptr ??
  • thrust?::?device_ptr?<int?>?dev_ptr?=?thrust?::?device_malloc?<int?>(N);??
  • //?extract?raw?pointer?from?device_ptr ??
  • int?*?raw_ptr?=?thrust?::?raw_pointer_cast?(?dev_ptr?);??
  • size_t N = 10; // create a device_ptr thrust :: device_ptr <int > dev_ptr = thrust :: device_malloc <int >(N); // extract raw pointer from device_ptr int * raw_ptr = thrust :: raw_pointer_cast ( dev_ptr );?

    ?

    迭代器另一個區(qū)別于指針的地方在于它可以遍歷各種數(shù)據(jù)結構。例如,STL提供了鏈表容器(std::list),提供雙向的(但不是隨機訪問)的迭代器。雖然Thrust不提供這類容器的設備端實現(xiàn),但是與它們兼容。?

    ?

    [cpp] view plaincopyprint?
  • #?include?<thrust?/?device_vector?.h> ??
  • #?include?<thrust?/?copy?.h> ??
  • #?include?<list?> ??
  • #?include?<vector?> ??
  • int?main?(?void?)??
  • {??
  • //?create?an?STL?list?with?4?values ??
  • std?::?list?<int?>?stl_list?;??
  • stl_list?.?push_back?(10)?;??
  • stl_list?.?push_back?(20)?;??
  • stl_list?.?push_back?(30)?;??
  • stl_list?.?push_back?(40)?;??
  • //?initialize?a?device_vector?with?the?list ??
  • thrust?::?device_vector?<int?>?D(?stl_list?.?begin?()?,?stl_list?.?end?());??
  • //?copy?a?device_vector?into?an?STL?vector ??
  • std?::?vector?<int?>?stl_vector?(D.?size?());??
  • thrust?::?copy?(D.?begin?()?,?D.?end?()?,?stl_vector?.?begin?());??
  • return?0;??
  • }??
  • # include <thrust / device_vector .h> # include <thrust / copy .h> # include <list > # include <vector > int main ( void ) { // create an STL list with 4 values std :: list <int > stl_list ; stl_list . push_back (10) ; stl_list . push_back (20) ; stl_list . push_back (30) ; stl_list . push_back (40) ; // initialize a device_vector with the list thrust :: device_vector <int > D( stl_list . begin () , stl_list . end ()); // copy a device_vector into an STL vector std :: vector <int > stl_vector (D. size ()); thrust :: copy (D. begin () , D. end () , stl_vector . begin ()); return 0; }?

    ?

    ?

    ?

    備注:到目前為止,我們所討論的是十分有用但相當基本的常用迭代器。除了這些常用迭代器,Thrust也提供了counting_iterator和zip_iterator這類特殊迭代器 。雖然他們看起來與常用迭代器一樣,但是特殊迭代器能夠提供更令人興奮的特性。我們將在后面的教程討論這個問題。

    轉載于:https://www.cnblogs.com/carekee/articles/2409503.html

    總結

    以上是生活随笔為你收集整理的Thrust快速入门教程(三)——迭代器与静态调度的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。