日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

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

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

你可能想知道當“原始”指針作為Thrust函數的參數會如何。和STL一樣,Thrust允許這種用法,它會調度主機端算法。如果傳入的指針是指向設備端內存的指針,那么在調用函數之前需要用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 );?

    ?

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

    ?

    [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快速入门教程(三)——迭代器与静态调度的全部內容,希望文章能夠幫你解決所遇到的問題。

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