copy from http://developer.amd.com/community/blog/2014/10/31/opencl-2-0-pipes/

OpenCL™ 2.0 – Pipes

In the previous post, we saw one of the important features of OpenCL™ 2.0, Shared Virtual Memory (SVM). In this blog, we will see another feature of OpenCL 2.0 called “pipes”.

To get the most from our discussions, we recommend the same approach as in the previous post:

Review the code snippets in each blog post along with the explanatory text.

Download the AMD OpenCL 2.0 Driver located here. This page also has a complete list of supported platforms.

Download the sample code from our OpenCL 2.0 Samples page here.

Write your own OpenCL 2.0 samples and share your results with the OpenCL community.

The code will run on a variety of AMD platforms, such as the Radeon HD8000 series. The driver page has a complete list of supported product families.

Overview

OpenCL 2.0 introduces a new mechanism for passing data between kernels, called “pipes.” A pipe is essentially a structured buffer containing some space for a set of “packets”—kernel-specified type objects. As the name suggests, these packets of data are ordered in the pipe. There is a write end of the pipe into which the data is written and a read end of the pipe from which the data is read. A pipe is essentially an addition to buffer objects, such as buffers and images. Pipes can only be accessed using the built-in functions provided by the kernel and cannot be accessed from the host.

Special built-in functions, read_pipe and write_pipe, provide access to pipes from the kernel. A given kernel can either read from or write to a pipe, but not both. Pipes are only coherent at the standard synchronization points; the result of concurrent accesses to the same pipe by multiple kernels (even hardware permitting) is undefined. The host side cannot access a pipe.

It is easy to create pipes. On the host, invoke clCreatePipe, and you are done.

You can use the pipes for a variety of functions. You can pass the pipes between kernels. Even better, combine pipes with the device-size enqueue feature in OpenCL 2.0 to dynamically construct computational data flow graphs.

Pipes come in two types: a read pipe, from which a number of packets can be read, and a write pipe, to which a number of packets can be written.

Note: You cannot write to a pipe specified as read-only, nor can you read from a pipe specified as write-only. You cannot both read from and write to a pipe at the same time.

Functions for Accessing Pipes

OpenCL 2.0 adds a new host API function to create a pipe.

cl_mem clCreatePipe ( cl_context context, cl_mem_flags flags,
cl_uint pipe_packet_size, cl_uint pipe_max_packets,
const cl_pipe_properties * properties,
cl_int *errcode_ret)

The memory allocated in this function can pass to kernels as read-only or write-only pipes. The pipe objects can only be passed as kernel arguments or kernel functions and cannot be declared inside a kernel or as program-scoped objects.

Also, the OpenCL 2.0 spec adds a set of built-in functions for operating on the pipes. The important ones are:

  • read_pipe (pipe p, gentype *ptr): for reading packet from pipe p into ptr.
  • write_pipe (pipe p, gentype *ptr): for writing packet pointed to by ptr to pipe p.

To ensure you have enough space in the pipe structure for reading and writing (before you actually do it), you can use built-in functions to “reserve” enough space. For example, you could reserve room by calling reserve_read_pipe or reserve_write_pipe. These functions return a reservation ID, which can be used when the actual operations are performed. Similarly, the standard has built-in functions for workgroup level reservations, such as work_group_reserve_read_pipe and work_group_reserve_write_pipe and for the workgroup order (in the program). These workgroup built-in functions operate at the workgroup level. Ordering across workgroups is undefined. Calls to commit_read_pipe and commit_write_pipe, as the names suggest, commit the actual operations (read/write).

Using Pipes—A Simple Example

Let’s look at a typical usage of pipes in the example code (SimplePipe). The code contains two kernels: producer_kernel, which writes to the pipe, and consumer_kernel, which reads from the same pipe. In the example, the producer writes a sequence of random numbers; the consumer reads them and creates a histogram.

