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 ...
随机推荐
- js模板引擎---jtemplates使用
昨天记录了如何使用腾讯的模板引擎,今天记录一下jquery的模板引擎jtemplates.官网:http://jtemplates.tpython.com/ 编写模板:需要在页面引入jquery和jt ...
- Python3中 sys.argv的用法
sys.avgr 是一个Python的引用模块.刚好做一个作业需要用到它,在sublime上编辑后运行,试图从结果发现它的用途,然而结果一直都是没结果. 后面在网上查了资料,才明白过来.sys.arg ...
- spfa【模板】
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using ...
- 【转】python mysql数据库 'latin-1' codec can't encode character错误问题解决
UnicodeEncodeError: 'latin-1' codec can't encode character "UnicodeEncodeError:'latin-1' code ...
- PDF文本框更改字体大小
在Adobe Acrobat Professional 7.0版本后里单击所插入的文本框,会出现文本框属性,此时只能改文本框的属性,不能修改文本框内的字体大小 要改字体很简单,左键选中要改的文本,按 ...
- redis sentinel 高可用(HA)方案部署,及python应用示例
redis sentinel(哨兵)高可用集群的部署方法,并通过 python 程序实例讲解如何使用 redis sentinel 简介 介绍 redis sentinel(哨兵)集群的部署,配置一主 ...
- MySQL--Alter Table注意事项
======================================================================== ALTER TABLE 和FLUSH TABLE导致的 ...
- vulcanjs 开源工具方便快速开发react graphql meteor 应用
vulcan 开源工具方便快速开发react graphql meteor 应用 操作环境mac os 安装 meteor 安装(此安装有点慢,可以通过正确上网解决) curl https://ins ...
- Python包管理工具easy_install使用
easy_install是python的包管理工具,可以方便的下载.安装.更新python包,并可以自动处理相关依赖.类似于ruby的gem,java的maven,nodejs的npm. 安装方法有两 ...
- fork 开源项目后如何参与项目
好的开源项目都很想参与到开源活动中,并且会 fork 一份. 经过几个月的学习,大概明白了如果参与开源项目. 当完成 fork 后,就需要在本地 git clone 一份. 有新的功能或需要修复的就开 ...