OpenCL memory object 之選擇傳輸path

對應用程序來說,選擇合適的memory object傳輸path可以有效提高程序性能。

?

下面先看一寫buffer bandwidth的例子:

?

1.? clEnqueueWriteBuffer()以及clEnqueueReadBuffer()

?

????? 如果應用程序已經通過malloc 或者mmap分配內存,CL_MEM_USE_HOST_PTR是個理想的選擇。

有兩種使用這種方式的方法:

?

第一種:

a. pinnedBuffer = clCreateBuffer( CL_MEM_ALLOC_HOST_PTR or CL_MEM_USE_HOST_PTR )
b. deviceBuffer = clCreateBuffer()
c. void *pinnedMemory = clEnqueueMapBuffer( pinnedBuffer )
d. clEnqueueRead/WriteBuffer( deviceBuffer, pinnedMemory )
e. clEnqueueUnmapMemObject( pinnedBuffer,
pinnedMemory )

?

???? pinning開銷在步驟a產生,步驟d沒有任何pinning開銷。通常應用立即程序執行a,b,c,e步驟,而在步驟d之后,要反復讀和修改pinnedMemory中的數據,

?

?

第二種

?? clEnqueueRead/WriteBuffer 直接在用戶的memory buffer中被使用。在copy(host->device)數據前,首先需要pin(lock page)操作,然后才能執行傳輸操作。這條path大概是peak interconnect bandwidth的2/3。


2. 在pre-pinned host buffer上使用clEnqueueCopyBuffer()

?

??? 和1類似,clEnqueueCopyBuffer在pre-pinned buffer上以peak interconnect bandwidth執行傳輸操作:

?

a. pinnedBuffer = clCreateBuffer( CL_MEM_ALLOC_HOST_PTR or CL_MEM_USE_HOST_PTR )
b. deviceBuffer = clCreateBuffer()
c. void *memory = clEnqueueMapBuffer( pinnedBuffer )
d. Application writes or modifies memory.
e. clEnqueueUnmapMemObject( pinnedBuffer, memory )
f. clEnqueueCopyBuffer( pinnedBuffer, deviceBuffer )
或者通過:
g. clEnqueueCopyBuffer( deviceBuffer, pinnedBuffer )
h. void *memory = clEnqueueMapBuffer( pinnedBuffer )
i. Application reads memory.
j. clEnqueueUnmapMemObject( pinnedBuffer, memory )

?

???

???? 由于pinned memory駐留在host memroy,所以clMap() 以及 clUnmap()調用不會導致數據傳輸。cpu可以以host memory帶寬來操作這些pinned buffer。

?
3、在device buffer上執行 clEnqueueMapBuffer() and clEnqueueUnmapMemObject()

?

???? 對于已經通過malloc和mmap分配空間的buffer,傳輸開銷除了interconnect傳輸外,還要包括一個memcpy過程,該過程把buffer拷貝進mapped device buffer。


a. Data transfer from host to device buffer.


1.

ptr = clEnqueueMapBuffer( .., buf, .., CL_MAP_WRITE, .. )
???? 由于緩沖被映射為write-only,所以沒有數據從device傳輸到host,映射開銷比較低。一個指向pinned host buffer的指針被返回。

?

?

2. 應用程序通過memset(ptr)填充host buffer?
??? memcpy ( ptr, srcptr ), fread( ptr ), 或者直接CPU寫, 這些操作以host memory全速帶寬讀寫。


3. clEnqueueUnmapMemObject( .., buf, ptr, .. )?
??? pre-pinned buffer以peak interconnect速度被傳輸到GPU device。

?
b. Data transfer from device buffer to host.


1. ptr = clEnqueueMapBuffer(.., buf, .., CL_MAP_READ, .. )
??? 這個命令啟動devcie到host數據傳輸,數據以peak interconnect bandwidth傳輸到一個pre-pinned的臨時緩沖中。返回一個指向pinned memory的指針。
2. 應用程序讀、處理數據或者執行 memcpy( dstptr, ptr ), fwrite (ptr), 或者其它類似的函數時候,由于buffer駐留在host memory中,所以操作以host memory bandwidth執行。

3. clEnqueueUnmapMemObject( .., buf, ptr, .. )

由于buffer被映射成只讀的,沒有實際數據傳輸,所以unmap操作的cost很低。


4. host直接訪問設備zero copy buffer

?? 這個訪問允許數據傳輸和GPU計算同時執行(overlapped),在一些稀疏(sparse)的寫或者更新情況下,比較有用。

a. 一個device上的 zero copy buffer通過下面的命令被創建


buf = clCreateBuffer ( .., CL_MEM_USE_PERSISTENT_MEM_AMD, .. )


