在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内核代码猜测设备寄存器个数的更多相关文章

  1. linux内核代码注释 赵炯 第三章引导启动程序

    linux内核代码注释 第三章引导启动程序 boot目录中的三个汇编代码文件   bootsect.s和setup.s采用近似intel的汇编语法,需要8086汇编器连接器as86和ld86 head ...

  2. 【自制操作系统06】终于开始用 C 语言了,第一行内核代码!

    一.整理下到目前为止的流程图 写到这,终于才把一些苦力活都干完了,也终于到了我们的内核代码部分,也终于开始第一次用 c 语言写代码了!为了这个阶段性的胜利,以及更好地进入内核部分,下图贴一张到目前为止 ...

  3. Linux内核分析—完成一个简单的时间片轮转多道程序内核代码

    ---恢复内容开始--- 20135125陈智威 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-10 ...

  4. [转] Linux内核代码风格 CodingStyle [CH]

    from:http://blog.csdn.net/jiang_dlut/article/details/8163731 中文版维护者: 张乐 Zhang Le <r0bertz@gentoo. ...

  5. 从linux内核代码分析操作系统启动过程

    朱宇轲 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 在本次的实验中, ...

  6. 第三次阅读赵炯博士的《linux内核代码完全注释》:序

    这是我第三次阅读linux内核代码完全注释了,当然前两次也没有读完,第一次读到第五章,第二次第七章. 所以说,赵炯博士对我最大的帮助时介绍了intel386的结构,以及内核编程的方法. 至于真正的内核 ...

  7. Linux学习笔记:【004】Linux内核代码风格

    Chinese translated version of Documentation/CodingStyle   If you have any comment or update to the c ...

  8. [11]Windows内核情景分析---设备驱动

    设备驱动 设备栈:从上层到下层的顺序依次是:过滤设备.类设备.过滤设备.小端口设备[过.类.过滤.小端口] 驱动栈:因设备堆栈原因而建立起来的一种堆栈 老式驱动:指不提供AddDevice的驱动,又叫 ...

  9. [转] LINUX内核代码编程规范

    这是一个简短的文档,描述了linux内核的首选代码风格.代码风格是因人而异的,而且我 不愿意把我的观点强加给任何人,不过这里所讲述的是我必须要维护的代码所遵守的风格, 并且我也希望绝大多数其他代码也能 ...

随机推荐

  1. 【ZZNU-oj-2116:人间不值得】(1亿以内的货币拼音转数值求折扣价原价)--hash+String大法好+字符串处理+超大暴力模拟题

    B : 人间不值得 概览问题列表状态排名编辑 Progress Bar 时间限制:1 Sec 内存限制:256 MiB提交:146 答案正确:12 提交 编辑 题目描述 家缠万贯来几时,我今停杯一问之 ...

  2. Python+request 登录接口reponse的返回值token跨py文件的传递《二》

    主要使用场景: 一般我们在进行接口测试时,依赖登录接口后reponse中的某些返回值,因此需要将login接口单独写一个py文件,另外的py文件均可调用login的reponse返回值使用.共用登录接 ...

  3. Oracle-锁的查询和处理

    1.查出锁定object的session的信息以及被锁定的object名 SELECT L.SESSION_ID SID, S.SERIAL#, L.LOCKED_MODE, L.ORACLE_USE ...

  4. jQuery匿名函数和自定义插件

    https://www.cnblogs.com/joey0210/p/3408349.html (function($){ //do something;   })(jQuery);

  5. C语言I作业12一学期总结

    一.我学到的内容 二.我的收获 作业 收获 C语言I博客作业01 学会了编程"Hello word" C语言I博客作业02 安装编译器,将代码建立在自己的文件里面 C语言I博客作业 ...

  6. django.db.utils.OperationalError: (1050, "Table 'article_category' already exists")

    (转自:https://blog.csdn.net/huanhuanq1209/article/details/77884014) 执行manage.py makemigrations 未提示错误信息 ...

  7. 快捷键IntelliJ IDEA For Mac

    http://www.cnblogs.com/wxd0108/p/5295017.html Mac键盘符号和修饰键说明 ⌘ Command ⇧ Shift ⌥ Option ⌃ Control ↩︎  ...

  8. P3355 骑士共存问题【洛谷】(二分图最大独立集变形题) //链接矩阵存图

    展开 题目描述 在一个 n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘上某些方格设置了障碍,骑士不得进入 对于给定的 n*n 个方格的国际象棋棋盘和障碍标志,计算棋盘上最多可 ...

  9. spring boot项目接入xxl-job

    关于分布式任务调度平台XXL-JOB,作者 许雪里 在其发布的中文教程中已经介绍的很清楚了,这里就不做过多的介绍了.按照文档搭建xxl-job,做此纪录. 1.源码下载地址 GitHub:https: ...

  10. tinymce+粘贴word图片例子

    tinymce是很优秀的一款富文本编辑器,可以去官网下载.https://www.tiny.cloud 这里分享的是它官网的一个收费插件powerpaste的旧版本源码,但也不影响功能使用. http ...