OpenACC 计算构建内的自定义函数
▶ 使用 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 计算构建内的自定义函数的更多相关文章
- SQL Server 2008 R2——使用计算列为表创建自定义的自增列
=================================版权声明================================= 版权声明:原创文章 谢绝转载 请通过右侧公告中的“联系邮 ...
- hive的内置函数和自定义函数
一.内置函数 1.一般常用函数 .取整函数 round() 当传入第二个参数则为精度 bround() 银行家舍入法:为5时,前一位为偶则舍,奇则进. .向下取整 floor() .向上取整 ceil ...
- python自定义函数和内置函数
函数 1.定义 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 先定义,后使用 1.2分类 系统函数 自定义函数 1.3语法: def functionname(parameter ...
- [VBA]发布一个计算桩号之差的Excel自定义函数(VBA)
这是一个可以计算桩号之差(也就是得到长度)的Excel(或WPS)扩展函数,可以减少工程师在统计工程量时的工作量. 该函数具有一定的通用性.可以在MS Office和金山WPS上使用. 文末会给出使用 ...
- 5.Smart使用内置函数或者自定义函数
1.使用内置函数 例如使用date函数 {"Y-m-d"|date:$time}格式{第一个参数|方法:第二个参数:第三个参数}即可转换成 2016-07-19 2.使用resi ...
- JSP第四篇【EL表达式介绍、获取各类数据、11个内置对象、执行运算、回显数据、自定义函数、fn方法库】
什么是EL表达式? 表达式语言(Expression Language,EL),EL表达式是用"${}"括起来的脚本,用来更方便的读取对象! EL表达式主要用来读取数据,进行内容的 ...
- Python之函数(自定义函数,内置函数,装饰器,迭代器,生成器)
Python之函数(自定义函数,内置函数,装饰器,迭代器,生成器) 1.初始函数 2.函数嵌套及作用域 3.装饰器 4.迭代器和生成器 6.内置函数 7.递归函数 8.匿名函数
- Hive内置函数和自定义函数的使用
一.内置函数的使用 查看当前hive版本支持的所有内置函数 show function; 查看某个函数的使用方法及作用,比如查看upper函数 desc function upper; 查看upper ...
- 利用函数计算构建微信小程序的Server端
10分钟上线 - 利用函数计算构建微信小程序的Server端-博客-云栖社区-阿里云 https://yq.aliyun.com/articles/435430 函数计算 读写 oss import ...
随机推荐
- BZOJ3230: 相似子串【后缀数组】
Description Input 输入第1行,包含3个整数N,Q.Q代表询问组数. 第2行是字符串S. 接下来Q行,每行两个整数i和j.(1≤i≤j). Output 输出共Q行,每行一个数表示每组 ...
- 状压dp2
2018年全国多校算法寒假训练营练习比赛(第二场) https://www.nowcoder.com/acm/contest/74/F 上一篇状压dp例题由于每个位置都含有一个非负数,所以不需要判断能 ...
- 【java规则引擎】《Drools7.0.0.Final规则引擎教程》第4章 4.2 agenda-group
转载:https://blog.csdn.net/wo541075754/article/details/75332720 agenda-group 规则的调用与执行是通过StatelessKieSe ...
- [转]G++与GCC的区别
转自http://www.52pojie.cn/thread-58109-1-1.html 误区一:gcc只能编译c代码,g++只能编译c++代码两者都可以,但是请注意:1.后缀为.c的,gcc把它当 ...
- day39 python 学习 数据库学习 五个约束,数据库设计(一对一,一对多等等)
删除重复数据: 注意数据库不能又查又删 *******#删除作者为重复数据并保留ID最大的是数据 delete from ren where author in (select * from(sel ...
- 关于 Javascript 严格模式下多文件合并时注意
Javascript 在第一行使用 "use strict" 声明严格模式. 但是在多个 js 文件合并时就需要注意了,可能你的是严格模式,但别的文件不是,就会造成错误. 为什么使 ...
- SQL语言:DDL/DML/DQL/DCL
SQL (Structure Query Language)语言是数据库的核心语言. SQL 的发展是从1974年开始的,其发展过程如下: 1974年-----由Boyce和Chamberlin提出, ...
- java 多线程之:sleep() 方法
sleep()介绍 sleep() 定义在java.lang.Thread中. sleep() 的作用是让当前线程休眠,即当前线程会从"运行状态"进入到"休眠(阻塞)状态 ...
- mysql ssh 端口转发
某些时候 mysql 只允许 指定的 ip连接 .这时候怎么在本机 连接mysql 的呢? 条件 1 mysql 只有 允许 指定ip连接 2 有连接 指定 ip 服务器的 账密 这时候我们可以通 ...
- Extjs下拉多选框
//------录入时间,下拉列表框------ var inputTimeRow = new Ext.data.Record.create([ { name : 'value' },{ name : ...