CPU能夠通過uncached WC path直接訪問該buffer。 通常可以使用雙緩沖機制,gpu在處理一個緩沖中的數據,cpu同時在填充另一個緩沖中的數據。

A zero copy device buffer can also be used to for sparse updates, such as assembling sub-rows of a larger matrix into a smaller, contiguous block for GPU processing. Due to the WC path, it is a good design choice to try to align writes to the cache line size, and to pick the write block size as large as possible.

?

b. Transfer from the host to the device.
1. ptr = clEnqueueMapBuffer( .., buf, .., CL_MAP_WRITE, .. )
This operation is low cost because the zero copy device buffer is directly mapped into the host address space.


2. The application transfers data via memset( ptr ), memcpy( ptr, srcptr ), or direct CPU writes.
The CPU writes directly across the interconnect into the zero copy device buffer. Depending on the chipset, the bandwidth can be of the same order of magnitude as the interconnect bandwidth, although it typically is lower than peak.


3. clEnqueueUnmapMemObject( .., buf, ptr, .. )
As with the preceding map, this operation is low cost because the buffer continues to reside on the device.
c. If the buffer content must be read back later, use clEnqueueReadBuffer( .., buf, ..)? or clEnqueueCopyBuffer( .., buf, zero copy host buffer, .. ).


This bypasses slow host reads through the uncached path.


5 - GPU直接訪問host zero copy memory

?

This option allows direct reads or writes of host memory by the GPU. A GPU kernel can import data from the host without explicit transfer, and write data directly back to host memory. An ideal use is to perform small I/Os straight from the kernel, or to integrate the transfer latency directly into the kernel execution time.


a:The application creates a zero copy host buffer.
???? buf = clCreateBuffer( .., CL_MEM_ALLOC_HOST_PTR, .. )
b:Next, the application modifies or reads the zero copy host buffer.


???? 1. ptr = clEnqueueMapBuffer( .., buf, .., CL_MAP_READ | CL_MAP_WRITE, .. )

This operation is very low cost because it is a map of a buffer already residing in host memory.
????? 2. The application modifies the data through memset( ptr ), memcpy( in either direction ), sparse or dense CPU reads or writes. Since the application is modifying a host buffer, these operations take place at host memory bandwidth.
????? 3. clEnqueueUnmapMemObject( .., buf, ptr, .. )

As with the preceding map, this operation is very low cost because the buffer continues to reside in host memory.
c. The application runs clEnqueueNDRangeKernel(), using buffers of this type as input or output. GPU kernel reads and writes go across the interconnect to host memory, and the data transfer becomes part of the
kernel execution.


The achievable bandwidth depends on the platform and chipset, but can be of the same order of magnitude as the peak interconnect bandwidth.


For discrete graphics cards, it is important to note that resulting GPU kernel bandwidth is an order of magnitude lower compared to a kernel accessing a regular device buffer located on the device.


d. Following kernel execution, the application can access data in the host buffer in the same manner as described above.

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/448804.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/448804.shtml
英文地址,請注明出處:http://en.pswp.cn/news/448804.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

struts入門超詳細

https://blog.csdn.net/yerenyuan_pku/article/details/52652262轉載于:https://www.cnblogs.com/liuna369-4369/p/10870873.html

RabbitMQ 從入門到精通 (一)

目錄 1. 初識RabbitMQ2. AMQP3.RabbitMQ的極速入門4. Exchange(交換機)詳解4.1 Direct Exchange4.2 Topic Exchange4.3 Fanout Exchange5. Message 消息1. 初識RabbitMQ RabbitMQ 是一個開源的消息代理和隊列服務器,用來通過普通協議在完全不同的應用之間共享數據&a…

接收并解析消息體傳參、解析 json 參數

前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。 1.場景:postman 發送了一個 post 請求,如下: 2. 解析方式為用一個 vo 對象來接收 json。把 json 中的…

OpenCL memory object 之 傳輸優化

首先我們了解一些優化時候的術語及其定義: 1、deferred allocation(延遲分配), 在第一次使用memory object傳輸數據時,runtime才對memory object真正分配空間。 這樣減少了資源浪費,但第一次使用時要慢一些…

VBS使文本框的光標位于所有字符后

有時候在文本框里會顯示一部分提示信息,用戶在這些提示信息后面輸入文本,但是將焦點設置于文本框后,光標總是在文本框的最前面, 用戶輸入的時候需要按"-->"鍵將光標移到最后才能輸入,這樣的操作很不爽。我…

記錄ionic 最小化應用時所遇的問題

ionic3與ionic4最小化插件安裝不一樣: ionic3安裝方法: $ ionic cordova plugin add cordova-plugin-appminimize $ npm install --save ionic-native/app-minimize4 并在app.module.ts中 注入依賴: import { AppMinimize } from ionic-nativ…

