通过OpenCL内核代码猜测设备寄存器个数
在OpenCL标准中,没有给出查看计算设备一共有多少寄存器,至少能分配给每个work-item多少寄存器使用的特征查询。而由于一个段内核代码是否因寄存器紧缺而导致性能严重下降也是一个比较重要的因素,因此我这边提供一个比较基本的方法来猜测当前计算设备至少能为每个work-item分配多少可用的寄存器。
这个方法的思路是,先定义四个临时变量,然后在一个大规模循环里面做一定规模的计算。然后把时间统计出来。随后,再定义八个临时变量,仍然,在与前者相同次数的循环里做一定规模的计算,再把时间统计出来。一般,如果寄存器不爆,或者由于Cache的缘故,性能影响不大的话,两者消耗时间一般在2倍左右。如果后者比前者超了2.2倍以上,那么我们即可认为寄存器爆了~
这个方法对于一般的GPU更有用些。由于CPU往往拥有L1 Data Cache,当寄存器不够用的时候,编译器会将不太常用的数据放到栈中,而栈在此时往往能获得高命中率的Cache访问,因此性能不会过受影响。而GPU端当寄存器不够用时,编译器往往会采取将不常用数据直接存放到VRAM中,而对外部VRAM的访问往往是比较慢的,因此,如果临时变量太多,使得频繁访问外部存储器,会使得整体计算性能大幅下降。当然,现在不少GPU也有了L1 Cache,但是空间也十分有限。因此,这里用“猜”这个词,呵呵~
下面先提供四个临时变量的kernel代码:
__kernel void QueryRegisterCount(__global int *pInOut)
{
int index = get_global_id(); int i0 = pInOut[(index * + ) * ];
int i1 = pInOut[(index * + ) * ];
int i2 = pInOut[(index * + ) * ];
int i3 = pInOut[(index * + ) * ]; for(int i = ; i < ; i++)
{
i1 += i0 << ;
i2 += i1 << ;
i3 += i2 << ;
i0 += i3 << ; i1 += i0 >> ;
i2 += i1 >> ;
i3 += i2 >> ;
i0 += i3 >> ; i1 += i0 >> ;
i2 += i1 >> ;
i3 += i2 >> ;
i0 += i3 >> ; i1 += i0 >> ;
i2 += i1 >> ;
i3 += i2 >> ;
i0 += i3 >> ;
} pInOut[(index * + ) * ] = i0;
pInOut[(index * + ) * ] = i1;
pInOut[(index * + ) * ] = i2;
pInOut[(index * + ) * ] = i3;
}
再提供八个临时变量的kernel代码:
__kernel void QueryRegisterCount(__global int *pInOut)
{
int index = get_global_id(); int i0 = pInOut[(index * + ) * ];
int i1 = pInOut[(index * + ) * ];
int i2 = pInOut[(index * + ) * ];
int i3 = pInOut[(index * + ) * ];
int i4 = pInOut[(index * + ) * ];
int i5 = pInOut[(index * + ) * ];
int i6 = pInOut[(index * + ) * ];
int i7 = pInOut[(index * + ) * ]; for(int i = ; i < ; i++)
{
i1 += i0 << ;
i2 += i1 << ;
i3 += i2 << ;
i4 += i3 << ;
i5 += i4 << ;
i6 += i5 << ;
i7 += i6 << ;
i0 += i7 << ; i1 += i0 >> ;
i2 += i1 >> ;
i3 += i2 >> ;
i4 += i3 >> ;
i5 += i4 >> ;
i6 += i5 >> ;
i7 += i6 >> ;
i0 += i7 >> ; i1 += i0 >> ;
i2 += i1 >> ;
i3 += i2 >> ;
i4 += i3 >> ;
i5 += i4 >> ;
i6 += i5 >> ;
i7 += i6 >> ;
i0 += i7 >> ; i1 += i0 >> ;
i2 += i1 >> ;
i3 += i2 >> ;
i4 += i3 >> ;
i5 += i4 >> ;
i6 += i5 >> ;
i7 += i6 >> ;
i0 += i7 >> ;
} pInOut[(index * + ) * ] = i0;
pInOut[(index * + ) * ] = i1;
pInOut[(index * + ) * ] = i2;
pInOut[(index * + ) * ] = i3;
pInOut[(index * + ) * ] = i4;
pInOut[(index * + ) * ] = i5;
pInOut[(index * + ) * ] = i6;
pInOut[(index * + ) * ] = i7;
}
而像16个、32个临时变量的方法依此类推~
然后,给出主机端代码:
/** Prepare for running an OpenCL kernel program to get register count */
/*Step 4: Creating command queue associate with the context.*/
commandQueue = clCreateCommandQueue(context, device, CL_QUEUE_PROFILING_ENABLE, NULL);
/*Step 5: Create program object */
// Read the kernel code to the buffer
kernelPath = [[NSBundle mainBundle] pathForResource:@"reg" ofType:@"ocl"];
aSource = [[NSString stringWithContentsOfFile:kernelPath encoding:NSUTF8StringEncoding error:nil] UTF8String];
kernelLength = strlen(aSource);
program = clCreateProgramWithSource(context, , &aSource, &kernelLength, NULL);
/*Step 6: Build program. */
status = clBuildProgram(program, , &device, NULL, NULL, NULL);
/*Step 7: Initial inputs and output for the host and create memory objects for the kernel*/
const size_t memSize = global_work_size[] * * * ;
cl_int *orgBufer = (cl_int*)malloc(memSize);
memset(orgBufer, , memSize);
outputMemObj = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, memSize, orgBufer, NULL);
/*Step 8: Create kernel object */
kernel = clCreateKernel(program, "QueryRegisterCount", NULL);
/*Step 9: Sets Kernel arguments.*/
status |= clSetKernelArg(kernel, , sizeof(outputMemObj), &outputMemObj);
/*Step 10: Running the kernel.*/
for(int i = ; i < ; i++)
{
NSTimeInterval beginTime = [[NSProcessInfo processInfo] systemUptime];
status |= clEnqueueNDRangeKernel(commandQueue, kernel, , NULL, global_work_size, local_work_size, , NULL, NULL);
clFinish(commandQueue);
NSTimeInterval endTime = [[NSProcessInfo processInfo] systemUptime];
NSLog(@"Time spent: %f", endTime - beginTime);
}
free(orgBufer);
if(status != CL_SUCCESS)
{
NSLog(@"Program built failed!");
return;
}
clReleaseMemObject(outputMemObj);
clReleaseProgram(program);
clReleaseKernel(kernel);
clReleaseCommandQueue(commandQueue);
clReleaseContext(context);
以上由于是在OS X下开发的,因此直接用Objective-C文件读写更方便些。但是大部分都是C代码,很容易读懂。
其中,最后一断代码中,我们做5次循环,统计时间。我们比较的时候往往选出5次执行时间中最小耗费的时间进行比较。
在2013年的MacBook Air中的Intel HD 5000中的测试结果为:
四个临时变量耗费:0.061020秒
八个临时变量耗费:0.121868秒
十六个临时变量耗费:0.243470秒
三十二个临时变量耗费:0.719506秒
很显然,我们可以猜得,Intel HD Graphics 5000至少可以为每个work-item分配16个寄存器。
我们如果要用在实际应用场合,可以通过动态生成kernel字符串依次执行进行检测,直到相邻两段kernel的执行时间超过2.2倍,那么我们即可终止。
通过OpenCL内核代码猜测设备寄存器个数的更多相关文章
- linux内核代码注释 赵炯 第三章引导启动程序
linux内核代码注释 第三章引导启动程序 boot目录中的三个汇编代码文件 bootsect.s和setup.s采用近似intel的汇编语法,需要8086汇编器连接器as86和ld86 head ...
- 【自制操作系统06】终于开始用 C 语言了,第一行内核代码!
一.整理下到目前为止的流程图 写到这,终于才把一些苦力活都干完了,也终于到了我们的内核代码部分,也终于开始第一次用 c 语言写代码了!为了这个阶段性的胜利,以及更好地进入内核部分,下图贴一张到目前为止 ...
- Linux内核分析—完成一个简单的时间片轮转多道程序内核代码
---恢复内容开始--- 20135125陈智威 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-10 ...
- [转] Linux内核代码风格 CodingStyle [CH]
from:http://blog.csdn.net/jiang_dlut/article/details/8163731 中文版维护者: 张乐 Zhang Le <r0bertz@gentoo. ...
- 从linux内核代码分析操作系统启动过程
朱宇轲 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 在本次的实验中, ...
- 第三次阅读赵炯博士的《linux内核代码完全注释》:序
这是我第三次阅读linux内核代码完全注释了,当然前两次也没有读完,第一次读到第五章,第二次第七章. 所以说,赵炯博士对我最大的帮助时介绍了intel386的结构,以及内核编程的方法. 至于真正的内核 ...
- Linux学习笔记:【004】Linux内核代码风格
Chinese translated version of Documentation/CodingStyle If you have any comment or update to the c ...
- [11]Windows内核情景分析---设备驱动
设备驱动 设备栈:从上层到下层的顺序依次是:过滤设备.类设备.过滤设备.小端口设备[过.类.过滤.小端口] 驱动栈:因设备堆栈原因而建立起来的一种堆栈 老式驱动:指不提供AddDevice的驱动,又叫 ...
- [转] LINUX内核代码编程规范
这是一个简短的文档,描述了linux内核的首选代码风格.代码风格是因人而异的,而且我 不愿意把我的观点强加给任何人,不过这里所讲述的是我必须要维护的代码所遵守的风格, 并且我也希望绝大多数其他代码也能 ...
随机推荐
- MySQL主从复制以及在本地环境搭建
MySQL主从复制原理: master(主服务器),slave(从服务器) MySQL master 将数据变更写入二进制日志( binary log, 其中记录叫做二进制日志事binary log ...
- 《数据结构与算法之美》 <06>栈:如何实现浏览器的前进和后退功能?
浏览器的前进.后退功能,我想你肯定很熟悉吧? 当你依次访问完一串页面 a-b-c 之后,点击浏览器的后退按钮,就可以查看之前浏览过的页面 b 和 a.当你后退到页面 a,点击前进按钮,就可以重新查看页 ...
- python 爬虫 Selenium的简单使用
一.Selenium基础介绍及安装 1.Selenium简介 Selenium是一个用于测试网站的自动化测试工具,支持各种浏览器包括Chrome.Firefox.Safari等主流界面浏览器,同时也支 ...
- chkconfig 系统服务管理
chkconfig --list 显示当前运行的所有服务/etc/inittabnetstat -antl all n端口 t tcp协议 l listen状态chkconfig --add ...
- TCP IP协议和网络安全
传输层的两个协议: 可靠传输 TCP 分段传输 建立对话(消耗系统资源) 丢失重传netstat -n 不可靠传输 UDP 一个数据包就能表达完整的意思或屏幕广播 应用层协议(默认 ...
- serializers--嵌套关系作为字段来表示
参考官网:https://www.django-rest-framework.org/api-guide/relations/#nested-relationships 先建立model class ...
- Mongo Restore
#!/bin/sh HOST_IP=`/sbin/ifconfig | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p' | head -n1` echo &q ...
- 指定js文件不使用 ESLint 语法检查
整个文件范围内禁止规则出现警告 将/* eslint-disable */放置于文件最顶部 /* eslint-disable */ alert('foo'); 在文件中临时禁止规则出现警告 将需要忽 ...
- 2014年9月9日 高级命令command的使用(上)
上次说到了菜单 工具条按钮 上下文菜单都已经改为用command的方式去做了,这次稍微详细地说说. 在command的实现拓展点handler上右键,可以看到有enabledWhen,activeWh ...
- mysql查看当前所有数据库中的表大小和元信息information_schema
查看所有mysql数据库表和索引大小 mysql查看当前所有的数据库和索引大小 ,),' mb') as data_size, concat(,),'mb') as index_size from i ...