简体   繁体   English

OpenCL内核汇总

[英]OpenCL Kernel summatory

I want to know if this is possible to do... I want to check inside of the kernel in java, if the array contains numbers and characters, and if yes, save how many times they appear in the output array. 我想知道这是否有可能...我想检查java内核内部,数组是否包含数字和字符,如果是,请保存它们在输出数组中出现的次数。

private static String programSource =
            "__kernel void sampleKernel(__global const char *a, __global int *c){" +
            "   c[0]=0; c[1]=0;"+
            "   int gid = get_global_id(0);" +
            "   if((a[gid] > 64 && a[gid] < 91) || (a[gid] > 96 && a[gid] < 123)) c[0]+=1; "+
            "   else if(a[gid] > 47 && a[gid] < 58) c[1]+=1;" + 
            "}";

This is the code I have... but in the output array it always the number 1... What is wrong? 这是我的代码...但是在输出数组中,它始终是数字1 ...这是什么问题? What would be the solution for this problem? 该问题的解决方案是什么?

Thanks! 谢谢!

All work-items are modifying c[0] and c[1] at the same time, resulting in the wrong output. 所有工作项同时修改c [0]和c [1],导致输出错误。

One solution would be to use atomic_inc . 一种解决方案是使用atomic_inc atomic_inc(c) instead of c[0]+=1, and atomic_inc(c+1) instead of c[1]+=1. atomic_inc(c)代替c [0] + = 1,atomic_inc(c + 1)代替c [1] + = 1。

Next, since you have only two outputs, there will be a lot of collisions, and this will probably be quite slow. 接下来,由于只有两个输出,因此会有很多冲突,并且这可能会很慢。 A reduction-like algorithm will be preferable. 类似减少的算法将是更可取的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM