简体   繁体   中英

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.

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? 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.

One solution would be to use atomic_inc . atomic_inc(c) instead of c[0]+=1, and atomic_inc(c+1) instead of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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