▶ 使用 routine 构件创建的自定义函数,在并行调用上的差别

● 代码,自定义一个 sqab 函数,使用内建函数 fabsf 和 sqrtf 计算一个矩阵所有元素绝对值的平方根

 #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <openacc.h> #define ROW 8
#define COL 64 #pragma acc routine vector
void sqab(float *a, const int m)
{
#pragma acc loop
for (int idx = ; idx < m; idx++)
a[idx] = sqrtf(fabsf(a[idx]));
} int main()
{
float x[ROW][COL];
int row, col;
for (row = ; row < ROW; row++)
{
for (col = ; col < COL; col++)
x[row][col] = row * + col;
}
printf("\nx[1][1] = %f\n", x[][]); #pragma acc parallel loop vector pcopy(x[0:ROW][0:COL]) // 之后在这里分别添加 gang,worker,vector
for (row = ; row < ROW; row++)
sqab(&x[row][], COL);
printf("\nx[1][1] = %f\n", x[][]); //getchar();
return ;
}

● 输出结果,第 28 行不添加并行级别子句(默认使用 gang)

 D:\Code\OpenACC\OpenACCProject\OpenACCProject>pgcc main.c -acc -Minfo -o main_acc.exe
sqab:
, Generating Tesla code
, #pragma acc loop vector /* threadIdx.x */
, Loop is parallelizable
main:
, Generating copy(x[:][:])
Accelerator kernel generated
Generating Tesla code
, #pragma acc loop gang /* blockIdx.x */ D:\Code\OpenACC\OpenACCProject\OpenACCProject>main_acc.exe x[][] = 11.000000
launch CUDA kernel file=D:\Code\OpenACC\OpenACCProject\OpenACCProject\main.c function=main
line= device= threadid= num_gangs= num_workers= vector_length= grid= block= // 8 个 gang 在 blockIdx.x 层级,1 个 worker,vector 在 threadIdx.x 层级 x[][] = 3.316625
PGI: "acc_shutdown" not detected, performance results might be incomplete.
Please add the call "acc_shutdown(acc_device_nvidia)" to the end of your application to ensure that the performance results are complete. Accelerator Kernel Timing data
D:\Code\OpenACC\OpenACCProject\OpenACCProject\main.c
main NVIDIA devicenum=
time(us):
: compute region reached time
: kernel launched time
grid: [] block: []
elapsed time(us): total= max= min= avg=
: data region reached times
: data copyin transfers:
device time(us): total= max= min= avg=
: data copyout transfers:
device time(us): total= max= min= avg=

● 输出结果,第 28 行添加并行级别子句 worker

 D:\Code\OpenACC\OpenACCProject\OpenACCProject>pgcc main.c -acc -Minfo -o main_acc.exe
sqab:
, Generating Tesla code
, #pragma acc loop vector /* threadIdx.x */
, Loop is parallelizable
main:
, Generating copy(x[:][:])
Accelerator kernel generated
Generating Tesla code
, #pragma acc loop worker(4) /* threadIdx.y */
, Loop is parallelizable D:\Code\OpenACC\OpenACCProject\OpenACCProject>main_acc.exe x[][] = 11.000000
launch CUDA kernel file=D:\Code\OpenACC\OpenACCProject\OpenACCProject\main.c function=main
line= device= threadid= num_gangs= num_workers= vector_length= grid= block=32x4 // 1 个 gang,4 个 worker 在 threadIdx.y 层级,使用 2 维线程网格 x[][] = 3.316625
PGI: "acc_shutdown" not detected, performance results might be incomplete.
Please add the call "acc_shutdown(acc_device_nvidia)" to the end of your application to ensure that the performance results are complete. Accelerator Kernel Timing data
D:\Code\OpenACC\OpenACCProject\OpenACCProject\main.c
main NVIDIA devicenum=
time(us):
: compute region reached time
: kernel launched time
grid: [] block: [32x4]
device time(us): total= max= min= avg=
: data region reached times
: data copyin transfers:
device time(us): total= max= min= avg=
: data copyout transfers:
device time(us): total= max= min= avg=

● 输出结果,第 28 行添加并行级别子句 vector

 D:\Code\OpenACC\OpenACCProject\OpenACCProject>pgcc main.c -acc -Minfo -o main_acc.exe
sqab:
, Generating Tesla code
, #pragma acc loop vector /* threadIdx.x */
, Loop is parallelizable
main:
, Generating copy(x[:][:])
Accelerator kernel generated
Generating Tesla code
, #pragma acc loop seq
, Loop is parallelizable D:\Code\OpenACC\OpenACCProject\OpenACCProject>main_acc.exe x[][] = 11.000000
launch CUDA kernel file=D:\Code\OpenACC\OpenACCProject\OpenACCProject\main.c function=main
line= device= threadid= num_gangs= num_workers= vector_length= grid= block= // 1 个 gang,1 个 worker,并行全都堆在 threadIdx.x 层级上 x[][] = 3.316625
PGI: "acc_shutdown" not detected, performance results might be incomplete.
Please add the call "acc_shutdown(acc_device_nvidia)" to the end of your application to ensure that the performance results are complete. Accelerator Kernel Timing data
D:\Code\OpenACC\OpenACCProject\OpenACCProject\main.c
main NVIDIA devicenum=
time(us):
: compute region reached time
: kernel launched time
grid: [] block: []
elapsed time(us): total= max= min= avg=
: data region reached times
: data copyin transfers:
device time(us): total= max= min= avg=
: data copyout transfers:
device time(us): total= max= min= avg=

