OpenCL Function Qualifiers (函数限定符)

OpenCL 3.0 Reference Pages -> OpenCL Compiler -> Function Qualifiers

1. Function Qualifiers (函数限定符)

1.1 __kernel or kernel

The __kernel or kernel qualifier declares a function to be a kernel that can be executed by an application on an OpenCL device(s). The following rules apply to functions that are declared with this qualifier:
限定符 __kernel or kernel 可以将函数声明为内核,使其可由应用程序在 OpenCL 设备上执行。对于用此限定符声明的函数有如下规定:

  • It can be executed on the device only
    仅可在设备上执行
  • It can be called by the host
    可由主机调用
  • It is just a regular function call if a __kernel function is called by another kernel function.
    当被另一个内核函数调用时,__kernel 函数仅仅是一个常规函数。

Kernel functions with variables declared inside the function with the __local or local qualifier can be called by the host using appropriate APIs such as clEnqueueNDRangeKernel.
如果内核函数中声明的变量带有限定符 __local or local,则主机可以使用恰当的 API 像 clEnqueueNDRangeKernel 来调用他。如果一个内核函数中有用 local 限定符声明的变量,则不能从另一个内核函数调用该函数。

The __kernel and kernel names are reserved for use as functions qualifiers and shall not be used otherwise.
保留关键字 __kernel and kernel 只能用作函数的限定符,不能挪作它用。

kernel void
parallel_add(global float *a, global float *b, global float *result) {...
}// The following example is an example of an illegal kernel declaration and will result in a compile-time error.
// The kernel function has a return type of int instead of void.
kernel int
parallel_add(global float *a, global float *b, global float *result) {...
}

kernel 函数适用以下规则:

  • 返回类型必须是 void,否则会产生编译错误。
  • 从宿主机执行内核的命令可以入队,从而可以在设备上执行这个函数。
  • 如果从一个内核函数调用,则函数表现为一个常规的函数。

一个内核函数调用另一个内核函数的情况,后者包含用 local 限定符声明的变量。其具体行为由实现定义,这些代码不能保证跨实现的可移植性,因此要避免这种做法。

kernel void
my_func_a(global float *src, global float *dst) {local float l_var[32];...
}kernel void
my_func_b(global float *src, global float *dst) {my_func_a(src, dst);  // implementation-defined behavior
}

要保证可移植性,更好的办法是将 local 变量作为参数传入 kernel。

kernel void
my_func_a(global float *src, global float *dst, local float *l_var) {...
}kernel void
my_func_b(global float *src, global float *dst, local float *l_var) {my_func_a(src, dst, l_var);
}

1.2 Optional Attribute Qualifiers (可选属性限定符)

The __kernel qualifier can be used with the keyword attribute to declare additional information about the kernel function as described below.
限定符 __kernel 可以与关键字 __attribute__ 一起使用,从而为内核函数声明额外的信息,如下所述。

The optional __attribute__((vec_type_hint(<type>))) [27] is a hint to the compiler and is intended to be a representation of the computational width of the __kernel, and should serve as the basis for calculating processor bandwidth utilization when the compiler is looking to autovectorize the code. In the __attribute__((vec_type_hint(<type>))) qualifier <type> is one of the built-in vector types listed in Built-in Vector Data Types or the constituent scalar element types. If vec_type_hint(<type>) is not specified, the kernel is assumed to have the __attribute__((vec_type_hint(int))) qualifier.
可选特性 __attribute__((vec_type_hint(<type>))) 可以给编译器提示 __kernel 可计算的 width,编译器在对代码进行自动矢量化时,可以将其作为基准来计算处理器的可利用带宽。其中 <type> 是 built-in vector types,或者是构成其元素的标量类型。如果没有指定 vec_type_hint (<type>) 则假定内核具有限定符 __attribute__((vec_type_hint(int)))<type> 的默认值是 int,指示本质上内核是标量。

[27] Implicit in autovectorization is the assumption that any libraries called from the __kernel must be recompilable at run time to handle cases where the compiler decides to merge or separate work-items. This probably means that such libraries can never be hard coded binaries or that hard coded binaries must be accompanied either by source or some retargetable intermediate representation. This may be a code security question for some.
自动矢量化 (autovectorization) 时会做隐式的假设:__kernel 中调用的所有库都必须可在运行时重新编译,只有这样,编译器才可以合并或分离 work-items。这可能意味着,这样的库不能是 hard coded binaries,如果是 hard coded binaries,则必须带有源码或可重定向的中间表示。在某些情况下,这可能导致代码安全问题。