The host creates the pipe, which both kernels will use, as follows:

rngPipe = clCreatePipe(context,
CL_MEM_READ_WRITE,
szPipePkt,
szPipe,
NULL,
&status);

This code makes a pipe that the program kernels can access (read/write). The host creates two kernels, producer_kernel and consumer_kernel. The producer kernel first reserves enough space for the write pipe:

//reserve space in pipe for writing random numbers.
reserve_id_t rid = work_group_reserve_write_pipe(rng_pipe, szgr);

Next, the kernel writes and commits to the pipe by invoking the following functions:

write_pipe(rng_pipe,rid,lid, &gfrn);
work_group_commit_write_pipe(rng_pipe, rid);

Similarly, the consumer kernel reads from the pipe:

//reserve pipe for reading
reserve_id_t rid = work_group_reserve_read_pipe(rng_pipe, szgr);
if(is_valid_reserve_id(rid)) {
//read random number from the pipe.
read_pipe(rng_pipe,rid,lid, &rn);
work_group_commit_read_pipe(rng_pipe, rid);
}

The consumer_kernel then uses this set of random number and constructs the histogram. The CPU creates the same histogram and verifies whether the histogram created by the kernel is correct. Here, lid is the local id of the work item, obtained by get_local_id(0).

The example code demonstrates how you can use a pipe as a convenient data structure that allows two kernels to communicate. It’s really pretty simple.

In OpenCL 1.2, this kind of communication typically involves the host – although kernels can communicate without returning control to the host. Pipes, however, ease programming by reducing the amount of code that some applications require. There are additional examples of pipes used in conjunction with device enqueue, which we will explore in later blogs in this series.

To conclude, using pipes in OpenCL 2.0 can make your code simpler and more readable. Don’t believe us? Write your own OpenCL 2.0 programs and tell us about the difference!

Sample code and readme

The sample code demonstrates the use of the pipes feature of OpenCL.2.0 using the SimplePipe (producer/consumer kernels) sample:

  • The sample code and readme are provided here.
  • Install AMD OpenCL 2.0 Driver located here. This page also has a complete list of supported platforms.
  • Build the sample using the instructions in the readme.
  • Let us know your feedback and comments on the Developer Central OpenCL forum.

– Prakash Raghavendra

Dr. Prakash Raghavendra is a technical lead at AMD. He has several years of experience in developing compilers and run-time. His postings are his own opinions and may not represent AMD’s positions, strategies or opinions. Links to third party sites, and references to third party trademarks, are provided for convenience and illustrative purposes only. Unless explicitly stated, AMD is not responsible for the contents of such links, and no third party endorsement of AMD or any of its products is implied.

