在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. 【leetcode】637. Average of Levels in Binary Tree

    原题 Given a non-empty binary tree, return the average value of the nodes on each level in the form of ...

  2. “高可用性”(High Availability)??

    “高可用性”(High Availability)通常来描述一个系统经过专门的设计,从而减少停工时间,而保持其服务的高度可用性. 计算机的高可用性 计算机系统的可用性用平均无故障时间(MTTF)来度量 ...

  3. mxnet在windows使用gpu 出错

    1. cuda windows安装 官网下载 代码: import mxnet as mxfrom mxnet import ndfrom mxnet.gluon import nn a = nd.a ...

  4. B进制星球(多进制 高精加)

    https://www.luogu.org/problemnew/show/P1604 B(2<=B<=36)进制计数.编写实现B进制加法的程序. 输入输出格式 输入格式: 共3行第1行: ...

  5. P1006 传纸条[棋盘DP]

    题目来源:洛谷 题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接 ...

  6. webpack4 Cannot find module '@babel/core'

    Error: // webpackCannot find module '@babel/core'解决办法一: 原因"babel-loader": "^8.0.0&quo ...

  7. JS 过滤数组里对象的某个属性

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. ESLint 报错 import/no-unresolved

    马的,就这个规则百度了大半天终于找到可以用的: 不得不说百度真的辣鸡 还是翻墙去谷歌找到了解决方法 解决方法是:在 .eslintrc 中设置 "rules": { "i ...

  9. mongodb中find $ne null 与$exists的区别

    $ne null 会把空列表也算入,即使不存在. $exists 的识别效果就比较好 1.插入样例数据 db.nullexistsdemo.insertMany( [{ "name" ...

  10. 2019牛客多校第五场generator2——BSGS&&手写Hash

    题目 几乎原题 BZOJ3122题解 分析 先推一波公式,然后除去特殊情况分类讨论,剩下就是形如 $a^i \equiv b(mod \ p)$ 的方程,可以使用BSGS算法. 在标准的BSGS中,内 ...