For example, where the developer specified a width of float4, the compiler should assume that the computation usually uses up to 4 lanes of a float vector, and would decide to merge work-items or possibly even separate one work-item into many threads to better match the hardware capabilities. A conforming implementation is not required to autovectorize code, but shall support the hint. A compiler may autovectorize, even if no hint is provided. If an implementation merges N work-items into one thread, it is responsible for correctly handling cases where the number of global or local work-items in any dimension modulo N is not zero.
例如,如果开发人员指定的宽度是 float4,则编译器应当假定通常使用四路 float 矢量进行计算,并可以决定合并 work-items 或者甚至可能将一个 work-item 分成多个线程,以更好的匹配硬件的能力。对于合格的实现,不要求可以对代码自动矢量化,但应当支持这个暗示。即使没有这个暗示,编译器也可能自动矢量化。如果实现将 N 个 work-items 合并到一个线程中,则它要负责正确地处理这种情况:global or local 作业项的数目在任一维度上模 N 都不为零。

hint [hɪnt]:n. 提示,暗示,迹象,窍门 v. 暗示,透露,示意
constituent [kənˈstɪtjʊənt]:n. 成分,构成要素,选民 adj. 组成的,构成的
conform [kənˈfɔː(r)m]:v. 一致,使一致,使顺应,使遵照
lane [leɪn]:n. 车道,小巷,泳道,胡同

Examples:

// autovectorize assuming float4 as the basic computation width
__kernel __attribute__((vec_type_hint(float4)))
void foo(__global float4 *p) { ... }// autovectorize assuming double as the basic computation width
__kernel __attribute__((vec_type_hint(double)))
void foo(__global float4 *p) { ... }// autovectorize assuming int (default) as the basic computation width
__kernel
void foo(__global float4 *p) { ... }

If for example, a __kernel function is declared with __attribute__(( vec_type_hint (float4))) (meaning that most operations in the __kernel function are explicitly vectorized using float4) and the kernel is running using Intel® Advanced Vector Instructions (Intel® AVX) which implements a 8-float-wide vector unit, the autovectorizer might choose to merge two work-items to one thread, running a second work-item in the high half of the 256-bit AVX register.
如果声明 __kernel 函数时带有 __attribute__(( vec_type_hint (float4))) (__kernel 函数中大部分运算都已使用 float4 显式矢量化),此内核在使用 Intel® Advanced Vector Instructions (Intel® AVX) 时,实现了 8 个 float 宽度的矢量单元,则自动矢量化时可能选择将两个 work-items 合并成一个线程,第二个 work-item 将在 256-bit AVX 寄存器的高 128 位中运行。

As another example, a Power4 machine has two scalar double precision floating-point units with an 6-cycle deep pipe. An autovectorizer for the Power4 machine might choose to interleave six kernels declared with the __attribute__(( vec_type_hint (double2))) qualifier into one hardware thread, to ensure that there is always 12-way parallelism available to saturate the FPUs. It might also choose to merge 4 or 8 work-items (or some other number) if it concludes that these are better choices, due to resource utilization concerns or some preference for divisibility by 2.
Power4 机器具有两个双精度浮点单元,均为六级流水线结构 (6-cycle deep pipe)。针对这种机器,自动矢量化时可能选择将六个声明时带有限定符 __attribute__(( vec_type_hint (double2))) 的内核间插到同一个硬件线程中,从而可以让 FPUs 可以一直 12 路全速并行。考虑到资源利用或者一些偏好,如 2 的指数,即仅将 4 个或 8 个 (或者其他数目的) work-items 进行合并,只要自动矢量化时认定这种选择更好就行。