解決 --- Docker 啟動時報錯:iptables:No chain/target/match by the name

問題:jenkins的docker containner啟動失敗,報錯:failed programming external connectivity … iptables: No chain/target/match by that name” docker 服務啟動的時候,docker服務會向iptables注冊一個鏈,以便讓dock…

AMD OpenCL 大學課程

AMD OpenCL大學課程是非常好的入門級OpenCL教程,通過看教程中的PPT,我們能夠很快的了解OpenCL機制以及編程方法。下載地址:http://developer.amd.com/zones/OpenCLZone/universities/Pages/default.aspx 教程中的英文很簡單,我相信…

第一篇 計算機基礎

1.什么是編程語言 python和中文、英語一樣、都是一門語言,只要是語言,其實就庫看成是一種事物與另一種事物溝通的介質。python屬于編程語言,編程語言是程序員與計算機之間溝通的介質;中文和英文則是人與人之間溝通的介質。 2.什么…

47.QT-QChart之曲線圖,餅狀圖,條形圖使用

1.使用準備 在pro中, 添加QT charts 然后在界面頭文件中添加頭文件并聲明命名空間,添加: #include <QtCharts> QT_CHARTS_USE_NAMESPACE 2.QChart之曲線圖 繪制曲線圖需要用到3個類 QSplineSeries: 用于創建有由一系列數據組成的曲線.類似的還有QPieSeries(餅圖數據). Q…

Docker 部署應用、jar 工程 docker 方式部署

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1. 把要部署的工程打成一個jar包。&#xff08;我的工程叫 gentle &#xff09; 打 jar 的方法&#xff1a;超簡單方法&#xff1a; Int…

流浪不是我的初衷 ... ...

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 或許&#xff0c;我從來就是一個習慣沉默的人 ... 或許&#xff0c;我從來就不善于傾述 ... 會有難過的時候&#xff0c;會有覺得累的…

第二階段沖刺(2)

1、整個項目預期的任務量 &#xff08;任務量 所有工作的預期時間&#xff09;和 目前已經花的時間 &#xff08;所有記錄的 ‘已經花費的時間’&#xff09;&#xff0c;還剩余的時間&#xff08;所有工作的 ‘剩余時間’&#xff09; &#xff1b; 所有工作的預期時間&#…

VS2008+OpenCL環境配置

1. 配置.cl文件支持: 1.1. 打開VS2008&#xff0c; 工具->選項->文本編輯器->文件擴展名&#xff0c;添加一個新的擴展名&#xff0c;指定編輯器為Microsoft Visual C 。這樣在OpenCL文件中就能顯示C的語法高亮了。 1.2. 配置OpenCL語法高亮 - 打開目錄~\NVIDIA Corpo…

第十二周學習進度報告

代碼時間&#xff1a;17小時左右&#xff0c; 代碼量&#xff1a;300行左右&#xff0c; 閱讀&#xff1a;一個app的誕生20頁&#xff1b;構建之法30頁 知識&#xff1a;抽象典型用戶&#xff08;具有代表性&#xff09;和場景&#xff0c;去設計相應功能。 轉載于:https://www…

我的桃花源

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 看了一個動畫片&#xff08;《貓與桃花源》&#xff09;&#xff0c;畫風和內容并不是我最偏好的... 但故事結尾的旁白和歌曲卻打動了一…

promise實例

不廢話&#xff0c;粘代碼 function ajax(method, url, data) {let request new XMLHttpRequest();return new Promise(function (resolve, reject) {request.onreadystatechange function () {if (request.readyState 4) {if (request.status 200) {resolve(request.respo…

華為路由器配置DHCP中繼

DHCP(動態主機配置協議)理論知識&#xff1a;DHCP主要用來為客戶機自動配置I P地址相關的網絡參數&#xff0c;包括IP地址、子網掩碼、默認網關、DNS服務器等。 DHCP 通信為廣播的方式&#xff0c;因此當需要 DHCP 服務器為不同廣播域&#xff08;路由或 VLAN 網段&#xff09;…

基于GPU的K-Means聚類算法

聚類是信息檢索、數據挖掘中的一類重要技術&#xff0c;是分析數據并從中發現有用信息的一種有效手段。它將數據對象分組成為多個類或簇&#xff0c;使得在同一個簇中的對象之間具有較高的相似度&#xff0c;而不同簇中的對象差別很大。作為統計學的一個分支和一種無監督的學習…

IntelliJ IDEA 工具篇之如何切換 git 分支

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1、進入項目和工程。 2、點擊右下角的git:master&#xff0c;然后選擇origin/master&#xff0c;然后選擇你要切換的分支&#xff0c;我…