● 如果自定义函数并行子句等级高于主调函数,则主调函数并行子句会变成 seq;如果自定义函数并行子句等级低于内部并行子句等级,则会报 warning,忽略掉内部并行子句:

 #pragma acc routine vector
void sqab(float *a, const int m)
{
#pragma acc loop worker
for (int idx = ; idx < m; idx++)
a[idx] = sqrtf(fabsf(a[idx]));
}

● 编译结果(运行结果通上面的 worker,不写)

D:\Code\OpenACC\OpenACCProject\OpenACCProject>pgcc main.c -acc -Minfo -o main_acc.exe
PGC-W--acc loop worker clause ignored in acc routine vector procedure (main.c: )
sqab:
, Generating Tesla code
, #pragma acc loop vector /* threadIdx.x */
, Loop is parallelizable

OpenACC 计算构建内的自定义函数的更多相关文章

  1. SQL Server 2008 R2——使用计算列为表创建自定义的自增列

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  请通过右侧公告中的“联系邮 ...

  2. hive的内置函数和自定义函数

    一.内置函数 1.一般常用函数 .取整函数 round() 当传入第二个参数则为精度 bround() 银行家舍入法:为5时,前一位为偶则舍,奇则进. .向下取整 floor() .向上取整 ceil ...

  3. python自定义函数和内置函数

    函数 1.定义 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 先定义,后使用 1.2分类 系统函数 自定义函数 1.3语法: def functionname(parameter ...

  4. [VBA]发布一个计算桩号之差的Excel自定义函数(VBA)

    这是一个可以计算桩号之差(也就是得到长度)的Excel(或WPS)扩展函数,可以减少工程师在统计工程量时的工作量. 该函数具有一定的通用性.可以在MS Office和金山WPS上使用. 文末会给出使用 ...

  5. 5.Smart使用内置函数或者自定义函数

    1.使用内置函数 例如使用date函数 {"Y-m-d"|date:$time}格式{第一个参数|方法:第二个参数:第三个参数}即可转换成 2016-07-19  2.使用resi ...

  6. JSP第四篇【EL表达式介绍、获取各类数据、11个内置对象、执行运算、回显数据、自定义函数、fn方法库】

    什么是EL表达式? 表达式语言(Expression Language,EL),EL表达式是用"${}"括起来的脚本,用来更方便的读取对象! EL表达式主要用来读取数据,进行内容的 ...

  7. Python之函数(自定义函数,内置函数,装饰器,迭代器,生成器)

    Python之函数(自定义函数,内置函数,装饰器,迭代器,生成器) 1.初始函数 2.函数嵌套及作用域 3.装饰器 4.迭代器和生成器 6.内置函数 7.递归函数 8.匿名函数

  8. Hive内置函数和自定义函数的使用

    一.内置函数的使用 查看当前hive版本支持的所有内置函数 show function; 查看某个函数的使用方法及作用,比如查看upper函数 desc function upper; 查看upper ...

  9. 利用函数计算构建微信小程序的Server端

    10分钟上线 - 利用函数计算构建微信小程序的Server端-博客-云栖社区-阿里云 https://yq.aliyun.com/articles/435430 函数计算  读写 oss import ...

随机推荐

  1. koa-connect源码解析

    文中提到的koa均为koa2 提到nodejs, 想必大家都知道express和koa. express: 大 koa: 小 比较的的是功能, 社区, 中间件,相关资源等 这里我就专门说说中间件吧, ...

  2. (5)可变、不可变和hash函数

    分类情况 与列表相似,列表用[],元组是()表示 内存角度看列表与数字的变与不变 列表 >>>l = [1,2,3,4] >>>id(l) 4392665160 & ...

  3. CTF-练习平台-WEB之 签到题

    一.签到题 根据提示直接加群在群公告里就能找到~

  4. Spring Data Jpa 使用@Query标注自定义查询语句

    https://blog.csdn.net/daniel7443/article/details/51159865 https://blog.csdn.net/pp_fzp/article/detai ...

  5. jQuery prop() 方法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 【java规则引擎】《Drools7.0.0.Final规则引擎教程》第4章 4.3 定时器

    定时器 规则用基于 interval(间隔)和cron的定时器(timer),替代了被标注过时的duration 属性.timer属性的使用示例: timer ( int: <initial d ...

  7. leetcode:Minimum Depth of Binary Tree【Python版】

    1.类中递归调用添加self: 2.root为None,返回0 3.root不为None,root左右孩子为None,返回1 4.返回l和r最小深度,l和r初始为极大值: # Definition f ...

  8. Drupal 7 建站学习手记(五):QuickTabs模块内的元素无法溢出的问题

    背景 项目要求站点首页放Views生成的区块,而且要求有很多其它链接. Views生成的区块默认的很多其它链接仅仅能选在列表上方和下方 下图是默认在上方的样式图: 为了美观.我将很多其它链接上移了若干 ...

  9. java中原子操作的实现分析

    一.CAS原理: CAS的全程即Compare And Swap,翻译成中文为比较并交换: CAS操作依赖于CPU指令CMPXCHG来实现比较并交换操作的原子性,通过查看HotSpot源码如下: 可以 ...

  10. some ideas

    1. 3d camera h/w: 单反+projector s/w: 抓图 -> 3d成像 -> 3d显示 -->  3d编辑(?具体要那些功能)--> to 3d prin ...