divisibility [dɪˌvɪzɪ'bɪlɪtɪ]:n. 可除尽,可分割性,(晶体的) 解理性

The optional __attribute__((work_group_size_hint(X, Y, Z))) is a hint to the compiler and is intended to specify the work-group size that may be used i.e. value most likely to be specified by the local_work_size argument to clEnqueueNDRangeKernel. For example, the __attribute__((work_group_size_hint(1, 1, 1))) is a hint to the compiler that the kernel will most likely be executed with a work-group size of 1.
可选特性 __attribute__((work_group_size_hint(X, Y, Z))) 可以给编译器提示 work-group 的大小,即最有可能传给 clEnqueueNDRangeKernel 的参数 local_work_size 的值。例如 __attribute__((work_group_size_hint(1, 1, 1))) 就是暗示编译器内核最有可能在大小为 1 的 work-group 中执行。

The optional __attribute__((reqd_work_group_size(X, Y, Z))) is the work-group size that must be used as the local_work_size argument to clEnqueueNDRangeKernel. This allows the compiler to optimize the generated code appropriately for this kernel.
可选特性 __attribute__((reqd_work_group_size(X, Y, Z))) 则是 clEnqueueNDRangeKernel 的参数 local_work_size 必须使用的值。指定将要使用的 work-group 大小,即 clEnqueueNDRangeKernellocal_work_size 参数中指定的值。这样编译器就可以根据对 work-group 大小的了解完成特定的优化。

If Z is one, the work_dim argument to clEnqueueNDRangeKernel can be 2 or 3. If Y and Z are one, the work_dim argument to clEnqueueNDRangeKernel can be 1, 2 or 3.
如果 Z 是 1,则 clEnqueueNDRangeKernel 的参数 work_dim 可以是 2 或 3。如果 YZ 都是 1,则 clEnqueueNDRangeKernel 的引数 work_dim 可以是 1、2 或 3。

References

Khronos OpenCL Registry
https://www.khronos.org/registry/OpenCL/

OpenCL 3.0 Reference Pages
https://www.khronos.org/registry/OpenCL/sdk/3.0/docs/man/html/

OpenCL C Language Specification
https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_C.html

OpenCL Function Qualifiers (函数限定符)相关推荐

  1. 翻译:Identifier Qualifiers标识限定符

    本文为mariadb官方手册:Identifier Qualifiers的译文. 原文:https://mariadb.com/kb/en/library/identifier-qualifiers/ ...

  2. C++ 学习笔记(19)new/delete表达式、定位new、typeid、dynamic_cast、type_info、枚举类型、成员函数指针、union、位域、volatile限定符、链接指示

    C++ 学习笔记(19)new/delete表达式.定位new.typeid.dynamic_cast.type_info.枚举类型.成员函数指针.union.位域.volatile限定符.链接指示 ...

  3. 对象含有与成员函数不兼容的类型限定符

    class assassin {int health;public:assassin(int _health = 0) :health(_health) {};int gethealth() {ret ...

  4. c++类之“对象包含与成员函数不兼容的类型限定符”与“对象含有与成员 函数 “CarBody::Geta” 不兼容的类型限定符”错误的修改

    首先看一段代码 #include<iostream> using namespace std; int car_num=0; struct position {double x, y; } ...

  5. C语言中的restrict限定符

    今天在写opencl的kernel端代码时用到了restrict关键字,做个记录. restrict限定符的作用 C语言中的一种类型限定符,用于告诉编译器,对象已经被指针所引用,不能通过除去该指针以外 ...

  6. 指针和Const限定符

    指针和Const限定符 1.指向const对象的指针 如果指针指向的是const对象,则不允许使用指针来改变其所指的const值.C++要求指向const对象的指针具有const特性. const d ...

  7. C++ Primer 5th笔记(chap 13 拷贝控制)引用限定符

    1. 问题 关于右值和左值引用成员函数,通常在一个对象上调用成员函数,而不管对象是一个左值还是一个右值: string s1 = "a value",s2 = "anot ...

  8. python中函数修饰符_python中的函数修饰符

    首先,什么是函数修饰符?函数修饰符就是对原有函数做一层包装.比如有以下两个函数: def func1(): print 'I am function func1' def func2(): print ...

  9. C++primer :const限定符

    1.问题引入 <span style="font-size:18px;"><span style="font-size:18px;">f ...

最新文章

  1. 紧急更新下降难度,《王者荣耀》绝悟 AI 难倒一片玩家
  2. 闲谈IPv6-典型特征的一些技术细节
  3. 混合云应用双活容灾实践
  4. datatables中的Options总结(2)
  5. php未定义常量破解,PHP未定义的常量错误没有意义
  6. [转载] python json unicode utf-8处理总结
  7. 广东工业大学华立学院c语言试题,广东工业大学华立学院考试试卷《高频电子线路》-2015.doc...
  8. 【51单片机】蜂鸣器程序
  9. 计算机电路基础答案刘怀望,计算机电路基础
  10. sublime text3 boxy主题 (本地 压缩包 安装)
  11. 软件工程——团队作业4
  12. python requests 最新抓取百度翻译内容,js逆向,亲测有效
  13. 新版js进阶高频面试题
  14. Android 测试工具集01
  15. 今天我被微软狠狠地雷翻了
  16. 数学建模竞赛知识点汇总(一)——层次分析法
  17. C语言实现数据结构之归并排序
  18. CG系统提交Java程序_Cg使用说明 第一章 介绍
  19. 搜索-Query理解(全)
  20. FutureTask源码学习

热门文章

  1. 【FS96生物医学工程学】生物医学工程复试问题
  2. 环保性能高的家装乳胶漆怎么选择
  3. 7人制足球技战术要点
  4. 企业抖音号怎么运营矩阵?运营有何技巧?
  5. 大学四年学习生活成长总结
  6. python画椭圆的逻辑_Python plt画椭圆
  7. 全面理解Python迭代器和生成器
  8. 谈小米内忧外患的困境
  9. mhdd4.6修复坏道图解教程
  10. php 正文提取算法,基于机器学习的网页正文提取方法