OpenCL框架与示例
下面的图简单说明了OpenCL的编程框架,图是用的GPU,其他类似;
名词的概念:
Platform (平台):主机加上OpenCL框架管理下的若干设备构成了这个平台,通过这个平台,应用程序可以与设备共享资源并在设备上执行kernel。实际使用中基本上一个厂商对应一个Platform,比如Intel, AMD都是这样。
Device(设备):官方的解释是计算单元(Compute Units)的集合。举例来说,GPU是典型的device。Intel和AMD的多核CPU也提供OpenCL接口,所以也可以作为Device。
Context(上下文):OpenCL的Platform上共享和使用资源的环境,包括kernel、device、memory objects、command queue等。使用中一般一个Platform对应一个Context。
Program:OpenCL程序,由kernel函数、其他函数和声明等组成。
Kernel(核函数):可以从主机端调用,运行在设备端的函数。
Memory Object(内存对象):在主机和设备之间传递数据的对象,一般映射到OpenCL程序中的global memory。有两种具体的类型:Buffer Object(缓存对象)和Image Object(图像对象)。
Command Queue(指令队列):在指定设备上管理多个指令(Command)。队列里指令执行可以顺序也可以乱序。一个设备可以对应多个指令队列。
NDRange:主机端运行设备端kernel函数的主要接口。实际上还有其他的,NDRange是非常常见的,用于分组运算,以后具体用到的时候就知道区别了。
Host端来看,OpenCL的组要执行流程是这样的:
内存模型
最后写一下Opencl的内存模型,看下面的示意图:
用核函数中的内存变量来简单地解释:用clCreateBuffer 创建、用clSetKernelArg 传递的数据在global memory 和constant memory中;核函数中的寄存器变量在private memory 中;核函数的内部变量、缓存等,在local memory 中。图例中可以看到Device 并不直接访问global memory,而是通过Cache 来访问。可以想象当同时运行的work-item,使用的内存都在同一块cache 中,则内存吞吐的效率最高。对应到work group 中,就是在程序设计上尽量使同一个work group 中的work item 操作连续的内存,以提高访存效率。
TVM OpenCL示例
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* “License”); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*!
* \file opencl_module.cc
*/
#include "opencl_module.h"
#include <dmlc/memory_io.h>
#include <tvm/runtime/registry.h>
#include