OpenCL™ 2.0 – Pipes的更多相关文章

  1. OpenCL介绍

    OpenCL(全称Open Computing Language,开放运算语言)是第一个面向异构系统通用目的并行编程的开放式.免费标准,也是一个统一的编程环境,便于软件开发人员为高性能计算服务器.桌面 ...

  2. OpenCL Kernel设计优化

    使用Intel® FPGA SDK for OpenCL™ 离线编译器,不需要调整kernel代码便可以将其最佳的适应于固定的硬件设备,而是离线编译器会根据kernel的要求自适应调整硬件的结构. 通 ...

  3. 基于SoCkit的opencl实验1-基础例程

    基于SoCkit的opencl实验1-基础例程 准备软硬件 Arrow SoCkit Board 4GB or larger microSD Card Quartus II v14.1 SoCEDS ...

  4. 面向OPENCL的ALTERA SDK

    面向OPENCL的ALTERA SDK 使用面向开放计算语言 (OpenCL™) 的 Altera® SDK,用户可以抽象出传统的硬件 FPGA 开发流程,采用更快.更高层面的软件开发流程.在基于 x ...

  5. OpenCL中三种内存创建image的效率对比

    第一种:使用ION: cl_mem_ion_host_ptr ion_host_ptr1; ion_host_ptr1.ext_host_ptr.allocation_type = CL_MEM_IO ...

  6. OpenCL科普及在ubuntu 16.04 LTS上的安装

    OpenCL(Open Computing Language,开放计算语言)是一个为异构平台编写程序的框架,此异构平台可由CPU.GPU.DSP.FPGA或其他类型的处理器與硬體加速器所组成.Open ...

  7. OpenCL 查询平台和设备

    ▶ 查询平台和设备的代码以结果,放在这里方便以后逐渐扩充和查询(没有营养) #include <stdio.h> #include <stdlib.h> #include &l ...

  8. OpenCL Hello World

    ▶ OpenCL 的环境配置与第一个程序 ● CUDA 中自带 OpenCL 需要的头文件和库,直接拉近项目里边去就行:AMD 需要下载 AMD APP SDK(https://community.a ...

  9. OpenCL双边滤波实现美颜功能

    OpenCL是一个并行异构计算的框架,包括intel,AMD,英伟达等等许多厂家都有对它的支持,不过英伟达只到1.2版本,主要发展自己的CUDA去了.虽然没有用过CUDA,但个人感觉CUDA比Open ...

随机推荐

  1. 文件(2)--IO流

    IO流 输入流和输出流 Java中的IO流根据功能划分为:输入流和输出流.输入流:用于读取数据.输出流:用于写出数据.输入输出的参照方向是根据我们的程序的. 字节流和字符流 Java中的IO流根据处理 ...

  2. spark SQL学习(数据源之parquet)

    Parquet是面向分析型业务得列式存储格式 编程方式加载数据 代码示例 package wujiadong_sparkSQL import org.apache.spark.sql.SQLConte ...

  3. HIVE 编写自定义函数UDF

    一 新建JAVA项目 并添加 hive-exec-2.1.0.jar 和hadoop-common-2.7.3.jar hive-exec-2.1.0.jar 在HIVE安装目录的lib目录下 had ...

  4. java.lang.IllegalArgumentException异常处理的一种方法

    我遇到的问题 用spring注解来加载bean,都是一个简单的了,但是还是报了这样一个错java.lang.IllegalArgumentException 网上查了一下,说是jdk版本要1.7的,然 ...

  5. linux service start|stop|restart

    用了这么些日子的linux/unix系统,也和别人一起合作开发了不少程序,发现高手都喜欢在命令行上操作,而且控制程序的运行偏好于使用脚本,加上参数如:start.restart.stop等. 后来自己 ...

  6. Confluence 6 测试电子邮件设置

    一个 Confluence 的管理员可以通过下面的步骤测试电子邮件服务器的配置: 按照上面的步骤中描述得方法设置一个电子邮件服务器. 单击 发送测试邮件(Send Test Email)来检查你设置的 ...

  7. C语言之内存四区模型和函数调用模型

      内存四区模型 流程说明1.操作系统把物理硬盘代码load到内存2.操作系统把c代码分成四个区3.操作系统找到main函数入口执行 1.内存四区: 一个由c/C++编译的程序占用的内存分为以下几个部 ...

  8. OpenStack Mitaka HA部署方案(随笔)

    [Toc] https://github.com/wanstack/AutoMitaka # 亲情奉献安装openstack HA脚本 使用python + shell,完成了基本的核心功能(纯二层的 ...

  9. Node.js小白开路(一)-- console篇

    在所有内容的学习之中我们经常首先要接受到的常常很大一部分为命令行或是工具的内容展示,console内容为node.js在命令行中答应数据内容的一个途径. Console是nodejs中的元老级模块了. ...

  10. 如何在PostgreSQL中建只读账号

    转: 如何在PostgreSQL中建只读账号 Posted on 2014-01-21 22:00:15 by osdba 在PostgreSQL中并没有CREATE TABLE权限名称,这是与其它数 ...