concept write in category parallel computing

This is an excerpt from Manning's book Parallel and High Performance Computing.
We could simply port this to the GPU, as was done in the earlier cases, by removing the for loop and replacing it with a call to get_global_id. But the GPU has a small local memory cache that is shared by each workgroup that can hold about 4,000 double precision values. We have 1173 values in the table and 51+23 axis values. These can fit in the local memory cache which can be accessed very quickly and shared among all the threads in the workgroup. The code in listing 5.10 shows how this is done. The first part of the code cooperatively loads the data values into the local memory using all of the threads. A synchronization is required to guarantee that all the data is loaded before moving on to the interpolation kernel. The remaining code looks much the same as the code for the CPU.
Listing 5.10 The table interpolation code in OpenCL for the GPU
table_kern.cl from PerfectHash 45 #define dataval(x,y) data[(x)+((y)*xstride)] 46 47 __kernel void interpolate_kernel( 48 const uint isize, 49 const uint xaxis_size, 50 const uint yaxis_size, 51 const uint data_size, 52 __global const double *xaxis_buffer, 53 __global const double *yaxis_buffer, 54 __global const double *data_buffer, 55 __local double *xaxis, 56 __local double *yaxis, 57 __local double *data, 58 __global const double *x_array, 59 __global const double *y_array, 60 __global double *value 61 ) 62 { 63 const uint tid = get_local_id(0); 64 const uint wgs = get_local_size(0); 65 const uint gid = get_global_id(0); 66 67 if (tid < xaxis_size) xaxis[tid]=xaxis_buffer[tid]; #A 68 if (tid < yaxis_size) yaxis[tid]=yaxis_buffer[tid]; #A 69 70 for (uint wid = tid; wid<data_size; wid+=wgs){ #B 71 data[wid] = data_buffer[wid]; #B 72 } #B 73 74 barrier(CLK_LOCAL_MEM_FENCE); #C 75 76 double x_increment = (xaxis[50]-xaxis[0])/50.0; #D 77 double y_increment = (yaxis[22]-yaxis[0])/22.0; #D 78 79 int xstride = 51; 80 81 if (gid < isize) { 82 double xdata = x_array[gid]; #E 83 double ydata = y_array[gid]; #E 84 85 int islot = (int)((xdata-xaxis[0])/x_increment); #F 86 int jslot = (int)((ydata-yaxis[0])/y_increment); #F 87 double xfrac = (xdata-xaxis[islot])/(xaxis[islot+1]-xaxis[islot]); #F 88 double yfrac = (ydata-yaxis[jslot])/(yaxis[jslot+1]-yaxis[jslot]); #F 89 90 value[gid] = xfrac * yfrac *dataval(islot+1,jslot+1) #G 91 + (1.0-xfrac)* yfrac *dataval(islot, jslot+1) #G 92 + xfrac *(1.0-yfrac)*dataval(islot+1,jslot) #G 93 + (1.0-xfrac)*(1.0-yfrac)*dataval(islot, jslot); #G 94 } 95 }