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.mzph.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;我…