- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have a kernel that takes in an array of integers and returns the index of the smallest element.
#ifndef LOCAL_SIZE #define LOCAL_SIZE 8 #endif // LOCAL_SIZE kernel void test( global int* in, global int* out ) { int id = get_local_id(0); local int indx[LOCAL_SIZE]; int temp = id; for (int i = id; i < 1024; i += LOCAL_SIZE) { temp = in < in[temp] ? i : temp; } indx[id] = temp; barrier(CLK_LOCAL_MEM_FENCE); for(int i = LOCAL_SIZE / 2; i!= 0; i>>=1) { if(id < i) { printf( "%4d: %3d, %4d: %3d\n", indx[id], in[indx[id]], indx[id + i], in[indx[id + i]] ); indx[id] = in[indx[id]] < in[indx[id+ i]] ? indx[id] : indx[id + i]; } barrier(CLK_LOCAL_MEM_FENCE); if(id == 0) printf("\n"); } out[0] = indx[0]; }
Before the first barrier each work item finds its smallest value and places it into a local buffer. Everything works fine here.
In the for loop the results from each work item is reduced further to find the result. However the second to last iteration fails on GPU everytime: in[indx[id]] and in[indx[id + i]] both return the same value.
Operating system: Windows 7 Enterprise
Device Driver Version: 10.18.14.4280
Device: Intel HD 4600 & Processor Intel i5-4590
Works fine on CPU and Nvidia GTX 970
I've attached the kernel and host code to reproduce
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am afraid there might be in race in your code. What about this:
#ifndef LOCAL_SIZE #define LOCAL_SIZE 8 #endif // LOCAL_SIZE kernel void test( global int* in, global int* out ) { int id = get_local_id(0); local int indx[LOCAL_SIZE]; int temp = id; for (int i = id; i < 1024; i += LOCAL_SIZE) { temp = in < in[temp] ? i : temp; } indx[id] = temp; barrier(CLK_LOCAL_MEM_FENCE); for(int i = LOCAL_SIZE / 2; i!= 0; i>>=1) { int tmp = indx[id], tmpi = indx[id + i]; int val = in[tmp], vali = in[tmpi]; barrier(CLK_LOCAL_MEM_FENCE); if(id < i) { printf( "%4d: %3d, %4d: %3d\n", tmp, val, tmpi, vali ); indx[id] = val < vali ? tmp : tmpi; } barrier(CLK_LOCAL_MEM_FENCE); if(id == 0) printf("\n"); } out[0] = indx[0]; }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Robert I. (Intel) wrote:
I am afraid there might be in race in your code. What about this:
Nope, the issue still persist even with this change.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
However doing this fixed the problem:
#ifndef LOCAL_SIZE #define LOCAL_SIZE 8 #endif // LOCAL_SIZE kernel void test( global int* in, global int* out ) { int id = get_local_id(0); local int indx[LOCAL_SIZE]; int temp = id; for (int i = id; i < 1024; i += LOCAL_SIZE) { temp = in < in[temp] ? i : temp; } indx[id] = temp; barrier(CLK_LOCAL_MEM_FENCE); for(int i = LOCAL_SIZE / 2; i!= 0; i>>=1) { int tmp = indx[id]; int val = in[tmp]; barrier(CLK_LOCAL_MEM_FENCE); int tmpi = indx[id + i]; int vali = in[tmpi]; if(id < i) { printf( "%4d: %3d, %4d: %3d\n", tmp, val, tmpi, vali ); indx[id] = val < vali ? tmp : tmpi; } barrier(CLK_LOCAL_MEM_FENCE); if(id == 0) printf("\n"); } out[0] = indx[0]; }
I just don't quite understand why the program behaves the way it does.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ok, looks like a compiler team needs to take a look at this one :)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Joose,
I showed this to the compiler folks and they think this is a compiler bug. Will file a bug. Thanks!

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page