生活随笔
收集整理的這篇文章主要介紹了
Thrust快速入门教程(二)——Vector的使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
http://blog.csdn.net/dreampursue/article/details/6278737
Trust?提供了兩個vector容器:host_vector?與?device_vector。按照命名規(guī)則,host_vector位于主機(jī)端,device_vector位于GPU設(shè)備端。Trust的vector容器與STL中的容器類似,是通用的容器,可以存儲任何數(shù)據(jù)類型,可以動態(tài)調(diào)整大小。以下源代碼展示如何使用Thrust的vector容器。
[cpp]?view plaincopy
#?include?<thrust?/?host_vector?.h>??#?include?<thrust?/?device_vector?.h>??#?include?<iostream?>??int?main?(?void?)??{??//?H?has?storage?for?4?integers??thrust?::?host_vector?<int?>?H?(4);??//?initialize?individual?elements??H?[0]?=?14;??H?[1]?=?20;??H?[2]?=?38;??H?[3]?=?46;??//?H.?size?()?returns?the?size?of?vector?H??std?::?cout?<<?"H?has?size?"?<<?H.?size?()?<<?std?::?endl?;??//?print?contents?of?H??for?(?int?i?=?0;?i?<?H.?size?();?i?++)??std?::?cout?<<?"H["?<<?i?<<?"]?=?"?<<?H[i]?<<?std?::?endl?;??//?resize?H??H.?resize?(2)?;??std?::?cout?<<?"H?now?has?size?"?<<?H.?size?()?<<?std?::?endl?;??//?Copy?host_vector?H?to?device_vector?D??thrust?::?device_vector?<int?>?D?=?H;??//?elements?of?D?can?be?modified??D?[0]?=?99;??D?[1]?=?88;??//?print?contents?of?D??for?(?int?i?=?0;?i?<?D.?size?();?i?++)??std?::?cout?<<?"D["?<<?i?<<?"]?=?"?<<?D[i]?<<?std?::?endl?;??//?H?and?D?are?automatically?deleted?when?the?function?returns??return?0;??}?? ?
如這個例子所示,運(yùn)算符”=”可以用來復(fù)制host_vector到
device_vector(反之亦然)。?運(yùn)算符”=”也可以用來復(fù)制host_vector到host_vector或device_vector到device_vector。同樣device_vector訪問單個元素可以使用標(biāo)準(zhǔn)的括號表示法。但是,由于每次訪問需要調(diào)用cudaMemcpy,應(yīng)謹(jǐn)慎使用。下面我們將看看一些更有效的技術(shù)。
初始化所有向量的元素為特定值、或從一個vector向另一個拷貝特定值,是非常常用的技術(shù)。Thrust提供了一些方法可以完成這些種操作。
[cpp]?view plaincopy
#?include?<thrust?/?host_vector?.h>??#?include?<thrust?/?device_vector?.h>??#?include?<thrust?/?copy?.h>??#?include?<thrust?/?fill?.h>??#?include?<thrust?/?sequence?.h>??#?include?<iostream?>??int?main?(?void?)??{??//?initialize?all?ten?integers?of?a?device_vector?to?1??thrust?::?device_vector?<int?>?D(10?,?1);??//?set?the?first?seven?elements?of?a?vector?to?9??thrust?::?fill?(D.?begin?()?,?D.?begin?()?+?7,?9);??//?initialize?a?host_vector?with?the?first?five?elements?of?D??thrust?::?host_vector?<int?>?H(D.?begin?()?,?D.?begin?()?+?5);??//?set?the?elements?of?H?to?0,?1,?2,?3,?...??thrust?::?sequence?(H.?begin?()?,?H.?end?());??//?copy?all?of?H?back?to?the?beginning?of?D??thrust?::?copy?(H.?begin?()?,?H.?end?()?,?D.?begin?());??//?print?D??for?(?int?i?=?0;?i?<?D.?size?();?i?++)??std?::?cout?<<?"D["?<<?i?<<?"]?=?"?<<?D[i]?<<?std?::?endl?;??return?0;??}?? ?
這里我們看到了fill、copy、sequence的使用方法。copy函數(shù)可以用來拷貝主機(jī)端或者設(shè)備端的數(shù)據(jù)到另外一個vector。與STL中的類似,fill用于簡單的向一段元素賦特定值。sequence可以用來生成等差數(shù)列。
?
Thrust命名空間
你可能會注意到在我們的例子中使用了thrust::host_vector?或?thrust::copy的字段。其中thrust::告訴編譯器在thrust命名空間中查找函數(shù)與類。命名空間是一個很好的方式避免命名重復(fù)。例如,thrust::copy就可以與STL中的std::copy區(qū)別開來。C++的命名空間允許我們使用這兩個copy函數(shù)。
轉(zhuǎn)載于:https://www.cnblogs.com/Vulkan/archive/2012/11/29/7530195.html
總結(jié)
以上是生活随笔為你收集整理的Thrust快速入门教程(二)——Vector的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。