Browse Source

Converted tabs to spaces and fixed indentation

In this mega commit I tried to convert all the tabs presents in the
source files to spaces and to fix the indentation accrodingly, with the
goal to improve the readability of the source code.

I kept all these changes in a single commit instead of putting them in
the various commit on the source files to avoid cluttering the diffs of
the commits.
Andrea Gussoni 9 năm trước cách đây
mục cha
commit
846e980370
59 tập tin đã thay đổi với 10633 bổ sung và 10650 xóa
  1. 93 93
      opencl/backprop/backprop.c
  2. 24 24
      opencl/backprop/backprop_kernel.cl
  3. 202 202
      opencl/backprop/backprop_ocl.cpp
  4. 3 3
      opencl/backprop/imagenet.c
  5. 388 388
      opencl/bfs/CLHelper.h
  6. 224 224
      opencl/bfs/bfs.cpp
  7. 743 743
      opencl/cfd/CLHelper.h
  8. 319 319
      opencl/cfd/euler3d.cpp
  9. 10 14
      opencl/dwt2d/dwt.cpp
  10. 252 252
      opencl/dwt2d/main.cpp
  11. 267 267
      opencl/gaussian/clutils.cpp
  12. 14 14
      opencl/gaussian/clutils.h
  13. 115 116
      opencl/gaussian/gaussianElim.cpp
  14. 4 4
      opencl/gaussian/gaussianElim.h
  15. 12 12
      opencl/gaussian/utils.cpp
  16. 3 3
      opencl/gaussian/utils.h
  17. 88 88
      opencl/hotspot/OpenCL_helper_library.c
  18. 210 210
      opencl/hotspot/hotspot.c
  19. 9 9
      opencl/hotspot/hotspot.h
  20. 426 426
      opencl/hybridsort/CLHelper.h
  21. 127 127
      opencl/hybridsort/bucketsort.c
  22. 2 2
      opencl/hybridsort/bucketsort.h
  23. 51 54
      opencl/hybridsort/hybridsort.c
  24. 78 78
      opencl/hybridsort/mergesort.c
  25. 3 3
      opencl/hybridsort/mergesort.h
  26. 61 62
      opencl/kmeans/cluster.c
  27. 181 181
      opencl/kmeans/kmeans.cpp
  28. 2 2
      opencl/kmeans/kmeans.h
  29. 56 57
      opencl/kmeans/kmeans_clustering.c
  30. 135 136
      opencl/kmeans/read_input.c
  31. 481 481
      opencl/lavaMD/kernel/kernel_gpu_opencl_wrapper.c
  32. 6 6
      opencl/lavaMD/kernel/kernel_gpu_opencl_wrapper.h
  33. 265 265
      opencl/lavaMD/main.c
  34. 28 28
      opencl/lavaMD/main.h
  35. 92 92
      opencl/leukocyte/OpenCL/OpenCL_helper_library.c
  36. 214 215
      opencl/leukocyte/OpenCL/avilib.c
  37. 41 41
      opencl/leukocyte/OpenCL/avilib.h
  38. 589 589
      opencl/leukocyte/OpenCL/find_ellipse.c
  39. 45 45
      opencl/leukocyte/OpenCL/misc_math.c
  40. 512 512
      opencl/leukocyte/OpenCL/track_ellipse.c
  41. 171 171
      opencl/leukocyte/OpenCL/track_ellipse_opencl.c
  42. 235 237
      opencl/lud/ocl/lud.cpp
  43. 251 252
      opencl/nn/clutils.cpp
  44. 13 13
      opencl/nn/clutils.h
  45. 37 38
      opencl/nn/nearestNeighbor.cpp
  46. 6 6
      opencl/nn/nearestNeighbor.h
  47. 305 306
      opencl/nw/nw.c
  48. 10 10
      opencl/particlefilter/ex_particle_OCL_double_seq.cpp
  49. 292 292
      opencl/particlefilter/ex_particle_OCL_naive_seq.cpp
  50. 10 10
      opencl/particlefilter/ex_particle_OCL_single_seq.cpp
  51. 236 236
      opencl/pathfinder/OpenCL.cpp
  52. 24 24
      opencl/pathfinder/OpenCL.h
  53. 171 171
      opencl/pathfinder/main.cpp
  54. 1122 1122
      opencl/srad/kernel/kernel_gpu_opencl_wrapper.c
  55. 14 14
      opencl/srad/kernel/kernel_gpu_opencl_wrapper.h
  56. 219 219
      opencl/srad/main.c
  57. 975 975
      opencl/streamcluster/CLHelper.h
  58. 146 146
      opencl/streamcluster/streamcluster.cpp
  59. 21 21
      opencl/streamcluster/streamcluster.h

+ 93 - 93
opencl/backprop/backprop.c

@@ -61,8 +61,8 @@ float *alloc_1d_dbl(int n)
 
   new_t = (float *) malloc ((unsigned) (n * sizeof (float)));
   if (new_t == NULL) {
-    printf("ALLOC_1D_DBL: Couldn't allocate array of floats\n");
-    return (NULL);
+  printf("ALLOC_1D_DBL: Couldn't allocate array of floats\n");
+  return (NULL);
   }
   return (new_t);
 }
@@ -77,12 +77,12 @@ float **alloc_2d_dbl(int m, int n)
 
   new_t = (float **) malloc ((unsigned) (m * sizeof (float *)));
   if (new_t == NULL) {
-    printf("ALLOC_2D_DBL: Couldn't allocate array of dbl ptrs\n");
-    return (NULL);
+  printf("ALLOC_2D_DBL: Couldn't allocate array of dbl ptrs\n");
+  return (NULL);
   }
 
   for (i = 0; i < m; i++) {
-    new_t[i] = alloc_1d_dbl(n);
+  new_t[i] = alloc_1d_dbl(n);
   }
 
   return (new_t);
@@ -94,20 +94,20 @@ void bpnn_randomize_weights(float **w, int m, int n)
   int i, j;
 
   for (i = 0; i <= m; i++) {
-    for (j = 0; j <= n; j++) {
-     w[i][j] = (float) rand()/RAND_MAX;
-    //  w[i][j] = dpn1();
-    }
+  for (j = 0; j <= n; j++) {
+   w[i][j] = (float) rand()/RAND_MAX;
+  //  w[i][j] = dpn1();
+  }
   }
 }
 
 void bpnn_randomize_row(float *w, int m)
 {
-	int i;
-	for (i = 0; i <= m; i++) {
-     //w[i] = (float) rand()/RAND_MAX;
-	 w[i] = 0.1;
-    }
+  int i;
+  for (i = 0; i <= m; i++) {
+   //w[i] = (float) rand()/RAND_MAX;
+   w[i] = 0.1;
+  }
 }
 
 
@@ -116,9 +116,9 @@ void bpnn_zero_weights(float **w, int m, int n)
   int i, j;
 
   for (i = 0; i <= m; i++) {
-    for (j = 0; j <= n; j++) {
-      w[i][j] = 0.0;
-    }
+  for (j = 0; j <= n; j++) {
+  w[i][j] = 0.0;
+  }
   }
 }
 
@@ -136,8 +136,8 @@ BPNN *bpnn_internal_create(int n_in, int n_hidden, int n_out)
 
   newnet = (BPNN *) malloc (sizeof (BPNN));
   if (newnet == NULL) {
-    printf("BPNN_CREATE: Couldn't allocate neural network\n");
-    return (NULL);
+  printf("BPNN_CREATE: Couldn't allocate neural network\n");
+  return (NULL);
   }
 
   newnet->input_n = n_in;
@@ -177,15 +177,15 @@ void bpnn_free(BPNN *net)
   free((char *) net->target);
 
   for (i = 0; i <= n1; i++) {
-    free((char *) net->input_weights[i]);
-    free((char *) net->input_prev_weights[i]);
+  free((char *) net->input_weights[i]);
+  free((char *) net->input_prev_weights[i]);
   }
   free((char *) net->input_weights);
   free((char *) net->input_prev_weights);
 
   for (i = 0; i <= n2; i++) {
-    free((char *) net->hidden_weights[i]);
-    free((char *) net->hidden_prev_weights[i]);
+  free((char *) net->hidden_weights[i]);
+  free((char *) net->hidden_prev_weights[i]);
   }
   free((char *) net->hidden_weights);
   free((char *) net->hidden_prev_weights);
@@ -195,12 +195,12 @@ void bpnn_free(BPNN *net)
 
 
 /*** Creates a new fully-connected network from scratch,
-     with the given numbers of input, hidden, and output units.
-     Threshold units are automatically included.  All weights are
-     randomly initialized.
+   with the given numbers of input, hidden, and output units.
+   Threshold units are automatically included.  All weights are
+   randomly initialized.
 
-     Space is also allocated for temporary storage (momentum weights,
-     error computations, etc).
+   Space is also allocated for temporary storage (momentum weights,
+   error computations, etc).
 ***/
 
 BPNN *bpnn_create(int n_in, int n_hidden, int n_out)
@@ -233,54 +233,54 @@ void bpnn_layerforward(float *l1, float *l2, float **conn, int n1, int n2)
 #ifdef OPEN
   omp_set_num_threads(NUM_THREAD);
   #pragma omp parallel for shared(conn, n1, n2, l1) private(k, j) reduction(+: sum) schedule(static)
-#endif 
+#endif
   /*** For each unit in second layer ***/
   for (j = 1; j <= n2; j++) {
 
-    /*** Compute weighted sum of its inputs ***/
-    sum = 0.0;
-    for (k = 0; k <= n1; k++) {	
-      sum += conn[k][j] * l1[k]; 
-    }
-    l2[j] = squash(sum);
+  /*** Compute weighted sum of its inputs ***/
+  sum = 0.0;
+  for (k = 0; k <= n1; k++) {
+  sum += conn[k][j] * l1[k];
+  }
+  l2[j] = squash(sum);
   }
 }
 
-void bpnn_output_error(float *delta, float *target, float *output, int nj, float *err)  
+void bpnn_output_error(float *delta, float *target, float *output, int nj, float *err)
 {
   int j;
   float o, t, errsum;
   errsum = 0.0;
   for (j = 1; j <= nj; j++) {
-    o = output[j];
-    t = target[j];
-    delta[j] = o * (1.0 - o) * (t - o);
-    errsum += ABS(delta[j]);
+  o = output[j];
+  t = target[j];
+  delta[j] = o * (1.0 - o) * (t - o);
+  errsum += ABS(delta[j]);
   }
   *err = errsum;
 }
 
 
-void bpnn_hidden_error(float *delta_h,   
-					   int nh, 
-					   float *delta_o, 
-					   int no, 
-					   float **who, 
-					   float *hidden, 
-					   float *err)
+void bpnn_hidden_error(float *delta_h,
+   int nh,
+   float *delta_o,
+   int no,
+   float **who,
+   float *hidden,
+   float *err)
 {
   int j, k;
   float h, sum, errsum;
 
   errsum = 0.0;
   for (j = 1; j <= nh; j++) {
-    h = hidden[j];
-    sum = 0.0;
-    for (k = 1; k <= no; k++) {
-      sum += delta_o[k] * who[j][k];
-    }
-    delta_h[j] = h * (1.0 - h) * sum;
-    errsum += ABS(delta_h[j]);
+  h = hidden[j];
+  sum = 0.0;
+  for (k = 1; k <= no; k++) {
+  sum += delta_o[k] * who[j][k];
+  }
+  delta_h[j] = h * (1.0 - h) * sum;
+  errsum += ABS(delta_h[j]);
   }
   *err = errsum;
 }
@@ -295,16 +295,16 @@ void bpnn_adjust_weights(float *delta, int ndelta, float *ly, int nly, float **w
 #ifdef OPEN
   omp_set_num_threads(NUM_THREAD);
   #pragma omp parallel for  \
-      shared(oldw, w, delta) \
-	  private(j, k, new_dw) \
-	  firstprivate(ndelta, nly, momentum) 
-#endif 
+  shared(oldw, w, delta) \
+  private(j, k, new_dw) \
+  firstprivate(ndelta, nly, momentum)
+#endif
   for (j = 1; j <= ndelta; j++) {
-    for (k = 0; k <= nly; k++) {
-      new_dw = ((ETA * delta[j] * ly[k]) + (MOMENTUM * oldw[k][j]));
-	  w[k][j] += new_dw;
-	  oldw[k][j] = new_dw;
-    }
+  for (k = 0; k <= nly; k++) {
+  new_dw = ((ETA * delta[j] * ly[k]) + (MOMENTUM * oldw[k][j]));
+  w[k][j] += new_dw;
+  oldw[k][j] = new_dw;
+  }
   }
 }
 
@@ -319,9 +319,9 @@ void bpnn_feedforward(BPNN *net)
 
   /*** Feed forward input activations. ***/
   bpnn_layerforward(net->input_units, net->hidden_units,
-      net->input_weights, in, hid);
+  net->input_weights, in, hid);
   bpnn_layerforward(net->hidden_units, net->output_units,
-      net->hidden_weights, hid, out);
+  net->hidden_weights, hid, out);
 
 }
 
@@ -337,23 +337,23 @@ void bpnn_train(BPNN *net, float *eo, float *eh)
 
   /*** Feed forward input activations. ***/
   bpnn_layerforward(net->input_units, net->hidden_units,
-      net->input_weights, in, hid);
+  net->input_weights, in, hid);
   bpnn_layerforward(net->hidden_units, net->output_units,
-      net->hidden_weights, hid, out);
+  net->hidden_weights, hid, out);
 
   /*** Compute error on output and hidden units. ***/
   bpnn_output_error(net->output_delta, net->target, net->output_units,
-      out, &out_err);
+  out, &out_err);
   bpnn_hidden_error(net->hidden_delta, hid, net->output_delta, out,
-      net->hidden_weights, net->hidden_units, &hid_err);
+  net->hidden_weights, net->hidden_units, &hid_err);
   *eo = out_err;
   *eh = hid_err;
 
   /*** Adjust input and hidden weights. ***/
   bpnn_adjust_weights(net->output_delta, out, net->hidden_units, hid,
-      net->hidden_weights, net->hidden_prev_weights);
+  net->hidden_weights, net->hidden_prev_weights);
   bpnn_adjust_weights(net->hidden_delta, hid, net->input_units, in,
-      net->input_weights, net->input_prev_weights);
+  net->input_weights, net->input_prev_weights);
 
 }
 
@@ -371,8 +371,8 @@ void bpnn_save(BPNN *net, char *filename)
   ///////
   /*
   if ((fd = creat(filename, 0644)) == -1) {
-    printf("BPNN_SAVE: Cannot create '%s'\n", filename);
-    return;
+  printf("BPNN_SAVE: Cannot create '%s'\n", filename);
+  return;
   }
   */
 
@@ -388,17 +388,17 @@ void bpnn_save(BPNN *net, char *filename)
   fwrite( (char *) &n2 , sizeof(char), sizeof(char), pFile);
   fwrite( (char *) &n3 , sizeof(char), sizeof(char), pFile);
 
-  
+
 
   memcnt = 0;
   w = net->input_weights;
   mem = (char *) malloc ((unsigned) ((n1+1) * (n2+1) * sizeof(float)));
   for (i = 0; i <= n1; i++) {
-    for (j = 0; j <= n2; j++) {
-      dvalue = w[i][j];
-      fastcopy(&mem[memcnt], &dvalue, sizeof(float));
-      memcnt += sizeof(float);
-    }
+  for (j = 0; j <= n2; j++) {
+  dvalue = w[i][j];
+  fastcopy(&mem[memcnt], &dvalue, sizeof(float));
+  memcnt += sizeof(float);
+  }
   }
   //write(fd, mem, (n1+1) * (n2+1) * sizeof(float));
   fwrite( mem , (unsigned)(sizeof(float)), (unsigned) ((n1+1) * (n2+1) * sizeof(float)) , pFile);
@@ -408,11 +408,11 @@ void bpnn_save(BPNN *net, char *filename)
   w = net->hidden_weights;
   mem = (char *) malloc ((unsigned) ((n2+1) * (n3+1) * sizeof(float)));
   for (i = 0; i <= n2; i++) {
-    for (j = 0; j <= n3; j++) {
-      dvalue = w[i][j];
-      fastcopy(&mem[memcnt], &dvalue, sizeof(float));
-      memcnt += sizeof(float);
-    }
+  for (j = 0; j <= n3; j++) {
+  dvalue = w[i][j];
+  fastcopy(&mem[memcnt], &dvalue, sizeof(float));
+  memcnt += sizeof(float);
+  }
   }
   //write(fd, mem, (n2+1) * (n3+1) * sizeof(float));
   fwrite( mem , sizeof(float), (unsigned) ((n2+1) * (n3+1) * sizeof(float)) , pFile);
@@ -430,7 +430,7 @@ BPNN *bpnn_read(char *filename)
   int fd, n1, n2, n3, i, j, memcnt;
 
   if ((fd = open(filename, 0, 0644)) == -1) {
-    return (NULL);
+  return (NULL);
   }
 
   printf("Reading '%s'\n", filename);  //fflush(stdout);
@@ -447,10 +447,10 @@ BPNN *bpnn_read(char *filename)
   mem = (char *) malloc ((unsigned) ((n1+1) * (n2+1) * sizeof(float)));
   read(fd, mem, (n1+1) * (n2+1) * sizeof(float));
   for (i = 0; i <= n1; i++) {
-    for (j = 0; j <= n2; j++) {
-      fastcopy(&(new_t->input_weights[i][j]), &mem[memcnt], sizeof(float));
-      memcnt += sizeof(float);
-    }
+  for (j = 0; j <= n2; j++) {
+  fastcopy(&(new_t->input_weights[i][j]), &mem[memcnt], sizeof(float));
+  memcnt += sizeof(float);
+  }
   }
   free(mem);
 
@@ -460,10 +460,10 @@ BPNN *bpnn_read(char *filename)
   mem = (char *) malloc ((unsigned) ((n2+1) * (n3+1) * sizeof(float)));
   read(fd, mem, (n2+1) * (n3+1) * sizeof(float));
   for (i = 0; i <= n2; i++) {
-    for (j = 0; j <= n3; j++) {
-      fastcopy(&(new_t->hidden_weights[i][j]), &mem[memcnt], sizeof(float));
-      memcnt += sizeof(float);
-    }
+  for (j = 0; j <= n3; j++) {
+  fastcopy(&(new_t->hidden_weights[i][j]), &mem[memcnt], sizeof(float));
+  memcnt += sizeof(float);
+  }
   }
   free(mem);
   close(fd);

+ 24 - 24
opencl/backprop/backprop_kernel.cl

@@ -1,14 +1,14 @@
 #define THREADS 256
-#define WIDTH 16  
-#define HEIGHT 16 
-#define ETA 0.3f       
-#define MOMENTUM 0.3f  
+#define WIDTH 16
+#define HEIGHT 16
+#define ETA 0.3f
+#define MOMENTUM 0.3f
 
 #ifndef _BACKPROP_CUDA_KERNEL_H_
 #define _BACKPROP_CUDA_KERNEL_H_
 #define WM(i, j)   weight_matrix[(j) + (i) * WIDTH]
 
-__kernel void 
+__kernel void
 bpnn_layerforward_ocl(__global float *input_cuda,
 	                  __global float *output_hidden_cuda,
 					  __global float *input_hidden_cuda,
@@ -16,41 +16,41 @@ bpnn_layerforward_ocl(__global float *input_cuda,
 					  __local float *input_node,
 					  __local float *weight_matrix,
 					  int in,
-					  int hid) 
+					  int hid)
 {
 
    int by = get_group_id(1);
    int tx = get_local_id(0);
    int ty = get_local_id(1);
 
-   int index =  ( hid + 1 ) * HEIGHT * by + ( hid + 1 ) * ty + tx + 1 + ( hid + 1 ) ;  
+   int index =  ( hid + 1 ) * HEIGHT * by + ( hid + 1 ) * ty + tx + 1 + ( hid + 1 ) ;
 
    int index_in = HEIGHT * by + ty + 1;
-   
+
 	if ( tx == 0 )
 		input_node[ty] = input_cuda[index_in] ;
 		barrier(CLK_LOCAL_MEM_FENCE);
 
 		weight_matrix[ty * WIDTH + tx] =  input_hidden_cuda[index];
 		barrier(CLK_LOCAL_MEM_FENCE);
-   
+
 		weight_matrix[ty * WIDTH + tx]= weight_matrix[ty * WIDTH + tx] * input_node[ty];
 		barrier(CLK_LOCAL_MEM_FENCE);
-   
+
 		for ( int i = 1 ; i <= HEIGHT ; i=i*2){
 	//for ( int i = 1 ; i <= 4 ; i++){
-      int power_two = i; 
+      int power_two = i;
 		//int power_two = 2 << (i - 1);
 
 	    if( ty % power_two == 0 )
 		  weight_matrix[ty * WIDTH + tx]= weight_matrix[ty * WIDTH + tx] + weight_matrix[(ty + power_two/2)* WIDTH + tx];
-		  
+
 		barrier(CLK_LOCAL_MEM_FENCE);
 
     }
-   
+
     input_hidden_cuda[index] =  weight_matrix[ty * WIDTH + tx];
-   
+
 	barrier(CLK_LOCAL_MEM_FENCE);
 
     if ( tx == 0 ) {
@@ -60,19 +60,19 @@ bpnn_layerforward_ocl(__global float *input_cuda,
 }
 
 
-__kernel void  bpnn_adjust_weights_ocl( __global float * delta,   
-										 int hid,         
-										__global float * ly,      
-										 int in,          
-										__global float * w,       
-										__global float * oldw)  									
+__kernel void  bpnn_adjust_weights_ocl( __global float * delta,
+										 int hid,
+										__global float * ly,
+										 int in,
+										__global float * w,
+										__global float * oldw)
 {
-   
+
    int by = get_group_id(1);
    int tx = get_local_id(0);
    int ty = get_local_id(1);
-	
-   int index =  ( hid + 1 ) * HEIGHT * by + ( hid + 1 ) * ty + tx + 1 + ( hid + 1 ) ;  
+
+   int index =  ( hid + 1 ) * HEIGHT * by + ( hid + 1 ) * ty + tx + 1 + ( hid + 1 ) ;
    int index_y = HEIGHT * by + ty + 1;
    int index_x = tx + 1;
 
@@ -87,4 +87,4 @@ __kernel void  bpnn_adjust_weights_ocl( __global float * delta,
    }
 
 }
-#endif 
+#endif

+ 202 - 202
opencl/backprop/backprop_ocl.cpp

@@ -7,9 +7,9 @@
 #include "backprop.h"
 
 #ifdef NV //NVIDIA
-	#include <oclUtils.h>
+  #include <oclUtils.h>
 #else
-	#include <CL/cl.h>
+  #include <CL/cl.h>
 #endif
 
 
@@ -24,56 +24,56 @@ static cl_int           num_devices;
 
 static int initialize(int platform_num, int device_num, int use_gpu)
 {
-	cl_int result;
-	size_t size;
-	// modify a bit the initialization phase to discover all the ocl platforms an not only the first one (needed to discover the cpu)
-	cl_uint platformCount;
-	cl_platform_id* platforms_ids;
-
-	// create OpenCL context
-	clGetPlatformIDs(0, NULL, &platformCount);
+  cl_int result;
+  size_t size;
+  // modify a bit the initialization phase to discover all the ocl platforms an not only the first one (needed to discover the cpu)
+  cl_uint platformCount;
+  cl_platform_id* platforms_ids;
+
+  // create OpenCL context
+  clGetPlatformIDs(0, NULL, &platformCount);
   platforms_ids = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount);
   clGetPlatformIDs(platformCount, platforms_ids, NULL);
 
-	if (clGetPlatformIDs(platformCount, platforms_ids, NULL) != CL_SUCCESS) { printf("ERROR: clGetPlatformIDs(1,*,0) failed\n"); return -1; }
-	cl_context_properties ctxprop[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platforms_ids[platform_num], 0};
-	device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
-	context = clCreateContextFromType( ctxprop, device_type, NULL, NULL, NULL );
-	if( !context ) { printf("ERROR: clCreateContextFromType(%s) failed\n", use_gpu ? "GPU" : "CPU"); return -1; }
-
-	// get the list of GPUs
-	result = clGetContextInfo( context, CL_CONTEXT_DEVICES, 0, NULL, &size );
-	num_devices = (int) (size / sizeof(cl_device_id));
-	printf("num_devices = %d\n", num_devices);
-
-	if( result != CL_SUCCESS || num_devices < 1 ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
-	device_list = new cl_device_id[num_devices];
-	//device_list = (cl_device_id *)malloc(sizeof(cl_device_id)*num_devices);
-	if( !device_list ) { printf("ERROR: new cl_device_id[] failed\n"); return -1; }
-	result = clGetContextInfo( context, CL_CONTEXT_DEVICES, size, device_list, NULL );
-	if( result != CL_SUCCESS ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
-
-	// create command queue for the first device
-	cmd_queue = clCreateCommandQueue( context, device_list[device_num], 0, NULL );
-	if( !cmd_queue ) { printf("ERROR: clCreateCommandQueue() failed\n"); return -1; }
-	return 0;
+  if (clGetPlatformIDs(platformCount, platforms_ids, NULL) != CL_SUCCESS) { printf("ERROR: clGetPlatformIDs(1,*,0) failed\n"); return -1; }
+  cl_context_properties ctxprop[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platforms_ids[platform_num], 0};
+  device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
+  context = clCreateContextFromType( ctxprop, device_type, NULL, NULL, NULL );
+  if( !context ) { printf("ERROR: clCreateContextFromType(%s) failed\n", use_gpu ? "GPU" : "CPU"); return -1; }
+
+  // get the list of GPUs
+  result = clGetContextInfo( context, CL_CONTEXT_DEVICES, 0, NULL, &size );
+  num_devices = (int) (size / sizeof(cl_device_id));
+  printf("num_devices = %d\n", num_devices);
+
+  if( result != CL_SUCCESS || num_devices < 1 ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
+  device_list = new cl_device_id[num_devices];
+  //device_list = (cl_device_id *)malloc(sizeof(cl_device_id)*num_devices);
+  if( !device_list ) { printf("ERROR: new cl_device_id[] failed\n"); return -1; }
+  result = clGetContextInfo( context, CL_CONTEXT_DEVICES, size, device_list, NULL );
+  if( result != CL_SUCCESS ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
+
+  // create command queue for the first device
+  cmd_queue = clCreateCommandQueue( context, device_list[device_num], 0, NULL );
+  if( !cmd_queue ) { printf("ERROR: clCreateCommandQueue() failed\n"); return -1; }
+  return 0;
 }
 
 static int shutdown()
 {
-	// release resources
-	if( cmd_queue ) clReleaseCommandQueue( cmd_queue );
-	if( context ) clReleaseContext( context );
-	if( device_list ) delete[] device_list;
-
-	// reset all variables
-	cmd_queue = 0;
-	context = 0;
-	device_list = 0;
-	num_devices = 0;
-	device_type = 0;
-
-	return 0;
+  // release resources
+  if( cmd_queue ) clReleaseCommandQueue( cmd_queue );
+  if( context ) clReleaseContext( context );
+  if( device_list ) delete[] device_list;
+
+  // reset all variables
+  cmd_queue = 0;
+  context = 0;
+  device_list = 0;
+  num_devices = 0;
+  device_type = 0;
+
+  return 0;
 }
 
 double gettime() {
@@ -91,170 +91,170 @@ unsigned int num_blocks = 0;
 int
 main( int argc, char** argv)
 {
-	setup(argc, argv);
+  setup(argc, argv);
 }
 
 
 
 int bpnn_train_kernel(BPNN *net, float *eo, float *eh, int platform_num, int device_num, int use_gpu)
 {
-	int in, hid, out;
-	float out_err, hid_err;
-
-	in = net->input_n;
-	hid = net->hidden_n;
-	out = net->output_n;
-
-	int sourcesize = 1024*1024;
-	char * source = (char *)calloc(sourcesize, sizeof(char));
-	if(!source) { printf("ERROR: calloc(%d) failed\n", sourcesize); return -1; }
-
-	// read the kernel core source
-	char * kernel_bp1  = "bpnn_layerforward_ocl";
-	char * kernel_bp2  = "bpnn_adjust_weights_ocl";
-	char * tempchar = "./backprop_kernel.cl";
-	FILE * fp = fopen(tempchar, "rb");
-	if(!fp) { printf("ERROR: unable to open '%s'\n", tempchar); return -1; }
-	fread(source + strlen(source), sourcesize, 1, fp);
-	fclose(fp);
-
-	if(initialize(platform_num, device_num, use_gpu)) return -1;
-
-	// compile kernel
-	cl_int err = 0;
-	const char * slist[2] = { source, 0 };
-	cl_program prog = clCreateProgramWithSource(context, 1, slist, NULL, &err);
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateProgramWithSource() => %d\n", err); return -1; }
-	err = clBuildProgram(prog, 0, NULL, NULL, NULL, NULL);
-	{ // show warnings/errors
-		//static char log[65536]; memset(log, 0, sizeof(log));
-		//cl_device_id device_id = 0;
-		//err = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(device_id), &device_id, NULL);
-		//clGetProgramBuildInfo(prog, device_id, CL_PROGRAM_BUILD_LOG, sizeof(log)-1, log, NULL);
-		//if(err || strstr(log,"warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);
-	}
-	if(err != CL_SUCCESS) { printf("ERROR: clBuildProgram() => %d\n", err); return -1; }
-
-	cl_kernel kernel1;
-	cl_kernel kernel2;
-	kernel1 = clCreateKernel(prog, kernel_bp1, &err);
-	kernel2 = clCreateKernel(prog, kernel_bp2, &err);
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateKernel() 0 => %d\n", err); return -1; }
-	clReleaseProgram(prog);
-
-	float *input_weights_one_dim;
+  int in, hid, out;
+  float out_err, hid_err;
+
+  in = net->input_n;
+  hid = net->hidden_n;
+  out = net->output_n;
+
+  int sourcesize = 1024*1024;
+  char * source = (char *)calloc(sourcesize, sizeof(char));
+  if(!source) { printf("ERROR: calloc(%d) failed\n", sourcesize); return -1; }
+
+  // read the kernel core source
+  char * kernel_bp1  = "bpnn_layerforward_ocl";
+  char * kernel_bp2  = "bpnn_adjust_weights_ocl";
+  char * tempchar = "./backprop_kernel.cl";
+  FILE * fp = fopen(tempchar, "rb");
+  if(!fp) { printf("ERROR: unable to open '%s'\n", tempchar); return -1; }
+  fread(source + strlen(source), sourcesize, 1, fp);
+  fclose(fp);
+
+  if(initialize(platform_num, device_num, use_gpu)) return -1;
+
+  // compile kernel
+  cl_int err = 0;
+  const char * slist[2] = { source, 0 };
+  cl_program prog = clCreateProgramWithSource(context, 1, slist, NULL, &err);
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateProgramWithSource() => %d\n", err); return -1; }
+  err = clBuildProgram(prog, 0, NULL, NULL, NULL, NULL);
+  { // show warnings/errors
+  //static char log[65536]; memset(log, 0, sizeof(log));
+  //cl_device_id device_id = 0;
+  //err = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(device_id), &device_id, NULL);
+  //clGetProgramBuildInfo(prog, device_id, CL_PROGRAM_BUILD_LOG, sizeof(log)-1, log, NULL);
+  //if(err || strstr(log,"warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);
+  }
+  if(err != CL_SUCCESS) { printf("ERROR: clBuildProgram() => %d\n", err); return -1; }
+
+  cl_kernel kernel1;
+  cl_kernel kernel2;
+  kernel1 = clCreateKernel(prog, kernel_bp1, &err);
+  kernel2 = clCreateKernel(prog, kernel_bp2, &err);
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateKernel() 0 => %d\n", err); return -1; }
+  clReleaseProgram(prog);
+
+  float *input_weights_one_dim;
     float *input_weights_prev_one_dim;
-	float * partial_sum;
-	float sum;
-	float num_blocks = in / BLOCK_SIZE;
-
-	input_weights_one_dim = (float *) malloc((in + 1)* (hid + 1) * sizeof(float));
-	input_weights_prev_one_dim = (float *) malloc((in + 1)* (hid + 1) * sizeof(float));
-	partial_sum = (float *) malloc(num_blocks * WIDTH * sizeof(float));
-
-	// set global and local workitems
-	size_t global_work[3] = { BLOCK_SIZE, BLOCK_SIZE * num_blocks, 1 };
-	size_t local_work[3] = { BLOCK_SIZE, BLOCK_SIZE, 1 };
-
-	// this preprocessing stage is temporarily added to correct the bug of wrong memcopy using two-dimensional net->inputweights
-	// todo: fix mem allocation
-	int m = 0;
-	for (int k = 0; k <= in; k++) {
-		for (int j = 0; j <= hid; j++) {
-		input_weights_one_dim[m] = net->input_weights[k][j];
-		input_weights_prev_one_dim[m] = net-> input_prev_weights[k][j];
-	    m++;
-		}
-	}
-
-	cl_mem input_hidden_ocl;
-	cl_mem input_ocl;
-	cl_mem output_hidden_ocl;
-	cl_mem hidden_partial_sum;
-	cl_mem hidden_delta_ocl;
-	cl_mem input_prev_weights_ocl;
-
-	input_ocl = clCreateBuffer(context, CL_MEM_READ_WRITE, (in + 1) * sizeof(float), NULL, &err );
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer input_ocl\n"); return -1;}
-	input_hidden_ocl = clCreateBuffer(context, CL_MEM_READ_WRITE, (in + 1) * (hid + 1) * sizeof(float), NULL, &err );
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer input_hidden_ocl\n"); return -1;}
-	output_hidden_ocl = clCreateBuffer(context, CL_MEM_READ_WRITE, (hid + 1) * sizeof(float), NULL, &err );
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer output_hidden_ocl\n"); return -1;}
-	hidden_partial_sum = clCreateBuffer(context, CL_MEM_READ_WRITE, num_blocks * WIDTH * sizeof(float), NULL, &err );
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer hidden_partial_sum\n"); return -1;}
-	hidden_delta_ocl = clCreateBuffer(context, CL_MEM_READ_WRITE, (hid + 1) * sizeof(float), NULL, &err );
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer hidden_delta_ocl\n"); return -1;}
-	input_prev_weights_ocl = clCreateBuffer(context, CL_MEM_READ_WRITE, (in + 1) * (hid + 1) * sizeof(float), NULL, &err );
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer input_prev_weights_ocl\n"); return -1;}
-
-	printf("Performing GPU computation\n");
-
-	//write buffers
-	err = clEnqueueWriteBuffer(cmd_queue, input_ocl, 1, 0, (in + 1) * sizeof(float), net->input_units, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer input_ocl\n"); return -1; }
-	err = clEnqueueWriteBuffer(cmd_queue, input_hidden_ocl, 1, 0, (in + 1) * (hid + 1) * sizeof(float), input_weights_one_dim, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer input_hidden_ocl\n"); return -1; }
-
-	clSetKernelArg(kernel1, 0, sizeof(void *), (void*) &input_ocl);
-	clSetKernelArg(kernel1, 1, sizeof(void *), (void*) &output_hidden_ocl);
-	clSetKernelArg(kernel1, 2, sizeof(void *), (void*) &input_hidden_ocl);
-	clSetKernelArg(kernel1, 3, sizeof(void *), (void*) &hidden_partial_sum );
-	clSetKernelArg(kernel1, 4, sizeof(float) *  HEIGHT, (void*)NULL );
-	clSetKernelArg(kernel1, 5, sizeof(float ) *  HEIGHT * WIDTH, (void*)NULL );
-	clSetKernelArg(kernel1, 6, sizeof(cl_int), (void*) &in);
-	clSetKernelArg(kernel1, 7, sizeof(cl_int), (void*) &hid);
-
-	err = clEnqueueNDRangeKernel(cmd_queue, kernel1, 2, NULL, global_work, local_work, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: 1  clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }
-
-	err = clEnqueueReadBuffer(cmd_queue, hidden_partial_sum, 1, 0, num_blocks * WIDTH * sizeof(float), partial_sum, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: 1  clEnqueueReadBuffer: partial sum\n"); return -1; }
-
-	for (int j = 1; j <= hid; j++) {
-		sum = 0.0;
-		for (int k = 0; k < num_blocks; k++) {
-		sum += partial_sum[k * hid + j-1] ;
+  float * partial_sum;
+  float sum;
+  float num_blocks = in / BLOCK_SIZE;
+
+  input_weights_one_dim = (float *) malloc((in + 1)* (hid + 1) * sizeof(float));
+  input_weights_prev_one_dim = (float *) malloc((in + 1)* (hid + 1) * sizeof(float));
+  partial_sum = (float *) malloc(num_blocks * WIDTH * sizeof(float));
+
+  // set global and local workitems
+  size_t global_work[3] = { BLOCK_SIZE, BLOCK_SIZE * num_blocks, 1 };
+  size_t local_work[3] = { BLOCK_SIZE, BLOCK_SIZE, 1 };
+
+  // this preprocessing stage is temporarily added to correct the bug of wrong memcopy using two-dimensional net->inputweights
+  // todo: fix mem allocation
+  int m = 0;
+  for (int k = 0; k <= in; k++) {
+  for (int j = 0; j <= hid; j++) {
+  input_weights_one_dim[m] = net->input_weights[k][j];
+  input_weights_prev_one_dim[m] = net-> input_prev_weights[k][j];
+      m++;
+  }
+  }
+
+  cl_mem input_hidden_ocl;
+  cl_mem input_ocl;
+  cl_mem output_hidden_ocl;
+  cl_mem hidden_partial_sum;
+  cl_mem hidden_delta_ocl;
+  cl_mem input_prev_weights_ocl;
+
+  input_ocl = clCreateBuffer(context, CL_MEM_READ_WRITE, (in + 1) * sizeof(float), NULL, &err );
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer input_ocl\n"); return -1;}
+  input_hidden_ocl = clCreateBuffer(context, CL_MEM_READ_WRITE, (in + 1) * (hid + 1) * sizeof(float), NULL, &err );
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer input_hidden_ocl\n"); return -1;}
+  output_hidden_ocl = clCreateBuffer(context, CL_MEM_READ_WRITE, (hid + 1) * sizeof(float), NULL, &err );
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer output_hidden_ocl\n"); return -1;}
+  hidden_partial_sum = clCreateBuffer(context, CL_MEM_READ_WRITE, num_blocks * WIDTH * sizeof(float), NULL, &err );
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer hidden_partial_sum\n"); return -1;}
+  hidden_delta_ocl = clCreateBuffer(context, CL_MEM_READ_WRITE, (hid + 1) * sizeof(float), NULL, &err );
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer hidden_delta_ocl\n"); return -1;}
+  input_prev_weights_ocl = clCreateBuffer(context, CL_MEM_READ_WRITE, (in + 1) * (hid + 1) * sizeof(float), NULL, &err );
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer input_prev_weights_ocl\n"); return -1;}
+
+  printf("Performing GPU computation\n");
+
+  //write buffers
+  err = clEnqueueWriteBuffer(cmd_queue, input_ocl, 1, 0, (in + 1) * sizeof(float), net->input_units, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer input_ocl\n"); return -1; }
+  err = clEnqueueWriteBuffer(cmd_queue, input_hidden_ocl, 1, 0, (in + 1) * (hid + 1) * sizeof(float), input_weights_one_dim, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer input_hidden_ocl\n"); return -1; }
+
+  clSetKernelArg(kernel1, 0, sizeof(void *), (void*) &input_ocl);
+  clSetKernelArg(kernel1, 1, sizeof(void *), (void*) &output_hidden_ocl);
+  clSetKernelArg(kernel1, 2, sizeof(void *), (void*) &input_hidden_ocl);
+  clSetKernelArg(kernel1, 3, sizeof(void *), (void*) &hidden_partial_sum );
+  clSetKernelArg(kernel1, 4, sizeof(float) *  HEIGHT, (void*)NULL );
+  clSetKernelArg(kernel1, 5, sizeof(float ) *  HEIGHT * WIDTH, (void*)NULL );
+  clSetKernelArg(kernel1, 6, sizeof(cl_int), (void*) &in);
+  clSetKernelArg(kernel1, 7, sizeof(cl_int), (void*) &hid);
+
+  err = clEnqueueNDRangeKernel(cmd_queue, kernel1, 2, NULL, global_work, local_work, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: 1  clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }
+
+  err = clEnqueueReadBuffer(cmd_queue, hidden_partial_sum, 1, 0, num_blocks * WIDTH * sizeof(float), partial_sum, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: 1  clEnqueueReadBuffer: partial sum\n"); return -1; }
+
+  for (int j = 1; j <= hid; j++) {
+  sum = 0.0;
+  for (int k = 0; k < num_blocks; k++) {
+  sum += partial_sum[k * hid + j-1] ;
     }
-		sum += net->input_weights[0][j];
-		net-> hidden_units[j] = float(1.0 / (1.0 + exp(-sum)));
-	}
-
-
-	bpnn_layerforward(net->hidden_units, net->output_units, net->hidden_weights, hid, out);
-	bpnn_output_error(net->output_delta, net->target, net->output_units, out, &out_err);
-	bpnn_hidden_error(net->hidden_delta, hid, net->output_delta, out, net->hidden_weights, net->hidden_units, &hid_err);
-	bpnn_adjust_weights(net->output_delta, out, net->hidden_units, hid, net->hidden_weights, net->hidden_prev_weights);
-
-	err = clEnqueueWriteBuffer(cmd_queue, hidden_delta_ocl,       1, 0, (hid + 1) * sizeof(float), net->hidden_delta, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer hidden_delta_ocl\n"); return -1; }
-	err = clEnqueueWriteBuffer(cmd_queue, input_prev_weights_ocl, 1, 0, (in + 1) * (hid + 1) * sizeof(float), input_weights_prev_one_dim, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer input_prev_weights_ocl\n"); return -1; }
-	err = clEnqueueWriteBuffer(cmd_queue, input_hidden_ocl,       1, 0, (in + 1) * (hid + 1) * sizeof(float), input_weights_one_dim, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer input_hidden_ocl\n"); return -1; }
-
-	clSetKernelArg(kernel2, 0, sizeof(void *), (void*) &hidden_delta_ocl);
-	clSetKernelArg(kernel2, 1, sizeof(cl_int), (void*) &hid);
-	clSetKernelArg(kernel2, 2, sizeof(void *), (void*) &input_ocl);
-	clSetKernelArg(kernel2, 3, sizeof(cl_int), (void*) &in);
-	clSetKernelArg(kernel2, 4, sizeof(void *), (void*) &input_hidden_ocl);
-	clSetKernelArg(kernel2, 5, sizeof(void *), (void*) &input_prev_weights_ocl );
-
-	err = clEnqueueNDRangeKernel(cmd_queue, kernel2, 2, NULL, global_work, local_work, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: 1  clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }
-
-	err = clEnqueueReadBuffer(cmd_queue, input_ocl, 1, 0, (in + 1) * sizeof(float), net->input_units, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: 1  clEnqueueReadBuffer: input_ocl\n"); return -1; }
-	err = clEnqueueReadBuffer(cmd_queue, input_hidden_ocl, 1, 0, (in + 1) * (hid + 1) * sizeof(float), input_weights_one_dim, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: 1  clEnqueueReadBuffer: input_hidden_ocl\n"); return -1; }
-
-	clReleaseMemObject(input_ocl);
-	clReleaseMemObject(output_hidden_ocl);
-	clReleaseMemObject(input_hidden_ocl);
-	clReleaseMemObject(hidden_partial_sum);
-	clReleaseMemObject(input_prev_weights_ocl);
-
-	free(input_weights_prev_one_dim);
-	free(partial_sum);
-	free(input_weights_one_dim);
+  sum += net->input_weights[0][j];
+  net-> hidden_units[j] = float(1.0 / (1.0 + exp(-sum)));
+  }
+
+
+  bpnn_layerforward(net->hidden_units, net->output_units, net->hidden_weights, hid, out);
+  bpnn_output_error(net->output_delta, net->target, net->output_units, out, &out_err);
+  bpnn_hidden_error(net->hidden_delta, hid, net->output_delta, out, net->hidden_weights, net->hidden_units, &hid_err);
+  bpnn_adjust_weights(net->output_delta, out, net->hidden_units, hid, net->hidden_weights, net->hidden_prev_weights);
+
+  err = clEnqueueWriteBuffer(cmd_queue, hidden_delta_ocl,       1, 0, (hid + 1) * sizeof(float), net->hidden_delta, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer hidden_delta_ocl\n"); return -1; }
+  err = clEnqueueWriteBuffer(cmd_queue, input_prev_weights_ocl, 1, 0, (in + 1) * (hid + 1) * sizeof(float), input_weights_prev_one_dim, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer input_prev_weights_ocl\n"); return -1; }
+  err = clEnqueueWriteBuffer(cmd_queue, input_hidden_ocl,       1, 0, (in + 1) * (hid + 1) * sizeof(float), input_weights_one_dim, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer input_hidden_ocl\n"); return -1; }
+
+  clSetKernelArg(kernel2, 0, sizeof(void *), (void*) &hidden_delta_ocl);
+  clSetKernelArg(kernel2, 1, sizeof(cl_int), (void*) &hid);
+  clSetKernelArg(kernel2, 2, sizeof(void *), (void*) &input_ocl);
+  clSetKernelArg(kernel2, 3, sizeof(cl_int), (void*) &in);
+  clSetKernelArg(kernel2, 4, sizeof(void *), (void*) &input_hidden_ocl);
+  clSetKernelArg(kernel2, 5, sizeof(void *), (void*) &input_prev_weights_ocl );
+
+  err = clEnqueueNDRangeKernel(cmd_queue, kernel2, 2, NULL, global_work, local_work, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: 1  clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }
+
+  err = clEnqueueReadBuffer(cmd_queue, input_ocl, 1, 0, (in + 1) * sizeof(float), net->input_units, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: 1  clEnqueueReadBuffer: input_ocl\n"); return -1; }
+  err = clEnqueueReadBuffer(cmd_queue, input_hidden_ocl, 1, 0, (in + 1) * (hid + 1) * sizeof(float), input_weights_one_dim, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: 1  clEnqueueReadBuffer: input_hidden_ocl\n"); return -1; }
+
+  clReleaseMemObject(input_ocl);
+  clReleaseMemObject(output_hidden_ocl);
+  clReleaseMemObject(input_hidden_ocl);
+  clReleaseMemObject(hidden_partial_sum);
+  clReleaseMemObject(input_prev_weights_ocl);
+
+  free(input_weights_prev_one_dim);
+  free(partial_sum);
+  free(input_weights_one_dim);
 
 }

+ 3 - 3
opencl/backprop/imagenet.c

@@ -12,13 +12,13 @@ void load(BPNN *net)
   int nr, nc, imgsize, i, j, k;
 
   nr = layer_size;
-  
+
   imgsize = nr * nc;
   units = net->input_units;
 
   k = 1;
   for (i = 0; i < nr; i++) {
-	  units[k] = (float) rand()/RAND_MAX ;
-	  k++;
+    units[k] = (float) rand()/RAND_MAX ;
+    k++;
     }
 }

+ 388 - 388
opencl/bfs/CLHelper.h

@@ -91,36 +91,36 @@ string FileToString(const string fileName)
 //Read command line parameters
 //
 void _clCmdParams(int argc, char* argv[]){
-	for (int i =0; i < argc; ++i)
-	{
-		switch (argv[i][1])
-		{
-		case 'g':	//--g stands for size of work group
-			if (++i < argc)
-			{
-				sscanf(argv[i], "%u", &work_group_size);
-			}
-			else
-			{
-				std::cerr << "Could not read argument after option " << argv[i-1] << std::endl;
-				throw;
-			}
-			break;
-		  case 'd':	 //--d stands for device id used in computaion
-			if (++i < argc)
-			{
-				sscanf(argv[i], "%u", &device_id_inused);
-			}
-			else
-			{
-				std::cerr << "Could not read argument after option " << argv[i-1] << std::endl;
-				throw;
-			}
-			break;
-		default:
-			;
-		}
-	}
+  for (int i =0; i < argc; ++i)
+  {
+  switch (argv[i][1])
+  {
+  case 'g':	//--g stands for size of work group
+  if (++i < argc)
+  {
+  sscanf(argv[i], "%u", &work_group_size);
+  }
+  else
+  {
+  std::cerr << "Could not read argument after option " << argv[i-1] << std::endl;
+  throw;
+  }
+  break;
+    case 'd':	 //--d stands for device id used in computaion
+  if (++i < argc)
+  {
+  sscanf(argv[i], "%u", &device_id_inused);
+  }
+  else
+  {
+  std::cerr << "Could not read argument after option " << argv[i-1] << std::endl;
+  throw;
+  }
+  break;
+  default:
+  ;
+  }
+  }
 
 }
 
@@ -174,7 +174,7 @@ void _clInit(int platform_num, int device_num, int use_gpu)
         if (resultCL != CL_SUCCESS)
             throw (string("InitCL()::Error: Getting platform info (clGetPlatformInfo)"));
 
-		printf("vedor is %s\n",pbuff);
+  printf("vedor is %s\n",pbuff);
 
     }
     free(allPlatforms);
@@ -213,7 +213,7 @@ void _clInit(int platform_num, int device_num, int use_gpu)
 
     /* Next, get the device list data */
    oclHandles.cl_status = clGetDeviceIDs(targetPlatform, device_type, deviceListSize, \
-								oclHandles.devices, NULL);
+  oclHandles.devices, NULL);
    if(oclHandles.cl_status!=CL_SUCCESS){
    	throw(string("exception in _clInit -> clGetDeviceIDs-2"));
    }
@@ -249,7 +249,7 @@ void _clInit(int platform_num, int device_num, int use_gpu)
     {
         cerr << "InitCL()::Error: In clBuildProgram" << endl;
 
-		size_t length;
+  size_t length;
         resultCL = clGetProgramBuildInfo(oclHandles.program,
                                         oclHandles.devices[device_num],
                                         CL_PROGRAM_BUILD_LOG,
@@ -259,7 +259,7 @@ void _clInit(int platform_num, int device_num, int use_gpu)
         if(resultCL != CL_SUCCESS)
             throw(string("InitCL()::Error: Getting Program build info(clGetProgramBuildInfo)"));
 
-		char* buffer = (char*)malloc(length);
+  char* buffer = (char*)malloc(length);
         resultCL = clGetProgramBuildInfo(oclHandles.program,
                                         oclHandles.devices[device_num],
                                         CL_PROGRAM_BUILD_LOG,
@@ -269,7 +269,7 @@ void _clInit(int platform_num, int device_num, int use_gpu)
         if(resultCL != CL_SUCCESS)
             throw(string("InitCL()::Error: Getting Program build info(clGetProgramBuildInfo)"));
 
-		cerr << buffer << endl;
+  cerr << buffer << endl;
         free(buffer);
 
         throw(string("InitCL()::Error: Building Program (clBuildProgram)"));
@@ -288,7 +288,7 @@ void _clInit(int platform_num, int device_num, int use_gpu)
     std::cout<<"--cambine:"<<binary_sizes<<std::endl;
     //copy over all of the generated binaries.
     for(int i=0;i<deviceListSize;i++)
-	binaries[i] = (char *)malloc( sizeof(char)*(binary_sizes[i]+1));
+  binaries[i] = (char *)malloc( sizeof(char)*(binary_sizes[i]+1));
     oclHandles.cl_status = clGetProgramInfo(oclHandles.program, CL_PROGRAM_BINARIES, sizeof(char *)*deviceListSize, binaries, NULL );
     if(oclHandles.cl_status!=CL_SUCCESS){
         throw(string("--cambine:exception in _InitCL -> clGetProgramInfo-3"));
@@ -298,13 +298,13 @@ void _clInit(int platform_num, int device_num, int use_gpu)
     std::cout<<"--cambine:writing ptd information..."<<std::endl;
     FILE * ptx_file = fopen("cl.ptx","w");
     if(ptx_file==NULL){
-	throw(string("exceptions in allocate ptx file."));
+  throw(string("exceptions in allocate ptx file."));
     }
     fprintf(ptx_file,"%s",binaries[DEVICE_ID_INUSED]);
     fclose(ptx_file);
     std::cout<<"--cambine:writing ptd information done."<<std::endl;
     for(int i=0;i<deviceListSize;i++)
-	free(binaries[i]);
+  free(binaries[i]);
     #endif
 
     for (int nKernel = 0; nKernel < total_kernels; nKernel++)
@@ -328,13 +328,13 @@ void _clInit(int platform_num, int device_num, int use_gpu)
     size_t ret_val_size;
     oclHandles.cl_status = clGetProgramBuildInfo(oclHandles.program, oclHandles.devices[DEVICE_ID_INUSED], CL_PROGRAM_BUILD_LOG, 0, NULL, &ret_val_size);
     if(oclHandles.cl_status!=CL_SUCCESS){
-	throw(string("exceptions in _InitCL -> getting resource information"));
+  throw(string("exceptions in _InitCL -> getting resource information"));
     }
 
     build_log = (char *)malloc(ret_val_size+1);
     oclHandles.cl_status = clGetProgramBuildInfo(oclHandles.program, oclHandles.devices[DEVICE_ID_INUSED], CL_PROGRAM_BUILD_LOG, ret_val_size, build_log, NULL);
     if(oclHandles.cl_status!=CL_SUCCESS){
-	throw(string("exceptions in _InitCL -> getting resources allocation information-2"));
+  throw(string("exceptions in _InitCL -> getting resources allocation information-2"));
     }
     build_log[ret_val_size] = '\0';
     std::cout<<"--cambine:"<<build_log<<std::endl;
@@ -403,415 +403,415 @@ void _clRelease()
 //--------------------------------------------------------
 //--cambine:create buffer and then copy data from host to device
 cl_mem _clCreateAndCpyMem(int size, void * h_mem_source) throw(string){
-	cl_mem d_mem;
-	d_mem = clCreateBuffer(oclHandles.context,	CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR,  \
-									size, h_mem_source, &oclHandles.cl_status);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clCreateAndCpyMem()"));
-	#endif
-	return d_mem;
+  cl_mem d_mem;
+  d_mem = clCreateBuffer(oclHandles.context,	CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR,  \
+  size, h_mem_source, &oclHandles.cl_status);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clCreateAndCpyMem()"));
+  #endif
+  return d_mem;
 }
 //-------------------------------------------------------
 //--cambine:	create read only  buffer for devices
 //--date:	17/01/2011
 cl_mem _clMallocRW(int size, void * h_mem_ptr) throw(string){
  	cl_mem d_mem;
-	d_mem = clCreateBuffer(oclHandles.context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, size, h_mem_ptr, &oclHandles.cl_status);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clMallocRW"));
-	#endif
-	return d_mem;
+  d_mem = clCreateBuffer(oclHandles.context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, size, h_mem_ptr, &oclHandles.cl_status);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clMallocRW"));
+  #endif
+  return d_mem;
 }
 //-------------------------------------------------------
 //--cambine:	create read and write buffer for devices
 //--date:	17/01/2011
 cl_mem _clMalloc(int size, void * h_mem_ptr) throw(string){
  	cl_mem d_mem;
-	d_mem = clCreateBuffer(oclHandles.context, CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR, size, h_mem_ptr, &oclHandles.cl_status);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clMalloc"));
-	#endif
-	return d_mem;
+  d_mem = clCreateBuffer(oclHandles.context, CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR, size, h_mem_ptr, &oclHandles.cl_status);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clMalloc"));
+  #endif
+  return d_mem;
 }
 
 //-------------------------------------------------------
 //--cambine:	transfer data from host to device
 //--date:	17/01/2011
 void _clMemcpyH2D(cl_mem d_mem, int size, const void *h_mem_ptr) throw(string){
-	oclHandles.cl_status = clEnqueueWriteBuffer(oclHandles.queue, d_mem, CL_TRUE, 0, size, h_mem_ptr, 0, NULL, NULL);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clMemcpyH2D"));
-	#endif
+  oclHandles.cl_status = clEnqueueWriteBuffer(oclHandles.queue, d_mem, CL_TRUE, 0, size, h_mem_ptr, 0, NULL, NULL);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clMemcpyH2D"));
+  #endif
 }
 //--------------------------------------------------------
 //--cambine:create buffer and then copy data from host to device with pinned
 // memory
 cl_mem _clCreateAndCpyPinnedMem(int size, float* h_mem_source) throw(string){
-	cl_mem d_mem, d_mem_pinned;
-	float * h_mem_pinned = NULL;
-	d_mem_pinned = clCreateBuffer(oclHandles.context,	CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR,  \
-									size, NULL, &oclHandles.cl_status);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clCreateAndCpyMem()->d_mem_pinned"));
-	#endif
-	//------------
-	d_mem = clCreateBuffer(oclHandles.context,	CL_MEM_READ_ONLY,  \
-									size, NULL, &oclHandles.cl_status);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clCreateAndCpyMem() -> d_mem "));
-	#endif
-	//----------
-	h_mem_pinned = (cl_float *)clEnqueueMapBuffer(oclHandles.queue, d_mem_pinned, CL_TRUE,  \
-										CL_MAP_WRITE, 0, size, 0, NULL,  \
-										NULL,  &oclHandles.cl_status);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clCreateAndCpyMem() -> clEnqueueMapBuffer"));
-	#endif
-	int element_number = size/sizeof(float);
-	#pragma omp parallel for
-	for(int i=0;i<element_number;i++){
-		h_mem_pinned[i] = h_mem_source[i];
-	}
-	//----------
-	oclHandles.cl_status = clEnqueueWriteBuffer(oclHandles.queue, d_mem, 	\
-									CL_TRUE, 0, size, h_mem_pinned,  \
-									0, NULL, NULL);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clCreateAndCpyMem() -> clEnqueueWriteBuffer"));
-	#endif
-
-	return d_mem;
+  cl_mem d_mem, d_mem_pinned;
+  float * h_mem_pinned = NULL;
+  d_mem_pinned = clCreateBuffer(oclHandles.context,	CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR,  \
+  size, NULL, &oclHandles.cl_status);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clCreateAndCpyMem()->d_mem_pinned"));
+  #endif
+  //------------
+  d_mem = clCreateBuffer(oclHandles.context,	CL_MEM_READ_ONLY,  \
+  size, NULL, &oclHandles.cl_status);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clCreateAndCpyMem() -> d_mem "));
+  #endif
+  //----------
+  h_mem_pinned = (cl_float *)clEnqueueMapBuffer(oclHandles.queue, d_mem_pinned, CL_TRUE,  \
+  CL_MAP_WRITE, 0, size, 0, NULL,  \
+  NULL,  &oclHandles.cl_status);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clCreateAndCpyMem() -> clEnqueueMapBuffer"));
+  #endif
+  int element_number = size/sizeof(float);
+  #pragma omp parallel for
+  for(int i=0;i<element_number;i++){
+  h_mem_pinned[i] = h_mem_source[i];
+  }
+  //----------
+  oclHandles.cl_status = clEnqueueWriteBuffer(oclHandles.queue, d_mem, 	\
+  CL_TRUE, 0, size, h_mem_pinned,  \
+  0, NULL, NULL);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clCreateAndCpyMem() -> clEnqueueWriteBuffer"));
+  #endif
+
+  return d_mem;
 }
 
 
 //--------------------------------------------------------
 //--cambine:create write only buffer on device
 cl_mem _clMallocWO(int size) throw(string){
-	cl_mem d_mem;
-	d_mem = clCreateBuffer(oclHandles.context, CL_MEM_WRITE_ONLY, size, 0, &oclHandles.cl_status);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clCreateMem()"));
-	#endif
-	return d_mem;
+  cl_mem d_mem;
+  d_mem = clCreateBuffer(oclHandles.context, CL_MEM_WRITE_ONLY, size, 0, &oclHandles.cl_status);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clCreateMem()"));
+  #endif
+  return d_mem;
 }
 
 //--------------------------------------------------------
 //transfer data from device to host
 void _clMemcpyD2H(cl_mem d_mem, int size, void * h_mem) throw(string){
-	oclHandles.cl_status = clEnqueueReadBuffer(oclHandles.queue, d_mem, CL_TRUE, 0, size, h_mem, 0,0,0);
-	#ifdef ERRMSG
-		oclHandles.error_str = "excpetion in _clCpyMemD2H -> ";
-		switch(oclHandles.cl_status){
-			case CL_INVALID_COMMAND_QUEUE:
-				oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-				break;
-			case CL_INVALID_CONTEXT:
-				oclHandles.error_str += "CL_INVALID_CONTEXT";
-				break;
-			case CL_INVALID_MEM_OBJECT:
-				oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
-				break;
-			case CL_INVALID_VALUE:
-				oclHandles.error_str += "CL_INVALID_VALUE";
-				break;
-			case CL_INVALID_EVENT_WAIT_LIST:
-				oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
-				break;
-			case CL_MEM_OBJECT_ALLOCATION_FAILURE:
-				oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
-				break;
-			case CL_OUT_OF_HOST_MEMORY:
-				oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-				break;
-			default:
-				oclHandles.error_str += "Unknown reason";
-				break;
-		}
-		if(oclHandles.cl_status != CL_SUCCESS)
-			throw(oclHandles.error_str);
-	#endif
+  oclHandles.cl_status = clEnqueueReadBuffer(oclHandles.queue, d_mem, CL_TRUE, 0, size, h_mem, 0,0,0);
+  #ifdef ERRMSG
+  oclHandles.error_str = "excpetion in _clCpyMemD2H -> ";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_COMMAND_QUEUE:
+  oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+  break;
+  case CL_INVALID_CONTEXT:
+  oclHandles.error_str += "CL_INVALID_CONTEXT";
+  break;
+  case CL_INVALID_MEM_OBJECT:
+  oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
+  break;
+  case CL_INVALID_VALUE:
+  oclHandles.error_str += "CL_INVALID_VALUE";
+  break;
+  case CL_INVALID_EVENT_WAIT_LIST:
+  oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
+  break;
+  case CL_MEM_OBJECT_ALLOCATION_FAILURE:
+  oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unknown reason";
+  break;
+  }
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(oclHandles.error_str);
+  #endif
 }
 
 //--------------------------------------------------------
 //set kernel arguments
 void _clSetArgs(int kernel_id, int arg_idx, void * d_mem, int size = 0) throw(string){
-	if(!size){
-		oclHandles.cl_status = clSetKernelArg(oclHandles.kernel[kernel_id], arg_idx, sizeof(d_mem), &d_mem);
-		#ifdef ERRMSG
-		oclHandles.error_str = "excpetion in _clSetKernelArg() ";
-		switch(oclHandles.cl_status){
-			case CL_INVALID_KERNEL:
-				oclHandles.error_str += "CL_INVALID_KERNEL";
-				break;
-			case CL_INVALID_ARG_INDEX:
-				oclHandles.error_str += "CL_INVALID_ARG_INDEX";
-				break;
-			case CL_INVALID_ARG_VALUE:
-				oclHandles.error_str += "CL_INVALID_ARG_VALUE";
-				break;
-			case CL_INVALID_MEM_OBJECT:
-				oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
-				break;
-			case CL_INVALID_SAMPLER:
-				oclHandles.error_str += "CL_INVALID_SAMPLER";
-				break;
-			case CL_INVALID_ARG_SIZE:
-				oclHandles.error_str += "CL_INVALID_ARG_SIZE";
-				break;
-			case CL_OUT_OF_RESOURCES:
-				oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-				break;
-			case CL_OUT_OF_HOST_MEMORY:
-				oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-				break;
-			default:
-				oclHandles.error_str += "Unknown reason";
-				break;
-		}
-		if(oclHandles.cl_status != CL_SUCCESS)
-			throw(oclHandles.error_str);
-		#endif
-	}
-	else{
-		oclHandles.cl_status = clSetKernelArg(oclHandles.kernel[kernel_id], arg_idx, size, d_mem);
-		#ifdef ERRMSG
-		oclHandles.error_str = "excpetion in _clSetKernelArg() ";
-		switch(oclHandles.cl_status){
-			case CL_INVALID_KERNEL:
-				oclHandles.error_str += "CL_INVALID_KERNEL";
-				break;
-			case CL_INVALID_ARG_INDEX:
-				oclHandles.error_str += "CL_INVALID_ARG_INDEX";
-				break;
-			case CL_INVALID_ARG_VALUE:
-				oclHandles.error_str += "CL_INVALID_ARG_VALUE";
-				break;
-			case CL_INVALID_MEM_OBJECT:
-				oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
-				break;
-			case CL_INVALID_SAMPLER:
-				oclHandles.error_str += "CL_INVALID_SAMPLER";
-				break;
-			case CL_INVALID_ARG_SIZE:
-				oclHandles.error_str += "CL_INVALID_ARG_SIZE";
-				break;
-			case CL_OUT_OF_RESOURCES:
-				oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-				break;
-			case CL_OUT_OF_HOST_MEMORY:
-				oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-				break;
-			default:
-				oclHandles.error_str += "Unknown reason";
-				break;
-		}
-		if(oclHandles.cl_status != CL_SUCCESS)
-			throw(oclHandles.error_str);
-		#endif
-	}
+  if(!size){
+  oclHandles.cl_status = clSetKernelArg(oclHandles.kernel[kernel_id], arg_idx, sizeof(d_mem), &d_mem);
+  #ifdef ERRMSG
+  oclHandles.error_str = "excpetion in _clSetKernelArg() ";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_KERNEL:
+  oclHandles.error_str += "CL_INVALID_KERNEL";
+  break;
+  case CL_INVALID_ARG_INDEX:
+  oclHandles.error_str += "CL_INVALID_ARG_INDEX";
+  break;
+  case CL_INVALID_ARG_VALUE:
+  oclHandles.error_str += "CL_INVALID_ARG_VALUE";
+  break;
+  case CL_INVALID_MEM_OBJECT:
+  oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
+  break;
+  case CL_INVALID_SAMPLER:
+  oclHandles.error_str += "CL_INVALID_SAMPLER";
+  break;
+  case CL_INVALID_ARG_SIZE:
+  oclHandles.error_str += "CL_INVALID_ARG_SIZE";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unknown reason";
+  break;
+  }
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(oclHandles.error_str);
+  #endif
+  }
+  else{
+  oclHandles.cl_status = clSetKernelArg(oclHandles.kernel[kernel_id], arg_idx, size, d_mem);
+  #ifdef ERRMSG
+  oclHandles.error_str = "excpetion in _clSetKernelArg() ";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_KERNEL:
+  oclHandles.error_str += "CL_INVALID_KERNEL";
+  break;
+  case CL_INVALID_ARG_INDEX:
+  oclHandles.error_str += "CL_INVALID_ARG_INDEX";
+  break;
+  case CL_INVALID_ARG_VALUE:
+  oclHandles.error_str += "CL_INVALID_ARG_VALUE";
+  break;
+  case CL_INVALID_MEM_OBJECT:
+  oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
+  break;
+  case CL_INVALID_SAMPLER:
+  oclHandles.error_str += "CL_INVALID_SAMPLER";
+  break;
+  case CL_INVALID_ARG_SIZE:
+  oclHandles.error_str += "CL_INVALID_ARG_SIZE";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unknown reason";
+  break;
+  }
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(oclHandles.error_str);
+  #endif
+  }
 }
 void _clFinish() throw(string){
-	oclHandles.cl_status = clFinish(oclHandles.queue);
-	#ifdef ERRMSG
-	oclHandles.error_str = "excpetion in _clFinish";
-	switch(oclHandles.cl_status){
-		case CL_INVALID_COMMAND_QUEUE:
-			oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-			break;
-		case CL_OUT_OF_RESOURCES:
-			oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-			break;
-		case CL_OUT_OF_HOST_MEMORY:
-			oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-			break;
-		default:
-			oclHandles.error_str += "Unknown reasons";
-			break;
-
-	}
-	if(oclHandles.cl_status!=CL_SUCCESS){
-		throw(oclHandles.error_str);
-	}
-	#endif
+  oclHandles.cl_status = clFinish(oclHandles.queue);
+  #ifdef ERRMSG
+  oclHandles.error_str = "excpetion in _clFinish";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_COMMAND_QUEUE:
+  oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unknown reasons";
+  break;
+
+  }
+  if(oclHandles.cl_status!=CL_SUCCESS){
+  throw(oclHandles.error_str);
+  }
+  #endif
 }
 //--------------------------------------------------------
 //--cambine:enqueue kernel
 void _clInvokeKernel(int kernel_id, int work_items, int work_group_size) throw(string){
-	cl_uint work_dim = WORK_DIM;
-	cl_event e[1];
-	if(work_items%work_group_size != 0)	//process situations that work_items cannot be divided by work_group_size
-	  work_items = work_items + (work_group_size-(work_items%work_group_size));
+  cl_uint work_dim = WORK_DIM;
+  cl_event e[1];
+  if(work_items%work_group_size != 0)	//process situations that work_items cannot be divided by work_group_size
+    work_items = work_items + (work_group_size-(work_items%work_group_size));
   	size_t local_work_size[] = {work_group_size, 1};
-	size_t global_work_size[] = {work_items, 1};
-	oclHandles.cl_status = clEnqueueNDRangeKernel(oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0, \
-											global_work_size, local_work_size, 0 , 0, &(e[0]) );
-	#ifdef ERRMSG
-	oclHandles.error_str = "excpetion in _clInvokeKernel() -> ";
-	switch(oclHandles.cl_status)
-	{
-		case CL_INVALID_PROGRAM_EXECUTABLE:
-			oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE";
-			break;
-		case CL_INVALID_COMMAND_QUEUE:
-			oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-			break;
-		case CL_INVALID_KERNEL:
-			oclHandles.error_str += "CL_INVALID_KERNEL";
-			break;
-		case CL_INVALID_CONTEXT:
-			oclHandles.error_str += "CL_INVALID_CONTEXT";
-			break;
-		case CL_INVALID_KERNEL_ARGS:
-			oclHandles.error_str += "CL_INVALID_KERNEL_ARGS";
-			break;
-		case CL_INVALID_WORK_DIMENSION:
-			oclHandles.error_str += "CL_INVALID_WORK_DIMENSION";
-			break;
-		case CL_INVALID_GLOBAL_WORK_SIZE:
-			oclHandles.error_str += "CL_INVALID_GLOBAL_WORK_SIZE";
-			break;
-		case CL_INVALID_WORK_GROUP_SIZE:
-			oclHandles.error_str += "CL_INVALID_WORK_GROUP_SIZE";
-			break;
-		case CL_INVALID_WORK_ITEM_SIZE:
-			oclHandles.error_str += "CL_INVALID_WORK_ITEM_SIZE";
-			break;
-		case CL_INVALID_GLOBAL_OFFSET:
-			oclHandles.error_str += "CL_INVALID_GLOBAL_OFFSET";
-			break;
-		case CL_OUT_OF_RESOURCES:
-			oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-			break;
-		case CL_MEM_OBJECT_ALLOCATION_FAILURE:
-			oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
-			break;
-		case CL_INVALID_EVENT_WAIT_LIST:
-			oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
-			break;
-		case CL_OUT_OF_HOST_MEMORY:
-			oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-			break;
-		default:
-			oclHandles.error_str += "Unkown reseason";
-			break;
-	}
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(oclHandles.error_str);
-	#endif
-	//_clFinish();
-	// oclHandles.cl_status = clWaitForEvents(1, &e[0]);
-	// #ifdef ERRMSG
+  size_t global_work_size[] = {work_items, 1};
+  oclHandles.cl_status = clEnqueueNDRangeKernel(oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0, \
+  global_work_size, local_work_size, 0 , 0, &(e[0]) );
+  #ifdef ERRMSG
+  oclHandles.error_str = "excpetion in _clInvokeKernel() -> ";
+  switch(oclHandles.cl_status)
+  {
+  case CL_INVALID_PROGRAM_EXECUTABLE:
+  oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE";
+  break;
+  case CL_INVALID_COMMAND_QUEUE:
+  oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+  break;
+  case CL_INVALID_KERNEL:
+  oclHandles.error_str += "CL_INVALID_KERNEL";
+  break;
+  case CL_INVALID_CONTEXT:
+  oclHandles.error_str += "CL_INVALID_CONTEXT";
+  break;
+  case CL_INVALID_KERNEL_ARGS:
+  oclHandles.error_str += "CL_INVALID_KERNEL_ARGS";
+  break;
+  case CL_INVALID_WORK_DIMENSION:
+  oclHandles.error_str += "CL_INVALID_WORK_DIMENSION";
+  break;
+  case CL_INVALID_GLOBAL_WORK_SIZE:
+  oclHandles.error_str += "CL_INVALID_GLOBAL_WORK_SIZE";
+  break;
+  case CL_INVALID_WORK_GROUP_SIZE:
+  oclHandles.error_str += "CL_INVALID_WORK_GROUP_SIZE";
+  break;
+  case CL_INVALID_WORK_ITEM_SIZE:
+  oclHandles.error_str += "CL_INVALID_WORK_ITEM_SIZE";
+  break;
+  case CL_INVALID_GLOBAL_OFFSET:
+  oclHandles.error_str += "CL_INVALID_GLOBAL_OFFSET";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_MEM_OBJECT_ALLOCATION_FAILURE:
+  oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
+  break;
+  case CL_INVALID_EVENT_WAIT_LIST:
+  oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unkown reseason";
+  break;
+  }
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(oclHandles.error_str);
+  #endif
+  //_clFinish();
+  // oclHandles.cl_status = clWaitForEvents(1, &e[0]);
+  // #ifdef ERRMSG
         // if (oclHandles.cl_status!= CL_SUCCESS)
         //     throw(string("excpetion in _clEnqueueNDRange() -> clWaitForEvents"));
-	// #endif
+  // #endif
 }
 void _clInvokeKernel2D(int kernel_id, int range_x, int range_y, int group_x, int group_y) throw(string){
-	cl_uint work_dim = WORK_DIM;
-	size_t local_work_size[] = {group_x, group_y};
-	size_t global_work_size[] = {range_x, range_y};
-	cl_event e[1];
-	/*if(work_items%work_group_size != 0)	//process situations that work_items cannot be divided by work_group_size
-	  work_items = work_items + (work_group_size-(work_items%work_group_size));*/
-	oclHandles.cl_status = clEnqueueNDRangeKernel(oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0, \
-											global_work_size, local_work_size, 0 , 0, &(e[0]) );
-	#ifdef ERRMSG
-	oclHandles.error_str = "excpetion in _clInvokeKernel() -> ";
-	switch(oclHandles.cl_status)
-	{
-		case CL_INVALID_PROGRAM_EXECUTABLE:
-			oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE";
-			break;
-		case CL_INVALID_COMMAND_QUEUE:
-			oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-			break;
-		case CL_INVALID_KERNEL:
-			oclHandles.error_str += "CL_INVALID_KERNEL";
-			break;
-		case CL_INVALID_CONTEXT:
-			oclHandles.error_str += "CL_INVALID_CONTEXT";
-			break;
-		case CL_INVALID_KERNEL_ARGS:
-			oclHandles.error_str += "CL_INVALID_KERNEL_ARGS";
-			break;
-		case CL_INVALID_WORK_DIMENSION:
-			oclHandles.error_str += "CL_INVALID_WORK_DIMENSION";
-			break;
-		case CL_INVALID_GLOBAL_WORK_SIZE:
-			oclHandles.error_str += "CL_INVALID_GLOBAL_WORK_SIZE";
-			break;
-		case CL_INVALID_WORK_GROUP_SIZE:
-			oclHandles.error_str += "CL_INVALID_WORK_GROUP_SIZE";
-			break;
-		case CL_INVALID_WORK_ITEM_SIZE:
-			oclHandles.error_str += "CL_INVALID_WORK_ITEM_SIZE";
-			break;
-		case CL_INVALID_GLOBAL_OFFSET:
-			oclHandles.error_str += "CL_INVALID_GLOBAL_OFFSET";
-			break;
-		case CL_OUT_OF_RESOURCES:
-			oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-			break;
-		case CL_MEM_OBJECT_ALLOCATION_FAILURE:
-			oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
-			break;
-		case CL_INVALID_EVENT_WAIT_LIST:
-			oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
-			break;
-		case CL_OUT_OF_HOST_MEMORY:
-			oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-			break;
-		default:
-			oclHandles.error_str += "Unkown reseason";
-			break;
-	}
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(oclHandles.error_str);
-	#endif
-	//_clFinish();
-	/*oclHandles.cl_status = clWaitForEvents(1, &e[0]);
-
-	#ifdef ERRMSG
+  cl_uint work_dim = WORK_DIM;
+  size_t local_work_size[] = {group_x, group_y};
+  size_t global_work_size[] = {range_x, range_y};
+  cl_event e[1];
+  /*if(work_items%work_group_size != 0)	//process situations that work_items cannot be divided by work_group_size
+    work_items = work_items + (work_group_size-(work_items%work_group_size));*/
+  oclHandles.cl_status = clEnqueueNDRangeKernel(oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0, \
+  global_work_size, local_work_size, 0 , 0, &(e[0]) );
+  #ifdef ERRMSG
+  oclHandles.error_str = "excpetion in _clInvokeKernel() -> ";
+  switch(oclHandles.cl_status)
+  {
+  case CL_INVALID_PROGRAM_EXECUTABLE:
+  oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE";
+  break;
+  case CL_INVALID_COMMAND_QUEUE:
+  oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+  break;
+  case CL_INVALID_KERNEL:
+  oclHandles.error_str += "CL_INVALID_KERNEL";
+  break;
+  case CL_INVALID_CONTEXT:
+  oclHandles.error_str += "CL_INVALID_CONTEXT";
+  break;
+  case CL_INVALID_KERNEL_ARGS:
+  oclHandles.error_str += "CL_INVALID_KERNEL_ARGS";
+  break;
+  case CL_INVALID_WORK_DIMENSION:
+  oclHandles.error_str += "CL_INVALID_WORK_DIMENSION";
+  break;
+  case CL_INVALID_GLOBAL_WORK_SIZE:
+  oclHandles.error_str += "CL_INVALID_GLOBAL_WORK_SIZE";
+  break;
+  case CL_INVALID_WORK_GROUP_SIZE:
+  oclHandles.error_str += "CL_INVALID_WORK_GROUP_SIZE";
+  break;
+  case CL_INVALID_WORK_ITEM_SIZE:
+  oclHandles.error_str += "CL_INVALID_WORK_ITEM_SIZE";
+  break;
+  case CL_INVALID_GLOBAL_OFFSET:
+  oclHandles.error_str += "CL_INVALID_GLOBAL_OFFSET";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_MEM_OBJECT_ALLOCATION_FAILURE:
+  oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
+  break;
+  case CL_INVALID_EVENT_WAIT_LIST:
+  oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unkown reseason";
+  break;
+  }
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(oclHandles.error_str);
+  #endif
+  //_clFinish();
+  /*oclHandles.cl_status = clWaitForEvents(1, &e[0]);
+
+  #ifdef ERRMSG
 
         if (oclHandles.cl_status!= CL_SUCCESS)
 
             throw(string("excpetion in _clEnqueueNDRange() -> clWaitForEvents"));
 
-	#endif*/
+  #endif*/
 }
 
 //--------------------------------------------------------
 //release OpenCL objects
 void _clFree(cl_mem ob) throw(string){
-	if(ob!=NULL)
-		oclHandles.cl_status = clReleaseMemObject(ob);
-	#ifdef ERRMSG
-	oclHandles.error_str = "excpetion in _clFree() ->";
-	switch(oclHandles.cl_status)
-	{
-		case CL_INVALID_MEM_OBJECT:
-			oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
-			break;
-		case CL_OUT_OF_RESOURCES:
-			oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-			break;
-		case CL_OUT_OF_HOST_MEMORY:
-			oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-			break;
-		default:
-			oclHandles.error_str += "Unkown reseason";
-			break;
-	}
+  if(ob!=NULL)
+  oclHandles.cl_status = clReleaseMemObject(ob);
+  #ifdef ERRMSG
+  oclHandles.error_str = "excpetion in _clFree() ->";
+  switch(oclHandles.cl_status)
+  {
+  case CL_INVALID_MEM_OBJECT:
+  oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unkown reseason";
+  break;
+  }
     if (oclHandles.cl_status!= CL_SUCCESS)
        throw(oclHandles.error_str);
-	#endif
+  #endif
 }
 #endif //_CL_HELPER_

+ 224 - 224
opencl/bfs/bfs.cpp

@@ -25,8 +25,8 @@ int use_gpu = 0;
 //Structure to hold a node information
 struct Node
 {
-	int starting;
-	int no_of_edges;
+  int starting;
+  int no_of_edges;
 };
 
 
@@ -37,146 +37,146 @@ struct Node
 //--note: width is changed to the new_width
 //----------------------------------------------------------
 void run_bfs_cpu(int no_of_nodes, Node *h_graph_nodes, int edge_list_size, \
-		int *h_graph_edges, char *h_graph_mask, char *h_updating_graph_mask, \
-		char *h_graph_visited, int *h_cost_ref){
-	char stop;
-	int k = 0;
-	do{
-		//if no thread changes this value then the loop stops
-		stop=false;
-		for(int tid = 0; tid < no_of_nodes; tid++ )
-		{
-			if (h_graph_mask[tid] == true){ 
-				h_graph_mask[tid]=false;
-				for(int i=h_graph_nodes[tid].starting; i<(h_graph_nodes[tid].no_of_edges + h_graph_nodes[tid].starting); i++){
-					int id = h_graph_edges[i];	//--cambine: node id is connected with node tid
-					if(!h_graph_visited[id]){	//--cambine: if node id has not been visited, enter the body below
-						h_cost_ref[id]=h_cost_ref[tid]+1;
-						h_updating_graph_mask[id]=true;
-					}
-				}
-			}		
-		}
+  int *h_graph_edges, char *h_graph_mask, char *h_updating_graph_mask, \
+  char *h_graph_visited, int *h_cost_ref){
+  char stop;
+  int k = 0;
+  do{
+  //if no thread changes this value then the loop stops
+  stop=false;
+  for(int tid = 0; tid < no_of_nodes; tid++ )
+  {
+  if (h_graph_mask[tid] == true){
+  h_graph_mask[tid]=false;
+  for(int i=h_graph_nodes[tid].starting; i<(h_graph_nodes[tid].no_of_edges + h_graph_nodes[tid].starting); i++){
+  int id = h_graph_edges[i];	//--cambine: node id is connected with node tid
+  if(!h_graph_visited[id]){	//--cambine: if node id has not been visited, enter the body below
+  h_cost_ref[id]=h_cost_ref[tid]+1;
+  h_updating_graph_mask[id]=true;
+  }
+  }
+  }
+  }
 
   		for(int tid=0; tid< no_of_nodes ; tid++ )
-		{
-			if (h_updating_graph_mask[tid] == true){
-			h_graph_mask[tid]=true;
-			h_graph_visited[tid]=true;
-			stop=true;
-			h_updating_graph_mask[tid]=false;
-			}
-		}
-		k++;
-	}
-	while(stop);
+  {
+  if (h_updating_graph_mask[tid] == true){
+  h_graph_mask[tid]=true;
+  h_graph_visited[tid]=true;
+  stop=true;
+  h_updating_graph_mask[tid]=false;
+  }
+  }
+  k++;
+  }
+  while(stop);
 }
 //----------------------------------------------------------
 //--breadth first search on GPUs
 //----------------------------------------------------------
 void run_bfs_gpu(int no_of_nodes, Node *h_graph_nodes, int edge_list_size, \
-		int *h_graph_edges, char *h_graph_mask, char *h_updating_graph_mask, \
-		char *h_graph_visited, int *h_cost) 
-					throw(std::string){
+  int *h_graph_edges, char *h_graph_mask, char *h_updating_graph_mask, \
+  char *h_graph_visited, int *h_cost)
+  throw(std::string){
 
-	//int number_elements = height*width;
-	char h_over;
-	cl_mem d_graph_nodes, d_graph_edges, d_graph_mask, d_updating_graph_mask, \
-			d_graph_visited, d_cost, d_over;
-	try{
-		//--1 transfer data from host to device
-		_clInit(platform_id, device_id, use_gpu);
-		d_graph_nodes = _clMalloc(no_of_nodes*sizeof(Node), h_graph_nodes);
-		d_graph_edges = _clMalloc(edge_list_size*sizeof(int), h_graph_edges);
-		d_graph_mask = _clMallocRW(no_of_nodes*sizeof(char), h_graph_mask);
-		d_updating_graph_mask = _clMallocRW(no_of_nodes*sizeof(char), h_updating_graph_mask);
-		d_graph_visited = _clMallocRW(no_of_nodes*sizeof(char), h_graph_visited);
+  //int number_elements = height*width;
+  char h_over;
+  cl_mem d_graph_nodes, d_graph_edges, d_graph_mask, d_updating_graph_mask, \
+  d_graph_visited, d_cost, d_over;
+  try{
+  //--1 transfer data from host to device
+  _clInit(platform_id, device_id, use_gpu);
+  d_graph_nodes = _clMalloc(no_of_nodes*sizeof(Node), h_graph_nodes);
+  d_graph_edges = _clMalloc(edge_list_size*sizeof(int), h_graph_edges);
+  d_graph_mask = _clMallocRW(no_of_nodes*sizeof(char), h_graph_mask);
+  d_updating_graph_mask = _clMallocRW(no_of_nodes*sizeof(char), h_updating_graph_mask);
+  d_graph_visited = _clMallocRW(no_of_nodes*sizeof(char), h_graph_visited);
 
 
-		d_cost = _clMallocRW(no_of_nodes*sizeof(int), h_cost);
-		d_over = _clMallocRW(sizeof(char), &h_over);
-		
-		_clMemcpyH2D(d_graph_nodes, no_of_nodes*sizeof(Node), h_graph_nodes);
-		_clMemcpyH2D(d_graph_edges, edge_list_size*sizeof(int), h_graph_edges);	
-		_clMemcpyH2D(d_graph_mask, no_of_nodes*sizeof(char), h_graph_mask);	
-		_clMemcpyH2D(d_updating_graph_mask, no_of_nodes*sizeof(char), h_updating_graph_mask);	
-		_clMemcpyH2D(d_graph_visited, no_of_nodes*sizeof(char), h_graph_visited);	
-		_clMemcpyH2D(d_cost, no_of_nodes*sizeof(int), h_cost);	
-			
-		//--2 invoke kernel
+  d_cost = _clMallocRW(no_of_nodes*sizeof(int), h_cost);
+  d_over = _clMallocRW(sizeof(char), &h_over);
+
+  _clMemcpyH2D(d_graph_nodes, no_of_nodes*sizeof(Node), h_graph_nodes);
+  _clMemcpyH2D(d_graph_edges, edge_list_size*sizeof(int), h_graph_edges);
+  _clMemcpyH2D(d_graph_mask, no_of_nodes*sizeof(char), h_graph_mask);
+  _clMemcpyH2D(d_updating_graph_mask, no_of_nodes*sizeof(char), h_updating_graph_mask);
+  _clMemcpyH2D(d_graph_visited, no_of_nodes*sizeof(char), h_graph_visited);
+  _clMemcpyH2D(d_cost, no_of_nodes*sizeof(int), h_cost);
+
+  //--2 invoke kernel
 #ifdef	PROFILING
-		timer kernel_timer;
-		double kernel_time = 0.0;		
-		kernel_timer.reset();
-		kernel_timer.start();
+  timer kernel_timer;
+  double kernel_time = 0.0;
+  kernel_timer.reset();
+  kernel_timer.start();
 #endif
-		do{
-			h_over = false;
-			_clMemcpyH2D(d_over, sizeof(char), &h_over);
-			//--kernel 0
-			int kernel_id = 0;
-			int kernel_idx = 0;
-			_clSetArgs(kernel_id, kernel_idx++, d_graph_nodes);
-			_clSetArgs(kernel_id, kernel_idx++, d_graph_edges);
-			_clSetArgs(kernel_id, kernel_idx++, d_graph_mask);
-			_clSetArgs(kernel_id, kernel_idx++, d_updating_graph_mask);
-			_clSetArgs(kernel_id, kernel_idx++, d_graph_visited);
-			_clSetArgs(kernel_id, kernel_idx++, d_cost);
-			_clSetArgs(kernel_id, kernel_idx++, &no_of_nodes, sizeof(int));
-			
-			//int work_items = no_of_nodes;
-			_clInvokeKernel(kernel_id, no_of_nodes, work_group_size);
-			
-			//--kernel 1
-			kernel_id = 1;
-			kernel_idx = 0;			
-			_clSetArgs(kernel_id, kernel_idx++, d_graph_mask);
-			_clSetArgs(kernel_id, kernel_idx++, d_updating_graph_mask);
-			_clSetArgs(kernel_id, kernel_idx++, d_graph_visited);
-			_clSetArgs(kernel_id, kernel_idx++, d_over);
-			_clSetArgs(kernel_id, kernel_idx++, &no_of_nodes, sizeof(int));
-			
-			//work_items = no_of_nodes;
-			_clInvokeKernel(kernel_id, no_of_nodes, work_group_size);			
-			
-			_clMemcpyD2H(d_over,sizeof(char), &h_over);
-			}while(h_over);
-			
-		_clFinish();
+  do{
+  h_over = false;
+  _clMemcpyH2D(d_over, sizeof(char), &h_over);
+  //--kernel 0
+  int kernel_id = 0;
+  int kernel_idx = 0;
+  _clSetArgs(kernel_id, kernel_idx++, d_graph_nodes);
+  _clSetArgs(kernel_id, kernel_idx++, d_graph_edges);
+  _clSetArgs(kernel_id, kernel_idx++, d_graph_mask);
+  _clSetArgs(kernel_id, kernel_idx++, d_updating_graph_mask);
+  _clSetArgs(kernel_id, kernel_idx++, d_graph_visited);
+  _clSetArgs(kernel_id, kernel_idx++, d_cost);
+  _clSetArgs(kernel_id, kernel_idx++, &no_of_nodes, sizeof(int));
+
+  //int work_items = no_of_nodes;
+  _clInvokeKernel(kernel_id, no_of_nodes, work_group_size);
+
+  //--kernel 1
+  kernel_id = 1;
+  kernel_idx = 0;
+  _clSetArgs(kernel_id, kernel_idx++, d_graph_mask);
+  _clSetArgs(kernel_id, kernel_idx++, d_updating_graph_mask);
+  _clSetArgs(kernel_id, kernel_idx++, d_graph_visited);
+  _clSetArgs(kernel_id, kernel_idx++, d_over);
+  _clSetArgs(kernel_id, kernel_idx++, &no_of_nodes, sizeof(int));
+
+  //work_items = no_of_nodes;
+  _clInvokeKernel(kernel_id, no_of_nodes, work_group_size);
+
+  _clMemcpyD2H(d_over,sizeof(char), &h_over);
+  }while(h_over);
+
+  _clFinish();
 #ifdef	PROFILING
-		kernel_timer.stop();
-		kernel_time = kernel_timer.getTimeInSeconds();
+  kernel_timer.stop();
+  kernel_time = kernel_timer.getTimeInSeconds();
 #endif
-		//--3 transfer data from device to host
-		_clMemcpyD2H(d_cost,no_of_nodes*sizeof(int), h_cost);
-		//--statistics
+  //--3 transfer data from device to host
+  _clMemcpyD2H(d_cost,no_of_nodes*sizeof(int), h_cost);
+  //--statistics
 #ifdef	PROFILING
-		std::cout<<"kernel time(s):"<<kernel_time<<std::endl;		
+  std::cout<<"kernel time(s):"<<kernel_time<<std::endl;
 #endif
-		//--4 release cl resources.
-		_clFree(d_graph_nodes);
-		_clFree(d_graph_edges);
-		_clFree(d_graph_mask);
-		_clFree(d_updating_graph_mask);
-		_clFree(d_graph_visited);
-		_clFree(d_cost);
-		_clFree(d_over);
-		_clRelease();
-	}
-	catch(std::string msg){		
-		_clFree(d_graph_nodes);
-		_clFree(d_graph_edges);
-		_clFree(d_graph_mask);
-		_clFree(d_updating_graph_mask);
-		_clFree(d_graph_visited);
-		_clFree(d_cost);
-		_clFree(d_over);
-		_clRelease();
-		std::string e_str = "in run_transpose_gpu -> ";
-		e_str += msg;
-		throw(e_str);
-	}
-	return ;
+  //--4 release cl resources.
+  _clFree(d_graph_nodes);
+  _clFree(d_graph_edges);
+  _clFree(d_graph_mask);
+  _clFree(d_updating_graph_mask);
+  _clFree(d_graph_visited);
+  _clFree(d_cost);
+  _clFree(d_over);
+  _clRelease();
+  }
+  catch(std::string msg){
+  _clFree(d_graph_nodes);
+  _clFree(d_graph_edges);
+  _clFree(d_graph_mask);
+  _clFree(d_updating_graph_mask);
+  _clFree(d_graph_visited);
+  _clFree(d_cost);
+  _clFree(d_over);
+  _clRelease();
+  std::string e_str = "in run_transpose_gpu -> ";
+  e_str += msg;
+  throw(e_str);
+  }
+  return ;
 }
 
 void Usage(char *argv0){
@@ -197,14 +197,14 @@ void Usage(char *argv0){
 //----------------------------------------------------------
 int main(int argc, char * argv[])
 {
-	int no_of_nodes;
-	int edge_list_size;
-	FILE *fp;
-	Node* h_graph_nodes;
-	char *h_graph_mask, *h_updating_graph_mask, *h_graph_visited;
-	char *input_f;
+  int no_of_nodes;
+  int edge_list_size;
+  FILE *fp;
+  Node* h_graph_nodes;
+  char *h_graph_mask, *h_updating_graph_mask, *h_graph_visited;
+  char *input_f;
 
-	try{
+  try{
     // Arguments parsing for platform, device and type
     // Variables to store information on platform and device to use_gpu
 
@@ -228,104 +228,104 @@ int main(int argc, char * argv[])
         }
     }
 
-		printf("Reading File\n");
-		//Read in Graph from a file
-		fp = fopen(input_f,"r");
-		if(!fp){
-		  printf("Error Reading graph file\n");
-		  return 0;
-		}
+  printf("Reading File\n");
+  //Read in Graph from a file
+  fp = fopen(input_f,"r");
+  if(!fp){
+    printf("Error Reading graph file\n");
+    return 0;
+  }
 
-		int source = 0;
+  int source = 0;
 
-		fscanf(fp,"%d",&no_of_nodes);
+  fscanf(fp,"%d",&no_of_nodes);
 
-		int num_of_blocks = 1;
-		int num_of_threads_per_block = no_of_nodes;
+  int num_of_blocks = 1;
+  int num_of_threads_per_block = no_of_nodes;
 
-		//Make execution Parameters according to the number of nodes
-		//Distribute threads across multiple Blocks if necessary
-		if(no_of_nodes>MAX_THREADS_PER_BLOCK){
-			num_of_blocks = (int)ceil(no_of_nodes/(double)MAX_THREADS_PER_BLOCK); 
-			num_of_threads_per_block = MAX_THREADS_PER_BLOCK; 
-		}
-		work_group_size = num_of_threads_per_block;
-		// allocate host memory
-		h_graph_nodes = (Node*) malloc(sizeof(Node)*no_of_nodes);
-		h_graph_mask = (char*) malloc(sizeof(char)*no_of_nodes);
-		h_updating_graph_mask = (char*) malloc(sizeof(char)*no_of_nodes);
-		h_graph_visited = (char*) malloc(sizeof(char)*no_of_nodes);
-	
-		int start, edgeno;   
-		// initalize the memory
-		for(int i = 0; i < no_of_nodes; i++){
-			fscanf(fp,"%d %d",&start,&edgeno);
-			h_graph_nodes[i].starting = start;
-			h_graph_nodes[i].no_of_edges = edgeno;
-			h_graph_mask[i]=false;
-			h_updating_graph_mask[i]=false;
-			h_graph_visited[i]=false;
-		}
-		//read the source node from the file
-		fscanf(fp,"%d",&source);
-		source=0;
-		//set the source node as true in the mask
-		h_graph_mask[source]=true;
-		h_graph_visited[source]=true;
+  //Make execution Parameters according to the number of nodes
+  //Distribute threads across multiple Blocks if necessary
+  if(no_of_nodes>MAX_THREADS_PER_BLOCK){
+  num_of_blocks = (int)ceil(no_of_nodes/(double)MAX_THREADS_PER_BLOCK);
+  num_of_threads_per_block = MAX_THREADS_PER_BLOCK;
+  }
+  work_group_size = num_of_threads_per_block;
+  // allocate host memory
+  h_graph_nodes = (Node*) malloc(sizeof(Node)*no_of_nodes);
+  h_graph_mask = (char*) malloc(sizeof(char)*no_of_nodes);
+  h_updating_graph_mask = (char*) malloc(sizeof(char)*no_of_nodes);
+  h_graph_visited = (char*) malloc(sizeof(char)*no_of_nodes);
+
+  int start, edgeno;
+  // initalize the memory
+  for(int i = 0; i < no_of_nodes; i++){
+  fscanf(fp,"%d %d",&start,&edgeno);
+  h_graph_nodes[i].starting = start;
+  h_graph_nodes[i].no_of_edges = edgeno;
+  h_graph_mask[i]=false;
+  h_updating_graph_mask[i]=false;
+  h_graph_visited[i]=false;
+  }
+  //read the source node from the file
+  fscanf(fp,"%d",&source);
+  source=0;
+  //set the source node as true in the mask
+  h_graph_mask[source]=true;
+  h_graph_visited[source]=true;
     	fscanf(fp,"%d",&edge_list_size);
    		int id,cost;
-		int* h_graph_edges = (int*) malloc(sizeof(int)*edge_list_size);
-		for(int i=0; i < edge_list_size ; i++){
-			fscanf(fp,"%d",&id);
-			fscanf(fp,"%d",&cost);
-			h_graph_edges[i] = id;
-		}
+  int* h_graph_edges = (int*) malloc(sizeof(int)*edge_list_size);
+  for(int i=0; i < edge_list_size ; i++){
+  fscanf(fp,"%d",&id);
+  fscanf(fp,"%d",&cost);
+  h_graph_edges[i] = id;
+  }
+
+  if(fp)
+  fclose(fp);
+  // allocate mem for the result on host side
+  int	*h_cost = (int*) malloc(sizeof(int)*no_of_nodes);
+  int *h_cost_ref = (int*)malloc(sizeof(int)*no_of_nodes);
+  for(int i=0;i<no_of_nodes;i++){
+  h_cost[i]=-1;
+  h_cost_ref[i] = -1;
+  }
+  h_cost[source]=0;
+  h_cost_ref[source]=0;
+  //---------------------------------------------------------
+  //--gpu entry
+  run_bfs_gpu(no_of_nodes,h_graph_nodes,edge_list_size,h_graph_edges, h_graph_mask, h_updating_graph_mask, h_graph_visited, h_cost);
+  //---------------------------------------------------------
+  //--cpu entry
+  // initalize the memory again
+  for(int i = 0; i < no_of_nodes; i++){
+  h_graph_mask[i]=false;
+  h_updating_graph_mask[i]=false;
+  h_graph_visited[i]=false;
+  }
+  //set the source node as true in the mask
+  source=0;
+  h_graph_mask[source]=true;
+  h_graph_visited[source]=true;
+  run_bfs_cpu(no_of_nodes,h_graph_nodes,edge_list_size,h_graph_edges, h_graph_mask, h_updating_graph_mask, h_graph_visited, h_cost_ref);
+  //---------------------------------------------------------
+  //--result varification
+  compare_results<int>(h_cost_ref, h_cost, no_of_nodes);
+  //release host memory
+  free(h_graph_nodes);
+  free(h_graph_mask);
+  free(h_updating_graph_mask);
+  free(h_graph_visited);
 
-		if(fp)
-			fclose(fp);    
-		// allocate mem for the result on host side
-		int	*h_cost = (int*) malloc(sizeof(int)*no_of_nodes);
-		int *h_cost_ref = (int*)malloc(sizeof(int)*no_of_nodes);
-		for(int i=0;i<no_of_nodes;i++){
-			h_cost[i]=-1;
-			h_cost_ref[i] = -1;
-		}
-		h_cost[source]=0;
-		h_cost_ref[source]=0;		
-		//---------------------------------------------------------
-		//--gpu entry
-		run_bfs_gpu(no_of_nodes,h_graph_nodes,edge_list_size,h_graph_edges, h_graph_mask, h_updating_graph_mask, h_graph_visited, h_cost);	
-		//---------------------------------------------------------
-		//--cpu entry
-		// initalize the memory again
-		for(int i = 0; i < no_of_nodes; i++){
-			h_graph_mask[i]=false;
-			h_updating_graph_mask[i]=false;
-			h_graph_visited[i]=false;
-		}
-		//set the source node as true in the mask
-		source=0;
-		h_graph_mask[source]=true;
-		h_graph_visited[source]=true;
-		run_bfs_cpu(no_of_nodes,h_graph_nodes,edge_list_size,h_graph_edges, h_graph_mask, h_updating_graph_mask, h_graph_visited, h_cost_ref);
-		//---------------------------------------------------------
-		//--result varification
-		compare_results<int>(h_cost_ref, h_cost, no_of_nodes);
-		//release host memory		
-		free(h_graph_nodes);
-		free(h_graph_mask);
-		free(h_updating_graph_mask);
-		free(h_graph_visited);
+  }
+  catch(std::string msg){
+  std::cout<<"--cambine: exception in main ->"<<msg<<std::endl;
+  //release host memory
+  free(h_graph_nodes);
+  free(h_graph_mask);
+  free(h_updating_graph_mask);
+  free(h_graph_visited);
+  }
 
-	}
-	catch(std::string msg){
-		std::cout<<"--cambine: exception in main ->"<<msg<<std::endl;
-		//release host memory
-		free(h_graph_nodes);
-		free(h_graph_mask);
-		free(h_updating_graph_mask);
-		free(h_graph_visited);		
-	}
-		
     return 0;
 }

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 743 - 743
opencl/cfd/CLHelper.h


+ 319 - 319
opencl/cfd/euler3d.cpp

@@ -1,15 +1,15 @@
 /********************************************************************
-	euler3d.cpp
-	: parallelized code of CFD
-
-	- original code from the AIAA-2009-4001 by Andrew Corrigan, acorriga@gmu.edu
-	- parallelization with OpenCL API has been applied by
-	Jianbin Fang - j.fang@tudelft.nl
-	Delft University of Technology
-	Faculty of Electrical Engineering, Mathematics and Computer Science
-	Department of Software Technology
-	Parallel and Distributed Systems Group
-	on 24/03/2011
+  euler3d.cpp
+  : parallelized code of CFD
+
+  - original code from the AIAA-2009-4001 by Andrew Corrigan, acorriga@gmu.edu
+  - parallelization with OpenCL API has been applied by
+  Jianbin Fang - j.fang@tudelft.nl
+  Delft University of Technology
+  Faculty of Electrical Engineering, Mathematics and Computer Science
+  Department of Software Technology
+  Parallel and Distributed Systems Group
+  on 24/03/2011
 ********************************************************************/
 
 #include <iostream>
@@ -24,7 +24,7 @@
 #define GAMMA 1.4f
 #define iterations 2000
 #ifndef block_length
-	#define block_length 192
+  #define block_length 192
 #endif
 
 #define NDIM 3
@@ -51,9 +51,9 @@
 
 //self-defined user type
 typedef struct{
-	float x;
-	float y;
-	float z;
+  float x;
+  float y;
+  float z;
 } float3;
 
 /*
@@ -61,142 +61,142 @@ typedef struct{
  */
 template <typename T>
 cl_mem alloc(int N){
-	cl_mem mem_d = _clMalloc(sizeof(T)*N);
-	return mem_d;
+  cl_mem mem_d = _clMalloc(sizeof(T)*N);
+  return mem_d;
 }
 
 template <typename T>
 void dealloc(cl_mem array){
-	_clFree(array);
+  _clFree(array);
 }
 
 template <typename T>
 void copy(cl_mem dst, cl_mem src, int N){
-	_clMemcpyD2D(dst, src, N*sizeof(T));
+  _clMemcpyD2D(dst, src, N*sizeof(T));
 }
 
 template <typename T>
 void upload(cl_mem dst, T* src, int N){
-	_clMemcpyH2D(dst, src, N*sizeof(T));
+  _clMemcpyH2D(dst, src, N*sizeof(T));
 }
 
 template <typename T>
 void download(T* dst, cl_mem src, int N){
-	_clMemcpyD2H(dst, src, N*sizeof(T));
+  _clMemcpyD2H(dst, src, N*sizeof(T));
 }
 
 void dump(cl_mem variables, int nel, int nelr){
-	float* h_variables = new float[nelr*NVAR];
-	download(h_variables, variables, nelr*NVAR);
-
-	{
-		std::ofstream file("density");
-		file << nel << " " << nelr << std::endl;
-		for(int i = 0; i < nel; i++) file << h_variables[i + VAR_DENSITY*nelr] << std::endl;
-	}
-
-
-	{
-		std::ofstream file("momentum");
-		file << nel << " " << nelr << std::endl;
-		for(int i = 0; i < nel; i++)
-		{
-			for(int j = 0; j != NDIM; j++)
-				file << h_variables[i + (VAR_MOMENTUM+j)*nelr] << " ";
-			file << std::endl;
-		}
-	}
-
-	{
-		std::ofstream file("density_energy");
-		file << nel << " " << nelr << std::endl;
-		for(int i = 0; i < nel; i++) file << h_variables[i + VAR_DENSITY_ENERGY*nelr] << std::endl;
-	}
-	delete[] h_variables;
+  float* h_variables = new float[nelr*NVAR];
+  download(h_variables, variables, nelr*NVAR);
+
+  {
+  std::ofstream file("density");
+  file << nel << " " << nelr << std::endl;
+  for(int i = 0; i < nel; i++) file << h_variables[i + VAR_DENSITY*nelr] << std::endl;
+  }
+
+
+  {
+  std::ofstream file("momentum");
+  file << nel << " " << nelr << std::endl;
+  for(int i = 0; i < nel; i++)
+  {
+  for(int j = 0; j != NDIM; j++)
+  file << h_variables[i + (VAR_MOMENTUM+j)*nelr] << " ";
+  file << std::endl;
+  }
+  }
+
+  {
+  std::ofstream file("density_energy");
+  file << nel << " " << nelr << std::endl;
+  for(int i = 0; i < nel; i++) file << h_variables[i + VAR_DENSITY_ENERGY*nelr] << std::endl;
+  }
+  delete[] h_variables;
 }
 
 void initialize_variables(int nelr, cl_mem variables, cl_mem ff_variable) throw(string){
 
-	int work_items = nelr;
-	int work_group_size = BLOCK_SIZE_1;
-	int kernel_id = 1;
-	int arg_idx = 0;
-	_clSetArgs(kernel_id, arg_idx++, variables);
-	_clSetArgs(kernel_id, arg_idx++, ff_variable);
-	_clSetArgs(kernel_id, arg_idx++, &nelr, sizeof(int));
-	_clInvokeKernel(kernel_id, work_items, work_group_size);
+  int work_items = nelr;
+  int work_group_size = BLOCK_SIZE_1;
+  int kernel_id = 1;
+  int arg_idx = 0;
+  _clSetArgs(kernel_id, arg_idx++, variables);
+  _clSetArgs(kernel_id, arg_idx++, ff_variable);
+  _clSetArgs(kernel_id, arg_idx++, &nelr, sizeof(int));
+  _clInvokeKernel(kernel_id, work_items, work_group_size);
 }
 
 void compute_step_factor(int nelr, cl_mem variables, cl_mem areas, cl_mem step_factors){
 
-	int work_items = nelr;
-	int work_group_size = BLOCK_SIZE_2;
-	int kernel_id = 2;
-	int arg_idx = 0;
-	_clSetArgs(kernel_id, arg_idx++, variables);
-	_clSetArgs(kernel_id, arg_idx++, areas);
-	_clSetArgs(kernel_id, arg_idx++, step_factors);
-	_clSetArgs(kernel_id, arg_idx++, &nelr, sizeof(int));
-	_clInvokeKernel(kernel_id, work_items, work_group_size);
+  int work_items = nelr;
+  int work_group_size = BLOCK_SIZE_2;
+  int kernel_id = 2;
+  int arg_idx = 0;
+  _clSetArgs(kernel_id, arg_idx++, variables);
+  _clSetArgs(kernel_id, arg_idx++, areas);
+  _clSetArgs(kernel_id, arg_idx++, step_factors);
+  _clSetArgs(kernel_id, arg_idx++, &nelr, sizeof(int));
+  _clInvokeKernel(kernel_id, work_items, work_group_size);
 }
 
 void compute_flux(int nelr, cl_mem elements_surrounding_elements, cl_mem normals, cl_mem variables, cl_mem ff_variable, \
-			cl_mem fluxes, cl_mem ff_flux_contribution_density_energy,
-			cl_mem ff_flux_contribution_momentum_x,
-			cl_mem ff_flux_contribution_momentum_y,
-			cl_mem ff_flux_contribution_momentum_z){
-
-	int work_items = nelr;
-	int work_group_size = BLOCK_SIZE_3;
-	int kernel_id = 3;
-	int arg_idx = 0;
-	_clSetArgs(kernel_id, arg_idx++, elements_surrounding_elements);
-	_clSetArgs(kernel_id, arg_idx++, normals);
-	_clSetArgs(kernel_id, arg_idx++, variables);
-	_clSetArgs(kernel_id, arg_idx++, ff_variable);
-	_clSetArgs(kernel_id, arg_idx++, fluxes);
-	_clSetArgs(kernel_id, arg_idx++, ff_flux_contribution_density_energy);
-	_clSetArgs(kernel_id, arg_idx++, ff_flux_contribution_momentum_x);
-	_clSetArgs(kernel_id, arg_idx++, ff_flux_contribution_momentum_y);
-	_clSetArgs(kernel_id, arg_idx++, ff_flux_contribution_momentum_z);
-	_clSetArgs(kernel_id, arg_idx++, &nelr, sizeof(int));
-	_clInvokeKernel(kernel_id, work_items, work_group_size);
+  cl_mem fluxes, cl_mem ff_flux_contribution_density_energy,
+  cl_mem ff_flux_contribution_momentum_x,
+  cl_mem ff_flux_contribution_momentum_y,
+  cl_mem ff_flux_contribution_momentum_z){
+
+  int work_items = nelr;
+  int work_group_size = BLOCK_SIZE_3;
+  int kernel_id = 3;
+  int arg_idx = 0;
+  _clSetArgs(kernel_id, arg_idx++, elements_surrounding_elements);
+  _clSetArgs(kernel_id, arg_idx++, normals);
+  _clSetArgs(kernel_id, arg_idx++, variables);
+  _clSetArgs(kernel_id, arg_idx++, ff_variable);
+  _clSetArgs(kernel_id, arg_idx++, fluxes);
+  _clSetArgs(kernel_id, arg_idx++, ff_flux_contribution_density_energy);
+  _clSetArgs(kernel_id, arg_idx++, ff_flux_contribution_momentum_x);
+  _clSetArgs(kernel_id, arg_idx++, ff_flux_contribution_momentum_y);
+  _clSetArgs(kernel_id, arg_idx++, ff_flux_contribution_momentum_z);
+  _clSetArgs(kernel_id, arg_idx++, &nelr, sizeof(int));
+  _clInvokeKernel(kernel_id, work_items, work_group_size);
 }
 
 void time_step(int j, int nelr, cl_mem old_variables, cl_mem variables, cl_mem step_factors, cl_mem fluxes){
 
-	int work_items = nelr;
-	int work_group_size = BLOCK_SIZE_4;
-	int kernel_id = 4;
-	int arg_idx = 0;
-	_clSetArgs(kernel_id, arg_idx++, &j, sizeof(int));
-	_clSetArgs(kernel_id, arg_idx++, &nelr, sizeof(int));
-	_clSetArgs(kernel_id, arg_idx++, old_variables);
-	_clSetArgs(kernel_id, arg_idx++, variables);
-	_clSetArgs(kernel_id, arg_idx++, step_factors);
-	_clSetArgs(kernel_id, arg_idx++, fluxes);
-
-	_clInvokeKernel(kernel_id, work_items, work_group_size);
+  int work_items = nelr;
+  int work_group_size = BLOCK_SIZE_4;
+  int kernel_id = 4;
+  int arg_idx = 0;
+  _clSetArgs(kernel_id, arg_idx++, &j, sizeof(int));
+  _clSetArgs(kernel_id, arg_idx++, &nelr, sizeof(int));
+  _clSetArgs(kernel_id, arg_idx++, old_variables);
+  _clSetArgs(kernel_id, arg_idx++, variables);
+  _clSetArgs(kernel_id, arg_idx++, step_factors);
+  _clSetArgs(kernel_id, arg_idx++, fluxes);
+
+  _clInvokeKernel(kernel_id, work_items, work_group_size);
 }
 inline void compute_flux_contribution(float& density, float3& momentum, float& density_energy, float& pressure, float3& velocity, float3& fc_momentum_x, float3& fc_momentum_y, float3& fc_momentum_z, float3& fc_density_energy)
 {
-	fc_momentum_x.x = velocity.x*momentum.x + pressure;
-	fc_momentum_x.y = velocity.x*momentum.y;
-	fc_momentum_x.z = velocity.x*momentum.z;
+  fc_momentum_x.x = velocity.x*momentum.x + pressure;
+  fc_momentum_x.y = velocity.x*momentum.y;
+  fc_momentum_x.z = velocity.x*momentum.z;
 
 
-	fc_momentum_y.x = fc_momentum_x.y;
-	fc_momentum_y.y = velocity.y*momentum.y + pressure;
-	fc_momentum_y.z = velocity.y*momentum.z;
+  fc_momentum_y.x = fc_momentum_x.y;
+  fc_momentum_y.y = velocity.y*momentum.y + pressure;
+  fc_momentum_y.z = velocity.y*momentum.z;
 
-	fc_momentum_z.x = fc_momentum_x.z;
-	fc_momentum_z.y = fc_momentum_y.z;
-	fc_momentum_z.z = velocity.z*momentum.z + pressure;
+  fc_momentum_z.x = fc_momentum_x.z;
+  fc_momentum_z.y = fc_momentum_y.z;
+  fc_momentum_z.z = velocity.z*momentum.z + pressure;
 
-	float de_p = density_energy+pressure;
-	fc_density_energy.x = velocity.x*de_p;
-	fc_density_energy.y = velocity.y*de_p;
-	fc_density_energy.z = velocity.z*de_p;
+  float de_p = density_energy+pressure;
+  fc_density_energy.x = velocity.x*de_p;
+  fc_density_energy.y = velocity.y*de_p;
+  fc_density_energy.z = velocity.z*de_p;
 }
 
 /*
@@ -205,213 +205,213 @@ inline void compute_flux_contribution(float& density, float3& momentum, float& d
 int main(int argc, char** argv){
   printf("WG size of kernel:initialize = %d, WG size of kernel:compute_step_factor = %d, WG size of kernel:compute_flux = %d, WG size of kernel:time_step = %d\n", BLOCK_SIZE_1, BLOCK_SIZE_2, BLOCK_SIZE_3, BLOCK_SIZE_4);
 
-	if (argc < 2){
-		std::cout << "specify data file name and [platform id] [device id] [device type] " << std::endl;
-		return 0;
-	}
-	const char* data_file_name = argv[1];
-	_clCmdParams(argc, argv);
-	cl_mem ff_variable, ff_flux_contribution_momentum_x, ff_flux_contribution_momentum_y,ff_flux_contribution_momentum_z,  ff_flux_contribution_density_energy;
-	cl_mem areas, elements_surrounding_elements, normals;
-	cl_mem variables, old_variables, fluxes, step_factors;
-	float h_ff_variable[NVAR];
-
-	try{
-		_clInit(device_type, device_id);
-		// set far field conditions and load them into constant memory on the gpu
-		{
-			//float h_ff_variable[NVAR];
-			const float angle_of_attack = float(3.1415926535897931 / 180.0f) * float(deg_angle_of_attack);
-
-			h_ff_variable[VAR_DENSITY] = float(1.4);
-
-			float ff_pressure = float(1.0f);
-			float ff_speed_of_sound = sqrt(GAMMA*ff_pressure / h_ff_variable[VAR_DENSITY]);
-			float ff_speed = float(ff_mach)*ff_speed_of_sound;
-
-			float3 ff_velocity;
-			ff_velocity.x = ff_speed*float(cos((float)angle_of_attack));
-			ff_velocity.y = ff_speed*float(sin((float)angle_of_attack));
-			ff_velocity.z = 0.0f;
-
-			h_ff_variable[VAR_MOMENTUM+0] = h_ff_variable[VAR_DENSITY] * ff_velocity.x;
-			h_ff_variable[VAR_MOMENTUM+1] = h_ff_variable[VAR_DENSITY] * ff_velocity.y;
-			h_ff_variable[VAR_MOMENTUM+2] = h_ff_variable[VAR_DENSITY] * ff_velocity.z;
-
-			h_ff_variable[VAR_DENSITY_ENERGY] = h_ff_variable[VAR_DENSITY]*(float(0.5f)*(ff_speed*ff_speed)) + (ff_pressure / float(GAMMA-1.0f));
-
-			float3 h_ff_momentum;
-			h_ff_momentum.x = *(h_ff_variable+VAR_MOMENTUM+0);
-			h_ff_momentum.y = *(h_ff_variable+VAR_MOMENTUM+1);
-			h_ff_momentum.z = *(h_ff_variable+VAR_MOMENTUM+2);
-			float3 h_ff_flux_contribution_momentum_x;
-			float3 h_ff_flux_contribution_momentum_y;
-			float3 h_ff_flux_contribution_momentum_z;
-			float3 h_ff_flux_contribution_density_energy;
-			compute_flux_contribution(h_ff_variable[VAR_DENSITY], h_ff_momentum, h_ff_variable[VAR_DENSITY_ENERGY], ff_pressure, ff_velocity, h_ff_flux_contribution_momentum_x, h_ff_flux_contribution_momentum_y, h_ff_flux_contribution_momentum_z, h_ff_flux_contribution_density_energy);
-
-			// copy far field conditions to the gpu
-			//cl_mem ff_variable, ff_flux_contribution_momentum_x, ff_flux_contribution_momentum_y,ff_flux_contribution_momentum_z,  ff_flux_contribution_density_energy;
-			ff_variable = _clMalloc(NVAR*sizeof(float));
-			ff_flux_contribution_momentum_x = _clMalloc(sizeof(float3));
-			ff_flux_contribution_momentum_y = _clMalloc(sizeof(float3));
-			ff_flux_contribution_momentum_z = _clMalloc(sizeof(float3));
-			ff_flux_contribution_density_energy = _clMalloc(sizeof(float3));
-			_clMemcpyH2D(ff_variable,          h_ff_variable,          NVAR*sizeof(float));
-			_clMemcpyH2D(ff_flux_contribution_momentum_x, &h_ff_flux_contribution_momentum_x, sizeof(float3));
-			_clMemcpyH2D(ff_flux_contribution_momentum_y, &h_ff_flux_contribution_momentum_y, sizeof(float3));
-			_clMemcpyH2D(ff_flux_contribution_momentum_z, &h_ff_flux_contribution_momentum_z, sizeof(float3));
-			_clMemcpyH2D(ff_flux_contribution_density_energy, &h_ff_flux_contribution_density_energy, sizeof(float3));
-			_clFinish();
-
-		}
-		int nel;
-		int nelr;
-		// read in domain geometry
-		//float* areas;
-		//int* elements_surrounding_elements;
-		//float* normals;
-		{
-			std::ifstream file(data_file_name);
-			if(file==NULL){
-				throw(string("can not find/open file!"));
-			}
-			file >> nel;
-			nelr = block_length*((nel / block_length )+ std::min(1, nel % block_length));
-			std::cout<<"--cambine: nel="<<nel<<", nelr="<<nelr<<std::endl;
-			float* h_areas = new float[nelr];
-			int* h_elements_surrounding_elements = new int[nelr*NNB];
-			float* h_normals = new float[nelr*NDIM*NNB];
-
-
-			// read in data
-			for(int i = 0; i < nel; i++)
-			{
-				file >> h_areas[i];
-				for(int j = 0; j < NNB; j++)
-				{
-					file >> h_elements_surrounding_elements[i + j*nelr];
-					if(h_elements_surrounding_elements[i+j*nelr] < 0) h_elements_surrounding_elements[i+j*nelr] = -1;
-					h_elements_surrounding_elements[i + j*nelr]--; //it's coming in with Fortran numbering
-
-					for(int k = 0; k < NDIM; k++)
-					{
-						file >> h_normals[i + (j + k*NNB)*nelr];
-						h_normals[i + (j + k*NNB)*nelr] = -h_normals[i + (j + k*NNB)*nelr];
-					}
-				}
-			}
-
-			// fill in remaining data
-			int last = nel-1;
-			for(int i = nel; i < nelr; i++)
-			{
-				h_areas[i] = h_areas[last];
-				for(int j = 0; j < NNB; j++)
-				{
-					// duplicate the last element
-					h_elements_surrounding_elements[i + j*nelr] = h_elements_surrounding_elements[last + j*nelr];
-					for(int k = 0; k < NDIM; k++) h_normals[last + (j + k*NNB)*nelr] = h_normals[last + (j + k*NNB)*nelr];
-				}
-			}
-
-			areas = alloc<float>(nelr);
-			upload<float>(areas, h_areas, nelr);
-
-			elements_surrounding_elements = alloc<int>(nelr*NNB);
-			upload<int>(elements_surrounding_elements, h_elements_surrounding_elements, nelr*NNB);
-
-			normals = alloc<float>(nelr*NDIM*NNB);
-			upload<float>(normals, h_normals, nelr*NDIM*NNB);
-
-			delete[] h_areas;
-			delete[] h_elements_surrounding_elements;
-			delete[] h_normals;
-		}
-
-		// Create arrays and set initial conditions
-		variables = alloc<float>(nelr*NVAR);
-		int tp = 0;
-		initialize_variables(nelr, variables, ff_variable);
-		old_variables = alloc<float>(nelr*NVAR);
-		fluxes = alloc<float>(nelr*NVAR);
-		step_factors = alloc<float>(nelr);
-		// make sure all memory is floatly allocated before we start timing
-		initialize_variables(nelr, old_variables, ff_variable);
-		initialize_variables(nelr, fluxes, ff_variable);
-		_clMemset(step_factors, 0, sizeof(float)*nelr);
-		// make sure CUDA isn't still doing something before we start timing
-		_clFinish();
-		// these need to be computed the first time in order to compute time step
-		std::cout << "Starting..." << std::endl;
-
-		// Begin iterations
-		for(int i = 0; i < iterations; i++){
-			copy<float>(old_variables, variables, nelr*NVAR);
-			// for the first iteration we compute the time step
-			compute_step_factor(nelr, variables, areas, step_factors);
-			for(int j = 0; j < RK; j++){
-				compute_flux(nelr, elements_surrounding_elements, normals, variables, ff_variable, fluxes, ff_flux_contribution_density_energy, \
-				ff_flux_contribution_momentum_x, ff_flux_contribution_momentum_y, ff_flux_contribution_momentum_z);
-
-				time_step(j, nelr, old_variables, variables, step_factors, fluxes);
-			}
-		}
-		_clFinish();
-		std::cout << "Saving solution..." << std::endl;
-		dump(variables, nel, nelr);
-		std::cout << "Saved solution..." << std::endl;
-		_clStatistics();
-		std::cout << "Cleaning up..." << std::endl;
-
-		//--release resources
-		_clFree(ff_variable);
-		_clFree(ff_flux_contribution_momentum_x);
-		_clFree(ff_flux_contribution_momentum_y);
-		_clFree(ff_flux_contribution_momentum_z);
-		_clFree(ff_flux_contribution_density_energy);
-		_clFree(areas);
-		_clFree(elements_surrounding_elements);
-		_clFree(normals);
-		_clFree(variables);
-		_clFree(old_variables);
-		_clFree(fluxes);
-		_clFree(step_factors);
-		_clRelease();
-		std::cout << "Done..." << std::endl;
-	}
-	catch(string msg){
-		std::cout<<"--cambine:( an exception catched in main body ->"<<msg<<std::endl;
-		_clFree(ff_variable);
-		_clFree(ff_flux_contribution_momentum_x);
-		_clFree(ff_flux_contribution_momentum_y);
-		_clFree(ff_flux_contribution_momentum_z);
-		_clFree(ff_flux_contribution_density_energy);
-		_clFree(areas);
-		_clFree(elements_surrounding_elements);
-		_clFree(normals);
-		_clFree(variables);
-		_clFree(old_variables);
-		_clFree(fluxes);
-		_clFree(step_factors);
-		_clRelease();
-	}
-	catch(...){
-		std::cout<<"--cambine:( unknow exceptions in main body..."<<std::endl;
-		_clFree(ff_variable);
-		_clFree(ff_flux_contribution_momentum_x);
-		_clFree(ff_flux_contribution_momentum_y);
-		_clFree(ff_flux_contribution_momentum_z);
-		_clFree(ff_flux_contribution_density_energy);
-		_clFree(areas);
-		_clFree(elements_surrounding_elements);
-		_clFree(normals);
-		_clFree(variables);
-		_clFree(old_variables);
-		_clFree(fluxes);
-		_clFree(step_factors);
-		_clRelease();
-	}
-
-	return 0;
+  if (argc < 2){
+  std::cout << "specify data file name and [platform id] [device id] [device type] " << std::endl;
+  return 0;
+  }
+  const char* data_file_name = argv[1];
+  _clCmdParams(argc, argv);
+  cl_mem ff_variable, ff_flux_contribution_momentum_x, ff_flux_contribution_momentum_y,ff_flux_contribution_momentum_z,  ff_flux_contribution_density_energy;
+  cl_mem areas, elements_surrounding_elements, normals;
+  cl_mem variables, old_variables, fluxes, step_factors;
+  float h_ff_variable[NVAR];
+
+  try{
+  _clInit(device_type, device_id);
+  // set far field conditions and load them into constant memory on the gpu
+  {
+  //float h_ff_variable[NVAR];
+  const float angle_of_attack = float(3.1415926535897931 / 180.0f) * float(deg_angle_of_attack);
+
+  h_ff_variable[VAR_DENSITY] = float(1.4);
+
+  float ff_pressure = float(1.0f);
+  float ff_speed_of_sound = sqrt(GAMMA*ff_pressure / h_ff_variable[VAR_DENSITY]);
+  float ff_speed = float(ff_mach)*ff_speed_of_sound;
+
+  float3 ff_velocity;
+  ff_velocity.x = ff_speed*float(cos((float)angle_of_attack));
+  ff_velocity.y = ff_speed*float(sin((float)angle_of_attack));
+  ff_velocity.z = 0.0f;
+
+  h_ff_variable[VAR_MOMENTUM+0] = h_ff_variable[VAR_DENSITY] * ff_velocity.x;
+  h_ff_variable[VAR_MOMENTUM+1] = h_ff_variable[VAR_DENSITY] * ff_velocity.y;
+  h_ff_variable[VAR_MOMENTUM+2] = h_ff_variable[VAR_DENSITY] * ff_velocity.z;
+
+  h_ff_variable[VAR_DENSITY_ENERGY] = h_ff_variable[VAR_DENSITY]*(float(0.5f)*(ff_speed*ff_speed)) + (ff_pressure / float(GAMMA-1.0f));
+
+  float3 h_ff_momentum;
+  h_ff_momentum.x = *(h_ff_variable+VAR_MOMENTUM+0);
+  h_ff_momentum.y = *(h_ff_variable+VAR_MOMENTUM+1);
+  h_ff_momentum.z = *(h_ff_variable+VAR_MOMENTUM+2);
+  float3 h_ff_flux_contribution_momentum_x;
+  float3 h_ff_flux_contribution_momentum_y;
+  float3 h_ff_flux_contribution_momentum_z;
+  float3 h_ff_flux_contribution_density_energy;
+  compute_flux_contribution(h_ff_variable[VAR_DENSITY], h_ff_momentum, h_ff_variable[VAR_DENSITY_ENERGY], ff_pressure, ff_velocity, h_ff_flux_contribution_momentum_x, h_ff_flux_contribution_momentum_y, h_ff_flux_contribution_momentum_z, h_ff_flux_contribution_density_energy);
+
+  // copy far field conditions to the gpu
+  //cl_mem ff_variable, ff_flux_contribution_momentum_x, ff_flux_contribution_momentum_y,ff_flux_contribution_momentum_z,  ff_flux_contribution_density_energy;
+  ff_variable = _clMalloc(NVAR*sizeof(float));
+  ff_flux_contribution_momentum_x = _clMalloc(sizeof(float3));
+  ff_flux_contribution_momentum_y = _clMalloc(sizeof(float3));
+  ff_flux_contribution_momentum_z = _clMalloc(sizeof(float3));
+  ff_flux_contribution_density_energy = _clMalloc(sizeof(float3));
+  _clMemcpyH2D(ff_variable,          h_ff_variable,          NVAR*sizeof(float));
+  _clMemcpyH2D(ff_flux_contribution_momentum_x, &h_ff_flux_contribution_momentum_x, sizeof(float3));
+  _clMemcpyH2D(ff_flux_contribution_momentum_y, &h_ff_flux_contribution_momentum_y, sizeof(float3));
+  _clMemcpyH2D(ff_flux_contribution_momentum_z, &h_ff_flux_contribution_momentum_z, sizeof(float3));
+  _clMemcpyH2D(ff_flux_contribution_density_energy, &h_ff_flux_contribution_density_energy, sizeof(float3));
+  _clFinish();
+
+  }
+  int nel;
+  int nelr;
+  // read in domain geometry
+  //float* areas;
+  //int* elements_surrounding_elements;
+  //float* normals;
+  {
+  std::ifstream file(data_file_name);
+  if(file==NULL){
+  throw(string("can not find/open file!"));
+  }
+  file >> nel;
+  nelr = block_length*((nel / block_length )+ std::min(1, nel % block_length));
+  std::cout<<"--cambine: nel="<<nel<<", nelr="<<nelr<<std::endl;
+  float* h_areas = new float[nelr];
+  int* h_elements_surrounding_elements = new int[nelr*NNB];
+  float* h_normals = new float[nelr*NDIM*NNB];
+
+
+  // read in data
+  for(int i = 0; i < nel; i++)
+  {
+  file >> h_areas[i];
+  for(int j = 0; j < NNB; j++)
+  {
+  file >> h_elements_surrounding_elements[i + j*nelr];
+  if(h_elements_surrounding_elements[i+j*nelr] < 0) h_elements_surrounding_elements[i+j*nelr] = -1;
+  h_elements_surrounding_elements[i + j*nelr]--; //it's coming in with Fortran numbering
+
+  for(int k = 0; k < NDIM; k++)
+  {
+  file >> h_normals[i + (j + k*NNB)*nelr];
+  h_normals[i + (j + k*NNB)*nelr] = -h_normals[i + (j + k*NNB)*nelr];
+  }
+  }
+  }
+
+  // fill in remaining data
+  int last = nel-1;
+  for(int i = nel; i < nelr; i++)
+  {
+  h_areas[i] = h_areas[last];
+  for(int j = 0; j < NNB; j++)
+  {
+  // duplicate the last element
+  h_elements_surrounding_elements[i + j*nelr] = h_elements_surrounding_elements[last + j*nelr];
+  for(int k = 0; k < NDIM; k++) h_normals[last + (j + k*NNB)*nelr] = h_normals[last + (j + k*NNB)*nelr];
+  }
+  }
+
+  areas = alloc<float>(nelr);
+  upload<float>(areas, h_areas, nelr);
+
+  elements_surrounding_elements = alloc<int>(nelr*NNB);
+  upload<int>(elements_surrounding_elements, h_elements_surrounding_elements, nelr*NNB);
+
+  normals = alloc<float>(nelr*NDIM*NNB);
+  upload<float>(normals, h_normals, nelr*NDIM*NNB);
+
+  delete[] h_areas;
+  delete[] h_elements_surrounding_elements;
+  delete[] h_normals;
+  }
+
+  // Create arrays and set initial conditions
+  variables = alloc<float>(nelr*NVAR);
+  int tp = 0;
+  initialize_variables(nelr, variables, ff_variable);
+  old_variables = alloc<float>(nelr*NVAR);
+  fluxes = alloc<float>(nelr*NVAR);
+  step_factors = alloc<float>(nelr);
+  // make sure all memory is floatly allocated before we start timing
+  initialize_variables(nelr, old_variables, ff_variable);
+  initialize_variables(nelr, fluxes, ff_variable);
+  _clMemset(step_factors, 0, sizeof(float)*nelr);
+  // make sure CUDA isn't still doing something before we start timing
+  _clFinish();
+  // these need to be computed the first time in order to compute time step
+  std::cout << "Starting..." << std::endl;
+
+  // Begin iterations
+  for(int i = 0; i < iterations; i++){
+  copy<float>(old_variables, variables, nelr*NVAR);
+  // for the first iteration we compute the time step
+  compute_step_factor(nelr, variables, areas, step_factors);
+  for(int j = 0; j < RK; j++){
+  compute_flux(nelr, elements_surrounding_elements, normals, variables, ff_variable, fluxes, ff_flux_contribution_density_energy, \
+  ff_flux_contribution_momentum_x, ff_flux_contribution_momentum_y, ff_flux_contribution_momentum_z);
+
+  time_step(j, nelr, old_variables, variables, step_factors, fluxes);
+  }
+  }
+  _clFinish();
+  std::cout << "Saving solution..." << std::endl;
+  dump(variables, nel, nelr);
+  std::cout << "Saved solution..." << std::endl;
+  _clStatistics();
+  std::cout << "Cleaning up..." << std::endl;
+
+  //--release resources
+  _clFree(ff_variable);
+  _clFree(ff_flux_contribution_momentum_x);
+  _clFree(ff_flux_contribution_momentum_y);
+  _clFree(ff_flux_contribution_momentum_z);
+  _clFree(ff_flux_contribution_density_energy);
+  _clFree(areas);
+  _clFree(elements_surrounding_elements);
+  _clFree(normals);
+  _clFree(variables);
+  _clFree(old_variables);
+  _clFree(fluxes);
+  _clFree(step_factors);
+  _clRelease();
+  std::cout << "Done..." << std::endl;
+  }
+  catch(string msg){
+  std::cout<<"--cambine:( an exception catched in main body ->"<<msg<<std::endl;
+  _clFree(ff_variable);
+  _clFree(ff_flux_contribution_momentum_x);
+  _clFree(ff_flux_contribution_momentum_y);
+  _clFree(ff_flux_contribution_momentum_z);
+  _clFree(ff_flux_contribution_density_energy);
+  _clFree(areas);
+  _clFree(elements_surrounding_elements);
+  _clFree(normals);
+  _clFree(variables);
+  _clFree(old_variables);
+  _clFree(fluxes);
+  _clFree(step_factors);
+  _clRelease();
+  }
+  catch(...){
+  std::cout<<"--cambine:( unknow exceptions in main body..."<<std::endl;
+  _clFree(ff_variable);
+  _clFree(ff_flux_contribution_momentum_x);
+  _clFree(ff_flux_contribution_momentum_y);
+  _clFree(ff_flux_contribution_momentum_z);
+  _clFree(ff_flux_contribution_density_energy);
+  _clFree(areas);
+  _clFree(elements_surrounding_elements);
+  _clFree(normals);
+  _clFree(variables);
+  _clFree(old_variables);
+  _clFree(fluxes);
+  _clFree(step_factors);
+  _clRelease();
+  }
+
+  return 0;
 }

+ 10 - 14
opencl/dwt2d/dwt.cpp

@@ -49,8 +49,8 @@ void samplesToChar(unsigned char * dst, float * src, int samplesNum)
 
     for(i = 0; i < samplesNum; i++) {
         float r = (src[i]+0.5f) * 255;
-        if (r > 255) r = 255; 
-        if (r < 0)   r = 0; 
+        if (r > 255) r = 255;
+        if (r < 0)   r = 0;
         dst[i] = (unsigned char)r;
     }
 }
@@ -62,7 +62,7 @@ void samplesToChar(unsigned char * dst, int * src, int samplesNum)
     for(i = 0; i < samplesNum; i++) {
         int r = src[i]+128;
         if (r > 255) r = 255;
-        if (r < 0)   r = 0; 
+        if (r < 0)   r = 0;
         dst[i] = (unsigned char)r;
     }
 }
@@ -72,24 +72,20 @@ template<typename T>
 int writeLinear(T *component_cuda, int pixWidth, int pixHeight,
                 const char * filename, const char * suffix)
 {
-	//need add segments
+  //need add segments
 
 }
-template int writeLinear<float>(float *component_cuda, int pixWidth, int pixHeight, const char * filename, const char * suffix); 
-template int writeLinear<int>(int *component_cuda, int pixWidth, int pixHeight, const char * filename, const char * suffix); 
+template int writeLinear<float>(float *component_cuda, int pixWidth, int pixHeight, const char * filename, const char * suffix);
+template int writeLinear<int>(int *component_cuda, int pixWidth, int pixHeight, const char * filename, const char * suffix);
 
 
 
 /* Write output visual ordered */
 template<typename T>
-int writeNStage2DDWT(T *component_cuda, int pixWidth, int pixHeight, 
-                     int stages, const char * filename, const char * suffix) 
+int writeNStage2DDWT(T *component_cuda, int pixWidth, int pixHeight,
+                     int stages, const char * filename, const char * suffix)
 {
     //need add segments
 }
-template int writeNStage2DDWT<float>(float *component_cuda, int pixWidth, int pixHeight, int stages, const char * filename, const char * suffix); 
-template int writeNStage2DDWT<int>(int *component_cuda, int pixWidth, int pixHeight, int stages, const char * filename, const char * suffix); 
-
-
-
-
+template int writeNStage2DDWT<float>(float *component_cuda, int pixWidth, int pixHeight, int stages, const char * filename, const char * suffix);
+template int writeNStage2DDWT<int>(int *component_cuda, int pixWidth, int pixHeight, int stages, const char * filename, const char * suffix);

+ 252 - 252
opencl/dwt2d/main.cpp

@@ -67,7 +67,7 @@ cl_context CreateContext(int platform_id, int use_gpu)
     std::vector<cl_platform_id> platformIds(platformIdCount);
     clGetPlatformIDs (platformIdCount, platformIds.data(), NULL);
 
-	// In this example, first platform is a CPU, the second one is a GPU. we just choose the first available device.
+  // In this example, first platform is a CPU, the second one is a GPU. we just choose the first available device.
     cl_context_properties contextProperties[] =
     {
         CL_CONTEXT_PLATFORM,
@@ -221,7 +221,7 @@ int getImg(char * srcFilename, unsigned char *srcImg, int inputSize)
     //read image
     int i = open(srcFilename, O_RDONLY, 0644);
     if (i == -1)
-	{
+  {
         error(0,errno,"cannot access %s", srcFilename);
         return -1;
     }
@@ -258,66 +258,66 @@ void usage() {
 void fatal_CL(cl_int error, int line_no)
 {
 
-	printf("At line %d: ", line_no);
-
-	switch(error) {
-
-		case CL_SUCCESS: 									printf("CL_SUCCESS\n"); break;
-		case CL_DEVICE_NOT_FOUND: 							printf("CL_DEVICE_NOT_FOUND\n"); break;
-		case CL_DEVICE_NOT_AVAILABLE: 						printf("CL_DEVICE_NOT_AVAILABLE\n"); break;
-		case CL_COMPILER_NOT_AVAILABLE: 					printf("CL_COMPILER_NOT_AVAILABLE\n"); break;
-		case CL_MEM_OBJECT_ALLOCATION_FAILURE: 				printf("CL_MEM_OBJECT_ALLOCATION_FAILURE\n"); break;
-		case CL_OUT_OF_RESOURCES: 							printf("CL_OUT_OF_RESOURCES\n"); break;
-		case CL_OUT_OF_HOST_MEMORY: 						printf("CL_OUT_OF_HOST_MEMORY\n"); break;
-		case CL_PROFILING_INFO_NOT_AVAILABLE: 				printf("CL_PROFILING_INFO_NOT_AVAILABLE\n"); break;
-		case CL_MEM_COPY_OVERLAP: 							printf("CL_MEM_COPY_OVERLAP\n"); break;
-		case CL_IMAGE_FORMAT_MISMATCH: 						printf("CL_IMAGE_FORMAT_MISMATCH\n"); break;
-		case CL_IMAGE_FORMAT_NOT_SUPPORTED: 				printf("CL_IMAGE_FORMAT_NOT_SUPPORTED\n"); break;
-		case CL_BUILD_PROGRAM_FAILURE: 						printf("CL_BUILD_PROGRAM_FAILURE\n"); break;
-		case CL_MAP_FAILURE: 								printf("CL_MAP_FAILURE\n"); break;
-		case CL_INVALID_VALUE: 								printf("CL_INVALID_VALUE\n"); break;
-		case CL_INVALID_DEVICE_TYPE: 						printf("CL_INVALID_DEVICE_TYPE\n"); break;
-		case CL_INVALID_PLATFORM: 							printf("CL_INVALID_PLATFORM\n"); break;
-		case CL_INVALID_DEVICE: 							printf("CL_INVALID_DEVICE\n"); break;
-		case CL_INVALID_CONTEXT: 							printf("CL_INVALID_CONTEXT\n"); break;
-		case CL_INVALID_QUEUE_PROPERTIES: 					printf("CL_INVALID_QUEUE_PROPERTIES\n"); break;
-		case CL_INVALID_COMMAND_QUEUE: 						printf("CL_INVALID_COMMAND_QUEUE\n"); break;
-		case CL_INVALID_HOST_PTR: 							printf("CL_INVALID_HOST_PTR\n"); break;
-		case CL_INVALID_MEM_OBJECT: 						printf("CL_INVALID_MEM_OBJECT\n"); break;
-		case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: 			printf("CL_INVALID_IMAGE_FORMAT_DESCRIPTOR\n"); break;
-		case CL_INVALID_IMAGE_SIZE: 						printf("CL_INVALID_IMAGE_SIZE\n"); break;
-		case CL_INVALID_SAMPLER: 							printf("CL_INVALID_SAMPLER\n"); break;
-		case CL_INVALID_BINARY: 							printf("CL_INVALID_BINARY\n"); break;
-		case CL_INVALID_BUILD_OPTIONS: 						printf("CL_INVALID_BUILD_OPTIONS\n"); break;
-		case CL_INVALID_PROGRAM: 							printf("CL_INVALID_PROGRAM\n"); break;
-		case CL_INVALID_PROGRAM_EXECUTABLE: 				printf("CL_INVALID_PROGRAM_EXECUTABLE\n"); break;
-		case CL_INVALID_KERNEL_NAME: 						printf("CL_INVALID_KERNEL_NAME\n"); break;
-		case CL_INVALID_KERNEL_DEFINITION: 					printf("CL_INVALID_KERNEL_DEFINITION\n"); break;
-		case CL_INVALID_KERNEL: 							printf("CL_INVALID_KERNEL\n"); break;
-		case CL_INVALID_ARG_INDEX: 							printf("CL_INVALID_ARG_INDEX\n"); break;
-		case CL_INVALID_ARG_VALUE: 							printf("CL_INVALID_ARG_VALUE\n"); break;
-		case CL_INVALID_ARG_SIZE: 							printf("CL_INVALID_ARG_SIZE\n"); break;
-		case CL_INVALID_KERNEL_ARGS: 						printf("CL_INVALID_KERNEL_ARGS\n"); break;
-		case CL_INVALID_WORK_DIMENSION: 					printf("CL_INVALID_WORK_DIMENSION\n"); break;
-		case CL_INVALID_WORK_GROUP_SIZE: 					printf("CL_INVALID_WORK_GROUP_SIZE\n"); break;
-		case CL_INVALID_WORK_ITEM_SIZE: 					printf("CL_INVALID_WORK_ITEM_SIZE\n"); break;
-		case CL_INVALID_GLOBAL_OFFSET: 						printf("CL_INVALID_GLOBAL_OFFSET\n"); break;
-		case CL_INVALID_EVENT_WAIT_LIST: 					printf("CL_INVALID_EVENT_WAIT_LIST\n"); break;
-		case CL_INVALID_EVENT: 								printf("CL_INVALID_EVENT\n"); break;
-		case CL_INVALID_OPERATION: 							printf("CL_INVALID_OPERATION\n"); break;
-		case CL_INVALID_GL_OBJECT: 							printf("CL_INVALID_GL_OBJECT\n"); break;
-		case CL_INVALID_BUFFER_SIZE: 						printf("CL_INVALID_BUFFER_SIZE\n"); break;
-		case CL_INVALID_MIP_LEVEL: 							printf("CL_INVALID_MIP_LEVEL\n"); break;
-		case CL_INVALID_GLOBAL_WORK_SIZE: 					printf("CL_INVALID_GLOBAL_WORK_SIZE\n"); break;
-
-		#ifdef CL_VERSION_1_1
-		case CL_MISALIGNED_SUB_BUFFER_OFFSET: 				printf("CL_MISALIGNED_SUB_BUFFER_OFFSET\n"); break;
-		case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST:	printf("CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST\n"); break;
-		#endif
-
-		default:											printf("Invalid OpenCL error code\n");
-
-	}
+  printf("At line %d: ", line_no);
+
+  switch(error) {
+
+  case CL_SUCCESS: 									printf("CL_SUCCESS\n"); break;
+  case CL_DEVICE_NOT_FOUND: 							printf("CL_DEVICE_NOT_FOUND\n"); break;
+  case CL_DEVICE_NOT_AVAILABLE: 						printf("CL_DEVICE_NOT_AVAILABLE\n"); break;
+  case CL_COMPILER_NOT_AVAILABLE: 					printf("CL_COMPILER_NOT_AVAILABLE\n"); break;
+  case CL_MEM_OBJECT_ALLOCATION_FAILURE: 				printf("CL_MEM_OBJECT_ALLOCATION_FAILURE\n"); break;
+  case CL_OUT_OF_RESOURCES: 							printf("CL_OUT_OF_RESOURCES\n"); break;
+  case CL_OUT_OF_HOST_MEMORY: 						printf("CL_OUT_OF_HOST_MEMORY\n"); break;
+  case CL_PROFILING_INFO_NOT_AVAILABLE: 				printf("CL_PROFILING_INFO_NOT_AVAILABLE\n"); break;
+  case CL_MEM_COPY_OVERLAP: 							printf("CL_MEM_COPY_OVERLAP\n"); break;
+  case CL_IMAGE_FORMAT_MISMATCH: 						printf("CL_IMAGE_FORMAT_MISMATCH\n"); break;
+  case CL_IMAGE_FORMAT_NOT_SUPPORTED: 				printf("CL_IMAGE_FORMAT_NOT_SUPPORTED\n"); break;
+  case CL_BUILD_PROGRAM_FAILURE: 						printf("CL_BUILD_PROGRAM_FAILURE\n"); break;
+  case CL_MAP_FAILURE: 								printf("CL_MAP_FAILURE\n"); break;
+  case CL_INVALID_VALUE: 								printf("CL_INVALID_VALUE\n"); break;
+  case CL_INVALID_DEVICE_TYPE: 						printf("CL_INVALID_DEVICE_TYPE\n"); break;
+  case CL_INVALID_PLATFORM: 							printf("CL_INVALID_PLATFORM\n"); break;
+  case CL_INVALID_DEVICE: 							printf("CL_INVALID_DEVICE\n"); break;
+  case CL_INVALID_CONTEXT: 							printf("CL_INVALID_CONTEXT\n"); break;
+  case CL_INVALID_QUEUE_PROPERTIES: 					printf("CL_INVALID_QUEUE_PROPERTIES\n"); break;
+  case CL_INVALID_COMMAND_QUEUE: 						printf("CL_INVALID_COMMAND_QUEUE\n"); break;
+  case CL_INVALID_HOST_PTR: 							printf("CL_INVALID_HOST_PTR\n"); break;
+  case CL_INVALID_MEM_OBJECT: 						printf("CL_INVALID_MEM_OBJECT\n"); break;
+  case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: 			printf("CL_INVALID_IMAGE_FORMAT_DESCRIPTOR\n"); break;
+  case CL_INVALID_IMAGE_SIZE: 						printf("CL_INVALID_IMAGE_SIZE\n"); break;
+  case CL_INVALID_SAMPLER: 							printf("CL_INVALID_SAMPLER\n"); break;
+  case CL_INVALID_BINARY: 							printf("CL_INVALID_BINARY\n"); break;
+  case CL_INVALID_BUILD_OPTIONS: 						printf("CL_INVALID_BUILD_OPTIONS\n"); break;
+  case CL_INVALID_PROGRAM: 							printf("CL_INVALID_PROGRAM\n"); break;
+  case CL_INVALID_PROGRAM_EXECUTABLE: 				printf("CL_INVALID_PROGRAM_EXECUTABLE\n"); break;
+  case CL_INVALID_KERNEL_NAME: 						printf("CL_INVALID_KERNEL_NAME\n"); break;
+  case CL_INVALID_KERNEL_DEFINITION: 					printf("CL_INVALID_KERNEL_DEFINITION\n"); break;
+  case CL_INVALID_KERNEL: 							printf("CL_INVALID_KERNEL\n"); break;
+  case CL_INVALID_ARG_INDEX: 							printf("CL_INVALID_ARG_INDEX\n"); break;
+  case CL_INVALID_ARG_VALUE: 							printf("CL_INVALID_ARG_VALUE\n"); break;
+  case CL_INVALID_ARG_SIZE: 							printf("CL_INVALID_ARG_SIZE\n"); break;
+  case CL_INVALID_KERNEL_ARGS: 						printf("CL_INVALID_KERNEL_ARGS\n"); break;
+  case CL_INVALID_WORK_DIMENSION: 					printf("CL_INVALID_WORK_DIMENSION\n"); break;
+  case CL_INVALID_WORK_GROUP_SIZE: 					printf("CL_INVALID_WORK_GROUP_SIZE\n"); break;
+  case CL_INVALID_WORK_ITEM_SIZE: 					printf("CL_INVALID_WORK_ITEM_SIZE\n"); break;
+  case CL_INVALID_GLOBAL_OFFSET: 						printf("CL_INVALID_GLOBAL_OFFSET\n"); break;
+  case CL_INVALID_EVENT_WAIT_LIST: 					printf("CL_INVALID_EVENT_WAIT_LIST\n"); break;
+  case CL_INVALID_EVENT: 								printf("CL_INVALID_EVENT\n"); break;
+  case CL_INVALID_OPERATION: 							printf("CL_INVALID_OPERATION\n"); break;
+  case CL_INVALID_GL_OBJECT: 							printf("CL_INVALID_GL_OBJECT\n"); break;
+  case CL_INVALID_BUFFER_SIZE: 						printf("CL_INVALID_BUFFER_SIZE\n"); break;
+  case CL_INVALID_MIP_LEVEL: 							printf("CL_INVALID_MIP_LEVEL\n"); break;
+  case CL_INVALID_GLOBAL_WORK_SIZE: 					printf("CL_INVALID_GLOBAL_WORK_SIZE\n"); break;
+
+  #ifdef CL_VERSION_1_1
+  case CL_MISALIGNED_SUB_BUFFER_OFFSET: 				printf("CL_MISALIGNED_SUB_BUFFER_OFFSET\n"); break;
+  case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST:	printf("CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST\n"); break;
+  #endif
+
+  default:											printf("Invalid OpenCL error code\n");
+
+  }
 }
 
 
@@ -330,26 +330,26 @@ void rgbToComponents(cl_mem d_r, cl_mem d_g, cl_mem d_b, unsigned char * h_src,
     int pixels      = width * height;
     int alignedSize =  DIVANDRND(width*height, THREADS) * THREADS * 3; //aligned to thread block size -- THREADS
 
-	cl_mem cl_d_src;
-	cl_d_src = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, pixels*3, h_src, &errNum);
-	// fatal_CL(errNum, __LINE__);
+  cl_mem cl_d_src;
+  cl_d_src = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, pixels*3, h_src, &errNum);
+  // fatal_CL(errNum, __LINE__);
 
-	size_t globalWorkSize[1] = { alignedSize/3};
+  size_t globalWorkSize[1] = { alignedSize/3};
     size_t localWorkSize[1] = { THREADS };
 
-	errNum  = clSetKernelArg(c_CopySrcToComponents, 0, sizeof(cl_mem), &d_r);
-	errNum |= clSetKernelArg(c_CopySrcToComponents, 1, sizeof(cl_mem), &d_g);
-	errNum |= clSetKernelArg(c_CopySrcToComponents, 2, sizeof(cl_mem), &d_b);
-	errNum |= clSetKernelArg(c_CopySrcToComponents, 3, sizeof(cl_mem), &cl_d_src);
-	errNum |= clSetKernelArg(c_CopySrcToComponents, 4, sizeof(int), &pixels);
-	// fatal_CL(errNum, __LINE__);
+  errNum  = clSetKernelArg(c_CopySrcToComponents, 0, sizeof(cl_mem), &d_r);
+  errNum |= clSetKernelArg(c_CopySrcToComponents, 1, sizeof(cl_mem), &d_g);
+  errNum |= clSetKernelArg(c_CopySrcToComponents, 2, sizeof(cl_mem), &d_b);
+  errNum |= clSetKernelArg(c_CopySrcToComponents, 3, sizeof(cl_mem), &cl_d_src);
+  errNum |= clSetKernelArg(c_CopySrcToComponents, 4, sizeof(int), &pixels);
+  // fatal_CL(errNum, __LINE__);
 
-	errNum = clEnqueueNDRangeKernel(commandQueue, c_CopySrcToComponents, 1, NULL, globalWorkSize, localWorkSize, 0, NULL, NULL);
-	// fatal_CL(errNum, __LINE__);
+  errNum = clEnqueueNDRangeKernel(commandQueue, c_CopySrcToComponents, 1, NULL, globalWorkSize, localWorkSize, 0, NULL, NULL);
+  // fatal_CL(errNum, __LINE__);
 
     // Free Memory
-	errNum = clReleaseMemObject(cl_d_src);
-	// fatal_CL(errNum, __LINE__);
+  errNum = clReleaseMemObject(cl_d_src);
+  // fatal_CL(errNum, __LINE__);
 }
 
 
@@ -359,30 +359,30 @@ void rgbToComponents(cl_mem d_r, cl_mem d_g, cl_mem d_b, unsigned char * h_src,
 //in file components.cu
 void bwToComponent(cl_mem d_c, unsigned char * h_src, int width, int height)
 {
-	cl_mem cl_d_src;
+  cl_mem cl_d_src;
     int pixels      = width*height;
     int alignedSize =  DIVANDRND(pixels, THREADS) * THREADS;
 
-	cl_d_src = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, pixels, h_src, NULL);
-	// fatal_CL(errNum, __LINE__);
+  cl_d_src = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, pixels, h_src, NULL);
+  // fatal_CL(errNum, __LINE__);
 
-	size_t globalWorkSize[1] = { alignedSize/9};
+  size_t globalWorkSize[1] = { alignedSize/9};
     size_t localWorkSize[1] = { THREADS };
-	assert(alignedSize%(THREADS*3) == 0);
+  assert(alignedSize%(THREADS*3) == 0);
 
-	errNum  = clSetKernelArg(c_CopySrcToComponent, 0, sizeof(cl_mem), &d_c);
-	errNum |= clSetKernelArg(c_CopySrcToComponent, 1, sizeof(cl_mem), &cl_d_src);
-	errNum |= clSetKernelArg(c_CopySrcToComponent, 2, sizeof(int), &pixels);
-	// fatal_CL(errNum, __LINE__);
+  errNum  = clSetKernelArg(c_CopySrcToComponent, 0, sizeof(cl_mem), &d_c);
+  errNum |= clSetKernelArg(c_CopySrcToComponent, 1, sizeof(cl_mem), &cl_d_src);
+  errNum |= clSetKernelArg(c_CopySrcToComponent, 2, sizeof(int), &pixels);
+  // fatal_CL(errNum, __LINE__);
 
-	errNum = clEnqueueNDRangeKernel(commandQueue, c_CopySrcToComponent, 1, NULL, globalWorkSize, localWorkSize, 0, NULL, NULL);
-	std::cout<<"in function bwToComponent errNum= "<<errNum<<"\n";
-	// fatal_CL(errNum, __LINE__);
-	std::cout<<"bwToComponent has finished\n";
+  errNum = clEnqueueNDRangeKernel(commandQueue, c_CopySrcToComponent, 1, NULL, globalWorkSize, localWorkSize, 0, NULL, NULL);
+  std::cout<<"in function bwToComponent errNum= "<<errNum<<"\n";
+  // fatal_CL(errNum, __LINE__);
+  std::cout<<"bwToComponent has finished\n";
 
     // Free Memory
-	errNum = clReleaseMemObject(cl_d_src);
-	// fatal_CL(errNum, __LINE__);
+  errNum = clReleaseMemObject(cl_d_src);
+  // fatal_CL(errNum, __LINE__);
 
 }
 
@@ -399,33 +399,33 @@ void bwToComponent(cl_mem d_c, unsigned char * h_src, int width, int height)
 void launchFDWT53Kernel (int WIN_SX, int WIN_SY, cl_mem in, cl_mem out, int sx, int sy)
 {
     // compute optimal number of steps of each sliding window
-	// cuda_dwt called a function divRndUp from namespace cuda_gwt. this function takes n and d, "return (n / d) + ((n % d) ? 1 : 0);"
-	//
+  // cuda_dwt called a function divRndUp from namespace cuda_gwt. this function takes n and d, "return (n / d) + ((n % d) ? 1 : 0);"
+  //
 
     const int steps = ( sy/ (15 * WIN_SY)) + ((sy % (15 * WIN_SY)) ? 1 : 0);
 
-	int gx = ( sx/ WIN_SX) + ((sx %  WIN_SX) ? 1 : 0);  //use function divRndUp(n, d){return (n / d) + ((n % d) ? 1 : 0);}
-	int gy = ( sy/ (WIN_SY*steps)) + ((sy %  (WIN_SY*steps)) ? 1 : 0);
+  int gx = ( sx/ WIN_SX) + ((sx %  WIN_SX) ? 1 : 0);  //use function divRndUp(n, d){return (n / d) + ((n % d) ? 1 : 0);}
+  int gy = ( sy/ (WIN_SY*steps)) + ((sy %  (WIN_SY*steps)) ? 1 : 0);
 
-	printf("sliding steps = %d , gx = %d , gy = %d \n", steps, gx, gy);
+  printf("sliding steps = %d , gx = %d , gy = %d \n", steps, gx, gy);
 
     // prepare grid size
-	size_t globalWorkSize[2] = { gx*WIN_SX, gy*1};
+  size_t globalWorkSize[2] = { gx*WIN_SX, gy*1};
     size_t localWorkSize[2]  = { WIN_SX , 1};
     // printf("\n globalx=%d, globaly=%d, blocksize=%d\n", gx, gy, WIN_SX);
 
-	errNum  = clSetKernelArg(kl_fdwt53Kernel, 0, sizeof(cl_mem), &in);
-	errNum |= clSetKernelArg(kl_fdwt53Kernel, 1, sizeof(cl_mem), &out);
-	errNum |= clSetKernelArg(kl_fdwt53Kernel, 2, sizeof(int), &sx);
-	errNum |= clSetKernelArg(kl_fdwt53Kernel, 3, sizeof(int), &sy);
-	errNum |= clSetKernelArg(kl_fdwt53Kernel, 4, sizeof(int), &steps);
-	errNum |= clSetKernelArg(kl_fdwt53Kernel, 5, sizeof(int), &WIN_SX);
-	errNum |= clSetKernelArg(kl_fdwt53Kernel, 6, sizeof(int), &WIN_SY);
-	// fatal_CL(errNum, __LINE__);
+  errNum  = clSetKernelArg(kl_fdwt53Kernel, 0, sizeof(cl_mem), &in);
+  errNum |= clSetKernelArg(kl_fdwt53Kernel, 1, sizeof(cl_mem), &out);
+  errNum |= clSetKernelArg(kl_fdwt53Kernel, 2, sizeof(int), &sx);
+  errNum |= clSetKernelArg(kl_fdwt53Kernel, 3, sizeof(int), &sy);
+  errNum |= clSetKernelArg(kl_fdwt53Kernel, 4, sizeof(int), &steps);
+  errNum |= clSetKernelArg(kl_fdwt53Kernel, 5, sizeof(int), &WIN_SX);
+  errNum |= clSetKernelArg(kl_fdwt53Kernel, 6, sizeof(int), &WIN_SY);
+  // fatal_CL(errNum, __LINE__);
 
-	errNum = clEnqueueNDRangeKernel(commandQueue, kl_fdwt53Kernel, 2, NULL, globalWorkSize, localWorkSize, 0, NULL, NULL);
-	// fatal_CL(errNum, __LINE__);
-	printf("kl_fdwt53Kernel in launchFDW53Kernel has finished\n");
+  errNum = clEnqueueNDRangeKernel(commandQueue, kl_fdwt53Kernel, 2, NULL, globalWorkSize, localWorkSize, 0, NULL, NULL);
+  // fatal_CL(errNum, __LINE__);
+  printf("kl_fdwt53Kernel in launchFDW53Kernel has finished\n");
 
 
 }
@@ -439,8 +439,8 @@ void launchFDWT53Kernel (int WIN_SX, int WIN_SY, cl_mem in, cl_mem out, int sx,
 ///from /cuda_gwt/common.h/namespace
 void memCopy (cl_mem dest,  cl_mem src, const size_t sx, const size_t sy){
 
-	errNum = clEnqueueCopyBuffer (commandQueue, src, dest, 0, 0, sx*sy*sizeof(int), 0, NULL, NULL);
-	// fatal_CL (errNum, __LINE__);
+  errNum = clEnqueueCopyBuffer (commandQueue, src, dest, 0, 0, sx*sy*sizeof(int), 0, NULL, NULL);
+  // fatal_CL (errNum, __LINE__);
 
 }
 
@@ -460,28 +460,28 @@ void fdwt53(cl_mem in, cl_mem out, int sizeX, int sizeY, int levels)
     // select right width of kernel for the size of the image
 
     if(sizeX >= 960)
-	{
+  {
       launchFDWT53Kernel(192, 8, in, out, sizeX, sizeY);
     }
-	else if (sizeX >= 480)
-	{
+  else if (sizeX >= 480)
+  {
       launchFDWT53Kernel(128, 8, in, out, sizeX, sizeY);
     } else
-	{
+  {
       launchFDWT53Kernel(64, 8, in, out, sizeX, sizeY);
-	}
-
-	// if this was not the last level, continue recursively with other levels
-	if (levels > 1)
-	{
-		// copy output's LL band back into input buffer
-		const int llSizeX = (sizeX / 2) + ((sizeX % 2) ? 1 :0);
-		const int llSizeY = (sizeY / 2) + ((sizeY % 2) ? 1 :0);
-		memCopy(in, out, llSizeX, llSizeY);
-
-		// run remaining levels of FDWT
-		fdwt53(in, out, llSizeX, llSizeY, levels - 1);
-	}
+  }
+
+  // if this was not the last level, continue recursively with other levels
+  if (levels > 1)
+  {
+  // copy output's LL band back into input buffer
+  const int llSizeX = (sizeX / 2) + ((sizeX % 2) ? 1 :0);
+  const int llSizeY = (sizeY / 2) + ((sizeY % 2) ? 1 :0);
+  memCopy(in, out, llSizeX, llSizeY);
+
+  // run remaining levels of FDWT
+  fdwt53(in, out, llSizeX, llSizeY, levels - 1);
+  }
 }
 
 
@@ -494,15 +494,15 @@ int nStage2dDWT(cl_mem in, cl_mem out, cl_mem backup, int pixWidth, int pixHeigh
     // create backup of input, because each test iteration overwrites it
     const int size = pixHeight * pixWidth * sizeof(int);
 
-	// Measure time of individual levels.
-	if (forward)
-		fdwt53(in, out, pixWidth, pixHeight, stages );
-	//else
-	//	rdwt(in, out, pixWidth, pixHeight, stages);
-	// rdwt means rdwt53(can be found in file rdwt53.cu) which has not been defined
+  // Measure time of individual levels.
+  if (forward)
+  fdwt53(in, out, pixWidth, pixHeight, stages );
+  //else
+  //	rdwt(in, out, pixWidth, pixHeight, stages);
+  // rdwt means rdwt53(can be found in file rdwt53.cu) which has not been defined
 
 
-	return 0;
+  return 0;
 }
 
 
@@ -514,7 +514,7 @@ void samplesToChar(unsigned char * dst, int * src, int samplesNum)
     int i;
 
     for(i = 0; i < samplesNum; i++)
-	{
+  {
         int r = src[i]+128;
         if (r > 255) r = 255;
         if (r < 0)   r = 0;
@@ -529,38 +529,38 @@ void samplesToChar(unsigned char * dst, int * src, int samplesNum)
 /// Write output linear orderd
 int writeLinear(cl_mem component, int pixWidth, int pixHeight, const char * filename, const char * suffix)
 {
-	unsigned char * result;
+  unsigned char * result;
     int *gpu_output;
     int i;
     int size;
     int samplesNum = pixWidth*pixHeight;
 
-	size = samplesNum*sizeof(int);
-	gpu_output = (int *)malloc(size);
+  size = samplesNum*sizeof(int);
+  gpu_output = (int *)malloc(size);
     memset(gpu_output, 0, size);
-	result = (unsigned char *)malloc(samplesNum);
+  result = (unsigned char *)malloc(samplesNum);
 
-	errNum = clEnqueueReadBuffer(commandQueue, component, CL_TRUE, 0, size, gpu_output, 0, NULL, NULL);
-	// fatal_CL(errNum, __LINE__);
+  errNum = clEnqueueReadBuffer(commandQueue, component, CL_TRUE, 0, size, gpu_output, 0, NULL, NULL);
+  // fatal_CL(errNum, __LINE__);
 
-	// T to char
-	samplesToChar(result, gpu_output, samplesNum);
+  // T to char
+  samplesToChar(result, gpu_output, samplesNum);
 
-	// Write component
-	char outfile[strlen(filename)+strlen(suffix)];
+  // Write component
+  char outfile[strlen(filename)+strlen(suffix)];
     strcpy(outfile, filename);
     strcpy(outfile+strlen(filename), suffix);
     i = open(outfile, O_CREAT|O_WRONLY, 0644);
-	if (i == -1)
-	{
+  if (i == -1)
+  {
         error(0,errno,"cannot access %s", outfile);
         return -1;
     }
-	printf("\nWriting to %s (%d x %d)\n", outfile, pixWidth, pixHeight);
+  printf("\nWriting to %s (%d x %d)\n", outfile, pixWidth, pixHeight);
     write(i, result, samplesNum);
     close(i);
 
-	// Clean up
+  // Clean up
     free(gpu_output);
     free(result);
 
@@ -574,7 +574,7 @@ int writeLinear(cl_mem component, int pixWidth, int pixHeight, const char * file
 //in file dwt.cu
 int writeNStage2DDWT(cl_mem component, int pixWidth, int pixHeight, int stages, const char * filename, const char * suffix)
 {
-	struct band {
+  struct band {
         int dimX;
         int dimY;
     };
@@ -587,7 +587,7 @@ int writeNStage2DDWT(cl_mem component, int pixWidth, int pixHeight, int stages,
 
     unsigned char * result;
     int *src;
-	int	*dst;
+  int	*dst;
     int i,s;
     int size;
     int offset;
@@ -595,7 +595,7 @@ int writeNStage2DDWT(cl_mem component, int pixWidth, int pixHeight, int stages,
     int samplesNum = pixWidth*pixHeight;
     struct dimensions * bandDims;
 
-	bandDims = (struct dimensions *)malloc(stages * sizeof(struct dimensions));
+  bandDims = (struct dimensions *)malloc(stages * sizeof(struct dimensions));
 
     bandDims[0].LL.dimX = DIVANDRND(pixWidth,2);
     bandDims[0].LL.dimY = DIVANDRND(pixHeight,2);
@@ -606,8 +606,8 @@ int writeNStage2DDWT(cl_mem component, int pixWidth, int pixHeight, int stages,
     bandDims[0].HH.dimX = bandDims[0].HL.dimX;
     bandDims[0].HH.dimY = bandDims[0].LH.dimY;
 
-	for (i = 1; i < stages; i++)
-	{
+  for (i = 1; i < stages; i++)
+  {
         bandDims[i].LL.dimX = DIVANDRND(bandDims[i-1].LL.dimX,2);
         bandDims[i].LL.dimY = DIVANDRND(bandDims[i-1].LL.dimY,2);
         bandDims[i].HL.dimX = bandDims[i-1].LL.dimX - bandDims[i].LL.dimX;
@@ -621,7 +621,7 @@ int writeNStage2DDWT(cl_mem component, int pixWidth, int pixHeight, int stages,
 #if 0
     printf("Original image pixWidth x pixHeight: %d x %d\n", pixWidth, pixHeight);
     for (i = 0; i < stages; i++)
-	{
+  {
         printf("Stage %d: LL: pixWidth x pixHeight: %d x %d\n", i, bandDims[i].LL.dimX, bandDims[i].LL.dimY);
         printf("Stage %d: HL: pixWidth x pixHeight: %d x %d\n", i, bandDims[i].HL.dimX, bandDims[i].HL.dimY);
         printf("Stage %d: LH: pixWidth x pixHeight: %d x %d\n", i, bandDims[i].LH.dimX, bandDims[i].LH.dimY);
@@ -629,69 +629,69 @@ int writeNStage2DDWT(cl_mem component, int pixWidth, int pixHeight, int stages,
     }
 #endif
 
-	size = samplesNum*sizeof(int);
+  size = samplesNum*sizeof(int);
 
-	src = (int *)malloc(size);
+  src = (int *)malloc(size);
     memset(src, 0, size);
-	dst = (int *)malloc(size);
+  dst = (int *)malloc(size);
     memset(dst, 0, size);
-	result = (unsigned char *)malloc(samplesNum);
+  result = (unsigned char *)malloc(samplesNum);
 
-	errNum = clEnqueueReadBuffer(commandQueue, component, CL_TRUE, 0, size, src, 0, NULL, NULL);
-	// fatal_CL(errNum, __LINE__);
+  errNum = clEnqueueReadBuffer(commandQueue, component, CL_TRUE, 0, size, src, 0, NULL, NULL);
+  // fatal_CL(errNum, __LINE__);
 
 
-	// LL Band
-	size = bandDims[stages-1].LL.dimX * sizeof(int);
-	for (i = 0; i < bandDims[stages-1].LL.dimY; i++)
-	{
+  // LL Band
+  size = bandDims[stages-1].LL.dimX * sizeof(int);
+  for (i = 0; i < bandDims[stages-1].LL.dimY; i++)
+  {
         memcpy(dst+i*pixWidth, src+i*bandDims[stages-1].LL.dimX, size);
-	}
+  }
 
     for (s = stages - 1; s >= 0; s--) {
         // HL Band
         size = bandDims[s].HL.dimX * sizeof(int);
         offset = bandDims[s].LL.dimX * bandDims[s].LL.dimY;
         for (i = 0; i < bandDims[s].HL.dimY; i++)
-		{
+  {
             memcpy(dst+i*pixWidth+bandDims[s].LL.dimX,
                 src+offset+i*bandDims[s].HL.dimX,
                 size);
         }
 
         // LH band
-		size = bandDims[s].LH.dimX * sizeof(int);
+  size = bandDims[s].LH.dimX * sizeof(int);
         offset += bandDims[s].HL.dimX * bandDims[s].HL.dimY;
         yOffset = bandDims[s].LL.dimY;
         for (i = 0; i < bandDims[s].HL.dimY; i++)
-		{
+  {
             memcpy(dst+(yOffset+i)*pixWidth,
                 src+offset+i*bandDims[s].LH.dimX,
                 size);
         }
 
-		//HH band
+  //HH band
         size = bandDims[s].HH.dimX * sizeof(int);
         offset += bandDims[s].LH.dimX * bandDims[s].LH.dimY;
         yOffset = bandDims[s].HL.dimY;
         for (i = 0; i < bandDims[s].HH.dimY; i++)
-		{
+  {
             memcpy(dst+(yOffset+i)*pixWidth+bandDims[s].LH.dimX,
                 src+offset+i*bandDims[s].HH.dimX,
                 size);
         }
-	}
+  }
 
     // Write component
-	samplesToChar(result, dst, samplesNum);
+  samplesToChar(result, dst, samplesNum);
 
-	char outfile[strlen(filename)+strlen(suffix)];
+  char outfile[strlen(filename)+strlen(suffix)];
     strcpy(outfile, filename);
     strcpy(outfile+strlen(filename), suffix);
     i = open(outfile, O_CREAT|O_WRONLY, 0644);
 
     if (i == -1)
-	{
+  {
         error(0,errno,"cannot access %s", outfile);
         return -1;
     }
@@ -700,12 +700,12 @@ int writeNStage2DDWT(cl_mem component, int pixWidth, int pixHeight, int stages,
     write(i, result, samplesNum);
     close(i);
 
-	free(src);
-	free(dst);
+  free(src);
+  free(dst);
     free(result);
     free(bandDims);
 
-	return 0;
+  return 0;
 }
 
 
@@ -718,42 +718,42 @@ template <typename T>
 void processDWT(struct dwt *d, int forward, int writeVisual)
 {
 
-	int componentSize = d->pixWidth * d->pixHeight * sizeof(T);
+  int componentSize = d->pixWidth * d->pixHeight * sizeof(T);
 
     T *c_r_out, *c_g_out, *c_b_out, *backup, *c_r, *c_g, *c_b;
 
-	// initialize to zeros
-	T *temp = (T *)malloc(componentSize);
-	memset(temp, 0, componentSize);
+  // initialize to zeros
+  T *temp = (T *)malloc(componentSize);
+  memset(temp, 0, componentSize);
 
-	cl_mem cl_c_r_out;
-	cl_c_r_out = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, componentSize, temp, &errNum);
-	// fatal_CL(errNum, __LINE__);
+  cl_mem cl_c_r_out;
+  cl_c_r_out = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, componentSize, temp, &errNum);
+  // fatal_CL(errNum, __LINE__);
 
 
 
-	cl_mem cl_backup;
-	cl_backup  = clCreateBuffer(context, CL_MEM_READ_WRITE |CL_MEM_COPY_HOST_PTR, componentSize, temp, &errNum);
-	// fatal_CL(errNum, __LINE__);
+  cl_mem cl_backup;
+  cl_backup  = clCreateBuffer(context, CL_MEM_READ_WRITE |CL_MEM_COPY_HOST_PTR, componentSize, temp, &errNum);
+  // fatal_CL(errNum, __LINE__);
 
-	if (d->components == 3) {
-		// Alloc two more buffers for G and B
-		cl_mem cl_c_g_out;
-		cl_c_g_out = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, componentSize, temp, &errNum);
-		// fatal_CL(errNum, __LINE__);
+  if (d->components == 3) {
+  // Alloc two more buffers for G and B
+  cl_mem cl_c_g_out;
+  cl_c_g_out = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, componentSize, temp, &errNum);
+  // fatal_CL(errNum, __LINE__);
 
-		cl_mem cl_c_b_out;
-		cl_c_b_out = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, componentSize, temp, &errNum);
+  cl_mem cl_c_b_out;
+  cl_c_b_out = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, componentSize, temp, &errNum);
         // fatal_CL(errNum, __LINE__);
 
         // Load components
         cl_mem cl_c_r, cl_c_g, cl_c_b;
-		cl_c_r = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,componentSize, temp, &errNum);
-		// fatal_CL(errNum, __LINE__);
-		cl_c_g = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,componentSize, temp, &errNum);
-		// fatal_CL(errNum, __LINE__);
-		cl_c_b = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,componentSize, temp, &errNum);
-		// fatal_CL(errNum, __LINE__);
+  cl_c_r = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,componentSize, temp, &errNum);
+  // fatal_CL(errNum, __LINE__);
+  cl_c_g = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,componentSize, temp, &errNum);
+  // fatal_CL(errNum, __LINE__);
+  cl_c_b = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,componentSize, temp, &errNum);
+  // fatal_CL(errNum, __LINE__);
 
 
         rgbToComponents(cl_c_r, cl_c_g, cl_c_b, d->srcImg, d->pixWidth, d->pixHeight);
@@ -768,12 +768,12 @@ void processDWT(struct dwt *d, int forward, int writeVisual)
 
         // ---------test----------
 /*      T *h_r_out=(T*)malloc(componentSize);
-		errNum = clEnqueueReadBuffer(commandQueue, cl_c_g_out, CL_TRUE, 0, componentSize, h_r_out, 0, NULL, NULL);
-		fatal_CL(errNum, __LINE__);
+  errNum = clEnqueueReadBuffer(commandQueue, cl_c_g_out, CL_TRUE, 0, componentSize, h_r_out, 0, NULL, NULL);
+  fatal_CL(errNum, __LINE__);
         int ii;
-		for(ii=0;ii<componentSize/sizeof(T);ii++) {
-			fprintf(stderr, "%d ", (int)h_r_out[ii]);
-			if((ii+1) % (d->pixWidth) == 0) fprintf(stderr, "\n");
+  for(ii=0;ii<componentSize/sizeof(T);ii++) {
+  fprintf(stderr, "%d ", (int)h_r_out[ii]);
+  if((ii+1) % (d->pixWidth) == 0) fprintf(stderr, "\n");
         }
 */        // ---------test----------
 
@@ -791,17 +791,17 @@ void processDWT(struct dwt *d, int forward, int writeVisual)
         }
 #endif
 
-		clReleaseMemObject(cl_c_r);
-		clReleaseMemObject(cl_c_g);
-		clReleaseMemObject(cl_c_b);
-		clReleaseMemObject(cl_c_g_out);
-		clReleaseMemObject(cl_c_b_out);
+  clReleaseMemObject(cl_c_r);
+  clReleaseMemObject(cl_c_g);
+  clReleaseMemObject(cl_c_b);
+  clReleaseMemObject(cl_c_g_out);
+  clReleaseMemObject(cl_c_b_out);
 
-	} else if(d->components == 1) {
+  } else if(d->components == 1) {
         // Load components
         cl_mem cl_c_r;
-		cl_c_r = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,componentSize, temp, &errNum);
-		// fatal_CL(errNum, __LINE__);
+  cl_c_r = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,componentSize, temp, &errNum);
+  // fatal_CL(errNum, __LINE__);
 
         bwToComponent(cl_c_r, d->srcImg, d->pixWidth, d->pixHeight);
 
@@ -816,12 +816,12 @@ void processDWT(struct dwt *d, int forward, int writeVisual)
         }
 
 
-		clReleaseMemObject(cl_c_r);
+  clReleaseMemObject(cl_c_r);
 
     }
 
-	free(temp);
-	clReleaseMemObject(cl_c_r_out);
+  free(temp);
+  clReleaseMemObject(cl_c_r_out);
 }
 
 
@@ -831,7 +831,7 @@ int main(int argc, char **argv)
     int optindex = 0;
     signed char ch;
     struct option longopts[] =
-	{
+  {
         {"dimension",   required_argument, 0, 'd'}, //dimensions of src img
         {"components",  required_argument, 0, 'c'}, //numger of components of src img
         {"depth",       required_argument, 0, 'b'}, //bit depth of src img
@@ -863,13 +863,13 @@ int main(int argc, char **argv)
     int use_gpu     = 0;
 
     while ((ch = getopt_long(argc, argv, "d:c:b:l:D:fr95whp:g:i:", longopts, &optindex)) != -1)
-	{
+  {
         switch (ch) {
         case 'd':
             pixWidth = atoi(optarg);
             pos = strstr(optarg, "x");
             if (pos == NULL || pixWidth == 0 || (strlen(pos) >= strlen(optarg)))
-			{
+  {
                 usage();
                 return -1;
             }
@@ -921,33 +921,33 @@ int main(int argc, char **argv)
             return -1;
         }
     }
-	argc -= optind;
-	argv += optind;
+  argc -= optind;
+  argv += optind;
 
     if (argc == 0)
-	{ // at least one filename is expected
+  { // at least one filename is expected
         printf("Please supply src file name\n");
         usage();
         return -1;
     }
 
     if (pixWidth <= 0 || pixHeight <=0)
-	{
+  {
         printf("Wrong or missing dimensions\n");
         usage();
         return -1;
     }
 
     if (forward == 0)
-	{
+  {
         writeVisual = 0; //do not write visual when RDWT
     }
 
 
 
-	//
-	// device init
-	// Create an OpenCL context on first available platform
+  //
+  // device init
+  // Create an OpenCL context on first available platform
     context = CreateContext(platform_id, use_gpu);
     if (context == NULL)
     {
@@ -964,36 +964,36 @@ int main(int argc, char **argv)
         return 1;
     }
 
-	// Create OpenCL program from com_dwt.cl kernel source
-	program = CreateProgram(context, cldevice, "com_dwt.cl");
+  // Create OpenCL program from com_dwt.cl kernel source
+  program = CreateProgram(context, cldevice, "com_dwt.cl");
     if (program == NULL)
     {
         printf("fail to create program!!\n");
     }
 
-	// Create OpenCL kernel
-	c_CopySrcToComponents = clCreateKernel(program, "c_CopySrcToComponents", NULL);
-	if (c_CopySrcToComponents == NULL)
+  // Create OpenCL kernel
+  c_CopySrcToComponents = clCreateKernel(program, "c_CopySrcToComponents", NULL);
+  if (c_CopySrcToComponents == NULL)
     {
         std::cerr << "Failed to create kernel" << std::endl;
     }
 
-	c_CopySrcToComponent = clCreateKernel(program, "c_CopySrcToComponent", NULL);
-	if (c_CopySrcToComponent == NULL)
+  c_CopySrcToComponent = clCreateKernel(program, "c_CopySrcToComponent", NULL);
+  if (c_CopySrcToComponent == NULL)
     {
         std::cerr << "Failed to create kernel" << std::endl;
     }
 
-	kl_fdwt53Kernel = clCreateKernel(program, "cl_fdwt53Kernel", NULL);
+  kl_fdwt53Kernel = clCreateKernel(program, "cl_fdwt53Kernel", NULL);
     if (kl_fdwt53Kernel == NULL)
-	{
-		std::cerr<<"Failed to create kernel\n";
-	}
+  {
+  std::cerr<<"Failed to create kernel\n";
+  }
 
 
 
-	//initialize struct dwt
-	struct dwt *d;
+  //initialize struct dwt
+  struct dwt *d;
     d = (struct dwt *)malloc(sizeof(struct dwt));
     d->srcImg = NULL;
     d->pixWidth = pixWidth;
@@ -1001,11 +1001,11 @@ int main(int argc, char **argv)
     d->components = compCount;
     d->dwtLvls  = dwtLvls;
 
-	// file names
+  // file names
     d->srcFilename = (char *)malloc(strlen(argv[0]));
     strcpy(d->srcFilename, argv[0]);
     if (argc == 1)
-	{ // only one filename supplyed
+  { // only one filename supplyed
         d->outFilename = (char *)malloc(strlen(d->srcFilename)+4);
         strcpy(d->outFilename, d->srcFilename);
         strcpy(d->outFilename+strlen(d->srcFilename), ".dwt");
@@ -1026,22 +1026,22 @@ int main(int argc, char **argv)
     int inputSize = pixWidth*pixHeight*compCount; //<amount of data (in bytes) to proccess
 
     //load img source image
-	d->srcImg = (unsigned char *) malloc (inputSize);
-	if (getImg(d->srcFilename, d->srcImg, inputSize) == -1)
+  d->srcImg = (unsigned char *) malloc (inputSize);
+  if (getImg(d->srcFilename, d->srcImg, inputSize) == -1)
         return -1;
 
-	 // DWT
-	// Create memory objects, Set arguments for kernel functions, Queue the kernel up for execution across the array, Read the output buffer back to the Host, Output the result buffer
+   // DWT
+  // Create memory objects, Set arguments for kernel functions, Queue the kernel up for execution across the array, Read the output buffer back to the Host, Output the result buffer
 
     if (forward == 1)
-	{
+  {
         if(dwt97 == 1 )
             processDWT<float>(d, forward, writeVisual);
         else // 5/3
             processDWT<int>(d, forward, writeVisual);
     }
     else
-	{ // reverse
+  { // reverse
         if(dwt97 == 1 )
             processDWT<float>(d, forward, writeVisual);
         else // 5/3
@@ -1049,9 +1049,9 @@ int main(int argc, char **argv)
     }
 
 
-	Cleanup(context, commandQueue, program, kernel);
-	clReleaseKernel(c_CopySrcToComponents);
-	clReleaseKernel(c_CopySrcToComponent);
+  Cleanup(context, commandQueue, program, kernel);
+  clReleaseKernel(c_CopySrcToComponents);
+  clReleaseKernel(c_CopySrcToComponent);
 
     return 0;
 

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 267 - 267
opencl/gaussian/clutils.cpp


+ 14 - 14
opencl/gaussian/clutils.h

@@ -1,4 +1,4 @@
-/****************************************************************************\ 
+/****************************************************************************\
  * Copyright (c) 2011, Advanced Micro Devices, Inc.                           *
  * All rights reserved.                                                       *
  *                                                                            *
@@ -58,7 +58,7 @@
 #ifdef _WIN32
 #include <tchar.h>
 #include <Windows.h>
-typedef __int64 cl_time; 
+typedef __int64 cl_time;
 #else
 #include <sys/time.h>
 typedef double cl_time;
@@ -103,7 +103,7 @@ void    cl_sync();
 //-------------------------------------------------------
 
 // Allocates a regular buffer on the device
-cl_mem  cl_allocBuffer(size_t mem_size, 
+cl_mem  cl_allocBuffer(size_t mem_size,
             cl_mem_flags flags = CL_MEM_READ_WRITE);
 
 // XXX I don't think this does exactly what we want it to do
@@ -114,7 +114,7 @@ cl_mem  cl_allocBufferConst(size_t mem_size, void* host_ptr);
 cl_mem  cl_allocBufferPinned(size_t mem_size);
 
 // Allocates an image on the device
-cl_mem  cl_allocImage(size_t height, size_t width, char type, 
+cl_mem  cl_allocImage(size_t height, size_t width, char type,
             cl_mem_flags flags = CL_MEM_READ_WRITE);
 
 
@@ -123,15 +123,15 @@ cl_mem  cl_allocImage(size_t height, size_t width, char type,
 // Data transfers
 //-------------------------------------------------------
 
-// Copies a buffer from the device to pinned memory on the host and 
+// Copies a buffer from the device to pinned memory on the host and
 // maps it so it can be read
-void*   cl_copyAndMapBuffer(cl_mem dst, cl_mem src, size_t size); 
+void*   cl_copyAndMapBuffer(cl_mem dst, cl_mem src, size_t size);
 
 // Copies from one buffer to another
 void    cl_copyBufferToBuffer(cl_mem dst, cl_mem src, size_t size);
 
 // Copies data to a buffer on the device
-void    cl_copyBufferToDevice(cl_mem dst, void *src, size_t mem_size, 
+void    cl_copyBufferToDevice(cl_mem dst, void *src, size_t mem_size,
             cl_bool blocking = CL_TRUE);
 
 // Copies data to an image on the device
@@ -141,7 +141,7 @@ void cl_copyImageToDevice(cl_mem dst, void* src, size_t height, size_t width);
 void    cl_copyImageToHost(void* dst, cl_mem src, size_t height, size_t width);
 
 // Copies data from a device buffer to the host
-void    cl_copyBufferToHost(void *dst, cl_mem src, size_t mem_size, 
+void    cl_copyBufferToHost(void *dst, cl_mem src, size_t mem_size,
             cl_bool blocking = CL_TRUE);
 
 // Copies data from a buffer on the device to an image on the device
@@ -161,7 +161,7 @@ void    cl_writeToZCBuffer(cl_mem mem, void* data, size_t size);
 //-------------------------------------------------------
 
 // Compiles a program
-cl_program  cl_compileProgram(char* kernelPath, char* compileoptions, 
+cl_program  cl_compileProgram(char* kernelPath, char* compileoptions,
                 bool verboseoptions = 0);
 
 // Creates a kernel
@@ -169,7 +169,7 @@ cl_kernel   cl_createKernel(cl_program program, const char* kernelName);
 
 
 // Sets a kernel argument
-void        cl_setKernelArg(cl_kernel kernel, unsigned int index, size_t size, 
+void        cl_setKernelArg(cl_kernel kernel, unsigned int index, size_t size,
                 void* data);
 
 
@@ -181,7 +181,7 @@ void        cl_setKernelArg(cl_kernel kernel, unsigned int index, size_t size,
 double  cl_computeExecTime(cl_event);
 
 // Compute the elapsed time between two CPU timer values
-double  cl_computeTime(cl_time start, cl_time end); 
+double  cl_computeTime(cl_time start, cl_time end);
 
 // Creates an event from CPU timers
 void    cl_createUserEvent(cl_time start, cl_time end, char* desc);
@@ -193,7 +193,7 @@ void    cl_disableEvents();
 void    cl_enableEvents();
 
 // Query the current system time
-void    cl_getTime(cl_time* time); 
+void    cl_getTime(cl_time* time);
 
 // Calls a function which prints events to the terminal
 void    cl_printEvents();
@@ -264,8 +264,8 @@ typedef struct{
 #define NUM_PROGRAMS 7
 
 #define NUM_KERNELS 13
-#define KERNEL_INIT_DET 0 
-#define KERNEL_BUILD_DET 1 
+#define KERNEL_INIT_DET 0
+#define KERNEL_BUILD_DET 1
 #define KERNEL_SURF_DESC 2
 #define KERNEL_NORM_DESC 3
 #define KERNEL_NON_MAX_SUP 4

+ 115 - 116
opencl/gaussian/gaussianElim.cpp

@@ -14,7 +14,7 @@
         #define BLOCK_SIZE_0 0
 #endif
 
-//2D defines. Go from specific to general                                                
+//2D defines. Go from specific to general
 #ifdef RD_WG_SIZE_1_0
         #define BLOCK_SIZE_1_X RD_WG_SIZE_1_0
 #elif defined(RD_WG_SIZE_1)
@@ -48,17 +48,17 @@ create_matrix(float *m, int size){
 
   for (i=0; i < size; i++)
     {
-      coe_i = 10*exp(lamda*i); 
-      j=size-1+i;     
+      coe_i = 10*exp(lamda*i);
+      j=size-1+i;
       coe[j]=coe_i;
-      j=size-1-i;     
+      j=size-1-i;
       coe[j]=coe_i;
     }
 
 
   for (i=0; i < size; i++) {
       for (j=0; j < size; j++) {
-	m[i*size+j]=coe[size-1-i+j];
+  m[i*size+j]=coe[size-1-i+j];
       }
   }
 
@@ -72,90 +72,90 @@ int main(int argc, char *argv[]) {
     float *a=NULL, *b=NULL, *finalVec=NULL;
     float *m=NULL;
     int size = -1;
-    
+
     FILE *fp;
-    
+
     // args
     char filename[200];
     int quiet=1,timing=0,platform=-1,device=-1,use_gpu=-1;
-    
+
     // parse command line
     if (parseCommandline(argc, argv, filename,
-			 &quiet, &timing, &platform, &device, &size, &use_gpu)) {
+   &quiet, &timing, &platform, &device, &size, &use_gpu)) {
     printUsage();
     return 0;
     }
 
     context = cl_init_context(platform,device,quiet);
-    
+
     if(size < 1)
       {
-	fp = fopen(filename, "r");
-	fscanf(fp, "%d", &size);
-    
-	a = (float *) malloc(size * size * sizeof(float));
-	InitMat(fp,size, a, size, size);
+  fp = fopen(filename, "r");
+  fscanf(fp, "%d", &size);
 
-	b = (float *) malloc(size * sizeof(float));
-	InitAry(fp, b, size);
+  a = (float *) malloc(size * size * sizeof(float));
+  InitMat(fp,size, a, size, size);
 
-	fclose(fp);
+  b = (float *) malloc(size * sizeof(float));
+  InitAry(fp, b, size);
+
+  fclose(fp);
 
       }
     else
       {
-	printf("create input internally before create, size = %d \n", size);
+  printf("create input internally before create, size = %d \n", size);
 
-	a = (float *) malloc(size * size * sizeof(float));
-	create_matrix(a, size);
+  a = (float *) malloc(size * size * sizeof(float));
+  create_matrix(a, size);
 
-	b = (float *) malloc(size * sizeof(float));
-	for (int i =0; i< size; i++)
-	  b[i]=1.0;
+  b = (float *) malloc(size * sizeof(float));
+  for (int i =0; i< size; i++)
+    b[i]=1.0;
 
       }
 
-    if (!quiet) {    
+    if (!quiet) {
       printf("The input matrix a is:\n");
       PrintMat(a, size, size, size);
 
       printf("The input array b is:\n");
       PrintAry(b, size);
     }
- 
+
     // create the solution matrix
     m = (float *) malloc(size * size * sizeof(float));
-	 
+
     // create a new vector to hold the final answer
 
     finalVec = (float *) malloc(size * sizeof(float));
-    
+
     InitPerRun(size,m);
 
-    //begin timing	
+    //begin timing
         // printf("The result of array b is before run: \n");
         // PrintAry(b, size);
-    
+
     // run kernels
-	ForwardSub(context,a,b,m,size,timing);
+  ForwardSub(context,a,b,m,size,timing);
         // printf("The result of array b is after run: \n");
         // PrintAry(b, size);
-    
+
     //end timing
     if (!quiet) {
         printf("The result of matrix m is: \n");
-        
+
         PrintMat(m, size, size, size);
         printf("The result of matrix a is: \n");
         PrintMat(a, size, size, size);
         printf("The result of array b is: \n");
         PrintAry(b, size);
-        
+
         BackSub(a,b,finalVec,size);
         printf("The final solution is: \n");
         PrintAry(finalVec,size);
     }
-    
+
     free(m);
     free(a);
     free(b);
@@ -171,7 +171,7 @@ int main(int argc, char *argv[]) {
  ** elimination.
  **------------------------------------------------------
  */
-void ForwardSub(cl_context context, float *a, float *b, float *m, int size,int timing){    
+void ForwardSub(cl_context context, float *a, float *b, float *m, int size,int timing){
     // 1. set up kernels
     cl_kernel fan1_kernel,fan2_kernel;
     cl_int status=0;
@@ -179,20 +179,20 @@ void ForwardSub(cl_context context, float *a, float *b, float *m, int size,int t
     cl_event writeEvent,kernelEvent,readEvent;
     float writeTime=0,readTime=0,kernelTime=0;
     float writeMB=0,readMB=0;
-    
+
     gaussianElim_program = cl_compileProgram(
         (char *)"gaussianElim_kernels.cl",NULL);
-   
+
     fan1_kernel = clCreateKernel(
         gaussianElim_program, "Fan1", &status);
     status = cl_errChk(status, (char *)"Error Creating Fan1 kernel",true);
     if(status)exit(1);
-   
+
     fan2_kernel = clCreateKernel(
         gaussianElim_program, "Fan2", &status);
     status = cl_errChk(status, (char *)"Error Creating Fan2 kernel",true);
     if(status)exit(1);
-    
+
     // 2. set up memory on device and send ipts data to device
 
     cl_mem a_dev, b_dev, m_dev;
@@ -201,7 +201,7 @@ void ForwardSub(cl_context context, float *a, float *b, float *m, int size,int t
 
     a_dev = clCreateBuffer(context, CL_MEM_READ_WRITE,
         sizeof(float)*size*size, NULL, &error);
-        
+
     b_dev = clCreateBuffer(context, CL_MEM_READ_WRITE,
         sizeof(float)*size, NULL, &error);
 
@@ -209,7 +209,7 @@ void ForwardSub(cl_context context, float *a, float *b, float *m, int size,int t
         sizeof(float) * size * size, NULL, &error);
 
     cl_command_queue command_queue = cl_getCommandQueue();
-    
+
     error = clEnqueueWriteBuffer(command_queue,
                a_dev,
                1, // change to 0 for nonblocking write
@@ -219,10 +219,10 @@ void ForwardSub(cl_context context, float *a, float *b, float *m, int size,int t
                0,
                NULL,
                &writeEvent);
-    
+
     if (timing) writeTime+=eventTime(writeEvent,command_queue);
     clReleaseEvent(writeEvent);
-    
+
     error = clEnqueueWriteBuffer(command_queue,
                b_dev,
                1, // change to 0 for nonblocking write
@@ -234,7 +234,7 @@ void ForwardSub(cl_context context, float *a, float *b, float *m, int size,int t
                &writeEvent);
     if (timing) writeTime+=eventTime(writeEvent,command_queue);
     clReleaseEvent(writeEvent);
-               
+
     error = clEnqueueWriteBuffer(command_queue,
                m_dev,
                1, // change to 0 for nonblocking write
@@ -273,9 +273,9 @@ izeFan2Buf[0])*localWorksizeFan2Buf[0];
 izeFan2Buf[1])*localWorksizeFan2Buf[1];
         }
 
-	int t;
-	// 4. Setup and Run kernels
-	for (t=0; t<(size-1); t++) {
+  int t;
+  // 4. Setup and Run kernels
+  for (t=0; t<(size-1); t++) {
         // kernel args
         cl_int argchk;
         argchk  = clSetKernelArg(fan1_kernel, 0, sizeof(cl_mem), (void *)&m_dev);
@@ -283,9 +283,9 @@ izeFan2Buf[1])*localWorksizeFan2Buf[1];
         argchk |= clSetKernelArg(fan1_kernel, 2, sizeof(cl_mem), (void *)&b_dev);
         argchk |= clSetKernelArg(fan1_kernel, 3, sizeof(int), (void *)&size);
         argchk |= clSetKernelArg(fan1_kernel, 4, sizeof(int), (void *)&t);
-    
+
         cl_errChk(argchk,"ERROR in Setting Fan1 kernel args",true);
-        
+
         // launch kernel
         error = clEnqueueNDRangeKernel(
                   command_queue,  fan1_kernel, 1, 0,
@@ -299,18 +299,18 @@ izeFan2Buf[1])*localWorksizeFan2Buf[1];
 //             printf("here1b\n");
         }
         clReleaseEvent(kernelEvent);
-		//Fan1<<<dimGrid,dimBlock>>>(m_cuda,a_cuda,Size,t);
-		//cudaThreadSynchronize();
-		
-		// kernel args
-		argchk  = clSetKernelArg(fan2_kernel, 0, sizeof(cl_mem), (void *)&m_dev);
+  //Fan1<<<dimGrid,dimBlock>>>(m_cuda,a_cuda,Size,t);
+  //cudaThreadSynchronize();
+
+  // kernel args
+  argchk  = clSetKernelArg(fan2_kernel, 0, sizeof(cl_mem), (void *)&m_dev);
         argchk |= clSetKernelArg(fan2_kernel, 1, sizeof(cl_mem), (void *)&a_dev);
         argchk |= clSetKernelArg(fan2_kernel, 2, sizeof(cl_mem), (void *)&b_dev);
         argchk |= clSetKernelArg(fan2_kernel, 3, sizeof(int), (void *)&size);
         argchk |= clSetKernelArg(fan2_kernel, 4, sizeof(int), (void *)&t);
-    
+
         cl_errChk(argchk,"ERROR in Setting Fan2 kernel args",true);
-        
+
         // launch kernel
         error = clEnqueueNDRangeKernel(
                   command_queue,  fan2_kernel, 2, 0,
@@ -324,9 +324,9 @@ izeFan2Buf[1])*localWorksizeFan2Buf[1];
 //             printf("here2b\n");
         }
         clReleaseEvent(kernelEvent);
-		//Fan2<<<dimGridXY,dimBlockXY>>>(m_cuda,a_cuda,b_cuda,Size,Size-t,t);
-		//cudaThreadSynchronize();
-	}
+  //Fan2<<<dimGridXY,dimBlockXY>>>(m_cuda,a_cuda,b_cuda,Size,Size-t,t);
+  //cudaThreadSynchronize();
+  }
     // 5. transfer data off of device
     error = clEnqueueReadBuffer(command_queue,
         a_dev,
@@ -341,7 +341,7 @@ izeFan2Buf[1])*localWorksizeFan2Buf[1];
     cl_errChk(error,"ERROR with clEnqueueReadBuffer",true);
     if (timing) readTime+=eventTime(readEvent,command_queue);
     clReleaseEvent(readEvent);
-    
+
     error = clEnqueueReadBuffer(command_queue,
         b_dev,
         1, // change to 0 for nonblocking write
@@ -354,7 +354,7 @@ izeFan2Buf[1])*localWorksizeFan2Buf[1];
     cl_errChk(error,"ERROR with clEnqueueReadBuffer",true);
     if (timing) readTime+=eventTime(readEvent,command_queue);
     clReleaseEvent(readEvent);
-    
+
     error = clEnqueueReadBuffer(command_queue,
         m_dev,
         1, // change to 0 for nonblocking write
@@ -369,22 +369,22 @@ izeFan2Buf[1])*localWorksizeFan2Buf[1];
     if (timing) readTime+=eventTime(readEvent,command_queue);
     clReleaseEvent(readEvent);
     readMB = (float)(sizeof(float) * size * (size + size + 1) / 1e6);
-    
+
     if (timing) {
         printf("Matrix Size\tWrite(s) [size]\t\tKernel(s)\tRead(s)  [size]\t\tTotal(s)\n");
         printf("%dx%d      \t",size,size);
-        
+
         printf("%f [%.2fMB]\t",writeTime,writeMB);
-        
+
 
         printf("%f\t",kernelTime);
-       
+
 
         printf("%f [%.2fMB]\t",readTime,readMB);
-       
+
         printf("%f\n\n",writeTime+kernelTime+readTime);
    }
-    
+
 }
 
 float eventTime(cl_event event,cl_command_queue command_queue){
@@ -393,7 +393,7 @@ float eventTime(cl_event event,cl_command_queue command_queue){
     clFinish(command_queue);
     error = clGetEventProfilingInfo(event,CL_PROFILING_COMMAND_START,
                                     sizeof(cl_ulong),&eventStart,NULL);
-    cl_errChk(error,"ERROR in Event Profiling.",true); 
+    cl_errChk(error,"ERROR in Event Profiling.",true);
     error = clGetEventProfilingInfo(event,CL_PROFILING_COMMAND_END,
                                     sizeof(cl_ulong),&eventEnd,NULL);
     cl_errChk(error,"ERROR in Event Profiling.",true);
@@ -408,7 +408,7 @@ int parseCommandline(int argc, char *argv[], char* filename,
     if (argc < 2) return 1; // error
     // strncpy(filename,argv[1],100);
     char flag;
-    
+
     for(i=1;i<argc;i++) {
       if (argv[i][0]=='-') {// flag
         flag = argv[i][1];
@@ -416,12 +416,12 @@ int parseCommandline(int argc, char *argv[], char* filename,
             case 's': // platform
               i++;
               *size = atoi(argv[i]);
-	      printf("Create matrix internally in parse, size = %d \n", *size);
+        printf("Create matrix internally in parse, size = %d \n", *size);
               break;
             case 'f': // platform
               i++;
-	      strncpy(filename,argv[i],100);
-	      printf("Read file from %s \n", filename);
+        strncpy(filename,argv[i],100);
+        printf("Read file from %s \n", filename);
               break;
             case 'h': // help
               return 1;
@@ -443,7 +443,7 @@ int parseCommandline(int argc, char *argv[], char* filename,
             case 'g': // device
               i++;
               *use_gpu = atoi(argv[i]);
-              break;  
+              break;
         }
       }
     }
@@ -482,34 +482,34 @@ void printUsage(){
  ** multipier matrix **m
  **------------------------------------------------------
  */
-void InitPerRun(int size,float *m) 
+void InitPerRun(int size,float *m)
 {
-	int i;
-	for (i=0; i<size*size; i++)
-			*(m+i) = 0.0;
+  int i;
+  for (i=0; i<size*size; i++)
+  *(m+i) = 0.0;
 }
 void BackSub(float *a, float *b, float *finalVec, int size)
 {
-	// solve "bottom up"
-	int i,j;
-	for(i=0;i<size;i++){
-		finalVec[size-i-1]=b[size-i-1];
-		for(j=0;j<i;j++)
-		{
-			finalVec[size-i-1]-=*(a+size*(size-i-1)+(size-j-1)) * finalVec[size-j-1];
-		}
-		finalVec[size-i-1]=finalVec[size-i-1]/ *(a+size*(size-i-1)+(size-i-1));
-	}
+  // solve "bottom up"
+  int i,j;
+  for(i=0;i<size;i++){
+  finalVec[size-i-1]=b[size-i-1];
+  for(j=0;j<i;j++)
+  {
+  finalVec[size-i-1]-=*(a+size*(size-i-1)+(size-j-1)) * finalVec[size-j-1];
+  }
+  finalVec[size-i-1]=finalVec[size-i-1]/ *(a+size*(size-i-1)+(size-i-1));
+  }
 }
 void InitMat(FILE *fp, int size, float *ary, int nrow, int ncol)
 {
-	int i, j;
-	
-	for (i=0; i<nrow; i++) {
-		for (j=0; j<ncol; j++) {
-			fscanf(fp, "%f",  ary+size*i+j);
-		}
-	}  
+  int i, j;
+
+  for (i=0; i<nrow; i++) {
+  for (j=0; j<ncol; j++) {
+  fscanf(fp, "%f",  ary+size*i+j);
+  }
+  }
 }
 /*------------------------------------------------------
  ** InitAry() -- Initialize the array (vector) by reading
@@ -518,27 +518,27 @@ void InitMat(FILE *fp, int size, float *ary, int nrow, int ncol)
  */
 void InitAry(FILE *fp, float *ary, int ary_size)
 {
-	int i;
-	
-	for (i=0; i<ary_size; i++) {
-		fscanf(fp, "%f",  &ary[i]);
-	}
-}  
+  int i;
+
+  for (i=0; i<ary_size; i++) {
+  fscanf(fp, "%f",  &ary[i]);
+  }
+}
 /*------------------------------------------------------
  ** PrintMat() -- Print the contents of the matrix
  **------------------------------------------------------
  */
 void PrintMat(float *ary, int size, int nrow, int ncol)
 {
-	int i, j;
-	
-	for (i=0; i<nrow; i++) {
-		for (j=0; j<ncol; j++) {
-			printf("%8.2e ", *(ary+size*i+j));
-		}
-		printf("\n");
-	}
-	printf("\n");
+  int i, j;
+
+  for (i=0; i<nrow; i++) {
+  for (j=0; j<ncol; j++) {
+  printf("%8.2e ", *(ary+size*i+j));
+  }
+  printf("\n");
+  }
+  printf("\n");
 }
 
 /*------------------------------------------------------
@@ -547,11 +547,10 @@ void PrintMat(float *ary, int size, int nrow, int ncol)
  */
 void PrintAry(float *ary, int ary_size)
 {
-	int i;
-	for (i=0; i<ary_size; i++) {
-		printf("%.2e ", ary[i]);
-	}
-	printf("\n\n");
+  int i;
+  for (i=0; i<ary_size; i++) {
+  printf("%.2e ", ary[i]);
+  }
+  printf("\n\n");
 }
 #endif
-

+ 4 - 4
opencl/gaussian/gaussianElim.h

@@ -16,16 +16,16 @@
     #include <OpenCL/opencl.h>
 #else
     #include <CL/opencl.h>
-#endif 
+#endif
 
 float *OpenClGaussianElimination(
-	cl_context context,
-	int timing);
+  cl_context context,
+  int timing);
 
 void printUsage();
 int parseCommandline(int argc, char *argv[], char* filename,
                      int *q, int *t, int *p, int *d, int *size, int *use_gpu);
-                     
+
 void InitPerRun(int size,float *m);
 void ForwardSub(cl_context context, float *a, float *b, float *m, int size,int timing);
 void BackSub(float *a, float *b, float *finalVec, int size);

+ 12 - 12
opencl/gaussian/utils.cpp

@@ -72,9 +72,9 @@ void* alloc(size_t size) {
 }
 
 // This function checks to make sure a file exists before we open it
-void checkFile(char* filename) 
+void checkFile(char* filename)
 {
-    
+
     struct stat fileStatus;
     if(stat(filename, &fileStatus) != 0) {
         printf("Error opening file: %s\n", filename);
@@ -89,10 +89,10 @@ void checkFile(char* filename)
 }
 
 
-// This function checks to make sure a directory exists 
-void checkDir(char* dirpath) 
+// This function checks to make sure a directory exists
+void checkDir(char* dirpath)
 {
-    
+
     struct stat fileStatus;
     if(stat(dirpath, &fileStatus) != 0) {
         printf("Directory does not exist: %s\n", dirpath);
@@ -107,10 +107,10 @@ void checkDir(char* dirpath)
 }
 
 // Parse the command line arguments
-void parseArguments(int argc, char** argv, char** input, char** events, 
-    char** ipts, char* devicePref, bool* verifyResults) 
+void parseArguments(int argc, char** argv, char** input, char** events,
+    char** ipts, char* devicePref, bool* verifyResults)
 {
-    
+
     for(int i = 2; i < argc; i++) {
         if(strcmp(argv[i], "-d") == 0) {   // Event dump found
             if(i == argc-1) {
@@ -165,7 +165,7 @@ void parseArguments(int argc, char** argv, char** input, char** events,
 unsigned int roundUp(unsigned int value, unsigned int multiple) {
 
    unsigned int remainder = value % multiple;
-   
+
    // Make the value a multiple of multiple
    if(remainder != 0) {
       value += (multiple-remainder);
@@ -176,7 +176,7 @@ unsigned int roundUp(unsigned int value, unsigned int multiple) {
 
 
 // Concatenate two strings and return a pointer to the new string
-char* smartStrcat(char* str1, char* str2) 
+char* smartStrcat(char* str1, char* str2)
 {
     char* newStr = NULL;
 
@@ -191,14 +191,14 @@ char* smartStrcat(char* str1, char* str2)
 
 // Set the value of using images to true if they are being
 // used, or false if they are not
-void setUsingImages(bool val) 
+void setUsingImages(bool val)
 {
     usingImages = val;
 }
 
 
 // Return whether or not images are being used
-bool isUsingImages() 
+bool isUsingImages()
 {
     return usingImages;
 }

+ 3 - 3
opencl/gaussian/utils.h

@@ -1,4 +1,4 @@
-/****************************************************************************\ 
+/****************************************************************************\
  * Copyright (c) 2011, Advanced Micro Devices, Inc.                           *
  * All rights reserved.                                                       *
  *                                                                            *
@@ -62,7 +62,7 @@ void checkDir(char* dirpath);
 void checkFile(char* filename);
 
 // Parse the input command line options to the program
-void parseArguments(int argc, char** argv, char** input, char** events, 
+void parseArguments(int argc, char** argv, char** input, char** events,
     char** ipts, char* devicePref, bool* verifyResults);
 
 
@@ -79,6 +79,6 @@ char* smartStrcat(char* str1, char* str2);
 void setUsingImages(bool val);
 
 // Return whether or not images are being used
-bool isUsingImages(); 
+bool isUsingImages();
 
 #endif

+ 88 - 88
opencl/hotspot/OpenCL_helper_library.c

@@ -2,105 +2,105 @@
 
 // Loads the kernel source code from the specified file into a C string and returns the string pointer
 char *load_kernel_source(const char *filename) {
-	// Open the source file
-	FILE *file = fopen(filename, "r");
-	if (file == NULL) fatal("Error opening kernel source file\n");
-	
-	// Determine the size of the file
-	if (fseek(file, 0, SEEK_END)) fatal("Error reading kernel source file\n");
-	size_t size = ftell(file);
-	
-	// Allocate space for the source code (plus one for null-terminator)
-	char *source = (char *) malloc(size + 1);
-	
-	// Read the source code into the string
-	fseek(file, 0, SEEK_SET);
-	// printf("Number of elements: %lu\nSize = %lu", fread(source, 1, size, file), size);
-	// exit(1);
-	if (fread(source, 1, size, file) != size) fatal("Error reading kernel source file\n");
-	
-	// Null-terminate the string
-	source[size] = '\0';
-	
-	// Return the pointer to the string
-	return source;
+  // Open the source file
+  FILE *file = fopen(filename, "r");
+  if (file == NULL) fatal("Error opening kernel source file\n");
+
+  // Determine the size of the file
+  if (fseek(file, 0, SEEK_END)) fatal("Error reading kernel source file\n");
+  size_t size = ftell(file);
+
+  // Allocate space for the source code (plus one for null-terminator)
+  char *source = (char *) malloc(size + 1);
+
+  // Read the source code into the string
+  fseek(file, 0, SEEK_SET);
+  // printf("Number of elements: %lu\nSize = %lu", fread(source, 1, size, file), size);
+  // exit(1);
+  if (fread(source, 1, size, file) != size) fatal("Error reading kernel source file\n");
+
+  // Null-terminate the string
+  source[size] = '\0';
+
+  // Return the pointer to the string
+  return source;
 }
 
 
 // Returns the current system time in microseconds
 long long get_time() {
-	struct timeval tv;
-	gettimeofday(&tv, NULL);
-	return (tv.tv_sec * 1000000) + tv.tv_usec;
+  struct timeval tv;
+  gettimeofday(&tv, NULL);
+  return (tv.tv_sec * 1000000) + tv.tv_usec;
 }
 
 void fatal(const char *s) {
-	fprintf(stderr, "Error: %s\n", s);
-	exit(1);
+  fprintf(stderr, "Error: %s\n", s);
+  exit(1);
 }
 
 // Prints a string version of the specified OpenCL error code
 void fatal_CL(cl_int error, int line_no) {
-	printf("Error at line %d: ", line_no);
-	
-	// Print 
-	switch(error) {
-		case CL_SUCCESS: 									printf("CL_SUCCESS\n"); break;
-		case CL_DEVICE_NOT_FOUND: 							printf("CL_DEVICE_NOT_FOUND\n"); break;
-		case CL_DEVICE_NOT_AVAILABLE: 						printf("CL_DEVICE_NOT_AVAILABLE\n"); break;
-		case CL_COMPILER_NOT_AVAILABLE: 					printf("CL_COMPILER_NOT_AVAILABLE\n"); break;
-		case CL_MEM_OBJECT_ALLOCATION_FAILURE: 				printf("CL_MEM_OBJECT_ALLOCATION_FAILURE\n"); break;
-		case CL_OUT_OF_RESOURCES: 							printf("CL_OUT_OF_RESOURCES\n"); break;
-		case CL_OUT_OF_HOST_MEMORY: 						printf("CL_OUT_OF_HOST_MEMORY\n"); break;
-		case CL_PROFILING_INFO_NOT_AVAILABLE: 				printf("CL_PROFILING_INFO_NOT_AVAILABLE\n"); break;
-		case CL_MEM_COPY_OVERLAP: 							printf("CL_MEM_COPY_OVERLAP\n"); break;
-		case CL_IMAGE_FORMAT_MISMATCH: 						printf("CL_IMAGE_FORMAT_MISMATCH\n"); break;
-		case CL_IMAGE_FORMAT_NOT_SUPPORTED: 				printf("CL_IMAGE_FORMAT_NOT_SUPPORTED\n"); break;
-		case CL_BUILD_PROGRAM_FAILURE: 						printf("CL_BUILD_PROGRAM_FAILURE\n"); break;
-		case CL_MAP_FAILURE: 								printf("CL_MAP_FAILURE\n"); break;
-		case CL_INVALID_VALUE: 								printf("CL_INVALID_VALUE\n"); break;
-		case CL_INVALID_DEVICE_TYPE: 						printf("CL_INVALID_DEVICE_TYPE\n"); break;
-		case CL_INVALID_PLATFORM: 							printf("CL_INVALID_PLATFORM\n"); break;
-		case CL_INVALID_DEVICE: 							printf("CL_INVALID_DEVICE\n"); break;
-		case CL_INVALID_CONTEXT: 							printf("CL_INVALID_CONTEXT\n"); break;
-		case CL_INVALID_QUEUE_PROPERTIES: 					printf("CL_INVALID_QUEUE_PROPERTIES\n"); break;
-		case CL_INVALID_COMMAND_QUEUE: 						printf("CL_INVALID_COMMAND_QUEUE\n"); break;
-		case CL_INVALID_HOST_PTR: 							printf("CL_INVALID_HOST_PTR\n"); break;
-		case CL_INVALID_MEM_OBJECT: 						printf("CL_INVALID_MEM_OBJECT\n"); break;
-		case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: 			printf("CL_INVALID_IMAGE_FORMAT_DESCRIPTOR\n"); break;
-		case CL_INVALID_IMAGE_SIZE: 						printf("CL_INVALID_IMAGE_SIZE\n"); break;
-		case CL_INVALID_SAMPLER: 							printf("CL_INVALID_SAMPLER\n"); break;
-		case CL_INVALID_BINARY: 							printf("CL_INVALID_BINARY\n"); break;
-		case CL_INVALID_BUILD_OPTIONS: 						printf("CL_INVALID_BUILD_OPTIONS\n"); break;
-		case CL_INVALID_PROGRAM: 							printf("CL_INVALID_PROGRAM\n"); break;
-		case CL_INVALID_PROGRAM_EXECUTABLE: 				printf("CL_INVALID_PROGRAM_EXECUTABLE\n"); break;
-		case CL_INVALID_KERNEL_NAME: 						printf("CL_INVALID_KERNEL_NAME\n"); break;
-		case CL_INVALID_KERNEL_DEFINITION: 					printf("CL_INVALID_KERNEL_DEFINITION\n"); break;
-		case CL_INVALID_KERNEL: 							printf("CL_INVALID_KERNEL\n"); break;
-		case CL_INVALID_ARG_INDEX: 							printf("CL_INVALID_ARG_INDEX\n"); break;
-		case CL_INVALID_ARG_VALUE: 							printf("CL_INVALID_ARG_VALUE\n"); break;
-		case CL_INVALID_ARG_SIZE: 							printf("CL_INVALID_ARG_SIZE\n"); break;
-		case CL_INVALID_KERNEL_ARGS: 						printf("CL_INVALID_KERNEL_ARGS\n"); break;
-		case CL_INVALID_WORK_DIMENSION: 					printf("CL_INVALID_WORK_DIMENSION\n"); break;
-		case CL_INVALID_WORK_GROUP_SIZE: 					printf("CL_INVALID_WORK_GROUP_SIZE\n"); break;
-		case CL_INVALID_WORK_ITEM_SIZE: 					printf("CL_INVALID_WORK_ITEM_SIZE\n"); break;
-		case CL_INVALID_GLOBAL_OFFSET: 						printf("CL_INVALID_GLOBAL_OFFSET\n"); break;
-		case CL_INVALID_EVENT_WAIT_LIST: 					printf("CL_INVALID_EVENT_WAIT_LIST\n"); break;
-		case CL_INVALID_EVENT: 								printf("CL_INVALID_EVENT\n"); break;
-		case CL_INVALID_OPERATION: 							printf("CL_INVALID_OPERATION\n"); break;
-		case CL_INVALID_GL_OBJECT: 							printf("CL_INVALID_GL_OBJECT\n"); break;
-		case CL_INVALID_BUFFER_SIZE: 						printf("CL_INVALID_BUFFER_SIZE\n"); break;
-		case CL_INVALID_MIP_LEVEL: 							printf("CL_INVALID_MIP_LEVEL\n"); break;
-		case CL_INVALID_GLOBAL_WORK_SIZE: 					printf("CL_INVALID_GLOBAL_WORK_SIZE\n"); break;
-		
-		#ifdef CL_VERSION_1_1
-		case CL_MISALIGNED_SUB_BUFFER_OFFSET: 				printf("CL_MISALIGNED_SUB_BUFFER_OFFSET\n"); break;
-		case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST:	printf("CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST\n"); break;
-		/* case CL_INVALID_PROPERTY: 							printf("CL_INVALID_PROPERTY\n"); break; */
-		#endif
-		
-		default:											printf("Invalid OpenCL error code\n");
-	}
+  printf("Error at line %d: ", line_no);
+
+  // Print
+  switch(error) {
+  case CL_SUCCESS: 									printf("CL_SUCCESS\n"); break;
+  case CL_DEVICE_NOT_FOUND: 							printf("CL_DEVICE_NOT_FOUND\n"); break;
+  case CL_DEVICE_NOT_AVAILABLE: 						printf("CL_DEVICE_NOT_AVAILABLE\n"); break;
+  case CL_COMPILER_NOT_AVAILABLE: 					printf("CL_COMPILER_NOT_AVAILABLE\n"); break;
+  case CL_MEM_OBJECT_ALLOCATION_FAILURE: 				printf("CL_MEM_OBJECT_ALLOCATION_FAILURE\n"); break;
+  case CL_OUT_OF_RESOURCES: 							printf("CL_OUT_OF_RESOURCES\n"); break;
+  case CL_OUT_OF_HOST_MEMORY: 						printf("CL_OUT_OF_HOST_MEMORY\n"); break;
+  case CL_PROFILING_INFO_NOT_AVAILABLE: 				printf("CL_PROFILING_INFO_NOT_AVAILABLE\n"); break;
+  case CL_MEM_COPY_OVERLAP: 							printf("CL_MEM_COPY_OVERLAP\n"); break;
+  case CL_IMAGE_FORMAT_MISMATCH: 						printf("CL_IMAGE_FORMAT_MISMATCH\n"); break;
+  case CL_IMAGE_FORMAT_NOT_SUPPORTED: 				printf("CL_IMAGE_FORMAT_NOT_SUPPORTED\n"); break;
+  case CL_BUILD_PROGRAM_FAILURE: 						printf("CL_BUILD_PROGRAM_FAILURE\n"); break;
+  case CL_MAP_FAILURE: 								printf("CL_MAP_FAILURE\n"); break;
+  case CL_INVALID_VALUE: 								printf("CL_INVALID_VALUE\n"); break;
+  case CL_INVALID_DEVICE_TYPE: 						printf("CL_INVALID_DEVICE_TYPE\n"); break;
+  case CL_INVALID_PLATFORM: 							printf("CL_INVALID_PLATFORM\n"); break;
+  case CL_INVALID_DEVICE: 							printf("CL_INVALID_DEVICE\n"); break;
+  case CL_INVALID_CONTEXT: 							printf("CL_INVALID_CONTEXT\n"); break;
+  case CL_INVALID_QUEUE_PROPERTIES: 					printf("CL_INVALID_QUEUE_PROPERTIES\n"); break;
+  case CL_INVALID_COMMAND_QUEUE: 						printf("CL_INVALID_COMMAND_QUEUE\n"); break;
+  case CL_INVALID_HOST_PTR: 							printf("CL_INVALID_HOST_PTR\n"); break;
+  case CL_INVALID_MEM_OBJECT: 						printf("CL_INVALID_MEM_OBJECT\n"); break;
+  case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: 			printf("CL_INVALID_IMAGE_FORMAT_DESCRIPTOR\n"); break;
+  case CL_INVALID_IMAGE_SIZE: 						printf("CL_INVALID_IMAGE_SIZE\n"); break;
+  case CL_INVALID_SAMPLER: 							printf("CL_INVALID_SAMPLER\n"); break;
+  case CL_INVALID_BINARY: 							printf("CL_INVALID_BINARY\n"); break;
+  case CL_INVALID_BUILD_OPTIONS: 						printf("CL_INVALID_BUILD_OPTIONS\n"); break;
+  case CL_INVALID_PROGRAM: 							printf("CL_INVALID_PROGRAM\n"); break;
+  case CL_INVALID_PROGRAM_EXECUTABLE: 				printf("CL_INVALID_PROGRAM_EXECUTABLE\n"); break;
+  case CL_INVALID_KERNEL_NAME: 						printf("CL_INVALID_KERNEL_NAME\n"); break;
+  case CL_INVALID_KERNEL_DEFINITION: 					printf("CL_INVALID_KERNEL_DEFINITION\n"); break;
+  case CL_INVALID_KERNEL: 							printf("CL_INVALID_KERNEL\n"); break;
+  case CL_INVALID_ARG_INDEX: 							printf("CL_INVALID_ARG_INDEX\n"); break;
+  case CL_INVALID_ARG_VALUE: 							printf("CL_INVALID_ARG_VALUE\n"); break;
+  case CL_INVALID_ARG_SIZE: 							printf("CL_INVALID_ARG_SIZE\n"); break;
+  case CL_INVALID_KERNEL_ARGS: 						printf("CL_INVALID_KERNEL_ARGS\n"); break;
+  case CL_INVALID_WORK_DIMENSION: 					printf("CL_INVALID_WORK_DIMENSION\n"); break;
+  case CL_INVALID_WORK_GROUP_SIZE: 					printf("CL_INVALID_WORK_GROUP_SIZE\n"); break;
+  case CL_INVALID_WORK_ITEM_SIZE: 					printf("CL_INVALID_WORK_ITEM_SIZE\n"); break;
+  case CL_INVALID_GLOBAL_OFFSET: 						printf("CL_INVALID_GLOBAL_OFFSET\n"); break;
+  case CL_INVALID_EVENT_WAIT_LIST: 					printf("CL_INVALID_EVENT_WAIT_LIST\n"); break;
+  case CL_INVALID_EVENT: 								printf("CL_INVALID_EVENT\n"); break;
+  case CL_INVALID_OPERATION: 							printf("CL_INVALID_OPERATION\n"); break;
+  case CL_INVALID_GL_OBJECT: 							printf("CL_INVALID_GL_OBJECT\n"); break;
+  case CL_INVALID_BUFFER_SIZE: 						printf("CL_INVALID_BUFFER_SIZE\n"); break;
+  case CL_INVALID_MIP_LEVEL: 							printf("CL_INVALID_MIP_LEVEL\n"); break;
+  case CL_INVALID_GLOBAL_WORK_SIZE: 					printf("CL_INVALID_GLOBAL_WORK_SIZE\n"); break;
+
+  #ifdef CL_VERSION_1_1
+  case CL_MISALIGNED_SUB_BUFFER_OFFSET: 				printf("CL_MISALIGNED_SUB_BUFFER_OFFSET\n"); break;
+  case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST:	printf("CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST\n"); break;
+  /* case CL_INVALID_PROPERTY: 							printf("CL_INVALID_PROPERTY\n"); break; */
+  #endif
+
+  default:											printf("Invalid OpenCL error code\n");
+  }
 
-	exit(error);
+  exit(error);
 }

+ 210 - 210
opencl/hotspot/hotspot.c

@@ -3,51 +3,51 @@
 
 void writeoutput(float *vect, int grid_rows, int grid_cols, char *file) {
 
-	int i,j, index=0;
-	FILE *fp;
-	char str[STR_SIZE];
+  int i,j, index=0;
+  FILE *fp;
+  char str[STR_SIZE];
 
-	if( (fp = fopen(file, "w" )) == 0 )
+  if( (fp = fopen(file, "w" )) == 0 )
           printf( "The file was not opened\n" );
 
 
-	for (i=0; i < grid_rows; i++) 
-	 for (j=0; j < grid_cols; j++)
-	 {
+  for (i=0; i < grid_rows; i++)
+   for (j=0; j < grid_cols; j++)
+   {
 
-		 sprintf(str, "%d\t%g\n", index, vect[i*grid_cols+j]);
-		 fputs(str,fp);
-		 index++;
-	 }
-		
-      fclose(fp);	
+   sprintf(str, "%d\t%g\n", index, vect[i*grid_cols+j]);
+   fputs(str,fp);
+   index++;
+   }
+
+      fclose(fp);
 }
 
 
 void readinput(float *vect, int grid_rows, int grid_cols, char *file) {
 
   	int i,j;
-	FILE *fp;
-	char str[STR_SIZE];
-	float val;
+  FILE *fp;
+  char str[STR_SIZE];
+  float val;
 
-	if( (fp  = fopen(file, "r" )) ==0 )
+  if( (fp  = fopen(file, "r" )) ==0 )
             fatal( "The file was not opened" );
 
 
-	for (i=0; i <= grid_rows-1; i++) 
-	 for (j=0; j <= grid_cols-1; j++)
-	 {
-		if (fgets(str, STR_SIZE, fp) == NULL) fatal("Error reading file\n");
-		if (feof(fp))
-			fatal("not enough lines in file");
-		//if ((sscanf(str, "%d%f", &index, &val) != 2) || (index != ((i-1)*(grid_cols-2)+j-1)))
-		if ((sscanf(str, "%f", &val) != 1))
-			fatal("invalid file format");
-		vect[i*grid_cols+j] = val;
-	}
+  for (i=0; i <= grid_rows-1; i++)
+   for (j=0; j <= grid_cols-1; j++)
+   {
+  if (fgets(str, STR_SIZE, fp) == NULL) fatal("Error reading file\n");
+  if (feof(fp))
+  fatal("not enough lines in file");
+  //if ((sscanf(str, "%d%f", &index, &val) != 2) || (index != ((i-1)*(grid_cols-2)+j-1)))
+  if ((sscanf(str, "%f", &val) != 1))
+  fatal("invalid file format");
+  vect[i*grid_cols+j] = val;
+  }
 
-	fclose(fp);	
+  fclose(fp);
 
 }
 
@@ -57,118 +57,118 @@ void readinput(float *vect, int grid_rows, int grid_cols, char *file) {
 */
 
 int compute_tran_temp(cl_mem MatrixPower, cl_mem MatrixTemp[2], int col, int row, \
-		int total_iterations, int num_iterations, int blockCols, int blockRows, int borderCols, int borderRows,
-		float *TempCPU, float *PowerCPU) 
-{ 
-	
-	float grid_height = chip_height / row;
-	float grid_width = chip_width / col;
-
-	float Cap = FACTOR_CHIP * SPEC_HEAT_SI * t_chip * grid_width * grid_height;
-	float Rx = grid_width / (2.0 * K_SI * t_chip * grid_height);
-	float Ry = grid_height / (2.0 * K_SI * t_chip * grid_width);
-	float Rz = t_chip / (K_SI * grid_height * grid_width);
-
-	float max_slope = MAX_PD / (FACTOR_CHIP * t_chip * SPEC_HEAT_SI);
-	float step = PRECISION / max_slope;
-	int t;
-
-	int src = 0, dst = 1;
-	
-	cl_int error;
-	
-	// Determine GPU work group grid
-	size_t global_work_size[2];
-	global_work_size[0] = BLOCK_SIZE * blockCols;
-	global_work_size[1] = BLOCK_SIZE * blockRows;
-	size_t local_work_size[2];
-	local_work_size[0] = BLOCK_SIZE;
-	local_work_size[1] = BLOCK_SIZE;
-	
-	
-	long long start_time = get_time();	
-	
-	for (t = 0; t < total_iterations; t += num_iterations) {
-		
-		// Specify kernel arguments
-		int iter = MIN(num_iterations, total_iterations - t);
-		clSetKernelArg(kernel, 0, sizeof(int), (void *) &iter);
-		clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &MatrixPower);
-		clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &MatrixTemp[src]);
-		clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *) &MatrixTemp[dst]);
-		clSetKernelArg(kernel, 4, sizeof(int), (void *) &col);
-		clSetKernelArg(kernel, 5, sizeof(int), (void *) &row);
-		clSetKernelArg(kernel, 6, sizeof(int), (void *) &borderCols);
-		clSetKernelArg(kernel, 7, sizeof(int), (void *) &borderRows);
-		clSetKernelArg(kernel, 8, sizeof(float), (void *) &Cap);
-		clSetKernelArg(kernel, 9, sizeof(float), (void *) &Rx);
-		clSetKernelArg(kernel, 10, sizeof(float), (void *) &Ry);
-		clSetKernelArg(kernel, 11, sizeof(float), (void *) &Rz);
-		clSetKernelArg(kernel, 12, sizeof(float), (void *) &step);
-		
-		// Launch kernel
-		error = clEnqueueNDRangeKernel(command_queue, kernel, 2, NULL, global_work_size, local_work_size, 0, NULL, NULL);
-		if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
-		
-		// Flush the queue
-		error = clFlush(command_queue);
-		if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
-		
-		// Swap input and output GPU matrices
-		src = 1 - src;
-		dst = 1 - dst;
-	}
-	
-	// Wait for all operations to finish
-	error = clFinish(command_queue);
-	if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
-	
-	long long end_time = get_time();
-	long long total_time = (end_time - start_time);	
-	printf("\nKernel time: %.3f seconds\n", ((float) total_time) / (1000*1000));
-	
-	return src;
+  int total_iterations, int num_iterations, int blockCols, int blockRows, int borderCols, int borderRows,
+  float *TempCPU, float *PowerCPU)
+{
+
+  float grid_height = chip_height / row;
+  float grid_width = chip_width / col;
+
+  float Cap = FACTOR_CHIP * SPEC_HEAT_SI * t_chip * grid_width * grid_height;
+  float Rx = grid_width / (2.0 * K_SI * t_chip * grid_height);
+  float Ry = grid_height / (2.0 * K_SI * t_chip * grid_width);
+  float Rz = t_chip / (K_SI * grid_height * grid_width);
+
+  float max_slope = MAX_PD / (FACTOR_CHIP * t_chip * SPEC_HEAT_SI);
+  float step = PRECISION / max_slope;
+  int t;
+
+  int src = 0, dst = 1;
+
+  cl_int error;
+
+  // Determine GPU work group grid
+  size_t global_work_size[2];
+  global_work_size[0] = BLOCK_SIZE * blockCols;
+  global_work_size[1] = BLOCK_SIZE * blockRows;
+  size_t local_work_size[2];
+  local_work_size[0] = BLOCK_SIZE;
+  local_work_size[1] = BLOCK_SIZE;
+
+
+  long long start_time = get_time();
+
+  for (t = 0; t < total_iterations; t += num_iterations) {
+
+  // Specify kernel arguments
+  int iter = MIN(num_iterations, total_iterations - t);
+  clSetKernelArg(kernel, 0, sizeof(int), (void *) &iter);
+  clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &MatrixPower);
+  clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &MatrixTemp[src]);
+  clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *) &MatrixTemp[dst]);
+  clSetKernelArg(kernel, 4, sizeof(int), (void *) &col);
+  clSetKernelArg(kernel, 5, sizeof(int), (void *) &row);
+  clSetKernelArg(kernel, 6, sizeof(int), (void *) &borderCols);
+  clSetKernelArg(kernel, 7, sizeof(int), (void *) &borderRows);
+  clSetKernelArg(kernel, 8, sizeof(float), (void *) &Cap);
+  clSetKernelArg(kernel, 9, sizeof(float), (void *) &Rx);
+  clSetKernelArg(kernel, 10, sizeof(float), (void *) &Ry);
+  clSetKernelArg(kernel, 11, sizeof(float), (void *) &Rz);
+  clSetKernelArg(kernel, 12, sizeof(float), (void *) &step);
+
+  // Launch kernel
+  error = clEnqueueNDRangeKernel(command_queue, kernel, 2, NULL, global_work_size, local_work_size, 0, NULL, NULL);
+  if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
+
+  // Flush the queue
+  error = clFlush(command_queue);
+  if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
+
+  // Swap input and output GPU matrices
+  src = 1 - src;
+  dst = 1 - dst;
+  }
+
+  // Wait for all operations to finish
+  error = clFinish(command_queue);
+  if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
+
+  long long end_time = get_time();
+  long long total_time = (end_time - start_time);
+  printf("\nKernel time: %.3f seconds\n", ((float) total_time) / (1000*1000));
+
+  return src;
 }
 
 void usage(int argc, char **argv) {
-	fprintf(stderr, "Usage: %s <grid_rows/grid_cols> <pyramid_height> <sim_time> <temp_file> <power_file> <output_file> <platform_id> <device_id> <use_gpu>\n", argv[0]);
-	fprintf(stderr, "\t<grid_rows/grid_cols>  - number of rows/cols in the grid (positive integer)\n");
-	fprintf(stderr, "\t<pyramid_height> - pyramid heigh(positive integer)\n");
-	fprintf(stderr, "\t<sim_time>   - number of iterations\n");
-	fprintf(stderr, "\t<temp_file>  - name of the file containing the initial temperature values of each cell\n");
-	fprintf(stderr, "\t<power_file> - name of the file containing the dissipated power values of each cell\n");
-	fprintf(stderr, "\t<output_file> - name of the output file\n");
+  fprintf(stderr, "Usage: %s <grid_rows/grid_cols> <pyramid_height> <sim_time> <temp_file> <power_file> <output_file> <platform_id> <device_id> <use_gpu>\n", argv[0]);
+  fprintf(stderr, "\t<grid_rows/grid_cols>  - number of rows/cols in the grid (positive integer)\n");
+  fprintf(stderr, "\t<pyramid_height> - pyramid heigh(positive integer)\n");
+  fprintf(stderr, "\t<sim_time>   - number of iterations\n");
+  fprintf(stderr, "\t<temp_file>  - name of the file containing the initial temperature values of each cell\n");
+  fprintf(stderr, "\t<power_file> - name of the file containing the dissipated power values of each cell\n");
+  fprintf(stderr, "\t<output_file> - name of the output file\n");
   fprintf(stderr, "\t<platform_id> - the target platform to use\n");
   fprintf(stderr, "\t<device_id> - the device to use\n");
   fprintf(stderr, "\t<use_gpu> - 1 for GPU, 0 for CPU\n");
-	exit(1);
+  exit(1);
 }
 
 int main(int argc, char** argv) {
 
   printf("WG size of kernel = %d X %d\n", BLOCK_SIZE, BLOCK_SIZE);
-  
+
   // Command line arguments "parsing"
   int size;
   int grid_rows,grid_cols = 0;
-  float *FilesavingTemp,*FilesavingPower; //,*MatrixOut; 
+  float *FilesavingTemp,*FilesavingPower; //,*MatrixOut;
   char *tfile, *pfile, *ofile;
-  
+
   int total_iterations = 60;
   int pyramid_height = 1; // number of iterations
   int platform_id = 0;
   int device_id = 0;
   int use_gpu = 0;
-	
-	if (argc < 10)
-		usage(argc, argv);
-	if((grid_rows = atoi(argv[1]))<=0||
-	   (grid_cols = atoi(argv[1]))<=0||
+
+  if (argc < 10)
+  usage(argc, argv);
+  if((grid_rows = atoi(argv[1]))<=0||
+     (grid_cols = atoi(argv[1]))<=0||
        (pyramid_height = atoi(argv[2]))<=0||
        (total_iterations = atoi(argv[3]))<=0)
-		usage(argc, argv);
-		
-	tfile=argv[4];
+  usage(argc, argv);
+
+  tfile=argv[4];
   pfile=argv[5];
   ofile=argv[6];
   platform_id = atoi(argv[7]);
@@ -177,52 +177,52 @@ int main(int argc, char** argv) {
 
   size=grid_rows*grid_cols;
 
-	cl_int error;
-	cl_uint num_platforms;
-  
+  cl_int error;
+  cl_uint num_platforms;
+
   // Selector for CPU/GPU
   cl_device_type device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
-	
-	// Get the number of platforms
-	error = clGetPlatformIDs(0, NULL, &num_platforms);
+
+  // Get the number of platforms
+  error = clGetPlatformIDs(0, NULL, &num_platforms);
     if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
-	
-	// Get the list of platforms
-	cl_platform_id* platforms = (cl_platform_id *) malloc(sizeof(cl_platform_id) * num_platforms);
-	error = clGetPlatformIDs(num_platforms, platforms, NULL);
+
+  // Get the list of platforms
+  cl_platform_id* platforms = (cl_platform_id *) malloc(sizeof(cl_platform_id) * num_platforms);
+  error = clGetPlatformIDs(num_platforms, platforms, NULL);
     if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
-	
-	// Print the chosen platform, selected on the basis of the corresponding command line aegument
-	cl_platform_id platform = platforms[platform_id];
-	char pbuf[100];
-	error = clGetPlatformInfo(platform, CL_PLATFORM_VENDOR, sizeof(pbuf), pbuf, NULL);
-	if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
-	printf("Platform: %s\n", pbuf);
-	
-	// Create a GPU context
-	cl_context_properties context_properties[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties) platform, 0};
+
+  // Print the chosen platform, selected on the basis of the corresponding command line aegument
+  cl_platform_id platform = platforms[platform_id];
+  char pbuf[100];
+  error = clGetPlatformInfo(platform, CL_PLATFORM_VENDOR, sizeof(pbuf), pbuf, NULL);
+  if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
+  printf("Platform: %s\n", pbuf);
+
+  // Create a GPU context
+  cl_context_properties context_properties[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties) platform, 0};
     context = clCreateContextFromType(context_properties, device_type, NULL, NULL, &error);
     if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
-	
-	// Get and print the chosen device (if there are multiple devices, choose the first one)
-	size_t devices_size;
-	error = clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &devices_size);
-	if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
-	cl_device_id *devices = (cl_device_id *) malloc(devices_size);
-	error = clGetContextInfo(context, CL_CONTEXT_DEVICES, devices_size, devices, NULL);
-	if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
-  
+
+  // Get and print the chosen device (if there are multiple devices, choose the first one)
+  size_t devices_size;
+  error = clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &devices_size);
+  if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
+  cl_device_id *devices = (cl_device_id *) malloc(devices_size);
+  error = clGetContextInfo(context, CL_CONTEXT_DEVICES, devices_size, devices, NULL);
+  if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
+
   // Select the device passed as command line argument
-	device = devices[device_id];
-	error = clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(pbuf), pbuf, NULL);
-	if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
-	printf("Device: %s\n", pbuf);
-	
-	// Create a command queue
-	command_queue = clCreateCommandQueue(context, device, 0, &error);
+  device = devices[device_id];
+  error = clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(pbuf), pbuf, NULL);
   if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
+  printf("Device: %s\n", pbuf);
 
-  // --------------- pyramid parameters --------------- 
+  // Create a command queue
+  command_queue = clCreateCommandQueue(context, device, 0, &error);
+  if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
+
+  // --------------- pyramid parameters ---------------
   int borderCols = (pyramid_height)*EXPAND_RATE/2;
   int borderRows = (pyramid_height)*EXPAND_RATE/2;
   int smallBlockCol = BLOCK_SIZE-(pyramid_height)*EXPAND_RATE;
@@ -237,77 +237,77 @@ int main(int argc, char** argv) {
   if( !FilesavingPower || !FilesavingTemp) // || !MatrixOut)
     fatal("unable to allocate memory");
 
-	// Read input data from disk
+  // Read input data from disk
   readinput(FilesavingTemp, grid_rows, grid_cols, tfile);
   readinput(FilesavingPower, grid_rows, grid_cols, pfile);
-	
-	// Load kernel source from file
-	const char *source = load_kernel_source("hotspot_kernel.cl");
-	size_t sourceSize = strlen(source);
-	
-	// Compile the kernel
+
+  // Load kernel source from file
+  const char *source = load_kernel_source("hotspot_kernel.cl");
+  size_t sourceSize = strlen(source);
+
+  // Compile the kernel
   cl_program program = clCreateProgramWithSource(context, 1, &source, &sourceSize, &error);
   if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
-	
-	char clOptions[110];
-	//  sprintf(clOptions,"-I../../src"); 
-	sprintf(clOptions," ");
+
+  char clOptions[110];
+  //  sprintf(clOptions,"-I../../src");
+  sprintf(clOptions," ");
 #ifdef BLOCK_SIZE
-	sprintf(clOptions + strlen(clOptions), " -DBLOCK_SIZE=%d", BLOCK_SIZE);
+  sprintf(clOptions + strlen(clOptions), " -DBLOCK_SIZE=%d", BLOCK_SIZE);
 #endif
 
     // Create an executable from the kernel
-	error = clBuildProgram(program, 1, &device, clOptions, NULL, NULL);
-	// Show compiler warnings/errors
-	static char log[65536]; memset(log, 0, sizeof(log));
-	clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(log)-1, log, NULL);
-	if (strstr(log,"warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);
+  error = clBuildProgram(program, 1, &device, clOptions, NULL, NULL);
+  // Show compiler warnings/errors
+  static char log[65536]; memset(log, 0, sizeof(log));
+  clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(log)-1, log, NULL);
+  if (strstr(log,"warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);
     if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
     kernel = clCreateKernel(program, "hotspot", &error);
     if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
-	
-		
-	long long start_time = get_time();
-	
-	// Create two temperature matrices and copy the temperature input data
-	cl_mem MatrixTemp[2];
-	// Create input memory buffers on device
-	MatrixTemp[0] = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, sizeof(float) * size, FilesavingTemp, &error);
-	if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
-    
+
+
+  long long start_time = get_time();
+
+  // Create two temperature matrices and copy the temperature input data
+  cl_mem MatrixTemp[2];
+  // Create input memory buffers on device
+  MatrixTemp[0] = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, sizeof(float) * size, FilesavingTemp, &error);
+  if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
+
     // Lingjie Zhang modifited at Nov 1, 2015
     //MatrixTemp[1] = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, sizeof(float) * size, NULL, &error);
     MatrixTemp[1] = clCreateBuffer(context, CL_MEM_READ_WRITE , sizeof(float) * size, NULL, &error);
     // end Lingjie Zhang modification
-    
+
     if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
-	
-	// Copy the power input data
-	cl_mem MatrixPower = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, sizeof(float) * size, FilesavingPower, &error);
-	if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
-	
-	// Perform the computation
-	int ret = compute_tran_temp(MatrixPower, MatrixTemp, grid_cols, grid_rows, total_iterations, pyramid_height,
-								blockCols, blockRows, borderCols, borderRows, FilesavingTemp, FilesavingPower);
-	
-	// Copy final temperature data back
-	cl_float *MatrixOut = (cl_float *) clEnqueueMapBuffer(command_queue, MatrixTemp[ret], CL_TRUE, CL_MAP_READ, 0, sizeof(float) * size, 0, NULL, NULL, &error);
-	if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
-	
-	long long end_time = get_time();	
-	printf("Total time: %.3f seconds\n", ((float) (end_time - start_time)) / (1000*1000));
-	
-	// Write final output to output file
+
+  // Copy the power input data
+  cl_mem MatrixPower = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, sizeof(float) * size, FilesavingPower, &error);
+  if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
+
+  // Perform the computation
+  int ret = compute_tran_temp(MatrixPower, MatrixTemp, grid_cols, grid_rows, total_iterations, pyramid_height,
+  blockCols, blockRows, borderCols, borderRows, FilesavingTemp, FilesavingPower);
+
+  // Copy final temperature data back
+  cl_float *MatrixOut = (cl_float *) clEnqueueMapBuffer(command_queue, MatrixTemp[ret], CL_TRUE, CL_MAP_READ, 0, sizeof(float) * size, 0, NULL, NULL, &error);
+  if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
+
+  long long end_time = get_time();
+  printf("Total time: %.3f seconds\n", ((float) (end_time - start_time)) / (1000*1000));
+
+  // Write final output to output file
     writeoutput(MatrixOut, grid_rows, grid_cols, ofile);
-    
-	error = clEnqueueUnmapMemObject(command_queue, MatrixTemp[ret], (void *) MatrixOut, 0, NULL, NULL);
-	if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
-	
-	clReleaseMemObject(MatrixTemp[0]);
-	clReleaseMemObject(MatrixTemp[1]);
-	clReleaseMemObject(MatrixPower);
-	
+
+  error = clEnqueueUnmapMemObject(command_queue, MatrixTemp[ret], (void *) MatrixOut, 0, NULL, NULL);
+  if (error != CL_SUCCESS) fatal_CL(error, __LINE__);
+
+  clReleaseMemObject(MatrixTemp[0]);
+  clReleaseMemObject(MatrixTemp[1]);
+  clReleaseMemObject(MatrixPower);
+
         clReleaseContext(context);
 
-	return 0;
+  return 0;
 }

+ 9 - 9
opencl/hotspot/hotspot.h

@@ -11,15 +11,15 @@
 #include <sys/time.h>
 #include <assert.h>
 
-#ifdef RD_WG_SIZE_0_0                                                            
-        #define BLOCK_SIZE RD_WG_SIZE_0_0                                        
-#elif defined(RD_WG_SIZE_0)                                                      
-        #define BLOCK_SIZE RD_WG_SIZE_0                                          
-#elif defined(RD_WG_SIZE)                                                        
-        #define BLOCK_SIZE RD_WG_SIZE                                            
-#else                                                                                    
-        #define BLOCK_SIZE 8                                                            
-#endif                                                                                   
+#ifdef RD_WG_SIZE_0_0
+        #define BLOCK_SIZE RD_WG_SIZE_0_0
+#elif defined(RD_WG_SIZE_0)
+        #define BLOCK_SIZE RD_WG_SIZE_0
+#elif defined(RD_WG_SIZE)
+        #define BLOCK_SIZE RD_WG_SIZE
+#else
+        #define BLOCK_SIZE 8
+#endif
 
 #define STR_SIZE 256
 # define EXPAND_RATE 2// add one iteration will extend the pyramid base by 2 per each borderline

+ 426 - 426
opencl/hybridsort/CLHelper.h

@@ -65,7 +65,7 @@ string FileToString(const string fileName)
             f.read(str, fileSize);
             f.close();
             str[size] = '\0';
-        
+
             s = str;
             delete [] str;
             return s;
@@ -91,49 +91,49 @@ string FileToString(const string fileName)
 //Read command line parameters
 //
 void _clCmdParams(int argc, char* argv[]){
-	for (int i =0; i < argc; ++i)
-	{
-		switch (argv[i][1])
-		{
-		case 'g':	//--g stands for size of work group
-			if (++i < argc)
-			{
-				sscanf(argv[i], "%u", &work_group_size);
-			}
-			else
-			{
-				std::cerr << "Could not read argument after option " << argv[i-1] << std::endl;
-				throw;
-			}
-			break;
-		  case 'd':	 //--d stands for device id used in computaion
-			if (++i < argc)
-			{
-				sscanf(argv[i], "%u", &device_id_inused);
-			}
-			else
-			{
-				std::cerr << "Could not read argument after option " << argv[i-1] << std::endl;
-				throw;
-			}
-			break;
-		default:
-			;
-		}
-	}
-	
+  for (int i =0; i < argc; ++i)
+  {
+  switch (argv[i][1])
+  {
+  case 'g':	//--g stands for size of work group
+  if (++i < argc)
+  {
+  sscanf(argv[i], "%u", &work_group_size);
+  }
+  else
+  {
+  std::cerr << "Could not read argument after option " << argv[i-1] << std::endl;
+  throw;
+  }
+  break;
+    case 'd':	 //--d stands for device id used in computaion
+  if (++i < argc)
+  {
+  sscanf(argv[i], "%u", &device_id_inused);
+  }
+  else
+  {
+  std::cerr << "Could not read argument after option " << argv[i-1] << std::endl;
+  throw;
+  }
+  break;
+  default:
+  ;
+  }
+  }
+
 }
 
 //---------------------------------------
 //Initlize CL objects
 //--description: there are 5 steps to initialize all the OpenCL objects needed
-//--revised on 04/01/2011: get the number of devices  and 
+//--revised on 04/01/2011: get the number of devices  and
 //  devices have no relationship with context
 void _clInit()
 {
     int DEVICE_ID_INUSED = device_id_inused;
     cl_int resultCL;
-    
+
     oclHandles.context = NULL;
     oclHandles.devices = NULL;
     oclHandles.queue = NULL;
@@ -174,7 +174,7 @@ void _clInit()
         if (resultCL != CL_SUCCESS)
             throw (string("InitCL()::Error: Getting platform info (clGetPlatformInfo)"));
 
-		//printf("vedor is %s\n",pbuff);
+  //printf("vedor is %s\n",pbuff);
 
     }
     free(allPlatforms);
@@ -182,20 +182,20 @@ void _clInit()
     //-----------------------------------------------
     //--cambine-2: create an OpenCL context
     cl_context_properties cprops[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)targetPlatform, 0 };
-    oclHandles.context = clCreateContextFromType(cprops, 
-                                                CL_DEVICE_TYPE_CPU, 
-                                                NULL, 
-                                                NULL, 
+    oclHandles.context = clCreateContextFromType(cprops,
+                                                CL_DEVICE_TYPE_CPU,
+                                                NULL,
+                                                NULL,
                                                 &resultCL);
 
     if ((resultCL != CL_SUCCESS) || (oclHandles.context == NULL))
         throw (string("InitCL()::Error: Creating Context (clCreateContextFromType)"));
     //-----------------------------------------------
-    //--cambine-3: detect OpenCL devices	
+    //--cambine-3: detect OpenCL devices
     /* First, get the size of device list */
    oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_CPU, 0, NULL, &deviceListSize);
    if(oclHandles.cl_status!=CL_SUCCESS){
-   	throw(string("exception in _clInit -> clGetDeviceIDs"));   	
+   	throw(string("exception in _clInit -> clGetDeviceIDs"));
    }
    if (deviceListSize == 0)
         throw(string("InitCL()::Error: No devices found."));
@@ -210,15 +210,15 @@ void _clInit()
 
     /* Next, get the device list data */
    oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_CPU, deviceListSize, \
-								oclHandles.devices, NULL);
+  oclHandles.devices, NULL);
    if(oclHandles.cl_status!=CL_SUCCESS){
-   	throw(string("exception in _clInit -> clGetDeviceIDs-2"));   	
+   	throw(string("exception in _clInit -> clGetDeviceIDs-2"));
    }
    //-----------------------------------------------
-   //--cambine-4: Create an OpenCL command queue    
-    oclHandles.queue = clCreateCommandQueue(oclHandles.context, 
-                                            oclHandles.devices[DEVICE_ID_INUSED], 
-                                            0, 
+   //--cambine-4: Create an OpenCL command queue
+    oclHandles.queue = clCreateCommandQueue(oclHandles.context,
+                                            oclHandles.devices[DEVICE_ID_INUSED],
+                                            0,
                                             &resultCL);
 
     if ((resultCL != CL_SUCCESS) || (oclHandles.queue == NULL))
@@ -229,14 +229,14 @@ void _clInit()
     const char * source    = source_str.c_str();
     size_t sourceSize[]    = { source_str.length() };
 
-    oclHandles.program = clCreateProgramWithSource(oclHandles.context, 
-                                                    1, 
+    oclHandles.program = clCreateProgramWithSource(oclHandles.context,
+                                                    1,
                                                     &source,
                                                     sourceSize,
                                                     &resultCL);
 
     if ((resultCL != CL_SUCCESS) || (oclHandles.program == NULL))
-        throw(string("InitCL()::Error: Loading Binary into cl_program. (clCreateProgramWithBinary)"));    
+        throw(string("InitCL()::Error: Loading Binary into cl_program. (clCreateProgramWithBinary)"));
     //insert debug information
     //std::string options= "-cl-nv-verbose"; //Doesn't work on AMD machines
     //options += " -cl-nv-opt-level=3";
@@ -246,46 +246,46 @@ void _clInit()
     {
         cerr << "InitCL()::Error: In clBuildProgram" << endl;
 
-		size_t length;
-        resultCL = clGetProgramBuildInfo(oclHandles.program, 
-                                        oclHandles.devices[DEVICE_ID_INUSED], 
-                                        CL_PROGRAM_BUILD_LOG, 
-                                        0, 
-                                        NULL, 
+  size_t length;
+        resultCL = clGetProgramBuildInfo(oclHandles.program,
+                                        oclHandles.devices[DEVICE_ID_INUSED],
+                                        CL_PROGRAM_BUILD_LOG,
+                                        0,
+                                        NULL,
                                         &length);
-        if(resultCL != CL_SUCCESS) 
+        if(resultCL != CL_SUCCESS)
             throw(string("InitCL()::Error: Getting Program build info(clGetProgramBuildInfo)"));
 
-		char* buffer = (char*)malloc(length);
-        resultCL = clGetProgramBuildInfo(oclHandles.program, 
-                                        oclHandles.devices[DEVICE_ID_INUSED], 
-                                        CL_PROGRAM_BUILD_LOG, 
-                                        length, 
-                                        buffer, 
+  char* buffer = (char*)malloc(length);
+        resultCL = clGetProgramBuildInfo(oclHandles.program,
+                                        oclHandles.devices[DEVICE_ID_INUSED],
+                                        CL_PROGRAM_BUILD_LOG,
+                                        length,
+                                        buffer,
                                         NULL);
-        if(resultCL != CL_SUCCESS) 
+        if(resultCL != CL_SUCCESS)
             throw(string("InitCL()::Error: Getting Program build info(clGetProgramBuildInfo)"));
 
-		cerr << buffer << endl;
+  cerr << buffer << endl;
         free(buffer);
 
         throw(string("InitCL()::Error: Building Program (clBuildProgram)"));
-    } 
+    }
 
     //get program information in intermediate representation
-    #ifdef PTX_MSG    
+    #ifdef PTX_MSG
     size_t binary_sizes[deviceListSize];
     char * binaries[deviceListSize];
-    //figure out number of devices and the sizes of the binary for each device. 
+    //figure out number of devices and the sizes of the binary for each device.
     oclHandles.cl_status = clGetProgramInfo(oclHandles.program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*deviceListSize, &binary_sizes, NULL );
     if(oclHandles.cl_status!=CL_SUCCESS){
         throw(string("--cambine:exception in _InitCL -> clGetProgramInfo-2"));
     }
 
     std::cout<<"--cambine:"<<binary_sizes<<std::endl;
-    //copy over all of the generated binaries. 
+    //copy over all of the generated binaries.
     for(int i=0;i<deviceListSize;i++)
-	binaries[i] = (char *)malloc( sizeof(char)*(binary_sizes[i]+1));
+  binaries[i] = (char *)malloc( sizeof(char)*(binary_sizes[i]+1));
     oclHandles.cl_status = clGetProgramInfo(oclHandles.program, CL_PROGRAM_BINARIES, sizeof(char *)*deviceListSize, binaries, NULL );
     if(oclHandles.cl_status!=CL_SUCCESS){
         throw(string("--cambine:exception in _InitCL -> clGetProgramInfo-3"));
@@ -295,13 +295,13 @@ void _clInit()
     std::cout<<"--cambine:writing ptd information..."<<std::endl;
     FILE * ptx_file = fopen("cl.ptx","w");
     if(ptx_file==NULL){
-	throw(string("exceptions in allocate ptx file."));
+  throw(string("exceptions in allocate ptx file."));
     }
     fprintf(ptx_file,"%s",binaries[DEVICE_ID_INUSED]);
     fclose(ptx_file);
     std::cout<<"--cambine:writing ptd information done."<<std::endl;
     for(int i=0;i<deviceListSize;i++)
-	free(binaries[i]);
+  free(binaries[i]);
     #endif
 
     for (int nKernel = 0; nKernel < total_kernels; nKernel++)
@@ -325,13 +325,13 @@ void _clInit()
     size_t ret_val_size;
     oclHandles.cl_status = clGetProgramBuildInfo(oclHandles.program, oclHandles.devices[DEVICE_ID_INUSED], CL_PROGRAM_BUILD_LOG, 0, NULL, &ret_val_size);
     if(oclHandles.cl_status!=CL_SUCCESS){
-	throw(string("exceptions in _InitCL -> getting resource information"));
-    }    
+  throw(string("exceptions in _InitCL -> getting resource information"));
+    }
 
     build_log = (char *)malloc(ret_val_size+1);
     oclHandles.cl_status = clGetProgramBuildInfo(oclHandles.program, oclHandles.devices[DEVICE_ID_INUSED], CL_PROGRAM_BUILD_LOG, ret_val_size, build_log, NULL);
     if(oclHandles.cl_status!=CL_SUCCESS){
-	throw(string("exceptions in _InitCL -> getting resources allocation information-2"));
+  throw(string("exceptions in _InitCL -> getting resources allocation information-2"));
     }
     build_log[ret_val_size] = '\0';
     std::cout<<"--cambine:"<<build_log<<std::endl;
@@ -400,415 +400,415 @@ void _clRelease()
 //--------------------------------------------------------
 //--cambine:create buffer and then copy data from host to device
 cl_mem _clCreateAndCpyMem(int size, void * h_mem_source) throw(string){
-	cl_mem d_mem;
-	d_mem = clCreateBuffer(oclHandles.context,	CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR,  \
-									size, h_mem_source, &oclHandles.cl_status);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clCreateAndCpyMem()"));
-	#endif
-	return d_mem;
+  cl_mem d_mem;
+  d_mem = clCreateBuffer(oclHandles.context,	CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR,  \
+  size, h_mem_source, &oclHandles.cl_status);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clCreateAndCpyMem()"));
+  #endif
+  return d_mem;
 }
 //-------------------------------------------------------
 //--cambine:	create read only  buffer for devices
-//--date:	17/01/2011	
+//--date:	17/01/2011
 cl_mem _clMallocRW(int size, void * h_mem_ptr) throw(string){
  	cl_mem d_mem;
-	d_mem = clCreateBuffer(oclHandles.context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, size, h_mem_ptr, &oclHandles.cl_status);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clMallocRW"));
-	#endif
-	return d_mem;
+  d_mem = clCreateBuffer(oclHandles.context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, size, h_mem_ptr, &oclHandles.cl_status);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clMallocRW"));
+  #endif
+  return d_mem;
 }
 //-------------------------------------------------------
 //--cambine:	create read and write buffer for devices
-//--date:	17/01/2011	
+//--date:	17/01/2011
 cl_mem _clMalloc(int size, void * h_mem_ptr) throw(string){
  	cl_mem d_mem;
-	d_mem = clCreateBuffer(oclHandles.context, CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR, size, h_mem_ptr, &oclHandles.cl_status);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clMalloc"));
-	#endif
-	return d_mem;
+  d_mem = clCreateBuffer(oclHandles.context, CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR, size, h_mem_ptr, &oclHandles.cl_status);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clMalloc"));
+  #endif
+  return d_mem;
 }
 
 //-------------------------------------------------------
 //--cambine:	transfer data from host to device
 //--date:	17/01/2011
 void _clMemcpyH2D(cl_mem d_mem, int size, const void *h_mem_ptr) throw(string){
-	oclHandles.cl_status = clEnqueueWriteBuffer(oclHandles.queue, d_mem, CL_TRUE, 0, size, h_mem_ptr, 0, NULL, NULL);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clMemcpyH2D"));
-	#endif
+  oclHandles.cl_status = clEnqueueWriteBuffer(oclHandles.queue, d_mem, CL_TRUE, 0, size, h_mem_ptr, 0, NULL, NULL);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clMemcpyH2D"));
+  #endif
 }
 //--------------------------------------------------------
-//--cambine:create buffer and then copy data from host to device with pinned 
+//--cambine:create buffer and then copy data from host to device with pinned
 // memory
 cl_mem _clCreateAndCpyPinnedMem(int size, float* h_mem_source) throw(string){
-	cl_mem d_mem, d_mem_pinned;
-	float * h_mem_pinned = NULL;
-	d_mem_pinned = clCreateBuffer(oclHandles.context,	CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR,  \
-									size, NULL, &oclHandles.cl_status);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clCreateAndCpyMem()->d_mem_pinned"));
-	#endif
-	//------------
-	d_mem = clCreateBuffer(oclHandles.context,	CL_MEM_READ_ONLY,  \
-									size, NULL, &oclHandles.cl_status);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clCreateAndCpyMem() -> d_mem "));
-	#endif
-	//----------
-	h_mem_pinned = (cl_float *)clEnqueueMapBuffer(oclHandles.queue, d_mem_pinned, CL_TRUE,  \
-										CL_MAP_WRITE, 0, size, 0, NULL,  \
-										NULL,  &oclHandles.cl_status);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clCreateAndCpyMem() -> clEnqueueMapBuffer"));
-	#endif
-	int element_number = size/sizeof(float);
-	#pragma omp parallel for
-	for(int i=0;i<element_number;i++){
-		h_mem_pinned[i] = h_mem_source[i];
-	}
-	//----------
-	oclHandles.cl_status = clEnqueueWriteBuffer(oclHandles.queue, d_mem, 	\
-									CL_TRUE, 0, size, h_mem_pinned,  \
-									0, NULL, NULL);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clCreateAndCpyMem() -> clEnqueueWriteBuffer"));
-	#endif
-	
-	return d_mem;
+  cl_mem d_mem, d_mem_pinned;
+  float * h_mem_pinned = NULL;
+  d_mem_pinned = clCreateBuffer(oclHandles.context,	CL_MEM_READ_ONLY|CL_MEM_ALLOC_HOST_PTR,  \
+  size, NULL, &oclHandles.cl_status);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clCreateAndCpyMem()->d_mem_pinned"));
+  #endif
+  //------------
+  d_mem = clCreateBuffer(oclHandles.context,	CL_MEM_READ_ONLY,  \
+  size, NULL, &oclHandles.cl_status);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clCreateAndCpyMem() -> d_mem "));
+  #endif
+  //----------
+  h_mem_pinned = (cl_float *)clEnqueueMapBuffer(oclHandles.queue, d_mem_pinned, CL_TRUE,  \
+  CL_MAP_WRITE, 0, size, 0, NULL,  \
+  NULL,  &oclHandles.cl_status);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clCreateAndCpyMem() -> clEnqueueMapBuffer"));
+  #endif
+  int element_number = size/sizeof(float);
+  #pragma omp parallel for
+  for(int i=0;i<element_number;i++){
+  h_mem_pinned[i] = h_mem_source[i];
+  }
+  //----------
+  oclHandles.cl_status = clEnqueueWriteBuffer(oclHandles.queue, d_mem, 	\
+  CL_TRUE, 0, size, h_mem_pinned,  \
+  0, NULL, NULL);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clCreateAndCpyMem() -> clEnqueueWriteBuffer"));
+  #endif
+
+  return d_mem;
 }
 
 
 //--------------------------------------------------------
 //--cambine:create write only buffer on device
 cl_mem _clMallocWO(int size) throw(string){
-	cl_mem d_mem;
-	d_mem = clCreateBuffer(oclHandles.context, CL_MEM_WRITE_ONLY, size, 0, &oclHandles.cl_status);
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(string("excpetion in _clCreateMem()"));
-	#endif
-	return d_mem;
+  cl_mem d_mem;
+  d_mem = clCreateBuffer(oclHandles.context, CL_MEM_WRITE_ONLY, size, 0, &oclHandles.cl_status);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(string("excpetion in _clCreateMem()"));
+  #endif
+  return d_mem;
 }
 
 //--------------------------------------------------------
 //transfer data from device to host
 void _clMemcpyD2H(cl_mem d_mem, int size, void * h_mem) throw(string){
-	oclHandles.cl_status = clEnqueueReadBuffer(oclHandles.queue, d_mem, CL_TRUE, 0, size, h_mem, 0,0,0);
-	#ifdef ERRMSG
-		oclHandles.error_str = "excpetion in _clCpyMemD2H -> ";
-		switch(oclHandles.cl_status){
-			case CL_INVALID_COMMAND_QUEUE:
-				oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-				break;
-			case CL_INVALID_CONTEXT:
-				oclHandles.error_str += "CL_INVALID_CONTEXT";
-				break;	
-			case CL_INVALID_MEM_OBJECT:
-				oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
-				break;
-			case CL_INVALID_VALUE:
-				oclHandles.error_str += "CL_INVALID_VALUE";
-				break;	
-			case CL_INVALID_EVENT_WAIT_LIST:
-				oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
-				break;
-			case CL_MEM_OBJECT_ALLOCATION_FAILURE:
-				oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
-				break;	
-			case CL_OUT_OF_HOST_MEMORY:
-				oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-				break;		
-			default:
-				oclHandles.error_str += "Unknown reason";
-				break;
-		}
-		if(oclHandles.cl_status != CL_SUCCESS)
-			throw(oclHandles.error_str);
-	#endif
+  oclHandles.cl_status = clEnqueueReadBuffer(oclHandles.queue, d_mem, CL_TRUE, 0, size, h_mem, 0,0,0);
+  #ifdef ERRMSG
+  oclHandles.error_str = "excpetion in _clCpyMemD2H -> ";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_COMMAND_QUEUE:
+  oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+  break;
+  case CL_INVALID_CONTEXT:
+  oclHandles.error_str += "CL_INVALID_CONTEXT";
+  break;
+  case CL_INVALID_MEM_OBJECT:
+  oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
+  break;
+  case CL_INVALID_VALUE:
+  oclHandles.error_str += "CL_INVALID_VALUE";
+  break;
+  case CL_INVALID_EVENT_WAIT_LIST:
+  oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
+  break;
+  case CL_MEM_OBJECT_ALLOCATION_FAILURE:
+  oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unknown reason";
+  break;
+  }
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(oclHandles.error_str);
+  #endif
 }
 
 //--------------------------------------------------------
 //set kernel arguments
 void _clSetArgs(int kernel_id, int arg_idx, void * d_mem, int size = 0) throw(string){
-	if(!size){
-		oclHandles.cl_status = clSetKernelArg(oclHandles.kernel[kernel_id], arg_idx, sizeof(d_mem), &d_mem);
-		#ifdef ERRMSG
-		oclHandles.error_str = "excpetion in _clSetKernelArg() ";
-		switch(oclHandles.cl_status){
-			case CL_INVALID_KERNEL:
-				oclHandles.error_str += "CL_INVALID_KERNEL";
-				break;
-			case CL_INVALID_ARG_INDEX:
-				oclHandles.error_str += "CL_INVALID_ARG_INDEX";
-				break;	
-			case CL_INVALID_ARG_VALUE:
-				oclHandles.error_str += "CL_INVALID_ARG_VALUE";
-				break;
-			case CL_INVALID_MEM_OBJECT:
-				oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
-				break;	
-			case CL_INVALID_SAMPLER:
-				oclHandles.error_str += "CL_INVALID_SAMPLER";
-				break;
-			case CL_INVALID_ARG_SIZE:
-				oclHandles.error_str += "CL_INVALID_ARG_SIZE";
-				break;	
-			case CL_OUT_OF_RESOURCES:
-				oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-				break;
-			case CL_OUT_OF_HOST_MEMORY:
-				oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-				break;
-			default:
-				oclHandles.error_str += "Unknown reason";
-				break;
-		}
-		if(oclHandles.cl_status != CL_SUCCESS)
-			throw(oclHandles.error_str);
-		#endif
-	}
-	else{
-		oclHandles.cl_status = clSetKernelArg(oclHandles.kernel[kernel_id], arg_idx, size, d_mem);
-		#ifdef ERRMSG
-		oclHandles.error_str = "excpetion in _clSetKernelArg() ";
-		switch(oclHandles.cl_status){
-			case CL_INVALID_KERNEL:
-				oclHandles.error_str += "CL_INVALID_KERNEL";
-				break;
-			case CL_INVALID_ARG_INDEX:
-				oclHandles.error_str += "CL_INVALID_ARG_INDEX";
-				break;	
-			case CL_INVALID_ARG_VALUE:
-				oclHandles.error_str += "CL_INVALID_ARG_VALUE";
-				break;
-			case CL_INVALID_MEM_OBJECT:
-				oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
-				break;	
-			case CL_INVALID_SAMPLER:
-				oclHandles.error_str += "CL_INVALID_SAMPLER";
-				break;
-			case CL_INVALID_ARG_SIZE:
-				oclHandles.error_str += "CL_INVALID_ARG_SIZE";
-				break;	
-			case CL_OUT_OF_RESOURCES:
-				oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-				break;
-			case CL_OUT_OF_HOST_MEMORY:
-				oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-				break;
-			default:
-				oclHandles.error_str += "Unknown reason";
-				break;
-		}
-		if(oclHandles.cl_status != CL_SUCCESS)
-			throw(oclHandles.error_str);
-		#endif
-	}
+  if(!size){
+  oclHandles.cl_status = clSetKernelArg(oclHandles.kernel[kernel_id], arg_idx, sizeof(d_mem), &d_mem);
+  #ifdef ERRMSG
+  oclHandles.error_str = "excpetion in _clSetKernelArg() ";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_KERNEL:
+  oclHandles.error_str += "CL_INVALID_KERNEL";
+  break;
+  case CL_INVALID_ARG_INDEX:
+  oclHandles.error_str += "CL_INVALID_ARG_INDEX";
+  break;
+  case CL_INVALID_ARG_VALUE:
+  oclHandles.error_str += "CL_INVALID_ARG_VALUE";
+  break;
+  case CL_INVALID_MEM_OBJECT:
+  oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
+  break;
+  case CL_INVALID_SAMPLER:
+  oclHandles.error_str += "CL_INVALID_SAMPLER";
+  break;
+  case CL_INVALID_ARG_SIZE:
+  oclHandles.error_str += "CL_INVALID_ARG_SIZE";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unknown reason";
+  break;
+  }
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(oclHandles.error_str);
+  #endif
+  }
+  else{
+  oclHandles.cl_status = clSetKernelArg(oclHandles.kernel[kernel_id], arg_idx, size, d_mem);
+  #ifdef ERRMSG
+  oclHandles.error_str = "excpetion in _clSetKernelArg() ";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_KERNEL:
+  oclHandles.error_str += "CL_INVALID_KERNEL";
+  break;
+  case CL_INVALID_ARG_INDEX:
+  oclHandles.error_str += "CL_INVALID_ARG_INDEX";
+  break;
+  case CL_INVALID_ARG_VALUE:
+  oclHandles.error_str += "CL_INVALID_ARG_VALUE";
+  break;
+  case CL_INVALID_MEM_OBJECT:
+  oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
+  break;
+  case CL_INVALID_SAMPLER:
+  oclHandles.error_str += "CL_INVALID_SAMPLER";
+  break;
+  case CL_INVALID_ARG_SIZE:
+  oclHandles.error_str += "CL_INVALID_ARG_SIZE";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unknown reason";
+  break;
+  }
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(oclHandles.error_str);
+  #endif
+  }
 }
 void _clFinish() throw(string){
-	oclHandles.cl_status = clFinish(oclHandles.queue);	
-	#ifdef ERRMSG
-	oclHandles.error_str = "excpetion in _clFinish";
-	switch(oclHandles.cl_status){
-		case CL_INVALID_COMMAND_QUEUE:
-			oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-			break;
-		case CL_OUT_OF_RESOURCES:
-			oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-			break;		
-		case CL_OUT_OF_HOST_MEMORY:
-			oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-			break;
-		default:
-			oclHandles.error_str += "Unknown reasons";
-			break;
-
-	}
-	if(oclHandles.cl_status!=CL_SUCCESS){
-		throw(oclHandles.error_str);
-	}
-	#endif
+  oclHandles.cl_status = clFinish(oclHandles.queue);
+  #ifdef ERRMSG
+  oclHandles.error_str = "excpetion in _clFinish";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_COMMAND_QUEUE:
+  oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unknown reasons";
+  break;
+
+  }
+  if(oclHandles.cl_status!=CL_SUCCESS){
+  throw(oclHandles.error_str);
+  }
+  #endif
 }
 //--------------------------------------------------------
 //--cambine:enqueue kernel
 void _clInvokeKernel(int kernel_id, int work_items, int work_group_size) throw(string){
-	cl_uint work_dim = WORK_DIM;
-	cl_event e[1];
-	if(work_items%work_group_size != 0)	//process situations that work_items cannot be divided by work_group_size
-	  work_items = work_items + (work_group_size-(work_items%work_group_size));
+  cl_uint work_dim = WORK_DIM;
+  cl_event e[1];
+  if(work_items%work_group_size != 0)	//process situations that work_items cannot be divided by work_group_size
+    work_items = work_items + (work_group_size-(work_items%work_group_size));
   	size_t local_work_size[] = {work_group_size, 1};
-	size_t global_work_size[] = {work_items, 1};
-	oclHandles.cl_status = clEnqueueNDRangeKernel(oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0, \
-											global_work_size, local_work_size, 0 , 0, &(e[0]) );	
-	#ifdef ERRMSG
-	oclHandles.error_str = "excpetion in _clInvokeKernel() -> ";
-	switch(oclHandles.cl_status)
-	{
-		case CL_INVALID_PROGRAM_EXECUTABLE:
-			oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE";
-			break;
-		case CL_INVALID_COMMAND_QUEUE:
-			oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-			break;
-		case CL_INVALID_KERNEL:
-			oclHandles.error_str += "CL_INVALID_KERNEL";
-			break;
-		case CL_INVALID_CONTEXT:
-			oclHandles.error_str += "CL_INVALID_CONTEXT";
-			break;
-		case CL_INVALID_KERNEL_ARGS:
-			oclHandles.error_str += "CL_INVALID_KERNEL_ARGS";
-			break;
-		case CL_INVALID_WORK_DIMENSION:
-			oclHandles.error_str += "CL_INVALID_WORK_DIMENSION";
-			break;
-		case CL_INVALID_GLOBAL_WORK_SIZE:
-			oclHandles.error_str += "CL_INVALID_GLOBAL_WORK_SIZE";
-			break;
-		case CL_INVALID_WORK_GROUP_SIZE:
-			oclHandles.error_str += "CL_INVALID_WORK_GROUP_SIZE";
-			break;
-		case CL_INVALID_WORK_ITEM_SIZE:
-			oclHandles.error_str += "CL_INVALID_WORK_ITEM_SIZE";
-			break;
-		case CL_INVALID_GLOBAL_OFFSET:
-			oclHandles.error_str += "CL_INVALID_GLOBAL_OFFSET";
-			break;
-		case CL_OUT_OF_RESOURCES:
-			oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-			break;
-		case CL_MEM_OBJECT_ALLOCATION_FAILURE:
-			oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
-			break;
-		case CL_INVALID_EVENT_WAIT_LIST:
-			oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
-			break;
-		case CL_OUT_OF_HOST_MEMORY:
-			oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-			break;
-		default: 
-			oclHandles.error_str += "Unkown reseason";
-			break;		
-	}
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(oclHandles.error_str);	
-	#endif
-	//_clFinish();
-	// oclHandles.cl_status = clWaitForEvents(1, &e[0]);
-	// #ifdef ERRMSG
+  size_t global_work_size[] = {work_items, 1};
+  oclHandles.cl_status = clEnqueueNDRangeKernel(oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0, \
+  global_work_size, local_work_size, 0 , 0, &(e[0]) );
+  #ifdef ERRMSG
+  oclHandles.error_str = "excpetion in _clInvokeKernel() -> ";
+  switch(oclHandles.cl_status)
+  {
+  case CL_INVALID_PROGRAM_EXECUTABLE:
+  oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE";
+  break;
+  case CL_INVALID_COMMAND_QUEUE:
+  oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+  break;
+  case CL_INVALID_KERNEL:
+  oclHandles.error_str += "CL_INVALID_KERNEL";
+  break;
+  case CL_INVALID_CONTEXT:
+  oclHandles.error_str += "CL_INVALID_CONTEXT";
+  break;
+  case CL_INVALID_KERNEL_ARGS:
+  oclHandles.error_str += "CL_INVALID_KERNEL_ARGS";
+  break;
+  case CL_INVALID_WORK_DIMENSION:
+  oclHandles.error_str += "CL_INVALID_WORK_DIMENSION";
+  break;
+  case CL_INVALID_GLOBAL_WORK_SIZE:
+  oclHandles.error_str += "CL_INVALID_GLOBAL_WORK_SIZE";
+  break;
+  case CL_INVALID_WORK_GROUP_SIZE:
+  oclHandles.error_str += "CL_INVALID_WORK_GROUP_SIZE";
+  break;
+  case CL_INVALID_WORK_ITEM_SIZE:
+  oclHandles.error_str += "CL_INVALID_WORK_ITEM_SIZE";
+  break;
+  case CL_INVALID_GLOBAL_OFFSET:
+  oclHandles.error_str += "CL_INVALID_GLOBAL_OFFSET";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_MEM_OBJECT_ALLOCATION_FAILURE:
+  oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
+  break;
+  case CL_INVALID_EVENT_WAIT_LIST:
+  oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unkown reseason";
+  break;
+  }
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(oclHandles.error_str);
+  #endif
+  //_clFinish();
+  // oclHandles.cl_status = clWaitForEvents(1, &e[0]);
+  // #ifdef ERRMSG
         // if (oclHandles.cl_status!= CL_SUCCESS)
         //     throw(string("excpetion in _clEnqueueNDRange() -> clWaitForEvents"));
-	// #endif
+  // #endif
 }
 void _clInvokeKernel2D(int kernel_id, int range_x, int range_y, int group_x, int group_y) throw(string){
-	cl_uint work_dim = WORK_DIM;
-	size_t local_work_size[] = {group_x, group_y};
-	size_t global_work_size[] = {range_x, range_y};
-	cl_event e[1];
-	/*if(work_items%work_group_size != 0)	//process situations that work_items cannot be divided by work_group_size
-	  work_items = work_items + (work_group_size-(work_items%work_group_size));*/
-	oclHandles.cl_status = clEnqueueNDRangeKernel(oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0, \
-											global_work_size, local_work_size, 0 , 0, &(e[0]) );	
-	#ifdef ERRMSG
-	oclHandles.error_str = "excpetion in _clInvokeKernel() -> ";
-	switch(oclHandles.cl_status)
-	{
-		case CL_INVALID_PROGRAM_EXECUTABLE:
-			oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE";
-			break;
-		case CL_INVALID_COMMAND_QUEUE:
-			oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-			break;
-		case CL_INVALID_KERNEL:
-			oclHandles.error_str += "CL_INVALID_KERNEL";
-			break;
-		case CL_INVALID_CONTEXT:
-			oclHandles.error_str += "CL_INVALID_CONTEXT";
-			break;
-		case CL_INVALID_KERNEL_ARGS:
-			oclHandles.error_str += "CL_INVALID_KERNEL_ARGS";
-			break;
-		case CL_INVALID_WORK_DIMENSION:
-			oclHandles.error_str += "CL_INVALID_WORK_DIMENSION";
-			break;
-		case CL_INVALID_GLOBAL_WORK_SIZE:
-			oclHandles.error_str += "CL_INVALID_GLOBAL_WORK_SIZE";
-			break;
-		case CL_INVALID_WORK_GROUP_SIZE:
-			oclHandles.error_str += "CL_INVALID_WORK_GROUP_SIZE";
-			break;
-		case CL_INVALID_WORK_ITEM_SIZE:
-			oclHandles.error_str += "CL_INVALID_WORK_ITEM_SIZE";
-			break;
-		case CL_INVALID_GLOBAL_OFFSET:
-			oclHandles.error_str += "CL_INVALID_GLOBAL_OFFSET";
-			break;
-		case CL_OUT_OF_RESOURCES:
-			oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-			break;
-		case CL_MEM_OBJECT_ALLOCATION_FAILURE:
-			oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
-			break;
-		case CL_INVALID_EVENT_WAIT_LIST:
-			oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
-			break;
-		case CL_OUT_OF_HOST_MEMORY:
-			oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-			break;
-		default: 
-			oclHandles.error_str += "Unkown reseason";
-			break;		
-	}
-	if(oclHandles.cl_status != CL_SUCCESS)
-		throw(oclHandles.error_str);	
-	#endif
-	//_clFinish();
-	/*oclHandles.cl_status = clWaitForEvents(1, &e[0]);
-
-	#ifdef ERRMSG
+  cl_uint work_dim = WORK_DIM;
+  size_t local_work_size[] = {group_x, group_y};
+  size_t global_work_size[] = {range_x, range_y};
+  cl_event e[1];
+  /*if(work_items%work_group_size != 0)	//process situations that work_items cannot be divided by work_group_size
+    work_items = work_items + (work_group_size-(work_items%work_group_size));*/
+  oclHandles.cl_status = clEnqueueNDRangeKernel(oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0, \
+  global_work_size, local_work_size, 0 , 0, &(e[0]) );
+  #ifdef ERRMSG
+  oclHandles.error_str = "excpetion in _clInvokeKernel() -> ";
+  switch(oclHandles.cl_status)
+  {
+  case CL_INVALID_PROGRAM_EXECUTABLE:
+  oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE";
+  break;
+  case CL_INVALID_COMMAND_QUEUE:
+  oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+  break;
+  case CL_INVALID_KERNEL:
+  oclHandles.error_str += "CL_INVALID_KERNEL";
+  break;
+  case CL_INVALID_CONTEXT:
+  oclHandles.error_str += "CL_INVALID_CONTEXT";
+  break;
+  case CL_INVALID_KERNEL_ARGS:
+  oclHandles.error_str += "CL_INVALID_KERNEL_ARGS";
+  break;
+  case CL_INVALID_WORK_DIMENSION:
+  oclHandles.error_str += "CL_INVALID_WORK_DIMENSION";
+  break;
+  case CL_INVALID_GLOBAL_WORK_SIZE:
+  oclHandles.error_str += "CL_INVALID_GLOBAL_WORK_SIZE";
+  break;
+  case CL_INVALID_WORK_GROUP_SIZE:
+  oclHandles.error_str += "CL_INVALID_WORK_GROUP_SIZE";
+  break;
+  case CL_INVALID_WORK_ITEM_SIZE:
+  oclHandles.error_str += "CL_INVALID_WORK_ITEM_SIZE";
+  break;
+  case CL_INVALID_GLOBAL_OFFSET:
+  oclHandles.error_str += "CL_INVALID_GLOBAL_OFFSET";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_MEM_OBJECT_ALLOCATION_FAILURE:
+  oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
+  break;
+  case CL_INVALID_EVENT_WAIT_LIST:
+  oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unkown reseason";
+  break;
+  }
+  if(oclHandles.cl_status != CL_SUCCESS)
+  throw(oclHandles.error_str);
+  #endif
+  //_clFinish();
+  /*oclHandles.cl_status = clWaitForEvents(1, &e[0]);
+
+  #ifdef ERRMSG
 
         if (oclHandles.cl_status!= CL_SUCCESS)
 
             throw(string("excpetion in _clEnqueueNDRange() -> clWaitForEvents"));
 
-	#endif*/
+  #endif*/
 }
 
 //--------------------------------------------------------
 //release OpenCL objects
 void _clFree(cl_mem ob) throw(string){
-	if(ob!=NULL)
-		oclHandles.cl_status = clReleaseMemObject(ob);	
-	#ifdef ERRMSG
-	oclHandles.error_str = "excpetion in _clFree() ->";
-	switch(oclHandles.cl_status)
-	{
-		case CL_INVALID_MEM_OBJECT:
-			oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
-			break;
-		case CL_OUT_OF_RESOURCES:
-			oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-			break;
-		case CL_OUT_OF_HOST_MEMORY:
-			oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-			break;			
-		default: 
-			oclHandles.error_str += "Unkown reseason";
-			break;		
-	}        
+  if(ob!=NULL)
+  oclHandles.cl_status = clReleaseMemObject(ob);
+  #ifdef ERRMSG
+  oclHandles.error_str = "excpetion in _clFree() ->";
+  switch(oclHandles.cl_status)
+  {
+  case CL_INVALID_MEM_OBJECT:
+  oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unkown reseason";
+  break;
+  }
     if (oclHandles.cl_status!= CL_SUCCESS)
        throw(oclHandles.error_str);
-	#endif
+  #endif
 }
 #endif //_CL_HELPER_

+ 127 - 127
opencl/hybridsort/bucketsort.c

@@ -32,8 +32,8 @@
 // Forward declarations
 ////////////////////////////////////////////////////////////////////////////////
 void calcPivotPoints(float *histogram, int histosize, int listsize,
-					 int divisions, float min, float max, float *pivotPoints,
-					 float histo_width);
+   int divisions, float min, float max, float *pivotPoints,
+   float histo_width);
 
 ////////////////////////////////////////////////////////////////////////////////
 // Globals
@@ -88,49 +88,49 @@ void init_bucketsort(int listsize, int platform_id, int device_id, int use_gpu)
     clGetPlatformIDs(0, NULL, &num);
     cl_platform_id platformID[num];
     clGetPlatformIDs(num, platformID, NULL);
-    
+
     // Selector for CPU/GPU
     cl_device_type device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
-    
+
     clGetDeviceIDs(platformID[platform_id],device_type,0,NULL,&num);
-    
+
     cl_device_id devices[num];
     err = clGetDeviceIDs(platformID[platform_id],device_type,num,devices,NULL);
 //    int gpu = 1;
 //    err = clGetDeviceIDs(NULL, gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU, 2, &device_id, NULL);
-    
+
     if (err != CL_SUCCESS)
     {
         printf("Error: Failed to create a device group!\n");
         exit(1);
     }
     char name[128];
-    
-    
+
+
     clGetDeviceInfo(devices[device_id],CL_DEVICE_NAME,128,name,NULL);
 
-    
+
     bucketContext = clCreateContext(0, 1, &devices[device_id], NULL, NULL, &err);
 
     bucketCommands = clCreateCommandQueue(bucketContext, devices[device_id], CL_QUEUE_PROFILING_ENABLE, &err);
-    
-	h_offsets = (unsigned int *) malloc(DIVISIONS * sizeof(unsigned int));
+
+  h_offsets = (unsigned int *) malloc(DIVISIONS * sizeof(unsigned int));
     for(int i = 0; i < DIVISIONS; i++){
         h_offsets[i] = 0;
     }
     d_offsets_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, DIVISIONS * sizeof(unsigned int),NULL,NULL);
-	pivotPoints = (float *)malloc(DIVISIONS * sizeof(float));
-    
+  pivotPoints = (float *)malloc(DIVISIONS * sizeof(float));
+
     d_indice_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, listsize * sizeof(int),NULL,NULL);
     d_indice_input_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, listsize * sizeof(int),NULL,NULL);
     d_indice = (int *)malloc(listsize * sizeof(int));
-	historesult = (float *)malloc(histosize * sizeof(float));
+  historesult = (float *)malloc(histosize * sizeof(float));
     l_pivotpoints = (float *)malloc(DIVISIONS*sizeof(float));
-	l_pivotpoints_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, DIVISIONS * sizeof(float), NULL, NULL);
-	l_offsets_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, DIVISIONS * sizeof(unsigned int), NULL, NULL);
-    
-	int blocks = ((listsize - 1) / (BUCKET_THREAD_N * BUCKET_BAND)) + 1;
-	d_prefixoffsets_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, blocks * BUCKET_BLOCK_MEMORY * sizeof(int), NULL, NULL);
+  l_pivotpoints_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, DIVISIONS * sizeof(float), NULL, NULL);
+  l_offsets_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, DIVISIONS * sizeof(unsigned int), NULL, NULL);
+
+  int blocks = ((listsize - 1) / (BUCKET_THREAD_N * BUCKET_BAND)) + 1;
+  d_prefixoffsets_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, blocks * BUCKET_BLOCK_MEMORY * sizeof(int), NULL, NULL);
     d_prefixoffsets = (unsigned int *)malloc(blocks*BUCKET_BLOCK_MEMORY*sizeof(int));
     d_prefixoffsets_altered = (unsigned int *)malloc(blocks*BUCKET_BLOCK_MEMORY*sizeof(int));
     d_prefixoffsets_input_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, blocks * BUCKET_BLOCK_MEMORY * sizeof(int), NULL, NULL);
@@ -139,37 +139,37 @@ void init_bucketsort(int listsize, int platform_id, int device_id, int use_gpu)
     const char fileName[]="./bucketsort_kernels.cl";
     size_t source_size;
     char *source_str;
-    
+
     fp = fopen(fileName, "r");
-	if (!fp) {
-		fprintf(stderr, "Failed to load bucket kernel.\n");
-		exit(1);
-	}
-    
-    
-	source_str = (char *)malloc(MAX_SOURCE_SIZE);
-	source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
-	fclose(fp);
-    
+  if (!fp) {
+  fprintf(stderr, "Failed to load bucket kernel.\n");
+  exit(1);
+  }
+
+
+  source_str = (char *)malloc(MAX_SOURCE_SIZE);
+  source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
+  fclose(fp);
+
     bucketProgram = clCreateProgramWithSource(bucketContext, 1, (const char **) &source_str, (const size_t)&source_size, &err);
     if (!bucketProgram)
     {
         printf("Error: Failed to create bucket compute program!\n");
         exit(1);
     }
-    
+
     err = clBuildProgram(bucketProgram, 0, NULL, NULL, NULL, NULL);
     if (err != CL_SUCCESS)
     {
         size_t len;
         char buffer[2048];
-        
+
         printf("Error: Failed to build bucket program executable!\n");
         clGetProgramBuildInfo(bucketProgram, devices[device_id], CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
         printf("%s\n", buffer);
         exit(1);
     }
-    
+
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -191,9 +191,9 @@ void finish_bucketsort()
     clReleaseKernel(bucketsortKernel);
     clReleaseCommandQueue(bucketCommands);
     clReleaseContext(bucketContext);
-	free(pivotPoints);
-	free(h_offsets);
-	free(historesult);
+  free(pivotPoints);
+  free(h_offsets);
+  free(historesult);
 }
 
 void histogramInit(int listsize, int platform_id, int device_id, int use_gpu) {
@@ -201,42 +201,42 @@ void histogramInit(int listsize, int platform_id, int device_id, int use_gpu) {
     clGetPlatformIDs(0, NULL, &num);
     cl_platform_id platformID[num];
     clGetPlatformIDs(num, platformID, NULL);
-    
+
     // Selector for CPU/GPU
     cl_device_type device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
-    
+
     clGetDeviceIDs(platformID[platform_id],device_type,0,NULL,&num);
-    
+
     num = 2;
     char name[128];
-    
+
     clGetPlatformInfo(platformID[platform_id], CL_PLATFORM_PROFILE,128,name,NULL);
-    
-    
+
+
     cl_device_id devices[num];
     err = clGetDeviceIDs(platformID[1],device_type,num,devices,NULL);
     //    int gpu = 1;
     //    err = clGetDeviceIDs(NULL, gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU, 2, &device_id, NULL);
-    
+
     if (err != CL_SUCCESS)
     {
         printf("Error: Failed to create a device group!\n");
         exit(1);
     }
-    
+
     clGetDeviceInfo(devices[device_id],CL_DEVICE_NAME,128,name,NULL);
-    
+
     printf("%s \n", name);
-    
+
     cl_context_properties contextProperties[] =
     {
         CL_CONTEXT_PLATFORM,
         (cl_context_properties)platformID[num],
         0
     };
-    
+
     histoContext = clCreateContext(0, 1, &devices[device_id], NULL, NULL, &err);
-    
+
     histoCommands = clCreateCommandQueue(histoContext, devices[device_id], CL_QUEUE_PROFILING_ENABLE, &err);
     histoInput = clCreateBuffer(histoContext,  CL_MEM_READ_ONLY,  listsize*(sizeof(float)), NULL, NULL);
     histoOutput = clCreateBuffer(histoContext, CL_MEM_READ_WRITE, 1024 * sizeof(unsigned int), NULL, NULL);
@@ -244,18 +244,18 @@ void histogramInit(int listsize, int platform_id, int device_id, int use_gpu) {
     const char fileName[]="./histogram1024.cl";
     size_t source_size;
     char *source_str;
-    
+
     fp = fopen(fileName, "r");
-	if (!fp) {
-		fprintf(stderr, "Failed to load kernel.\n");
-		exit(1);
-	}
-    
-    
-	source_str = (char *)malloc(MAX_SOURCE_SIZE);
-	source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
-	fclose(fp);
-    
+  if (!fp) {
+  fprintf(stderr, "Failed to load kernel.\n");
+  exit(1);
+  }
+
+
+  source_str = (char *)malloc(MAX_SOURCE_SIZE);
+  source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
+  fclose(fp);
+
     histoProgram = clCreateProgramWithSource(histoContext, 1, (const char **) &source_str, (const size_t)&source_size, &err);
     if (!histoProgram)
     {
@@ -268,20 +268,20 @@ void histogramInit(int listsize, int platform_id, int device_id, int use_gpu) {
     {
         size_t len;
         char buffer[2048];
-        
+
         printf("Error: Failed to build program executable!\n");
         clGetProgramBuildInfo(histoProgram, devices[device_id], CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
         printf("%s\n", buffer);
         exit(1);
     }
-    
+
     histoKernel = clCreateKernel(histoProgram, "histogram1024Kernel", &err);
     if (!histoKernel || err != CL_SUCCESS)
     {
         printf("Error: Failed to create compute kernel!\n");
         exit(1);
     }
-    
+
 }
 void histogram1024GPU(unsigned int *h_Result, float *d_Data, float minimum, float maximum,int listsize){
     err = clEnqueueWriteBuffer(histoCommands, histoInput, CL_TRUE, 0, listsize*sizeof(float), d_Data, 0, NULL, NULL);
@@ -302,13 +302,13 @@ void histogram1024GPU(unsigned int *h_Result, float *d_Data, float minimum, floa
     err  = clSetKernelArg(histoKernel, 2, sizeof(float), &minimum);
     err  = clSetKernelArg(histoKernel, 3, sizeof(float), &maximum);
     err  = clSetKernelArg(histoKernel, 4, sizeof(int), &listsize);
-    
+
     if (err != CL_SUCCESS)
     {
         printf("Error: Failed to set kernel arguments! %d\n", err);
         exit(1);
     }
-    
+
     size_t global = 6144;
     size_t local;
 #ifdef HISTO_WG_SIZE_0
@@ -331,7 +331,7 @@ void histogram1024GPU(unsigned int *h_Result, float *d_Data, float minimum, floa
         exit(1);
     }
     clFinish(histoCommands);
-    
+
     cl_ulong time_start, time_end;
     double total_time;
     clGetEventProfilingInfo(histoEvent, CL_PROFILING_COMMAND_START, sizeof(time_start), &time_start, NULL);
@@ -354,28 +354,28 @@ void finish_histogram() {
 // sort the elements into float4 aligned buckets of roughly equal size
 ////////////////////////////////////////////////////////////////////////////////
 void bucketSort(float *d_input, float *d_output, int listsize,
-				int *sizes, int *nullElements, float minimum, float maximum,
-				unsigned int *origOffsets, int platform_id, int device_id, int use_gpu)
+  int *sizes, int *nullElements, float minimum, float maximum,
+  unsigned int *origOffsets, int platform_id, int device_id, int use_gpu)
 {
 //	////////////////////////////////////////////////////////////////////////////
 //	// First pass - Create 1024 bin histogram
 //	////////////////////////////////////////////////////////////////////////////
     histogramInit(listsize, platform_id, device_id, use_gpu);
-	  histogram1024GPU(h_offsets, d_input, minimum, maximum, listsize);
+    histogram1024GPU(h_offsets, d_input, minimum, maximum, listsize);
     finish_histogram();
     for(int i=0; i<histosize; i++) historesult[i] = (float)h_offsets[i];
 
 //	///////////////////////////////////////////////////////////////////////////
 //	// Calculate pivot points (CPU algorithm)
 //	///////////////////////////////////////////////////////////////////////////
-	calcPivotPoints(historesult, histosize, listsize, DIVISIONS,
+  calcPivotPoints(historesult, histosize, listsize, DIVISIONS,
                     minimum, maximum, pivotPoints,
                     (maximum - minimum)/(float)histosize);
 //
 //	///////////////////////////////////////////////////////////////////////////
 //	// Count the bucket sizes in new divisions
 //	///////////////////////////////////////////////////////////////////////////
-    
+
     bucketcountKernel = clCreateKernel(bucketProgram, "bucketcount", &err);
     if (!bucketcountKernel || err != CL_SUCCESS)
     {
@@ -388,9 +388,9 @@ void bucketSort(float *d_input, float *d_output, int listsize,
         printf("Error: Failed to write to l_pivotpoints source array!\n");
         exit(1);
     }
-    
+
     d_input_buff = clCreateBuffer(bucketContext,CL_MEM_READ_WRITE, (listsize + (DIVISIONS*4))*sizeof(float),NULL,NULL);
-    
+
     err = clEnqueueWriteBuffer(bucketCommands, d_input_buff, CL_TRUE, 0, (listsize + (DIVISIONS*4))*sizeof(float), d_input, 0, NULL, NULL);
     if (err != CL_SUCCESS)
     {
@@ -403,7 +403,7 @@ void bucketSort(float *d_input, float *d_output, int listsize,
     err  = clSetKernelArg(bucketcountKernel, 2, sizeof(cl_mem), &d_prefixoffsets_buff);
     err  = clSetKernelArg(bucketcountKernel, 3, sizeof(cl_int), &listsize);
     err  = clSetKernelArg(bucketcountKernel, 4, sizeof(cl_mem), &l_pivotpoints_buff);
-    
+
     if (err != CL_SUCCESS)
     {
         printf("Error: Failed to set kernel arguments! %d\n", err);
@@ -413,7 +413,7 @@ void bucketSort(float *d_input, float *d_output, int listsize,
     int blocks =((listsize -1) / (BUCKET_THREAD_N*BUCKET_BAND)) + 1;
     size_t global[] = {blocks*BUCKET_THREAD_N,1,1};
     size_t local[] = {BUCKET_THREAD_N,1,1};
-    
+
     err = clEnqueueNDRangeKernel(bucketCommands, bucketcountKernel, 3, NULL, global, local, 0, NULL, &bucketCountEvent);
     if (err)
     {
@@ -429,7 +429,7 @@ void bucketSort(float *d_input, float *d_output, int listsize,
         exit(1);
     }
     err = clEnqueueReadBuffer( bucketCommands, d_indice_buff, CL_TRUE, 0, listsize * sizeof(int), d_indice, 0, NULL, NULL );
-    
+
     if (err != CL_SUCCESS)
     {
         printf("Error: Failed to read indice output array! %d\n", err);
@@ -443,7 +443,7 @@ void bucketSort(float *d_input, float *d_output, int listsize,
     total_time = time_end - time_start;
     sum+= total_time/1000000;
     printf("Bucket Count Kernel Time: %0.3f \n", total_time/1000000);
-    
+
 
 //
 //	///////////////////////////////////////////////////////////////////////////
@@ -456,7 +456,7 @@ void bucketSort(float *d_input, float *d_output, int listsize,
     size_t localpre[] = {128,1,1};
 #endif
     size_t globalpre[] = {(DIVISIONS),1,1};
-    
+
     bucketprefixKernel = clCreateKernel(bucketProgram, "bucketprefixoffset", &err);
     if (!bucketprefixKernel || err != CL_SUCCESS)
     {
@@ -507,22 +507,22 @@ void bucketSort(float *d_input, float *d_output, int listsize,
     sum+= total_time/1000000;
     printf("Bucket Prefix Kernel Time: %0.3f \n", total_time/1000000);
 //	// copy the sizes from device to host
-	origOffsets[0] = 0;
-	for(int i=0; i<DIVISIONS; i++){
-		origOffsets[i+1] = h_offsets[i] + origOffsets[i];
-		if((h_offsets[i] % 4) != 0){
-			nullElements[i] = (h_offsets[i] & ~3) + 4 - h_offsets[i];
-		}
-		else nullElements[i] = 0;
-	}
-	for(int i=0; i<DIVISIONS; i++) sizes[i] = (h_offsets[i] + nullElements[i])/4;
-	for(int i=0; i<DIVISIONS; i++) {
-		if((h_offsets[i] % 4) != 0)	h_offsets[i] = (h_offsets[i] & ~3) + 4;
-	}
-	for(int i=1; i<DIVISIONS; i++) h_offsets[i] = h_offsets[i-1] + h_offsets[i];
-	for(int i=DIVISIONS - 1; i>0; i--) h_offsets[i] = h_offsets[i-1];
-	h_offsets[0] = 0;
-    
+  origOffsets[0] = 0;
+  for(int i=0; i<DIVISIONS; i++){
+  origOffsets[i+1] = h_offsets[i] + origOffsets[i];
+  if((h_offsets[i] % 4) != 0){
+  nullElements[i] = (h_offsets[i] & ~3) + 4 - h_offsets[i];
+  }
+  else nullElements[i] = 0;
+  }
+  for(int i=0; i<DIVISIONS; i++) sizes[i] = (h_offsets[i] + nullElements[i])/4;
+  for(int i=0; i<DIVISIONS; i++) {
+  if((h_offsets[i] % 4) != 0)	h_offsets[i] = (h_offsets[i] & ~3) + 4;
+  }
+  for(int i=1; i<DIVISIONS; i++) h_offsets[i] = h_offsets[i-1] + h_offsets[i];
+  for(int i=DIVISIONS - 1; i>0; i--) h_offsets[i] = h_offsets[i-1];
+  h_offsets[0] = 0;
+
 
 
 //	///////////////////////////////////////////////////////////////////////////
@@ -608,39 +608,39 @@ double getBucketTime() {
 // the list into approximately listsize/divisions elements each
 ////////////////////////////////////////////////////////////////////////////////
 void calcPivotPoints(float *histogram, int histosize, int listsize,
-					 int divisions, float min, float max, float *pivotPoints, float histo_width)
+   int divisions, float min, float max, float *pivotPoints, float histo_width)
 {
-	float elemsPerSlice = listsize/(float)divisions;
-	float startsAt = min;
-	float endsAt = min + histo_width;
-	float we_need = elemsPerSlice;
-	int p_idx = 0;
-	for(int i=0; i<histosize; i++)
-	{
-		if(i == histosize - 1){
-			if(!(p_idx < divisions)){
-				pivotPoints[p_idx++] = startsAt + (we_need/histogram[i]) * histo_width;
-			}
-			break;
-		}
-		while(histogram[i] > we_need){
-			if(!(p_idx < divisions)){
-				printf("i=%d, p_idx = %d, divisions = %d\n", i, p_idx, divisions);
-				exit(0);
-			}
-			pivotPoints[p_idx++] = startsAt + (we_need/histogram[i]) * histo_width;
-			startsAt += (we_need/histogram[i]) * histo_width;
-			histogram[i] -= we_need;
-			we_need = elemsPerSlice;
-		}
-		// grab what we can from what remains of it
-		we_need -= histogram[i];
-        
-		startsAt = endsAt;
-		endsAt += histo_width;
-	}
-	while(p_idx < divisions){
-		pivotPoints[p_idx] = pivotPoints[p_idx-1];
-		p_idx++;
-	}
+  float elemsPerSlice = listsize/(float)divisions;
+  float startsAt = min;
+  float endsAt = min + histo_width;
+  float we_need = elemsPerSlice;
+  int p_idx = 0;
+  for(int i=0; i<histosize; i++)
+  {
+  if(i == histosize - 1){
+  if(!(p_idx < divisions)){
+  pivotPoints[p_idx++] = startsAt + (we_need/histogram[i]) * histo_width;
+  }
+  break;
+  }
+  while(histogram[i] > we_need){
+  if(!(p_idx < divisions)){
+  printf("i=%d, p_idx = %d, divisions = %d\n", i, p_idx, divisions);
+  exit(0);
+  }
+  pivotPoints[p_idx++] = startsAt + (we_need/histogram[i]) * histo_width;
+  startsAt += (we_need/histogram[i]) * histo_width;
+  histogram[i] -= we_need;
+  we_need = elemsPerSlice;
+  }
+  // grab what we can from what remains of it
+  we_need -= histogram[i];
+
+  startsAt = endsAt;
+  endsAt += histo_width;
+  }
+  while(p_idx < divisions){
+  pivotPoints[p_idx] = pivotPoints[p_idx-1];
+  p_idx++;
+  }
 }

+ 2 - 2
opencl/hybridsort/bucketsort.h

@@ -7,8 +7,8 @@
 void init_bucketsort(int listsize, int platform_id, int device_id, int use_gpu);
 void finish_bucketsort();
 void bucketSort(float *d_input, float *d_output, int listsize,
-				int *sizes, int *nullElements, float minimum, float maximum,
-				unsigned int *origOffsets, int platform_id, int device_id, int use_gpu);
+  int *sizes, int *nullElements, float minimum, float maximum,
+  unsigned int *origOffsets, int platform_id, int device_id, int use_gpu);
 void histogram1024GPU(
                       unsigned int *h_Result,
                       float *d_Data,

+ 51 - 54
opencl/hybridsort/hybridsort.c

@@ -32,9 +32,9 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 int compare(const void *a, const void *b) {
-	if(*((float *)a) < *((float *)b)) return -1;
-	else if(*((float *)a) > *((float *)b)) return 1;
-	else return 0;
+  if(*((float *)a) < *((float *)b)) return -1;
+  else if(*((float *)a) > *((float *)b)) return 1;
+  else return 0;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -48,7 +48,7 @@ int parseCommandline(int argc, char *argv[], int *platform_id, int *device_id, i
    printf("%d", argc);
    if (argc < 5) return 1; // error
    char flag;
-   
+
    for(i=1;i<argc;i++) {
      if (argv[i][0]=='-') {// flag
        flag = argv[i][1];
@@ -64,7 +64,7 @@ int parseCommandline(int argc, char *argv[], int *platform_id, int *device_id, i
            case 'g': // device
              i++;
              *use_gpu = atoi(argv[i]);
-             break;  
+             break;
        }
      }
    }
@@ -85,36 +85,36 @@ void printUsage(){
   printf("-d [int]     Choose the device (must choose both platform and device)\n");
   printf("-g [int]     1 for gpu and 0 for cpu\n");
   printf("\n");
-}                       
+}
 
 int main(int argc, char** argv)
 {
     int err;                            // error code returned from api calls
-    
+
     unsigned int correct;               // number of correct results returned
 
     size_t global;                      // global domain size for our calculation
     size_t local;                       // local domain size for our calculation
     unsigned int *results;
-    
+
     int platform_id=-1,device_id=-1,use_gpu=-1;
-  
+
     // parse command line
     if (parseCommandline(argc, argv, &platform_id, &device_id, &use_gpu)) {
     printUsage();
     return 0;
     }
-    
+
     // Fill our data set with random float values
     //
-    
+
     int numElements = 0 ;
-        
+
     if(strcmp(argv[1],"r") == 0) {
         numElements = SIZE;
-	}
+  }
     else {
-		FILE *fp;
+  FILE *fp;
         fp = fopen(argv[1],"r");
         if(fp == NULL) {
             printf("Error reading file \n");
@@ -122,36 +122,36 @@ int main(int argc, char** argv)
         }
         int count = 0;
         float c;
-        
+
         while(fscanf(fp,"%f",&c) != EOF) {
             count++;
         }
         fclose(fp);
-        
+
         numElements = count;
     }
   printf("Sorting list of %d floats.\n", numElements);
   int mem_size = (numElements + (DIVISIONS*4))*sizeof(float);
-	// Allocate enough for the input list
-	float *cpu_idata = (float *)malloc(mem_size);
+  // Allocate enough for the input list
+  float *cpu_idata = (float *)malloc(mem_size);
   float *cpu_odata = (float *)malloc(mem_size);
-	// Allocate enough for the output list on the cpu side
-	float *d_output = (float *)malloc(mem_size);
-	// Allocate enough memory for the output list on the gpu side
-	float *gpu_odata = (float *)malloc(mem_size);
-	float datamin = FLT_MAX;
-	float datamax = -FLT_MAX;
-    
+  // Allocate enough for the output list on the cpu side
+  float *d_output = (float *)malloc(mem_size);
+  // Allocate enough memory for the output list on the gpu side
+  float *gpu_odata = (float *)malloc(mem_size);
+  float datamin = FLT_MAX;
+  float datamax = -FLT_MAX;
+
     if(strcmp(argv[1],"r")==0) {
     for (int i = 0; i < numElements; i++) {
         // Generate random floats between 0 and 1 for the input data
-		cpu_idata[i] = ((float) rand() / RAND_MAX);
-  
+  cpu_idata[i] = ((float) rand() / RAND_MAX);
+
         //Compare data at index to data minimum, if less than current minimum, set that element as new minimum
-		datamin = fminf(cpu_idata[i], datamin);
+  datamin = fminf(cpu_idata[i], datamin);
         //Same as above but for maximum
-		datamax = fmaxf(cpu_idata[i], datamax);
-	}
+  datamax = fmaxf(cpu_idata[i], datamax);
+  }
     }
     else {
         FILE *fp;
@@ -161,14 +161,14 @@ int main(int argc, char** argv)
             datamin = fminf(cpu_idata[i], datamin);
             datamax = fmaxf(cpu_idata[i],datamax);
         }
-	}
+  }
     FILE *tp;
     const char filename2[]="./hybridinput.txt";
     tp = fopen(filename2,"w");
     for(int i = 0; i < SIZE; i++) {
         fprintf(tp,"%f ",cpu_idata[i]);
     }
-    
+
     fclose(tp);
     memcpy(cpu_odata, cpu_idata, mem_size);
     clock_t gpu_start = clock();
@@ -184,12 +184,12 @@ int main(int argc, char** argv)
 
     cl_float4 *d_origList = (cl_float4*) d_output;
     cl_float4 *d_resultList = (cl_float4*) cpu_idata;
-    
+
     int newlistsize = 0;
     for(int i = 0; i < DIVISIONS; i++){
         newlistsize += sizes[i] * 4;
     }
-    
+
     init_mergesort(newlistsize, platform_id, device_id, use_gpu);
     clock_t mergesort_start = clock();
     cl_float4 *mergeresult = runMergeSort(newlistsize,DIVISIONS,d_origList,d_resultList,sizes,nullElements,origOffsets);
@@ -209,28 +209,28 @@ int main(int argc, char** argv)
 #endif
 #ifdef VERIFY
     clock_t cpu_start = clock(), cpu_diff;
-    
+
     qsort(cpu_odata, numElements, sizeof(float), compare);
     cpu_diff = clock() - cpu_start;
     int cpu_msec = cpu_diff * 1000 / CLOCKS_PER_SEC;
     printf("CPU execution time: %d ms  \n", cpu_msec);
     printf("Checking result...");
-    
-	// Result checking
-	int count = 0;
-	for(int i = 0; i < numElements; i++){
-		if(cpu_odata[i] != gpu_odata[i])
-		{
-			printf("Sort missmatch on element %d: \n", i);
-			printf("CPU = %f : GPU = %f\n", cpu_odata[i], gpu_odata[i]);
-			count++;
-			break;
-		}
+
+  // Result checking
+  int count = 0;
+  for(int i = 0; i < numElements; i++){
+  if(cpu_odata[i] != gpu_odata[i])
+  {
+  printf("Sort missmatch on element %d: \n", i);
+  printf("CPU = %f : GPU = %f\n", cpu_odata[i], gpu_odata[i]);
+  count++;
+  break;
+  }
     }
-	if(count == 0) printf("PASSED.\n");
-	else printf("FAILED.\n");
+  if(count == 0) printf("PASSED.\n");
+  else printf("FAILED.\n");
 #endif
-    
+
 #ifdef OUTPUT
     FILE *tp1;
     const char filename3[]="./hybridoutput.txt";
@@ -238,10 +238,10 @@ int main(int argc, char** argv)
     for(int i = 0; i < SIZE; i++) {
         fprintf(tp1,"%f ",cpu_idata[i]);
     }
-    
+
     fclose(tp1);
 #endif
-    
+
 
 //    printf("%d \n",cpu_odata[1]);
 //    int summy = 0;
@@ -250,6 +250,3 @@ int main(int argc, char** argv)
 //    printf("%d \n", summy);
     return 0;
 }
-
-
-

+ 78 - 78
opencl/hybridsort/mergesort.c

@@ -53,64 +53,64 @@ void init_mergesort(int listsize, int platform_id, int device_id, int use_gpu){
     clGetPlatformIDs(0,NULL,&num);
     cl_platform_id platformID[num];
     clGetPlatformIDs(num,platformID,NULL);
-    
+
     // Selector for CPU/GPU
     cl_device_type device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
-    
+
     clGetDeviceIDs(platformID[platform_id],device_type,0,NULL,&num);
     cl_device_id devices[num];
     err = clGetDeviceIDs(platformID[platform_id],device_type,num,devices,NULL);
-    
+
     if (err != CL_SUCCESS)
     {
         printf("Error: Failed to create a device group!\n");
         exit(1);
     }
     char name[128];
-    
-    
+
+
     clGetDeviceInfo(devices[device_id],CL_DEVICE_NAME,128,name,NULL);
-    
+
     mergeContext = clCreateContext(0, 1, &devices[device_id], NULL, NULL, &err);
-    
+
     mergeCommands = clCreateCommandQueue(mergeContext, devices[device_id], CL_QUEUE_PROFILING_ENABLE, &err);
-    
+
     d_resultList_first_altered = (cl_float4 *)malloc(listsize*sizeof(float));
     d_resultList_first_buff = clCreateBuffer(mergeContext,CL_MEM_READ_WRITE, listsize * sizeof(float),NULL,NULL);
     d_origList_first_buff = clCreateBuffer(mergeContext,CL_MEM_READ_WRITE, listsize * sizeof(float),NULL,NULL);
-    
+
     d_orig = clCreateBuffer(mergeContext,CL_MEM_READ_WRITE, listsize * sizeof(float),NULL,NULL);
     d_res = clCreateBuffer(mergeContext,CL_MEM_READ_WRITE, listsize * sizeof(float),NULL,NULL);
-    
+
     FILE *fp;
     const char fileName[]="./mergesort.cl";
     size_t source_size;
     char *source_str;
-    
+
     fp = fopen(fileName, "r");
-	if (!fp) {
-		fprintf(stderr, "Failed to load mergesort kernel.\n");
-		exit(1);
-	}
-    
-    
-	source_str = (char *)malloc(MAX_SOURCE_SIZE);
-	source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
-
-	fclose(fp);
+  if (!fp) {
+  fprintf(stderr, "Failed to load mergesort kernel.\n");
+  exit(1);
+  }
+
+
+  source_str = (char *)malloc(MAX_SOURCE_SIZE);
+  source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
+
+  fclose(fp);
     mergeProgram = clCreateProgramWithSource(mergeContext, 1, (const char **) &source_str, (const size_t)&source_size, &err);
     if (!mergeProgram)
     {
         printf("Error: Failed to create merge compute program!\n");
         exit(1);
     }
-    
+
     err = clBuildProgram(mergeProgram, 0, NULL, NULL, NULL, NULL);
     if (err != CL_SUCCESS)
     {
         size_t len;
         char buffer[2048];
-        
+
         printf("Error: Failed to build merge program executable!\n");
         clGetProgramBuildInfo(mergeProgram, device_id, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
         printf("%s\n", buffer);
@@ -135,24 +135,24 @@ cl_float4* runMergeSort(int listsize, int divisions,
                         cl_float4 *d_origList, cl_float4 *d_resultList,
                         int *sizes, int *nullElements,
                         unsigned int *origOffsets){
-    
+
     int *startaddr = (int *)malloc((divisions + 1)*sizeof(int));
-	int largestSize = -1;
-	startaddr[0] = 0;
-	for(int i=1; i<=divisions; i++)
-	{
-		startaddr[i] = startaddr[i-1] + sizes[i-1];
-		if(sizes[i-1] > largestSize) largestSize = sizes[i-1];
-	}
-	largestSize *= 4;
-    
+  int largestSize = -1;
+  startaddr[0] = 0;
+  for(int i=1; i<=divisions; i++)
+  {
+  startaddr[i] = startaddr[i-1] + sizes[i-1];
+  if(sizes[i-1] > largestSize) largestSize = sizes[i-1];
+  }
+  largestSize *= 4;
+
     mergeFirstKernel = clCreateKernel(mergeProgram, "mergeSortFirst", &err);
     if (!mergeFirstKernel || err != CL_SUCCESS)
     {
         printf("Error: Failed to create merge sort first compute kernel!\n");
         exit(1);
     }
-    
+
     err = clEnqueueWriteBuffer(mergeCommands, d_resultList_first_buff, CL_TRUE, 0, listsize*sizeof(float), d_resultList, 0, NULL, NULL);
     if (err != CL_SUCCESS)
     {
@@ -165,7 +165,7 @@ cl_float4* runMergeSort(int listsize, int divisions,
         printf("Error: Failed to write to d_origList_first_buff source array!\n");
         exit(1);
     }
-    
+
     err = 0;
     err  = clSetKernelArg(mergeFirstKernel, 0, sizeof(cl_mem), &d_origList_first_buff);
     err  = clSetKernelArg(mergeFirstKernel, 1, sizeof(cl_mem), &d_resultList_first_buff);
@@ -181,10 +181,10 @@ cl_float4* runMergeSort(int listsize, int divisions,
     const int THREADS = 256;
 #endif
     size_t local[] = {THREADS,1,1};
-	int blocks = ((listsize/4)%THREADS == 0) ? (listsize/4)/THREADS : (listsize/4)/THREADS + 1;
-	size_t global[] = {blocks*THREADS,1,1};
+  int blocks = ((listsize/4)%THREADS == 0) ? (listsize/4)/THREADS : (listsize/4)/THREADS + 1;
+  size_t global[] = {blocks*THREADS,1,1};
     size_t grid[] = {blocks,1,1,1};
-    
+
     err = clEnqueueNDRangeKernel(mergeCommands, mergeFirstKernel, 3, NULL, global, local, 0, NULL, &mergeFirstEvent);
     if (err)
     {
@@ -193,7 +193,7 @@ cl_float4* runMergeSort(int listsize, int divisions,
     }
     clWaitForEvents(1 , &mergeFirstEvent);
     clFinish(mergeCommands);
-    
+
     err = clEnqueueReadBuffer( mergeCommands, d_resultList_first_buff, CL_TRUE, 0, listsize*sizeof(float), d_resultList, 0, NULL, NULL );
     if (err != CL_SUCCESS)
     {
@@ -213,7 +213,7 @@ cl_float4* runMergeSort(int listsize, int divisions,
     total_time = time_end - time_start;
     mergesum+= total_time / 1000000;
     printf("Merge First Kernel Time: %0.3f \n", total_time/1000000);
-    
+
 //    for(int i =0; i < listsize/4; i++) {
 //        printf("TEST %f \n", d_resultList[i].s[0]);
 //        printf("TEST %f \n", d_resultList[i].s[1]);
@@ -221,15 +221,15 @@ cl_float4* runMergeSort(int listsize, int divisions,
 //        printf("TEST %f \n", d_resultList[i].s[3]);
 //    }
     constStartAddr = clCreateBuffer(mergeContext,CL_MEM_READ_WRITE, (divisions+1)*sizeof(int),NULL,NULL);
-    
+
     err = clEnqueueWriteBuffer(mergeCommands, constStartAddr, CL_TRUE, 0, (divisions+1)*sizeof(int), startaddr, 0, NULL, NULL);
     if (err != CL_SUCCESS)
     {
         printf("Error: Failed to write to constStartAddr source array!\n");
         exit(1);
     }
-    
-    
+
+
     mergePassKernel = clCreateKernel(mergeProgram, "mergeSortPass", &err);
     if (!mergePassKernel || err != CL_SUCCESS)
     {
@@ -239,33 +239,33 @@ cl_float4* runMergeSort(int listsize, int divisions,
 
     double mergePassTime = 0;
     int nrElems = 2;
-	while(1==1){
-		int floatsperthread = (nrElems*4);
+  while(1==1){
+  int floatsperthread = (nrElems*4);
 //        printf("FPT %d \n", floatsperthread);
-		int threadsPerDiv = (int)ceil(largestSize/(float)floatsperthread);
+  int threadsPerDiv = (int)ceil(largestSize/(float)floatsperthread);
 //        printf("TPD %d \n",threadsPerDiv);
-		int threadsNeeded = threadsPerDiv * divisions;
+  int threadsNeeded = threadsPerDiv * divisions;
 //        printf("TN %d \n", threadsNeeded);
-        
+
 #ifdef MERGE_WG_SIZE_1
         local[0] = MERGE_WG_SIZE_1;
 #else
-		local[0] = 208;
+  local[0] = 208;
 #endif
-		grid[0] = ((threadsNeeded%local[0]) == 0) ?
+  grid[0] = ((threadsNeeded%local[0]) == 0) ?
         threadsNeeded/local[0] :
         (threadsNeeded/local[0]) + 1;
-		if(grid[0] < 8){
-			grid[0] = 8;
-			local[0] = ((threadsNeeded%grid[0]) == 0) ?
+  if(grid[0] < 8){
+  grid[0] = 8;
+  local[0] = ((threadsNeeded%grid[0]) == 0) ?
             threadsNeeded / grid[0] :
             (threadsNeeded / grid[0]) + 1;
-		}
-		// Swap orig/result list
-		cl_float4 *tempList = d_origList;
-		d_origList = d_resultList;
-		d_resultList = tempList;
-      
+  }
+  // Swap orig/result list
+  cl_float4 *tempList = d_origList;
+  d_origList = d_resultList;
+  d_resultList = tempList;
+
         err = clEnqueueWriteBuffer(mergeCommands, d_resultList_first_buff, CL_TRUE, 0, listsize*sizeof(float), d_resultList, 0, NULL, NULL);
         if (err != CL_SUCCESS)
         {
@@ -290,7 +290,7 @@ cl_float4* runMergeSort(int listsize, int divisions,
             printf("Error: Failed to set merge pass kernel arguments! %d\n", err);
             exit(1);
         }
-		
+
         global[0] = grid[0]*local[0];
         err = clEnqueueNDRangeKernel(mergeCommands, mergePassKernel, 3, NULL, global, local, 0, NULL, &mergePassEvent);
         if (err)
@@ -298,7 +298,7 @@ cl_float4* runMergeSort(int listsize, int divisions,
             printf("Error: Failed to execute mergePass kernel!\n");
             exit(1);
         }
-        
+
         clFinish(mergeCommands);
         err = clEnqueueReadBuffer( mergeCommands, d_resultList_first_buff, CL_TRUE, 0, listsize*sizeof(float), d_resultList, 0, NULL, NULL );
         if (err != CL_SUCCESS)
@@ -313,24 +313,24 @@ cl_float4* runMergeSort(int listsize, int divisions,
         mergesum+= total_time /1000000;
         mergePassTime+= total_time/1000000;
         printf("Merge Pass Kernel Iteration Time: %0.3f \n", total_time/1000000);
-		nrElems *= 2;
-		floatsperthread = (nrElems*4);
-        
-		if(threadsPerDiv == 1) break;
-	}
+  nrElems *= 2;
+  floatsperthread = (nrElems*4);
+
+  if(threadsPerDiv == 1) break;
+  }
     printf("Merge Pass Kernel Time: %0.3f \n", mergePassTime);
 
     finalStartAddr = clCreateBuffer(mergeContext,CL_MEM_READ_WRITE, (divisions+1)*sizeof(int),NULL,NULL);
-    
+
     err = clEnqueueWriteBuffer(mergeCommands, finalStartAddr, CL_TRUE, 0, (divisions+1)*sizeof(int), origOffsets, 0, NULL, NULL);
     if (err != CL_SUCCESS)
     {
         printf("Error: Failed to write to finalStartAddr source array!\n");
         exit(1);
     }
-    
+
     nullElems = clCreateBuffer(mergeContext,CL_MEM_READ_WRITE, (divisions)*sizeof(int),NULL,NULL);
-    
+
     err = clEnqueueWriteBuffer(mergeCommands, nullElems, CL_TRUE, 0, (divisions)*sizeof(int), nullElements, 0, NULL, NULL);
     if (err != CL_SUCCESS)
     {
@@ -342,14 +342,14 @@ cl_float4* runMergeSort(int listsize, int divisions,
 #else
     local[0] = 256;
 #endif
-	grid[0] = ((largestSize%local[0]) == 0) ?
+  grid[0] = ((largestSize%local[0]) == 0) ?
     largestSize/local[0] :
     (largestSize/local[0]) + 1;
-	grid[1] = divisions;
+  grid[1] = divisions;
 //    grid[0] = 17;
     global[0] = grid[0]*local[0];
     global[1] = grid[1]*local[1];
-    
+
     mergePackKernel = clCreateKernel(mergeProgram, "mergepack", &err);
     if (!mergePackKernel || err != CL_SUCCESS)
     {
@@ -362,7 +362,7 @@ cl_float4* runMergeSort(int listsize, int divisions,
         printf("Error: Failed to write to d_resultList_first_buff source array!\n");
         exit(1);
     }
-    
+
     err = clEnqueueWriteBuffer(mergeCommands, d_orig, CL_TRUE, 0, listsize*sizeof(float), d_origList, 0, NULL, NULL);
     if (err != CL_SUCCESS)
     {
@@ -398,11 +398,11 @@ cl_float4* runMergeSort(int listsize, int divisions,
         printf("Error: Failed to read origList array! %d\n", err);
         exit(1);
     }
-    
-    
-	free(startaddr);
-	return d_origList;
-    
+
+
+  free(startaddr);
+  return d_origList;
+
 }
 
 double getMergeTime() {

+ 3 - 3
opencl/hybridsort/mergesort.h

@@ -4,9 +4,9 @@
 #include "bucketsort.h"
 
 cl_float4 *runMergeSort(int listsize, int divisions,
-					 cl_float4 *d_origList, cl_float4 *d_resultList,
-					 int *sizes, int *nullElements,
-					 unsigned int *origOffsets);
+   cl_float4 *d_origList, cl_float4 *d_resultList,
+   int *sizes, int *nullElements,
+   unsigned int *origOffsets);
 void init_mergesort(int listsize, int platform_id, int device_id, int use_gpu);
 void finish_mergesort();
 double getMergeTime();

+ 61 - 62
opencl/kmeans/cluster.c

@@ -17,7 +17,7 @@
 /*                                                                           */
 /*2       Redistributions in binary form must reproduce the above copyright   */
 /*        notice, this list of conditions and the following disclaimer in the */
-/*        documentation and/or other materials provided with the distribution.*/ 
+/*        documentation and/or other materials provided with the distribution.*/
 /*                                                                            */
 /*3       Neither the name of Northwestern University nor the names of its    */
 /*        contributors may be used to endorse or promote products derived     */
@@ -74,86 +74,85 @@
 #include <omp.h>
 #include "kmeans.h"
 
-float	min_rmse_ref = FLT_MAX;		
+float	min_rmse_ref = FLT_MAX;
 extern double wtime(void);
-	/* reference min_rmse value */
+  /* reference min_rmse value */
 
 /*---< cluster() >-----------------------------------------------------------*/
 int cluster(int   npoints,				/* number of data points */
             int   nfeatures,				/* number of attributes for each point */
-            float **features,			/* array: [npoints][nfeatures] */                  
+            float **features,			/* array: [npoints][nfeatures] */
             int   min_nclusters,			/* range of min to max number of clusters */
-			      int		max_nclusters,
+        int		max_nclusters,
             float threshold,				/* loop terminating factor */
             int   *best_nclusters,		/* out: number between min and max with lowest RMSE */
             float ***cluster_centres,		/* out: [best_nclusters][nfeatures] */
-			      float	*min_rmse,				/* out: minimum RMSE */
-			      int		isRMSE,				/* calculate RMSE */
-			      int	  nloops,
+        float	*min_rmse,				/* out: minimum RMSE */
+        int		isRMSE,				/* calculate RMSE */
+        int	  nloops,
             int   platform_id,
             int   device_id,
             int   use_gpu					/* number of iteration for each number of clusters */
-			)
-{    
-	int		nclusters;						/* number of clusters k */	
-	int		index =0;						/* number of iteration to reach the best RMSE */
-	int		rmse;							/* RMSE for each clustering */
+  )
+{
+  int		nclusters;						/* number of clusters k */
+  int		index =0;						/* number of iteration to reach the best RMSE */
+  int		rmse;							/* RMSE for each clustering */
   int    *membership;						/* which cluster a data point belongs to */
   float **tmp_cluster_centres;			/* hold coordinates of cluster centers */
-	int		i;
+  int		i;
 
-	/* allocate memory for membership */
+  /* allocate memory for membership */
     membership = (int*) malloc(npoints * sizeof(int));
 
-	/* sweep k from min to max_nclusters to find the best number of clusters */
-	for(nclusters = min_nclusters; nclusters <= max_nclusters; nclusters++)
-	{
-		if (nclusters > npoints) break;	/* cannot have more clusters than points */
-
-		/* allocate device memory, invert data array (@ kmeans_cuda.cu) */
-		allocate(npoints, nfeatures, nclusters, features, platform_id, device_id, use_gpu);
-
-		/* iterate nloops times for each number of clusters */
-		for(i = 0; i < nloops; i++)
-		{
-			/* initialize initial cluster centers, CUDA calls (@ kmeans_cuda.cu) */
-			tmp_cluster_centres = kmeans_clustering(features,
-													nfeatures,
-													npoints,
-													nclusters,
-													threshold,
-													membership);
-
-			if (*cluster_centres) {
-				free((*cluster_centres)[0]);
-				free(*cluster_centres);
-			}
-			*cluster_centres = tmp_cluster_centres;
-	        
-					
-			/* find the number of clusters with the best RMSE */
-			if(isRMSE)
-			{
-				rmse = rms_err(features,
-							   nfeatures,
-							   npoints,
-							   tmp_cluster_centres,
-							   nclusters);
-				
-				if(rmse < min_rmse_ref){
-					min_rmse_ref = rmse;			//update reference min RMSE
-					*min_rmse = min_rmse_ref;		//update return min RMSE
-					*best_nclusters = nclusters;	//update optimum number of clusters
-					index = i;						//update number of iteration to reach best RMSE
-				}
-			}			
-		}
-		
-		deallocateMemory();							/* free device memory (@ kmeans_cuda.cu) */
-	}
+  /* sweep k from min to max_nclusters to find the best number of clusters */
+  for(nclusters = min_nclusters; nclusters <= max_nclusters; nclusters++)
+  {
+  if (nclusters > npoints) break;	/* cannot have more clusters than points */
+
+  /* allocate device memory, invert data array (@ kmeans_cuda.cu) */
+  allocate(npoints, nfeatures, nclusters, features, platform_id, device_id, use_gpu);
+
+  /* iterate nloops times for each number of clusters */
+  for(i = 0; i < nloops; i++)
+  {
+  /* initialize initial cluster centers, CUDA calls (@ kmeans_cuda.cu) */
+  tmp_cluster_centres = kmeans_clustering(features,
+  nfeatures,
+  npoints,
+  nclusters,
+  threshold,
+  membership);
+
+  if (*cluster_centres) {
+  free((*cluster_centres)[0]);
+  free(*cluster_centres);
+  }
+  *cluster_centres = tmp_cluster_centres;
+
+
+  /* find the number of clusters with the best RMSE */
+  if(isRMSE)
+  {
+  rmse = rms_err(features,
+     nfeatures,
+     npoints,
+     tmp_cluster_centres,
+     nclusters);
+
+  if(rmse < min_rmse_ref){
+  min_rmse_ref = rmse;			//update reference min RMSE
+  *min_rmse = min_rmse_ref;		//update return min RMSE
+  *best_nclusters = nclusters;	//update optimum number of clusters
+  index = i;						//update number of iteration to reach best RMSE
+  }
+  }
+  }
+
+  deallocateMemory();							/* free device memory (@ kmeans_cuda.cu) */
+  }
 
     free(membership);
 
     return index;
 }
-

+ 181 - 181
opencl/kmeans/kmeans.cpp

@@ -7,22 +7,22 @@
 #include "kmeans.h"
 
 #ifdef WIN
-	#include <windows.h>
+  #include <windows.h>
 #else
-	#include <pthread.h>
-	#include <sys/time.h>
-	double gettime() {
-		struct timeval t;
-		gettimeofday(&t,NULL);
-		return t.tv_sec+t.tv_usec*1e-6;
-	}
+  #include <pthread.h>
+  #include <sys/time.h>
+  double gettime() {
+  struct timeval t;
+  gettimeofday(&t,NULL);
+  return t.tv_sec+t.tv_usec*1e-6;
+  }
 #endif
 
 
-#ifdef NV 
-	#include <oclUtils.h>
+#ifdef NV
+  #include <oclUtils.h>
 #else
-	#include <CL/cl.h>
+  #include <CL/cl.h>
 #endif
 
 #ifndef FLT_MAX
@@ -60,55 +60,55 @@ static cl_int           num_devices;
 
 static int initialize(int platform_id, int device_id, int use_gpu)
 {
-	cl_int result;
-	size_t size;
-
-	// modify a bit the initialization of the platforms in order to handle more than one platform (since on the odroid we have the GPU and the CPU platform).
-	cl_uint platformCount;
-	cl_platform_id *platforms_ids;
-	// create OpenCL context
-	clGetPlatformIDs(0, NULL, &platformCount);
+  cl_int result;
+  size_t size;
+
+  // modify a bit the initialization of the platforms in order to handle more than one platform (since on the odroid we have the GPU and the CPU platform).
+  cl_uint platformCount;
+  cl_platform_id *platforms_ids;
+  // create OpenCL context
+  clGetPlatformIDs(0, NULL, &platformCount);
   platforms_ids = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount);
   clGetPlatformIDs(platformCount, platforms_ids, NULL);
 
-	if (clGetPlatformIDs(platformCount, platforms_ids, NULL) != CL_SUCCESS) { printf("ERROR: clGetPlatformIDs(1,*,0) failed\n"); return -1; }
-	cl_context_properties ctxprop[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platforms_ids[platform_id], 0};
-	device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
-	context = clCreateContextFromType( ctxprop, device_type, NULL, NULL, NULL );
-	if( !context ) { printf("ERROR: clCreateContextFromType(%s) failed\n", use_gpu ? "GPU" : "CPU"); return -1; }
-
-	// get the list of GPUs
-	result = clGetContextInfo( context, CL_CONTEXT_DEVICES, 0, NULL, &size );
-	num_devices = (int) (size / sizeof(cl_device_id));
-	
-	if( result != CL_SUCCESS || num_devices < 1 ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
-	device_list = new cl_device_id[num_devices];
-	if( !device_list ) { printf("ERROR: new cl_device_id[] failed\n"); return -1; }
-	result = clGetContextInfo( context, CL_CONTEXT_DEVICES, size, device_list, NULL );
-	if( result != CL_SUCCESS ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
-
-	// create command queue for the first device
-	cmd_queue = clCreateCommandQueue( context, device_list[device_id], 0, NULL );
-	if( !cmd_queue ) { printf("ERROR: clCreateCommandQueue() failed\n"); return -1; }
-
-	return 0;
+  if (clGetPlatformIDs(platformCount, platforms_ids, NULL) != CL_SUCCESS) { printf("ERROR: clGetPlatformIDs(1,*,0) failed\n"); return -1; }
+  cl_context_properties ctxprop[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platforms_ids[platform_id], 0};
+  device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
+  context = clCreateContextFromType( ctxprop, device_type, NULL, NULL, NULL );
+  if( !context ) { printf("ERROR: clCreateContextFromType(%s) failed\n", use_gpu ? "GPU" : "CPU"); return -1; }
+
+  // get the list of GPUs
+  result = clGetContextInfo( context, CL_CONTEXT_DEVICES, 0, NULL, &size );
+  num_devices = (int) (size / sizeof(cl_device_id));
+
+  if( result != CL_SUCCESS || num_devices < 1 ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
+  device_list = new cl_device_id[num_devices];
+  if( !device_list ) { printf("ERROR: new cl_device_id[] failed\n"); return -1; }
+  result = clGetContextInfo( context, CL_CONTEXT_DEVICES, size, device_list, NULL );
+  if( result != CL_SUCCESS ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
+
+  // create command queue for the first device
+  cmd_queue = clCreateCommandQueue( context, device_list[device_id], 0, NULL );
+  if( !cmd_queue ) { printf("ERROR: clCreateCommandQueue() failed\n"); return -1; }
+
+  return 0;
 }
 
 static int shutdown()
 {
-	// release resources
-	if( cmd_queue ) clReleaseCommandQueue( cmd_queue );
-	if( context ) clReleaseContext( context );
-	if( device_list ) delete device_list;
-
-	// reset all variables
-	cmd_queue = 0;
-	context = 0;
-	device_list = 0;
-	num_devices = 0;
-	device_type = 0;
-
-	return 0;
+  // release resources
+  if( cmd_queue ) clReleaseCommandQueue( cmd_queue );
+  if( context ) clReleaseContext( context );
+  if( device_list ) delete device_list;
+
+  // reset all variables
+  cmd_queue = 0;
+  context = 0;
+  device_list = 0;
+  num_devices = 0;
+  device_type = 0;
+
+  return 0;
 }
 
 cl_mem d_feature;
@@ -129,92 +129,92 @@ float *center_d;
 int allocate(int n_points, int n_features, int n_clusters, float **feature, int platform_id, int device_id, int use_gpu)
 {
 
-	int sourcesize = 1024*1024;
-	char * source = (char *)calloc(sourcesize, sizeof(char)); 
-	if(!source) { printf("ERROR: calloc(%d) failed\n", sourcesize); return -1; }
-
-	// read the kernel core source
-	char * tempchar = "./kmeans.cl";
-	FILE * fp = fopen(tempchar, "rb"); 
-	if(!fp) { printf("ERROR: unable to open '%s'\n", tempchar); return -1; }
-	fread(source + strlen(source), sourcesize, 1, fp);
-	fclose(fp);
-		
-	// OpenCL initialization
-	if(initialize(platform_id, device_id, use_gpu)) return -1;
-
-	// compile kernel
-	cl_int err = 0;
-	const char * slist[2] = { source, 0 };
-	cl_program prog = clCreateProgramWithSource(context, 1, slist, NULL, &err);
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateProgramWithSource() => %d\n", err); return -1; }
-	err = clBuildProgram(prog, 0, NULL, NULL, NULL, NULL);
-	{ // show warnings/errors
-	//	static char log[65536]; memset(log, 0, sizeof(log));
-	//	cl_device_id device_id = 0;
-	//	err = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(device_id), &device_id, NULL);
-	//	clGetProgramBuildInfo(prog, device_id, CL_PROGRAM_BUILD_LOG, sizeof(log)-1, log, NULL);
-	//	if(err || strstr(log,"warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);
-	}
-	if(err != CL_SUCCESS) { printf("ERROR: clBuildProgram() => %d\n", err); return -1; }
-	
-	char * kernel_kmeans_c  = "kmeans_kernel_c";
-	char * kernel_swap  = "kmeans_swap";	
-		
-	kernel_s = clCreateKernel(prog, kernel_kmeans_c, &err);  
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateKernel() 0 => %d\n", err); return -1; }
-	kernel2 = clCreateKernel(prog, kernel_swap, &err);  
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateKernel() 0 => %d\n", err); return -1; }
-		
-	clReleaseProgram(prog);	
-	
-	d_feature = clCreateBuffer(context, CL_MEM_READ_WRITE, n_points * n_features * sizeof(float), NULL, &err );
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer d_feature (size:%d) => %d\n", n_points * n_features, err); return -1;}
-	d_feature_swap = clCreateBuffer(context, CL_MEM_READ_WRITE, n_points * n_features * sizeof(float), NULL, &err );
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer d_feature_swap (size:%d) => %d\n", n_points * n_features, err); return -1;}
-	d_cluster = clCreateBuffer(context, CL_MEM_READ_WRITE, n_clusters * n_features  * sizeof(float), NULL, &err );
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer d_cluster (size:%d) => %d\n", n_clusters * n_features, err); return -1;}
-	d_membership = clCreateBuffer(context, CL_MEM_READ_WRITE, n_points * sizeof(int), NULL, &err );
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer d_membership (size:%d) => %d\n", n_points, err); return -1;}
-		
-	//write buffers
-	err = clEnqueueWriteBuffer(cmd_queue, d_feature, 1, 0, n_points * n_features * sizeof(float), feature[0], 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer d_feature (size:%d) => %d\n", n_points * n_features, err); return -1; }
-	
-	clSetKernelArg(kernel2, 0, sizeof(void *), (void*) &d_feature);
-	clSetKernelArg(kernel2, 1, sizeof(void *), (void*) &d_feature_swap);
-	clSetKernelArg(kernel2, 2, sizeof(cl_int), (void*) &n_points);
-	clSetKernelArg(kernel2, 3, sizeof(cl_int), (void*) &n_features);
-	
-	size_t global_work[3] = { n_points, 1, 1 };
-	/// Ke Wang adjustable local group size 2013/08/07 10:37:33
-	size_t local_work_size= BLOCK_SIZE; // work group size is defined by RD_WG_SIZE_0 or RD_WG_SIZE_0_0 2014/06/10 17:00:51
-	if(global_work[0]%local_work_size !=0)
-	  global_work[0]=(global_work[0]/local_work_size+1)*local_work_size;
-
-	err = clEnqueueNDRangeKernel(cmd_queue, kernel2, 1, NULL, global_work, &local_work_size, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }
-	
-	membership_OCL = (int*) malloc(n_points * sizeof(int));
+  int sourcesize = 1024*1024;
+  char * source = (char *)calloc(sourcesize, sizeof(char));
+  if(!source) { printf("ERROR: calloc(%d) failed\n", sourcesize); return -1; }
+
+  // read the kernel core source
+  char * tempchar = "./kmeans.cl";
+  FILE * fp = fopen(tempchar, "rb");
+  if(!fp) { printf("ERROR: unable to open '%s'\n", tempchar); return -1; }
+  fread(source + strlen(source), sourcesize, 1, fp);
+  fclose(fp);
+
+  // OpenCL initialization
+  if(initialize(platform_id, device_id, use_gpu)) return -1;
+
+  // compile kernel
+  cl_int err = 0;
+  const char * slist[2] = { source, 0 };
+  cl_program prog = clCreateProgramWithSource(context, 1, slist, NULL, &err);
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateProgramWithSource() => %d\n", err); return -1; }
+  err = clBuildProgram(prog, 0, NULL, NULL, NULL, NULL);
+  { // show warnings/errors
+  //	static char log[65536]; memset(log, 0, sizeof(log));
+  //	cl_device_id device_id = 0;
+  //	err = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(device_id), &device_id, NULL);
+  //	clGetProgramBuildInfo(prog, device_id, CL_PROGRAM_BUILD_LOG, sizeof(log)-1, log, NULL);
+  //	if(err || strstr(log,"warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);
+  }
+  if(err != CL_SUCCESS) { printf("ERROR: clBuildProgram() => %d\n", err); return -1; }
+
+  char * kernel_kmeans_c  = "kmeans_kernel_c";
+  char * kernel_swap  = "kmeans_swap";
+
+  kernel_s = clCreateKernel(prog, kernel_kmeans_c, &err);
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateKernel() 0 => %d\n", err); return -1; }
+  kernel2 = clCreateKernel(prog, kernel_swap, &err);
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateKernel() 0 => %d\n", err); return -1; }
+
+  clReleaseProgram(prog);
+
+  d_feature = clCreateBuffer(context, CL_MEM_READ_WRITE, n_points * n_features * sizeof(float), NULL, &err );
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer d_feature (size:%d) => %d\n", n_points * n_features, err); return -1;}
+  d_feature_swap = clCreateBuffer(context, CL_MEM_READ_WRITE, n_points * n_features * sizeof(float), NULL, &err );
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer d_feature_swap (size:%d) => %d\n", n_points * n_features, err); return -1;}
+  d_cluster = clCreateBuffer(context, CL_MEM_READ_WRITE, n_clusters * n_features  * sizeof(float), NULL, &err );
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer d_cluster (size:%d) => %d\n", n_clusters * n_features, err); return -1;}
+  d_membership = clCreateBuffer(context, CL_MEM_READ_WRITE, n_points * sizeof(int), NULL, &err );
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer d_membership (size:%d) => %d\n", n_points, err); return -1;}
+
+  //write buffers
+  err = clEnqueueWriteBuffer(cmd_queue, d_feature, 1, 0, n_points * n_features * sizeof(float), feature[0], 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer d_feature (size:%d) => %d\n", n_points * n_features, err); return -1; }
+
+  clSetKernelArg(kernel2, 0, sizeof(void *), (void*) &d_feature);
+  clSetKernelArg(kernel2, 1, sizeof(void *), (void*) &d_feature_swap);
+  clSetKernelArg(kernel2, 2, sizeof(cl_int), (void*) &n_points);
+  clSetKernelArg(kernel2, 3, sizeof(cl_int), (void*) &n_features);
+
+  size_t global_work[3] = { n_points, 1, 1 };
+  /// Ke Wang adjustable local group size 2013/08/07 10:37:33
+  size_t local_work_size= BLOCK_SIZE; // work group size is defined by RD_WG_SIZE_0 or RD_WG_SIZE_0_0 2014/06/10 17:00:51
+  if(global_work[0]%local_work_size !=0)
+    global_work[0]=(global_work[0]/local_work_size+1)*local_work_size;
+
+  err = clEnqueueNDRangeKernel(cmd_queue, kernel2, 1, NULL, global_work, &local_work_size, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }
+
+  membership_OCL = (int*) malloc(n_points * sizeof(int));
 }
 
 void deallocateMemory()
 {
-	clReleaseMemObject(d_feature);
-	clReleaseMemObject(d_feature_swap);
-	clReleaseMemObject(d_cluster);
-	clReleaseMemObject(d_membership);
-	free(membership_OCL);
+  clReleaseMemObject(d_feature);
+  clReleaseMemObject(d_feature_swap);
+  clReleaseMemObject(d_cluster);
+  clReleaseMemObject(d_membership);
+  free(membership_OCL);
 
 }
 
 
-int main( int argc, char** argv) 
+int main( int argc, char** argv)
 {
-	printf("WG size of kernel_swap = %d, WG size of kernel_kmeans = %d \n", BLOCK_SIZE, BLOCK_SIZE2);
+  printf("WG size of kernel_swap = %d, WG size of kernel_kmeans = %d \n", BLOCK_SIZE, BLOCK_SIZE2);
 
-	setup(argc, argv);
-	shutdown();
+  setup(argc, argv);
+  shutdown();
 }
 
 int	kmeansOCL(float **feature,    /* in: [npoints][nfeatures] */
@@ -222,57 +222,57 @@ int	kmeansOCL(float **feature,    /* in: [npoints][nfeatures] */
        int     n_points,
        int     n_clusters,
        int    *membership,
-		   float **clusters,
-		   int     *new_centers_len,
-       float  **new_centers)	
+     float **clusters,
+     int     *new_centers_len,
+       float  **new_centers)
 {
-  
-	int delta = 0;
-	int i, j, k;
-	cl_int err = 0;
-	
-	size_t global_work[3] = { n_points, 1, 1 }; 
-
-	/// Ke Wang adjustable local group size 2013/08/07 10:37:33
-	size_t local_work_size=BLOCK_SIZE2; // work group size is defined by RD_WG_SIZE_1 or RD_WG_SIZE_1_0 2014/06/10 17:00:41
-	if(global_work[0]%local_work_size !=0)
-	  global_work[0]=(global_work[0]/local_work_size+1)*local_work_size;
-	
-	err = clEnqueueWriteBuffer(cmd_queue, d_cluster, 1, 0, n_clusters * n_features * sizeof(float), clusters[0], 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer d_cluster (size:%d) => %d\n", n_points, err); return -1; }
-
-	int size = 0; int offset = 0;
-					
-	clSetKernelArg(kernel_s, 0, sizeof(void *), (void*) &d_feature_swap);
-	clSetKernelArg(kernel_s, 1, sizeof(void *), (void*) &d_cluster);
-	clSetKernelArg(kernel_s, 2, sizeof(void *), (void*) &d_membership);
-	clSetKernelArg(kernel_s, 3, sizeof(cl_int), (void*) &n_points);
-	clSetKernelArg(kernel_s, 4, sizeof(cl_int), (void*) &n_clusters);
-	clSetKernelArg(kernel_s, 5, sizeof(cl_int), (void*) &n_features);
-	clSetKernelArg(kernel_s, 6, sizeof(cl_int), (void*) &offset);
-	clSetKernelArg(kernel_s, 7, sizeof(cl_int), (void*) &size);
-
-	err = clEnqueueNDRangeKernel(cmd_queue, kernel_s, 1, NULL, global_work, &local_work_size, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }
-	clFinish(cmd_queue);
-	err = clEnqueueReadBuffer(cmd_queue, d_membership, 1, 0, n_points * sizeof(int), membership_OCL, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: Memcopy Out\n"); return -1; }
-	
-	delta = 0;
-	for (i = 0; i < n_points; i++)
-	{
-		int cluster_id = membership_OCL[i];
-		new_centers_len[cluster_id]++;
-		if (membership_OCL[i] != membership[i])
-		{
-			delta++;
-			membership[i] = membership_OCL[i];
-		}
-		for (j = 0; j < n_features; j++)
-		{
-			new_centers[cluster_id][j] += feature[i][j];
-		}
-	}
-
-	return delta;
+
+  int delta = 0;
+  int i, j, k;
+  cl_int err = 0;
+
+  size_t global_work[3] = { n_points, 1, 1 };
+
+  /// Ke Wang adjustable local group size 2013/08/07 10:37:33
+  size_t local_work_size=BLOCK_SIZE2; // work group size is defined by RD_WG_SIZE_1 or RD_WG_SIZE_1_0 2014/06/10 17:00:41
+  if(global_work[0]%local_work_size !=0)
+    global_work[0]=(global_work[0]/local_work_size+1)*local_work_size;
+
+  err = clEnqueueWriteBuffer(cmd_queue, d_cluster, 1, 0, n_clusters * n_features * sizeof(float), clusters[0], 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer d_cluster (size:%d) => %d\n", n_points, err); return -1; }
+
+  int size = 0; int offset = 0;
+
+  clSetKernelArg(kernel_s, 0, sizeof(void *), (void*) &d_feature_swap);
+  clSetKernelArg(kernel_s, 1, sizeof(void *), (void*) &d_cluster);
+  clSetKernelArg(kernel_s, 2, sizeof(void *), (void*) &d_membership);
+  clSetKernelArg(kernel_s, 3, sizeof(cl_int), (void*) &n_points);
+  clSetKernelArg(kernel_s, 4, sizeof(cl_int), (void*) &n_clusters);
+  clSetKernelArg(kernel_s, 5, sizeof(cl_int), (void*) &n_features);
+  clSetKernelArg(kernel_s, 6, sizeof(cl_int), (void*) &offset);
+  clSetKernelArg(kernel_s, 7, sizeof(cl_int), (void*) &size);
+
+  err = clEnqueueNDRangeKernel(cmd_queue, kernel_s, 1, NULL, global_work, &local_work_size, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }
+  clFinish(cmd_queue);
+  err = clEnqueueReadBuffer(cmd_queue, d_membership, 1, 0, n_points * sizeof(int), membership_OCL, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: Memcopy Out\n"); return -1; }
+
+  delta = 0;
+  for (i = 0; i < n_points; i++)
+  {
+  int cluster_id = membership_OCL[i];
+  new_centers_len[cluster_id]++;
+  if (membership_OCL[i] != membership[i])
+  {
+  delta++;
+  membership[i] = membership_OCL[i];
+  }
+  for (j = 0; j < n_features; j++)
+  {
+  new_centers[cluster_id][j] += feature[i][j];
+  }
+  }
+
+  return delta;
 }

+ 2 - 2
opencl/kmeans/kmeans.h

@@ -17,7 +17,7 @@
 /*                                                                           */
 /*2       Redistributions in binary form must reproduce the above copyright   */
 /*        notice, this list of conditions and the following disclaimer in the */
-/*        documentation and/or other materials provided with the distribution.*/ 
+/*        documentation and/or other materials provided with the distribution.*/
 /*                                                                            */
 /*3       Neither the name of Northwestern University nor the names of its    */
 /*        contributors may be used to endorse or promote products derived     */
@@ -53,6 +53,6 @@ int setup(int argc, char** argv);
 int allocate(int npoints, int nfeatures, int nclusters, float **feature, int platform_id, int device_id, int use_gpu);
 void deallocateMemory();
 int	kmeansOCL(float **feature, int nfeatures, int npoints, int nclusters, int *membership, float **clusters, int *new_centers_len, float  **new_centers);
-float** kmeans_clustering(float **feature, int nfeatures, int npoints, int nclusters, float threshold, int *membership); 
+float** kmeans_clustering(float **feature, int nfeatures, int npoints, int nclusters, float threshold, int *membership);
 
 #endif

+ 56 - 57
opencl/kmeans/kmeans_clustering.c

@@ -17,7 +17,7 @@
 /*                                                                           */
 /*2       Redistributions in binary form must reproduce the above copyright   */
 /*        notice, this list of conditions and the following disclaimer in the */
-/*        documentation and/or other materials provided with the distribution.*/ 
+/*        documentation and/or other materials provided with the distribution.*/
 /*                                                                            */
 /*3       Neither the name of Northwestern University nor the names of its    */
 /*        contributors may be used to endorse or promote products derived     */
@@ -79,25 +79,25 @@ float** kmeans_clustering(float **feature,    /* in: [npoints][nfeatures] */
                           int     nclusters,
                           float   threshold,
                           int    *membership) /* out: [npoints] */
-{    
+{
   int      i, j, n = 0;				/* counters */
-	int		 loop=0, temp;
+  int		 loop=0, temp;
   int     *new_centers_len;	/* [nclusters]: no. of points in each cluster */
   float    delta;				/* if the point moved */
   float  **clusters;			/* out: [nclusters][nfeatures] */
   float  **new_centers;		/* [nclusters][nfeatures] */
 
-	int     *initial;			/* used to hold the index of points not yet selected
-								   prevents the "birthday problem" of dual selection (?)
-								   considered holding initial cluster indices, but changed due to
-								   possible, though unlikely, infinite loops */
-	int      initial_points;
-	int		 c = 0;
+  int     *initial;			/* used to hold the index of points not yet selected
+     prevents the "birthday problem" of dual selection (?)
+     considered holding initial cluster indices, but changed due to
+     possible, though unlikely, infinite loops */
+  int      initial_points;
+  int		 c = 0;
 
-	/* nclusters should never be > npoints
-	   that would guarantee a cluster without points */
-	if (nclusters > npoints)
-		nclusters = npoints;
+  /* nclusters should never be > npoints
+     that would guarantee a cluster without points */
+  if (nclusters > npoints)
+  nclusters = npoints;
 
   /* allocate space for and initialize returning variable clusters[] */
   clusters    = (float**) malloc(nclusters *             sizeof(float*));
@@ -105,33 +105,33 @@ float** kmeans_clustering(float **feature,    /* in: [npoints][nfeatures] */
   for (i=1; i<nclusters; i++)
       clusters[i] = clusters[i-1] + nfeatures;
 
-	/* initialize the random clusters */
-	initial = (int *) malloc (npoints * sizeof(int));
-	for (i = 0; i < npoints; i++)
-	{
-		initial[i] = i;
-	}
-	initial_points = npoints;
+  /* initialize the random clusters */
+  initial = (int *) malloc (npoints * sizeof(int));
+  for (i = 0; i < npoints; i++)
+  {
+  initial[i] = i;
+  }
+  initial_points = npoints;
 
     /* randomly pick cluster centers */
     for (i=0; i<nclusters && initial_points >= 0; i++) {
-		//n = (int)rand() % initial_points;		
-		
+  //n = (int)rand() % initial_points;
+
         for (j=0; j<nfeatures; j++)
             clusters[i][j] = feature[initial[n]][j];	// remapped
 
-		/* swap the selected index to the end (not really necessary,
-		   could just move the end up) */
-		temp = initial[n];
-		initial[n] = initial[initial_points-1];
-		initial[initial_points-1] = temp;
-		initial_points--;
-		n++;
+  /* swap the selected index to the end (not really necessary,
+     could just move the end up) */
+  temp = initial[n];
+  initial[n] = initial[initial_points-1];
+  initial[initial_points-1] = temp;
+  initial_points--;
+  n++;
     }
 
-	/* initialize the membership to -1 for all */
+  /* initialize the membership to -1 for all */
     for (i=0; i < npoints; i++)
-	  membership[i] = -1;
+    membership[i] = -1;
 
     /* allocate space for and initialize new_centers_len and new_centers */
     new_centers_len = (int*) calloc(nclusters, sizeof(int));
@@ -141,37 +141,36 @@ float** kmeans_clustering(float **feature,    /* in: [npoints][nfeatures] */
     for (i=1; i<nclusters; i++)
         new_centers[i] = new_centers[i-1] + nfeatures;
 
-	/* iterate until convergence */
-	do {
+  /* iterate until convergence */
+  do {
         delta = 0.0;
-		// CUDA
-		delta = (float) kmeansOCL(feature,			/* in: [npoints][nfeatures] */
-								   nfeatures,		/* number of attributes for each point */
-								   npoints,			/* number of data points */
-								   nclusters,		/* number of clusters */
-								   membership,		/* which cluster the point belongs to */
-								   clusters,		/* out: [nclusters][nfeatures] */
-								   new_centers_len,	/* out: number of points in each cluster */
-								   new_centers		/* sum of points in each cluster */
-								   );
-
-		/* replace old cluster centers with new_centers */
-		/* CPU side of reduction */
-		for (i=0; i<nclusters; i++) {
-			for (j=0; j<nfeatures; j++) {
-				if (new_centers_len[i] > 0)
-					clusters[i][j] = new_centers[i][j] / new_centers_len[i];	/* take average i.e. sum/n */
-				new_centers[i][j] = 0.0;	/* set back to 0 */
-			}
-			new_centers_len[i] = 0;			/* set back to 0 */
-		}	 
-		c++;
+  // CUDA
+  delta = (float) kmeansOCL(feature,			/* in: [npoints][nfeatures] */
+     nfeatures,		/* number of attributes for each point */
+     npoints,			/* number of data points */
+     nclusters,		/* number of clusters */
+     membership,		/* which cluster the point belongs to */
+     clusters,		/* out: [nclusters][nfeatures] */
+     new_centers_len,	/* out: number of points in each cluster */
+     new_centers		/* sum of points in each cluster */
+     );
+
+  /* replace old cluster centers with new_centers */
+  /* CPU side of reduction */
+  for (i=0; i<nclusters; i++) {
+  for (j=0; j<nfeatures; j++) {
+  if (new_centers_len[i] > 0)
+  clusters[i][j] = new_centers[i][j] / new_centers_len[i];	/* take average i.e. sum/n */
+  new_centers[i][j] = 0.0;	/* set back to 0 */
+  }
+  new_centers_len[i] = 0;			/* set back to 0 */
+  }
+  c++;
     } while ((delta > threshold) && (loop++ < 500));	/* makes sure loop terminates */
-	printf("iterated %d times\n", c);
+  printf("iterated %d times\n", c);
     free(new_centers[0]);
     free(new_centers);
     free(new_centers_len);
 
     return clusters;
 }
-

+ 135 - 136
opencl/kmeans/read_input.c

@@ -17,7 +17,7 @@
 /*                                                                           */
 /*2       Redistributions in binary form must reproduce the above copyright   */
 /*        notice, this list of conditions and the following disclaimer in the */
-/*        documentation and/or other materials provided with the distribution.*/ 
+/*        documentation and/or other materials provided with the distribution.*/
 /*                                                                            */
 /*3       Neither the name of Northwestern University nor the names of its    */
 /*        contributors may be used to endorse or promote products derived     */
@@ -88,14 +88,14 @@ extern double wtime(void);
 void usage(char *argv0) {
     char *help =
     "\nUsage: %s [switches] -i filename\n\n"
-		"    -i filename      :file containing data to be clustered\n"		
-		"    -m max_nclusters :maximum number of clusters allowed    [default=5]\n"
+  "    -i filename      :file containing data to be clustered\n"
+  "    -m max_nclusters :maximum number of clusters allowed    [default=5]\n"
     "    -n min_nclusters :minimum number of clusters allowed    [default=5]\n"
-		"    -t threshold     :threshold value                       [default=0.001]\n"
-		"    -l nloops        :iteration for each number of clusters [default=1]\n"
-		"    -b               :input file is in binary format\n"
+  "    -t threshold     :threshold value                       [default=0.001]\n"
+  "    -l nloops        :iteration for each number of clusters [default=1]\n"
+  "    -b               :input file is in binary format\n"
     "    -r               :calculate RMSE                        [default=off]\n"
-		"    -o               :output cluster center coordinates     [default=off]\n"
+  "    -o               :output cluster center coordinates     [default=off]\n"
     "    -p platform_id   :OCL platform to use                   [default=0]\n"
     "    -d device_id     :OCL device to use                     [default=0]\n"
     "    -g use_gpu       :1 for GPU 0 for CPU                   [default=0]\n";
@@ -105,62 +105,62 @@ void usage(char *argv0) {
 
 /*---< main() >-------------------------------------------------------------*/
 int setup(int argc, char **argv) {
-		int		opt;
-    extern char   *optarg;
-		char   *filename = 0;
-		float  *buf;
-		char	line[1024];
-		int		isBinaryFile = 0;
-
-		float	threshold = 0.001;		/* default value */
-		int		max_nclusters=5;		/* default value */
-		int		min_nclusters=5;		/* default value */
-		int		best_nclusters = 0;
-		int		nfeatures = 0;
-		int		npoints = 0;
-		float	len;
-		         
-		float **features;
-		float **cluster_centres=NULL;
-		int		i, j, index;
-		int		nloops = 1;				/* default value */
-				
-		int		isRMSE = 0;		
-		float	rmse;
-		
-		int		isOutput = 0;
-		//float	cluster_timing, io_timing;		
-    
+  int		opt;
+  extern char   *optarg;
+  char   *filename = 0;
+  float  *buf;
+  char	line[1024];
+  int		isBinaryFile = 0;
+
+  float	threshold = 0.001;		/* default value */
+  int		max_nclusters=5;		/* default value */
+  int		min_nclusters=5;		/* default value */
+  int		best_nclusters = 0;
+  int		nfeatures = 0;
+  int		npoints = 0;
+  float	len;
+
+  float **features;
+  float **cluster_centres=NULL;
+  int		i, j, index;
+  int		nloops = 1;				/* default value */
+
+  int		isRMSE = 0;
+  float	rmse;
+
+  int		isOutput = 0;
+  //float	cluster_timing, io_timing;
+
     // Variables to store information on platform and device to use_gpu
     int platform_id = 0;
     int device_id = 0;
     int use_gpu = 0;
 
-		/* obtain command line arguments and change appropriate options */
-		while ( (opt=getopt(argc,argv,"i:t:m:n:l:brop:d:g:"))!= EOF) {
+  /* obtain command line arguments and change appropriate options */
+  while ( (opt=getopt(argc,argv,"i:t:m:n:l:brop:d:g:"))!= EOF) {
         switch (opt) {
             case 'i': filename=optarg;
                       break;
             case 'b': isBinaryFile = 1;
-                      break;            
+                      break;
             case 't': threshold=atof(optarg);
                       break;
             case 'm': max_nclusters = atoi(optarg);
                       break;
             case 'n': min_nclusters = atoi(optarg);
                       break;
-			      case 'r': isRMSE = 1;
+        case 'r': isRMSE = 1;
                       break;
-			      case 'o': isOutput = 1;
-					            break;
-		        case 'l': nloops = atoi(optarg);
-					            break;
+        case 'o': isOutput = 1;
+              break;
+          case 'l': nloops = atoi(optarg);
+              break;
             case 'p': platform_id = atoi(optarg);
-					            break;
+              break;
             case 'd': device_id = atoi(optarg);
-					            break;
+              break;
             case 'g': use_gpu = atoi(optarg);
-                      break;                                                    
+                      break;
             case '?': usage(argv[0]);
                       break;
             default: usage(argv[0]);
@@ -169,8 +169,8 @@ int setup(int argc, char **argv) {
     }
 
     if (filename == 0) usage(argv[0]);
-		
-	/* ============== I/O begin ==============*/
+
+  /* ============== I/O begin ==============*/
     /* get nfeatures and npoints */
     //io_timing = omp_get_wtime();
     if (isBinaryFile) {		//Binary file input
@@ -180,7 +180,7 @@ int setup(int argc, char **argv) {
             exit(1);
         }
         read(infile, &npoints,   sizeof(int));
-        read(infile, &nfeatures, sizeof(int));        
+        read(infile, &nfeatures, sizeof(int));
 
         /* allocate space for features[][] and read attributes of all objects */
         buf         = (float*) malloc(npoints*nfeatures*sizeof(float));
@@ -198,10 +198,10 @@ int setup(int argc, char **argv) {
         if ((infile = fopen(filename, "r")) == NULL) {
             fprintf(stderr, "Error: no such file (%s)\n", filename);
             exit(1);
-		}		
+  }
         while (fgets(line, 1024, infile) != NULL)
-			if (strtok(line, " \t\n") != 0)
-                npoints++;			
+  if (strtok(line, " \t\n") != 0)
+                npoints++;
         rewind(infile);
         while (fgets(line, 1024, infile) != NULL) {
             if (strtok(line, " \t\n") != 0) {
@@ -209,7 +209,7 @@ int setup(int argc, char **argv) {
                 while (strtok(NULL, " ,\t\n") != NULL) nfeatures++;
                 break;
             }
-        }        
+        }
 
         /* allocate space for features[] and read attributes of all objects */
         buf         = (float*) malloc(npoints*nfeatures*sizeof(float));
@@ -220,104 +220,103 @@ int setup(int argc, char **argv) {
         rewind(infile);
         i = 0;
         while (fgets(line, 1024, infile) != NULL) {
-            if (strtok(line, " \t\n") == NULL) continue;            
+            if (strtok(line, " \t\n") == NULL) continue;
             for (j=0; j<nfeatures; j++) {
-                buf[i] = atof(strtok(NULL, " ,\t\n"));             
+                buf[i] = atof(strtok(NULL, " ,\t\n"));
                 i++;
-            }            
+            }
         }
         fclose(infile);
     }
     //io_timing = omp_get_wtime() - io_timing;
-	
-	printf("\nI/O completed\n");
-	printf("\nNumber of objects: %d\n", npoints);
-	printf("Number of features: %d\n", nfeatures);	
-	/* ============== I/O end ==============*/
 
-	// error check for clusters
-	if (npoints < min_nclusters)
-	{
-		printf("Error: min_nclusters(%d) > npoints(%d) -- cannot proceed\n", min_nclusters, npoints);
-		exit(0);
-	}
+  printf("\nI/O completed\n");
+  printf("\nNumber of objects: %d\n", npoints);
+  printf("Number of features: %d\n", nfeatures);
+  /* ============== I/O end ==============*/
+
+  // error check for clusters
+  if (npoints < min_nclusters)
+  {
+  printf("Error: min_nclusters(%d) > npoints(%d) -- cannot proceed\n", min_nclusters, npoints);
+  exit(0);
+  }
 
-	srand(7);												/* seed for future random number generator */	
-	memcpy(features[0], buf, npoints*nfeatures*sizeof(float)); /* now features holds 2-dimensional array of features */
-	free(buf);
+  srand(7);												/* seed for future random number generator */
+  memcpy(features[0], buf, npoints*nfeatures*sizeof(float)); /* now features holds 2-dimensional array of features */
+  free(buf);
 
-	/* ======================= core of the clustering ===================*/
+  /* ======================= core of the clustering ===================*/
 
     //cluster_timing = omp_get_wtime();		/* Total clustering time */
-	cluster_centres = NULL;
+  cluster_centres = NULL;
     index = cluster(npoints,				/* number of data points */
-					nfeatures,				/* number of features for each point */
-					features,				/* array: [npoints][nfeatures] */
-					min_nclusters,			/* range of min to max number of clusters */
-					max_nclusters,
-					threshold,				/* loop termination factor */
-				  &best_nclusters,			/* return: number between min and max */
-				  &cluster_centres,		/* return: [best_nclusters][nfeatures] */  
-				  &rmse,					/* Root Mean Squared Error */
-					isRMSE,					/* calculate RMSE */
-					nloops,
+  nfeatures,				/* number of features for each point */
+  features,				/* array: [npoints][nfeatures] */
+  min_nclusters,			/* range of min to max number of clusters */
+  max_nclusters,
+  threshold,				/* loop termination factor */
+    &best_nclusters,			/* return: number between min and max */
+    &cluster_centres,		/* return: [best_nclusters][nfeatures] */
+    &rmse,					/* Root Mean Squared Error */
+  isRMSE,					/* calculate RMSE */
+  nloops,
           platform_id,
           device_id,
-          use_gpu);				/* number of iteration for each number of clusters */		
-    
-	//cluster_timing = omp_get_wtime() - cluster_timing;
-
-
-	/* =============== Command Line Output =============== */
-
-	/* cluster center coordinates
-	   :displayed only for when k=1*/
-	if((min_nclusters == max_nclusters) && (isOutput == 1)) {
-		printf("\n================= Centroid Coordinates =================\n");
-		for(i = 0; i < max_nclusters; i++){
-			printf("%d:", i);
-			for(j = 0; j < nfeatures; j++){
-				printf(" %.2f", cluster_centres[i][j]);
-			}
-			printf("\n\n");
-		}
-	}
-	
-	len = (float) ((max_nclusters - min_nclusters + 1)*nloops);
-
-	printf("Number of Iteration: %d\n", nloops);
-	//printf("Time for I/O: %.5fsec\n", io_timing);
-	//printf("Time for Entire Clustering: %.5fsec\n", cluster_timing);
-	
-	if(min_nclusters != max_nclusters){
-		if(nloops != 1){									//range of k, multiple iteration
-			//printf("Average Clustering Time: %fsec\n",
-			//		cluster_timing / len);
-			printf("Best number of clusters is %d\n", best_nclusters);				
-		}
-		else{												//range of k, single iteration
-			//printf("Average Clustering Time: %fsec\n",
-			//		cluster_timing / len);
-			printf("Best number of clusters is %d\n", best_nclusters);				
-		}
-	}
-	else{
-		if(nloops != 1){									// single k, multiple iteration
-			//printf("Average Clustering Time: %.5fsec\n",
-			//		cluster_timing / nloops);
-			if(isRMSE)										// if calculated RMSE
-				printf("Number of trials to approach the best RMSE of %.3f is %d\n", rmse, index + 1);
-		}
-		else{												// single k, single iteration				
-			if(isRMSE)										// if calculated RMSE
-				printf("Root Mean Squared Error: %.3f\n", rmse);
-		}
-	}
-	
-
-	/* free up memory */
-	free(features[0]);
-	free(features);    
+          use_gpu);				/* number of iteration for each number of clusters */
+
+  //cluster_timing = omp_get_wtime() - cluster_timing;
+
+
+  /* =============== Command Line Output =============== */
+
+  /* cluster center coordinates
+     :displayed only for when k=1*/
+  if((min_nclusters == max_nclusters) && (isOutput == 1)) {
+  printf("\n================= Centroid Coordinates =================\n");
+  for(i = 0; i < max_nclusters; i++){
+  printf("%d:", i);
+  for(j = 0; j < nfeatures; j++){
+  printf(" %.2f", cluster_centres[i][j]);
+  }
+  printf("\n\n");
+  }
+  }
+
+  len = (float) ((max_nclusters - min_nclusters + 1)*nloops);
+
+  printf("Number of Iteration: %d\n", nloops);
+  //printf("Time for I/O: %.5fsec\n", io_timing);
+  //printf("Time for Entire Clustering: %.5fsec\n", cluster_timing);
+
+  if(min_nclusters != max_nclusters){
+  if(nloops != 1){									//range of k, multiple iteration
+  //printf("Average Clustering Time: %fsec\n",
+  //		cluster_timing / len);
+  printf("Best number of clusters is %d\n", best_nclusters);
+  }
+  else{												//range of k, single iteration
+  //printf("Average Clustering Time: %fsec\n",
+  //		cluster_timing / len);
+  printf("Best number of clusters is %d\n", best_nclusters);
+  }
+  }
+  else{
+  if(nloops != 1){									// single k, multiple iteration
+  //printf("Average Clustering Time: %.5fsec\n",
+  //		cluster_timing / nloops);
+  if(isRMSE)										// if calculated RMSE
+  printf("Number of trials to approach the best RMSE of %.3f is %d\n", rmse, index + 1);
+  }
+  else{												// single k, single iteration
+  if(isRMSE)										// if calculated RMSE
+  printf("Root Mean Squared Error: %.3f\n", rmse);
+  }
+  }
+
+
+  /* free up memory */
+  free(features[0]);
+  free(features);
     return(0);
 }
-

+ 481 - 481
opencl/lavaMD/kernel/kernel_gpu_opencl_wrapper.c

@@ -36,504 +36,504 @@ extern "C" {
 //	KERNEL_GPU_OPENCL_WRAPPER FUNCTION
 //========================================================================================================================================================================================================200
 
-void 
+void
 kernel_gpu_opencl_wrapper(par_str par_cpu,
-							            dim_str dim_cpu,
-							            box_str* box_cpu,
-							            FOUR_VECTOR* rv_cpu,
-							            fp* qv_cpu,
-							            FOUR_VECTOR* fv_cpu,
+              dim_str dim_cpu,
+              box_str* box_cpu,
+              FOUR_VECTOR* rv_cpu,
+              fp* qv_cpu,
+              FOUR_VECTOR* fv_cpu,
                           int platform_id,
                           int device_id,
                           int use_gpu)
 {
 
-	//======================================================================================================================================================150
-	//	CPU VARIABLES
-	//======================================================================================================================================================150
-
-	// timer
-	long long time0;
-	long long time1;
-	long long time2;
-	long long time3;
-	long long time4;
-	long long time5;
-	long long time6;
-
-	time0 = get_time();
-
-	//======================================================================================================================================================150
-	//	GPU SETUP
-	//======================================================================================================================================================150
-
-	//====================================================================================================100
-	//	COMMON VARIABLES
-	//====================================================================================================100
-
-	// common variables
-	cl_int error;
-
-	//====================================================================================================100
-	//	GET PLATFORMS (Intel, AMD, NVIDIA, based on provided library), SELECT ONE
-	//====================================================================================================100
-
-	// Get the number of available platforms
-	cl_uint num_platforms;
-	error = clGetPlatformIDs(	0, 
-								NULL, 
-								&num_platforms);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// Get the list of available platforms
-	cl_platform_id *platforms = (cl_platform_id *)malloc(sizeof(cl_platform_id) * num_platforms);
-	error = clGetPlatformIDs(	num_platforms, 
-								platforms, 
-								NULL);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// Select the platform in accordance to platform_id passed as parameter
-	cl_platform_id platform = platforms[platform_id];
-
-	// Get the name of the selected platform and print it (if there are multiple platforms, choose the first one)
-	char pbuf[100];
-	error = clGetPlatformInfo(	platform, 
-								CL_PLATFORM_VENDOR, 
-								sizeof(pbuf), 
-								pbuf, 
-								NULL);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	printf("Platform: %s\n", pbuf);
-
-	//====================================================================================================100
-	//	CREATE CONTEXT FOR THE PLATFORM
-	//====================================================================================================100
+  //======================================================================================================================================================150
+  //	CPU VARIABLES
+  //======================================================================================================================================================150
+
+  // timer
+  long long time0;
+  long long time1;
+  long long time2;
+  long long time3;
+  long long time4;
+  long long time5;
+  long long time6;
+
+  time0 = get_time();
+
+  //======================================================================================================================================================150
+  //	GPU SETUP
+  //======================================================================================================================================================150
+
+  //====================================================================================================100
+  //	COMMON VARIABLES
+  //====================================================================================================100
+
+  // common variables
+  cl_int error;
+
+  //====================================================================================================100
+  //	GET PLATFORMS (Intel, AMD, NVIDIA, based on provided library), SELECT ONE
+  //====================================================================================================100
+
+  // Get the number of available platforms
+  cl_uint num_platforms;
+  error = clGetPlatformIDs(	0,
+  NULL,
+  &num_platforms);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // Get the list of available platforms
+  cl_platform_id *platforms = (cl_platform_id *)malloc(sizeof(cl_platform_id) * num_platforms);
+  error = clGetPlatformIDs(	num_platforms,
+  platforms,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // Select the platform in accordance to platform_id passed as parameter
+  cl_platform_id platform = platforms[platform_id];
+
+  // Get the name of the selected platform and print it (if there are multiple platforms, choose the first one)
+  char pbuf[100];
+  error = clGetPlatformInfo(	platform,
+  CL_PLATFORM_VENDOR,
+  sizeof(pbuf),
+  pbuf,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  printf("Platform: %s\n", pbuf);
+
+  //====================================================================================================100
+  //	CREATE CONTEXT FOR THE PLATFORM
+  //====================================================================================================100
 
   // Selector for the device type in accordance to what passed as parameter
   cl_device_type device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
-  
-	// Create context properties for selected platform
-	cl_context_properties context_properties[3] = {	CL_CONTEXT_PLATFORM, 
-													(cl_context_properties) platform, 
-													0};
-
-	// Create context for selected platform being CPU/GPU
-	cl_context context;
-	context = clCreateContextFromType(context_properties, 
-										                device_type, 
-										                NULL, 
-										                NULL, 
-										                &error);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	//	GET DEVICES AVAILABLE FOR THE CONTEXT, SELECT ONE
-	//====================================================================================================100
-
-	// Get the number of devices (previousely selected for the context)
-	size_t devices_size;
-	error = clGetContextInfo(	context, 
-								CL_CONTEXT_DEVICES, 
-								0, 
-								NULL, 
-								&devices_size);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// Get the list of devices (previousely selected for the context)
-	cl_device_id *devices = (cl_device_id *) malloc(devices_size);
-	error = clGetContextInfo(	context, 
-								CL_CONTEXT_DEVICES, 
-								devices_size, 
-								devices, 
-								NULL);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// Select the device passed as parameter
-	cl_device_id device;
-	device = devices[device_id];
-
-	// Get the name of the selected device (previousely selected for the context) and print it
-	error = clGetDeviceInfo(device, 
-							CL_DEVICE_NAME, 
-							sizeof(pbuf), 
-							pbuf, 
-							NULL);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	printf("Device: %s\n", pbuf);
-
-	//====================================================================================================100
-	//	CREATE COMMAND QUEUE FOR THE DEVICE
-	//====================================================================================================100
-
-	// Create a command queue
-	cl_command_queue command_queue;
-	command_queue = clCreateCommandQueue(	context, 
-											device, 
-											0, 
-											&error);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	//	CRATE PROGRAM, COMPILE IT
-	//====================================================================================================100
-
-	// Load kernel source code from file
-	const char *source = load_kernel_source("./kernel/kernel_gpu_opencl.cl");
-	size_t sourceSize = strlen(source);
-
-	// Create the program
-	cl_program program = clCreateProgramWithSource(	context, 
-													1, 
-													&source, 
-													&sourceSize, 
-													&error);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// parameterized kernel dimension
-	char clOptions[110];
-	//  sprintf(clOptions,"-I../../src");                                                                                 
-	sprintf(clOptions,"-I.");
+
+  // Create context properties for selected platform
+  cl_context_properties context_properties[3] = {	CL_CONTEXT_PLATFORM,
+  (cl_context_properties) platform,
+  0};
+
+  // Create context for selected platform being CPU/GPU
+  cl_context context;
+  context = clCreateContextFromType(context_properties,
+                  device_type,
+                  NULL,
+                  NULL,
+                  &error);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	GET DEVICES AVAILABLE FOR THE CONTEXT, SELECT ONE
+  //====================================================================================================100
+
+  // Get the number of devices (previousely selected for the context)
+  size_t devices_size;
+  error = clGetContextInfo(	context,
+  CL_CONTEXT_DEVICES,
+  0,
+  NULL,
+  &devices_size);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // Get the list of devices (previousely selected for the context)
+  cl_device_id *devices = (cl_device_id *) malloc(devices_size);
+  error = clGetContextInfo(	context,
+  CL_CONTEXT_DEVICES,
+  devices_size,
+  devices,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // Select the device passed as parameter
+  cl_device_id device;
+  device = devices[device_id];
+
+  // Get the name of the selected device (previousely selected for the context) and print it
+  error = clGetDeviceInfo(device,
+  CL_DEVICE_NAME,
+  sizeof(pbuf),
+  pbuf,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  printf("Device: %s\n", pbuf);
+
+  //====================================================================================================100
+  //	CREATE COMMAND QUEUE FOR THE DEVICE
+  //====================================================================================================100
+
+  // Create a command queue
+  cl_command_queue command_queue;
+  command_queue = clCreateCommandQueue(	context,
+  device,
+  0,
+  &error);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	CRATE PROGRAM, COMPILE IT
+  //====================================================================================================100
+
+  // Load kernel source code from file
+  const char *source = load_kernel_source("./kernel/kernel_gpu_opencl.cl");
+  size_t sourceSize = strlen(source);
+
+  // Create the program
+  cl_program program = clCreateProgramWithSource(	context,
+  1,
+  &source,
+  &sourceSize,
+  &error);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // parameterized kernel dimension
+  char clOptions[110];
+  //  sprintf(clOptions,"-I../../src");
+  sprintf(clOptions,"-I.");
 #ifdef RD_WG_SIZE
-	sprintf(clOptions + strlen(clOptions), " -DRD_WG_SIZE=%d", RD_WG_SIZE);
+  sprintf(clOptions + strlen(clOptions), " -DRD_WG_SIZE=%d", RD_WG_SIZE);
 #endif
 #ifdef RD_WG_SIZE_0
-	sprintf(clOptions + strlen(clOptions), " -DRD_WG_SIZE_0=%d", RD_WG_SIZE_0);
+  sprintf(clOptions + strlen(clOptions), " -DRD_WG_SIZE_0=%d", RD_WG_SIZE_0);
 #endif
 #ifdef RD_WG_SIZE_0_0
-	sprintf(clOptions + strlen(clOptions), " -DRD_WG_SIZE_0_0=%d", RD_WG_SIZE_0_0);
+  sprintf(clOptions + strlen(clOptions), " -DRD_WG_SIZE_0_0=%d", RD_WG_SIZE_0_0);
 #endif
 
 
-	// Compile the program
-	error = clBuildProgram(	program, 
-							1, 
-							&device, 
-							clOptions, 
-							NULL, 
-							NULL);
-	// Print warnings and errors from compilation
-	static char log[65536]; 
-	memset(log, 0, sizeof(log));
-	clGetProgramBuildInfo(	program, 
-							device, 
-							CL_PROGRAM_BUILD_LOG, 
-							sizeof(log)-1, 
-							log, 
-							NULL);
-	if (strstr(log,"warning:") || strstr(log, "error:")) 
-		printf("<<<<\n%s\n>>>>\n", log);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// Create kernel
-	cl_kernel kernel;
-	kernel = clCreateKernel(program, 
-							"kernel_gpu_opencl", 
-							&error);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	//	INITIAL DRIVER OVERHEAD
-	//====================================================================================================100
-
-	// cudaThreadSynchronize();
-
-	//====================================================================================================100
-	//	EXECUTION PARAMETERS
-	//====================================================================================================100
-
-	size_t local_work_size[1];
-	local_work_size[0] = NUMBER_THREADS;
-	size_t global_work_size[1];
-	global_work_size[0] = dim_cpu.number_boxes * local_work_size[0];
-
-	printf("# of blocks = %d, # of threads/block = %d (ensure that device can handle)\n", global_work_size[0]/local_work_size[0], local_work_size[0]);
-
-	time1 = get_time();
-
-	//======================================================================================================================================================150
-	//	GPU MEMORY				(MALLOC)
-	//======================================================================================================================================================150
-
-	//====================================================================================================100
-	//	GPU MEMORY				COPY IN
-	//====================================================================================================100
-
-	//==================================================50
-	//	boxes
-	//==================================================50
-
-	cl_mem d_box_gpu;
-	d_box_gpu = clCreateBuffer(	context, 
-								CL_MEM_READ_WRITE, 
-								dim_cpu.box_mem, 
-								NULL, 
-								&error );
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//==================================================50
-	//	rv
-	//==================================================50
-
-	cl_mem d_rv_gpu;
-	d_rv_gpu = clCreateBuffer(	context, 
-								CL_MEM_READ_WRITE, 
-								dim_cpu.space_mem, 
-								NULL, 
-								&error );
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//==================================================50
-	//	qv
-	//==================================================50
-
-	cl_mem d_qv_gpu;
-	d_qv_gpu = clCreateBuffer(	context, 
-								CL_MEM_READ_WRITE, 
-								dim_cpu.space_mem2, 
-								NULL, 
-								&error );
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	//	GPU MEMORY				COPY (IN & OUT)
-	//====================================================================================================100
-
-	//==================================================50
-	//	fv
-	//==================================================50
-
-	cl_mem d_fv_gpu;
-	d_fv_gpu = clCreateBuffer(	context, 
-								CL_MEM_READ_WRITE, 
-								dim_cpu.space_mem, 
-								NULL, 
-								&error );
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	time2 = get_time();
-
-	//======================================================================================================================================================150
-	//	GPU MEMORY				COPY IN
-	//======================================================================================================================================================150
-
-	//====================================================================================================100
-	//	GPU MEMORY				COPY IN
-	//====================================================================================================100
-
-	//==================================================50
-	//	boxes
-	//==================================================50
-
-	error = clEnqueueWriteBuffer(	command_queue,			// command queue
-									d_box_gpu,				// destination
-									1,						// block the source from access until this copy operation complates (1=yes, 0=no)
-									0,						// offset in destination to write to
-									dim_cpu.box_mem,		// size to be copied
-									box_cpu,				// source
-									0,						// # of events in the list of events to wait for
-									NULL,					// list of events to wait for
-									NULL);					// ID of this operation to be used by waiting operations
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//==================================================50
-	//	rv
-	//==================================================50
-
-	error = clEnqueueWriteBuffer(	command_queue, 
-									d_rv_gpu, 
-									1, 
-									0, 
-									dim_cpu.space_mem, 
-									rv_cpu, 
-									0, 
-									0, 
-									0);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//==================================================50
-	//	qv
-	//==================================================50
-
-	error = clEnqueueWriteBuffer(	command_queue, 
-									d_qv_gpu, 
-									1, 
-									0, 
-									dim_cpu.space_mem2, 
-									qv_cpu, 
-									0, 
-									0, 
-									0);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	//	GPU MEMORY				COPY (IN & OUT)
-	//====================================================================================================100
-
-	//==================================================50
-	//	fv
-	//==================================================50
-
-	error = clEnqueueWriteBuffer(	command_queue, 
-									d_fv_gpu, 
-									1,
-									0, 
-									dim_cpu.space_mem, 
-									fv_cpu, 
-									0, 
-									0, 
-									0);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	time3 = get_time();
-
-	//======================================================================================================================================================150
-	//	KERNEL
-	//======================================================================================================================================================150
-
-	// ???
-	clSetKernelArg(	kernel, 
-					0, 
-					sizeof(par_str), 
-					(void *) &par_cpu);
-	clSetKernelArg(	kernel, 
-					1, 
-					sizeof(dim_str), 
-					(void *) &dim_cpu);
-	clSetKernelArg(	kernel, 
-					2, 
-					sizeof(cl_mem), 
-					(void *) &d_box_gpu);
-	clSetKernelArg(	kernel, 
-					3, 
-					sizeof(cl_mem), 
-					(void *) &d_rv_gpu);
-	clSetKernelArg(	kernel, 
-					4, 
-					sizeof(cl_mem), 
-					(void *) &d_qv_gpu);
-	clSetKernelArg(	kernel, 
-					5, 
-					sizeof(cl_mem), 
-					(void *) &d_fv_gpu);
-
-	// launch kernel - all boxes
-	error = clEnqueueNDRangeKernel(	command_queue, 
-									kernel, 
-									1, 
-									NULL, 
-									global_work_size, 
-									local_work_size, 
-									0, 
-									NULL, 
-									NULL);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// Wait for all operations to finish NOT SURE WHERE THIS SHOULD GO
-	error = clFinish(command_queue);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	time4 = get_time();
-
-	//======================================================================================================================================================150
-	//	GPU MEMORY				COPY OUT
-	//======================================================================================================================================================150
-
-	//====================================================================================================100
-	//	GPU MEMORY				COPY (IN & OUT)
-	//====================================================================================================100
-
-	//==================================================50
-	//	fv
-	//==================================================50
-
-	error = clEnqueueReadBuffer(command_queue,               // The command queue.
-								d_fv_gpu,                         // The image on the device.
-								CL_TRUE,                     // Blocking? (ie. Wait at this line until read has finished?)
-								0,                           // Offset. None in this case.
-								dim_cpu.space_mem, 			// Size to copy.
-								fv_cpu,                       // The pointer to the image on the host.
-								0,                           // Number of events in wait list. Not used.
-								NULL,                        // Event wait list. Not used.
-								NULL);                       // Event object for determining status. Not used.
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// (enable for testing purposes only - prints some range of output, make sure not to initialize input in main.c with random numbers for comparison across runs)
-	// int g;
-	// int offset = 395;
-	// for(g=0; g<10; g++){
-		// printf("%f, %f, %f, %f\n", fv_cpu[offset+g].v, fv_cpu[offset+g].x, fv_cpu[offset+g].y, fv_cpu[offset+g].z);
-	// }
-
-	time5 = get_time();
-
-	//======================================================================================================================================================150
-	//	GPU MEMORY DEALLOCATION
-	//======================================================================================================================================================150
-
-	// Release kernels...
-	clReleaseKernel(kernel);
-
-	// Now the program...
-	clReleaseProgram(program);
-
-	// Clean up the device memory...
-	clReleaseMemObject(d_rv_gpu);
-	clReleaseMemObject(d_qv_gpu);
-	clReleaseMemObject(d_fv_gpu);
-	clReleaseMemObject(d_box_gpu);
-
-	// Flush the queue
-	error = clFlush(command_queue);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// ...and finally, the queue and context.
-	clReleaseCommandQueue(command_queue);
-
-	// ???
-	clReleaseContext(context);
-
-	time6 = get_time();
-
-	//======================================================================================================================================================150
-	//	DISPLAY TIMING
-	//======================================================================================================================================================150
-
-	printf("Time spent in different stages of GPU_CUDA KERNEL:\n");
-
-	printf("%15.12f s, %15.12f % : GPU: SET DEVICE / DRIVER INIT\n",	(float) (time1-time0) / 1000000, (float) (time1-time0) / (float) (time6-time0) * 100);
-	printf("%15.12f s, %15.12f % : GPU MEM: ALO\n", 					(float) (time2-time1) / 1000000, (float) (time2-time1) / (float) (time6-time0) * 100);
-	printf("%15.12f s, %15.12f % : GPU MEM: COPY IN\n",					(float) (time3-time2) / 1000000, (float) (time3-time2) / (float) (time6-time0) * 100);
-
-	printf("%15.12f s, %15.12f % : GPU: KERNEL\n",						(float) (time4-time3) / 1000000, (float) (time4-time3) / (float) (time6-time0) * 100);
-
-	printf("%15.12f s, %15.12f % : GPU MEM: COPY OUT\n",				(float) (time5-time4) / 1000000, (float) (time5-time4) / (float) (time6-time0) * 100);
-	printf("%15.12f s, %15.12f % : GPU MEM: FRE\n", 					(float) (time6-time5) / 1000000, (float) (time6-time5) / (float) (time6-time0) * 100);
-
-	printf("Total time:\n");
-	printf("%.12f s\n", 												(float) (time6-time0) / 1000000);
+  // Compile the program
+  error = clBuildProgram(	program,
+  1,
+  &device,
+  clOptions,
+  NULL,
+  NULL);
+  // Print warnings and errors from compilation
+  static char log[65536];
+  memset(log, 0, sizeof(log));
+  clGetProgramBuildInfo(	program,
+  device,
+  CL_PROGRAM_BUILD_LOG,
+  sizeof(log)-1,
+  log,
+  NULL);
+  if (strstr(log,"warning:") || strstr(log, "error:"))
+  printf("<<<<\n%s\n>>>>\n", log);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // Create kernel
+  cl_kernel kernel;
+  kernel = clCreateKernel(program,
+  "kernel_gpu_opencl",
+  &error);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	INITIAL DRIVER OVERHEAD
+  //====================================================================================================100
+
+  // cudaThreadSynchronize();
+
+  //====================================================================================================100
+  //	EXECUTION PARAMETERS
+  //====================================================================================================100
+
+  size_t local_work_size[1];
+  local_work_size[0] = NUMBER_THREADS;
+  size_t global_work_size[1];
+  global_work_size[0] = dim_cpu.number_boxes * local_work_size[0];
+
+  printf("# of blocks = %d, # of threads/block = %d (ensure that device can handle)\n", global_work_size[0]/local_work_size[0], local_work_size[0]);
+
+  time1 = get_time();
+
+  //======================================================================================================================================================150
+  //	GPU MEMORY				(MALLOC)
+  //======================================================================================================================================================150
+
+  //====================================================================================================100
+  //	GPU MEMORY				COPY IN
+  //====================================================================================================100
+
+  //==================================================50
+  //	boxes
+  //==================================================50
+
+  cl_mem d_box_gpu;
+  d_box_gpu = clCreateBuffer(	context,
+  CL_MEM_READ_WRITE,
+  dim_cpu.box_mem,
+  NULL,
+  &error );
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //==================================================50
+  //	rv
+  //==================================================50
+
+  cl_mem d_rv_gpu;
+  d_rv_gpu = clCreateBuffer(	context,
+  CL_MEM_READ_WRITE,
+  dim_cpu.space_mem,
+  NULL,
+  &error );
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //==================================================50
+  //	qv
+  //==================================================50
+
+  cl_mem d_qv_gpu;
+  d_qv_gpu = clCreateBuffer(	context,
+  CL_MEM_READ_WRITE,
+  dim_cpu.space_mem2,
+  NULL,
+  &error );
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	GPU MEMORY				COPY (IN & OUT)
+  //====================================================================================================100
+
+  //==================================================50
+  //	fv
+  //==================================================50
+
+  cl_mem d_fv_gpu;
+  d_fv_gpu = clCreateBuffer(	context,
+  CL_MEM_READ_WRITE,
+  dim_cpu.space_mem,
+  NULL,
+  &error );
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  time2 = get_time();
+
+  //======================================================================================================================================================150
+  //	GPU MEMORY				COPY IN
+  //======================================================================================================================================================150
+
+  //====================================================================================================100
+  //	GPU MEMORY				COPY IN
+  //====================================================================================================100
+
+  //==================================================50
+  //	boxes
+  //==================================================50
+
+  error = clEnqueueWriteBuffer(	command_queue,			// command queue
+  d_box_gpu,				// destination
+  1,						// block the source from access until this copy operation complates (1=yes, 0=no)
+  0,						// offset in destination to write to
+  dim_cpu.box_mem,		// size to be copied
+  box_cpu,				// source
+  0,						// # of events in the list of events to wait for
+  NULL,					// list of events to wait for
+  NULL);					// ID of this operation to be used by waiting operations
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //==================================================50
+  //	rv
+  //==================================================50
+
+  error = clEnqueueWriteBuffer(	command_queue,
+  d_rv_gpu,
+  1,
+  0,
+  dim_cpu.space_mem,
+  rv_cpu,
+  0,
+  0,
+  0);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //==================================================50
+  //	qv
+  //==================================================50
+
+  error = clEnqueueWriteBuffer(	command_queue,
+  d_qv_gpu,
+  1,
+  0,
+  dim_cpu.space_mem2,
+  qv_cpu,
+  0,
+  0,
+  0);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	GPU MEMORY				COPY (IN & OUT)
+  //====================================================================================================100
+
+  //==================================================50
+  //	fv
+  //==================================================50
+
+  error = clEnqueueWriteBuffer(	command_queue,
+  d_fv_gpu,
+  1,
+  0,
+  dim_cpu.space_mem,
+  fv_cpu,
+  0,
+  0,
+  0);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  time3 = get_time();
+
+  //======================================================================================================================================================150
+  //	KERNEL
+  //======================================================================================================================================================150
+
+  // ???
+  clSetKernelArg(	kernel,
+  0,
+  sizeof(par_str),
+  (void *) &par_cpu);
+  clSetKernelArg(	kernel,
+  1,
+  sizeof(dim_str),
+  (void *) &dim_cpu);
+  clSetKernelArg(	kernel,
+  2,
+  sizeof(cl_mem),
+  (void *) &d_box_gpu);
+  clSetKernelArg(	kernel,
+  3,
+  sizeof(cl_mem),
+  (void *) &d_rv_gpu);
+  clSetKernelArg(	kernel,
+  4,
+  sizeof(cl_mem),
+  (void *) &d_qv_gpu);
+  clSetKernelArg(	kernel,
+  5,
+  sizeof(cl_mem),
+  (void *) &d_fv_gpu);
+
+  // launch kernel - all boxes
+  error = clEnqueueNDRangeKernel(	command_queue,
+  kernel,
+  1,
+  NULL,
+  global_work_size,
+  local_work_size,
+  0,
+  NULL,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // Wait for all operations to finish NOT SURE WHERE THIS SHOULD GO
+  error = clFinish(command_queue);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  time4 = get_time();
+
+  //======================================================================================================================================================150
+  //	GPU MEMORY				COPY OUT
+  //======================================================================================================================================================150
+
+  //====================================================================================================100
+  //	GPU MEMORY				COPY (IN & OUT)
+  //====================================================================================================100
+
+  //==================================================50
+  //	fv
+  //==================================================50
+
+  error = clEnqueueReadBuffer(command_queue,               // The command queue.
+  d_fv_gpu,                         // The image on the device.
+  CL_TRUE,                     // Blocking? (ie. Wait at this line until read has finished?)
+  0,                           // Offset. None in this case.
+  dim_cpu.space_mem, 			// Size to copy.
+  fv_cpu,                       // The pointer to the image on the host.
+  0,                           // Number of events in wait list. Not used.
+  NULL,                        // Event wait list. Not used.
+  NULL);                       // Event object for determining status. Not used.
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // (enable for testing purposes only - prints some range of output, make sure not to initialize input in main.c with random numbers for comparison across runs)
+  // int g;
+  // int offset = 395;
+  // for(g=0; g<10; g++){
+  // printf("%f, %f, %f, %f\n", fv_cpu[offset+g].v, fv_cpu[offset+g].x, fv_cpu[offset+g].y, fv_cpu[offset+g].z);
+  // }
+
+  time5 = get_time();
+
+  //======================================================================================================================================================150
+  //	GPU MEMORY DEALLOCATION
+  //======================================================================================================================================================150
+
+  // Release kernels...
+  clReleaseKernel(kernel);
+
+  // Now the program...
+  clReleaseProgram(program);
+
+  // Clean up the device memory...
+  clReleaseMemObject(d_rv_gpu);
+  clReleaseMemObject(d_qv_gpu);
+  clReleaseMemObject(d_fv_gpu);
+  clReleaseMemObject(d_box_gpu);
+
+  // Flush the queue
+  error = clFlush(command_queue);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // ...and finally, the queue and context.
+  clReleaseCommandQueue(command_queue);
+
+  // ???
+  clReleaseContext(context);
+
+  time6 = get_time();
+
+  //======================================================================================================================================================150
+  //	DISPLAY TIMING
+  //======================================================================================================================================================150
+
+  printf("Time spent in different stages of GPU_CUDA KERNEL:\n");
+
+  printf("%15.12f s, %15.12f % : GPU: SET DEVICE / DRIVER INIT\n",	(float) (time1-time0) / 1000000, (float) (time1-time0) / (float) (time6-time0) * 100);
+  printf("%15.12f s, %15.12f % : GPU MEM: ALO\n", 					(float) (time2-time1) / 1000000, (float) (time2-time1) / (float) (time6-time0) * 100);
+  printf("%15.12f s, %15.12f % : GPU MEM: COPY IN\n",					(float) (time3-time2) / 1000000, (float) (time3-time2) / (float) (time6-time0) * 100);
+
+  printf("%15.12f s, %15.12f % : GPU: KERNEL\n",						(float) (time4-time3) / 1000000, (float) (time4-time3) / (float) (time6-time0) * 100);
+
+  printf("%15.12f s, %15.12f % : GPU MEM: COPY OUT\n",				(float) (time5-time4) / 1000000, (float) (time5-time4) / (float) (time6-time0) * 100);
+  printf("%15.12f s, %15.12f % : GPU MEM: FRE\n", 					(float) (time6-time5) / 1000000, (float) (time6-time5) / (float) (time6-time0) * 100);
+
+  printf("Total time:\n");
+  printf("%.12f s\n", 												(float) (time6-time0) / 1000000);
 
 }
 

+ 6 - 6
opencl/lavaMD/kernel/kernel_gpu_opencl_wrapper.h

@@ -6,13 +6,13 @@ extern "C" {
 //	KERNEL_GPU_OPENCL_WRAPPER HEADER
 //========================================================================================================================================================================================================200
 
-void 
+void
 kernel_gpu_opencl_wrapper(par_str parms_cpu,
-							            dim_str dim_cpu,
-							            box_str* box_cpu,
-							            FOUR_VECTOR* rv_cpu,
-							            fp* qv_cpu,
-							            FOUR_VECTOR* fv_cpu,
+              dim_str dim_cpu,
+              box_str* box_cpu,
+              FOUR_VECTOR* rv_cpu,
+              fp* qv_cpu,
+              FOUR_VECTOR* fv_cpu,
                           int platform_id,
                           int device_id,
                           int use_gpu);

+ 265 - 265
opencl/lavaMD/main.c

@@ -65,7 +65,7 @@ extern "C" {
 void usage(char *argv0) {
     char *help =
     "\nUsage: %s [switches] \n\n"
-		"    -b               :number of boxes     [default=0]\n"
+  "    -b               :number of boxes     [default=0]\n"
     "    -p platform_id   :OCL platform to use [default=0]\n"
     "    -d device_id     :OCL device to use   [default=0]\n"
     "    -g use_gpu       :1 for GPU 0 for CPU [default=0]\n";
@@ -77,98 +77,98 @@ void usage(char *argv0) {
 //	MAIN FUNCTION
 //========================================================================================================================================================================================================200
 
-int main(	int argc, 
-		char *argv [])
+int main(	int argc,
+  char *argv [])
 {
 
-	//======================================================================================================================================================150
-	//	CPU/MCPU VARIABLES
-	//======================================================================================================================================================150
-
-	// timer
-	long long time0;
-
-	time0 = get_time();
-
-	// timer
-	long long time1;
-	long long time2;
-	long long time3;
-	long long time4;
-	long long time5;
-	long long time6;
-	long long time7;
-
-	// counters
-	int i, j, k, l, m, n;
-
-	// system memory
-	par_str par_cpu;
-	dim_str dim_cpu;
-	box_str* box_cpu;
-	FOUR_VECTOR* rv_cpu;
-	fp* qv_cpu;
-	FOUR_VECTOR* fv_cpu;
-	int nh;
-
-
-	printf("WG size of kernel = %d \n", NUMBER_THREADS);
-
-	time1 = get_time();
-
-	//======================================================================================================================================================150
-	//	CHECK INPUT ARGUMENTS
-	//======================================================================================================================================================150
-
-	// assing default values
-	dim_cpu.arch_arg = 0;
-	dim_cpu.cores_arg = 1;
-	dim_cpu.boxes1d_arg = 1;
-
-	/*// go through arguments
-	if(argc>=3){
-		for(dim_cpu.cur_arg=1; dim_cpu.cur_arg<argc; dim_cpu.cur_arg++){
-			// check if -boxes1d
-			if(strcmp(argv[dim_cpu.cur_arg], "-boxes1d")==0){
-				// check if value provided
-				if(argc>=dim_cpu.cur_arg+1){
-					// check if value is a number
-					if(isInteger(argv[dim_cpu.cur_arg+1])==1){
-						dim_cpu.boxes1d_arg = atoi(argv[dim_cpu.cur_arg+1]);
-						if(dim_cpu.boxes1d_arg<0){
-							printf("ERROR: Wrong value to -boxes1d argument, cannot be <=0\n");
-							return 0;
-						}
-						dim_cpu.cur_arg = dim_cpu.cur_arg+1;
-					}
-					// value is not a number
-					else{
-						printf("ERROR: Value to -boxes1d argument in not a number\n");
-						return 0;
-					}
-				}
-				// value not provided
-				else{
-					printf("ERROR: Missing value to -boxes1d argument\n");
-					return 0;
-				}
-			}
-			// unknown
-			else{
-				printf("ERROR: Unknown argument\n");
-				return 0;
-			}
-		}
-		// Print configuration
-		printf("Configuration used: arch = %d, cores = %d, boxes1d = %d\n", dim_cpu.arch_arg, dim_cpu.cores_arg, dim_cpu.boxes1d_arg);
-	}
-	else{
-		printf("Provide boxes1d argument, example: -boxes1d 16");
-		return 0;
-	}*/
-  
+  //======================================================================================================================================================150
+  //	CPU/MCPU VARIABLES
+  //======================================================================================================================================================150
+
+  // timer
+  long long time0;
+
+  time0 = get_time();
+
+  // timer
+  long long time1;
+  long long time2;
+  long long time3;
+  long long time4;
+  long long time5;
+  long long time6;
+  long long time7;
+
+  // counters
+  int i, j, k, l, m, n;
+
+  // system memory
+  par_str par_cpu;
+  dim_str dim_cpu;
+  box_str* box_cpu;
+  FOUR_VECTOR* rv_cpu;
+  fp* qv_cpu;
+  FOUR_VECTOR* fv_cpu;
+  int nh;
+
+
+  printf("WG size of kernel = %d \n", NUMBER_THREADS);
+
+  time1 = get_time();
+
+  //======================================================================================================================================================150
+  //	CHECK INPUT ARGUMENTS
+  //======================================================================================================================================================150
+
+  // assing default values
+  dim_cpu.arch_arg = 0;
+  dim_cpu.cores_arg = 1;
+  dim_cpu.boxes1d_arg = 1;
+
+  /*// go through arguments
+  if(argc>=3){
+  for(dim_cpu.cur_arg=1; dim_cpu.cur_arg<argc; dim_cpu.cur_arg++){
+  // check if -boxes1d
+  if(strcmp(argv[dim_cpu.cur_arg], "-boxes1d")==0){
+  // check if value provided
+  if(argc>=dim_cpu.cur_arg+1){
+  // check if value is a number
+  if(isInteger(argv[dim_cpu.cur_arg+1])==1){
+  dim_cpu.boxes1d_arg = atoi(argv[dim_cpu.cur_arg+1]);
+  if(dim_cpu.boxes1d_arg<0){
+  printf("ERROR: Wrong value to -boxes1d argument, cannot be <=0\n");
+  return 0;
+  }
+  dim_cpu.cur_arg = dim_cpu.cur_arg+1;
+  }
+  // value is not a number
+  else{
+  printf("ERROR: Value to -boxes1d argument in not a number\n");
+  return 0;
+  }
+  }
+  // value not provided
+  else{
+  printf("ERROR: Missing value to -boxes1d argument\n");
+  return 0;
+  }
+  }
+  // unknown
+  else{
+  printf("ERROR: Unknown argument\n");
+  return 0;
+  }
+  }
+  // Print configuration
+  printf("Configuration used: arch = %d, cores = %d, boxes1d = %d\n", dim_cpu.arch_arg, dim_cpu.cores_arg, dim_cpu.boxes1d_arg);
+  }
+  else{
+  printf("Provide boxes1d argument, example: -boxes1d 16");
+  return 0;
+  }*/
+
   // Arguments parsing for platform, device and type
-  
+
   // Variables to store information on platform and device to use_gpu
   int platform_id = 0;
   int device_id = 0;
@@ -184,9 +184,9 @@ int main(	int argc,
           case 'd': device_id = atoi(optarg);
                     break;
           case 'g': use_gpu = atoi(optarg);
-                    break;      
+                    break;
           case 'b': dim_cpu.boxes1d_arg = atoi(optarg);
-                    break;                                                        
+                    break;
           case '?': usage(argv[0]);
                     break;
           default: usage(argv[0]);
@@ -194,204 +194,204 @@ int main(	int argc,
       }
   }
 
-	time2 = get_time();
+  time2 = get_time();
+
+  //======================================================================================================================================================150
+  //	INPUTS
+  //======================================================================================================================================================150
+
+  par_cpu.alpha = 0.5;
+
+  time3 = get_time();
+
+  //======================================================================================================================================================150
+  //	DIMENSIONS
+  //======================================================================================================================================================150
+
+  // total number of boxes
+  dim_cpu.number_boxes = dim_cpu.boxes1d_arg * dim_cpu.boxes1d_arg * dim_cpu.boxes1d_arg; // 8*8*8=512
+
+  // how many particles space has in each direction
+  dim_cpu.space_elem = dim_cpu.number_boxes * NUMBER_PAR_PER_BOX;							//512*100=51,200
+  dim_cpu.space_mem = dim_cpu.space_elem * sizeof(FOUR_VECTOR);
+  dim_cpu.space_mem2 = dim_cpu.space_elem * sizeof(fp);
+
+  // box array
+  dim_cpu.box_mem = dim_cpu.number_boxes * sizeof(box_str);
+
+  time4 = get_time();
 
-	//======================================================================================================================================================150
-	//	INPUTS
-	//======================================================================================================================================================150
+  //======================================================================================================================================================150
+  //	SYSTEM MEMORY
+  //======================================================================================================================================================150
 
-	par_cpu.alpha = 0.5;
+  //====================================================================================================100
+  //	BOX
+  //====================================================================================================100
 
-	time3 = get_time();
+  // allocate boxes
+  box_cpu = (box_str*)malloc(dim_cpu.box_mem);
 
-	//======================================================================================================================================================150
-	//	DIMENSIONS
-	//======================================================================================================================================================150
+  // initialize number of home boxes
+  nh = 0;
 
-	// total number of boxes
-	dim_cpu.number_boxes = dim_cpu.boxes1d_arg * dim_cpu.boxes1d_arg * dim_cpu.boxes1d_arg; // 8*8*8=512
-
-	// how many particles space has in each direction
-	dim_cpu.space_elem = dim_cpu.number_boxes * NUMBER_PAR_PER_BOX;							//512*100=51,200
-	dim_cpu.space_mem = dim_cpu.space_elem * sizeof(FOUR_VECTOR);
-	dim_cpu.space_mem2 = dim_cpu.space_elem * sizeof(fp);
-
-	// box array
-	dim_cpu.box_mem = dim_cpu.number_boxes * sizeof(box_str);
-
-	time4 = get_time();
-
-	//======================================================================================================================================================150
-	//	SYSTEM MEMORY
-	//======================================================================================================================================================150
-
-	//====================================================================================================100
-	//	BOX
-	//====================================================================================================100
-
-	// allocate boxes
-	box_cpu = (box_str*)malloc(dim_cpu.box_mem);
-
-	// initialize number of home boxes
-	nh = 0;
-
-	// home boxes in z direction
-	for(i=0; i<dim_cpu.boxes1d_arg; i++){
-		// home boxes in y direction
-		for(j=0; j<dim_cpu.boxes1d_arg; j++){
-			// home boxes in x direction
-			for(k=0; k<dim_cpu.boxes1d_arg; k++){
-
-				// current home box
-				box_cpu[nh].x = k;
-				box_cpu[nh].y = j;
-				box_cpu[nh].z = i;
-				box_cpu[nh].number = nh;
-				box_cpu[nh].offset = nh * NUMBER_PAR_PER_BOX;
-
-				// initialize number of neighbor boxes
-				box_cpu[nh].nn = 0;
-
-				// neighbor boxes in z direction
-				for(l=-1; l<2; l++){
-					// neighbor boxes in y direction
-					for(m=-1; m<2; m++){
-						// neighbor boxes in x direction
-						for(n=-1; n<2; n++){
-
-							// check if (this neighbor exists) and (it is not the same as home box)
-							if(		(((i+l)>=0 && (j+m)>=0 && (k+n)>=0)==true && ((i+l)<dim_cpu.boxes1d_arg && (j+m)<dim_cpu.boxes1d_arg && (k+n)<dim_cpu.boxes1d_arg)==true)	&&
-									(l==0 && m==0 && n==0)==false	){
-
-								// current neighbor box
-								box_cpu[nh].nei[box_cpu[nh].nn].x = (k+n);
-								box_cpu[nh].nei[box_cpu[nh].nn].y = (j+m);
-								box_cpu[nh].nei[box_cpu[nh].nn].z = (i+l);
-								box_cpu[nh].nei[box_cpu[nh].nn].number =	(box_cpu[nh].nei[box_cpu[nh].nn].z * dim_cpu.boxes1d_arg * dim_cpu.boxes1d_arg) + 
-																			(box_cpu[nh].nei[box_cpu[nh].nn].y * dim_cpu.boxes1d_arg) + 
-																			 box_cpu[nh].nei[box_cpu[nh].nn].x;
-								box_cpu[nh].nei[box_cpu[nh].nn].offset = box_cpu[nh].nei[box_cpu[nh].nn].number * NUMBER_PAR_PER_BOX;
-
-								// increment neighbor box
-								box_cpu[nh].nn = box_cpu[nh].nn + 1;
-
-							}
-
-						} // neighbor boxes in x direction
-					} // neighbor boxes in y direction
-				} // neighbor boxes in z direction
-
-				// increment home box
-				nh = nh + 1;
-
-			} // home boxes in x direction
-		} // home boxes in y direction
-	} // home boxes in z direction
-
-	//====================================================================================================100
-	//	PARAMETERS, DISTANCE, CHARGE AND FORCE
-	//====================================================================================================100
-
-	// random generator seed set to random value - time in this case
-	srand(time(NULL));
-
-	// input (distances)
-	rv_cpu = (FOUR_VECTOR*)malloc(dim_cpu.space_mem);
-	for(i=0; i<dim_cpu.space_elem; i=i+1){
-		rv_cpu[i].v = (rand()%10 + 1) / 10.0;			// get a number in the range 0.1 - 1.0
-		// rv_cpu[i].v = 0.1;			// get a number in the range 0.1 - 1.0
-		rv_cpu[i].x = (rand()%10 + 1) / 10.0;			// get a number in the range 0.1 - 1.0
-		// rv_cpu[i].x = 0.2;			// get a number in the range 0.1 - 1.0
-		rv_cpu[i].y = (rand()%10 + 1) / 10.0;			// get a number in the range 0.1 - 1.0
-		// rv_cpu[i].y = 0.3;			// get a number in the range 0.1 - 1.0
-		rv_cpu[i].z = (rand()%10 + 1) / 10.0;			// get a number in the range 0.1 - 1.0
-		// rv_cpu[i].z = 0.4;			// get a number in the range 0.1 - 1.0
-	}
-
-	// input (charge)
-	qv_cpu = (fp*)malloc(dim_cpu.space_mem2);
-	for(i=0; i<dim_cpu.space_elem; i=i+1){
-		qv_cpu[i] = (rand()%10 + 1) / 10.0;			// get a number in the range 0.1 - 1.0
-		// qv_cpu[i] = 0.5;			// get a number in the range 0.1 - 1.0
-	}
-
-	// output (forces)
-	fv_cpu = (FOUR_VECTOR*)malloc(dim_cpu.space_mem);
-	for(i=0; i<dim_cpu.space_elem; i=i+1){
-		fv_cpu[i].v = 0;								// set to 0, because kernels keeps adding to initial value
-		fv_cpu[i].x = 0;								// set to 0, because kernels keeps adding to initial value
-		fv_cpu[i].y = 0;								// set to 0, because kernels keeps adding to initial value
-		fv_cpu[i].z = 0;								// set to 0, because kernels keeps adding to initial value
-	}
-
-	time5 = get_time();
-
-	//======================================================================================================================================================150
-	//	KERNEL
-	//======================================================================================================================================================150
-
-	//====================================================================================================100
-	//	GPU_OPENCL
-	//====================================================================================================100
-
-	kernel_gpu_opencl_wrapper(par_cpu,
-								            dim_cpu,
-								            box_cpu,
-								            rv_cpu,
-								            qv_cpu,
-								            fv_cpu,
+  // home boxes in z direction
+  for(i=0; i<dim_cpu.boxes1d_arg; i++){
+  // home boxes in y direction
+  for(j=0; j<dim_cpu.boxes1d_arg; j++){
+  // home boxes in x direction
+  for(k=0; k<dim_cpu.boxes1d_arg; k++){
+
+  // current home box
+  box_cpu[nh].x = k;
+  box_cpu[nh].y = j;
+  box_cpu[nh].z = i;
+  box_cpu[nh].number = nh;
+  box_cpu[nh].offset = nh * NUMBER_PAR_PER_BOX;
+
+  // initialize number of neighbor boxes
+  box_cpu[nh].nn = 0;
+
+  // neighbor boxes in z direction
+  for(l=-1; l<2; l++){
+  // neighbor boxes in y direction
+  for(m=-1; m<2; m++){
+  // neighbor boxes in x direction
+  for(n=-1; n<2; n++){
+
+  // check if (this neighbor exists) and (it is not the same as home box)
+  if(		(((i+l)>=0 && (j+m)>=0 && (k+n)>=0)==true && ((i+l)<dim_cpu.boxes1d_arg && (j+m)<dim_cpu.boxes1d_arg && (k+n)<dim_cpu.boxes1d_arg)==true)	&&
+  (l==0 && m==0 && n==0)==false	){
+
+  // current neighbor box
+  box_cpu[nh].nei[box_cpu[nh].nn].x = (k+n);
+  box_cpu[nh].nei[box_cpu[nh].nn].y = (j+m);
+  box_cpu[nh].nei[box_cpu[nh].nn].z = (i+l);
+  box_cpu[nh].nei[box_cpu[nh].nn].number =	(box_cpu[nh].nei[box_cpu[nh].nn].z * dim_cpu.boxes1d_arg * dim_cpu.boxes1d_arg) +
+  (box_cpu[nh].nei[box_cpu[nh].nn].y * dim_cpu.boxes1d_arg) +
+   box_cpu[nh].nei[box_cpu[nh].nn].x;
+  box_cpu[nh].nei[box_cpu[nh].nn].offset = box_cpu[nh].nei[box_cpu[nh].nn].number * NUMBER_PAR_PER_BOX;
+
+  // increment neighbor box
+  box_cpu[nh].nn = box_cpu[nh].nn + 1;
+
+  }
+
+  } // neighbor boxes in x direction
+  } // neighbor boxes in y direction
+  } // neighbor boxes in z direction
+
+  // increment home box
+  nh = nh + 1;
+
+  } // home boxes in x direction
+  } // home boxes in y direction
+  } // home boxes in z direction
+
+  //====================================================================================================100
+  //	PARAMETERS, DISTANCE, CHARGE AND FORCE
+  //====================================================================================================100
+
+  // random generator seed set to random value - time in this case
+  srand(time(NULL));
+
+  // input (distances)
+  rv_cpu = (FOUR_VECTOR*)malloc(dim_cpu.space_mem);
+  for(i=0; i<dim_cpu.space_elem; i=i+1){
+  rv_cpu[i].v = (rand()%10 + 1) / 10.0;			// get a number in the range 0.1 - 1.0
+  // rv_cpu[i].v = 0.1;			// get a number in the range 0.1 - 1.0
+  rv_cpu[i].x = (rand()%10 + 1) / 10.0;			// get a number in the range 0.1 - 1.0
+  // rv_cpu[i].x = 0.2;			// get a number in the range 0.1 - 1.0
+  rv_cpu[i].y = (rand()%10 + 1) / 10.0;			// get a number in the range 0.1 - 1.0
+  // rv_cpu[i].y = 0.3;			// get a number in the range 0.1 - 1.0
+  rv_cpu[i].z = (rand()%10 + 1) / 10.0;			// get a number in the range 0.1 - 1.0
+  // rv_cpu[i].z = 0.4;			// get a number in the range 0.1 - 1.0
+  }
+
+  // input (charge)
+  qv_cpu = (fp*)malloc(dim_cpu.space_mem2);
+  for(i=0; i<dim_cpu.space_elem; i=i+1){
+  qv_cpu[i] = (rand()%10 + 1) / 10.0;			// get a number in the range 0.1 - 1.0
+  // qv_cpu[i] = 0.5;			// get a number in the range 0.1 - 1.0
+  }
+
+  // output (forces)
+  fv_cpu = (FOUR_VECTOR*)malloc(dim_cpu.space_mem);
+  for(i=0; i<dim_cpu.space_elem; i=i+1){
+  fv_cpu[i].v = 0;								// set to 0, because kernels keeps adding to initial value
+  fv_cpu[i].x = 0;								// set to 0, because kernels keeps adding to initial value
+  fv_cpu[i].y = 0;								// set to 0, because kernels keeps adding to initial value
+  fv_cpu[i].z = 0;								// set to 0, because kernels keeps adding to initial value
+  }
+
+  time5 = get_time();
+
+  //======================================================================================================================================================150
+  //	KERNEL
+  //======================================================================================================================================================150
+
+  //====================================================================================================100
+  //	GPU_OPENCL
+  //====================================================================================================100
+
+  kernel_gpu_opencl_wrapper(par_cpu,
+              dim_cpu,
+              box_cpu,
+              rv_cpu,
+              qv_cpu,
+              fv_cpu,
                             platform_id,
                             device_id,
                             use_gpu);
 
-	time6 = get_time();
+  time6 = get_time();
 
-	//======================================================================================================================================================150
-	//	SYSTEM MEMORY DEALLOCATION
-	//======================================================================================================================================================150
+  //======================================================================================================================================================150
+  //	SYSTEM MEMORY DEALLOCATION
+  //======================================================================================================================================================150
 
-	// dump results
+  // dump results
 #ifdef OUTPUT
         FILE *fptr;
-	fptr = fopen("result.txt", "w");	
-	for(i=0; i<dim_cpu.space_elem; i=i+1){
+  fptr = fopen("result.txt", "w");
+  for(i=0; i<dim_cpu.space_elem; i=i+1){
         	fprintf(fptr, "%f, %f, %f, %f\n", fv_cpu[i].v, fv_cpu[i].x, fv_cpu[i].y, fv_cpu[i].z);
-	}
-	fclose(fptr);
-#endif       	
+  }
+  fclose(fptr);
+#endif
 
 
-	free(rv_cpu);
-	free(qv_cpu);
-	free(fv_cpu);
-	free(box_cpu);
+  free(rv_cpu);
+  free(qv_cpu);
+  free(fv_cpu);
+  free(box_cpu);
 
-	time7 = get_time();
+  time7 = get_time();
 
-	//======================================================================================================================================================150
-	//	DISPLAY TIMING
-	//======================================================================================================================================================150
+  //======================================================================================================================================================150
+  //	DISPLAY TIMING
+  //======================================================================================================================================================150
 
-	// printf("Time spent in different stages of the application:\n");
+  // printf("Time spent in different stages of the application:\n");
 
-	// printf("%15.12f s, %15.12f % : VARIABLES\n",						(float) (time1-time0) / 1000000, (float) (time1-time0) / (float) (time7-time0) * 100);
-	// printf("%15.12f s, %15.12f % : INPUT ARGUMENTS\n", 					(float) (time2-time1) / 1000000, (float) (time2-time1) / (float) (time7-time0) * 100);
-	// printf("%15.12f s, %15.12f % : INPUTS\n",							(float) (time3-time2) / 1000000, (float) (time3-time2) / (float) (time7-time0) * 100);
-	// printf("%15.12f s, %15.12f % : dim_cpu\n", 							(float) (time4-time3) / 1000000, (float) (time4-time3) / (float) (time7-time0) * 100);
-	// printf("%15.12f s, %15.12f % : SYS MEM: ALO\n",						(float) (time5-time4) / 1000000, (float) (time5-time4) / (float) (time7-time0) * 100);
+  // printf("%15.12f s, %15.12f % : VARIABLES\n",						(float) (time1-time0) / 1000000, (float) (time1-time0) / (float) (time7-time0) * 100);
+  // printf("%15.12f s, %15.12f % : INPUT ARGUMENTS\n", 					(float) (time2-time1) / 1000000, (float) (time2-time1) / (float) (time7-time0) * 100);
+  // printf("%15.12f s, %15.12f % : INPUTS\n",							(float) (time3-time2) / 1000000, (float) (time3-time2) / (float) (time7-time0) * 100);
+  // printf("%15.12f s, %15.12f % : dim_cpu\n", 							(float) (time4-time3) / 1000000, (float) (time4-time3) / (float) (time7-time0) * 100);
+  // printf("%15.12f s, %15.12f % : SYS MEM: ALO\n",						(float) (time5-time4) / 1000000, (float) (time5-time4) / (float) (time7-time0) * 100);
 
-	// printf("%15.12f s, %15.12f % : KERNEL: COMPUTE\n",					(float) (time6-time5) / 1000000, (float) (time6-time5) / (float) (time7-time0) * 100);
+  // printf("%15.12f s, %15.12f % : KERNEL: COMPUTE\n",					(float) (time6-time5) / 1000000, (float) (time6-time5) / (float) (time7-time0) * 100);
 
-	// printf("%15.12f s, %15.12f % : SYS MEM: FRE\n", 					(float) (time7-time6) / 1000000, (float) (time7-time6) / (float) (time7-time0) * 100);
+  // printf("%15.12f s, %15.12f % : SYS MEM: FRE\n", 					(float) (time7-time6) / 1000000, (float) (time7-time6) / (float) (time7-time0) * 100);
 
-	// printf("Total time:\n");
-	// printf("%.12f s\n", 												(float) (time7-time0) / 1000000);
+  // printf("Total time:\n");
+  // printf("%.12f s\n", 												(float) (time7-time0) / 1000000);
 
-	//======================================================================================================================================================150
-	//	RETURN
-	//======================================================================================================================================================150
+  //======================================================================================================================================================150
+  //	RETURN
+  //======================================================================================================================================================150
 
-	return 0.0;																					// always returns 0.0
+  return 0.0;																					// always returns 0.0
 
 }
 

+ 28 - 28
opencl/lavaMD/main.h

@@ -33,62 +33,62 @@
 
 typedef struct
 {
-	fp x, y, z;
+  fp x, y, z;
 
 } THREE_VECTOR;
 
 typedef struct
 {
-	fp v, x, y, z;
+  fp v, x, y, z;
 
 } FOUR_VECTOR;
 
 typedef struct nei_str
 {
 
-	// neighbor box
-	int x, y, z;
-	int number;
-	long long offset;
+  // neighbor box
+  int x, y, z;
+  int number;
+  long long offset;
 
 } nei_str;
 
 typedef struct box_str
 {
 
-	// home box
-	int x, y, z;
-	int number;
-	long long offset;
+  // home box
+  int x, y, z;
+  int number;
+  long long offset;
 
-	// neighbor boxes
-	int nn;
-	nei_str nei[26];
+  // neighbor boxes
+  int nn;
+  nei_str nei[26];
 
 } box_str;
 
 typedef struct par_str
 {
 
-	fp alpha;
+  fp alpha;
 
 } par_str;
 
 typedef struct dim_str
 {
 
-	// input arguments
-	int cur_arg;
-	int arch_arg;
-	int cores_arg;
-	int boxes1d_arg;
+  // input arguments
+  int cur_arg;
+  int arch_arg;
+  int cores_arg;
+  int boxes1d_arg;
 
-	// system memory
-	long long number_boxes;
-	long long box_mem;
-	long long space_elem;
-	long long space_mem;
-	long long space_mem2;
+  // system memory
+  long long number_boxes;
+  long long box_mem;
+  long long space_elem;
+  long long space_mem;
+  long long space_mem2;
 
 } dim_str;
 
@@ -96,9 +96,9 @@ typedef struct dim_str
 //	FUNCTION PROTOTYPES
 //===============================================================================================================================================================================================================200
 
-int 
-main(	int argc, 
-		char *argv []);
+int
+main(	int argc,
+  char *argv []);
 
 //===============================================================================================================================================================================================================200
 //	END

+ 92 - 92
opencl/leukocyte/OpenCL/OpenCL_helper_library.c

@@ -2,111 +2,111 @@
 
 // Loads the kernel source code from the specified file into a C string and returns the string pointer
 char *load_kernel_source(const char *filename) {
-	// Open the source file
-	FILE *file = fopen(filename, "r");
-	if (file == NULL) fatal("Error opening kernel source file\n");
-	
-	// Determine the size of the file
-	if (fseek(file, 0, SEEK_END)) fatal("Error reading kernel source file\n");
-	size_t size = ftell(file);
-	
-	// Allocate space for the source code (plus one for null-terminator)
-	char *source = (char *) malloc(size + 1);
-	
-	// Read the source code into the string
-	fseek(file, 0, SEEK_SET);
-	// printf("Number of elements: %lu\nSize = %lu", fread(source, 1, size, file), size);
-	// exit(1);
-	if (fread(source, 1, size, file) != size) fatal("Error reading kernel source file\n");
-	
-	// Null-terminate the string
-	source[size] = '\0';
-	
-	// Return the pointer to the string
-	return source;
+  // Open the source file
+  FILE *file = fopen(filename, "r");
+  if (file == NULL) fatal("Error opening kernel source file\n");
+
+  // Determine the size of the file
+  if (fseek(file, 0, SEEK_END)) fatal("Error reading kernel source file\n");
+  size_t size = ftell(file);
+
+  // Allocate space for the source code (plus one for null-terminator)
+  char *source = (char *) malloc(size + 1);
+
+  // Read the source code into the string
+  fseek(file, 0, SEEK_SET);
+  // printf("Number of elements: %lu\nSize = %lu", fread(source, 1, size, file), size);
+  // exit(1);
+  if (fread(source, 1, size, file) != size) fatal("Error reading kernel source file\n");
+
+  // Null-terminate the string
+  source[size] = '\0';
+
+  // Return the pointer to the string
+  return source;
 }
 
 
 // Returns the current system time in microseconds
 /*long long get_time() {
-	struct timeval tv;
-	gettimeofday(&tv, NULL);
-	return (tv.tv_sec * 1000000) + tv.tv_usec;
+  struct timeval tv;
+  gettimeofday(&tv, NULL);
+  return (tv.tv_sec * 1000000) + tv.tv_usec;
 }*/
 
 void fatal(const char *s) {
-	fprintf(stderr, "Error: %s\n", s);
-	exit(1);
+  fprintf(stderr, "Error: %s\n", s);
+  exit(1);
 }
 
 // Prints a string version of the specified OpenCL error code
 void fatal_CL(cl_int error, char *file, int line) {
-	printf("Error in %s at line %d: ", file, line);
-	
-	// Print 
-	switch(error) {
-		case CL_SUCCESS: 									printf("CL_SUCCESS\n"); break;
-		case CL_DEVICE_NOT_FOUND: 							printf("CL_DEVICE_NOT_FOUND\n"); break;
-		case CL_DEVICE_NOT_AVAILABLE: 						printf("CL_DEVICE_NOT_AVAILABLE\n"); break;
-		case CL_COMPILER_NOT_AVAILABLE: 					printf("CL_COMPILER_NOT_AVAILABLE\n"); break;
-		case CL_MEM_OBJECT_ALLOCATION_FAILURE: 				printf("CL_MEM_OBJECT_ALLOCATION_FAILURE\n"); break;
-		case CL_OUT_OF_RESOURCES: 							printf("CL_OUT_OF_RESOURCES\n"); break;
-		case CL_OUT_OF_HOST_MEMORY: 						printf("CL_OUT_OF_HOST_MEMORY\n"); break;
-		case CL_PROFILING_INFO_NOT_AVAILABLE: 				printf("CL_PROFILING_INFO_NOT_AVAILABLE\n"); break;
-		case CL_MEM_COPY_OVERLAP: 							printf("CL_MEM_COPY_OVERLAP\n"); break;
-		case CL_IMAGE_FORMAT_MISMATCH: 						printf("CL_IMAGE_FORMAT_MISMATCH\n"); break;
-		case CL_IMAGE_FORMAT_NOT_SUPPORTED: 				printf("CL_IMAGE_FORMAT_NOT_SUPPORTED\n"); break;
-		case CL_BUILD_PROGRAM_FAILURE: 						printf("CL_BUILD_PROGRAM_FAILURE\n"); break;
-		case CL_MAP_FAILURE: 								printf("CL_MAP_FAILURE\n"); break;
-		case CL_INVALID_VALUE: 								printf("CL_INVALID_VALUE\n"); break;
-		case CL_INVALID_DEVICE_TYPE: 						printf("CL_INVALID_DEVICE_TYPE\n"); break;
-		case CL_INVALID_PLATFORM: 							printf("CL_INVALID_PLATFORM\n"); break;
-		case CL_INVALID_DEVICE: 							printf("CL_INVALID_DEVICE\n"); break;
-		case CL_INVALID_CONTEXT: 							printf("CL_INVALID_CONTEXT\n"); break;
-		case CL_INVALID_QUEUE_PROPERTIES: 					printf("CL_INVALID_QUEUE_PROPERTIES\n"); break;
-		case CL_INVALID_COMMAND_QUEUE: 						printf("CL_INVALID_COMMAND_QUEUE\n"); break;
-		case CL_INVALID_HOST_PTR: 							printf("CL_INVALID_HOST_PTR\n"); break;
-		case CL_INVALID_MEM_OBJECT: 						printf("CL_INVALID_MEM_OBJECT\n"); break;
-		case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: 			printf("CL_INVALID_IMAGE_FORMAT_DESCRIPTOR\n"); break;
-		case CL_INVALID_IMAGE_SIZE: 						printf("CL_INVALID_IMAGE_SIZE\n"); break;
-		case CL_INVALID_SAMPLER: 							printf("CL_INVALID_SAMPLER\n"); break;
-		case CL_INVALID_BINARY: 							printf("CL_INVALID_BINARY\n"); break;
-		case CL_INVALID_BUILD_OPTIONS: 						printf("CL_INVALID_BUILD_OPTIONS\n"); break;
-		case CL_INVALID_PROGRAM: 							printf("CL_INVALID_PROGRAM\n"); break;
-		case CL_INVALID_PROGRAM_EXECUTABLE: 				printf("CL_INVALID_PROGRAM_EXECUTABLE\n"); break;
-		case CL_INVALID_KERNEL_NAME: 						printf("CL_INVALID_KERNEL_NAME\n"); break;
-		case CL_INVALID_KERNEL_DEFINITION: 					printf("CL_INVALID_KERNEL_DEFINITION\n"); break;
-		case CL_INVALID_KERNEL: 							printf("CL_INVALID_KERNEL\n"); break;
-		case CL_INVALID_ARG_INDEX: 							printf("CL_INVALID_ARG_INDEX\n"); break;
-		case CL_INVALID_ARG_VALUE: 							printf("CL_INVALID_ARG_VALUE\n"); break;
-		case CL_INVALID_ARG_SIZE: 							printf("CL_INVALID_ARG_SIZE\n"); break;
-		case CL_INVALID_KERNEL_ARGS: 						printf("CL_INVALID_KERNEL_ARGS\n"); break;
-		case CL_INVALID_WORK_DIMENSION: 					printf("CL_INVALID_WORK_DIMENSION\n"); break;
-		case CL_INVALID_WORK_GROUP_SIZE: 					printf("CL_INVALID_WORK_GROUP_SIZE\n"); break;
-		case CL_INVALID_WORK_ITEM_SIZE: 					printf("CL_INVALID_WORK_ITEM_SIZE\n"); break;
-		case CL_INVALID_GLOBAL_OFFSET: 						printf("CL_INVALID_GLOBAL_OFFSET\n"); break;
-		case CL_INVALID_EVENT_WAIT_LIST: 					printf("CL_INVALID_EVENT_WAIT_LIST\n"); break;
-		case CL_INVALID_EVENT: 								printf("CL_INVALID_EVENT\n"); break;
-		case CL_INVALID_OPERATION: 							printf("CL_INVALID_OPERATION\n"); break;
-		case CL_INVALID_GL_OBJECT: 							printf("CL_INVALID_GL_OBJECT\n"); break;
-		case CL_INVALID_BUFFER_SIZE: 						printf("CL_INVALID_BUFFER_SIZE\n"); break;
-		case CL_INVALID_MIP_LEVEL: 							printf("CL_INVALID_MIP_LEVEL\n"); break;
-		case CL_INVALID_GLOBAL_WORK_SIZE: 					printf("CL_INVALID_GLOBAL_WORK_SIZE\n"); break;
-		
-		#ifdef CL_VERSION_1_1
-		case CL_MISALIGNED_SUB_BUFFER_OFFSET: 				printf("CL_MISALIGNED_SUB_BUFFER_OFFSET\n"); break;
-		case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST:	printf("CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST\n"); break;
-		case CL_INVALID_PROPERTY: 							printf("CL_INVALID_PROPERTY\n"); break;
-		#endif
-		
-		default:											printf("Invalid OpenCL error code\n");
-	}
+  printf("Error in %s at line %d: ", file, line);
 
-	exit(error);
+  // Print
+  switch(error) {
+  case CL_SUCCESS: 									printf("CL_SUCCESS\n"); break;
+  case CL_DEVICE_NOT_FOUND: 							printf("CL_DEVICE_NOT_FOUND\n"); break;
+  case CL_DEVICE_NOT_AVAILABLE: 						printf("CL_DEVICE_NOT_AVAILABLE\n"); break;
+  case CL_COMPILER_NOT_AVAILABLE: 					printf("CL_COMPILER_NOT_AVAILABLE\n"); break;
+  case CL_MEM_OBJECT_ALLOCATION_FAILURE: 				printf("CL_MEM_OBJECT_ALLOCATION_FAILURE\n"); break;
+  case CL_OUT_OF_RESOURCES: 							printf("CL_OUT_OF_RESOURCES\n"); break;
+  case CL_OUT_OF_HOST_MEMORY: 						printf("CL_OUT_OF_HOST_MEMORY\n"); break;
+  case CL_PROFILING_INFO_NOT_AVAILABLE: 				printf("CL_PROFILING_INFO_NOT_AVAILABLE\n"); break;
+  case CL_MEM_COPY_OVERLAP: 							printf("CL_MEM_COPY_OVERLAP\n"); break;
+  case CL_IMAGE_FORMAT_MISMATCH: 						printf("CL_IMAGE_FORMAT_MISMATCH\n"); break;
+  case CL_IMAGE_FORMAT_NOT_SUPPORTED: 				printf("CL_IMAGE_FORMAT_NOT_SUPPORTED\n"); break;
+  case CL_BUILD_PROGRAM_FAILURE: 						printf("CL_BUILD_PROGRAM_FAILURE\n"); break;
+  case CL_MAP_FAILURE: 								printf("CL_MAP_FAILURE\n"); break;
+  case CL_INVALID_VALUE: 								printf("CL_INVALID_VALUE\n"); break;
+  case CL_INVALID_DEVICE_TYPE: 						printf("CL_INVALID_DEVICE_TYPE\n"); break;
+  case CL_INVALID_PLATFORM: 							printf("CL_INVALID_PLATFORM\n"); break;
+  case CL_INVALID_DEVICE: 							printf("CL_INVALID_DEVICE\n"); break;
+  case CL_INVALID_CONTEXT: 							printf("CL_INVALID_CONTEXT\n"); break;
+  case CL_INVALID_QUEUE_PROPERTIES: 					printf("CL_INVALID_QUEUE_PROPERTIES\n"); break;
+  case CL_INVALID_COMMAND_QUEUE: 						printf("CL_INVALID_COMMAND_QUEUE\n"); break;
+  case CL_INVALID_HOST_PTR: 							printf("CL_INVALID_HOST_PTR\n"); break;
+  case CL_INVALID_MEM_OBJECT: 						printf("CL_INVALID_MEM_OBJECT\n"); break;
+  case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: 			printf("CL_INVALID_IMAGE_FORMAT_DESCRIPTOR\n"); break;
+  case CL_INVALID_IMAGE_SIZE: 						printf("CL_INVALID_IMAGE_SIZE\n"); break;
+  case CL_INVALID_SAMPLER: 							printf("CL_INVALID_SAMPLER\n"); break;
+  case CL_INVALID_BINARY: 							printf("CL_INVALID_BINARY\n"); break;
+  case CL_INVALID_BUILD_OPTIONS: 						printf("CL_INVALID_BUILD_OPTIONS\n"); break;
+  case CL_INVALID_PROGRAM: 							printf("CL_INVALID_PROGRAM\n"); break;
+  case CL_INVALID_PROGRAM_EXECUTABLE: 				printf("CL_INVALID_PROGRAM_EXECUTABLE\n"); break;
+  case CL_INVALID_KERNEL_NAME: 						printf("CL_INVALID_KERNEL_NAME\n"); break;
+  case CL_INVALID_KERNEL_DEFINITION: 					printf("CL_INVALID_KERNEL_DEFINITION\n"); break;
+  case CL_INVALID_KERNEL: 							printf("CL_INVALID_KERNEL\n"); break;
+  case CL_INVALID_ARG_INDEX: 							printf("CL_INVALID_ARG_INDEX\n"); break;
+  case CL_INVALID_ARG_VALUE: 							printf("CL_INVALID_ARG_VALUE\n"); break;
+  case CL_INVALID_ARG_SIZE: 							printf("CL_INVALID_ARG_SIZE\n"); break;
+  case CL_INVALID_KERNEL_ARGS: 						printf("CL_INVALID_KERNEL_ARGS\n"); break;
+  case CL_INVALID_WORK_DIMENSION: 					printf("CL_INVALID_WORK_DIMENSION\n"); break;
+  case CL_INVALID_WORK_GROUP_SIZE: 					printf("CL_INVALID_WORK_GROUP_SIZE\n"); break;
+  case CL_INVALID_WORK_ITEM_SIZE: 					printf("CL_INVALID_WORK_ITEM_SIZE\n"); break;
+  case CL_INVALID_GLOBAL_OFFSET: 						printf("CL_INVALID_GLOBAL_OFFSET\n"); break;
+  case CL_INVALID_EVENT_WAIT_LIST: 					printf("CL_INVALID_EVENT_WAIT_LIST\n"); break;
+  case CL_INVALID_EVENT: 								printf("CL_INVALID_EVENT\n"); break;
+  case CL_INVALID_OPERATION: 							printf("CL_INVALID_OPERATION\n"); break;
+  case CL_INVALID_GL_OBJECT: 							printf("CL_INVALID_GL_OBJECT\n"); break;
+  case CL_INVALID_BUFFER_SIZE: 						printf("CL_INVALID_BUFFER_SIZE\n"); break;
+  case CL_INVALID_MIP_LEVEL: 							printf("CL_INVALID_MIP_LEVEL\n"); break;
+  case CL_INVALID_GLOBAL_WORK_SIZE: 					printf("CL_INVALID_GLOBAL_WORK_SIZE\n"); break;
+
+  #ifdef CL_VERSION_1_1
+  case CL_MISALIGNED_SUB_BUFFER_OFFSET: 				printf("CL_MISALIGNED_SUB_BUFFER_OFFSET\n"); break;
+  case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST:	printf("CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST\n"); break;
+  case CL_INVALID_PROPERTY: 							printf("CL_INVALID_PROPERTY\n"); break;
+  #endif
+
+  default:											printf("Invalid OpenCL error code\n");
+  }
+
+  exit(error);
 }
 
 void check_error(cl_int error, char *file, int line) {
-	if (error != CL_SUCCESS) {
-		fatal_CL(error, file, line);
-	}
-}
+  if (error != CL_SUCCESS) {
+  fatal_CL(error, file, line);
+  }
+}

+ 214 - 215
opencl/leukocyte/OpenCL/avilib.c

@@ -1,27 +1,27 @@
 /*
  *  avilib.c
  *
- *  Copyright (C) Thomas Östreich - June 2001
- *  multiple audio track support Copyright (C) 2002 Thomas Östreich 
+ *  Copyright (C) Thomas �streich - June 2001
+ *  multiple audio track support Copyright (C) 2002 Thomas �streich
  *
  *  Original code:
- *  Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de> 
+ *  Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
  *
  *  This file is part of transcode, a linux video stream processing tool
- *      
+ *
  *  transcode is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2, or (at your option)
  *  any later version.
- *   
+ *
  *  transcode is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
- *   
+ *
  *  You should have received a copy of the GNU General Public License
  *  along with GNU Make; see the file COPYING.  If not, write to
- *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  *
  */
 
@@ -65,7 +65,7 @@ static size_t avi_read(int fd, char *buf, size_t len)
       n = read (fd, buf + r, len - r);
 
       if (n <= 0)
-	  return r;
+    return r;
       r += n;
    }
 
@@ -81,7 +81,7 @@ static size_t avi_write (int fd, char *buf, size_t len)
       n = write (fd, buf + r, len - r);
       if (n < 0)
          return n;
-      
+
       r += n;
    }
    return r;
@@ -130,7 +130,7 @@ static int avi_sampsize(avi_t *AVI, int j)
    int s;
    s = ((AVI->track[j].a_bits+7)/8)*AVI->track[j].a_chans;
    //   if(s==0) s=1; /* avoid possible zero divisions */
-   if(s<4) s=4; /* avoid possible zero divisions */ 
+   if(s<4) s=4; /* avoid possible zero divisions */
    return s;
 }
 
@@ -175,7 +175,7 @@ static int avi_add_index_entry(avi_t *AVI, unsigned char *tag, long flags, unsig
 
    if(AVI->n_idx>=AVI->max_idx) {
      ptr = realloc((void *)AVI->idx,(AVI->max_idx+4096)*16);
-     
+
      if(ptr == 0) {
        AVI_errno = AVI_ERR_NO_MEM;
        return -1;
@@ -183,7 +183,7 @@ static int avi_add_index_entry(avi_t *AVI, unsigned char *tag, long flags, unsig
      AVI->max_idx += 4096;
      AVI->idx = (unsigned char((*)[16]) ) ptr;
    }
-   
+
    /* Add index entry */
 
    //   fprintf(stderr, "INDEX %s %ld %lu %lu\n", tag, flags, pos, len);
@@ -192,7 +192,7 @@ static int avi_add_index_entry(avi_t *AVI, unsigned char *tag, long flags, unsig
    long2str(AVI->idx[AVI->n_idx]+ 4,flags);
    long2str(AVI->idx[AVI->n_idx]+ 8, pos);
    long2str(AVI->idx[AVI->n_idx]+12, len);
-   
+
    /* Update counter */
 
    AVI->n_idx++;
@@ -215,7 +215,7 @@ avi_t* AVI_open_output_file(char * filename)
    int i;
 
    int mask = 0;
-   
+
    unsigned char AVI_header[HEADERBYTES];
 
    /* Allocate the avi_t struct and zero it */
@@ -275,13 +275,13 @@ void AVI_set_video(avi_t *AVI, int width, int height, double fps, char *compress
    AVI->width  = width;
    AVI->height = height;
    AVI->fps    = fps;
-   
+
    if(strncmp(compressor, "RGB", 3)==0) {
      memset(AVI->compressor, 0, 4);
    } else {
      memcpy(AVI->compressor,compressor,4);
-   }     
-   
+   }
+
    AVI->compressor[4] = 0;
 
    avi_update_header(AVI);
@@ -378,7 +378,7 @@ int avi_update_header(avi_t *AVI)
    OUT4CC ("avih");
    OUTLONG(56);                 /* # of bytes to follow */
    OUTLONG(ms_per_frame);       /* Microseconds per frame */
-   //ThOe ->0 
+   //ThOe ->0
    //   OUTLONG(10000000);           /* MaxBytesPerSec, I hope this will never be used */
    OUTLONG(0);
    OUTLONG(0);                  /* PaddingGranularity (whatever that might be) */
@@ -450,33 +450,33 @@ int avi_update_header(avi_t *AVI)
 
    long2str(AVI_header+strl_start-4,nhb-strl_start);
 
-   
+
    /* Start the audio stream list ---------------------------------- */
-   
+
    for(j=0; j<AVI->anum; ++j) {
-       
+
        sampsize = avi_sampsize(AVI, j);
-   
+
        OUT4CC ("LIST");
        OUTLONG(0);        /* Length of list in bytes, don't know yet */
        strl_start = nhb;  /* Store start position */
        OUT4CC ("strl");
-       
+
        /* The audio stream header */
-       
+
        OUT4CC ("strh");
        OUTLONG(56);            /* # of bytes to follow */
        OUT4CC ("auds");
-       
+
        // -----------
        // ThOe
        OUTLONG(0);             /* Format (Optionally) */
        // -----------
-       
+
        OUTLONG(0);             /* Flags */
        OUTLONG(0);             /* Reserved, MS says: wPriority, wLanguage */
        OUTLONG(0);             /* InitialFrames */
-       
+
        // ThOe /4
        OUTLONG(sampsize/4);      /* Scale */
        OUTLONG(1000*AVI->track[j].mp3rate/8);
@@ -484,17 +484,17 @@ int avi_update_header(avi_t *AVI)
        OUTLONG(4*AVI->track[j].audio_bytes/sampsize);   /* Length */
        OUTLONG(0);             /* SuggestedBufferSize */
        OUTLONG(-1);            /* Quality */
-       
+
        // ThOe /4
        OUTLONG(sampsize/4);    /* SampleSize */
-       
+
        OUTLONG(0);             /* Frame */
        OUTLONG(0);             /* Frame */
        //       OUTLONG(0);             /* Frame */
        //OUTLONG(0);             /* Frame */
-       
+
        /* The audio stream format */
-       
+
        OUT4CC ("strf");
        OUTLONG(16);                   /* # of bytes to follow */
        OUTSHRT(AVI->track[j].a_fmt);           /* Format */
@@ -503,47 +503,47 @@ int avi_update_header(avi_t *AVI)
        // ThOe
        OUTLONG(1000*AVI->track[j].mp3rate/8);
        //ThOe (/4)
-       
+
        OUTSHRT(sampsize/4);           /* BlockAlign */
-       
-       
+
+
        OUTSHRT(AVI->track[j].a_bits);          /* BitsPerSample */
-       
+
        /* Finish stream list, i.e. put number of bytes in the list to proper pos */
-       
+
        long2str(AVI_header+strl_start-4,nhb-strl_start);
    }
-   
+
    /* Finish header list */
-   
+
    long2str(AVI_header+hdrl_start-4,nhb-hdrl_start);
-   
-   
+
+
    /* Calculate the needed amount of junk bytes, output junk */
-   
+
    njunk = HEADERBYTES - nhb - 8 - 12;
-   
+
    /* Safety first: if njunk <= 0, somebody has played with
       HEADERBYTES without knowing what (s)he did.
       This is a fatal error */
-   
+
    if(njunk<=0)
      {
        fprintf(stderr,"AVI_close_output_file: # of header bytes too small\n");
        exit(1);
      }
-   
+
    OUT4CC ("JUNK");
    OUTLONG(njunk);
    memset(AVI_header+nhb,0,njunk);
-   
-   //11/14/01 added id string 
+
+   //11/14/01 added id string
 
    if(njunk > strlen(id_str)+8) {
      sprintf(id_str, "%s-%s", PACKAGE, VERSION);
      memcpy(AVI_header+nhb, id_str, strlen(id_str));
    }
-   
+
    nhb += njunk;
 
    /* Start the movi list */
@@ -557,7 +557,7 @@ int avi_update_header(avi_t *AVI)
 
    if ( lseek(AVI->fdes,0,SEEK_SET)<0 ||
         avi_write(AVI->fdes,(char *)AVI_header,HEADERBYTES)!=HEADERBYTES ||
-	lseek(AVI->fdes,AVI->pos,SEEK_SET)<0)
+  lseek(AVI->fdes,AVI->pos,SEEK_SET)<0)
      {
        AVI_errno = AVI_ERR_CLOSE;
        return -1;
@@ -604,7 +604,7 @@ static int avi_close_output_file(avi_t *AVI)
      idxerror = 1;
      AVI_errno = AVI_ERR_WRITE_INDEX;
    }
-   
+
    /* Calculate Microseconds per frame */
 
    if(AVI->fps < 0.001) {
@@ -646,7 +646,7 @@ static int avi_close_output_file(avi_t *AVI)
    OUT4CC ("avih");
    OUTLONG(56);                 /* # of bytes to follow */
    OUTLONG(ms_per_frame);       /* Microseconds per frame */
-   //ThOe ->0 
+   //ThOe ->0
    //   OUTLONG(10000000);           /* MaxBytesPerSec, I hope this will never be used */
    OUTLONG(0);
    OUTLONG(0);                  /* PaddingGranularity (whatever that might be) */
@@ -725,71 +725,71 @@ static int avi_close_output_file(avi_t *AVI)
    /* Start the audio stream list ---------------------------------- */
 
    for(j=0; j<AVI->anum; ++j) {
-     
+
      //if (AVI->track[j].a_chans && AVI->track[j].audio_bytes)
        {
-	   
-	 sampsize = avi_sampsize(AVI, j);
-	   
-	 OUT4CC ("LIST");
-	 OUTLONG(0);        /* Length of list in bytes, don't know yet */
-	 strl_start = nhb;  /* Store start position */
-	 OUT4CC ("strl");
-	   
-	 /* The audio stream header */
-	 
-	 OUT4CC ("strh");
-	 OUTLONG(56);            /* # of bytes to follow */
-	 OUT4CC ("auds");
-	 
-	 // -----------
-	 // ThOe
-	 OUTLONG(0);             /* Format (Optionally) */
-	   // -----------
-	   
-	 OUTLONG(0);             /* Flags */
-	 OUTLONG(0);             /* Reserved, MS says: wPriority, wLanguage */
-	 OUTLONG(0);             /* InitialFrames */
-	   
-	 // ThOe /4
-	 OUTLONG(sampsize/4);      /* Scale */
-	 OUTLONG(1000*AVI->track[j].mp3rate/8);
-	 OUTLONG(0);             /* Start */
-	 OUTLONG(4*AVI->track[j].audio_bytes/sampsize);   /* Length */
-	 OUTLONG(0);             /* SuggestedBufferSize */
-	 OUTLONG(-1);            /* Quality */
-	   
-	 // ThOe /4
-	 OUTLONG(sampsize/4);    /* SampleSize */
-	   
-	 OUTLONG(0);             /* Frame */
-	 OUTLONG(0);             /* Frame */
-	 //	 OUTLONG(0);             /* Frame */
-	 //OUTLONG(0);             /* Frame */
-	   
-	 /* The audio stream format */
-	 
-	 OUT4CC ("strf");
-	 OUTLONG(16);                   /* # of bytes to follow */
-	 OUTSHRT(AVI->track[j].a_fmt);           /* Format */
-	 OUTSHRT(AVI->track[j].a_chans);         /* Number of channels */
-	 OUTLONG(AVI->track[j].a_rate);          /* SamplesPerSec */
-	 // ThOe
-	 OUTLONG(1000*AVI->track[j].mp3rate/8);
-	 //ThOe (/4)
-	 
-	 OUTSHRT(sampsize/4);           /* BlockAlign */
-	 
-	 
-	 OUTSHRT(AVI->track[j].a_bits);          /* BitsPerSample */
-	 
-	 /* Finish stream list, i.e. put number of bytes in the list to proper pos */
+
+   sampsize = avi_sampsize(AVI, j);
+
+   OUT4CC ("LIST");
+   OUTLONG(0);        /* Length of list in bytes, don't know yet */
+   strl_start = nhb;  /* Store start position */
+   OUT4CC ("strl");
+
+   /* The audio stream header */
+
+   OUT4CC ("strh");
+   OUTLONG(56);            /* # of bytes to follow */
+   OUT4CC ("auds");
+
+   // -----------
+   // ThOe
+   OUTLONG(0);             /* Format (Optionally) */
+     // -----------
+
+   OUTLONG(0);             /* Flags */
+   OUTLONG(0);             /* Reserved, MS says: wPriority, wLanguage */
+   OUTLONG(0);             /* InitialFrames */
+
+   // ThOe /4
+   OUTLONG(sampsize/4);      /* Scale */
+   OUTLONG(1000*AVI->track[j].mp3rate/8);
+   OUTLONG(0);             /* Start */
+   OUTLONG(4*AVI->track[j].audio_bytes/sampsize);   /* Length */
+   OUTLONG(0);             /* SuggestedBufferSize */
+   OUTLONG(-1);            /* Quality */
+
+   // ThOe /4
+   OUTLONG(sampsize/4);    /* SampleSize */
+
+   OUTLONG(0);             /* Frame */
+   OUTLONG(0);             /* Frame */
+   //	 OUTLONG(0);             /* Frame */
+   //OUTLONG(0);             /* Frame */
+
+   /* The audio stream format */
+
+   OUT4CC ("strf");
+   OUTLONG(16);                   /* # of bytes to follow */
+   OUTSHRT(AVI->track[j].a_fmt);           /* Format */
+   OUTSHRT(AVI->track[j].a_chans);         /* Number of channels */
+   OUTLONG(AVI->track[j].a_rate);          /* SamplesPerSec */
+   // ThOe
+   OUTLONG(1000*AVI->track[j].mp3rate/8);
+   //ThOe (/4)
+
+   OUTSHRT(sampsize/4);           /* BlockAlign */
+
+
+   OUTSHRT(AVI->track[j].a_bits);          /* BitsPerSample */
+
+   /* Finish stream list, i.e. put number of bytes in the list to proper pos */
        }
        long2str(AVI_header+strl_start-4,nhb-strl_start);
    }
-   
+
    /* Finish header list */
-   
+
    long2str(AVI_header+hdrl_start-4,nhb-hdrl_start);
 
 
@@ -797,7 +797,7 @@ static int avi_close_output_file(avi_t *AVI)
 
 #ifdef INFO_LIST
    OUT4CC ("LIST");
-   
+
    //FIXME
    info_len = MAX_INFO_STRLEN + 12;
    OUTLONG(info_len);
@@ -822,7 +822,7 @@ static int avi_close_output_file(avi_t *AVI)
 //   OUT4CC ("ICMT");
 //   OUTLONG(MAX_INFO_STRLEN);
 
-//   calptr=time(NULL); 
+//   calptr=time(NULL);
 //   sprintf(id_str, "\t%s %s", ctime(&calptr), "");
 //   memset(AVI_header+nhb, 0, MAX_INFO_STRLEN);
 //   memcpy(AVI_header+nhb, id_str, 25);
@@ -830,15 +830,15 @@ static int avi_close_output_file(avi_t *AVI)
 #endif
 
    // ----------------------------
-   
+
    /* Calculate the needed amount of junk bytes, output junk */
-   
+
    njunk = HEADERBYTES - nhb - 8 - 12;
-   
+
    /* Safety first: if njunk <= 0, somebody has played with
       HEADERBYTES without knowing what (s)he did.
       This is a fatal error */
-   
+
    if(njunk<=0)
    {
       fprintf(stderr,"AVI_close_output_file: # of header bytes too small\n");
@@ -848,7 +848,7 @@ static int avi_close_output_file(avi_t *AVI)
    OUT4CC ("JUNK");
    OUTLONG(njunk);
    memset(AVI_header+nhb,0,njunk);
-   
+
    nhb += njunk;
 
    /* Start the movi list */
@@ -861,8 +861,8 @@ static int avi_close_output_file(avi_t *AVI)
       actually written, report an error if someting goes wrong */
 
    if ( lseek(AVI->fdes,0,SEEK_SET)<0 ||
-        avi_write(AVI->fdes,(char *)AVI_header,HEADERBYTES)!=HEADERBYTES 
-        //|| ftruncate(AVI->fdes,AVI->pos)<0 
+        avi_write(AVI->fdes,(char *)AVI_header,HEADERBYTES)!=HEADERBYTES
+        //|| ftruncate(AVI->fdes,AVI->pos)<0
         )
    {
       AVI_errno = AVI_ERR_CLOSE;
@@ -891,12 +891,12 @@ static int avi_write_data(avi_t *AVI, char *data, unsigned long length, int audi
    unsigned char astr[5];
 
    /* Check for maximum file length */
-   
+
    if ( (AVI->pos + 8 + length + 8 + (AVI->n_idx+1)*16) > AVI_MAX_LEN ) {
      AVI_errno = AVI_ERR_SIZELIM;
      return -1;
    }
-   
+
    /* Add index entry */
 
    //set tag for current audio track
@@ -906,31 +906,31 @@ static int avi_write_data(avi_t *AVI, char *data, unsigned long length, int audi
      n = avi_add_index_entry(AVI,astr,0x00,AVI->pos,length);
    else
      n = avi_add_index_entry(AVI,(unsigned char *) "00db",((keyframe)?0x10:0x0),AVI->pos,length);
-   
+
    if(n) return -1;
-   
+
    /* Output tag and data */
-   
+
    if(audio)
      n = avi_add_chunk(AVI,(unsigned char *) astr, (unsigned char *)data,length);
    else
      n = avi_add_chunk(AVI,(unsigned char *)"00db",(unsigned char *)data,length);
-   
+
    if (n) return -1;
-   
+
    return 0;
 }
 
 int AVI_write_frame(avi_t *AVI, char *data, long bytes, int keyframe)
 {
   unsigned long pos;
-  
+
   if(AVI->mode==AVI_MODE_READ) { AVI_errno = AVI_ERR_NOT_PERM; return -1; }
-  
+
   pos = AVI->pos;
 
   if(avi_write_data(AVI,data,bytes,0,keyframe)) return -1;
-   
+
   AVI->last_pos = pos;
   AVI->last_len = bytes;
   AVI->video_frames++;
@@ -965,15 +965,15 @@ int AVI_append_audio(avi_t *AVI, char *data, long bytes)
   unsigned char c[4];
 
   if(AVI->mode==AVI_MODE_READ) { AVI_errno = AVI_ERR_NOT_PERM; return -1; }
-  
+
   // update last index entry:
-  
+
   --AVI->n_idx;
   length = str2ulong(AVI->idx[AVI->n_idx]+12);
   pos    = str2ulong(AVI->idx[AVI->n_idx]+8);
 
   //update;
-  long2str(AVI->idx[AVI->n_idx]+12,length+bytes);   
+  long2str(AVI->idx[AVI->n_idx]+12,length+bytes);
 
   ++AVI->n_idx;
 
@@ -981,7 +981,7 @@ int AVI_append_audio(avi_t *AVI, char *data, long bytes)
 
   //update chunk header
   lseek(AVI->fdes, pos+4, SEEK_SET);
-  long2str(c, length+bytes);     
+  long2str(c, length+bytes);
   avi_write(AVI->fdes,(char *) c, 4);
 
   lseek(AVI->fdes, pos+8+length, SEEK_SET);
@@ -1012,7 +1012,7 @@ long AVI_bytes_written(avi_t *AVI)
 
 int AVI_set_audio_track(avi_t *AVI, int track)
 {
-  
+
   if(track < 0 || track + 1 > AVI->anum) return(-1);
 
   //this info is not written to file anyway
@@ -1067,9 +1067,9 @@ int AVI_close(avi_t *AVI)
 avi_t *AVI_open_input_file(char *filename, int getIndex)
 {
   avi_t *AVI=NULL;
-  
+
   /* Create avi_t structure */
-  
+
   AVI = (avi_t *) malloc(sizeof(avi_t));
   if(AVI==NULL)
     {
@@ -1077,11 +1077,11 @@ avi_t *AVI_open_input_file(char *filename, int getIndex)
       return 0;
     }
   memset((void *)AVI,0,sizeof(avi_t));
-  
+
   AVI->mode = AVI_MODE_READ; /* open for reading */
-  
+
   /* Open the file */
-  
+
   AVI->fdes = open(filename,O_RDONLY|O_BINARY);
   if(AVI->fdes < 0)
     {
@@ -1089,10 +1089,10 @@ avi_t *AVI_open_input_file(char *filename, int getIndex)
       free(AVI);
       return 0;
     }
-  
+
   avi_parse_input_file(AVI, getIndex);
 
-  AVI->aptr=0; //reset  
+  AVI->aptr=0; //reset
 
   return AVI;
 }
@@ -1100,9 +1100,9 @@ avi_t *AVI_open_input_file(char *filename, int getIndex)
 avi_t *AVI_open_fd(int fd, int getIndex)
 {
   avi_t *AVI=NULL;
-  
+
   /* Create avi_t structure */
-  
+
   AVI = (avi_t *) malloc(sizeof(avi_t));
   if(AVI==NULL)
     {
@@ -1110,16 +1110,16 @@ avi_t *AVI_open_fd(int fd, int getIndex)
       return 0;
     }
   memset((void *)AVI,0,sizeof(avi_t));
-  
+
   AVI->mode = AVI_MODE_READ; /* open for reading */
-  
+
   // file alread open
   AVI->fdes = fd;
-  
+
   avi_parse_input_file(AVI, getIndex);
 
   AVI->aptr=0; //reset
-  
+
   return AVI;
 }
 
@@ -1138,7 +1138,7 @@ int avi_parse_input_file(avi_t *AVI, int getIndex)
   //  int auds_strf_seen = 0;
   int num_stream = 0;
   char data[256];
-  
+
   /* Read first 12 bytes and check that this is an AVI file */
 
    if( avi_read(AVI->fdes,data,12) != 12 ) ERR_EXIT(AVI_ERR_READ)
@@ -1168,11 +1168,11 @@ int avi_parse_input_file(avi_t *AVI, int getIndex)
             hdrl_len = n;
             hdrl_data = (unsigned char *) malloc(n);
             if(hdrl_data==0) ERR_EXIT(AVI_ERR_NO_MEM);
-				 
-	    // offset of header
-	    
-	    header_offset = lseek(AVI->fdes,0,SEEK_CUR);
-				 
+
+      // offset of header
+
+      header_offset = lseek(AVI->fdes,0,SEEK_CUR);
+
             if( avi_read(AVI->fdes,(char *)hdrl_data,n) != n ) ERR_EXIT(AVI_ERR_READ)
          }
          else if(strncasecmp(data,"movi",4) == 0)
@@ -1221,38 +1221,38 @@ int avi_parse_input_file(avi_t *AVI, int getIndex)
             memcpy(AVI->compressor,hdrl_data+i+4,4);
             AVI->compressor[4] = 0;
 
-	    // ThOe
-	    AVI->v_codech_off = header_offset + i+4;
+      // ThOe
+      AVI->v_codech_off = header_offset + i+4;
 
             scale = str2ulong((unsigned char *)hdrl_data+i+20);
             rate  = str2ulong(hdrl_data+i+24);
             if(scale!=0) AVI->fps = (double)rate/(double)scale;
             AVI->video_frames = str2ulong(hdrl_data+i+32);
             AVI->video_strn = num_stream;
-	    AVI->max_len = 0;
+      AVI->max_len = 0;
             vids_strh_seen = 1;
             lasttag = 1; /* vids */
          }
          else if (strncasecmp ((char *) hdrl_data+i,"auds",4) ==0 && ! auds_strh_seen)
          {
 
-	   //inc audio tracks
-	   AVI->aptr=AVI->anum;
-	   ++AVI->anum;
-	   
-	   if(AVI->anum > AVI_MAX_TRACKS) {
-	     fprintf(stderr, "error - only %d audio tracks supported\n", AVI_MAX_TRACKS);
-	     return(-1);
-	   }
-	   
-	   AVI->track[AVI->aptr].audio_bytes = str2ulong(hdrl_data+i+32)*avi_sampsize(AVI, 0);
-	   AVI->track[AVI->aptr].audio_strn = num_stream;
-	   //	   auds_strh_seen = 1;
-	   lasttag = 2; /* auds */
-	   
-	   // ThOe
-	   AVI->track[AVI->aptr].a_codech_off = header_offset + i;
-	   
+     //inc audio tracks
+     AVI->aptr=AVI->anum;
+     ++AVI->anum;
+
+     if(AVI->anum > AVI_MAX_TRACKS) {
+       fprintf(stderr, "error - only %d audio tracks supported\n", AVI_MAX_TRACKS);
+       return(-1);
+     }
+
+     AVI->track[AVI->aptr].audio_bytes = str2ulong(hdrl_data+i+32)*avi_sampsize(AVI, 0);
+     AVI->track[AVI->aptr].audio_strn = num_stream;
+     //	   auds_strh_seen = 1;
+     lasttag = 2; /* auds */
+
+     // ThOe
+     AVI->track[AVI->aptr].a_codech_off = header_offset + i;
+
          }
          else
             lasttag = 0;
@@ -1266,10 +1266,10 @@ int avi_parse_input_file(avi_t *AVI, int getIndex)
             AVI->width  = str2ulong(hdrl_data+i+4);
             AVI->height = str2ulong(hdrl_data+i+8);
             vids_strf_seen = 1;
-	    //ThOe
-	    AVI->v_codecf_off = header_offset + i+16;
+      //ThOe
+      AVI->v_codecf_off = header_offset + i+16;
 
-	    memcpy(AVI->compressor2, hdrl_data+i+16, 4);
+      memcpy(AVI->compressor2, hdrl_data+i+16, 4);
             AVI->compressor2[4] = 0;
 
          }
@@ -1277,16 +1277,16 @@ int avi_parse_input_file(avi_t *AVI, int getIndex)
          {
             AVI->track[AVI->aptr].a_fmt   = str2ushort(hdrl_data+i  );
 
-	    //ThOe
-	    AVI->track[AVI->aptr].a_codecf_off = header_offset + i;
-	    
+      //ThOe
+      AVI->track[AVI->aptr].a_codecf_off = header_offset + i;
+
             AVI->track[AVI->aptr].a_chans = str2ushort(hdrl_data+i+2);
             AVI->track[AVI->aptr].a_rate  = str2ulong (hdrl_data+i+4);
-	    //ThOe: read mp3bitrate
-	    AVI->track[AVI->aptr].mp3rate = 8*str2ulong(hdrl_data+i+8)/1000;
-	    //:ThOe
+      //ThOe: read mp3bitrate
+      AVI->track[AVI->aptr].mp3rate = 8*str2ulong(hdrl_data+i+8)/1000;
+      //:ThOe
             AVI->track[AVI->aptr].a_bits  = str2ushort(hdrl_data+i+14);
-	    //            auds_strf_seen = 1;
+      //            auds_strf_seen = 1;
          }
          lasttag = 0;
       }
@@ -1345,7 +1345,7 @@ int avi_parse_input_file(avi_t *AVI, int getIndex)
 
       lseek(AVI->fdes,pos,SEEK_SET);
       if(avi_read(AVI->fdes,data,8)!=8) ERR_EXIT(AVI_ERR_READ)
-      if( strncasecmp((char *)data,(char *)AVI->idx[i],4)==0 && 
+      if( strncasecmp((char *)data,(char *)AVI->idx[i],4)==0 &&
       str2ulong((unsigned char *)data+4)==len )
       {
          idx_type = 1; /* Index from start of file */
@@ -1384,15 +1384,15 @@ int avi_parse_input_file(avi_t *AVI, int getIndex)
          }
 
          /* Check if we got a tag ##db, ##dc or ##wb */
-	 
+
          if( ( (data[2]=='d' || data[2]=='D') &&
                (data[3]=='b' || data[3]=='B' || data[3]=='c' || data[3]=='C') )
-	     || ( (data[2]=='w' || data[2]=='W') &&
-		  (data[3]=='b' || data[3]=='B') ) )
-	   {
-	   avi_add_index_entry(AVI,(unsigned char *) data,0,lseek(AVI->fdes,0,SEEK_CUR)-8,n);
+       || ( (data[2]=='w' || data[2]=='W') &&
+    (data[3]=='b' || data[3]=='B') ) )
+     {
+     avi_add_index_entry(AVI,(unsigned char *) data,0,lseek(AVI->fdes,0,SEEK_CUR)-8,n);
          }
-	 
+
          lseek(AVI->fdes,PAD_EVEN(n),SEEK_CUR);
       }
       idx_type = 1;
@@ -1404,12 +1404,12 @@ int avi_parse_input_file(avi_t *AVI, int getIndex)
    for(j=0; j<AVI->anum; ++j) nai[j] = 0;
 
    for(i=0;i<AVI->n_idx;i++) {
-     
+
      if(strncasecmp((char *)AVI->idx[i],(char *) AVI->video_tag,3) == 0) nvi++;
-     
+
      for(j=0; j<AVI->anum; ++j) if(strncasecmp((char *)AVI->idx[i], AVI->track[j].audio_tag,4) == 0) nai[j]++;
    }
-   
+
    AVI->video_frames = nvi;
    for(j=0; j<AVI->anum; ++j) AVI->track[j].audio_chunks = nai[j];
 
@@ -1418,19 +1418,19 @@ int avi_parse_input_file(avi_t *AVI, int getIndex)
    if(AVI->video_frames==0) ERR_EXIT(AVI_ERR_NO_VIDS);
    AVI->video_index = (video_index_entry *) malloc(nvi*sizeof(video_index_entry));
    if(AVI->video_index==0) ERR_EXIT(AVI_ERR_NO_MEM);
-   
+
    for(j=0; j<AVI->anum; ++j) {
        if(AVI->track[j].audio_chunks) {
-	   AVI->track[j].audio_index = (audio_index_entry *) malloc(nai[j]*sizeof(audio_index_entry));
-	   if(AVI->track[j].audio_index==0) ERR_EXIT(AVI_ERR_NO_MEM);
+     AVI->track[j].audio_index = (audio_index_entry *) malloc(nai[j]*sizeof(audio_index_entry));
+     if(AVI->track[j].audio_index==0) ERR_EXIT(AVI_ERR_NO_MEM);
        }
-   }   
-   
+   }
+
    nvi = 0;
    for(j=0; j<AVI->anum; ++j) nai[j] = tot[j] = 0;
-   
+
    ioff = idx_type == 1 ? 8 : AVI->movi_start+4;
-   
+
    for(i=0;i<AVI->n_idx;i++) {
 
      //video
@@ -1440,25 +1440,25 @@ int avi_parse_input_file(avi_t *AVI, int getIndex)
        AVI->video_index[nvi].len = str2ulong(AVI->idx[i]+12);
        nvi++;
      }
-     
+
      //audio
      for(j=0; j<AVI->anum; ++j) {
-	 
+
        if(strncasecmp((char *)AVI->idx[i],AVI->track[j].audio_tag,4) == 0) {
-	 AVI->track[j].audio_index[nai[j]].pos = str2ulong(AVI->idx[i]+ 8)+ioff;
-	 AVI->track[j].audio_index[nai[j]].len = str2ulong(AVI->idx[i]+12);
-	 AVI->track[j].audio_index[nai[j]].tot = tot[j];
-	 tot[j] += AVI->track[j].audio_index[nai[j]].len;
-	 nai[j]++;
+   AVI->track[j].audio_index[nai[j]].pos = str2ulong(AVI->idx[i]+ 8)+ioff;
+   AVI->track[j].audio_index[nai[j]].len = str2ulong(AVI->idx[i]+12);
+   AVI->track[j].audio_index[nai[j]].tot = tot[j];
+   tot[j] += AVI->track[j].audio_index[nai[j]].len;
+   nai[j]++;
        }
      }
    }
-   
-   
+
+
    for(j=0; j<AVI->anum; ++j) AVI->track[j].audio_bytes = tot[j];
-   
+
    /* Reposition the file */
-   
+
    lseek(AVI->fdes,AVI->movi_start,SEEK_SET);
    AVI->video_pos = 0;
 
@@ -1488,7 +1488,7 @@ char* AVI_video_compressor(avi_t *AVI)
 
 long AVI_max_video_chunk(avi_t *AVI)
 {
-   return AVI->max_len; 
+   return AVI->max_len;
 }
 
 int AVI_audio_tracks(avi_t *AVI)
@@ -1564,7 +1564,7 @@ long AVI_audio_size(avi_t *AVI, long frame)
 {
   if(AVI->mode==AVI_MODE_WRITE) { AVI_errno = AVI_ERR_NOT_PERM; return -1; }
   if(!AVI->track[AVI->aptr].audio_index)         { AVI_errno = AVI_ERR_NO_IDX;   return -1; }
-  
+
   if(frame < 0 || frame >= AVI->track[AVI->aptr].audio_chunks) return 0;
   return(AVI->track[AVI->aptr].audio_index[frame].len);
 }
@@ -1605,7 +1605,7 @@ int AVI_set_audio_bitrate(avi_t *AVI, long bitrate)
    AVI->track[AVI->aptr].mp3rate = bitrate;
    return 0;
 }
-      
+
 
 long AVI_read_frame(avi_t *AVI, char *vidbuf, int *keyframe)
 {
@@ -1719,7 +1719,7 @@ int AVI_read_data(avi_t *AVI, char *vidbuf, long max_vidbuf,
 
    int n;
    char data[8];
- 
+
    if(AVI->mode==AVI_MODE_WRITE) return 0;
 
    while(1)
@@ -1836,4 +1836,3 @@ uint64_t AVI_max_size()
 {
   return((uint64_t) AVI_MAX_LEN);
 }
-

+ 41 - 41
opencl/leukocyte/OpenCL/avilib.h

@@ -1,27 +1,27 @@
 /*
  *  avilib.h
  *
- *  Copyright (C) Thomas Östreich - June 2001
- *  multiple audio track support Copyright (C) 2002 Thomas Östreich
+ *  Copyright (C) Thomas �streich - June 2001
+ *  multiple audio track support Copyright (C) 2002 Thomas �streich
  *
  *  Original code:
- *  Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de> 
+ *  Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
  *
  *  This file is part of transcode, a linux video stream processing tool
- *      
+ *
  *  transcode is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2, or (at your option)
  *  any later version.
- *   
+ *
  *  transcode is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
- *   
+ *
  *  You should have received a copy of the GNU General Public License
  *  along with GNU Make; see the file COPYING.  If not, write to
- *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
+ *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  *
  */
 
@@ -71,9 +71,9 @@ typedef struct track_s
     char   audio_tag[4];      /* Tag of audio data */
     long   audio_posc;        /* Audio position: chunk */
     long   audio_posb;        /* Audio position: byte within chunk */
- 
-    long  a_codech_off;       /* absolut offset of audio codec information */ 
-    long  a_codecf_off;       /* absolut offset of audio codec information */ 
+
+    long  a_codech_off;       /* absolut offset of audio codec information */
+    long  a_codecf_off;       /* absolut offset of audio codec information */
 
     audio_index_entry *audio_index;
 
@@ -81,10 +81,10 @@ typedef struct track_s
 
 typedef struct
 {
-  
+
   long   fdes;              /* File descriptor of AVI file */
   long   mode;              /* 0 for reading, 1 for writing */
-  
+
   long   width;             /* Width  of a video frame */
   long   height;            /* Height of a video frame */
   double fps;               /* Frames per second */
@@ -94,30 +94,30 @@ typedef struct
   long   video_frames;      /* Number of video frames */
   char   video_tag[4];      /* Tag of video data */
   long   video_pos;         /* Number of next frame to be read
-			       (if index present) */
-  
+         (if index present) */
+
   unsigned long max_len;    /* maximum video chunk present */
-  
+
   track_t track[AVI_MAX_TRACKS];  // up to AVI_MAX_TRACKS audio tracks supported
-  
+
   unsigned long pos;        /* position in file */
   long   n_idx;             /* number of index entries actually filled */
   long   max_idx;           /* number of index entries actually allocated */
-  
-  long  v_codech_off;       /* absolut offset of video codec (strh) info */ 
-  long  v_codecf_off;       /* absolut offset of video codec (strf) info */ 
-  
+
+  long  v_codech_off;       /* absolut offset of video codec (strh) info */
+  long  v_codecf_off;       /* absolut offset of video codec (strf) info */
+
   unsigned char (*idx)[16]; /* index entries (AVI idx1 tag) */
   video_index_entry *video_index;
-  
+
   unsigned long last_pos;          /* Position of last frame written */
   unsigned long last_len;          /* Length of last frame written */
   int must_use_index;              /* Flag if frames are duplicated */
   unsigned long   movi_start;
-  
-  int anum;            // total number of audio tracks 
-  int aptr;            // current audio working track 
-  
+
+  int anum;            // total number of audio tracks
+  int aptr;            // current audio working track
+
 } avi_t;
 
 #define AVI_MODE_WRITE  0
@@ -257,7 +257,7 @@ int AVI_get_audio_track(avi_t *AVI);
 int AVI_audio_tracks(avi_t *AVI);
 
 
-struct riff_struct 
+struct riff_struct
 {
   unsigned char id[4];   /* RIFF */
   unsigned long len;
@@ -265,28 +265,28 @@ struct riff_struct
 };
 
 
-struct chunk_struct 
+struct chunk_struct
 {
-	unsigned char id[4];
-	unsigned long len;
+  unsigned char id[4];
+  unsigned long len;
 };
 
-struct common_struct 
+struct common_struct
 {
-	unsigned short wFormatTag;
-	unsigned short wChannels;
-	unsigned long dwSamplesPerSec;
-	unsigned long dwAvgBytesPerSec;
-	unsigned short wBlockAlign;
-	unsigned short wBitsPerSample;  /* Only for PCM */
+  unsigned short wFormatTag;
+  unsigned short wChannels;
+  unsigned long dwSamplesPerSec;
+  unsigned long dwAvgBytesPerSec;
+  unsigned short wBlockAlign;
+  unsigned short wBitsPerSample;  /* Only for PCM */
 };
 
-struct wave_header 
+struct wave_header
 {
-	struct riff_struct   riff;
-	struct chunk_struct  format;
-	struct common_struct common;
-	struct chunk_struct  data;
+  struct riff_struct   riff;
+  struct chunk_struct  format;
+  struct common_struct common;
+  struct chunk_struct  data;
 };
 
 

+ 589 - 589
opencl/leukocyte/OpenCL/find_ellipse.c

@@ -17,9 +17,9 @@ extern MAT * m_inverse(MAT * A, MAT * out);
 
 // Returns the current system time in microseconds
 long long get_time() {
-	struct timeval tv;
-	gettimeofday(&tv, NULL);
-	return (tv.tv_sec * 1000000) + tv.tv_usec;
+  struct timeval tv;
+  gettimeofday(&tv, NULL);
+  return (tv.tv_sec * 1000000) + tv.tv_usec;
 }
 
 
@@ -28,184 +28,184 @@ long long get_time() {
 //  (hardcoded to the boundaries of the blood vessel in the test video)
 // If scaled == true, all values are scaled to the range [0.0, 1.0]
 MAT * get_frame(avi_t *cell_file, int frame_num, int cropped, int scaled) {
-	int dummy;
-	int width = AVI_video_width(cell_file);
-	int height = AVI_video_height(cell_file);
-	unsigned char *image_buf = (unsigned char *) malloc(width * height);
-
-	// There are 600 frames in this file (i.e. frame_num = 600 causes an error)
-	AVI_set_video_position(cell_file, frame_num);
-
-	//Read in the frame from the AVI
-	if(AVI_read_frame(cell_file, (char *)image_buf, &dummy) == -1) {
-		AVI_print_error("Error with AVI_read_frame");
-		exit(-1);
-	}
-
-	// The image is read in upside-down, so we need to flip it
-	MAT * image_chopped;
-	if (cropped) {
-		// Crop and flip image so we deal only with the interior of the vein
-		image_chopped = chop_flip_image(image_buf, height, width, TOP, BOTTOM, 0, width - 1, scaled);
-	} else {
-		// Just flip the image
-		image_chopped = chop_flip_image(image_buf, height, width, 0, height - 1, 0, width - 1, scaled);
-	}
-	
-	free(image_buf);
-	
-	return image_chopped;
+  int dummy;
+  int width = AVI_video_width(cell_file);
+  int height = AVI_video_height(cell_file);
+  unsigned char *image_buf = (unsigned char *) malloc(width * height);
+
+  // There are 600 frames in this file (i.e. frame_num = 600 causes an error)
+  AVI_set_video_position(cell_file, frame_num);
+
+  //Read in the frame from the AVI
+  if(AVI_read_frame(cell_file, (char *)image_buf, &dummy) == -1) {
+  AVI_print_error("Error with AVI_read_frame");
+  exit(-1);
+  }
+
+  // The image is read in upside-down, so we need to flip it
+  MAT * image_chopped;
+  if (cropped) {
+  // Crop and flip image so we deal only with the interior of the vein
+  image_chopped = chop_flip_image(image_buf, height, width, TOP, BOTTOM, 0, width - 1, scaled);
+  } else {
+  // Just flip the image
+  image_chopped = chop_flip_image(image_buf, height, width, 0, height - 1, 0, width - 1, scaled);
+  }
+
+  free(image_buf);
+
+  return image_chopped;
 }
 
 
 // Flips the specified image and crops it to the specified dimensions
 // If scaled == true, all values are scaled to the range [0.0, 1.0
 MAT * chop_flip_image(unsigned char *image, int height, int width, int top, int bottom, int left, int right, int scaled) {
-	MAT * result = m_get(bottom - top + 1, right - left + 1);
-	int i, j;
-	if (scaled) {
-		double scale = 1.0 / 255.0;
-		for(i = 0; i <= (bottom - top); i++)
-			for(j = 0; j <= (right - left); j++)
-				//m_set_val(result, i, j, (double) image[((height - (i + top)) * width) + (j + left)] * scale);
-				  m_set_val(result, i, j, (double) image[((height - 1 - (i + top)) * width) + (j + left)] * scale);
-	} else {
-		for(i = 0; i <= (bottom - top); i++)
-			for(j = 0; j <= (right - left); j++)
-				//m_set_val(result, i, j, (double) image[((height - (i + top)) * width) + (j + left)]);
-				  m_set_val(result, i, j, (double) image[((height - 1 - (i + top)) * width) + (j + left)]);
-	}
-
-	return result;
+  MAT * result = m_get(bottom - top + 1, right - left + 1);
+  int i, j;
+  if (scaled) {
+  double scale = 1.0 / 255.0;
+  for(i = 0; i <= (bottom - top); i++)
+  for(j = 0; j <= (right - left); j++)
+  //m_set_val(result, i, j, (double) image[((height - (i + top)) * width) + (j + left)] * scale);
+    m_set_val(result, i, j, (double) image[((height - 1 - (i + top)) * width) + (j + left)] * scale);
+  } else {
+  for(i = 0; i <= (bottom - top); i++)
+  for(j = 0; j <= (right - left); j++)
+  //m_set_val(result, i, j, (double) image[((height - (i + top)) * width) + (j + left)]);
+    m_set_val(result, i, j, (double) image[((height - 1 - (i + top)) * width) + (j + left)]);
+  }
+
+  return result;
 }
 
 
 // Chooses the best GPU on the current machine
 void choose_GPU(int platform_id, int device_id, int use_gpu) {
-	select_device(platform_id, device_id, use_gpu);
+  select_device(platform_id, device_id, use_gpu);
 }
 
 
 // Computes and then transfers to the GPU all of the
 //  constant matrices required by the GPU kernels
 void compute_constants() {
-	// Compute memory sizes
-	int strel_m = 12 * 2 + 1;
-	int strel_n = 12 * 2 + 1;
-	
-	int n, k;
-	// Compute the sine and cosine of the angle to each point in each sample circle
-	//  (which are the same across all sample circles)
-	float host_sin_angle[NPOINTS], host_cos_angle[NPOINTS], theta[NPOINTS];
-	for(n = 0; n < NPOINTS; n++) {
-		theta[n] = (((double) n) * 2.0 * PI) / ((double) NPOINTS);
-		host_sin_angle[n] = sin(theta[n]);
-		host_cos_angle[n] = cos(theta[n]);
-	}
-
-	// Compute the (x,y) pixel offsets of each sample point in each sample circle
-	int host_tX[NCIRCLES * NPOINTS], host_tY[NCIRCLES * NPOINTS];
-	for (k = 0; k < NCIRCLES; k++) {
-		double rad = (double) (MIN_RAD + (2 * k)); 
-		for (n = 0; n < NPOINTS; n++) {
-			host_tX[(k * NPOINTS) + n] = (int)(cos(theta[n]) * rad);
-			host_tY[(k * NPOINTS) + n] = (int)(sin(theta[n]) * rad);
-		}
-	}
-	
-	// Compute the structuring element used in dilation
-	float *host_strel = structuring_element(12);
-	
-	// Transfer the computed matrices to the GPU
-	transfer_constants(host_sin_angle, host_cos_angle, host_tX, host_tY, strel_m, strel_n, host_strel);
-	
-	// Free the memory (only need to free strel since the rest are declared statically)
-	free(host_strel);
+  // Compute memory sizes
+  int strel_m = 12 * 2 + 1;
+  int strel_n = 12 * 2 + 1;
+
+  int n, k;
+  // Compute the sine and cosine of the angle to each point in each sample circle
+  //  (which are the same across all sample circles)
+  float host_sin_angle[NPOINTS], host_cos_angle[NPOINTS], theta[NPOINTS];
+  for(n = 0; n < NPOINTS; n++) {
+  theta[n] = (((double) n) * 2.0 * PI) / ((double) NPOINTS);
+  host_sin_angle[n] = sin(theta[n]);
+  host_cos_angle[n] = cos(theta[n]);
+  }
+
+  // Compute the (x,y) pixel offsets of each sample point in each sample circle
+  int host_tX[NCIRCLES * NPOINTS], host_tY[NCIRCLES * NPOINTS];
+  for (k = 0; k < NCIRCLES; k++) {
+  double rad = (double) (MIN_RAD + (2 * k));
+  for (n = 0; n < NPOINTS; n++) {
+  host_tX[(k * NPOINTS) + n] = (int)(cos(theta[n]) * rad);
+  host_tY[(k * NPOINTS) + n] = (int)(sin(theta[n]) * rad);
+  }
+  }
+
+  // Compute the structuring element used in dilation
+  float *host_strel = structuring_element(12);
+
+  // Transfer the computed matrices to the GPU
+  transfer_constants(host_sin_angle, host_cos_angle, host_tX, host_tY, strel_m, strel_n, host_strel);
+
+  // Free the memory (only need to free strel since the rest are declared statically)
+  free(host_strel);
 }
 
 
 // Given x- and y-gradients of a video frame, computes the GICOV
 //  score for each sample ellipse at every pixel in the frame
 MAT *GICOV(MAT *grad_x, MAT *grad_y) {
-	// Determine the dimensions of the frame
-	int grad_m = grad_x->m;
-	int grad_n = grad_y->n;
-	
-	// Allocate host memory for grad_x and grad_y
-	unsigned int grad_mem_size = sizeof(float) * grad_m * grad_n;
-	float *host_grad_x = (float*) malloc(grad_mem_size);
-	float *host_grad_y = (float*) malloc(grad_mem_size);
-	// initalize float versions of grad_x and grad_y
-	int m, n;
-	for (m = 0; m < grad_m; m++) {
-		for (n = 0; n < grad_n; n++) {
-			host_grad_x[(n * grad_m) + m] = (float) m_get_val(grad_x, m, n);
-			host_grad_y[(n * grad_m) + m] = (float) m_get_val(grad_y, m, n);
-		}
-	}
-
-	// Offload the GICOV score computation to the GPU
-	float *host_gicov = GICOV_OpenCL(grad_m, grad_n, host_grad_x, host_grad_y);
-
-	// Copy the results into a new host matrix
-	MAT *gicov = m_get(grad_m, grad_n);
-	for (m = 0; m < grad_m; m++)
-		for (n = 0; n < grad_n; n++)
-			m_set_val(gicov, m, n, host_gicov[(n * grad_m) + m]);
-
-	// Cleanup memory
-	free(host_grad_x);
-	free(host_grad_y);
-	/* free(host_gicov); */
-
-	return gicov;
+  // Determine the dimensions of the frame
+  int grad_m = grad_x->m;
+  int grad_n = grad_y->n;
+
+  // Allocate host memory for grad_x and grad_y
+  unsigned int grad_mem_size = sizeof(float) * grad_m * grad_n;
+  float *host_grad_x = (float*) malloc(grad_mem_size);
+  float *host_grad_y = (float*) malloc(grad_mem_size);
+  // initalize float versions of grad_x and grad_y
+  int m, n;
+  for (m = 0; m < grad_m; m++) {
+  for (n = 0; n < grad_n; n++) {
+  host_grad_x[(n * grad_m) + m] = (float) m_get_val(grad_x, m, n);
+  host_grad_y[(n * grad_m) + m] = (float) m_get_val(grad_y, m, n);
+  }
+  }
+
+  // Offload the GICOV score computation to the GPU
+  float *host_gicov = GICOV_OpenCL(grad_m, grad_n, host_grad_x, host_grad_y);
+
+  // Copy the results into a new host matrix
+  MAT *gicov = m_get(grad_m, grad_n);
+  for (m = 0; m < grad_m; m++)
+  for (n = 0; n < grad_n; n++)
+  m_set_val(gicov, m, n, host_gicov[(n * grad_m) + m]);
+
+  // Cleanup memory
+  free(host_grad_x);
+  free(host_grad_y);
+  /* free(host_gicov); */
+
+  return gicov;
 }
 
 
 // Returns a circular structuring element of the specified radius
 float * structuring_element(int radius) {
-	int m = radius*2+1;
-	int n = radius*2+1;
-	float *result = (float *) malloc(sizeof(float) * m * n);
-	
-	int i, j;
-	for (i = 0; i < m; i++) {
-		for (j = 0; j < n; j++) {
-			if (sqrt((float)((i-radius)*(i-radius)+(j-radius)*(j-radius))) <= radius)
-				result[(i * m) + j] = 1.0;
-			else
-				result[(i * m) + j] = 0.0;
-		}
-	}
-
-	return result;
+  int m = radius*2+1;
+  int n = radius*2+1;
+  float *result = (float *) malloc(sizeof(float) * m * n);
+
+  int i, j;
+  for (i = 0; i < m; i++) {
+  for (j = 0; j < n; j++) {
+  if (sqrt((float)((i-radius)*(i-radius)+(j-radius)*(j-radius))) <= radius)
+  result[(i * m) + j] = 1.0;
+  else
+  result[(i * m) + j] = 0.0;
+  }
+  }
+
+  return result;
 }
 
 
 // Performs an image dilation on the specified matrix
 MAT *dilate(MAT *img_in) {
-	// Determine the dimensions of the frame
-	int max_gicov_m = img_in->m;
-	int max_gicov_n = img_in->n;
+  // Determine the dimensions of the frame
+  int max_gicov_m = img_in->m;
+  int max_gicov_n = img_in->n;
 
-	// Determine the dimensions of the structuring element
-	int strel_m = 12 * 2 + 1;
-	int strel_n = 12 * 2 + 1;
+  // Determine the dimensions of the structuring element
+  int strel_m = 12 * 2 + 1;
+  int strel_n = 12 * 2 + 1;
 
-	// Offload the dilation to the GPU
-	float *host_img_dilated = dilate_OpenCL(max_gicov_m, max_gicov_n, strel_m, strel_n);
+  // Offload the dilation to the GPU
+  float *host_img_dilated = dilate_OpenCL(max_gicov_m, max_gicov_n, strel_m, strel_n);
 
-	// Copy results into a new host matrix
-	MAT *dilated = m_get(max_gicov_m, max_gicov_n);
-	int m, n;
-	for (m = 0; m < max_gicov_m; m++)
-		for (n = 0; n < max_gicov_n; n++)
-			m_set_val(dilated, m, n, host_img_dilated[(m * max_gicov_n) + n]);
+  // Copy results into a new host matrix
+  MAT *dilated = m_get(max_gicov_m, max_gicov_n);
+  int m, n;
+  for (m = 0; m < max_gicov_m; m++)
+  for (n = 0; n < max_gicov_n; n++)
+  m_set_val(dilated, m, n, host_img_dilated[(m * max_gicov_n) + n]);
 
-	// Cleanup memory
-	free(host_img_dilated);
+  // Cleanup memory
+  free(host_img_dilated);
 
-	return dilated;
+  return dilated;
 }
 
 
@@ -214,489 +214,489 @@ MAT *dilate(MAT *img_in) {
 //Get special TMatrix
 MAT * TMatrix(unsigned int N, unsigned int M)
 {
-	MAT * B = NULL, * LB = NULL, * B_TEMP = NULL, * B_TEMP_INV = NULL, * B_RET = NULL;
-	int * aindex, * bindex, * cindex, * dindex;
-	int i, j;
-
-	aindex = (int*) malloc(N*sizeof(int));
-	bindex = (int*) malloc(N*sizeof(int));
-	cindex = (int*) malloc(N*sizeof(int));
-	dindex = (int*) malloc(N*sizeof(int));
-
-	for(i = 1; i < N; i++)
-		aindex[i] = i-1;
-	aindex[0] = N-1;
-
-	for(i = 0; i < N; i++)
-		bindex[i] = i;
-	
-	for(i = 0; i < N-1; i++)
-		cindex[i] = i+1;
-	cindex[N-1] = 0;
-
-	for(i = 0; i < N-2; i++)
-		dindex[i] = i+2;
-	dindex[N-2] = 0;
-	dindex[N-1] = 1;
-
-
-	B = m_get(N*M, N);
-	LB = m_get(M, N);
-
-	for(i = 0; i < N; i++)
-	{
-		m_zero(LB);
-		
-		for(j = 0; j < M; j++)
-		{
-			double s = (double)j / (double)M;
-			double a, b, c, d;
-
-			a = (-1.0*s*s*s + 3.0*s*s - 3.0*s + 1.0) / 6.0;
-			b = (3.0*s*s*s - 6.0*s*s + 4.0) / 6.0;
-			c = (-3.0*s*s*s + 3.0*s*s + 3.0*s + 1.0) / 6.0;
-			d = s*s*s / 6.0;
-
-			m_set_val(LB, j, aindex[i], a);
-			m_set_val(LB, j, bindex[i], b);
-			m_set_val(LB, j, cindex[i], c);
-			m_set_val(LB, j, dindex[i], d);
-		}
-		int m, n;
-
-		for(m = i*M; m < (i+1)*M; m++)
-			for(n = 0; n < N; n++)
-				m_set_val(B, m, n, m_get_val(LB, m%M, n));
-	}
-
-	B_TEMP = mtrm_mlt(B, B, B_TEMP);
-	B_TEMP_INV = m_inverse(B_TEMP, B_TEMP_INV);
-	B_RET = mmtr_mlt(B_TEMP_INV, B, B_RET);
-	
-	m_free(B);
-	m_free(LB);
-	m_free(B_TEMP);
-	m_free(B_TEMP_INV);
-
-	free(dindex);
-	free(cindex);
-	free(bindex);
-	free(aindex);
-
-	return B_RET;
+  MAT * B = NULL, * LB = NULL, * B_TEMP = NULL, * B_TEMP_INV = NULL, * B_RET = NULL;
+  int * aindex, * bindex, * cindex, * dindex;
+  int i, j;
+
+  aindex = (int*) malloc(N*sizeof(int));
+  bindex = (int*) malloc(N*sizeof(int));
+  cindex = (int*) malloc(N*sizeof(int));
+  dindex = (int*) malloc(N*sizeof(int));
+
+  for(i = 1; i < N; i++)
+  aindex[i] = i-1;
+  aindex[0] = N-1;
+
+  for(i = 0; i < N; i++)
+  bindex[i] = i;
+
+  for(i = 0; i < N-1; i++)
+  cindex[i] = i+1;
+  cindex[N-1] = 0;
+
+  for(i = 0; i < N-2; i++)
+  dindex[i] = i+2;
+  dindex[N-2] = 0;
+  dindex[N-1] = 1;
+
+
+  B = m_get(N*M, N);
+  LB = m_get(M, N);
+
+  for(i = 0; i < N; i++)
+  {
+  m_zero(LB);
+
+  for(j = 0; j < M; j++)
+  {
+  double s = (double)j / (double)M;
+  double a, b, c, d;
+
+  a = (-1.0*s*s*s + 3.0*s*s - 3.0*s + 1.0) / 6.0;
+  b = (3.0*s*s*s - 6.0*s*s + 4.0) / 6.0;
+  c = (-3.0*s*s*s + 3.0*s*s + 3.0*s + 1.0) / 6.0;
+  d = s*s*s / 6.0;
+
+  m_set_val(LB, j, aindex[i], a);
+  m_set_val(LB, j, bindex[i], b);
+  m_set_val(LB, j, cindex[i], c);
+  m_set_val(LB, j, dindex[i], d);
+  }
+  int m, n;
+
+  for(m = i*M; m < (i+1)*M; m++)
+  for(n = 0; n < N; n++)
+  m_set_val(B, m, n, m_get_val(LB, m%M, n));
+  }
+
+  B_TEMP = mtrm_mlt(B, B, B_TEMP);
+  B_TEMP_INV = m_inverse(B_TEMP, B_TEMP_INV);
+  B_RET = mmtr_mlt(B_TEMP_INV, B, B_RET);
+
+  m_free(B);
+  m_free(LB);
+  m_free(B_TEMP);
+  m_free(B_TEMP_INV);
+
+  free(dindex);
+  free(cindex);
+  free(bindex);
+  free(aindex);
+
+  return B_RET;
 }
 
 void uniformseg(VEC * cellx_row, VEC * celly_row, MAT * x, MAT * y)
 {
-	double dx[36], dy[36], dist[36], dsum[36], perm = 0.0, uperm;
-	int i, j, index[36];
-
-	for(i = 1; i <= 36; i++)
-	{
-		dx[i%36] = v_get_val(cellx_row, i%36) - v_get_val(cellx_row, (i-1)%36);
-		dy[i%36] = v_get_val(celly_row, i%36) - v_get_val(celly_row, (i-1)%36);
-		dist[i%36] = sqrt(dx[i%36]*dx[i%36] + dy[i%36]*dy[i%36]);
-		perm+= dist[i%36];
-	}
-	uperm = perm / 36.0;
-	dsum[0] = dist[0];
-	for(i = 1; i < 36; i++)
-		dsum[i] = dsum[i-1]+dist[i];
-
-	for(i = 0; i < 36; i++)
-	{
-		double minimum=DBL_MAX, temp;
-		int min_index = 0;
-		for(j = 0; j < 36; j++)
-		{
-			temp = fabs(dsum[j]- (double)i*uperm);
-			if (temp < minimum)
-			{
-				minimum = temp;
-				min_index = j;
-			}
-		}
-		index[i] = min_index;
-	}
-
-	for(i = 0; i < 36; i++)
-	{
-		m_set_val(x, 0, i, v_get_val(cellx_row, index[i]));
-		m_set_val(y, 0, i, v_get_val(celly_row, index[i]));
-	}
+  double dx[36], dy[36], dist[36], dsum[36], perm = 0.0, uperm;
+  int i, j, index[36];
+
+  for(i = 1; i <= 36; i++)
+  {
+  dx[i%36] = v_get_val(cellx_row, i%36) - v_get_val(cellx_row, (i-1)%36);
+  dy[i%36] = v_get_val(celly_row, i%36) - v_get_val(celly_row, (i-1)%36);
+  dist[i%36] = sqrt(dx[i%36]*dx[i%36] + dy[i%36]*dy[i%36]);
+  perm+= dist[i%36];
+  }
+  uperm = perm / 36.0;
+  dsum[0] = dist[0];
+  for(i = 1; i < 36; i++)
+  dsum[i] = dsum[i-1]+dist[i];
+
+  for(i = 0; i < 36; i++)
+  {
+  double minimum=DBL_MAX, temp;
+  int min_index = 0;
+  for(j = 0; j < 36; j++)
+  {
+  temp = fabs(dsum[j]- (double)i*uperm);
+  if (temp < minimum)
+  {
+  minimum = temp;
+  min_index = j;
+  }
+  }
+  index[i] = min_index;
+  }
+
+  for(i = 0; i < 36; i++)
+  {
+  m_set_val(x, 0, i, v_get_val(cellx_row, index[i]));
+  m_set_val(y, 0, i, v_get_val(celly_row, index[i]));
+  }
 }
 
 //Get minimum element in a matrix
 double m_min(MAT * m)
 {
-	int i, j;
-	double minimum = DBL_MAX, temp;
-	for(i = 0; i < m->m; i++)
-	{
-		for(j = 0; j < m->n; j++)
-		{
-			temp = m_get_val(m, i, j);
-			if(temp < minimum)
-				minimum = temp;
-		}
-	}
-	return minimum;
+  int i, j;
+  double minimum = DBL_MAX, temp;
+  for(i = 0; i < m->m; i++)
+  {
+  for(j = 0; j < m->n; j++)
+  {
+  temp = m_get_val(m, i, j);
+  if(temp < minimum)
+  minimum = temp;
+  }
+  }
+  return minimum;
 }
 
 //Get maximum element in a matrix
 double m_max(MAT * m)
 {
-	int i, j;
-	double maximum = DBL_MIN, temp;
-	for(i = 0; i < m->m; i++)
-	{
-		for(j = 0; j < m->n; j++)
-		{
-			temp = m_get_val(m, i, j);
-			if(temp > maximum)
-				maximum = temp;
-		}
-	}
-	return maximum;
+  int i, j;
+  double maximum = DBL_MIN, temp;
+  for(i = 0; i < m->m; i++)
+  {
+  for(j = 0; j < m->n; j++)
+  {
+  temp = m_get_val(m, i, j);
+  if(temp > maximum)
+  maximum = temp;
+  }
+  }
+  return maximum;
 }
 
 VEC * getsampling(MAT * m, int ns)
 {
-	int N = m->n > m->m ? m-> n:m->m, M = ns;
-	int * aindex, * bindex, * cindex, * dindex;
-	int i, j;
-	VEC * retval = v_get(N*M);
-
-	aindex = (int*) malloc(N*sizeof(int));
-	bindex = (int*) malloc(N*sizeof(int));
-	cindex = (int*) malloc(N*sizeof(int));
-	dindex = (int*) malloc(N*sizeof(int));
-
-	for(i = 1; i < N; i++)
-		aindex[i] = i-1;
-	aindex[0] = N-1;
-
-	for(i = 0; i < N; i++)
-		bindex[i] = i;
-	
-	for(i = 0; i < N-1; i++)
-		cindex[i] = i+1;
-	cindex[N-1] = 0;
-
-	for(i = 0; i < N-2; i++)
-		dindex[i] = i+2;
-	dindex[N-2] = 0;
-	dindex[N-1] = 1;
-
-	for(i = 0; i < N; i++)
-	{
-		for(j = 0; j < M; j++)
-		{
-			double s = (double)j / (double)M;
-			double a, b, c, d;
-
-			a = m_get_val(m, 0, aindex[i]) * (-1.0*s*s*s + 3.0*s*s - 3.0*s + 1.0);
-			b = m_get_val(m, 0, bindex[i]) * (3.0*s*s*s - 6.0*s*s + 4.0);
-			c = m_get_val(m, 0, cindex[i]) * (-3.0*s*s*s + 3.0*s*s + 3.0*s + 1.0);
-			d = m_get_val(m, 0, dindex[i]) * s*s*s;
-			v_set_val(retval, i*M+j,(a+b+c+d)/6.0);
-
-		}
-	}
-
-	free(dindex);
-	free(cindex);
-	free(bindex);
-	free(aindex);
-
-	return retval;
+  int N = m->n > m->m ? m-> n:m->m, M = ns;
+  int * aindex, * bindex, * cindex, * dindex;
+  int i, j;
+  VEC * retval = v_get(N*M);
+
+  aindex = (int*) malloc(N*sizeof(int));
+  bindex = (int*) malloc(N*sizeof(int));
+  cindex = (int*) malloc(N*sizeof(int));
+  dindex = (int*) malloc(N*sizeof(int));
+
+  for(i = 1; i < N; i++)
+  aindex[i] = i-1;
+  aindex[0] = N-1;
+
+  for(i = 0; i < N; i++)
+  bindex[i] = i;
+
+  for(i = 0; i < N-1; i++)
+  cindex[i] = i+1;
+  cindex[N-1] = 0;
+
+  for(i = 0; i < N-2; i++)
+  dindex[i] = i+2;
+  dindex[N-2] = 0;
+  dindex[N-1] = 1;
+
+  for(i = 0; i < N; i++)
+  {
+  for(j = 0; j < M; j++)
+  {
+  double s = (double)j / (double)M;
+  double a, b, c, d;
+
+  a = m_get_val(m, 0, aindex[i]) * (-1.0*s*s*s + 3.0*s*s - 3.0*s + 1.0);
+  b = m_get_val(m, 0, bindex[i]) * (3.0*s*s*s - 6.0*s*s + 4.0);
+  c = m_get_val(m, 0, cindex[i]) * (-3.0*s*s*s + 3.0*s*s + 3.0*s + 1.0);
+  d = m_get_val(m, 0, dindex[i]) * s*s*s;
+  v_set_val(retval, i*M+j,(a+b+c+d)/6.0);
+
+  }
+  }
+
+  free(dindex);
+  free(cindex);
+  free(bindex);
+  free(aindex);
+
+  return retval;
 }
 
 VEC * getfdriv(MAT * m, int ns)
 {
-	int N = m->n > m->m ? m-> n:m->m, M = ns;
-	int * aindex, * bindex, * cindex, * dindex;
-	int i, j;
-	VEC * retval = v_get(N*M);
-
-	aindex = (int*) malloc(N*sizeof(int));
-	bindex = (int*) malloc(N*sizeof(int));
-	cindex = (int*) malloc(N*sizeof(int));
-	dindex = (int*) malloc(N*sizeof(int));
-
-	for(i = 1; i < N; i++)
-		aindex[i] = i-1;
-	aindex[0] = N-1;
-
-	for(i = 0; i < N; i++)
-		bindex[i] = i;
-	
-	for(i = 0; i < N-1; i++)
-		cindex[i] = i+1;
-	cindex[N-1] = 0;
-
-	for(i = 0; i < N-2; i++)
-		dindex[i] = i+2;
-	dindex[N-2] = 0;
-	dindex[N-1] = 1;
-
-	for(i = 0; i < N; i++)
-	{
-		for(j = 0; j < M; j++)
-		{
-			double s = (double)j / (double)M;
-			double a, b, c, d;
-
-			a = m_get_val(m, 0, aindex[i]) * (-3.0*s*s + 6.0*s - 3.0);
-			b = m_get_val(m, 0, bindex[i]) * (9.0*s*s - 12.0*s);
-			c = m_get_val(m, 0, cindex[i]) * (-9.0*s*s + 6.0*s + 3.0);
-			d = m_get_val(m, 0, dindex[i]) * (3.0 *s*s);
-			v_set_val(retval, i*M+j, (a+b+c+d)/6.0);
-
-		}
-	}
-
-	free(dindex);
-	free(cindex);
-	free(bindex);
-	free(aindex);
-
-	return retval;
+  int N = m->n > m->m ? m-> n:m->m, M = ns;
+  int * aindex, * bindex, * cindex, * dindex;
+  int i, j;
+  VEC * retval = v_get(N*M);
+
+  aindex = (int*) malloc(N*sizeof(int));
+  bindex = (int*) malloc(N*sizeof(int));
+  cindex = (int*) malloc(N*sizeof(int));
+  dindex = (int*) malloc(N*sizeof(int));
+
+  for(i = 1; i < N; i++)
+  aindex[i] = i-1;
+  aindex[0] = N-1;
+
+  for(i = 0; i < N; i++)
+  bindex[i] = i;
+
+  for(i = 0; i < N-1; i++)
+  cindex[i] = i+1;
+  cindex[N-1] = 0;
+
+  for(i = 0; i < N-2; i++)
+  dindex[i] = i+2;
+  dindex[N-2] = 0;
+  dindex[N-1] = 1;
+
+  for(i = 0; i < N; i++)
+  {
+  for(j = 0; j < M; j++)
+  {
+  double s = (double)j / (double)M;
+  double a, b, c, d;
+
+  a = m_get_val(m, 0, aindex[i]) * (-3.0*s*s + 6.0*s - 3.0);
+  b = m_get_val(m, 0, bindex[i]) * (9.0*s*s - 12.0*s);
+  c = m_get_val(m, 0, cindex[i]) * (-9.0*s*s + 6.0*s + 3.0);
+  d = m_get_val(m, 0, dindex[i]) * (3.0 *s*s);
+  v_set_val(retval, i*M+j, (a+b+c+d)/6.0);
+
+  }
+  }
+
+  free(dindex);
+  free(cindex);
+  free(bindex);
+  free(aindex);
+
+  return retval;
 }
 
 //Performs bilinear interpolation, getting the values of m specified in the vectors X and Y
 MAT * linear_interp2(MAT * m, VEC * X, VEC * Y)
 {
-	//Kind of assumes X and Y have same len!
+  //Kind of assumes X and Y have same len!
 
-	MAT * retval = m_get(1, X->dim);
-	double x_coord, y_coord, new_val, a, b;
-	int l, k, i;
+  MAT * retval = m_get(1, X->dim);
+  double x_coord, y_coord, new_val, a, b;
+  int l, k, i;
 
-	for(i = 0; i < X->dim; i++)
-	{
-		x_coord = v_get_val(X, i);
-		y_coord = v_get_val(Y, i);
+  for(i = 0; i < X->dim; i++)
+  {
+  x_coord = v_get_val(X, i);
+  y_coord = v_get_val(Y, i);
 
-		l = (int)x_coord;
-		k = (int)y_coord;
+  l = (int)x_coord;
+  k = (int)y_coord;
 
-		a = x_coord - (double)l;
-		b = y_coord - (double)k;
+  a = x_coord - (double)l;
+  b = y_coord - (double)k;
 
-		//printf("xc: %f \t yc: %f \t i: %d \t l: %d \t k: %d \t a: %f \t b: %f\n", x_coord, y_coord, i, l, k, a, b);
+  //printf("xc: %f \t yc: %f \t i: %d \t l: %d \t k: %d \t a: %f \t b: %f\n", x_coord, y_coord, i, l, k, a, b);
 
-		new_val = (1.0-a)*(1.0-b)*m_get_val(m, k, l) +
-				  a*(1.0-b)*m_get_val(m, k, l+1) +
-				  (1.0-a)*b*m_get_val(m, k+1, l) +
-				  a*b*m_get_val(m, k+1, l+1);
+  new_val = (1.0-a)*(1.0-b)*m_get_val(m, k, l) +
+    a*(1.0-b)*m_get_val(m, k, l+1) +
+    (1.0-a)*b*m_get_val(m, k+1, l) +
+    a*b*m_get_val(m, k+1, l+1);
 
-		m_set_val(retval, 0, i, new_val);
-	}
+  m_set_val(retval, 0, i, new_val);
+  }
 
-	return retval;
+  return retval;
 }
 
 void splineenergyform01(MAT * Cx, MAT * Cy, MAT * Ix, MAT * Iy, int ns, double delta, double dt, int typeofcell)
 {
-	VEC * X, * Y, * Xs, * Ys, * Nx, * Ny, * X1, * Y1, * X2, * Y2, *	XY, * XX, * YY, * dCx, * dCy, * Ix1, * Ix2, *Iy1, *Iy2;
-	MAT * Ix1_mat, * Ix2_mat, * Iy1_mat, * Iy2_mat;
-	int i,j, N, * aindex, * bindex, * cindex, * dindex;
-
-	X = getsampling(Cx, ns);
-	Y = getsampling(Cy, ns);
-	Xs = getfdriv(Cx, ns);
-	Ys = getfdriv(Cy, ns);
-
-	Nx = v_get(Ys->dim);
-	for(i = 0; i < Nx->dim; i++)
-		v_set_val(Nx, i, v_get_val(Ys, i) / sqrt(v_get_val(Xs, i)*v_get_val(Xs, i) + v_get_val(Ys, i)*v_get_val(Ys, i)));
-
-	Ny = v_get(Xs->dim);
-	for(i = 0; i < Ny->dim; i++)
-		v_set_val(Ny, i, -1.0 * v_get_val(Xs, i) / sqrt(v_get_val(Xs, i)*v_get_val(Xs, i) + v_get_val(Ys, i)*v_get_val(Ys, i)));
-	
-	X1 = v_get(Nx->dim);
-	for(i = 0; i < X1->dim; i++)
-		v_set_val(X1, i, v_get_val(X, i) + delta*v_get_val(Nx, i));
-
-	Y1 = v_get(Ny->dim);
-	for(i = 0; i < Y1->dim; i++)
-		v_set_val(Y1, i, v_get_val(Y, i) + delta*v_get_val(Ny, i));
-
-	X2 = v_get(Nx->dim);
-	for(i = 0; i < X2->dim; i++)
-		v_set_val(X2, i, v_get_val(X, i) - delta*v_get_val(Nx, i));
-
-	Y2 = v_get(Ny->dim);
-	for(i = 0; i < Y2->dim; i++)
-		v_set_val(Y2, i, v_get_val(Y, i) + delta*v_get_val(Ny, i));
-
-	// seg fault happens at this func call
-	Ix1_mat = linear_interp2(Ix, X1, Y1);
-	Iy1_mat = linear_interp2(Iy, X1, Y1);
-	Ix2_mat = linear_interp2(Ix, X2, Y2);
-	Iy2_mat = linear_interp2(Iy, X2, Y2);
-
-	Ix1 = v_get(Ix1_mat->n);
-	Iy1 = v_get(Iy1_mat->n);
-	Ix2 = v_get(Ix2_mat->n);
-	Iy2 = v_get(Iy2_mat->n);
-
-	Ix1 = get_row(Ix1_mat, 0, Ix1);
-	Iy1 = get_row(Iy1_mat, 0, Iy1);
-	Ix2 = get_row(Ix2_mat, 0, Ix2);
-	Iy2 = get_row(Iy2_mat, 0, Iy2);
-
-	N = Cx->m;
-
-	//VEC * retval = v_get(N*ns);
-
-	aindex = (int*) malloc(N*sizeof(int));
-	bindex = (int*) malloc(N*sizeof(int));
-	cindex = (int*) malloc(N*sizeof(int));
-	dindex = (int*) malloc(N*sizeof(int));
-
-	for(i = 1; i < N; i++)
-		aindex[i] = i-1;
-	aindex[0] = N-1;
-
-	for(i = 0; i < N; i++)
-		bindex[i] = i;
-	
-	for(i = 0; i < N-1; i++)
-		cindex[i] = i+1;
-	cindex[N-1] = 0;
-
-	for(i = 0; i < N-2; i++)
-		dindex[i] = i+2;
-	dindex[N-2] = 0;
-	dindex[N-1] = 1;
-
-	XY = v_get(Xs->dim);
-	for(i = 0; i < Xs->dim; i++)
-		v_set_val(XY, i, v_get_val(Xs, i) * v_get_val(Ys, i));
-
-	XX = v_get(Xs->dim);
-	for(i = 0; i < Xs->dim; i++)
-		v_set_val(XX, i, v_get_val(Xs, i) * v_get_val(Xs, i));
-
-	YY = v_get(Ys->dim);
-	for(i = 0; i < Xs->dim; i++)
-		v_set_val(YY, i, v_get_val(Ys, i) * v_get_val(Ys, i));
-
-	dCx = v_get(Cx->m);
-	dCy = v_get(Cy->m);
-
-	//get control points for splines
-	for(i = 0; i < Cx->m; i++)
-	{
-		for(j = 0; j < ns; j++)
-		{
-			double s = (double)j / (double)ns;
-			double A1, A2, A3, A4, B1, B2, B3, B4, D, D_3, Tx1, Tx2, Tx3, Tx4, Ty1, Ty2, Ty3, Ty4;
-			int k;
-
-			A1 = (-1.0*(s-1.0)*(s-1.0)*(s-1.0)) / 6.0;
-			A2 = (3.0*s*s*s - 6.0*s*s + 4.0) / 6.0;
-			A3 = (-3.0*s*s*s + 3.0*s*s + 3.0*s + 1.0) / 6.0;
-			A4 = s*s*s / 6.0;
-
-			B1 = (-3.0*s*s + 6.0*s - 3.0) / 6.0;
-			B2 = (9.0*s*s - 12.0*s) / 6.0;
-			B3 = (-9.0*s*s + 6.0*s + 3.0) / 6.0;
-			B4 = 3.0*s*s / 6.0;
-
-			k = i*ns+j;
-			D = sqrt(v_get_val(Xs, k)*v_get_val(Xs, k) + v_get_val(Ys, k)*v_get_val(Ys, k));
-			D_3 = D*D*D;
-			
-			//1st control point
-			
-			Tx1 = A1 - delta * v_get_val(XY, k) * B1 / D_3;
-			Tx2 = -1.0 * delta*(B1/D - v_get_val(XX, k)*B1/D_3);
-			Tx3 = A1 + delta * v_get_val(XY, k) * B1 / D_3;
-			Tx4 = delta*(B1/D - v_get_val(XX, k)*B1/D_3);
-
-			Ty1 = delta*(B1/D - v_get_val(YY, k)*B1/D_3);
-			Ty2 = A1 + delta * v_get_val(XY, k) * B1 / D_3;
-			Ty3 = -1.0 * delta*(B1/D - v_get_val(YY, k)*B1/D_3);
-			Ty4 = A1 - delta * v_get_val(XY, k) * B1 / D_3;
-
-			v_set_val(dCx, aindex[i], v_get_val(dCx, aindex[i]) + Tx1*v_get_val(Ix1, k) + Tx2*v_get_val(Iy1,k) - Tx3*v_get_val(Ix2, k) - Tx4*v_get_val(Iy2, k));
-			v_set_val(dCy, aindex[i], v_get_val(dCy, aindex[i]) + Ty1*v_get_val(Ix1, k) + Ty2*v_get_val(Iy1,k) - Ty3*v_get_val(Ix2, k) - Ty4*v_get_val(Iy2, k));
-		
-			//2nd control point
-
-			Tx1 = A2 - delta * v_get_val(XY, k) * B2 / D_3;
-			Tx2 = -1.0 * delta*(B2/D - v_get_val(XX, k)*B2/D_3);
-			Tx3 = A2 + delta * v_get_val(XY, k) * B2 / D_3;
-			Tx4 = delta*(B2/D - v_get_val(XX, k)*B2/D_3);
-
-			Ty1 = delta*(B2/D - v_get_val(YY, k)*B2/D_3);
-			Ty2 = A2 + delta * v_get_val(XY, k) * B2 / D_3;
-			Ty3 = -1.0 * delta*(B2/D - v_get_val(YY, k)*B2/D_3);
-			Ty4 = A2 - delta * v_get_val(XY, k) * B2 / D_3;
-
-			v_set_val(dCx, bindex[i], v_get_val(dCx, bindex[i]) + Tx1*v_get_val(Ix1, k) + Tx2*v_get_val(Iy1,k) - Tx3*v_get_val(Ix2, k) - Tx4*v_get_val(Iy2, k));
-			v_set_val(dCy, bindex[i], v_get_val(dCy, bindex[i]) + Ty1*v_get_val(Ix1, k) + Ty2*v_get_val(Iy1,k) - Ty3*v_get_val(Ix2, k) - Ty4*v_get_val(Iy2, k));
-
-			//3nd control point
-
-			Tx1 = A3 - delta * v_get_val(XY, k) * B3 / D_3;
-			Tx2 = -1.0 * delta*(B3/D - v_get_val(XX, k)*B3/D_3);
-			Tx3 = A3 + delta * v_get_val(XY, k) * B3 / D_3;
-			Tx4 = delta*(B3/D - v_get_val(XX, k)*B3/D_3);
-
-			Ty1 = delta*(B3/D - v_get_val(YY, k)*B3/D_3);
-			Ty2 = A3 + delta * v_get_val(XY, k) * B3 / D_3;
-			Ty3 = -1.0 * delta*(B3/D - v_get_val(YY, k)*B3/D_3);
-			Ty4 = A3 - delta * v_get_val(XY, k) * B3 / D_3;
-
-			v_set_val(dCx, cindex[i], v_get_val(dCx, cindex[i]) + Tx1*v_get_val(Ix1, k) + Tx2*v_get_val(Iy1,k) - Tx3*v_get_val(Ix2, k) - Tx4*v_get_val(Iy2, k));
-			v_set_val(dCy, cindex[i], v_get_val(dCy, cindex[i]) + Ty1*v_get_val(Ix1, k) + Ty2*v_get_val(Iy1,k) - Ty3*v_get_val(Ix2, k) - Ty4*v_get_val(Iy2, k));
-	
-			//4nd control point
-
-			Tx1 = A4 - delta * v_get_val(XY, k) * B4 / D_3;
-			Tx2 = -1.0 * delta*(B4/D - v_get_val(XX, k)*B4/D_3);
-			Tx3 = A4 + delta * v_get_val(XY, k) * B4 / D_3;
-			Tx4 = delta*(B4/D - v_get_val(XX, k)*B4/D_3);
-
-			Ty1 = delta*(B4/D - v_get_val(YY, k)*B4/D_3);
-			Ty2 = A4 + delta * v_get_val(XY, k) * B4 / D_3;
-			Ty3 = -1.0 * delta*(B4/D - v_get_val(YY, k)*B4/D_3);
-			Ty4 = A4 - delta * v_get_val(XY, k) * B4 / D_3;
-
-			v_set_val(dCx, dindex[i], v_get_val(dCx, dindex[i]) + Tx1*v_get_val(Ix1, k) + Tx2*v_get_val(Iy1,k) - Tx3*v_get_val(Ix2, k) - Tx4*v_get_val(Iy2, k));
-			v_set_val(dCy, dindex[i], v_get_val(dCy, dindex[i]) + Ty1*v_get_val(Ix1, k) + Ty2*v_get_val(Iy1,k) - Ty3*v_get_val(Ix2, k) - Ty4*v_get_val(Iy2, k));		
-		}
-	}
-
-	if(typeofcell==1)
-	{
-		for(i = 0; i < Cx->n; i++)
-			m_set_val(Cx, 0, i, m_get_val(Cx, 1, i) - dt*v_get_val(dCx, i));
-
-		for(i = 0; i < Cy->n; i++)
-			m_set_val(Cy, 0, i, m_get_val(Cy, 1, i) - dt*v_get_val(dCy, i));
-	}
-	else
-	{
-		for(i = 0; i < Cx->n; i++)
-			m_set_val(Cx, 0, i, m_get_val(Cx, 1, i) + dt*v_get_val(dCx, i));
-
-		for(i = 0; i < Cy->n; i++)
-			m_set_val(Cy, 0, i, m_get_val(Cy, 1, i) + dt*v_get_val(dCy, i));
-	}
-
-	v_free(dCy); v_free(dCx); v_free(YY); v_free(XX); v_free(XY);
-
-	free(dindex); free(cindex); free(bindex); free(aindex); 
-
-	v_free(Iy2); v_free(Ix2); v_free(Iy1); v_free(Ix1); 
-
-	m_free(Iy2_mat); m_free(Ix2_mat); m_free(Iy1_mat); m_free(Ix1_mat); 
-
-	v_free(Y2); v_free(X2); v_free(Y1); v_free(X1); v_free(Ny); v_free(Nx); v_free(Ys); v_free(Xs); v_free(Y); v_free(X); 
+  VEC * X, * Y, * Xs, * Ys, * Nx, * Ny, * X1, * Y1, * X2, * Y2, *	XY, * XX, * YY, * dCx, * dCy, * Ix1, * Ix2, *Iy1, *Iy2;
+  MAT * Ix1_mat, * Ix2_mat, * Iy1_mat, * Iy2_mat;
+  int i,j, N, * aindex, * bindex, * cindex, * dindex;
+
+  X = getsampling(Cx, ns);
+  Y = getsampling(Cy, ns);
+  Xs = getfdriv(Cx, ns);
+  Ys = getfdriv(Cy, ns);
+
+  Nx = v_get(Ys->dim);
+  for(i = 0; i < Nx->dim; i++)
+  v_set_val(Nx, i, v_get_val(Ys, i) / sqrt(v_get_val(Xs, i)*v_get_val(Xs, i) + v_get_val(Ys, i)*v_get_val(Ys, i)));
+
+  Ny = v_get(Xs->dim);
+  for(i = 0; i < Ny->dim; i++)
+  v_set_val(Ny, i, -1.0 * v_get_val(Xs, i) / sqrt(v_get_val(Xs, i)*v_get_val(Xs, i) + v_get_val(Ys, i)*v_get_val(Ys, i)));
+
+  X1 = v_get(Nx->dim);
+  for(i = 0; i < X1->dim; i++)
+  v_set_val(X1, i, v_get_val(X, i) + delta*v_get_val(Nx, i));
+
+  Y1 = v_get(Ny->dim);
+  for(i = 0; i < Y1->dim; i++)
+  v_set_val(Y1, i, v_get_val(Y, i) + delta*v_get_val(Ny, i));
+
+  X2 = v_get(Nx->dim);
+  for(i = 0; i < X2->dim; i++)
+  v_set_val(X2, i, v_get_val(X, i) - delta*v_get_val(Nx, i));
+
+  Y2 = v_get(Ny->dim);
+  for(i = 0; i < Y2->dim; i++)
+  v_set_val(Y2, i, v_get_val(Y, i) + delta*v_get_val(Ny, i));
+
+  // seg fault happens at this func call
+  Ix1_mat = linear_interp2(Ix, X1, Y1);
+  Iy1_mat = linear_interp2(Iy, X1, Y1);
+  Ix2_mat = linear_interp2(Ix, X2, Y2);
+  Iy2_mat = linear_interp2(Iy, X2, Y2);
+
+  Ix1 = v_get(Ix1_mat->n);
+  Iy1 = v_get(Iy1_mat->n);
+  Ix2 = v_get(Ix2_mat->n);
+  Iy2 = v_get(Iy2_mat->n);
+
+  Ix1 = get_row(Ix1_mat, 0, Ix1);
+  Iy1 = get_row(Iy1_mat, 0, Iy1);
+  Ix2 = get_row(Ix2_mat, 0, Ix2);
+  Iy2 = get_row(Iy2_mat, 0, Iy2);
+
+  N = Cx->m;
+
+  //VEC * retval = v_get(N*ns);
+
+  aindex = (int*) malloc(N*sizeof(int));
+  bindex = (int*) malloc(N*sizeof(int));
+  cindex = (int*) malloc(N*sizeof(int));
+  dindex = (int*) malloc(N*sizeof(int));
+
+  for(i = 1; i < N; i++)
+  aindex[i] = i-1;
+  aindex[0] = N-1;
+
+  for(i = 0; i < N; i++)
+  bindex[i] = i;
+
+  for(i = 0; i < N-1; i++)
+  cindex[i] = i+1;
+  cindex[N-1] = 0;
+
+  for(i = 0; i < N-2; i++)
+  dindex[i] = i+2;
+  dindex[N-2] = 0;
+  dindex[N-1] = 1;
+
+  XY = v_get(Xs->dim);
+  for(i = 0; i < Xs->dim; i++)
+  v_set_val(XY, i, v_get_val(Xs, i) * v_get_val(Ys, i));
+
+  XX = v_get(Xs->dim);
+  for(i = 0; i < Xs->dim; i++)
+  v_set_val(XX, i, v_get_val(Xs, i) * v_get_val(Xs, i));
+
+  YY = v_get(Ys->dim);
+  for(i = 0; i < Xs->dim; i++)
+  v_set_val(YY, i, v_get_val(Ys, i) * v_get_val(Ys, i));
+
+  dCx = v_get(Cx->m);
+  dCy = v_get(Cy->m);
+
+  //get control points for splines
+  for(i = 0; i < Cx->m; i++)
+  {
+  for(j = 0; j < ns; j++)
+  {
+  double s = (double)j / (double)ns;
+  double A1, A2, A3, A4, B1, B2, B3, B4, D, D_3, Tx1, Tx2, Tx3, Tx4, Ty1, Ty2, Ty3, Ty4;
+  int k;
+
+  A1 = (-1.0*(s-1.0)*(s-1.0)*(s-1.0)) / 6.0;
+  A2 = (3.0*s*s*s - 6.0*s*s + 4.0) / 6.0;
+  A3 = (-3.0*s*s*s + 3.0*s*s + 3.0*s + 1.0) / 6.0;
+  A4 = s*s*s / 6.0;
+
+  B1 = (-3.0*s*s + 6.0*s - 3.0) / 6.0;
+  B2 = (9.0*s*s - 12.0*s) / 6.0;
+  B3 = (-9.0*s*s + 6.0*s + 3.0) / 6.0;
+  B4 = 3.0*s*s / 6.0;
+
+  k = i*ns+j;
+  D = sqrt(v_get_val(Xs, k)*v_get_val(Xs, k) + v_get_val(Ys, k)*v_get_val(Ys, k));
+  D_3 = D*D*D;
+
+  //1st control point
+
+  Tx1 = A1 - delta * v_get_val(XY, k) * B1 / D_3;
+  Tx2 = -1.0 * delta*(B1/D - v_get_val(XX, k)*B1/D_3);
+  Tx3 = A1 + delta * v_get_val(XY, k) * B1 / D_3;
+  Tx4 = delta*(B1/D - v_get_val(XX, k)*B1/D_3);
+
+  Ty1 = delta*(B1/D - v_get_val(YY, k)*B1/D_3);
+  Ty2 = A1 + delta * v_get_val(XY, k) * B1 / D_3;
+  Ty3 = -1.0 * delta*(B1/D - v_get_val(YY, k)*B1/D_3);
+  Ty4 = A1 - delta * v_get_val(XY, k) * B1 / D_3;
+
+  v_set_val(dCx, aindex[i], v_get_val(dCx, aindex[i]) + Tx1*v_get_val(Ix1, k) + Tx2*v_get_val(Iy1,k) - Tx3*v_get_val(Ix2, k) - Tx4*v_get_val(Iy2, k));
+  v_set_val(dCy, aindex[i], v_get_val(dCy, aindex[i]) + Ty1*v_get_val(Ix1, k) + Ty2*v_get_val(Iy1,k) - Ty3*v_get_val(Ix2, k) - Ty4*v_get_val(Iy2, k));
+
+  //2nd control point
+
+  Tx1 = A2 - delta * v_get_val(XY, k) * B2 / D_3;
+  Tx2 = -1.0 * delta*(B2/D - v_get_val(XX, k)*B2/D_3);
+  Tx3 = A2 + delta * v_get_val(XY, k) * B2 / D_3;
+  Tx4 = delta*(B2/D - v_get_val(XX, k)*B2/D_3);
+
+  Ty1 = delta*(B2/D - v_get_val(YY, k)*B2/D_3);
+  Ty2 = A2 + delta * v_get_val(XY, k) * B2 / D_3;
+  Ty3 = -1.0 * delta*(B2/D - v_get_val(YY, k)*B2/D_3);
+  Ty4 = A2 - delta * v_get_val(XY, k) * B2 / D_3;
+
+  v_set_val(dCx, bindex[i], v_get_val(dCx, bindex[i]) + Tx1*v_get_val(Ix1, k) + Tx2*v_get_val(Iy1,k) - Tx3*v_get_val(Ix2, k) - Tx4*v_get_val(Iy2, k));
+  v_set_val(dCy, bindex[i], v_get_val(dCy, bindex[i]) + Ty1*v_get_val(Ix1, k) + Ty2*v_get_val(Iy1,k) - Ty3*v_get_val(Ix2, k) - Ty4*v_get_val(Iy2, k));
+
+  //3nd control point
+
+  Tx1 = A3 - delta * v_get_val(XY, k) * B3 / D_3;
+  Tx2 = -1.0 * delta*(B3/D - v_get_val(XX, k)*B3/D_3);
+  Tx3 = A3 + delta * v_get_val(XY, k) * B3 / D_3;
+  Tx4 = delta*(B3/D - v_get_val(XX, k)*B3/D_3);
+
+  Ty1 = delta*(B3/D - v_get_val(YY, k)*B3/D_3);
+  Ty2 = A3 + delta * v_get_val(XY, k) * B3 / D_3;
+  Ty3 = -1.0 * delta*(B3/D - v_get_val(YY, k)*B3/D_3);
+  Ty4 = A3 - delta * v_get_val(XY, k) * B3 / D_3;
+
+  v_set_val(dCx, cindex[i], v_get_val(dCx, cindex[i]) + Tx1*v_get_val(Ix1, k) + Tx2*v_get_val(Iy1,k) - Tx3*v_get_val(Ix2, k) - Tx4*v_get_val(Iy2, k));
+  v_set_val(dCy, cindex[i], v_get_val(dCy, cindex[i]) + Ty1*v_get_val(Ix1, k) + Ty2*v_get_val(Iy1,k) - Ty3*v_get_val(Ix2, k) - Ty4*v_get_val(Iy2, k));
+
+  //4nd control point
+
+  Tx1 = A4 - delta * v_get_val(XY, k) * B4 / D_3;
+  Tx2 = -1.0 * delta*(B4/D - v_get_val(XX, k)*B4/D_3);
+  Tx3 = A4 + delta * v_get_val(XY, k) * B4 / D_3;
+  Tx4 = delta*(B4/D - v_get_val(XX, k)*B4/D_3);
+
+  Ty1 = delta*(B4/D - v_get_val(YY, k)*B4/D_3);
+  Ty2 = A4 + delta * v_get_val(XY, k) * B4 / D_3;
+  Ty3 = -1.0 * delta*(B4/D - v_get_val(YY, k)*B4/D_3);
+  Ty4 = A4 - delta * v_get_val(XY, k) * B4 / D_3;
+
+  v_set_val(dCx, dindex[i], v_get_val(dCx, dindex[i]) + Tx1*v_get_val(Ix1, k) + Tx2*v_get_val(Iy1,k) - Tx3*v_get_val(Ix2, k) - Tx4*v_get_val(Iy2, k));
+  v_set_val(dCy, dindex[i], v_get_val(dCy, dindex[i]) + Ty1*v_get_val(Ix1, k) + Ty2*v_get_val(Iy1,k) - Ty3*v_get_val(Ix2, k) - Ty4*v_get_val(Iy2, k));
+  }
+  }
+
+  if(typeofcell==1)
+  {
+  for(i = 0; i < Cx->n; i++)
+  m_set_val(Cx, 0, i, m_get_val(Cx, 1, i) - dt*v_get_val(dCx, i));
+
+  for(i = 0; i < Cy->n; i++)
+  m_set_val(Cy, 0, i, m_get_val(Cy, 1, i) - dt*v_get_val(dCy, i));
+  }
+  else
+  {
+  for(i = 0; i < Cx->n; i++)
+  m_set_val(Cx, 0, i, m_get_val(Cx, 1, i) + dt*v_get_val(dCx, i));
+
+  for(i = 0; i < Cy->n; i++)
+  m_set_val(Cy, 0, i, m_get_val(Cy, 1, i) + dt*v_get_val(dCy, i));
+  }
+
+  v_free(dCy); v_free(dCx); v_free(YY); v_free(XX); v_free(XY);
+
+  free(dindex); free(cindex); free(bindex); free(aindex);
+
+  v_free(Iy2); v_free(Ix2); v_free(Iy1); v_free(Ix1);
+
+  m_free(Iy2_mat); m_free(Ix2_mat); m_free(Iy1_mat); m_free(Ix1_mat);
+
+  v_free(Y2); v_free(X2); v_free(Y1); v_free(X1); v_free(Ny); v_free(Nx); v_free(Ys); v_free(Xs); v_free(Y); v_free(X);
 }

+ 45 - 45
opencl/leukocyte/OpenCL/misc_math.c

@@ -7,72 +7,72 @@
 
 inline int double_eq(double f1, double f2)
 {
-	return fabs(f1-f2) < THRESHOLD_double;
+  return fabs(f1-f2) < THRESHOLD_double;
 }
 
 //Given a matrix, return the matrix containing an approximation of the gradient matrix dM/dx
 MAT * gradient_x(MAT * input)
 {
-	int i, j;
-	MAT * result = m_get(input->m, input->n);
+  int i, j;
+  MAT * result = m_get(input->m, input->n);
 
-	for(i = 0; i < result->m; i++)
-	{
-		for(j = 0; j < result->n; j++)
-		{
-			if(j==0)
-				m_set_val(result, i, j, m_get_val(input, i, j+1) - m_get_val(input, i, j));
-			else if(j==input->n-1)
-				m_set_val(result, i, j, m_get_val(input, i, j) - m_get_val(input, i, j-1));
-			else
-				m_set_val(result, i, j, (m_get_val(input, i, j+1) - m_get_val(input, i, j-1)) / 2.0);
-		}
-	}
+  for(i = 0; i < result->m; i++)
+  {
+  for(j = 0; j < result->n; j++)
+  {
+  if(j==0)
+  m_set_val(result, i, j, m_get_val(input, i, j+1) - m_get_val(input, i, j));
+  else if(j==input->n-1)
+  m_set_val(result, i, j, m_get_val(input, i, j) - m_get_val(input, i, j-1));
+  else
+  m_set_val(result, i, j, (m_get_val(input, i, j+1) - m_get_val(input, i, j-1)) / 2.0);
+  }
+  }
 
-	return result;
+  return result;
 }
 
 //Given a matrix, return the matrix containing an approximation of the gradient matrix dM/dy
 MAT * gradient_y(MAT * input)
 {
-	int i, j;
-	MAT * result = m_get(input->m, input->n);
-	
-	for(i = 0; i < result->n; i++)
-	{
-		for(j = 0; j < result->m; j++)
-		{
-			if(j==0)
-				m_set_val(result, j, i, m_get_val(input, j+1, i) - m_get_val(input, j, i));
-			else if(j==input->m-1)
-				m_set_val(result, j, i, m_get_val(input, j, i) - m_get_val(input, j-1, i));
-			else
-				m_set_val(result, j, i, (m_get_val(input, j+1, i) - m_get_val(input, j-1, i)) / 2.0);
-		}
-	}
-	return result;
+  int i, j;
+  MAT * result = m_get(input->m, input->n);
+
+  for(i = 0; i < result->n; i++)
+  {
+  for(j = 0; j < result->m; j++)
+  {
+  if(j==0)
+  m_set_val(result, j, i, m_get_val(input, j+1, i) - m_get_val(input, j, i));
+  else if(j==input->m-1)
+  m_set_val(result, j, i, m_get_val(input, j, i) - m_get_val(input, j-1, i));
+  else
+  m_set_val(result, j, i, (m_get_val(input, j+1, i) - m_get_val(input, j-1, i)) / 2.0);
+  }
+  }
+  return result;
 }
 
 //Return the mean of the values in a vector
 double mean(VEC * in)
 {
-	double sum = 0.0;
-	int i;
-	for(i = 0; i < in->dim; i++)
-		sum+=v_get_val(in, i);
+  double sum = 0.0;
+  int i;
+  for(i = 0; i < in->dim; i++)
+  sum+=v_get_val(in, i);
 
-	return sum/(double)in->dim;
+  return sum/(double)in->dim;
 }
 
 //Return the standard deviation of the values in a vector
 double std_dev(VEC * in)
 {
-	double m = mean(in), sum =0.0;
-	int i;
-	for(i = 0; i < in->dim; i++)
-	{
-		double temp = v_get_val(in, i) - m;
-		sum+=temp*temp;
-	}
-	return sqrt(sum/(double)in->dim);
+  double m = mean(in), sum =0.0;
+  int i;
+  for(i = 0; i < in->dim; i++)
+  {
+  double temp = v_get_val(in, i) - m;
+  sum+=temp*temp;
+  }
+  return sqrt(sum/(double)in->dim);
 }

+ 512 - 512
opencl/leukocyte/OpenCL/track_ellipse.c

@@ -3,558 +3,558 @@
 
 
 void ellipsetrack(avi_t *video, double *xc0, double *yc0, int Nc, int R, int Np, int Nf) {
-	/*
-	% ELLIPSETRACK tracks cells in the movie specified by 'video', at
-	%  locations 'xc0'/'yc0' with radii R using an ellipse with Np discrete
-	%  points, starting at frame number one and stopping at frame number 'Nf'.
-	%
-	% INPUTS:
-	%   video.......pointer to avi video object
-	%   xc0,yc0.....initial center location (Nc entries)
-	%   Nc..........number of cells
-	%   R...........initial radius
-	%   Np..........number of snaxels points per snake
-	%   Nf..........number of frames in which to track
-	%
-	% Matlab code written by: DREW GILLIAM (based on code by GANG DONG /
-	%                                                        NILANJAN RAY)
-	% Ported to C by: MICHAEL BOYER
-	*/
-	
-	// Compute angle parameter
-	double *t = (double *) malloc(sizeof(double) * Np);
-	double increment = (2.0 * PI) / (double) Np;
-	int i, j;
-	for (i = 0; i < Np; i++) {
-		t[i] =  increment * (double) i ;
-	}
-
-	// Allocate space for a snake for each cell in each frame
-	double **xc = alloc_2d_double(Nc, Nf + 1);
-	double **yc = alloc_2d_double(Nc, Nf + 1);
-	double ***r = alloc_3d_double(Nc, Np, Nf + 1);
-	double ***x = alloc_3d_double(Nc, Np, Nf + 1);
-	double ***y = alloc_3d_double(Nc, Np, Nf + 1);
-	
-	// Save the first snake for each cell
-	for (i = 0; i < Nc; i++) {
-		xc[i][0] = xc0[i];
-		yc[i][0] = yc0[i];
-		for (j = 0; j < Np; j++) {
-			r[i][j][0] = (double) R;
-		}
-	}
-	
-	// Generate ellipse points for each cell
-	for (i = 0; i < Nc; i++) {
-		for (j = 0; j < Np; j++) {
-			x[i][j][0] = xc[i][0] + (r[i][j][0] * cos(t[j]));
-			y[i][j][0] = yc[i][0] + (r[i][j][0] * sin(t[j]));
-		}
-	}
-	
-	// Allocate arrays so we can break up the per-cell for loop below
-	double *xci = (double *) malloc(sizeof(double) * Nc);
-	double *yci = (double *) malloc(sizeof(double) * Nc);
-	double **ri = alloc_2d_double(Nc, Np);
-	double *ycavg = (double *) malloc(sizeof(double) * Nc);
-	int *u1 = (int *) malloc(sizeof(int) * Nc);
-	int *u2 = (int *) malloc(sizeof(int) * Nc);
-	int *v1 = (int *) malloc(sizeof(int) * Nc);
-	int *v2 = (int *) malloc(sizeof(int) * Nc);
-	MAT **Isub = (MAT **) malloc(sizeof(MAT *) * Nc);
-	MAT **Ix = (MAT **) malloc(sizeof(MAT *) * Nc);
-	MAT **Iy = (MAT **) malloc(sizeof(MAT *) * Nc);
-	MAT **IE = (MAT **) malloc(sizeof(MAT *) * Nc);
-	
-	// Keep track of the total time spent on computing
-	//  the MGVF matrix and evolving the snakes
-	long long  MGVF_time = 0;
-	long long snake_time = 0;
-	
-	
-	// Process each frame sequentially
-	int frame_num;
-	for (frame_num = 1; frame_num <= Nf; frame_num++) {	 
-		printf("\rProcessing frame %d / %d", frame_num, Nf);
-		fflush(stdout);
-		
-		// Get the current video frame and its dimensions
-		MAT *I = get_frame(video, frame_num, 0, 1);
-		int Ih = I->m;
-		int Iw = I->n;
-	    
-	    // Initialize the current positions to be equal to the previous positions		
-		for (i = 0; i < Nc; i++) {
-			xc[i][frame_num] = xc[i][frame_num - 1];
-			yc[i][frame_num] = yc[i][frame_num - 1];
-			for (j = 0; j < Np; j++) {
-				r[i][j][frame_num] = r[i][j][frame_num - 1];
-			}
-		}
-		
-		// Sequentially extract the subimage near each cell
-		int cell_num;
-		for (cell_num = 0; cell_num < Nc; cell_num++) {
-			// Make copies of the current cell's location
-			xci[cell_num] = xc[cell_num][frame_num];
-			yci[cell_num] = yc[cell_num][frame_num];
-			for (j = 0; j < Np; j++) {
-				ri[cell_num][j] = r[cell_num][j][frame_num];
-			}
-			
-			// Add up the last ten y values for this cell
-			//  (or fewer if there are not yet ten previous frames)
-			ycavg[cell_num] = 0.0;
-			for (i = (frame_num > 10 ? frame_num - 10 : 0); i < frame_num; i++) {
-				ycavg[cell_num] += yc[cell_num][i];
-			}
-			// Compute the average of the last ten values
-			//  (this represents the expected location of the cell)
-			ycavg[cell_num] = ycavg[cell_num] / (double) (frame_num > 10 ? 10 : frame_num);
-			
-			// Determine the range of the subimage surrounding the current position
-			u1[cell_num] = max(xci[cell_num] - 4.0 * R + 0.5, 0 );
-			u2[cell_num] = min(xci[cell_num] + 4.0 * R + 0.5, Iw - 1);
-			v1[cell_num] = max(yci[cell_num] - 2.0 * R + 1.5, 0 );    
-			v2[cell_num] = min(yci[cell_num] + 2.0 * R + 1.5, Ih - 1);
-			
-			// Extract the subimage
-			Isub[cell_num] = m_get(v2[cell_num] - v1[cell_num] + 1, u2[cell_num] - u1[cell_num] + 1);
-			for (i = v1[cell_num]; i <= v2[cell_num]; i++) {
-				for (j = u1[cell_num]; j <= u2[cell_num]; j++) {
-					m_set_val(Isub[cell_num], i - v1[cell_num], j - u1[cell_num], m_get_val(I, i, j));
-				}
-			}
-			
-	        // Compute the subimage gradient magnitude			
-			Ix[cell_num] = gradient_x(Isub[cell_num]);
-			Iy[cell_num] = gradient_y(Isub[cell_num]);
-			IE[cell_num] = m_get(Isub[cell_num]->m, Isub[cell_num]->n);
-			for (i = 0; i < Isub[cell_num]->m; i++) {
-				for (j = 0; j < Isub[cell_num]->n; j++) {
-					double temp_x = m_get_val(Ix[cell_num], i, j);
-					double temp_y = m_get_val(Iy[cell_num], i, j);
-					m_set_val(IE[cell_num], i, j, sqrt((temp_x * temp_x) + (temp_y * temp_y)));
-				}
-			}
-		}
-		
-		// Compute the motion gradient vector flow (MGVF) edgemaps for all cells concurrently
-		long long MGVF_start_time = get_time();
-		MAT **IMGVF = MGVF(IE, 1, 1, Nc);
-		MGVF_time += get_time() - MGVF_start_time;
-		
-		// Sequentially determine the new location of each cell
-		for (cell_num = 0; cell_num < Nc; cell_num++) {	
-			// Determine the position of the cell in the subimage			
-			xci[cell_num] = xci[cell_num] - (double) u1[cell_num];
-			yci[cell_num] = yci[cell_num] - (double) (v1[cell_num] - 1);
-			ycavg[cell_num] = ycavg[cell_num] - (double) (v1[cell_num] - 1);
-			
-			// Evolve the snake
-			long long snake_start_time = get_time();
-			ellipseevolve(IMGVF[cell_num], &(xci[cell_num]), &(yci[cell_num]), ri[cell_num], t, Np, (double) R, ycavg[cell_num]);
-			snake_time += get_time() - snake_start_time;
-			
-			// Compute the cell's new position in the full image
-			xci[cell_num] = xci[cell_num] + u1[cell_num];
-			yci[cell_num] = yci[cell_num] + (v1[cell_num] - 1);
-			
-			// Store the new location of the cell and the snake
-			xc[cell_num][frame_num] = xci[cell_num];
-			yc[cell_num][frame_num] = yci[cell_num];
-			for (j = 0; j < Np; j++) {
-				r[cell_num][j][frame_num] = 0;
-				r[cell_num][j][frame_num] = ri[cell_num][j];
-				x[cell_num][j][frame_num] = xc[cell_num][frame_num] + (ri[cell_num][j] * cos(t[j]));
-				y[cell_num][j][frame_num] = yc[cell_num][frame_num] + (ri[cell_num][j] * sin(t[j]));
-			}
-			
-			// Output the updated center of each cell
-			// printf("\n%d,%f,%f", cell_num, xci[cell_num], yci[cell_num]);
-
-
-			
-			// Free temporary memory
-			m_free(Isub[cell_num]);
-			m_free(Ix[cell_num]);
-			m_free(Iy[cell_num]);
-			m_free(IE[cell_num]);
-			m_free(IMGVF[cell_num]);
-	    }
+  /*
+  % ELLIPSETRACK tracks cells in the movie specified by 'video', at
+  %  locations 'xc0'/'yc0' with radii R using an ellipse with Np discrete
+  %  points, starting at frame number one and stopping at frame number 'Nf'.
+  %
+  % INPUTS:
+  %   video.......pointer to avi video object
+  %   xc0,yc0.....initial center location (Nc entries)
+  %   Nc..........number of cells
+  %   R...........initial radius
+  %   Np..........number of snaxels points per snake
+  %   Nf..........number of frames in which to track
+  %
+  % Matlab code written by: DREW GILLIAM (based on code by GANG DONG /
+  %                                                        NILANJAN RAY)
+  % Ported to C by: MICHAEL BOYER
+  */
+
+  // Compute angle parameter
+  double *t = (double *) malloc(sizeof(double) * Np);
+  double increment = (2.0 * PI) / (double) Np;
+  int i, j;
+  for (i = 0; i < Np; i++) {
+  t[i] =  increment * (double) i ;
+  }
+
+  // Allocate space for a snake for each cell in each frame
+  double **xc = alloc_2d_double(Nc, Nf + 1);
+  double **yc = alloc_2d_double(Nc, Nf + 1);
+  double ***r = alloc_3d_double(Nc, Np, Nf + 1);
+  double ***x = alloc_3d_double(Nc, Np, Nf + 1);
+  double ***y = alloc_3d_double(Nc, Np, Nf + 1);
+
+  // Save the first snake for each cell
+  for (i = 0; i < Nc; i++) {
+  xc[i][0] = xc0[i];
+  yc[i][0] = yc0[i];
+  for (j = 0; j < Np; j++) {
+  r[i][j][0] = (double) R;
+  }
+  }
+
+  // Generate ellipse points for each cell
+  for (i = 0; i < Nc; i++) {
+  for (j = 0; j < Np; j++) {
+  x[i][j][0] = xc[i][0] + (r[i][j][0] * cos(t[j]));
+  y[i][j][0] = yc[i][0] + (r[i][j][0] * sin(t[j]));
+  }
+  }
+
+  // Allocate arrays so we can break up the per-cell for loop below
+  double *xci = (double *) malloc(sizeof(double) * Nc);
+  double *yci = (double *) malloc(sizeof(double) * Nc);
+  double **ri = alloc_2d_double(Nc, Np);
+  double *ycavg = (double *) malloc(sizeof(double) * Nc);
+  int *u1 = (int *) malloc(sizeof(int) * Nc);
+  int *u2 = (int *) malloc(sizeof(int) * Nc);
+  int *v1 = (int *) malloc(sizeof(int) * Nc);
+  int *v2 = (int *) malloc(sizeof(int) * Nc);
+  MAT **Isub = (MAT **) malloc(sizeof(MAT *) * Nc);
+  MAT **Ix = (MAT **) malloc(sizeof(MAT *) * Nc);
+  MAT **Iy = (MAT **) malloc(sizeof(MAT *) * Nc);
+  MAT **IE = (MAT **) malloc(sizeof(MAT *) * Nc);
+
+  // Keep track of the total time spent on computing
+  //  the MGVF matrix and evolving the snakes
+  long long  MGVF_time = 0;
+  long long snake_time = 0;
+
+
+  // Process each frame sequentially
+  int frame_num;
+  for (frame_num = 1; frame_num <= Nf; frame_num++) {
+  printf("\rProcessing frame %d / %d", frame_num, Nf);
+  fflush(stdout);
+
+  // Get the current video frame and its dimensions
+  MAT *I = get_frame(video, frame_num, 0, 1);
+  int Ih = I->m;
+  int Iw = I->n;
+
+      // Initialize the current positions to be equal to the previous positions
+  for (i = 0; i < Nc; i++) {
+  xc[i][frame_num] = xc[i][frame_num - 1];
+  yc[i][frame_num] = yc[i][frame_num - 1];
+  for (j = 0; j < Np; j++) {
+  r[i][j][frame_num] = r[i][j][frame_num - 1];
+  }
+  }
+
+  // Sequentially extract the subimage near each cell
+  int cell_num;
+  for (cell_num = 0; cell_num < Nc; cell_num++) {
+  // Make copies of the current cell's location
+  xci[cell_num] = xc[cell_num][frame_num];
+  yci[cell_num] = yc[cell_num][frame_num];
+  for (j = 0; j < Np; j++) {
+  ri[cell_num][j] = r[cell_num][j][frame_num];
+  }
+
+  // Add up the last ten y values for this cell
+  //  (or fewer if there are not yet ten previous frames)
+  ycavg[cell_num] = 0.0;
+  for (i = (frame_num > 10 ? frame_num - 10 : 0); i < frame_num; i++) {
+  ycavg[cell_num] += yc[cell_num][i];
+  }
+  // Compute the average of the last ten values
+  //  (this represents the expected location of the cell)
+  ycavg[cell_num] = ycavg[cell_num] / (double) (frame_num > 10 ? 10 : frame_num);
+
+  // Determine the range of the subimage surrounding the current position
+  u1[cell_num] = max(xci[cell_num] - 4.0 * R + 0.5, 0 );
+  u2[cell_num] = min(xci[cell_num] + 4.0 * R + 0.5, Iw - 1);
+  v1[cell_num] = max(yci[cell_num] - 2.0 * R + 1.5, 0 );
+  v2[cell_num] = min(yci[cell_num] + 2.0 * R + 1.5, Ih - 1);
+
+  // Extract the subimage
+  Isub[cell_num] = m_get(v2[cell_num] - v1[cell_num] + 1, u2[cell_num] - u1[cell_num] + 1);
+  for (i = v1[cell_num]; i <= v2[cell_num]; i++) {
+  for (j = u1[cell_num]; j <= u2[cell_num]; j++) {
+  m_set_val(Isub[cell_num], i - v1[cell_num], j - u1[cell_num], m_get_val(I, i, j));
+  }
+  }
+
+          // Compute the subimage gradient magnitude
+  Ix[cell_num] = gradient_x(Isub[cell_num]);
+  Iy[cell_num] = gradient_y(Isub[cell_num]);
+  IE[cell_num] = m_get(Isub[cell_num]->m, Isub[cell_num]->n);
+  for (i = 0; i < Isub[cell_num]->m; i++) {
+  for (j = 0; j < Isub[cell_num]->n; j++) {
+  double temp_x = m_get_val(Ix[cell_num], i, j);
+  double temp_y = m_get_val(Iy[cell_num], i, j);
+  m_set_val(IE[cell_num], i, j, sqrt((temp_x * temp_x) + (temp_y * temp_y)));
+  }
+  }
+  }
+
+  // Compute the motion gradient vector flow (MGVF) edgemaps for all cells concurrently
+  long long MGVF_start_time = get_time();
+  MAT **IMGVF = MGVF(IE, 1, 1, Nc);
+  MGVF_time += get_time() - MGVF_start_time;
+
+  // Sequentially determine the new location of each cell
+  for (cell_num = 0; cell_num < Nc; cell_num++) {
+  // Determine the position of the cell in the subimage
+  xci[cell_num] = xci[cell_num] - (double) u1[cell_num];
+  yci[cell_num] = yci[cell_num] - (double) (v1[cell_num] - 1);
+  ycavg[cell_num] = ycavg[cell_num] - (double) (v1[cell_num] - 1);
+
+  // Evolve the snake
+  long long snake_start_time = get_time();
+  ellipseevolve(IMGVF[cell_num], &(xci[cell_num]), &(yci[cell_num]), ri[cell_num], t, Np, (double) R, ycavg[cell_num]);
+  snake_time += get_time() - snake_start_time;
+
+  // Compute the cell's new position in the full image
+  xci[cell_num] = xci[cell_num] + u1[cell_num];
+  yci[cell_num] = yci[cell_num] + (v1[cell_num] - 1);
+
+  // Store the new location of the cell and the snake
+  xc[cell_num][frame_num] = xci[cell_num];
+  yc[cell_num][frame_num] = yci[cell_num];
+  for (j = 0; j < Np; j++) {
+  r[cell_num][j][frame_num] = 0;
+  r[cell_num][j][frame_num] = ri[cell_num][j];
+  x[cell_num][j][frame_num] = xc[cell_num][frame_num] + (ri[cell_num][j] * cos(t[j]));
+  y[cell_num][j][frame_num] = yc[cell_num][frame_num] + (ri[cell_num][j] * sin(t[j]));
+  }
+
+  // Output the updated center of each cell
+  // printf("\n%d,%f,%f", cell_num, xci[cell_num], yci[cell_num]);
+
+
+
+  // Free temporary memory
+  m_free(Isub[cell_num]);
+  m_free(Ix[cell_num]);
+  m_free(Iy[cell_num]);
+  m_free(IE[cell_num]);
+  m_free(IMGVF[cell_num]);
+      }
 
 #ifdef OUTPUT
-		if (frame_num == Nf)
-		  {
-		    FILE * pFile;
-		    pFile = fopen ("result.txt","w+");
-	
-		    for (cell_num = 0; cell_num < Nc; cell_num++)		
-		      fprintf(pFile,"\n%d,%f,%f", cell_num, xci[cell_num], yci[cell_num]);
-
-		    fclose (pFile);
-		  }
-		
+  if (frame_num == Nf)
+    {
+      FILE * pFile;
+      pFile = fopen ("result.txt","w+");
+
+      for (cell_num = 0; cell_num < Nc; cell_num++)
+        fprintf(pFile,"\n%d,%f,%f", cell_num, xci[cell_num], yci[cell_num]);
+
+      fclose (pFile);
+    }
+
 #endif
 
-		
-		free(IMGVF);
-		
-		// Output a new line to visually distinguish the output from different frames
-		//printf("\n");
-	}
-	
-	// Free temporary memory
-	free_2d_double(xc);
-	free_2d_double(yc);
-	free_3d_double(r);
-	free_3d_double(x);
-	free_3d_double(y);
-	free(t);	
-	free(xci);
-	free(yci);
-	free_2d_double(ri);
-	free(ycavg);
-	free(u1);
-	free(u2);
-	free(v1);
-	free(v2);
-	free(Isub);
-	free(Ix);
-	free(Iy);
-	free(IE);
-	
-	// Report average processing time per frame
-	printf("\n\nTracking runtime (average per frame):\n");
-	printf("------------------------------------\n");
-	printf("MGVF computation: %.5f seconds\n", ((float) (MGVF_time)) / (float) (1000*1000*Nf));
-	printf(" Snake evolution: %.5f seconds\n", ((float) (snake_time)) / (float) (1000*1000*Nf));
+
+  free(IMGVF);
+
+  // Output a new line to visually distinguish the output from different frames
+  //printf("\n");
+  }
+
+  // Free temporary memory
+  free_2d_double(xc);
+  free_2d_double(yc);
+  free_3d_double(r);
+  free_3d_double(x);
+  free_3d_double(y);
+  free(t);
+  free(xci);
+  free(yci);
+  free_2d_double(ri);
+  free(ycavg);
+  free(u1);
+  free(u2);
+  free(v1);
+  free(v2);
+  free(Isub);
+  free(Ix);
+  free(Iy);
+  free(IE);
+
+  // Report average processing time per frame
+  printf("\n\nTracking runtime (average per frame):\n");
+  printf("------------------------------------\n");
+  printf("MGVF computation: %.5f seconds\n", ((float) (MGVF_time)) / (float) (1000*1000*Nf));
+  printf(" Snake evolution: %.5f seconds\n", ((float) (snake_time)) / (float) (1000*1000*Nf));
 }
 
 
 MAT **MGVF(MAT **IE, double vx, double vy, int Nc) {
-	/*
-	% MGVF calculate the motion gradient vector flow (MGVF) 
-	%  for the image 'I'
-	%
-	% Based on the algorithm in:
-	%  Motion gradient vector flow: an external force for tracking rolling 
-	%   leukocytes with shape and size constrained active contours
-	%  Ray, N. and Acton, S.T.
-	%  IEEE Transactions on Medical Imaging
-	%  Volume: 23, Issue: 12, December 2004 
-	%  Pages: 1466 - 1478
-	%
-	% INPUTS
-	%   I...........image
-	%   vx,vy.......velocity vector
-	%   
-	% OUTPUT
-	%   IMGVF.......MGVF vector field as image
-	%
-	% Matlab code written by: DREW GILLIAM (based on work by GANG DONG /
-	%                                                        NILANJAN RAY)
-	% Ported to C by: MICHAEL BOYER
-	*/
-
-	// Constants
-	double converge = 0.00001;
-	double epsilon = 0.0000000001;
-	// Smallest positive value expressable in double-precision
-	double eps = pow(2.0, -52.0);
-	// Maximum number of iterations to compute the MGVF matrix
-	int iterations = 500;
-	
-	// Allocate memory for pointers to the MGVF for each cell
-	MAT **IMGVF = (MAT **) malloc(sizeof(MAT *) * Nc);
-	
-	// Normalize the sub-image for each cell
-	int cell_num;
-	for (cell_num = 0; cell_num < Nc; cell_num++) {
-		MAT *I = IE[cell_num];
-		
-		// Find the maximum and minimum values in I
-		int m = I->m, n = I->n, i, j;
-		double Imax = m_get_val(I, 0, 0);
-		double Imin = m_get_val(I, 0, 0);
-		for (i = 0; i < m; i++) {
-			for (j = 0; j < n; j++) {
-				double temp = m_get_val(I, i, j);
-				if (temp > Imax) Imax = temp;
-				else if (temp < Imin) Imin = temp;
-			}
-		}
-		
-		// Normalize the images I
-		double scale = 1.0 / (Imax - Imin + eps);
-		for (i = 0; i < m; i++) {
-			for (j = 0; j < n; j++) {
-				double old_val = m_get_val(I, i, j);
-				m_set_val(I, i, j, (old_val - Imin) * scale);
-			}
-		}
-		
-		// Allocate memory for this cell's MGVF matrix
-		IMGVF[cell_num] = m_get(m, n);
-	}
-	
-	// Offload the MGVF computation to the GPU
-	IMGVF_OpenCL(IE, IMGVF, vx, vy, epsilon, iterations, converge, Nc);
-
-	return IMGVF;
+  /*
+  % MGVF calculate the motion gradient vector flow (MGVF)
+  %  for the image 'I'
+  %
+  % Based on the algorithm in:
+  %  Motion gradient vector flow: an external force for tracking rolling
+  %   leukocytes with shape and size constrained active contours
+  %  Ray, N. and Acton, S.T.
+  %  IEEE Transactions on Medical Imaging
+  %  Volume: 23, Issue: 12, December 2004
+  %  Pages: 1466 - 1478
+  %
+  % INPUTS
+  %   I...........image
+  %   vx,vy.......velocity vector
+  %
+  % OUTPUT
+  %   IMGVF.......MGVF vector field as image
+  %
+  % Matlab code written by: DREW GILLIAM (based on work by GANG DONG /
+  %                                                        NILANJAN RAY)
+  % Ported to C by: MICHAEL BOYER
+  */
+
+  // Constants
+  double converge = 0.00001;
+  double epsilon = 0.0000000001;
+  // Smallest positive value expressable in double-precision
+  double eps = pow(2.0, -52.0);
+  // Maximum number of iterations to compute the MGVF matrix
+  int iterations = 500;
+
+  // Allocate memory for pointers to the MGVF for each cell
+  MAT **IMGVF = (MAT **) malloc(sizeof(MAT *) * Nc);
+
+  // Normalize the sub-image for each cell
+  int cell_num;
+  for (cell_num = 0; cell_num < Nc; cell_num++) {
+  MAT *I = IE[cell_num];
+
+  // Find the maximum and minimum values in I
+  int m = I->m, n = I->n, i, j;
+  double Imax = m_get_val(I, 0, 0);
+  double Imin = m_get_val(I, 0, 0);
+  for (i = 0; i < m; i++) {
+  for (j = 0; j < n; j++) {
+  double temp = m_get_val(I, i, j);
+  if (temp > Imax) Imax = temp;
+  else if (temp < Imin) Imin = temp;
+  }
+  }
+
+  // Normalize the images I
+  double scale = 1.0 / (Imax - Imin + eps);
+  for (i = 0; i < m; i++) {
+  for (j = 0; j < n; j++) {
+  double old_val = m_get_val(I, i, j);
+  m_set_val(I, i, j, (old_val - Imin) * scale);
+  }
+  }
+
+  // Allocate memory for this cell's MGVF matrix
+  IMGVF[cell_num] = m_get(m, n);
+  }
+
+  // Offload the MGVF computation to the GPU
+  IMGVF_OpenCL(IE, IMGVF, vx, vy, epsilon, iterations, converge, Nc);
+
+  return IMGVF;
 }
 
 
 void ellipseevolve(MAT *f, double *xc0, double *yc0, double *r0, double *t, int Np, double Er, double Ey) {
-	/*
-	% ELLIPSEEVOLVE evolves a parametric snake according
-	%  to some energy constraints.
-	%
-	% INPUTS:
-	%   f............potential surface
-	%   xc0,yc0......initial center position
-	%   r0,t.........initial radii & angle vectors (with Np elements each)
-	%   Np...........number of snaxel points per snake
-	%   Er...........expected radius
-	%   Ey...........expected y position
-	%
-	% OUTPUTS
-	%   xc0,yc0.......final center position
-	%   r0...........final radii
-	%
-	% Matlab code written by: DREW GILLIAM (based on work by GANG DONG /
-	%                                                        NILANJAN RAY)
-	% Ported to C by: MICHAEL BOYER
-	*/
-	
-	
-	// Constants
-	double deltax = 0.2;
-	double deltay = 0.2;
-	double deltar = 0.2; 
-	double converge = 0.1;
-	double lambdaedge = 1;
-	double lambdasize = 0.2;
-	double lambdapath = 0.05;
-	int iterations = 1000;      // maximum number of iterations
-
-	int i, j;
-
-	// Initialize variables
-	double xc = *xc0;
-	double yc = *yc0;
-	double *r = (double *) malloc(sizeof(double) * Np);
-	for (i = 0; i < Np; i++) r[i] = r0[i];
-	
-	// Compute the x- and y-gradients of the MGVF matrix
-	MAT *fx = gradient_x(f);
-	MAT *fy = gradient_y(f);
-	
-	// Normalize the gradients
-	int fh = f->m, fw = f->n;
-	for (i = 0; i < fh; i++) {
-		for (j = 0; j < fw; j++) {
-			double temp_x = m_get_val(fx, i, j);
-			double temp_y = m_get_val(fy, i, j);
-			double fmag = sqrt((temp_x * temp_x) + (temp_y * temp_y));
-			// Fix 0/0 error
-			if (fmag > 1E-15) {
-				m_set_val(fx, i, j, temp_x / fmag);
-				m_set_val(fy, i, j, temp_y / fmag);
-			}	else {
-				m_set_val(fx, i, j, 0.0);
-				m_set_val(fy, i, j, 0.0);
-			}
-			/* m_set_val(fx, i, j, temp_x / fmag); */
-			/* m_set_val(fy, i, j, temp_y / fmag); */
-		}
-	}
-	
-	double *r_old = (double *) malloc(sizeof(double) * Np);
-	VEC *x = v_get(Np);
-	VEC *y = v_get(Np);
-	
-	
-	// Evolve the snake
-	int iter = 0;
-	double snakediff = 1.0;
-	while (iter < iterations && snakediff > converge) {
-		
-		// Save the values from the previous iteration
-		double xc_old = xc, yc_old = yc;
-		for (i = 0; i < Np; i++) {
-			r_old[i] = r[i];
-		}
-		
-		// Compute the locations of the snaxels
-		for (i = 0; i < Np; i++) {
-			v_set_val(x, i, xc + r[i] * cos(t[i]));
-			v_set_val(y, i, yc + r[i] * sin(t[i]));
-		}
-		
-		// See if any of the points in the snake are off the edge of the image
-		double min_x = v_get_val(x, 0), max_x = v_get_val(x, 0);
-		double min_y = v_get_val(y, 0), max_y = v_get_val(y, 0);
-		for (i = 1; i < Np; i++) {
-			double x_i = v_get_val(x, i);
-			if (x_i < min_x) min_x = x_i;
-			else if (x_i > max_x) max_x = x_i;
-			double y_i = v_get_val(y, i);
-			if (y_i < min_y) min_y = y_i;
-			else if (y_i > max_y) max_y = y_i;
-		}
-		if (min_x < 0.0 || max_x > (double) fw - 1.0 || min_y < 0 || max_y > (double) fh - 1.0) break;
-		
-		
-		// Compute the length of the snake		
-		double L = 0.0;
-		for (i = 0; i < Np - 1; i++) {
-			double diff_x = v_get_val(x, i + 1) - v_get_val(x, i);
-			double diff_y = v_get_val(y, i + 1) - v_get_val(y, i);
-			L += sqrt((diff_x * diff_x) + (diff_y * diff_y));
-		}
-		double diff_x = v_get_val(x, 0) - v_get_val(x, Np - 1);
-		double diff_y = v_get_val(y, 0) - v_get_val(y, Np - 1);
-		L += sqrt((diff_x * diff_x) + (diff_y * diff_y));
-		
-		// Compute the potential surface at each snaxel
-		MAT *vf  = linear_interp2(f,  x, y);
-		MAT *vfx = linear_interp2(fx, x, y);
-		MAT *vfy = linear_interp2(fy, x, y);
-		
-		// Compute the average potential surface around the snake
-		double vfmean  = sum_m(vf ) / L;
-		double vfxmean = sum_m(vfx) / L;
-		double vfymean = sum_m(vfy) / L;
-		
-		// Compute the radial potential surface		
-		int m = vf->m, n = vf->n;
-		MAT *vfr = m_get(m, n);
-		for (i = 0; i < n; i++) {
-			double vf_val  = m_get_val(vf,  0, i);
-			double vfx_val = m_get_val(vfx, 0, i);
-			double vfy_val = m_get_val(vfy, 0, i);
-			double x_val = v_get_val(x, i);
-			double y_val = v_get_val(y, i);
-			double new_val = (vf_val + vfx_val * (x_val - xc) + vfy_val * (y_val - yc) - vfmean) / L;
-			m_set_val(vfr, 0, i, new_val);
-		}		
-		
-		// Update the snake center and snaxels
-		xc =  xc + (deltax * lambdaedge * vfxmean);
-		yc = (yc + (deltay * lambdaedge * vfymean) + (deltay * lambdapath * Ey)) / (1.0 + deltay * lambdapath);
-		double r_diff = 0.0;
-		for (i = 0; i < Np; i++) {
-			r[i] = (r[i] + (deltar * lambdaedge * m_get_val(vfr, 0, i)) + (deltar * lambdasize * Er)) /
-			       (1.0 + deltar * lambdasize);
-			r_diff += fabs(r[i] - r_old[i]);
-		}
-		
-		// Test for convergence
-		snakediff = fabs(xc - xc_old) + fabs(yc - yc_old) + r_diff;
-		
-		// Free temporary matrices
-		m_free(vf);
-		m_free(vfx);
-		m_free(vfy);
-		m_free(vfr);
-	    
-		iter++;
-	}
-	
-	// Set the return values
-	*xc0 = xc;
-	*yc0 = yc;
-	for (i = 0; i < Np; i++)
-		r0[i] = r[i];
-	
-	// Free memory
-	free(r); free(r_old);
-	v_free( x); v_free( y);
-	m_free(fx); m_free(fy);
+  /*
+  % ELLIPSEEVOLVE evolves a parametric snake according
+  %  to some energy constraints.
+  %
+  % INPUTS:
+  %   f............potential surface
+  %   xc0,yc0......initial center position
+  %   r0,t.........initial radii & angle vectors (with Np elements each)
+  %   Np...........number of snaxel points per snake
+  %   Er...........expected radius
+  %   Ey...........expected y position
+  %
+  % OUTPUTS
+  %   xc0,yc0.......final center position
+  %   r0...........final radii
+  %
+  % Matlab code written by: DREW GILLIAM (based on work by GANG DONG /
+  %                                                        NILANJAN RAY)
+  % Ported to C by: MICHAEL BOYER
+  */
+
+
+  // Constants
+  double deltax = 0.2;
+  double deltay = 0.2;
+  double deltar = 0.2;
+  double converge = 0.1;
+  double lambdaedge = 1;
+  double lambdasize = 0.2;
+  double lambdapath = 0.05;
+  int iterations = 1000;      // maximum number of iterations
+
+  int i, j;
+
+  // Initialize variables
+  double xc = *xc0;
+  double yc = *yc0;
+  double *r = (double *) malloc(sizeof(double) * Np);
+  for (i = 0; i < Np; i++) r[i] = r0[i];
+
+  // Compute the x- and y-gradients of the MGVF matrix
+  MAT *fx = gradient_x(f);
+  MAT *fy = gradient_y(f);
+
+  // Normalize the gradients
+  int fh = f->m, fw = f->n;
+  for (i = 0; i < fh; i++) {
+  for (j = 0; j < fw; j++) {
+  double temp_x = m_get_val(fx, i, j);
+  double temp_y = m_get_val(fy, i, j);
+  double fmag = sqrt((temp_x * temp_x) + (temp_y * temp_y));
+  // Fix 0/0 error
+  if (fmag > 1E-15) {
+  m_set_val(fx, i, j, temp_x / fmag);
+  m_set_val(fy, i, j, temp_y / fmag);
+  }	else {
+  m_set_val(fx, i, j, 0.0);
+  m_set_val(fy, i, j, 0.0);
+  }
+  /* m_set_val(fx, i, j, temp_x / fmag); */
+  /* m_set_val(fy, i, j, temp_y / fmag); */
+  }
+  }
+
+  double *r_old = (double *) malloc(sizeof(double) * Np);
+  VEC *x = v_get(Np);
+  VEC *y = v_get(Np);
+
+
+  // Evolve the snake
+  int iter = 0;
+  double snakediff = 1.0;
+  while (iter < iterations && snakediff > converge) {
+
+  // Save the values from the previous iteration
+  double xc_old = xc, yc_old = yc;
+  for (i = 0; i < Np; i++) {
+  r_old[i] = r[i];
+  }
+
+  // Compute the locations of the snaxels
+  for (i = 0; i < Np; i++) {
+  v_set_val(x, i, xc + r[i] * cos(t[i]));
+  v_set_val(y, i, yc + r[i] * sin(t[i]));
+  }
+
+  // See if any of the points in the snake are off the edge of the image
+  double min_x = v_get_val(x, 0), max_x = v_get_val(x, 0);
+  double min_y = v_get_val(y, 0), max_y = v_get_val(y, 0);
+  for (i = 1; i < Np; i++) {
+  double x_i = v_get_val(x, i);
+  if (x_i < min_x) min_x = x_i;
+  else if (x_i > max_x) max_x = x_i;
+  double y_i = v_get_val(y, i);
+  if (y_i < min_y) min_y = y_i;
+  else if (y_i > max_y) max_y = y_i;
+  }
+  if (min_x < 0.0 || max_x > (double) fw - 1.0 || min_y < 0 || max_y > (double) fh - 1.0) break;
+
+
+  // Compute the length of the snake
+  double L = 0.0;
+  for (i = 0; i < Np - 1; i++) {
+  double diff_x = v_get_val(x, i + 1) - v_get_val(x, i);
+  double diff_y = v_get_val(y, i + 1) - v_get_val(y, i);
+  L += sqrt((diff_x * diff_x) + (diff_y * diff_y));
+  }
+  double diff_x = v_get_val(x, 0) - v_get_val(x, Np - 1);
+  double diff_y = v_get_val(y, 0) - v_get_val(y, Np - 1);
+  L += sqrt((diff_x * diff_x) + (diff_y * diff_y));
+
+  // Compute the potential surface at each snaxel
+  MAT *vf  = linear_interp2(f,  x, y);
+  MAT *vfx = linear_interp2(fx, x, y);
+  MAT *vfy = linear_interp2(fy, x, y);
+
+  // Compute the average potential surface around the snake
+  double vfmean  = sum_m(vf ) / L;
+  double vfxmean = sum_m(vfx) / L;
+  double vfymean = sum_m(vfy) / L;
+
+  // Compute the radial potential surface
+  int m = vf->m, n = vf->n;
+  MAT *vfr = m_get(m, n);
+  for (i = 0; i < n; i++) {
+  double vf_val  = m_get_val(vf,  0, i);
+  double vfx_val = m_get_val(vfx, 0, i);
+  double vfy_val = m_get_val(vfy, 0, i);
+  double x_val = v_get_val(x, i);
+  double y_val = v_get_val(y, i);
+  double new_val = (vf_val + vfx_val * (x_val - xc) + vfy_val * (y_val - yc) - vfmean) / L;
+  m_set_val(vfr, 0, i, new_val);
+  }
+
+  // Update the snake center and snaxels
+  xc =  xc + (deltax * lambdaedge * vfxmean);
+  yc = (yc + (deltay * lambdaedge * vfymean) + (deltay * lambdapath * Ey)) / (1.0 + deltay * lambdapath);
+  double r_diff = 0.0;
+  for (i = 0; i < Np; i++) {
+  r[i] = (r[i] + (deltar * lambdaedge * m_get_val(vfr, 0, i)) + (deltar * lambdasize * Er)) /
+         (1.0 + deltar * lambdasize);
+  r_diff += fabs(r[i] - r_old[i]);
+  }
+
+  // Test for convergence
+  snakediff = fabs(xc - xc_old) + fabs(yc - yc_old) + r_diff;
+
+  // Free temporary matrices
+  m_free(vf);
+  m_free(vfx);
+  m_free(vfy);
+  m_free(vfr);
+
+  iter++;
+  }
+
+  // Set the return values
+  *xc0 = xc;
+  *yc0 = yc;
+  for (i = 0; i < Np; i++)
+  r0[i] = r[i];
+
+  // Free memory
+  free(r); free(r_old);
+  v_free( x); v_free( y);
+  m_free(fx); m_free(fy);
 }
 
 
 // Returns the sum of all of the elements in the specified matrix
 double sum_m(MAT *matrix) {
-	if (matrix == NULL) return 0.0;	
-	
-	int i, j;
-	double sum = 0.0;
-	for (i = 0; i < matrix->m; i++)
-		for (j = 0; j < matrix->n; j++)
-			sum += m_get_val(matrix, i, j);
-	
-	return sum;
+  if (matrix == NULL) return 0.0;
+
+  int i, j;
+  double sum = 0.0;
+  for (i = 0; i < matrix->m; i++)
+  for (j = 0; j < matrix->n; j++)
+  sum += m_get_val(matrix, i, j);
+
+  return sum;
 }
 
 
 // Returns the sum of all of the elements in the specified vector
 double sum_v(VEC *vector) {
-	if (vector == NULL) return 0.0;	
-	
-	int i;
-	double sum = 0.0;
-	for (i = 0; i < vector->dim; i++)
-		sum += v_get_val(vector, i);
-	
-	return sum;
+  if (vector == NULL) return 0.0;
+
+  int i;
+  double sum = 0.0;
+  for (i = 0; i < vector->dim; i++)
+  sum += v_get_val(vector, i);
+
+  return sum;
 }
 
 
 // Creates a zeroed x-by-y matrix of doubles
 double **alloc_2d_double(int x, int y) {
-	if (x < 1 || y < 1) return NULL;
-	
-	// Allocate the data and the pointers to the data
-	double *data = (double *) calloc(x * y, sizeof(double));
-	double **pointers = (double **) malloc(sizeof(double *) * x);
-	
-	// Make the pointers point to the data
-	int i;
-	for (i = 0; i < x; i++) {
-		pointers[i] = data + (i * y);
-	}
-	
-	return pointers;
+  if (x < 1 || y < 1) return NULL;
+
+  // Allocate the data and the pointers to the data
+  double *data = (double *) calloc(x * y, sizeof(double));
+  double **pointers = (double **) malloc(sizeof(double *) * x);
+
+  // Make the pointers point to the data
+  int i;
+  for (i = 0; i < x; i++) {
+  pointers[i] = data + (i * y);
+  }
+
+  return pointers;
 }
 
 
 // Creates a zeroed x-by-y-by-z matrix of doubles
 double ***alloc_3d_double(int x, int y, int z) {
-	if (x < 1 || y < 1 || z < 1) return NULL;
-	
-	// Allocate the data and the two levels of pointers
-	double *data = (double *) calloc(x * y * z, sizeof(double));
-	double **pointers_to_data = (double **) malloc(sizeof(double *) * x * y);
-	double ***pointers_to_pointers = (double ***) malloc(sizeof(double **) * x);
-	
-	// Make the pointers point to the data
-	int i;
-	for (i = 0; i < x * y; i++) pointers_to_data[i] = data + (i * z);
-	for (i = 0; i < x; i++) pointers_to_pointers[i] = pointers_to_data + (i * y);
-	
-	return pointers_to_pointers;
+  if (x < 1 || y < 1 || z < 1) return NULL;
+
+  // Allocate the data and the two levels of pointers
+  double *data = (double *) calloc(x * y * z, sizeof(double));
+  double **pointers_to_data = (double **) malloc(sizeof(double *) * x * y);
+  double ***pointers_to_pointers = (double ***) malloc(sizeof(double **) * x);
+
+  // Make the pointers point to the data
+  int i;
+  for (i = 0; i < x * y; i++) pointers_to_data[i] = data + (i * z);
+  for (i = 0; i < x; i++) pointers_to_pointers[i] = pointers_to_data + (i * y);
+
+  return pointers_to_pointers;
 }
 
 
 // Frees a 2d matrix generated by the alloc_2d_double function
 void free_2d_double(double **p) {
-	if (p != NULL) {
-		if (p[0] != NULL) free(p[0]);
-		free(p);
-	}
+  if (p != NULL) {
+  if (p[0] != NULL) free(p[0]);
+  free(p);
+  }
 }
 
 
 // Frees a 3d matrix generated by the alloc_3d_double function
 void free_3d_double(double ***p) {
-	if (p != NULL) {
-		if (p[0] != NULL) {
-			if (p[0][0] != NULL) free(p[0][0]);
-			free(p[0]);
-		}
-		free(p);
-	}
+  if (p != NULL) {
+  if (p[0] != NULL) {
+  if (p[0][0] != NULL) free(p[0][0]);
+  free(p[0]);
+  }
+  free(p);
+  }
 }

+ 171 - 171
opencl/leukocyte/OpenCL/track_ellipse_opencl.c

@@ -25,185 +25,185 @@ cl_kernel IMGVF_kernel;
 // Host function that launches an OpenCL kernel to compute the MGVF matrices for the specified cells
 void IMGVF_OpenCL(MAT **I, MAT **IMGVF, double vx, double vy, double e, int max_iterations, double cutoff, int num_cells) {
 
-	cl_int error;
-	
-	// Initialize the data on the GPU
-	IMGVF_OpenCL_init(I, num_cells);
-	
-	if (! compiled) {
-		// Load the kernel source from the file
-		const char *source = load_kernel_source("track_ellipse_kernel.cl");
-		size_t sourceSize = strlen(source);
-
-		// Compile the kernel
-		cl_program program = clCreateProgramWithSource(context, 1, &source, &sourceSize, &error);
-		check_error(error, __FILE__, __LINE__);
-		error = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
-		// Show compiler warnings/errors
-		static char log[65536]; memset(log, 0, sizeof(log));
-		clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(log)-1, log, NULL);
-		if (strstr(log,"warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);
-		check_error(error, __FILE__, __LINE__);
-
-		// Create the IMGVF kernels
-		IMGVF_kernel = clCreateKernel(program, "IMGVF_kernel", &error);
-		check_error(error, __FILE__, __LINE__);
-
-		// Record that compiling has already completed
-		compiled = TRUE;
-	}
-	
-	// Setup execution parameters
-	size_t num_work_groups = num_cells;
-	size_t global_work_size = num_work_groups * local_work_size;
-	
-	// Convert double-precision parameters to single-precision
-	float vx_float = (float) vx;
-	float vy_float = (float) vy;
-	float e_float = (float) e;
-	float cutoff_float = (float) cutoff;
-	
-	// Set the kernel arguments
-	clSetKernelArg(IMGVF_kernel, 0, sizeof(cl_mem), (void *) &device_IMGVF_all);
-	clSetKernelArg(IMGVF_kernel, 1, sizeof(cl_mem), (void *) &device_I_all);
-	clSetKernelArg(IMGVF_kernel, 2, sizeof(cl_mem), (void *) &device_I_offsets);
-	clSetKernelArg(IMGVF_kernel, 3, sizeof(cl_mem), (void *) &device_m_array);
-	clSetKernelArg(IMGVF_kernel, 4, sizeof(cl_mem), (void *) &device_n_array);
-	clSetKernelArg(IMGVF_kernel, 5, sizeof(cl_float), (void *) &vx_float);
-	clSetKernelArg(IMGVF_kernel, 6, sizeof(cl_float), (void *) &vy_float);
-	clSetKernelArg(IMGVF_kernel, 7, sizeof(cl_float), (void *) &e_float);
-	clSetKernelArg(IMGVF_kernel, 8, sizeof(cl_int), (void *) &max_iterations);
-	clSetKernelArg(IMGVF_kernel, 9, sizeof(cl_float), (void *) &cutoff_float);
-	
-	// Compute the MGVF on the GPU
-	error = clEnqueueNDRangeKernel(command_queue, IMGVF_kernel, 1, NULL, &global_work_size, &local_work_size, 0, NULL, NULL);
-	check_error(error, __FILE__, __LINE__);
-	
-	// Check for kernel errors
-	error = clFinish(command_queue);
-	check_error(error, __FILE__, __LINE__);
-	
-	// Copy back the final results from the GPU
-	IMGVF_OpenCL_cleanup(IMGVF, num_cells);
+  cl_int error;
+
+  // Initialize the data on the GPU
+  IMGVF_OpenCL_init(I, num_cells);
+
+  if (! compiled) {
+  // Load the kernel source from the file
+  const char *source = load_kernel_source("track_ellipse_kernel.cl");
+  size_t sourceSize = strlen(source);
+
+  // Compile the kernel
+  cl_program program = clCreateProgramWithSource(context, 1, &source, &sourceSize, &error);
+  check_error(error, __FILE__, __LINE__);
+  error = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
+  // Show compiler warnings/errors
+  static char log[65536]; memset(log, 0, sizeof(log));
+  clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(log)-1, log, NULL);
+  if (strstr(log,"warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);
+  check_error(error, __FILE__, __LINE__);
+
+  // Create the IMGVF kernels
+  IMGVF_kernel = clCreateKernel(program, "IMGVF_kernel", &error);
+  check_error(error, __FILE__, __LINE__);
+
+  // Record that compiling has already completed
+  compiled = TRUE;
+  }
+
+  // Setup execution parameters
+  size_t num_work_groups = num_cells;
+  size_t global_work_size = num_work_groups * local_work_size;
+
+  // Convert double-precision parameters to single-precision
+  float vx_float = (float) vx;
+  float vy_float = (float) vy;
+  float e_float = (float) e;
+  float cutoff_float = (float) cutoff;
+
+  // Set the kernel arguments
+  clSetKernelArg(IMGVF_kernel, 0, sizeof(cl_mem), (void *) &device_IMGVF_all);
+  clSetKernelArg(IMGVF_kernel, 1, sizeof(cl_mem), (void *) &device_I_all);
+  clSetKernelArg(IMGVF_kernel, 2, sizeof(cl_mem), (void *) &device_I_offsets);
+  clSetKernelArg(IMGVF_kernel, 3, sizeof(cl_mem), (void *) &device_m_array);
+  clSetKernelArg(IMGVF_kernel, 4, sizeof(cl_mem), (void *) &device_n_array);
+  clSetKernelArg(IMGVF_kernel, 5, sizeof(cl_float), (void *) &vx_float);
+  clSetKernelArg(IMGVF_kernel, 6, sizeof(cl_float), (void *) &vy_float);
+  clSetKernelArg(IMGVF_kernel, 7, sizeof(cl_float), (void *) &e_float);
+  clSetKernelArg(IMGVF_kernel, 8, sizeof(cl_int), (void *) &max_iterations);
+  clSetKernelArg(IMGVF_kernel, 9, sizeof(cl_float), (void *) &cutoff_float);
+
+  // Compute the MGVF on the GPU
+  error = clEnqueueNDRangeKernel(command_queue, IMGVF_kernel, 1, NULL, &global_work_size, &local_work_size, 0, NULL, NULL);
+  check_error(error, __FILE__, __LINE__);
+
+  // Check for kernel errors
+  error = clFinish(command_queue);
+  check_error(error, __FILE__, __LINE__);
+
+  // Copy back the final results from the GPU
+  IMGVF_OpenCL_cleanup(IMGVF, num_cells);
 }
 
 
 // Initializes data on the GPU for the MGVF kernel
 void IMGVF_OpenCL_init(MAT **IE, int num_cells) {
-	cl_int error;
-	
-	// Allocate array of offsets to each cell's image
-	size_t mem_size = sizeof(int) * num_cells;
-	host_I_offsets = (int *) malloc(mem_size);
-	device_I_offsets = clCreateBuffer(context, CL_MEM_READ_ONLY, mem_size, NULL, &error);
-	check_error(error, __FILE__, __LINE__);
-	
-	// Allocate arrays to hold the dimensions of each cell's image
-	host_m_array = (int *) malloc(mem_size);
-	host_n_array = (int *) malloc(mem_size);
-	device_m_array = clCreateBuffer(context, CL_MEM_READ_ONLY, mem_size, NULL, &error);
-	check_error(error, __FILE__, __LINE__);
-	device_n_array = clCreateBuffer(context, CL_MEM_READ_ONLY, mem_size, NULL, &error);
-	check_error(error, __FILE__, __LINE__);
-	
-	// Figure out the size of all of the matrices combined
-	int i, j, cell_num;
-	size_t total_size = 0;
-	for (cell_num = 0; cell_num < num_cells; cell_num++) {
-		MAT *I = IE[cell_num];
-		size_t size = I->m * I->n;
-		total_size += size;
-	}
-	total_mem_size = total_size * sizeof(float);
-	
-	// Allocate host memory just once for all cells
-	host_I_all = (float *) malloc(total_mem_size);
-	
-	// Allocate device memory just once for all cells
-	device_I_all = clCreateBuffer(context, CL_MEM_READ_ONLY, total_mem_size, NULL, &error);
-	check_error(error, __FILE__, __LINE__);
-	device_IMGVF_all = clCreateBuffer(context, CL_MEM_READ_WRITE, total_mem_size, NULL, &error);
-	check_error(error, __FILE__, __LINE__);
-	
-	// Copy each initial matrix into the allocated host memory
-	int offset = 0;
-	for (cell_num = 0; cell_num < num_cells; cell_num++) {
-		MAT *I = IE[cell_num];
-		
-		// Determine the size of the matrix
-		int m = I->m, n = I->n;
-		int size = m * n;
-		
-		// Store memory dimensions
-		host_m_array[cell_num] = m;
-		host_n_array[cell_num] = n;
-		
-		// Store offsets to this cell's image
-		host_I_offsets[cell_num] = offset;
-		
-		// Copy matrix I (which is also the initial IMGVF matrix) into the overall array
-		for (i = 0; i < m; i++)
-			for (j = 0; j < n; j++)
-				host_I_all[offset + (i * n) + j] = (float) m_get_val(I, i, j);
-		
-		offset += size;
-	}
-	
-	// Copy I matrices (which are also the initial IMGVF matrices) to device
-	error = clEnqueueWriteBuffer(command_queue, device_I_all, CL_TRUE, 0, total_mem_size, (void *) host_I_all, 0, NULL, NULL);
-	check_error(error, __FILE__, __LINE__);
-	error = clEnqueueWriteBuffer(command_queue, device_IMGVF_all, CL_TRUE, 0, total_mem_size, (void *) host_I_all, 0, NULL, NULL);
-	check_error(error, __FILE__, __LINE__);
-	
-	// Copy offsets array to device
-	error = clEnqueueWriteBuffer(command_queue, device_I_offsets, CL_TRUE, 0, mem_size, (void *) host_I_offsets, 0, NULL, NULL);
-	check_error(error, __FILE__, __LINE__);
-	
-	// Copy memory dimension arrays to device
-	error = clEnqueueWriteBuffer(command_queue, device_m_array, CL_TRUE, 0, mem_size, (void *) host_m_array, 0, NULL, NULL);
-	check_error(error, __FILE__, __LINE__);
-	error = clEnqueueWriteBuffer(command_queue, device_n_array, CL_TRUE, 0, mem_size, (void *) host_n_array, 0, NULL, NULL);
-	check_error(error, __FILE__, __LINE__);
+  cl_int error;
+
+  // Allocate array of offsets to each cell's image
+  size_t mem_size = sizeof(int) * num_cells;
+  host_I_offsets = (int *) malloc(mem_size);
+  device_I_offsets = clCreateBuffer(context, CL_MEM_READ_ONLY, mem_size, NULL, &error);
+  check_error(error, __FILE__, __LINE__);
+
+  // Allocate arrays to hold the dimensions of each cell's image
+  host_m_array = (int *) malloc(mem_size);
+  host_n_array = (int *) malloc(mem_size);
+  device_m_array = clCreateBuffer(context, CL_MEM_READ_ONLY, mem_size, NULL, &error);
+  check_error(error, __FILE__, __LINE__);
+  device_n_array = clCreateBuffer(context, CL_MEM_READ_ONLY, mem_size, NULL, &error);
+  check_error(error, __FILE__, __LINE__);
+
+  // Figure out the size of all of the matrices combined
+  int i, j, cell_num;
+  size_t total_size = 0;
+  for (cell_num = 0; cell_num < num_cells; cell_num++) {
+  MAT *I = IE[cell_num];
+  size_t size = I->m * I->n;
+  total_size += size;
+  }
+  total_mem_size = total_size * sizeof(float);
+
+  // Allocate host memory just once for all cells
+  host_I_all = (float *) malloc(total_mem_size);
+
+  // Allocate device memory just once for all cells
+  device_I_all = clCreateBuffer(context, CL_MEM_READ_ONLY, total_mem_size, NULL, &error);
+  check_error(error, __FILE__, __LINE__);
+  device_IMGVF_all = clCreateBuffer(context, CL_MEM_READ_WRITE, total_mem_size, NULL, &error);
+  check_error(error, __FILE__, __LINE__);
+
+  // Copy each initial matrix into the allocated host memory
+  int offset = 0;
+  for (cell_num = 0; cell_num < num_cells; cell_num++) {
+  MAT *I = IE[cell_num];
+
+  // Determine the size of the matrix
+  int m = I->m, n = I->n;
+  int size = m * n;
+
+  // Store memory dimensions
+  host_m_array[cell_num] = m;
+  host_n_array[cell_num] = n;
+
+  // Store offsets to this cell's image
+  host_I_offsets[cell_num] = offset;
+
+  // Copy matrix I (which is also the initial IMGVF matrix) into the overall array
+  for (i = 0; i < m; i++)
+  for (j = 0; j < n; j++)
+  host_I_all[offset + (i * n) + j] = (float) m_get_val(I, i, j);
+
+  offset += size;
+  }
+
+  // Copy I matrices (which are also the initial IMGVF matrices) to device
+  error = clEnqueueWriteBuffer(command_queue, device_I_all, CL_TRUE, 0, total_mem_size, (void *) host_I_all, 0, NULL, NULL);
+  check_error(error, __FILE__, __LINE__);
+  error = clEnqueueWriteBuffer(command_queue, device_IMGVF_all, CL_TRUE, 0, total_mem_size, (void *) host_I_all, 0, NULL, NULL);
+  check_error(error, __FILE__, __LINE__);
+
+  // Copy offsets array to device
+  error = clEnqueueWriteBuffer(command_queue, device_I_offsets, CL_TRUE, 0, mem_size, (void *) host_I_offsets, 0, NULL, NULL);
+  check_error(error, __FILE__, __LINE__);
+
+  // Copy memory dimension arrays to device
+  error = clEnqueueWriteBuffer(command_queue, device_m_array, CL_TRUE, 0, mem_size, (void *) host_m_array, 0, NULL, NULL);
+  check_error(error, __FILE__, __LINE__);
+  error = clEnqueueWriteBuffer(command_queue, device_n_array, CL_TRUE, 0, mem_size, (void *) host_n_array, 0, NULL, NULL);
+  check_error(error, __FILE__, __LINE__);
 }
 
 
 // Copies the results of the MGVF kernel back to the host
 void IMGVF_OpenCL_cleanup(MAT **IMGVF_out_array, int num_cells) {
-	cl_int error;
-	
-	// Copy the result matrices from the device to the host
-	float *host_IMGVF_all = (cl_float *) clEnqueueMapBuffer(command_queue, device_IMGVF_all, CL_TRUE, CL_MAP_READ, 0, total_mem_size, 0, NULL, NULL, &error);
-	check_error(error, __FILE__, __LINE__);
-	
-	// Copy each result matrix into its appropriate host matrix
-	int cell_num, offset = 0;	
-	for (cell_num = 0; cell_num < num_cells; cell_num++) {
-		MAT *IMGVF_out = IMGVF_out_array[cell_num];
-		
-		// Determine the size of the matrix
-		int m = IMGVF_out->m, n = IMGVF_out->n, i, j;
-		// Pack the result into the matrix
-		for (i = 0; i < m; i++)
-			for (j = 0; j < n; j++)
-				m_set_val(IMGVF_out, i, j, (double) host_IMGVF_all[offset + (i * n) + j]);
-		
-		offset += (m * n);
-	}
-	
-	// Unmap results buffer
-	error = clEnqueueUnmapMemObject(command_queue, device_IMGVF_all, (void *) host_IMGVF_all, 0, NULL, NULL);
-	check_error(error, __FILE__, __LINE__);
-	
-	// Free device memory
-	clReleaseMemObject(device_m_array);
-	clReleaseMemObject(device_n_array);
-	clReleaseMemObject(device_IMGVF_all);
-	clReleaseMemObject(device_I_all);
-	clReleaseMemObject(device_I_offsets);
-	
-	// Free host memory
-	free(host_m_array);
-	free(host_n_array);
-	free(host_I_all);
-	free(host_I_offsets);
+  cl_int error;
+
+  // Copy the result matrices from the device to the host
+  float *host_IMGVF_all = (cl_float *) clEnqueueMapBuffer(command_queue, device_IMGVF_all, CL_TRUE, CL_MAP_READ, 0, total_mem_size, 0, NULL, NULL, &error);
+  check_error(error, __FILE__, __LINE__);
+
+  // Copy each result matrix into its appropriate host matrix
+  int cell_num, offset = 0;
+  for (cell_num = 0; cell_num < num_cells; cell_num++) {
+  MAT *IMGVF_out = IMGVF_out_array[cell_num];
+
+  // Determine the size of the matrix
+  int m = IMGVF_out->m, n = IMGVF_out->n, i, j;
+  // Pack the result into the matrix
+  for (i = 0; i < m; i++)
+  for (j = 0; j < n; j++)
+  m_set_val(IMGVF_out, i, j, (double) host_IMGVF_all[offset + (i * n) + j]);
+
+  offset += (m * n);
+  }
+
+  // Unmap results buffer
+  error = clEnqueueUnmapMemObject(command_queue, device_IMGVF_all, (void *) host_IMGVF_all, 0, NULL, NULL);
+  check_error(error, __FILE__, __LINE__);
+
+  // Free device memory
+  clReleaseMemObject(device_m_array);
+  clReleaseMemObject(device_n_array);
+  clReleaseMemObject(device_IMGVF_all);
+  clReleaseMemObject(device_I_all);
+  clReleaseMemObject(device_I_offsets);
+
+  // Free host memory
+  free(host_m_array);
+  free(host_n_array);
+  free(host_I_all);
+  free(host_I_offsets);
 }

+ 235 - 237
opencl/lud/ocl/lud.cpp

@@ -52,54 +52,54 @@ static cl_int           num_devices;
 
 static int initialize(int platform_id, int device_id, int use_gpu)
 {
-	cl_int result;
-	size_t size;
-	
-	// modify a little bit the initialization of the platforms to handle also the case in which we have more than one platform on our system.
-	cl_uint platformCount;
-	cl_platform_id *platforms_ids;
-	
-	// create OpenCL context
-	clGetPlatformIDs(0, NULL, &platformCount);
-	platforms_ids = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount);
-	if (clGetPlatformIDs(platformCount, platforms_ids, NULL) != CL_SUCCESS) { printf("ERROR: clGetPlatformIDs(1,*,0) failed\n"); return -1; }
-	cl_context_properties ctxprop[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platforms_ids[platform_id], 0};
-	device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
-	context = clCreateContextFromType( ctxprop, device_type, NULL, NULL, NULL );
-	if( !context ) { printf("ERROR: clCreateContextFromType(%s) failed\n", use_gpu ? "GPU" : "CPU"); return -1; }
-
-	// get the list of GPUs/CPUs
-	result = clGetContextInfo( context, CL_CONTEXT_DEVICES, 0, NULL, &size );
-	num_devices = (int) (size / sizeof(cl_device_id));
-	printf("num_devices = %d\n", num_devices);
-	
-	if( result != CL_SUCCESS || num_devices < 1 ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
-	device_list = new cl_device_id[num_devices];
-	if( !device_list ) { printf("ERROR: new cl_device_id[] failed\n"); return -1; }
-	result = clGetContextInfo( context, CL_CONTEXT_DEVICES, size, device_list, NULL );
-	if( result != CL_SUCCESS ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
-
-	// create command queue for the device passed as parameter
-	cmd_queue = clCreateCommandQueue( context, device_list[device_id], 0, NULL );
-	if( !cmd_queue ) { printf("ERROR: clCreateCommandQueue() failed\n"); return -1; }
-	return 0;
+  cl_int result;
+  size_t size;
+
+  // modify a little bit the initialization of the platforms to handle also the case in which we have more than one platform on our system.
+  cl_uint platformCount;
+  cl_platform_id *platforms_ids;
+
+  // create OpenCL context
+  clGetPlatformIDs(0, NULL, &platformCount);
+  platforms_ids = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount);
+  if (clGetPlatformIDs(platformCount, platforms_ids, NULL) != CL_SUCCESS) { printf("ERROR: clGetPlatformIDs(1,*,0) failed\n"); return -1; }
+  cl_context_properties ctxprop[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platforms_ids[platform_id], 0};
+  device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
+  context = clCreateContextFromType( ctxprop, device_type, NULL, NULL, NULL );
+  if( !context ) { printf("ERROR: clCreateContextFromType(%s) failed\n", use_gpu ? "GPU" : "CPU"); return -1; }
+
+  // get the list of GPUs/CPUs
+  result = clGetContextInfo( context, CL_CONTEXT_DEVICES, 0, NULL, &size );
+  num_devices = (int) (size / sizeof(cl_device_id));
+  printf("num_devices = %d\n", num_devices);
+
+  if( result != CL_SUCCESS || num_devices < 1 ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
+  device_list = new cl_device_id[num_devices];
+  if( !device_list ) { printf("ERROR: new cl_device_id[] failed\n"); return -1; }
+  result = clGetContextInfo( context, CL_CONTEXT_DEVICES, size, device_list, NULL );
+  if( result != CL_SUCCESS ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
+
+  // create command queue for the device passed as parameter
+  cmd_queue = clCreateCommandQueue( context, device_list[device_id], 0, NULL );
+  if( !cmd_queue ) { printf("ERROR: clCreateCommandQueue() failed\n"); return -1; }
+  return 0;
 }
 
 static int shutdown()
 {
-	// release resources
-	if( cmd_queue ) clReleaseCommandQueue( cmd_queue );
-	if( context ) clReleaseContext( context );
-	if( device_list ) delete device_list;
-
-	// reset all variables
-	cmd_queue = 0;
-	context = 0;
-	device_list = 0;
-	num_devices = 0;
-	device_type = 0;
-
-	return 0;
+  // release resources
+  if( cmd_queue ) clReleaseCommandQueue( cmd_queue );
+  if( context ) clReleaseContext( context );
+  if( device_list ) delete device_list;
+
+  // reset all variables
+  cmd_queue = 0;
+  context = 0;
+  device_list = 0;
+  num_devices = 0;
+  device_type = 0;
+
+  return 0;
 }
 
 static int do_verify = 0;
@@ -117,218 +117,216 @@ int
 main ( int argc, char *argv[] )
 {
   printf("WG size of kernel = %d X %d\n", BLOCK_SIZE, BLOCK_SIZE);
-	int matrix_dim = 32; /* default matrix_dim */
-	int opt, option_index=0;
-	func_ret_t ret;
-	const char *input_file = NULL;
-	float *m, *mm;
-	stopwatch sw;
-  
+  int matrix_dim = 32; /* default matrix_dim */
+  int opt, option_index=0;
+  func_ret_t ret;
+  const char *input_file = NULL;
+  float *m, *mm;
+  stopwatch sw;
+
   // Variables to store information on platform and device to use_gpu
   int platform_id = 0;
   int device_id = 0;
   int use_gpu = 0;
-	
-	while ((opt = getopt_long(argc, argv, "::vs:i:p:d:g:", 
+
+  while ((opt = getopt_long(argc, argv, "::vs:i:p:d:g:",
                             long_options, &option_index)) != -1 ) {
-		switch(opt){
-			case 'i':
-			   input_file = optarg;
-			   break;
-			case 'v':
-			   do_verify = 1;
-			   break;
+  switch(opt){
+  case 'i':
+     input_file = optarg;
+     break;
+  case 'v':
+     do_verify = 1;
+     break;
       case 's':
-			   matrix_dim = atoi(optarg);
-			   printf("Generate input matrix internally, size =%d\n", matrix_dim);
-			   break;
+     matrix_dim = atoi(optarg);
+     printf("Generate input matrix internally, size =%d\n", matrix_dim);
+     break;
       case 'p':
-			   platform_id = atoi(optarg);
-			   break;
+     platform_id = atoi(optarg);
+     break;
       case 'd':
   		   device_id = atoi(optarg);
   		   break;
       case 'g':
          use_gpu = atoi(optarg);
-         break;            
+         break;
       case '?':
-			   fprintf(stderr, "invalid option\n");
-			   break;
+     fprintf(stderr, "invalid option\n");
+     break;
       case ':':
-			   fprintf(stderr, "missing argument\n");
-			   break;
+     fprintf(stderr, "missing argument\n");
+     break;
       default:
-			   fprintf(stderr, "Usage: %s [-v] [-s matrix_size|-i input_file] -p platform -d device -g use_gpu\n",
+     fprintf(stderr, "Usage: %s [-v] [-s matrix_size|-i input_file] -p platform -d device -g use_gpu\n",
                  argv[0]);
-			   exit(EXIT_FAILURE);
-		}
-	}
-  
-	if ( (optind < argc) || (optind == 1)) {
-		fprintf(stderr, "Usage: %s [-v] [-s matrix_size|-i input_file]\n", argv[0]);
-		exit(EXIT_FAILURE);
-	}	
-
-	if (input_file) {
-		printf("Reading matrix from file %s\n", input_file);
-		ret = create_matrix_from_file(&m, input_file, &matrix_dim);
-		if (ret != RET_SUCCESS) {
-			m = NULL;
-			fprintf(stderr, "error create matrix from file %s\n", input_file);
-			exit(EXIT_FAILURE);
-		}
-	} 
-	
-	else if (matrix_dim) {
-	  printf("Creating matrix internally size=%d\n", matrix_dim);
-	  ret = create_matrix(&m, matrix_dim);
-	  if (ret != RET_SUCCESS) {
-	    m = NULL;
-	    fprintf(stderr, "error create matrix internally size=%d\n", matrix_dim);
-	    exit(EXIT_FAILURE);
-	  }
-	}
-
-	else {
-	  printf("No input file specified!\n");
-	  exit(EXIT_FAILURE);
-	}
-
-	if (do_verify){
-		printf("Before LUD\n");
-		// print_matrix(m, matrix_dim);
-		matrix_duplicate(m, &mm, matrix_dim);
-	}
-	
-	int sourcesize = 1024*1024;
-	char * source = (char *)calloc(sourcesize, sizeof(char)); 
-	if(!source) { printf("ERROR: calloc(%d) failed\n", sourcesize); return -1; }
-
-	char * kernel_lud_diag   = "lud_diagonal";
-	char * kernel_lud_peri   = "lud_perimeter";
-	char * kernel_lud_inter  = "lud_internal";
-	FILE * fp = fopen("./lud_kernel.cl", "rb"); 
-	if(!fp) { printf("ERROR: unable to open '%s'\n"); return -1; }
-	fread(source + strlen(source), sourcesize, 1, fp);
-	fclose(fp);
-
-	// OpenCL initialization
-	if(initialize(platform_id, device_id, use_gpu)) return -1;
-	// compile kernel
-	cl_int err = 0;
-	const char * slist[2] = { source, 0 };
-	cl_program prog = clCreateProgramWithSource(context, 1, slist, NULL, &err);
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateProgramWithSource() => %d\n", err); return -1; }
-	char clOptions[110];
-	//  sprintf(clOptions,"-I../../src"); 
-	sprintf(clOptions," ");
+     exit(EXIT_FAILURE);
+  }
+  }
+
+  if ( (optind < argc) || (optind == 1)) {
+  fprintf(stderr, "Usage: %s [-v] [-s matrix_size|-i input_file]\n", argv[0]);
+  exit(EXIT_FAILURE);
+  }
+
+  if (input_file) {
+  printf("Reading matrix from file %s\n", input_file);
+  ret = create_matrix_from_file(&m, input_file, &matrix_dim);
+  if (ret != RET_SUCCESS) {
+  m = NULL;
+  fprintf(stderr, "error create matrix from file %s\n", input_file);
+  exit(EXIT_FAILURE);
+  }
+  }
+
+  else if (matrix_dim) {
+    printf("Creating matrix internally size=%d\n", matrix_dim);
+    ret = create_matrix(&m, matrix_dim);
+    if (ret != RET_SUCCESS) {
+      m = NULL;
+      fprintf(stderr, "error create matrix internally size=%d\n", matrix_dim);
+      exit(EXIT_FAILURE);
+    }
+  }
+
+  else {
+    printf("No input file specified!\n");
+    exit(EXIT_FAILURE);
+  }
+
+  if (do_verify){
+  printf("Before LUD\n");
+  // print_matrix(m, matrix_dim);
+  matrix_duplicate(m, &mm, matrix_dim);
+  }
+
+  int sourcesize = 1024*1024;
+  char * source = (char *)calloc(sourcesize, sizeof(char));
+  if(!source) { printf("ERROR: calloc(%d) failed\n", sourcesize); return -1; }
+
+  char * kernel_lud_diag   = "lud_diagonal";
+  char * kernel_lud_peri   = "lud_perimeter";
+  char * kernel_lud_inter  = "lud_internal";
+  FILE * fp = fopen("./lud_kernel.cl", "rb");
+  if(!fp) { printf("ERROR: unable to open '%s'\n"); return -1; }
+  fread(source + strlen(source), sourcesize, 1, fp);
+  fclose(fp);
+
+  // OpenCL initialization
+  if(initialize(platform_id, device_id, use_gpu)) return -1;
+  // compile kernel
+  cl_int err = 0;
+  const char * slist[2] = { source, 0 };
+  cl_program prog = clCreateProgramWithSource(context, 1, slist, NULL, &err);
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateProgramWithSource() => %d\n", err); return -1; }
+  char clOptions[110];
+  //  sprintf(clOptions,"-I../../src");
+  sprintf(clOptions," ");
 #ifdef BLOCK_SIZE
-	sprintf(clOptions + strlen(clOptions), " -DBLOCK_SIZE=%d", BLOCK_SIZE);
+  sprintf(clOptions + strlen(clOptions), " -DBLOCK_SIZE=%d", BLOCK_SIZE);
 #endif
 
-	err = clBuildProgram(prog, 0, NULL, clOptions, NULL, NULL);
-	{ // show warnings/errors
-		//static char log[65536]; memset(log, 0, sizeof(log));
-		//cl_device_id device_id = 0;
-		//err = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(device_id), &device_id, NULL);
-		//clGetProgramBuildInfo(prog, device_id, CL_PROGRAM_BUILD_LOG, sizeof(log)-1, log, NULL);
-		//if(err || strstr(log,"warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);
-	}
-	if(err != CL_SUCCESS) { printf("ERROR: clBuildProgram() => %d\n", err); return -1; }
-    
-	cl_kernel diagnal;
-	cl_kernel perimeter;
-	cl_kernel internal;
-	diagnal   = clCreateKernel(prog, kernel_lud_diag, &err);  
-	perimeter = clCreateKernel(prog, kernel_lud_peri, &err);  
-	internal  = clCreateKernel(prog, kernel_lud_inter, &err);  
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateKernel() 0 => %d\n", err); return -1; }
-	clReleaseProgram(prog);
-  
-	//size_t local_work[3] = { 1, 1, 1 };
-	//size_t global_work[3] = {1, 1, 1 }; 
-  
-	cl_mem d_m;
-	d_m = clCreateBuffer(context, CL_MEM_READ_WRITE, matrix_dim*matrix_dim * sizeof(float), NULL, &err );
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer d_m (size:%d) => %d\n", matrix_dim*matrix_dim, err); return -1;} 
-
-	/* beginning of timing point */
-	stopwatch_start(&sw);
-	err = clEnqueueWriteBuffer(cmd_queue, d_m, 1, 0, matrix_dim*matrix_dim*sizeof(float), m, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer d_m (size:%d) => %d\n", matrix_dim*matrix_dim, err); return -1; }
-	
-	int i=0;
-	for (i=0; i < matrix_dim-BLOCK_SIZE; i += BLOCK_SIZE) {
-	 
-	  clSetKernelArg(diagnal, 0, sizeof(void *), (void*) &d_m);
-	  clSetKernelArg(diagnal, 1, sizeof(float) * BLOCK_SIZE * BLOCK_SIZE, (void*)NULL );
-	  clSetKernelArg(diagnal, 2, sizeof(cl_int), (void*) &matrix_dim);
-	  clSetKernelArg(diagnal, 3, sizeof(cl_int), (void*) &i);
-      
-	  size_t global_work1[3]  = {BLOCK_SIZE, 1, 1};
-	  size_t local_work1[3]  = {BLOCK_SIZE, 1, 1};
-	   
-	  err = clEnqueueNDRangeKernel(cmd_queue, diagnal, 2, NULL, global_work1, local_work1, 0, 0, 0);
-	  if(err != CL_SUCCESS) { printf("ERROR:  diagnal clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }	
-	  
-	  clSetKernelArg(perimeter, 0, sizeof(void *), (void*) &d_m);
-	  clSetKernelArg(perimeter, 1, sizeof(float) * BLOCK_SIZE * BLOCK_SIZE, (void*)NULL );
-	  clSetKernelArg(perimeter, 2, sizeof(float) * BLOCK_SIZE * BLOCK_SIZE, (void*)NULL );
-	  clSetKernelArg(perimeter, 3, sizeof(float) * BLOCK_SIZE * BLOCK_SIZE, (void*)NULL );
-	  clSetKernelArg(perimeter, 4, sizeof(cl_int), (void*) &matrix_dim);
-	  clSetKernelArg(perimeter, 5, sizeof(cl_int), (void*) &i);
-	  
-	  size_t global_work2[3] = {BLOCK_SIZE * 2 * ((matrix_dim-i)/BLOCK_SIZE-1), 1, 1};
-	  size_t local_work2[3]  = {BLOCK_SIZE * 2, 1, 1};
-	  
-	  err = clEnqueueNDRangeKernel(cmd_queue, perimeter, 2, NULL, global_work2, local_work2, 0, 0, 0);
-	  if(err != CL_SUCCESS) { printf("ERROR:  perimeter clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }	
-	  
-	  clSetKernelArg(internal, 0, sizeof(void *), (void*) &d_m);
-	  clSetKernelArg(internal, 1, sizeof(float) * BLOCK_SIZE * BLOCK_SIZE, (void*)NULL );
-	  clSetKernelArg(internal, 2, sizeof(float) * BLOCK_SIZE * BLOCK_SIZE, (void*)NULL );
-	  clSetKernelArg(internal, 3, sizeof(cl_int), (void*) &matrix_dim);
-	  clSetKernelArg(internal, 4, sizeof(cl_int), (void*) &i);
-      
-	  size_t global_work3[3] = {BLOCK_SIZE * ((matrix_dim-i)/BLOCK_SIZE-1), BLOCK_SIZE * ((matrix_dim-i)/BLOCK_SIZE-1), 1};
-	  size_t local_work3[3] = {BLOCK_SIZE, BLOCK_SIZE, 1};
-
-	  err = clEnqueueNDRangeKernel(cmd_queue, internal, 2, NULL, global_work3, local_work3, 0, 0, 0);
-	  if(err != CL_SUCCESS) { printf("ERROR:  internal clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }	
-	}
-	clSetKernelArg(diagnal, 0, sizeof(void *), (void*) &d_m);
-	clSetKernelArg(diagnal, 1, sizeof(float) * BLOCK_SIZE * BLOCK_SIZE, (void*)NULL );
-	clSetKernelArg(diagnal, 2, sizeof(cl_int), (void*) &matrix_dim);
-	clSetKernelArg(diagnal, 3, sizeof(cl_int), (void*) &i);
-      
-	size_t global_work1[3]  = {BLOCK_SIZE, 1, 1};
-	size_t local_work1[3]  = {BLOCK_SIZE, 1, 1};
-	err = clEnqueueNDRangeKernel(cmd_queue, diagnal, 2, NULL, global_work1, local_work1, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR:  diagnal clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }	
-	
-	err = clEnqueueReadBuffer(cmd_queue, d_m, 1, 0, matrix_dim*matrix_dim*sizeof(float), m, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: clEnqueueReadBuffer  d_m (size:%d) => %d\n", matrix_dim*matrix_dim, err); return -1; }
-	clFinish(cmd_queue);
-	/* end of timing point */
-	stopwatch_stop(&sw);
-	printf("Time consumed(ms): %lf\n", 1000*get_interval_by_sec(&sw));
-
-	clReleaseMemObject(d_m);
-
-	if (do_verify){
-		printf("After LUD\n");
-		// print_matrix(m, matrix_dim);
-		printf(">>>Verify<<<<\n");
-		lud_verify(mm, m, matrix_dim); 
-		free(mm);
-	}
-
-	free(m);
-	
-	if(shutdown()) return -1;
-	
-}				
+  err = clBuildProgram(prog, 0, NULL, clOptions, NULL, NULL);
+  { // show warnings/errors
+  //static char log[65536]; memset(log, 0, sizeof(log));
+  //cl_device_id device_id = 0;
+  //err = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(device_id), &device_id, NULL);
+  //clGetProgramBuildInfo(prog, device_id, CL_PROGRAM_BUILD_LOG, sizeof(log)-1, log, NULL);
+  //if(err || strstr(log,"warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);
+  }
+  if(err != CL_SUCCESS) { printf("ERROR: clBuildProgram() => %d\n", err); return -1; }
 
-/* ----------  end of function main  ---------- */
+  cl_kernel diagnal;
+  cl_kernel perimeter;
+  cl_kernel internal;
+  diagnal   = clCreateKernel(prog, kernel_lud_diag, &err);
+  perimeter = clCreateKernel(prog, kernel_lud_peri, &err);
+  internal  = clCreateKernel(prog, kernel_lud_inter, &err);
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateKernel() 0 => %d\n", err); return -1; }
+  clReleaseProgram(prog);
+
+  //size_t local_work[3] = { 1, 1, 1 };
+  //size_t global_work[3] = {1, 1, 1 };
+
+  cl_mem d_m;
+  d_m = clCreateBuffer(context, CL_MEM_READ_WRITE, matrix_dim*matrix_dim * sizeof(float), NULL, &err );
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer d_m (size:%d) => %d\n", matrix_dim*matrix_dim, err); return -1;}
 
+  /* beginning of timing point */
+  stopwatch_start(&sw);
+  err = clEnqueueWriteBuffer(cmd_queue, d_m, 1, 0, matrix_dim*matrix_dim*sizeof(float), m, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer d_m (size:%d) => %d\n", matrix_dim*matrix_dim, err); return -1; }
 
+  int i=0;
+  for (i=0; i < matrix_dim-BLOCK_SIZE; i += BLOCK_SIZE) {
+
+    clSetKernelArg(diagnal, 0, sizeof(void *), (void*) &d_m);
+    clSetKernelArg(diagnal, 1, sizeof(float) * BLOCK_SIZE * BLOCK_SIZE, (void*)NULL );
+    clSetKernelArg(diagnal, 2, sizeof(cl_int), (void*) &matrix_dim);
+    clSetKernelArg(diagnal, 3, sizeof(cl_int), (void*) &i);
+
+    size_t global_work1[3]  = {BLOCK_SIZE, 1, 1};
+    size_t local_work1[3]  = {BLOCK_SIZE, 1, 1};
+
+    err = clEnqueueNDRangeKernel(cmd_queue, diagnal, 2, NULL, global_work1, local_work1, 0, 0, 0);
+    if(err != CL_SUCCESS) { printf("ERROR:  diagnal clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }
+
+    clSetKernelArg(perimeter, 0, sizeof(void *), (void*) &d_m);
+    clSetKernelArg(perimeter, 1, sizeof(float) * BLOCK_SIZE * BLOCK_SIZE, (void*)NULL );
+    clSetKernelArg(perimeter, 2, sizeof(float) * BLOCK_SIZE * BLOCK_SIZE, (void*)NULL );
+    clSetKernelArg(perimeter, 3, sizeof(float) * BLOCK_SIZE * BLOCK_SIZE, (void*)NULL );
+    clSetKernelArg(perimeter, 4, sizeof(cl_int), (void*) &matrix_dim);
+    clSetKernelArg(perimeter, 5, sizeof(cl_int), (void*) &i);
+
+    size_t global_work2[3] = {BLOCK_SIZE * 2 * ((matrix_dim-i)/BLOCK_SIZE-1), 1, 1};
+    size_t local_work2[3]  = {BLOCK_SIZE * 2, 1, 1};
+
+    err = clEnqueueNDRangeKernel(cmd_queue, perimeter, 2, NULL, global_work2, local_work2, 0, 0, 0);
+    if(err != CL_SUCCESS) { printf("ERROR:  perimeter clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }
+
+    clSetKernelArg(internal, 0, sizeof(void *), (void*) &d_m);
+    clSetKernelArg(internal, 1, sizeof(float) * BLOCK_SIZE * BLOCK_SIZE, (void*)NULL );
+    clSetKernelArg(internal, 2, sizeof(float) * BLOCK_SIZE * BLOCK_SIZE, (void*)NULL );
+    clSetKernelArg(internal, 3, sizeof(cl_int), (void*) &matrix_dim);
+    clSetKernelArg(internal, 4, sizeof(cl_int), (void*) &i);
+
+    size_t global_work3[3] = {BLOCK_SIZE * ((matrix_dim-i)/BLOCK_SIZE-1), BLOCK_SIZE * ((matrix_dim-i)/BLOCK_SIZE-1), 1};
+    size_t local_work3[3] = {BLOCK_SIZE, BLOCK_SIZE, 1};
+
+    err = clEnqueueNDRangeKernel(cmd_queue, internal, 2, NULL, global_work3, local_work3, 0, 0, 0);
+    if(err != CL_SUCCESS) { printf("ERROR:  internal clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }
+  }
+  clSetKernelArg(diagnal, 0, sizeof(void *), (void*) &d_m);
+  clSetKernelArg(diagnal, 1, sizeof(float) * BLOCK_SIZE * BLOCK_SIZE, (void*)NULL );
+  clSetKernelArg(diagnal, 2, sizeof(cl_int), (void*) &matrix_dim);
+  clSetKernelArg(diagnal, 3, sizeof(cl_int), (void*) &i);
+
+  size_t global_work1[3]  = {BLOCK_SIZE, 1, 1};
+  size_t local_work1[3]  = {BLOCK_SIZE, 1, 1};
+  err = clEnqueueNDRangeKernel(cmd_queue, diagnal, 2, NULL, global_work1, local_work1, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR:  diagnal clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }
+
+  err = clEnqueueReadBuffer(cmd_queue, d_m, 1, 0, matrix_dim*matrix_dim*sizeof(float), m, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: clEnqueueReadBuffer  d_m (size:%d) => %d\n", matrix_dim*matrix_dim, err); return -1; }
+  clFinish(cmd_queue);
+  /* end of timing point */
+  stopwatch_stop(&sw);
+  printf("Time consumed(ms): %lf\n", 1000*get_interval_by_sec(&sw));
+
+  clReleaseMemObject(d_m);
+
+  if (do_verify){
+  printf("After LUD\n");
+  // print_matrix(m, matrix_dim);
+  printf(">>>Verify<<<<\n");
+  lud_verify(mm, m, matrix_dim);
+  free(mm);
+  }
+
+  free(m);
+
+  if(shutdown()) return -1;
+
+}
+
+/* ----------  end of function main  ---------- */

+ 251 - 252
opencl/nn/clutils.cpp

@@ -78,10 +78,10 @@ static cl_platform_id platform = NULL;
 static cl_device_id device = NULL;
 
 //! OpenCL context
-static cl_context context = NULL;        
+static cl_context context = NULL;
 
 //! OpenCL command queue
-static cl_command_queue commandQueue = NULL;  
+static cl_command_queue commandQueue = NULL;
 static cl_command_queue commandQueueProf = NULL;
 static cl_command_queue commandQueueNoProf = NULL;
 
@@ -96,25 +96,25 @@ static bool eventsEnabled = false;
 //! Initialize OpenCl environment on one device
 /*!
     Init function for one device. Looks for supported devices and creates a context
-    \return returns a context initialized 
+    \return returns a context initialized
 */
-cl_context cl_init(char devicePreference) 
+cl_context cl_init(char devicePreference)
 {
-    cl_int status;   
+    cl_int status;
 
     // Discover and populate the platforms
     status = clGetPlatformIDs(0, NULL, &numPlatforms);
     cl_errChk(status, "Getting platform IDs", true);
-    if (numPlatforms > 0) 
+    if (numPlatforms > 0)
     {
         // Get all the platforms
-        platforms = (cl_platform_id*)alloc(numPlatforms * 
+        platforms = (cl_platform_id*)alloc(numPlatforms *
             sizeof(cl_platform_id));
 
         status = clGetPlatformIDs(numPlatforms, platforms, NULL);
         cl_errChk(status, "Getting platform IDs", true);
     }
-    else 
+    else
     {
         // If no platforms are available, we shouldn't continue
         printf("No OpenCL platforms found\n");
@@ -137,16 +137,16 @@ cl_context cl_init(char devicePreference)
 
     // Traverse the platforms array printing information and
     // populating devices
-    for(unsigned int i = 0; i < numPlatforms ; i++) 
+    for(unsigned int i = 0; i < numPlatforms ; i++)
     {
         // Print out some basic info about the platform
         char* platformName = NULL;
         char* platformVendor = NULL;
-            
+
         platformName = cl_getPlatformName(platforms[i]);
         platformVendor = cl_getPlatformVendor(platforms[i]);
 
-        status = clGetDeviceIDs(platforms[i], deviceType, 0, NULL, &numDevices[i]);     
+        status = clGetDeviceIDs(platforms[i], deviceType, 0, NULL, &numDevices[i]);
         cl_errChk(status, "Getting device IDs", false);
         if(status != CL_SUCCESS) {
             printf("This is a known NVIDIA bug (if platform == AMD then die)\n");
@@ -162,19 +162,19 @@ cl_context cl_init(char devicePreference)
         free(platformVendor);
 
         // Populate OpenCL devices if any exist
-        if(numDevices[i] != 0) 
+        if(numDevices[i] != 0)
         {
-            // Allocate an array of devices of size "numDevices" 
+            // Allocate an array of devices of size "numDevices"
             devices[i] = (cl_device_id*)alloc(sizeof(cl_device_id)*numDevices[i]);
 
             // Populate Arrray with devices
-            status = clGetDeviceIDs(platforms[i], deviceType, numDevices[i],            
-                devices[i], NULL);      
-            cl_errChk(status, "Getting device IDs", true);                                      
+            status = clGetDeviceIDs(platforms[i], deviceType, numDevices[i],
+                devices[i], NULL);
+            cl_errChk(status, "Getting device IDs", true);
         }
 
         // Print some information about each device
-        for( unsigned int j = 0; j < numDevices[i]; j++) 
+        for( unsigned int j = 0; j < numDevices[i]; j++)
         {
             char* deviceName = NULL;
             char* deviceVendor = NULL;
@@ -183,27 +183,27 @@ cl_context cl_init(char devicePreference)
 
             deviceName = cl_getDeviceName(devices[i][j]);
             deviceVendor = cl_getDeviceVendor(devices[i][j]);
-                
+
             printf("\t\tName: %s\n", deviceName);
-            printf("\t\tVendor: %s\n", deviceVendor);   
+            printf("\t\tVendor: %s\n", deviceVendor);
 
             free(deviceName);
             free(deviceVendor);
-        }                       
-    }   
+        }
+    }
 
-    // Hard-code in the platform/device to use, or uncomment 'scanf' 
+    // Hard-code in the platform/device to use, or uncomment 'scanf'
     // to decide at runtime
     cl_uint chosen_platform, chosen_device;
     // UNCOMMENT the following two lines to manually select device each time
     //printf("Enter Platform and Device No (Seperated by Space) \n");
-    //scanf("%d %d", &chosen_platform, &chosen_device); 
-    chosen_platform = 0; 
+    //scanf("%d %d", &chosen_platform, &chosen_device);
+    chosen_platform = 0;
     chosen_device = 0;
     printf("Using Platform %d, Device %d \n", chosen_platform, chosen_device);
 
     // Do a sanity check of platform/device selection
-    if(chosen_platform >= numPlatforms || 
+    if(chosen_platform >= numPlatforms ||
         chosen_device >= numDevices[chosen_platform]) {
         printf("Invalid platform/device combination\n");
         exit(-1);
@@ -214,13 +214,13 @@ cl_context cl_init(char devicePreference)
     device = devices[chosen_platform][chosen_device];
 
     // Create the context
-    cl_context_properties cps[3] = {CL_CONTEXT_PLATFORM, 
+    cl_context_properties cps[3] = {CL_CONTEXT_PLATFORM,
         (cl_context_properties)(platform), 0};
-    context = clCreateContext(cps, 1, &device, NULL, NULL, &status);    
+    context = clCreateContext(cps, 1, &device, NULL, NULL, &status);
     cl_errChk(status, "Creating context", true);
- 
+
     // Create the command queue
-    commandQueueProf = clCreateCommandQueue(context, device, 
+    commandQueueProf = clCreateCommandQueue(context, device,
                             CL_QUEUE_PROFILING_ENABLE, &status);
     cl_errChk(status, "creating command queue", true);
 
@@ -241,140 +241,140 @@ cl_context cl_init(char devicePreference)
 cl_context cl_init_context(int platform_id, int device_id, int quiet, int use_gpu) {
   int printInfo=1;
   if (platform_id >= 0 && device_id >= 0) printInfo = 0;
-	cl_int status;
-	// Used to iterate through the platforms and devices, respectively
-	cl_uint numPlatforms;
-	cl_uint numDevices;
-
-	// These will hold the platform and device we select (can potentially be
-	// multiple, but we're just doing one for now)
-	// cl_platform_id platform = NULL;
-
-	status = clGetPlatformIDs(0, NULL, &numPlatforms);
-	if (printInfo) printf("Number of platforms detected:%d\n", numPlatforms);
-
-	// Print some information about the available platforms
-	cl_platform_id *platforms = NULL;
-	cl_device_id * devices = NULL;
-	if (numPlatforms > 0)
-	{
-		// get all the platforms
-		platforms = (cl_platform_id*)malloc(numPlatforms *
-			sizeof(cl_platform_id));
-		status = clGetPlatformIDs(numPlatforms, platforms, NULL);
-
-		// Traverse the platforms array
-		if (printInfo) printf("Checking For OpenCl Compatible Devices\n");
-		for(unsigned int i = 0; i < numPlatforms ; i++)
-		{
-			char pbuf[100];
-			if (printInfo) printf("Platform %d:\t", i);
-			status = clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR,
-				sizeof(pbuf), pbuf, NULL);
-			if (printInfo) printf("Vendor: %s\n", pbuf);
-
-			//unsigned int numDevices;
-
-			status = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &numDevices);
-			if(cl_errChk(status, "checking for devices",true))
-				exit(1);
-			if(numDevices == 0) {
-				printf("There are no devices for Platform %d\n",i);
-				exit(0);
-			}
-			else
-			{
-				if (printInfo) printf("\tNo of devices for Platform %d is %u\n",i, numDevices);
-				//! Allocate an array of devices of size "numDevices"
-				devices = (cl_device_id*)malloc(sizeof(cl_device_id)*numDevices);
-				//! Populate Arrray with devices
-				status = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, numDevices,
-					devices, NULL);
-				if(cl_errChk(status, "getting device IDs",true)) {
-					exit(1);
-				}
-			}
-			for( unsigned int j = 0; j < numDevices; j++)
-			{
-				char dbuf[100];
-				char deviceStr[100];
-				if (printInfo) printf("\tDevice: %d\t", j);
-				status = clGetDeviceInfo(devices[j], CL_DEVICE_VENDOR, sizeof(dbuf),
-					deviceStr, NULL);
-				cl_errChk(status, "Getting Device Info\n",true);
-			    if (printInfo) printf("Vendor: %s", deviceStr);
-				status = clGetDeviceInfo(devices[j], CL_DEVICE_NAME, sizeof(dbuf),
-					dbuf, NULL);
-				if (printInfo) printf("\n\t\tName: %s\n", dbuf);
-			}
-		}
-	}
-	else
-	{
-		// If no platforms are available, we're sunk!
-		printf("No OpenCL platforms found\n");
-		exit(0);
-	}
-
-	int platform_touse;
-	unsigned int device_touse;
-	if (printInfo) printf("Enter Platform and Device No (Seperated by Space) \n");
-	if (printInfo) scanf("%d %d", &platform_touse, &device_touse);
-	else {
-	  platform_touse = platform_id;
-	  device_touse = device_id;
-	}
-	if (!quiet) printf("Using Platform %d \t Device No %d \n",platform_touse, device_touse);
-
-	//! Recheck how many devices does our chosen platform have
-	status = clGetDeviceIDs(platforms[platform_touse], CL_DEVICE_TYPE_ALL, 0, NULL, &numDevices);
-
-	if(device_touse > numDevices)
-	{
-		printf("Invalid Device Number\n");
-		exit(1);
-	}
-	
-	//! Populate devices array with all the visible devices of our chosen platform
-	devices = (cl_device_id *)malloc(sizeof(cl_device_id)*numDevices);
-	status = clGetDeviceIDs(platforms[platform_touse],
-					CL_DEVICE_TYPE_ALL, numDevices,
-					devices, NULL);
-	if(cl_errChk(status,"Error in Getting Devices\n",true)) exit(1);
+  cl_int status;
+  // Used to iterate through the platforms and devices, respectively
+  cl_uint numPlatforms;
+  cl_uint numDevices;
+
+  // These will hold the platform and device we select (can potentially be
+  // multiple, but we're just doing one for now)
+  // cl_platform_id platform = NULL;
+
+  status = clGetPlatformIDs(0, NULL, &numPlatforms);
+  if (printInfo) printf("Number of platforms detected:%d\n", numPlatforms);
+
+  // Print some information about the available platforms
+  cl_platform_id *platforms = NULL;
+  cl_device_id * devices = NULL;
+  if (numPlatforms > 0)
+  {
+  // get all the platforms
+  platforms = (cl_platform_id*)malloc(numPlatforms *
+  sizeof(cl_platform_id));
+  status = clGetPlatformIDs(numPlatforms, platforms, NULL);
+
+  // Traverse the platforms array
+  if (printInfo) printf("Checking For OpenCl Compatible Devices\n");
+  for(unsigned int i = 0; i < numPlatforms ; i++)
+  {
+  char pbuf[100];
+  if (printInfo) printf("Platform %d:\t", i);
+  status = clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR,
+  sizeof(pbuf), pbuf, NULL);
+  if (printInfo) printf("Vendor: %s\n", pbuf);
+
+  //unsigned int numDevices;
+
+  status = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &numDevices);
+  if(cl_errChk(status, "checking for devices",true))
+  exit(1);
+  if(numDevices == 0) {
+  printf("There are no devices for Platform %d\n",i);
+  exit(0);
+  }
+  else
+  {
+  if (printInfo) printf("\tNo of devices for Platform %d is %u\n",i, numDevices);
+  //! Allocate an array of devices of size "numDevices"
+  devices = (cl_device_id*)malloc(sizeof(cl_device_id)*numDevices);
+  //! Populate Arrray with devices
+  status = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, numDevices,
+  devices, NULL);
+  if(cl_errChk(status, "getting device IDs",true)) {
+  exit(1);
+  }
+  }
+  for( unsigned int j = 0; j < numDevices; j++)
+  {
+  char dbuf[100];
+  char deviceStr[100];
+  if (printInfo) printf("\tDevice: %d\t", j);
+  status = clGetDeviceInfo(devices[j], CL_DEVICE_VENDOR, sizeof(dbuf),
+  deviceStr, NULL);
+  cl_errChk(status, "Getting Device Info\n",true);
+      if (printInfo) printf("Vendor: %s", deviceStr);
+  status = clGetDeviceInfo(devices[j], CL_DEVICE_NAME, sizeof(dbuf),
+  dbuf, NULL);
+  if (printInfo) printf("\n\t\tName: %s\n", dbuf);
+  }
+  }
+  }
+  else
+  {
+  // If no platforms are available, we're sunk!
+  printf("No OpenCL platforms found\n");
+  exit(0);
+  }
+
+  int platform_touse;
+  unsigned int device_touse;
+  if (printInfo) printf("Enter Platform and Device No (Seperated by Space) \n");
+  if (printInfo) scanf("%d %d", &platform_touse, &device_touse);
+  else {
+    platform_touse = platform_id;
+    device_touse = device_id;
+  }
+  if (!quiet) printf("Using Platform %d \t Device No %d \n",platform_touse, device_touse);
+
+  //! Recheck how many devices does our chosen platform have
+  status = clGetDeviceIDs(platforms[platform_touse], CL_DEVICE_TYPE_ALL, 0, NULL, &numDevices);
+
+  if(device_touse > numDevices)
+  {
+  printf("Invalid Device Number\n");
+  exit(1);
+  }
+
+  //! Populate devices array with all the visible devices of our chosen platform
+  devices = (cl_device_id *)malloc(sizeof(cl_device_id)*numDevices);
+  status = clGetDeviceIDs(platforms[platform_touse],
+  CL_DEVICE_TYPE_ALL, numDevices,
+  devices, NULL);
+  if(cl_errChk(status,"Error in Getting Devices\n",true)) exit(1);
 
   // Selector for CPU/GPU on the basis of the use_gpu parameter
-	cl_device_type device_type= use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
+  cl_device_type device_type= use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
 
-	cl_context_properties cps[3] = {CL_CONTEXT_PLATFORM,
-		(cl_context_properties)(platforms[platform_touse]), 0};
+  cl_context_properties cps[3] = {CL_CONTEXT_PLATFORM,
+  (cl_context_properties)(platforms[platform_touse]), 0};
 
-	cl_context_properties *cprops = cps;
+  cl_context_properties *cprops = cps;
 
-	context = clCreateContextFromType(
-					cprops, device_type,
-					NULL, NULL, &status);
-	if(cl_errChk(status, "creating Context",true)) {
-		exit(1);
-	}
+  context = clCreateContextFromType(
+  cprops, device_type,
+  NULL, NULL, &status);
+  if(cl_errChk(status, "creating Context",true)) {
+  exit(1);
+  }
 
 #define PROFILING
 
 #ifdef PROFILING
 
-	commandQueue = clCreateCommandQueue(context,
-						devices[device_touse], CL_QUEUE_PROFILING_ENABLE, &status);
+  commandQueue = clCreateCommandQueue(context,
+  devices[device_touse], CL_QUEUE_PROFILING_ENABLE, &status);
 
 #else
 
-	clCommandQueue = clCreateCommandQueue(clGPUContext,
-						devices[device_touse], NULL, &status);
+  clCommandQueue = clCreateCommandQueue(clGPUContext,
+  devices[device_touse], NULL, &status);
 
 #endif // PROFILING
 
-	if(cl_errChk(status, "creating command queue",true)) {
-		exit(1);
-	}
-	return context;
+  if(cl_errChk(status, "creating command queue",true)) {
+  exit(1);
+  }
+  return context;
 }
 /*!
     Release all resources that the user doesn't have access to.
@@ -385,7 +385,7 @@ void  cl_cleanup()
     if(commandQueue) {
         clReleaseCommandQueue(commandQueue);
     }
-    
+
     // Free the context
     if(context) {
         clReleaseContext(context);
@@ -403,7 +403,7 @@ void  cl_cleanup()
     \param mem The kernel object to release
 */
 void cl_freeKernel(cl_kernel kernel)
-{  
+{
     cl_int status;
 
     if(kernel != NULL) {
@@ -417,7 +417,7 @@ void cl_freeKernel(cl_kernel kernel)
     \param mem The device pointer to release
 */
 void cl_freeMem(cl_mem mem)
-{  
+{
     cl_int status;
 
     if(mem != NULL) {
@@ -431,7 +431,7 @@ void cl_freeMem(cl_mem mem)
     \param mem The program object to release
 */
 void cl_freeProgram(cl_program program)
-{  
+{
     cl_int status;
 
     if(program != NULL) {
@@ -442,12 +442,12 @@ void cl_freeProgram(cl_program program)
 
 //! Returns a reference to the command queue
 /*!
-	Returns a reference to the command queue \n
-	Used for any OpenCl call that needs the command queue declared in clutils.cpp
+  Returns a reference to the command queue \n
+  Used for any OpenCl call that needs the command queue declared in clutils.cpp
 */
 cl_command_queue cl_getCommandQueue()
 {
-	return commandQueue;
+  return commandQueue;
 }
 
 //-------------------------------------------------------
@@ -457,7 +457,7 @@ cl_command_queue cl_getCommandQueue()
 /*!
     Wait till all pending commands in queue are finished
 */
-void cl_sync() 
+void cl_sync()
 {
     clFinish(commandQueue);
 }
@@ -476,7 +476,7 @@ void cl_sync()
 cl_mem cl_allocBuffer(size_t mem_size, cl_mem_flags flags)
 {
     cl_mem mem;
-    cl_int status;          
+    cl_int status;
 
     /*!
         Logging information for keeping track of device memory
@@ -486,7 +486,7 @@ cl_mem cl_allocBuffer(size_t mem_size, cl_mem_flags flags)
 
     allocationCount++;
     allocationSize += mem_size;
-    
+
     mem = clCreateBuffer(context, flags, mem_size, NULL, &status);
 
     cl_errChk(status, "creating buffer", true);
@@ -503,16 +503,16 @@ cl_mem cl_allocBuffer(size_t mem_size, cl_mem_flags flags)
 cl_mem cl_allocBufferConst(size_t mem_size, void* host_ptr)
 {
     cl_mem mem;
-    cl_int status;          
+    cl_int status;
 
     mem = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
                          mem_size, host_ptr, &status);
     cl_errChk(status, "Error creating const mem buffer", true);
-    
+
     return mem;
 }
 
-//! Allocate a buffer on device pinning the host memory at host_ptr 
+//! Allocate a buffer on device pinning the host memory at host_ptr
 /*!
     \param mem_size Size of memory in bytes
     \return Returns a cl_mem object that points to pinned memory on the host
@@ -525,7 +525,7 @@ cl_mem cl_allocBufferPinned(size_t mem_size)
     mem = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR,
                          mem_size, NULL, &status);
     cl_errChk(status, "Error allocating pinned memory", true);
-    
+
     return mem;
 }
 
@@ -540,7 +540,7 @@ cl_mem cl_allocBufferPinned(size_t mem_size)
 cl_mem cl_allocImage(size_t height, size_t width, char type, cl_mem_flags flags)
 {
     cl_mem mem;
-    cl_int status;          
+    cl_int status;
 
     size_t elemSize = 0;
 
@@ -590,7 +590,7 @@ cl_mem cl_allocImage(size_t height, size_t width, char type, cl_mem_flags flags)
 //-------------------------------------------------------
 
 
-// Copy and map a buffer 
+// Copy and map a buffer
 void* cl_copyAndMapBuffer(cl_mem dst, cl_mem src, size_t size) {
 
     void* ptr;  // Pointer to the pinned memory that will be returned
@@ -603,8 +603,8 @@ void* cl_copyAndMapBuffer(cl_mem dst, cl_mem src, size_t size) {
 }
 
 // Copy a buffer
-void cl_copyBufferToBuffer(cl_mem dst, cl_mem src, size_t size) 
-{    
+void cl_copyBufferToBuffer(cl_mem dst, cl_mem src, size_t size)
+{
     cl_int status;
     status = clEnqueueCopyBuffer(commandQueue, src, dst, 0, 0, size, 0, NULL,
         NULL);
@@ -621,9 +621,9 @@ void cl_copyBufferToBuffer(cl_mem dst, cl_mem src, size_t size)
 */
 void cl_copyBufferToDevice(cl_mem dst, void* src, size_t mem_size, cl_bool blocking)
 {
-    cl_int status;     
-    status = clEnqueueWriteBuffer(commandQueue, dst, blocking, 0, 
-        mem_size, src, 0, NULL, NULL); 
+    cl_int status;
+    status = clEnqueueWriteBuffer(commandQueue, dst, blocking, 0,
+        mem_size, src, 0, NULL, NULL);
     cl_errChk(status, "Writing buffer", true);
 
 }
@@ -635,10 +635,10 @@ void cl_copyBufferToDevice(cl_mem dst, void* src, size_t mem_size, cl_bool block
     \param mem_size Size of data to copy
         \param blocking Blocking or non-blocking operation
 */
-void cl_copyBufferToHost(void* dst, cl_mem src, size_t mem_size, cl_bool blocking) 
+void cl_copyBufferToHost(void* dst, cl_mem src, size_t mem_size, cl_bool blocking)
 {
-    cl_int status;          
-    status = clEnqueueReadBuffer(commandQueue, src, blocking, 0, 
+    cl_int status;
+    status = clEnqueueReadBuffer(commandQueue, src, blocking, 0,
         mem_size, dst, 0, NULL, NULL);
     cl_errChk(status, "Reading buffer", true);
 
@@ -650,13 +650,13 @@ void cl_copyBufferToHost(void* dst, cl_mem src, size_t mem_size, cl_bool blockin
     \param dst Empty device image
     \param mem_size Size of data to copy
 */
-void cl_copyBufferToImage(cl_mem buffer, cl_mem image, int height, int width) 
+void cl_copyBufferToImage(cl_mem buffer, cl_mem image, int height, int width)
 {
     size_t origin[3] = {0, 0, 0};
     size_t region[3] = {width, height, 1};
 
-    cl_int status;          
-    status = clEnqueueCopyBufferToImage(commandQueue, buffer, image, 0, 
+    cl_int status;
+    status = clEnqueueCopyBufferToImage(commandQueue, buffer, image, 0,
         origin, region, 0, NULL, NULL);
     cl_errChk(status, "Copying buffer to image", true);
 
@@ -669,13 +669,13 @@ void cl_copyBufferToImage(cl_mem buffer, cl_mem image, int height, int width)
     \param height Height of the image
     \param width Width of the image
 */
-void cl_copyImageToDevice(cl_mem dst, void* src, size_t height, size_t width) 
+void cl_copyImageToDevice(cl_mem dst, void* src, size_t height, size_t width)
 {
-    cl_int status;          
+    cl_int status;
     size_t origin[3] = {0, 0, 0};
     size_t region[3] = {width, height, 1};
 
-    status = clEnqueueWriteImage(commandQueue, dst, CL_TRUE, origin, 
+    status = clEnqueueWriteImage(commandQueue, dst, CL_TRUE, origin,
         region, 0, 0, src, 0, NULL, NULL);
     cl_errChk(status, "Writing image", true);
 }
@@ -687,13 +687,13 @@ void cl_copyImageToDevice(cl_mem dst, void* src, size_t height, size_t width)
     \param height Height of the image
     \param width Width of the image
 */
-void cl_copyImageToHost(void* dst, cl_mem src, size_t height, size_t width) 
+void cl_copyImageToHost(void* dst, cl_mem src, size_t height, size_t width)
 {
-    cl_int status;          
+    cl_int status;
     size_t origin[3] = {0, 0, 0};
     size_t region[3] = {width, height, 1};
 
-    status = clEnqueueReadImage(commandQueue, src, CL_TRUE, origin, 
+    status = clEnqueueReadImage(commandQueue, src, CL_TRUE, origin,
         region, 0, 0, dst, 0, NULL, NULL);
     cl_errChk(status, "Reading image", true);
 }
@@ -707,10 +707,10 @@ void cl_copyImageToHost(void* dst, cl_mem src, size_t height, size_t width)
 */
 void *cl_mapBuffer(cl_mem mem, size_t mem_size, cl_mem_flags flags)
 {
-    cl_int status;          
+    cl_int status;
     void *ptr;
 
-    ptr = (void *)clEnqueueMapBuffer(commandQueue, mem, CL_TRUE, flags, 
+    ptr = (void *)clEnqueueMapBuffer(commandQueue, mem, CL_TRUE, flags,
                                              0, mem_size, 0, NULL, NULL, &status);
 
     cl_errChk(status, "Error mapping a buffer", true);
@@ -729,8 +729,8 @@ void cl_unmapBuffer(cl_mem mem, void *ptr)
     // TODO It looks like AMD doesn't support profiling unmapping yet. Leaving the
     //      commented code here until it's supported
 
-    cl_int status;          
-    
+    cl_int status;
+
     status = clEnqueueUnmapMemObject(commandQueue, mem, ptr, 0, NULL, NULL);
 
     cl_errChk(status, "Error unmapping a buffer or image", true);
@@ -738,7 +738,7 @@ void cl_unmapBuffer(cl_mem mem, void *ptr)
 
 void cl_writeToZCBuffer(cl_mem mem, void* data, size_t size)
 {
-    
+
     void* ptr;
 
     ptr = cl_mapBuffer(mem, size, CL_MAP_WRITE);
@@ -762,7 +762,7 @@ Compile Opencl source file into a cl_program. The cl_program will be made into a
 */
 cl_program cl_compileProgram(char* kernelPath, char* compileoptions, bool verbosebuild )
 {
-    cl_int status;          
+    cl_int status;
     FILE *fp = NULL;
     char *source = NULL;
     long int size;
@@ -804,7 +804,7 @@ cl_program cl_compileProgram(char* kernelPath, char* compileoptions, bool verbos
     source[size] = '\0';
 
     // Create the program object
-    cl_program clProgramReturn = clCreateProgramWithSource(context, 1, 
+    cl_program clProgramReturn = clCreateProgramWithSource(context, 1,
         (const char **)&source, NULL, &status);
     cl_errChk(status, "Creating program", true);
 
@@ -813,31 +813,31 @@ cl_program cl_compileProgram(char* kernelPath, char* compileoptions, bool verbos
 
     // Try to compile the program
     status = clBuildProgram(clProgramReturn, 0, NULL, compileoptions, NULL, NULL);
-    if(cl_errChk(status, "Building program", false) || verbosebuild == 1) 
+    if(cl_errChk(status, "Building program", false) || verbosebuild == 1)
     {
 
         cl_build_status build_status;
 
-        clGetProgramBuildInfo(clProgramReturn, device, CL_PROGRAM_BUILD_STATUS, 
+        clGetProgramBuildInfo(clProgramReturn, device, CL_PROGRAM_BUILD_STATUS,
             sizeof(cl_build_status), &build_status, NULL);
 
         if(build_status == CL_SUCCESS && verbosebuild == 0) {
-            return clProgramReturn;      
-        }       
+            return clProgramReturn;
+        }
 
         //char *build_log;
         size_t ret_val_size;
         printf("Device: %p",device);
-        clGetProgramBuildInfo(clProgramReturn, device, CL_PROGRAM_BUILD_LOG, 0, 
+        clGetProgramBuildInfo(clProgramReturn, device, CL_PROGRAM_BUILD_LOG, 0,
             NULL, &ret_val_size);
 
         char *build_log = (char*)alloc(ret_val_size+1);
 
-        clGetProgramBuildInfo(clProgramReturn, device, CL_PROGRAM_BUILD_LOG, 
+        clGetProgramBuildInfo(clProgramReturn, device, CL_PROGRAM_BUILD_LOG,
             ret_val_size+1, build_log, NULL);
 
         // to be careful, terminate with \0
-        // there's no information in the reference whether the string is 0 
+        // there's no information in the reference whether the string is 0
         // terminated or not
         build_log[ret_val_size] = '\0';
 
@@ -845,9 +845,9 @@ cl_program cl_compileProgram(char* kernelPath, char* compileoptions, bool verbos
         if(build_status != CL_SUCCESS) {
             getchar();
             exit(-1);
-        }       
+        }
         else
-            return clProgramReturn;       
+            return clProgramReturn;
     }
 
     // print the ptx information
@@ -865,7 +865,7 @@ Create a kernel from compiled source
 \return Returns a cl_kernel object for the specified kernel
 */
 cl_kernel cl_createKernel(cl_program program, const char* kernel_name) {
-    
+
     cl_kernel kernel;
     cl_int status;
 
@@ -884,7 +884,7 @@ Set an argument for a OpenCL kernel
 \param size The size of the argument
 \param data A pointer to the argument
 */
-void cl_setKernelArg(cl_kernel kernel, unsigned int index, size_t size, 
+void cl_setKernelArg(cl_kernel kernel, unsigned int index, size_t size,
                      void* data)
 {
     cl_int status;
@@ -923,11 +923,11 @@ double cl_computeExecTime(cl_event event_time)
     // Convert to ms
     elapsed = (double)(endtime-starttime)/1000000.0;
 
-    return elapsed;     
+    return elapsed;
 }
 
 //! Compute the elapsed time between two timer values
-double cl_computeTime(cl_time start, cl_time end) 
+double cl_computeTime(cl_time start, cl_time end)
 {
 #ifdef _WIN32
     __int64 freq;
@@ -942,13 +942,13 @@ double cl_computeTime(cl_time start, cl_time end)
     // Return time in ms
     return double(end-start)/(double(freq)/1000.0);
 #else
-    
+
     return end-start;
 #endif
 }
 
 //! Grab the current time using a system-specific timer
-void cl_getTime(cl_time* time) 
+void cl_getTime(cl_time* time)
 {
 
 #ifdef _WIN32
@@ -961,7 +961,7 @@ void cl_getTime(cl_time* time)
     // Use gettimeofday to get the current time
     struct timeval curTime;
     gettimeofday(&curTime, NULL);
-    
+
     // Convert timeval into double
     *time = curTime.tv_sec * 1000 + (double)curTime.tv_usec/1000;
 #endif
@@ -980,18 +980,18 @@ void cl_getTime(cl_time* time)
     The error code is the index within this array
 */
 char *cl_errs[MAX_ERR_VAL] = {
-    (char *)"CL_SUCCESS",                         // 0                            
-    (char *)"CL_DEVICE_NOT_FOUND",                //-1                         
-    (char *)"CL_DEVICE_NOT_AVAILABLE",            //-2                    
-    (char *)"CL_COMPILER_NOT_AVAILABLE",          //-3                 
-    (char *)"CL_MEM_OBJECT_ALLOCATION_FAILURE",   //-4            
-    (char *)"CL_OUT_OF_RESOURCES",                //-5                         
-    (char *)"CL_OUT_OF_HOST_MEMORY",              //-6                      
-    (char *)"CL_PROFILING_INFO_NOT_AVAILABLE",    //-7            
-    (char *)"CL_MEM_COPY_OVERLAP",                //-8                        
-    (char *)"CL_IMAGE_FORMAT_MISMATCH",           //-9                   
+    (char *)"CL_SUCCESS",                         // 0
+    (char *)"CL_DEVICE_NOT_FOUND",                //-1
+    (char *)"CL_DEVICE_NOT_AVAILABLE",            //-2
+    (char *)"CL_COMPILER_NOT_AVAILABLE",          //-3
+    (char *)"CL_MEM_OBJECT_ALLOCATION_FAILURE",   //-4
+    (char *)"CL_OUT_OF_RESOURCES",                //-5
+    (char *)"CL_OUT_OF_HOST_MEMORY",              //-6
+    (char *)"CL_PROFILING_INFO_NOT_AVAILABLE",    //-7
+    (char *)"CL_MEM_COPY_OVERLAP",                //-8
+    (char *)"CL_IMAGE_FORMAT_MISMATCH",           //-9
     (char *)"CL_IMAGE_FORMAT_NOT_SUPPORTED",      //-10
-    (char *)"CL_BUILD_PROGRAM_FAILURE",           //-11           
+    (char *)"CL_BUILD_PROGRAM_FAILURE",           //-11
     (char *)"CL_MAP_FAILURE",                     //-12
     (char *)"",                                   //-13
     (char *)"",                                   //-14
@@ -1049,7 +1049,7 @@ char *cl_errs[MAX_ERR_VAL] = {
 /*!
 Checks for error code as per cl_int returned by OpenCl
 \param status Error value as cl_int
-\param msg User provided error message 
+\param msg User provided error message
 \return True if Error Seen, False if no error
 */
 int cl_errChk(const cl_int status, const char * msg, bool exitOnErr)
@@ -1057,11 +1057,11 @@ int cl_errChk(const cl_int status, const char * msg, bool exitOnErr)
 
     if(status != CL_SUCCESS) {
         printf("OpenCL Error: %d %s %s\n", status, cl_errs[-status], msg);
-        
+
         if(exitOnErr) {
             exit(-1);
         }
-        
+
         return true;
     }
     return false;
@@ -1086,15 +1086,15 @@ int cl_errChk(const cl_int status, const char * msg, bool exitOnErr)
 
     printf("There are %d supported image formats\n", numFormats);
 
-    cl_uint orders[]={CL_R,  CL_A, CL_INTENSITY, CL_LUMINANCE, CL_RG,  
+    cl_uint orders[]={CL_R,  CL_A, CL_INTENSITY, CL_LUMINANCE, CL_RG,
         CL_RA, CL_RGB, CL_RGBA, CL_ARGB, CL_BGRA};
-    char  *orderstr[]={(char *)"CL_R", (char *)"CL_A",(char *)"CL_INTENSITY", (char *)"CL_LUMINANCE", (char *)"CL_RG", 
+    char  *orderstr[]={(char *)"CL_R", (char *)"CL_A",(char *)"CL_INTENSITY", (char *)"CL_LUMINANCE", (char *)"CL_RG",
         (char *)"CL_RA", (char *)"CL_RGB", (char *)"CL_RGBA", (char *)"CL_ARGB", (char *)"CL_BGRA"};
 
     cl_uint types[]={
-        CL_SNORM_INT8 , CL_SNORM_INT16, CL_UNORM_INT8, CL_UNORM_INT16, 
+        CL_SNORM_INT8 , CL_SNORM_INT16, CL_UNORM_INT8, CL_UNORM_INT16,
         CL_UNORM_SHORT_565, CL_UNORM_SHORT_555, CL_UNORM_INT_101010,CL_SIGNED_INT8,
-        CL_SIGNED_INT16,  CL_SIGNED_INT32, CL_UNSIGNED_INT8, CL_UNSIGNED_INT16, 
+        CL_SIGNED_INT16,  CL_SIGNED_INT32, CL_UNSIGNED_INT8, CL_UNSIGNED_INT16,
         CL_UNSIGNED_INT32, CL_HALF_FLOAT, CL_FLOAT};
 
     char * typesstr[]={
@@ -1189,21 +1189,21 @@ char* cl_getDeviceDriverVersion(cl_device_id dev)
     }
 
     // Print the vendor
-    status = clGetDeviceInfo(dev, CL_DRIVER_VERSION, 0,                 
-        NULL, &devInfoSize);            
+    status = clGetDeviceInfo(dev, CL_DRIVER_VERSION, 0,
+        NULL, &devInfoSize);
     cl_errChk(status, "Getting vendor name", true);
 
     devInfoStr = (char*)alloc(devInfoSize);
 
-    status = clGetDeviceInfo(dev, CL_DRIVER_VERSION, devInfoSize,                       
-        devInfoStr, NULL);              
+    status = clGetDeviceInfo(dev, CL_DRIVER_VERSION, devInfoSize,
+        devInfoStr, NULL);
     cl_errChk(status, "Getting vendor name", true);
 
     return devInfoStr;
 }
 
 //! The the name of the device as supplied by the OpenCL implementation
-char* cl_getDeviceName(cl_device_id dev) 
+char* cl_getDeviceName(cl_device_id dev)
 {
     cl_int status;
     size_t devInfoSize;
@@ -1215,14 +1215,14 @@ char* cl_getDeviceName(cl_device_id dev)
     }
 
     // Print the name
-    status = clGetDeviceInfo(dev, CL_DEVICE_NAME, 0,                    
-        NULL, &devInfoSize);    
+    status = clGetDeviceInfo(dev, CL_DEVICE_NAME, 0,
+        NULL, &devInfoSize);
     cl_errChk(status, "Getting device name", true);
-                
+
     devInfoStr = (char*)alloc(devInfoSize);
 
-    status = clGetDeviceInfo(dev, CL_DEVICE_NAME, devInfoSize,                  
-        devInfoStr, NULL);      
+    status = clGetDeviceInfo(dev, CL_DEVICE_NAME, devInfoSize,
+        devInfoStr, NULL);
     cl_errChk(status, "Getting device name", true);
 
     return(devInfoStr);
@@ -1241,14 +1241,14 @@ char* cl_getDeviceVendor(cl_device_id dev)
     }
 
     // Print the vendor
-    status = clGetDeviceInfo(dev, CL_DEVICE_VENDOR, 0,                  
-        NULL, &devInfoSize);            
+    status = clGetDeviceInfo(dev, CL_DEVICE_VENDOR, 0,
+        NULL, &devInfoSize);
     cl_errChk(status, "Getting vendor name", true);
 
     devInfoStr = (char*)alloc(devInfoSize);
 
-    status = clGetDeviceInfo(dev, CL_DEVICE_VENDOR, devInfoSize,                        
-        devInfoStr, NULL);              
+    status = clGetDeviceInfo(dev, CL_DEVICE_VENDOR, devInfoSize,
+        devInfoStr, NULL);
     cl_errChk(status, "Getting vendor name", true);
 
     return devInfoStr;
@@ -1267,58 +1267,57 @@ char* cl_getDeviceVersion(cl_device_id dev)
     }
 
     // Print the vendor
-    status = clGetDeviceInfo(dev, CL_DEVICE_VERSION, 0,                 
-        NULL, &devInfoSize);            
+    status = clGetDeviceInfo(dev, CL_DEVICE_VERSION, 0,
+        NULL, &devInfoSize);
     cl_errChk(status, "Getting vendor name", true);
 
     devInfoStr = (char*)alloc(devInfoSize);
 
-    status = clGetDeviceInfo(dev, CL_DEVICE_VERSION, devInfoSize,                       
-        devInfoStr, NULL);              
+    status = clGetDeviceInfo(dev, CL_DEVICE_VERSION, devInfoSize,
+        devInfoStr, NULL);
     cl_errChk(status, "Getting vendor name", true);
 
     return devInfoStr;
 }
 
 //! The the name of the device as supplied by the OpenCL implementation
-char* cl_getPlatformName(cl_platform_id platform) 
+char* cl_getPlatformName(cl_platform_id platform)
 {
     cl_int status;
     size_t platformInfoSize;
     char* platformInfoStr = NULL;
 
     // Print the name
-    status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, 0,                   
-        NULL, &platformInfoSize);       
+    status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, 0,
+        NULL, &platformInfoSize);
     cl_errChk(status, "Getting platform name", true);
-                
+
     platformInfoStr = (char*)alloc(platformInfoSize);
 
-    status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, platformInfoSize,                    
-        platformInfoStr, NULL); 
+    status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, platformInfoSize,
+        platformInfoStr, NULL);
     cl_errChk(status, "Getting platform name", true);
 
     return(platformInfoStr);
 }
 
 //! The the name of the device as supplied by the OpenCL implementation
-char* cl_getPlatformVendor(cl_platform_id platform) 
+char* cl_getPlatformVendor(cl_platform_id platform)
 {
     cl_int status;
     size_t platformInfoSize;
     char* platformInfoStr = NULL;
 
     // Print the name
-    status = clGetPlatformInfo(platform, CL_PLATFORM_VENDOR, 0,                         
-        NULL, &platformInfoSize);       
+    status = clGetPlatformInfo(platform, CL_PLATFORM_VENDOR, 0,
+        NULL, &platformInfoSize);
     cl_errChk(status, "Getting platform name", true);
-                
+
     platformInfoStr = (char*)alloc(platformInfoSize);
 
-    status = clGetPlatformInfo(platform, CL_PLATFORM_VENDOR, platformInfoSize,                  
-        platformInfoStr, NULL); 
+    status = clGetPlatformInfo(platform, CL_PLATFORM_VENDOR, platformInfoSize,
+        platformInfoStr, NULL);
     cl_errChk(status, "Getting platform name", true);
 
     return(platformInfoStr);
 }
-

+ 13 - 13
opencl/nn/clutils.h

@@ -58,7 +58,7 @@
 #ifdef _WIN32
 #include <tchar.h>
 #include <Windows.h>
-typedef __int64 cl_time; 
+typedef __int64 cl_time;
 #else
 #include <sys/time.h>
 typedef double cl_time;
@@ -103,7 +103,7 @@ void    cl_sync();
 //-------------------------------------------------------
 
 // Allocates a regular buffer on the device
-cl_mem  cl_allocBuffer(size_t mem_size, 
+cl_mem  cl_allocBuffer(size_t mem_size,
             cl_mem_flags flags = CL_MEM_READ_WRITE);
 
 // XXX I don't think this does exactly what we want it to do
@@ -114,7 +114,7 @@ cl_mem  cl_allocBufferConst(size_t mem_size, void* host_ptr);
 cl_mem  cl_allocBufferPinned(size_t mem_size);
 
 // Allocates an image on the device
-cl_mem  cl_allocImage(size_t height, size_t width, char type, 
+cl_mem  cl_allocImage(size_t height, size_t width, char type,
             cl_mem_flags flags = CL_MEM_READ_WRITE);
 
 
@@ -123,15 +123,15 @@ cl_mem  cl_allocImage(size_t height, size_t width, char type,
 // Data transfers
 //-------------------------------------------------------
 
-// Copies a buffer from the device to pinned memory on the host and 
+// Copies a buffer from the device to pinned memory on the host and
 // maps it so it can be read
-void*   cl_copyAndMapBuffer(cl_mem dst, cl_mem src, size_t size); 
+void*   cl_copyAndMapBuffer(cl_mem dst, cl_mem src, size_t size);
 
 // Copies from one buffer to another
 void    cl_copyBufferToBuffer(cl_mem dst, cl_mem src, size_t size);
 
 // Copies data to a buffer on the device
-void    cl_copyBufferToDevice(cl_mem dst, void *src, size_t mem_size, 
+void    cl_copyBufferToDevice(cl_mem dst, void *src, size_t mem_size,
             cl_bool blocking = CL_TRUE);
 
 // Copies data to an image on the device
@@ -141,7 +141,7 @@ void cl_copyImageToDevice(cl_mem dst, void* src, size_t height, size_t width);
 void    cl_copyImageToHost(void* dst, cl_mem src, size_t height, size_t width);
 
 // Copies data from a device buffer to the host
-void    cl_copyBufferToHost(void *dst, cl_mem src, size_t mem_size, 
+void    cl_copyBufferToHost(void *dst, cl_mem src, size_t mem_size,
             cl_bool blocking = CL_TRUE);
 
 // Copies data from a buffer on the device to an image on the device
@@ -161,7 +161,7 @@ void    cl_writeToZCBuffer(cl_mem mem, void* data, size_t size);
 //-------------------------------------------------------
 
 // Compiles a program
-cl_program  cl_compileProgram(char* kernelPath, char* compileoptions, 
+cl_program  cl_compileProgram(char* kernelPath, char* compileoptions,
                 bool verboseoptions = 0);
 
 // Creates a kernel
@@ -169,7 +169,7 @@ cl_kernel   cl_createKernel(cl_program program, const char* kernelName);
 
 
 // Sets a kernel argument
-void        cl_setKernelArg(cl_kernel kernel, unsigned int index, size_t size, 
+void        cl_setKernelArg(cl_kernel kernel, unsigned int index, size_t size,
                 void* data);
 
 
@@ -181,7 +181,7 @@ void        cl_setKernelArg(cl_kernel kernel, unsigned int index, size_t size,
 double  cl_computeExecTime(cl_event);
 
 // Compute the elapsed time between two CPU timer values
-double  cl_computeTime(cl_time start, cl_time end); 
+double  cl_computeTime(cl_time start, cl_time end);
 
 // Creates an event from CPU timers
 void    cl_createUserEvent(cl_time start, cl_time end, char* desc);
@@ -193,7 +193,7 @@ void    cl_disableEvents();
 void    cl_enableEvents();
 
 // Query the current system time
-void    cl_getTime(cl_time* time); 
+void    cl_getTime(cl_time* time);
 
 // Calls a function which prints events to the terminal
 void    cl_printEvents();
@@ -264,8 +264,8 @@ typedef struct{
 #define NUM_PROGRAMS 7
 
 #define NUM_KERNELS 13
-#define KERNEL_INIT_DET 0 
-#define KERNEL_BUILD_DET 1 
+#define KERNEL_INIT_DET 0
+#define KERNEL_BUILD_DET 1
 #define KERNEL_SURF_DESC 2
 #define KERNEL_NORM_DESC 3
 #define KERNEL_NON_MAX_SUP 4

+ 37 - 38
opencl/nn/nearestNeighbor.cpp

@@ -15,16 +15,16 @@ int main(int argc, char *argv[]) {
   char filename[100];
   int resultsCount=10,quiet=0,timing=0,platform=-1,device=-1,use_gpu=-1;
   float lat=0.0,lng=0.0;
-  
+
   // parse command line
   if (parseCommandline(argc, argv, filename,&resultsCount,&lat,&lng,
                      &quiet, &timing, &platform, &device, &use_gpu)) {
     printUsage();
     return 0;
   }
-  
+
   int numRecords = loadData(filename,records,locations);
-  
+
   //for(i=0;i<numRecords;i++)
   //    printf("%s, %f, %f\n",(records[i].recString),locations[i].lat,locations[i].lng);
 
@@ -37,7 +37,7 @@ int main(int argc, char *argv[]) {
   if (resultsCount > numRecords) resultsCount = numRecords;
 
   context = cl_init_context(platform,device,quiet,use_gpu);
-  
+
   recordDistances = OpenClFindNearestNeighbors(context,numRecords,locations,lat,lng,timing);
 
   // find the resultsCount least distances
@@ -53,10 +53,10 @@ int main(int argc, char *argv[]) {
 }
 
 float *OpenClFindNearestNeighbors(
-	cl_context context,
-	int numRecords,
-	std::vector<LatLong> &locations,float lat,float lng,
-	int timing) {
+  cl_context context,
+  int numRecords,
+  std::vector<LatLong> &locations,float lat,float lng,
+  int timing) {
 
     // 1. set up kernel
     cl_kernel NN_kernel;
@@ -64,7 +64,7 @@ float *OpenClFindNearestNeighbors(
         cl_program cl_NN_program;
         cl_NN_program = cl_compileProgram(
             (char *)"nearestNeighbor_kernel.cl",NULL);
-       
+
         NN_kernel = clCreateKernel(
             cl_NN_program, "NearestNeighbor", &status);
         status = cl_errChk(status, (char *)"Error Creating Nearest Neighbor kernel",true);
@@ -109,7 +109,7 @@ float *OpenClFindNearestNeighbors(
     size_t globalWorkSize[1];
     globalWorkSize[0] = numRecords;
     if (numRecords % 64) globalWorkSize[0] += 64 - (numRecords % 64);
-    //printf("Global Work Size: %zu\n",globalWorkSize[0]);      
+    //printf("Global Work Size: %zu\n",globalWorkSize[0]);
 
     error = clEnqueueNDRangeKernel(
         command_queue,  NN_kernel, 1, 0,
@@ -119,7 +119,7 @@ float *OpenClFindNearestNeighbors(
     cl_errChk(error,"ERROR in Executing Kernel NearestNeighbor",true);
 
     // 5. transfer data off of device
-    
+
     // create distances std::vector
     float *distances = (float *)malloc(sizeof(float) * numRecords);
 
@@ -142,7 +142,7 @@ float *OpenClFindNearestNeighbors(
         // Write Buffer
         error = clGetEventProfilingInfo(writeEvent,CL_PROFILING_COMMAND_START,
                                         sizeof(cl_ulong),&eventStart,NULL);
-        cl_errChk(error,"ERROR in Event Profiling (Write Start)",true); 
+        cl_errChk(error,"ERROR in Event Profiling (Write Start)",true);
         error = clGetEventProfilingInfo(writeEvent,CL_PROFILING_COMMAND_END,
                                         sizeof(cl_ulong),&eventEnd,NULL);
         cl_errChk(error,"ERROR in Event Profiling (Write End)",true);
@@ -152,7 +152,7 @@ float *OpenClFindNearestNeighbors(
         // Kernel
         error = clGetEventProfilingInfo(kernelEvent,CL_PROFILING_COMMAND_START,
                                         sizeof(cl_ulong),&eventStart,NULL);
-        cl_errChk(error,"ERROR in Event Profiling (Kernel Start)",true); 
+        cl_errChk(error,"ERROR in Event Profiling (Kernel Start)",true);
         error = clGetEventProfilingInfo(kernelEvent,CL_PROFILING_COMMAND_END,
                                         sizeof(cl_ulong),&eventEnd,NULL);
         cl_errChk(error,"ERROR in Event Profiling (Kernel End)",true);
@@ -162,38 +162,38 @@ float *OpenClFindNearestNeighbors(
         // Read Buffer
         error = clGetEventProfilingInfo(readEvent,CL_PROFILING_COMMAND_START,
                                         sizeof(cl_ulong),&eventStart,NULL);
-        cl_errChk(error,"ERROR in Event Profiling (Read Start)",true); 
+        cl_errChk(error,"ERROR in Event Profiling (Read Start)",true);
         error = clGetEventProfilingInfo(readEvent,CL_PROFILING_COMMAND_END,
                                         sizeof(cl_ulong),&eventEnd,NULL);
         cl_errChk(error,"ERROR in Event Profiling (Read End)",true);
 
         printf("%f [%.2fMB]\t",(float)((eventEnd-eventStart)/1e9),(float)((sizeof(float) * numRecords)/1e6));
         totalTime += eventEnd-eventStart;
-        
+
         printf("%f\n\n",(float)(totalTime/1e9));
     }
     // 6. return finalized data and release buffers
     clReleaseMemObject(d_locations);
     clReleaseMemObject(d_distances);
-	return distances;
+  return distances;
 }
 
 int loadData(char *filename,std::vector<Record> &records,std::vector<LatLong> &locations){
     FILE   *flist,*fp;
-	int    i=0;
-	char dbname[64];
-	int recNum=0;
-	
+  int    i=0;
+  char dbname[64];
+  int recNum=0;
+
     /**Main processing **/
-    
+
     flist = fopen(filename, "r");
-	while(!feof(flist)) {
-		/**
-		* Read in REC_WINDOW records of length REC_LENGTH
-		* If this is the last file in the filelist, then done
-		* else open next file to be read next iteration
-		*/
-		if(fscanf(flist, "%s\n", dbname) != 1) {
+  while(!feof(flist)) {
+  /**
+  * Read in REC_WINDOW records of length REC_LENGTH
+  * If this is the last file in the filelist, then done
+  * else open next file to be read next iteration
+  */
+  if(fscanf(flist, "%s\n", dbname) != 1) {
             fprintf(stderr, "error reading filelist\n");
             exit(0);
         }
@@ -209,18 +209,18 @@ int loadData(char *filename,std::vector<Record> &records,std::vector<LatLong> &l
             fgets(record.recString,49,fp);
             fgetc(fp); // newline
             if (feof(fp)) break;
-            
+
             // parse for lat and long
             char substr[6];
-            
+
             for(i=0;i<5;i++) substr[i] = *(record.recString+i+28);
             substr[5] = '\0';
             latLong.lat = atof(substr);
-            
+
             for(i=0;i<5;i++) substr[i] = *(record.recString+i+33);
             substr[5] = '\0';
             latLong.lng = atof(substr);
-            
+
             locations.push_back(latLong);
             records.push_back(record);
             recNum++;
@@ -237,7 +237,7 @@ void findLowest(std::vector<Record> &records,float *distances,int numRecords,int
   int minLoc;
   Record *tempRec;
   float tempDist;
-  
+
   for(i=0;i<topN;i++) {
     minLoc = i;
     for(j=i;j<numRecords;j++) {
@@ -248,11 +248,11 @@ void findLowest(std::vector<Record> &records,float *distances,int numRecords,int
     tempRec = &records[i];
     records[i] = records[minLoc];
     records[minLoc] = *tempRec;
-    
+
     tempDist = distances[i];
     distances[i] = distances[minLoc];
     distances[minLoc] = tempDist;
-    
+
     // add distance to the min we just found
     records[i].distance = distances[i];
   }
@@ -264,7 +264,7 @@ int parseCommandline(int argc, char *argv[], char* filename,int *r,float *lat,fl
     if (argc < 2) return 1; // error
     strncpy(filename,argv[1],100);
     char flag;
-    
+
     for(i=1;i<argc;i++) {
       if (argv[i][0]=='-') {// flag
         flag = argv[i][1];
@@ -302,7 +302,7 @@ int parseCommandline(int argc, char *argv[], char* filename,int *r,float *lat,fl
             case 'g': // device
               i++;
               *g = atoi(argv[i]);
-              break;              
+              break;
         }
       }
     }
@@ -339,4 +339,3 @@ void printUsage(){
 }
 
 #endif
-

+ 6 - 6
opencl/nn/nearestNeighbor.h

@@ -13,7 +13,7 @@
     #include <OpenCL/opencl.h>
 #else
     #include <CL/opencl.h>
-#endif 
+#endif
 
 #include "clutils.h"
 //#include "utils.h"
@@ -21,7 +21,7 @@
 #include <algorithm>
 
 
-	
+
 #define REC_LENGTH 49 // size of a record in db
 
 typedef struct latLong
@@ -37,10 +37,10 @@ typedef struct record
 } Record;
 
 float *OpenClFindNearestNeighbors(
-	cl_context context,
-	int numRecords,
-	std::vector<LatLong> &locations,float lat,float lng,
-	int timing);
+  cl_context context,
+  int numRecords,
+  std::vector<LatLong> &locations,float lat,float lng,
+  int timing);
 
 int loadData(char *filename,std::vector<Record> &records,std::vector<LatLong> &locations);
 void findLowest(std::vector<Record> &records,float *distances,int numRecords,int topN);

+ 305 - 306
opencl/nw/nw.c

@@ -1,11 +1,11 @@
 #ifdef RD_WG_SIZE_0_0
-	#define BLOCK_SIZE RD_WG_SIZE_0_0
+  #define BLOCK_SIZE RD_WG_SIZE_0_0
 #elif defined(RD_WG_SIZE_0)
-	#define BLOCK_SIZE RD_WG_SIZE_0
+  #define BLOCK_SIZE RD_WG_SIZE_0
 #elif defined(RD_WG_SIZE)
-	#define BLOCK_SIZE RD_WG_SIZE
+  #define BLOCK_SIZE RD_WG_SIZE
 #else
-	#define BLOCK_SIZE 16
+  #define BLOCK_SIZE 16
 #endif
 
 #define LIMIT -999
@@ -19,9 +19,9 @@
 #include <unistd.h>
 
 #ifdef NV //NVIDIA
-	#include <oclUtils.h>
-#else 
-	#include <CL/cl.h>
+  #include <oclUtils.h>
+#else
+  #include <CL/cl.h>
 #endif
 
 //global variables
@@ -62,79 +62,79 @@ static cl_int           num_devices;
 
 static int initialize(int platform_id, int device_id, int use_gpu)
 {
-	cl_int result;
-	size_t size;
-
-	// modification to handle the case in which we have more than one OpenCL platform available on the system.
-	cl_uint platformCount;
-	
-	// create OpenCL context
-	clGetPlatformIDs(0, NULL, &platformCount);
-	
-	cl_platform_id *platforms_ids;
-	platforms_ids = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount);
-	if (clGetPlatformIDs(platformCount, platforms_ids, NULL) != CL_SUCCESS) { printf("ERROR: clGetPlatformIDs(1,*,0) failed\n"); return -1; }
-	cl_context_properties ctxprop[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platforms_ids[platform_id], 0};
-	device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
-	context = clCreateContextFromType( ctxprop, device_type, NULL, NULL, NULL );
-	if( !context ) { printf("ERROR: clCreateContextFromType(%s) failed\n", use_gpu ? "GPU" : "CPU"); return -1; }
-
-	// get the list of GPUs
-	result = clGetContextInfo( context, CL_CONTEXT_DEVICES, 0, NULL, &size );
-	num_devices = (int) (size / sizeof(cl_device_id));
-	printf("num_devices = %d\n", num_devices);
-	
-	if( result != CL_SUCCESS || num_devices < 1 ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
-	device_list = new cl_device_id[num_devices];
-	if( !device_list ) { printf("ERROR: new cl_device_id[] failed\n"); return -1; }
-	result = clGetContextInfo( context, CL_CONTEXT_DEVICES, size, device_list, NULL );
-	if( result != CL_SUCCESS ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
-
-	// create command queue for the first device
-	cmd_queue = clCreateCommandQueue( context, device_list[device_id], 0, NULL );
-	if( !cmd_queue ) { printf("ERROR: clCreateCommandQueue() failed\n"); return -1; }
-	return 0;
+  cl_int result;
+  size_t size;
+
+  // modification to handle the case in which we have more than one OpenCL platform available on the system.
+  cl_uint platformCount;
+
+  // create OpenCL context
+  clGetPlatformIDs(0, NULL, &platformCount);
+
+  cl_platform_id *platforms_ids;
+  platforms_ids = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount);
+  if (clGetPlatformIDs(platformCount, platforms_ids, NULL) != CL_SUCCESS) { printf("ERROR: clGetPlatformIDs(1,*,0) failed\n"); return -1; }
+  cl_context_properties ctxprop[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platforms_ids[platform_id], 0};
+  device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
+  context = clCreateContextFromType( ctxprop, device_type, NULL, NULL, NULL );
+  if( !context ) { printf("ERROR: clCreateContextFromType(%s) failed\n", use_gpu ? "GPU" : "CPU"); return -1; }
+
+  // get the list of GPUs
+  result = clGetContextInfo( context, CL_CONTEXT_DEVICES, 0, NULL, &size );
+  num_devices = (int) (size / sizeof(cl_device_id));
+  printf("num_devices = %d\n", num_devices);
+
+  if( result != CL_SUCCESS || num_devices < 1 ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
+  device_list = new cl_device_id[num_devices];
+  if( !device_list ) { printf("ERROR: new cl_device_id[] failed\n"); return -1; }
+  result = clGetContextInfo( context, CL_CONTEXT_DEVICES, size, device_list, NULL );
+  if( result != CL_SUCCESS ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
+
+  // create command queue for the first device
+  cmd_queue = clCreateCommandQueue( context, device_list[device_id], 0, NULL );
+  if( !cmd_queue ) { printf("ERROR: clCreateCommandQueue() failed\n"); return -1; }
+  return 0;
 }
 
 static int shutdown()
 {
-	// release resources
-	if( cmd_queue ) clReleaseCommandQueue( cmd_queue );
-	if( context ) clReleaseContext( context );
-	if( device_list ) delete device_list;
-
-	// reset all variables
-	cmd_queue = 0;
-	context = 0;
-	device_list = 0;
-	num_devices = 0;
-	device_type = 0;
-
-	return 0;
+  // release resources
+  if( cmd_queue ) clReleaseCommandQueue( cmd_queue );
+  if( context ) clReleaseContext( context );
+  if( device_list ) delete device_list;
+
+  // reset all variables
+  cmd_queue = 0;
+  context = 0;
+  device_list = 0;
+  num_devices = 0;
+  device_type = 0;
+
+  return 0;
 }
 
 int maximum( int a,
-		 int b,
-		 int c){
-
-	int k;
-	if( a <= b )
-	  k = b;
-	else 
-	  k = a;
-	if( k <=c )
-	  return(c);
-	else
-	  return(k);
+   int b,
+   int c){
+
+  int k;
+  if( a <= b )
+    k = b;
+  else
+    k = a;
+  if( k <=c )
+    return(c);
+  else
+    return(k);
 }
 
 void usage(int argc, char **argv)
 {
-	fprintf(stderr, "Usage: %s <max_rows/max_cols> <penalty> \n", argv[0]);
-	fprintf(stderr, "\t<dimension>  - x and y dimensions\n");
-	fprintf(stderr, "\t<penalty> - penalty(positive integer)\n");
-	fprintf(stderr, "\t<file> - filename\n");
-	exit(1);
+  fprintf(stderr, "Usage: %s <max_rows/max_cols> <penalty> \n", argv[0]);
+  fprintf(stderr, "\t<dimension>  - x and y dimensions\n");
+  fprintf(stderr, "\t<penalty> - penalty(positive integer)\n");
+  fprintf(stderr, "\t<file> - filename\n");
+  exit(1);
 }
 
 double gettime() {
@@ -148,310 +148,309 @@ int main(int argc, char **argv){
   printf("WG size of kernel = %d \n", BLOCK_SIZE);
 
   int max_rows, max_cols, penalty;
-	char * tempchar;	
-  
+  char * tempchar;
+
   // Rewritten parameters parsing for selecting platform and device and old
   // parameters
-  
+
   // Variables to store information on platform and device to use_gpu
   int platform_id = 0;
   int device_id = 0;
   int use_gpu = 0;
-  
+
   // The lengths of the two sequences should be able to divided by 16.
-	// And at current stage  max_rows needs to equal max_cols
+  // And at current stage  max_rows needs to equal max_cols
   int opt;
   extern char *optarg;
-	while ((opt = getopt(argc, argv, "r:c:x:t:p:d:g:")) != -1 ) {
-		switch(opt){
-			case 'r':
-			   max_rows = atoi(optarg);
+  while ((opt = getopt(argc, argv, "r:c:x:t:p:d:g:")) != -1 ) {
+  switch(opt){
+  case 'r':
+     max_rows = atoi(optarg);
          max_cols = atoi(optarg);
          if(max_rows%16!=0){
        	   fprintf(stderr,"The dimension values must be a multiple of 16\n");
        	   exit(1);
        	 }
-			   break;
-			case 'c':
+     break;
+  case 'c':
          max_rows = atoi(optarg);
-			   max_cols = atoi(optarg);
+     max_cols = atoi(optarg);
          if(max_cols%16!=0){
        	   fprintf(stderr,"The dimension values must be a multiple of 16\n");
        	   exit(1);
        	 }
-			   break;
+     break;
       case 'x':
-			   penalty = atoi(optarg);
+     penalty = atoi(optarg);
          break;
       case 't':
-			   tempchar = optarg;
-			   break;
+     tempchar = optarg;
+     break;
       case 'p':
-			   platform_id = atoi(optarg);
-			   break;
+     platform_id = atoi(optarg);
+     break;
       case 'd':
   		   device_id = atoi(optarg);
   		   break;
       case 'g':
          use_gpu = atoi(optarg);
-         break;            
+         break;
       case ':':
-			   fprintf(stderr, "missing argument\n");
-			   break;
+     fprintf(stderr, "missing argument\n");
+     break;
       default:
-			   fprintf(stderr, "Usage: %s -r/-c <max_rows/max_cols> -x <penalty> -p <platform> -d <device> -g <use_gpu>\n",
+     fprintf(stderr, "Usage: %s -r/-c <max_rows/max_cols> -x <penalty> -p <platform> -d <device> -g <use_gpu>\n",
                  argv[0]);
-			   exit(EXIT_FAILURE);
-		}
-	}
+     exit(EXIT_FAILURE);
+  }
+  }
+
+  max_rows = max_rows + 1;
+  max_cols = max_cols + 1;
 
-	max_rows = max_rows + 1;
-	max_cols = max_cols + 1;
+  int *reference;
+  int *input_itemsets;
+  int *output_itemsets;
 
-	int *reference;
-	int *input_itemsets;
-	int *output_itemsets;
-	
   reference = (int *)malloc( max_rows * max_cols * sizeof(int) );
   input_itemsets = (int *)malloc( max_rows * max_cols * sizeof(int) );
   output_itemsets = (int *)malloc( max_rows * max_cols * sizeof(int) );
-	
-	srand(7);
-	
-	//initialization 
-	for (int i = 0 ; i < max_cols; i++){
-		for (int j = 0 ; j < max_rows; j++){
-			input_itemsets[i*max_cols+j] = 0;
-		}
-	}
-
-	for( int i=1; i< max_rows ; i++){    //initialize the cols
-			input_itemsets[i*max_cols] = rand() % 10 + 1;
-	}
-	
+
+  srand(7);
+
+  //initialization
+  for (int i = 0 ; i < max_cols; i++){
+  for (int j = 0 ; j < max_rows; j++){
+  input_itemsets[i*max_cols+j] = 0;
+  }
+  }
+
+  for( int i=1; i< max_rows ; i++){    //initialize the cols
+  input_itemsets[i*max_cols] = rand() % 10 + 1;
+  }
+
     for( int j=1; j< max_cols ; j++){    //initialize the rows
-			input_itemsets[j] = rand() % 10 + 1;
-	}
-	
-	for (int i = 1 ; i < max_cols; i++){
-		for (int j = 1 ; j < max_rows; j++){
-		reference[i*max_cols+j] = blosum62[input_itemsets[i*max_cols]][input_itemsets[j]];
-		}
-	}
+  input_itemsets[j] = rand() % 10 + 1;
+  }
+
+  for (int i = 1 ; i < max_cols; i++){
+  for (int j = 1 ; j < max_rows; j++){
+  reference[i*max_cols+j] = blosum62[input_itemsets[i*max_cols]][input_itemsets[j]];
+  }
+  }
 
     for( int i = 1; i< max_rows ; i++)
        input_itemsets[i*max_cols] = -i * penalty;
-	for( int j = 1; j< max_cols ; j++)
+  for( int j = 1; j< max_cols ; j++)
        input_itemsets[j] = -j * penalty;
-	
-	int sourcesize = 1024*1024;
-		
-	char * source = (char *)calloc(sourcesize, sizeof(char)); 
-	if(!source) { printf("ERROR: calloc(%d) failed\n", sourcesize); return -1; }
-
-	// read the kernel core source
-	char * kernel_nw1  = "nw_kernel1";
-	char * kernel_nw2  = "nw_kernel2";
-	FILE * fp = fopen(tempchar, "rb"); 
-	if(!fp) { printf("ERROR: unable to open '%s'\n", tempchar); return -1; }
-	fread(source + strlen(source), sourcesize, 1, fp);
-	fclose(fp);
-
-	int nworkitems, workgroupsize = 0;
-	nworkitems = BLOCK_SIZE;
-
-	if(nworkitems < 1 || workgroupsize < 0){
-		printf("ERROR: invalid or missing <num_work_items>[/<work_group_size>]\n"); 
-		return -1;
-	}
-		// set global and local workitems
-	size_t local_work[3] = { (workgroupsize>0)?workgroupsize:1, 1, 1 };
-	size_t global_work[3] = { nworkitems, 1, 1 }; //nworkitems = no. of GPU threads
-	
-	// OpenCL initialization
-	if(initialize(platform_id, device_id, use_gpu)) return -1;
-
-	// compile kernel
-	cl_int err = 0;
-	const char * slist[2] = { source, 0 };
-	cl_program prog = clCreateProgramWithSource(context, 1, slist, NULL, &err);
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateProgramWithSource() => %d\n", err); return -1; }
-
-	char clOptions[110];
-	//  sprintf(clOptions,"-I../../src");                                                                                 
-	sprintf(clOptions," ");
+
+  int sourcesize = 1024*1024;
+
+  char * source = (char *)calloc(sourcesize, sizeof(char));
+  if(!source) { printf("ERROR: calloc(%d) failed\n", sourcesize); return -1; }
+
+  // read the kernel core source
+  char * kernel_nw1  = "nw_kernel1";
+  char * kernel_nw2  = "nw_kernel2";
+  FILE * fp = fopen(tempchar, "rb");
+  if(!fp) { printf("ERROR: unable to open '%s'\n", tempchar); return -1; }
+  fread(source + strlen(source), sourcesize, 1, fp);
+  fclose(fp);
+
+  int nworkitems, workgroupsize = 0;
+  nworkitems = BLOCK_SIZE;
+
+  if(nworkitems < 1 || workgroupsize < 0){
+  printf("ERROR: invalid or missing <num_work_items>[/<work_group_size>]\n");
+  return -1;
+  }
+  // set global and local workitems
+  size_t local_work[3] = { (workgroupsize>0)?workgroupsize:1, 1, 1 };
+  size_t global_work[3] = { nworkitems, 1, 1 }; //nworkitems = no. of GPU threads
+
+  // OpenCL initialization
+  if(initialize(platform_id, device_id, use_gpu)) return -1;
+
+  // compile kernel
+  cl_int err = 0;
+  const char * slist[2] = { source, 0 };
+  cl_program prog = clCreateProgramWithSource(context, 1, slist, NULL, &err);
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateProgramWithSource() => %d\n", err); return -1; }
+
+  char clOptions[110];
+  //  sprintf(clOptions,"-I../../src");
+  sprintf(clOptions," ");
 
 #ifdef BLOCK_SIZE
-	sprintf(clOptions + strlen(clOptions), " -DBLOCK_SIZE=%d", BLOCK_SIZE);
+  sprintf(clOptions + strlen(clOptions), " -DBLOCK_SIZE=%d", BLOCK_SIZE);
 #endif
 
-	err = clBuildProgram(prog, 0, NULL, clOptions, NULL, NULL);
-	/*{ // show warnings/errors
-		static char log[65536]; memset(log, 0, sizeof(log));
-		cl_device_id device_id = 0;
-		err = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(device_id), &device_id, NULL);
-		clGetProgramBuildInfo(prog, device_id, CL_PROGRAM_BUILD_LOG, sizeof(log)-1, log, NULL);
-		if(err || strstr(log,"warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);
-	}*/
-	if(err != CL_SUCCESS) { printf("ERROR: clBuildProgram() => %d\n", err); return -1; }
-    	
-	cl_kernel kernel1;
-	cl_kernel kernel2;
-	kernel1 = clCreateKernel(prog, kernel_nw1, &err);  
-	kernel2 = clCreateKernel(prog, kernel_nw2, &err);  
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateKernel() 0 => %d\n", err); return -1; }
-	clReleaseProgram(prog);
-	
-		
-	
-	// creat buffers
-	cl_mem input_itemsets_d;
-	cl_mem output_itemsets_d;
-	cl_mem reference_d;
-	
-	input_itemsets_d = clCreateBuffer(context, CL_MEM_READ_WRITE, max_cols * max_rows * sizeof(int), NULL, &err );
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer input_item_set (size:%d) => %d\n", max_cols * max_rows, err); return -1;}
-	reference_d		 = clCreateBuffer(context, CL_MEM_READ_WRITE, max_cols * max_rows * sizeof(int), NULL, &err );
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer reference (size:%d) => %d\n", max_cols * max_rows, err); return -1;}
-	output_itemsets_d = clCreateBuffer(context, CL_MEM_READ_WRITE, max_cols * max_rows * sizeof(int), NULL, &err );
-	if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer output_item_set (size:%d) => %d\n", max_cols * max_rows, err); return -1;}
-	
-	//write buffers
-	err = clEnqueueWriteBuffer(cmd_queue, input_itemsets_d, 1, 0, max_cols * max_rows * sizeof(int), input_itemsets, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer bufIn1 (size:%d) => %d\n", max_cols * max_rows, err); return -1; }
-	err = clEnqueueWriteBuffer(cmd_queue, reference_d, 1, 0, max_cols * max_rows * sizeof(int), reference, 0, 0, 0);
-	if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer bufIn2 (size:%d) => %d\n", max_cols * max_rows, err); return -1; }
-		
-	int worksize = max_cols - 1;
-	printf("worksize = %d\n", worksize);
-	//these two parameters are for extension use, don't worry about it.
-	int offset_r = 0, offset_c = 0;
-	int block_width = worksize/BLOCK_SIZE ;
-	
-	clSetKernelArg(kernel1, 0, sizeof(void *), (void*) &reference_d);
-	clSetKernelArg(kernel1, 1, sizeof(void *), (void*) &input_itemsets_d);
-	clSetKernelArg(kernel1, 2, sizeof(void *), (void*) &output_itemsets_d);
-	clSetKernelArg(kernel1, 3, sizeof(cl_int) * (BLOCK_SIZE + 1) *(BLOCK_SIZE+1), (void*)NULL );
-	clSetKernelArg(kernel1, 4, sizeof(cl_int) *  BLOCK_SIZE * BLOCK_SIZE, (void*)NULL );
-	clSetKernelArg(kernel1, 5, sizeof(cl_int), (void*) &max_cols);
-	clSetKernelArg(kernel1, 6, sizeof(cl_int), (void*) &penalty);
-	clSetKernelArg(kernel1, 8, sizeof(cl_int), (void*) &block_width);
-	clSetKernelArg(kernel1, 9, sizeof(cl_int), (void*) &worksize);
-	clSetKernelArg(kernel1, 10, sizeof(cl_int), (void*) &offset_r);
-	clSetKernelArg(kernel1, 11, sizeof(cl_int), (void*) &offset_c);
-
-	clSetKernelArg(kernel2, 0, sizeof(void *), (void*) &reference_d);
-	clSetKernelArg(kernel2, 1, sizeof(void *), (void*) &input_itemsets_d);
-	clSetKernelArg(kernel2, 2, sizeof(void *), (void*) &output_itemsets_d);
-	clSetKernelArg(kernel2, 3, sizeof(cl_int) * (BLOCK_SIZE + 1) *(BLOCK_SIZE+1), (void*)NULL );
-	clSetKernelArg(kernel2, 4, sizeof(cl_int) * BLOCK_SIZE *BLOCK_SIZE, (void*)NULL );
-	clSetKernelArg(kernel2, 5, sizeof(cl_int), (void*) &max_cols);
-	clSetKernelArg(kernel2, 6, sizeof(cl_int), (void*) &penalty);
-	clSetKernelArg(kernel2, 8, sizeof(cl_int), (void*) &block_width);
-	clSetKernelArg(kernel2, 9, sizeof(cl_int), (void*) &worksize);
-	clSetKernelArg(kernel2, 10, sizeof(cl_int), (void*) &offset_r);
-	clSetKernelArg(kernel2, 11, sizeof(cl_int), (void*) &offset_c);
-	
-	printf("Processing upper-left matrix\n");
-	for( int blk = 1 ; blk <= worksize/BLOCK_SIZE ; blk++){
-	
-		global_work[0] = BLOCK_SIZE * blk;
-		local_work[0]  = BLOCK_SIZE;
-		clSetKernelArg(kernel1, 7, sizeof(cl_int), (void*) &blk);
-		err = clEnqueueNDRangeKernel(cmd_queue, kernel1, 2, NULL, global_work, local_work, 0, 0, 0);
-		if(err != CL_SUCCESS) { printf("ERROR: 1  clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }			
-	}
-	clFinish(cmd_queue);
-	
-	printf("Processing lower-right matrix\n");
-	for( int blk =  worksize/BLOCK_SIZE - 1  ; blk >= 1 ; blk--){	   
-		global_work[0] = BLOCK_SIZE * blk;
-		local_work[0] =  BLOCK_SIZE;
-		clSetKernelArg(kernel2, 7, sizeof(cl_int), (void*) &blk);
+  err = clBuildProgram(prog, 0, NULL, clOptions, NULL, NULL);
+  /*{ // show warnings/errors
+  static char log[65536]; memset(log, 0, sizeof(log));
+  cl_device_id device_id = 0;
+  err = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(device_id), &device_id, NULL);
+  clGetProgramBuildInfo(prog, device_id, CL_PROGRAM_BUILD_LOG, sizeof(log)-1, log, NULL);
+  if(err || strstr(log,"warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);
+  }*/
+  if(err != CL_SUCCESS) { printf("ERROR: clBuildProgram() => %d\n", err); return -1; }
+
+  cl_kernel kernel1;
+  cl_kernel kernel2;
+  kernel1 = clCreateKernel(prog, kernel_nw1, &err);
+  kernel2 = clCreateKernel(prog, kernel_nw2, &err);
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateKernel() 0 => %d\n", err); return -1; }
+  clReleaseProgram(prog);
+
+
+
+  // creat buffers
+  cl_mem input_itemsets_d;
+  cl_mem output_itemsets_d;
+  cl_mem reference_d;
+
+  input_itemsets_d = clCreateBuffer(context, CL_MEM_READ_WRITE, max_cols * max_rows * sizeof(int), NULL, &err );
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer input_item_set (size:%d) => %d\n", max_cols * max_rows, err); return -1;}
+  reference_d		 = clCreateBuffer(context, CL_MEM_READ_WRITE, max_cols * max_rows * sizeof(int), NULL, &err );
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer reference (size:%d) => %d\n", max_cols * max_rows, err); return -1;}
+  output_itemsets_d = clCreateBuffer(context, CL_MEM_READ_WRITE, max_cols * max_rows * sizeof(int), NULL, &err );
+  if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer output_item_set (size:%d) => %d\n", max_cols * max_rows, err); return -1;}
+
+  //write buffers
+  err = clEnqueueWriteBuffer(cmd_queue, input_itemsets_d, 1, 0, max_cols * max_rows * sizeof(int), input_itemsets, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer bufIn1 (size:%d) => %d\n", max_cols * max_rows, err); return -1; }
+  err = clEnqueueWriteBuffer(cmd_queue, reference_d, 1, 0, max_cols * max_rows * sizeof(int), reference, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer bufIn2 (size:%d) => %d\n", max_cols * max_rows, err); return -1; }
+
+  int worksize = max_cols - 1;
+  printf("worksize = %d\n", worksize);
+  //these two parameters are for extension use, don't worry about it.
+  int offset_r = 0, offset_c = 0;
+  int block_width = worksize/BLOCK_SIZE ;
+
+  clSetKernelArg(kernel1, 0, sizeof(void *), (void*) &reference_d);
+  clSetKernelArg(kernel1, 1, sizeof(void *), (void*) &input_itemsets_d);
+  clSetKernelArg(kernel1, 2, sizeof(void *), (void*) &output_itemsets_d);
+  clSetKernelArg(kernel1, 3, sizeof(cl_int) * (BLOCK_SIZE + 1) *(BLOCK_SIZE+1), (void*)NULL );
+  clSetKernelArg(kernel1, 4, sizeof(cl_int) *  BLOCK_SIZE * BLOCK_SIZE, (void*)NULL );
+  clSetKernelArg(kernel1, 5, sizeof(cl_int), (void*) &max_cols);
+  clSetKernelArg(kernel1, 6, sizeof(cl_int), (void*) &penalty);
+  clSetKernelArg(kernel1, 8, sizeof(cl_int), (void*) &block_width);
+  clSetKernelArg(kernel1, 9, sizeof(cl_int), (void*) &worksize);
+  clSetKernelArg(kernel1, 10, sizeof(cl_int), (void*) &offset_r);
+  clSetKernelArg(kernel1, 11, sizeof(cl_int), (void*) &offset_c);
+
+  clSetKernelArg(kernel2, 0, sizeof(void *), (void*) &reference_d);
+  clSetKernelArg(kernel2, 1, sizeof(void *), (void*) &input_itemsets_d);
+  clSetKernelArg(kernel2, 2, sizeof(void *), (void*) &output_itemsets_d);
+  clSetKernelArg(kernel2, 3, sizeof(cl_int) * (BLOCK_SIZE + 1) *(BLOCK_SIZE+1), (void*)NULL );
+  clSetKernelArg(kernel2, 4, sizeof(cl_int) * BLOCK_SIZE *BLOCK_SIZE, (void*)NULL );
+  clSetKernelArg(kernel2, 5, sizeof(cl_int), (void*) &max_cols);
+  clSetKernelArg(kernel2, 6, sizeof(cl_int), (void*) &penalty);
+  clSetKernelArg(kernel2, 8, sizeof(cl_int), (void*) &block_width);
+  clSetKernelArg(kernel2, 9, sizeof(cl_int), (void*) &worksize);
+  clSetKernelArg(kernel2, 10, sizeof(cl_int), (void*) &offset_r);
+  clSetKernelArg(kernel2, 11, sizeof(cl_int), (void*) &offset_c);
+
+  printf("Processing upper-left matrix\n");
+  for( int blk = 1 ; blk <= worksize/BLOCK_SIZE ; blk++){
+
+  global_work[0] = BLOCK_SIZE * blk;
+  local_work[0]  = BLOCK_SIZE;
+  clSetKernelArg(kernel1, 7, sizeof(cl_int), (void*) &blk);
+  err = clEnqueueNDRangeKernel(cmd_queue, kernel1, 2, NULL, global_work, local_work, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: 1  clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }
+  }
+  clFinish(cmd_queue);
+
+  printf("Processing lower-right matrix\n");
+  for( int blk =  worksize/BLOCK_SIZE - 1  ; blk >= 1 ; blk--){
+  global_work[0] = BLOCK_SIZE * blk;
+  local_work[0] =  BLOCK_SIZE;
+  clSetKernelArg(kernel2, 7, sizeof(cl_int), (void*) &blk);
         err = clEnqueueNDRangeKernel(cmd_queue, kernel2, 2, NULL, global_work, local_work, 0, 0, 0);
-		if(err != CL_SUCCESS) { printf("ERROR: 2 clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }
-	}
-    
+  if(err != CL_SUCCESS) { printf("ERROR: 2 clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }
+  }
+
     // Lingjie Zhang modified at Nov 1, 2015
     //	clFinish(cmd_queue);
     //	fflush(stdout);
-	//end Lingjie Zhang modification
+  //end Lingjie Zhang modification
 
     err = clEnqueueReadBuffer(cmd_queue, input_itemsets_d, 1, 0, max_cols * max_rows * sizeof(int), output_itemsets, 0, 0, 0);
-	clFinish(cmd_queue);
+  clFinish(cmd_queue);
 
-//#define TRACEBACK	
+//#define TRACEBACK
 #ifdef TRACEBACK
-	
-	FILE *fpo = fopen("result.txt","w");
-	fprintf(fpo, "print traceback value GPU:\n");
-    
-	for (int i = max_rows - 2,  j = max_rows - 2; i>=0, j>=0;){
-		int nw, n, w, traceback;
-		if ( i == max_rows - 2 && j == max_rows - 2 )
-			fprintf(fpo, "%d ", output_itemsets[ i * max_cols + j]); //print the first element
-		if ( i == 0 && j == 0 )
+
+  FILE *fpo = fopen("result.txt","w");
+  fprintf(fpo, "print traceback value GPU:\n");
+
+  for (int i = max_rows - 2,  j = max_rows - 2; i>=0, j>=0;){
+  int nw, n, w, traceback;
+  if ( i == max_rows - 2 && j == max_rows - 2 )
+  fprintf(fpo, "%d ", output_itemsets[ i * max_cols + j]); //print the first element
+  if ( i == 0 && j == 0 )
            break;
-		if ( i > 0 && j > 0 ){
-			nw = output_itemsets[(i - 1) * max_cols + j - 1];
-		    w  = output_itemsets[ i * max_cols + j - 1 ];
+  if ( i > 0 && j > 0 ){
+  nw = output_itemsets[(i - 1) * max_cols + j - 1];
+      w  = output_itemsets[ i * max_cols + j - 1 ];
             n  = output_itemsets[(i - 1) * max_cols + j];
-		}
-		else if ( i == 0 ){
-		    nw = n = LIMIT;
-		    w  = output_itemsets[ i * max_cols + j - 1 ];
-		}
-		else if ( j == 0 ){
-		    nw = w = LIMIT;
+  }
+  else if ( i == 0 ){
+      nw = n = LIMIT;
+      w  = output_itemsets[ i * max_cols + j - 1 ];
+  }
+  else if ( j == 0 ){
+      nw = w = LIMIT;
             n  = output_itemsets[(i - 1) * max_cols + j];
-		}
-		else{
-		}
-
-		//traceback = maximum(nw, w, n);
-		int new_nw, new_w, new_n;
-		new_nw = nw + reference[i * max_cols + j];
-		new_w = w - penalty;
-		new_n = n - penalty;
-		
-		traceback = maximum(new_nw, new_w, new_n);
-		if(traceback == new_nw)
-			traceback = nw;
-		if(traceback == new_w)
-			traceback = w;
-		if(traceback == new_n)
+  }
+  else{
+  }
+
+  //traceback = maximum(nw, w, n);
+  int new_nw, new_w, new_n;
+  new_nw = nw + reference[i * max_cols + j];
+  new_w = w - penalty;
+  new_n = n - penalty;
+
+  traceback = maximum(new_nw, new_w, new_n);
+  if(traceback == new_nw)
+  traceback = nw;
+  if(traceback == new_w)
+  traceback = w;
+  if(traceback == new_n)
             traceback = n;
-			
-		fprintf(fpo, "%d ", traceback);
 
-		if(traceback == nw )
-		{i--; j--; continue;}
+  fprintf(fpo, "%d ", traceback);
+
+  if(traceback == nw )
+  {i--; j--; continue;}
 
         else if(traceback == w )
-		{j--; continue;}
+  {j--; continue;}
 
         else if(traceback == n )
-		{i--; continue;}
+  {i--; continue;}
 
-		else
-		;
-	}
-	
-	fclose(fpo);
+  else
+  ;
+  }
+
+  fclose(fpo);
 
 #endif
 
-	printf("Computation Done\n");
+  printf("Computation Done\n");
     // OpenCL shutdown
-	if(shutdown()) return -1;
+  if(shutdown()) return -1;
 
-	clReleaseMemObject(input_itemsets_d);
-	clReleaseMemObject(output_itemsets_d);
-	clReleaseMemObject(reference_d);
+  clReleaseMemObject(input_itemsets_d);
+  clReleaseMemObject(output_itemsets_d);
+  clReleaseMemObject(reference_d);
 
-	free(reference);
-	free(input_itemsets);
-	free(output_itemsets);
-	
-}
+  free(reference);
+  free(input_itemsets);
+  free(output_itemsets);
 
+}

+ 10 - 10
opencl/particlefilter/ex_particle_OCL_double_seq.cpp

@@ -285,7 +285,7 @@ void strelDisk(int * disk, int radius) {
             double distance = sqrt(pow((double) (x - radius + 1), 2) + pow((double) (y - radius + 1), 2));
             if (distance < radius)
                 disk[x * diameter + y] = 1;
-	    else
+      else
                 disk[x * diameter + y] = 0;
         }
     }
@@ -706,7 +706,7 @@ int particleFilter(unsigned char * I, int IszX, int IszY, int Nfr, int * seed, i
         return -1;
     }
 
-	//Donnie - this loop is different because in this kernel, arrayX and arrayY
+  //Donnie - this loop is different because in this kernel, arrayX and arrayY
     //  are set equal to xj before every iteration, so effectively, arrayX and
     //  arrayY will be set to xe and ye before the first iteration.
     for (x = 0; x < Nparticles; x++) {
@@ -790,7 +790,7 @@ int particleFilter(unsigned char * I, int IszX, int IszY, int Nfr, int * seed, i
         clFinish(cmd_queue);
         if (err != CL_SUCCESS) {
             printf("ERROR: clEnqueueNDRangeKernel(kernel_likelihood)=>%d failed\n", err);
-	    //check_error(err, __FILE__, __LINE__);
+      //check_error(err, __FILE__, __LINE__);
             return -1;
         }
         /****************** E N D    L I K E L I H O O D **********************/
@@ -803,7 +803,7 @@ int particleFilter(unsigned char * I, int IszX, int IszY, int Nfr, int * seed, i
         clFinish(cmd_queue);
         if (err != CL_SUCCESS) {
             printf("ERROR: clEnqueueNDRangeKernel(kernel_sum)=>%d failed\n", err);
-	    //check_error(err, __FILE__, __LINE__);
+      //check_error(err, __FILE__, __LINE__);
             return -1;
         }/*************************** E N D   S U M ****************************/
 
@@ -822,12 +822,12 @@ int particleFilter(unsigned char * I, int IszX, int IszY, int Nfr, int * seed, i
         clFinish(cmd_queue);
         if (err != CL_SUCCESS) {
             printf("ERROR: clEnqueueNDRangeKernel(normalize_weights)=>%d failed\n", err);
-	    //check_error(err, __FILE__, __LINE__);
+      //check_error(err, __FILE__, __LINE__);
             return -1;
         }
         /************* E N D    N O R M A L I Z E     W E I G H T S ***********/
 
-	  //	ocl_print_double_array(cmd_queue, partial_sums, 40);
+    //	ocl_print_double_array(cmd_queue, partial_sums, 40);
         // /********* I N T E R M E D I A T E     R E S U L T S ***************/
         // //OpenCL memory copying back from GPU to CPU memory
          err = clEnqueueReadBuffer(cmd_queue, arrayX_GPU, 1, 0, sizeof (double) *Nparticles, arrayX, 0, 0, 0);
@@ -838,10 +838,10 @@ int particleFilter(unsigned char * I, int IszX, int IszY, int Nfr, int * seed, i
          ye = 0;
          double total=0.0;
          // estimate the object location by expected values
-	 for (x = 0; x < Nparticles; x++) {
+   for (x = 0; x < Nparticles; x++) {
             // if( 0.0000000 < arrayX[x]*weights[x]) printf("arrayX[%d]:%f, arrayY[%d]:%f, weights[%d]:%0.10f\n",x,arrayX[x], x, arrayY[x], x, weights[x]);
-	//	printf("arrayX[%d]:%f | arrayY[%d]:%f | weights[%d]:%f\n",
- 	//		x, arrayX[x], x, arrayY[x], x, weights[x]); 
+  //	printf("arrayX[%d]:%f | arrayY[%d]:%f | weights[%d]:%f\n",
+ 	//		x, arrayX[x], x, arrayY[x], x, weights[x]);
              xe += arrayX[x] * weights[x];
              ye += arrayY[x] * weights[x];
              total+= weights[x];
@@ -869,7 +869,7 @@ int particleFilter(unsigned char * I, int IszX, int IszY, int Nfr, int * seed, i
         clFinish(cmd_queue);
         if (err != CL_SUCCESS) {
             printf("ERROR: clEnqueueNDRangeKernel(find_index)=>%d failed\n", err);
-	    //check_error(err, __FILE__, __LINE__);
+      //check_error(err, __FILE__, __LINE__);
             return -1;
         }
         /******************* E N D    F I N D    I N D E X ********************/

+ 292 - 292
opencl/particlefilter/ex_particle_OCL_naive_seq.cpp

@@ -1,7 +1,7 @@
 /**
  * @file ex_particle_OPENMP_seq.c
  * @author Michael Trotter & Matt Goodrum
- * @brief Particle filter implementation in C/OpenMP 
+ * @brief Particle filter implementation in C/OpenMP
  */
 #include <stdio.h>
 #include <stdlib.h>
@@ -78,18 +78,18 @@ double * u;
 
 /***************************************************
   @brief initializes the OpenCL context and detects available platforms
-@param use_gpu 
+@param use_gpu
 **************************************************/
 static int initialize(int platform_id, int device_id, int use_gpu) {
     cl_int result;
     size_t size;
-    
+
     // modification to handle the case in which we have more than one OpenCL platform available on the system.
   	cl_uint platformCount;
 
     // create OpenCL context
     clGetPlatformIDs(0, NULL, &platformCount);
-  	
+
   	cl_platform_id *platforms_ids;
   	platforms_ids = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount);
   	if (clGetPlatformIDs(platformCount, platforms_ids, NULL) != CL_SUCCESS) { printf("ERROR: clGetPlatformIDs(1,*,0) failed\n"); return -1; }
@@ -163,7 +163,7 @@ float elapsed_time(long long start_time, long long end_time) {
     return (float) (end_time - start_time) / (1000 * 1000);
 }
 
-/** 
+/**
  * Takes in a double and returns an integer that approximates to that double
  * @return if the mantissa < .5 => return value < input value; else return value > input value
  */
@@ -432,115 +432,115 @@ int findIndex(double * CDF, int lengthCDF, double value) {
 }
 /*
 *@brief allocates and initializes the data for the computation
- * @param Nparticles 
+ * @param Nparticles
  * @param countOnes
 *
 */
 static int allocate(int Nparticles, int countOnes, int platform_id, int device_id, int use_gpu){
-	/***** variables ******/
-
-	int sourcesize = 1024 * 1024;
-	char * source = (char *) calloc(sourcesize, sizeof (char));
-	if (!source) {
-		printf("ERROR: calloc(%d) failed\n", sourcesize);
-		return -1;
-	}
-
-	// read the kernel core source
-	char * tempchar = "./particle_naive.cl";
-	FILE * fp = fopen(tempchar, "rb");
-	if (!fp) {
-		printf("ERROR: unable to open '%s'\n", tempchar);
-		return -1;
-	}
-	fread(source + strlen(source), sourcesize, 1, fp);
-	fclose(fp);
-
-	// OpenCL initialization
-	if (initialize(platform_id, device_id, use_gpu)) return -1;
-
-	// compile kernel
-	cl_int err = 0;
-	const char * slist[2] = {source, 0};
-	cl_program prog = clCreateProgramWithSource(context, 1, slist, NULL, &err);
-	if (err != CL_SUCCESS) {
-		printf("ERROR: clCreateProgramWithSource() => %d\n", err);
-		return -1;
-	}
-	err = clBuildProgram(prog, 0, NULL, "-cl-fast-relaxed-math", NULL, NULL);
-	{ // show warnings/errors
-		static char log[65536];
-		memset(log, 0, sizeof (log));
-		cl_device_id device_id[2] = {0};
-		err = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof (device_id), device_id, NULL);
-		if (err != CL_SUCCESS) {
-			if (err == CL_INVALID_CONTEXT)
-				printf("ERROR: clGetContextInfo() => CL_INVALID_CONTEXT\n");
-			if (err == CL_INVALID_VALUE)
-				printf("ERROR: clGetContextInfo() => CL_INVALID_VALUE\n");
-		}
-		err = clGetProgramBuildInfo(prog, device_id[0], CL_PROGRAM_BUILD_LOG, sizeof (log) - 1, log, NULL);
-		if (err != CL_SUCCESS) {
-			printf("ERROR: clGetProgramBuildInfo() => %d\n", err);
-		}
-		if (err || strstr(log, "warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);
-	}//*/
-	if (err != CL_SUCCESS) {
-		printf("ERROR: clBuildProgram() => %d\n", err);
-		return -1;
-	}
-
-	char * particle_kernel = "particle_kernel";
-
-	kernel_s = clCreateKernel(prog, particle_kernel, &err);
-	if (err != CL_SUCCESS) {
-		printf("ERROR: clCreateKernel() 0 => %d\n", err);
-		return -1;
-	}
-
-	clReleaseProgram(prog);
-
-
-	likelihood = (double *) malloc(sizeof (double) *Nparticles);
-	arrayX = (double *) malloc(sizeof (double) *Nparticles);
-	arrayY = (double *) malloc(sizeof (double) *Nparticles);
-	xj = (double *) malloc(sizeof (double) *Nparticles);
-	yj = (double *) malloc(sizeof (double) *Nparticles);
-	CDF = (double *) malloc(sizeof (double) *Nparticles);
-
-	ind = (int*) malloc(sizeof (int) *countOnes);
-	u = (double *) malloc(sizeof (double) *Nparticles);
-
-	//OpenCL memory allocation
-	arrayX_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
-	if (err != CL_SUCCESS) {
-		printf("ERROR: clCreateBuffer arrayX_GPU (size:%d) => %d\n", Nparticles, err);
-		return -1;
-	}
-	arrayY_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
-	if (err != CL_SUCCESS) {
-		printf("ERROR: clCreateBuffer arrayY_GPU (size:%d) => %d\n", Nparticles, err);
-		return -1;
-	}
-	xj_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
-	if (err != CL_SUCCESS) {
-		printf("ERROR: clCreateBuffer xj_GPU (size:%d) => %d\n", Nparticles, err);
-		return -1;
-	}
-	yj_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
-	if (err != CL_SUCCESS) {
-		printf("ERROR: clCreateBuffer yj_GPU (size:%d) => %d\n", Nparticles, err);
-		return -1;
-	}
-	CDF_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
-	if (err != CL_SUCCESS) {
-		printf("ERROR: clCreateBuffer CDF_GPU (size:%d) => %d\n", Nparticles, err);
-		return -1;
-	}
-	u_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
-	if (err != CL_SUCCESS) { printf("ERROR: clCreateBuffer u_GPU (size:%d) => %d\n", Nparticles, err); return -1; }
-
-  
+  /***** variables ******/
+
+  int sourcesize = 1024 * 1024;
+  char * source = (char *) calloc(sourcesize, sizeof (char));
+  if (!source) {
+  printf("ERROR: calloc(%d) failed\n", sourcesize);
+  return -1;
+  }
+
+  // read the kernel core source
+  char * tempchar = "./particle_naive.cl";
+  FILE * fp = fopen(tempchar, "rb");
+  if (!fp) {
+  printf("ERROR: unable to open '%s'\n", tempchar);
+  return -1;
+  }
+  fread(source + strlen(source), sourcesize, 1, fp);
+  fclose(fp);
+
+  // OpenCL initialization
+  if (initialize(platform_id, device_id, use_gpu)) return -1;
+
+  // compile kernel
+  cl_int err = 0;
+  const char * slist[2] = {source, 0};
+  cl_program prog = clCreateProgramWithSource(context, 1, slist, NULL, &err);
+  if (err != CL_SUCCESS) {
+  printf("ERROR: clCreateProgramWithSource() => %d\n", err);
+  return -1;
+  }
+  err = clBuildProgram(prog, 0, NULL, "-cl-fast-relaxed-math", NULL, NULL);
+  { // show warnings/errors
+  static char log[65536];
+  memset(log, 0, sizeof (log));
+  cl_device_id device_id[2] = {0};
+  err = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof (device_id), device_id, NULL);
+  if (err != CL_SUCCESS) {
+  if (err == CL_INVALID_CONTEXT)
+  printf("ERROR: clGetContextInfo() => CL_INVALID_CONTEXT\n");
+  if (err == CL_INVALID_VALUE)
+  printf("ERROR: clGetContextInfo() => CL_INVALID_VALUE\n");
+  }
+  err = clGetProgramBuildInfo(prog, device_id[0], CL_PROGRAM_BUILD_LOG, sizeof (log) - 1, log, NULL);
+  if (err != CL_SUCCESS) {
+  printf("ERROR: clGetProgramBuildInfo() => %d\n", err);
+  }
+  if (err || strstr(log, "warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);
+  }//*/
+  if (err != CL_SUCCESS) {
+  printf("ERROR: clBuildProgram() => %d\n", err);
+  return -1;
+  }
+
+  char * particle_kernel = "particle_kernel";
+
+  kernel_s = clCreateKernel(prog, particle_kernel, &err);
+  if (err != CL_SUCCESS) {
+  printf("ERROR: clCreateKernel() 0 => %d\n", err);
+  return -1;
+  }
+
+  clReleaseProgram(prog);
+
+
+  likelihood = (double *) malloc(sizeof (double) *Nparticles);
+  arrayX = (double *) malloc(sizeof (double) *Nparticles);
+  arrayY = (double *) malloc(sizeof (double) *Nparticles);
+  xj = (double *) malloc(sizeof (double) *Nparticles);
+  yj = (double *) malloc(sizeof (double) *Nparticles);
+  CDF = (double *) malloc(sizeof (double) *Nparticles);
+
+  ind = (int*) malloc(sizeof (int) *countOnes);
+  u = (double *) malloc(sizeof (double) *Nparticles);
+
+  //OpenCL memory allocation
+  arrayX_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
+  if (err != CL_SUCCESS) {
+  printf("ERROR: clCreateBuffer arrayX_GPU (size:%d) => %d\n", Nparticles, err);
+  return -1;
+  }
+  arrayY_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
+  if (err != CL_SUCCESS) {
+  printf("ERROR: clCreateBuffer arrayY_GPU (size:%d) => %d\n", Nparticles, err);
+  return -1;
+  }
+  xj_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
+  if (err != CL_SUCCESS) {
+  printf("ERROR: clCreateBuffer xj_GPU (size:%d) => %d\n", Nparticles, err);
+  return -1;
+  }
+  yj_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
+  if (err != CL_SUCCESS) {
+  printf("ERROR: clCreateBuffer yj_GPU (size:%d) => %d\n", Nparticles, err);
+  return -1;
+  }
+  CDF_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
+  if (err != CL_SUCCESS) {
+  printf("ERROR: clCreateBuffer CDF_GPU (size:%d) => %d\n", Nparticles, err);
+  return -1;
+  }
+  u_GPU = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof (double) *Nparticles, NULL, &err);
+  if (err != CL_SUCCESS) { printf("ERROR: clCreateBuffer u_GPU (size:%d) => %d\n", Nparticles, err); return -1; }
+
+
 
 }//eo allocate
 
@@ -556,188 +556,188 @@ static int allocate(int Nparticles, int countOnes, int platform_id, int device_i
  * @param Nparticles The number of particles to be used
  */
 int particleFilter(int * I, int IszX, int IszY, int Nfr, int * seed, int Nparticles, int platform_id, int device_id, int use_gpu) {
-	int max_size = IszX * IszY*Nfr;
-	long long start = get_time();
-	//original particle centroid
-	double xe = roundDouble(IszY / 2.0);
-	double ye = roundDouble(IszX / 2.0);
-
-	//expected object locations, compared to center
-	int radius = 5;
-	int diameter = radius * 2 - 1;
-	int * disk = (int *) malloc(diameter * diameter * sizeof (int));
-	strelDisk(disk, radius);
-	int countOnes = 0;
-	int x, y;
-	for (x = 0; x < diameter; x++) {
-		for (y = 0; y < diameter; y++) {
-			if (disk[x * diameter + y] == 1)
-				countOnes++;
-		}
-	}
-	double * objxy = (double *) malloc(countOnes * 2 * sizeof (double));
-	getneighbors(disk, countOnes, objxy, radius);
-
-	long long get_neighbors = get_time();
-	printf("TIME TO GET NEIGHBORS TOOK: %f\n", elapsed_time(start, get_neighbors));
-	//initial weights are all equal (1/Nparticles)
-	double * weights = (double *) malloc(sizeof (double) *Nparticles);
-	for (x = 0; x < Nparticles; x++) {
-		weights[x] = 1 / ((double) (Nparticles));
-	}
-	long long get_weights = get_time();
-	printf("TIME TO GET WEIGHTSTOOK: %f\n", elapsed_time(get_neighbors, get_weights));
-	//initial likelihood to 0.0
-
-	//allocate all of the memory for the computation 
-	allocate(Nparticles, countOnes, platform_id, device_id, use_gpu);
-	
-	for (x = 0; x < Nparticles; x++) {
-		arrayX[x] = xe;
-		arrayY[x] = ye;
-	}
-	int k;
-	int indX, indY;
-	for (k = 1; k < Nfr; k++) {
-		long long set_arrays = get_time();
-		//printf("TIME TO SET ARRAYS TOOK: %f\n", elapsed_time(get_weights, set_arrays));
-		//apply motion model
-		//draws sample from motion model (random walk). The only prior information
-		//is that the object moves 2x as fast as in the y direction
-
-		for (x = 0; x < Nparticles; x++) {
-			arrayX[x] = arrayX[x] + 1.0 + 5.0 * randn(seed, x);
-			arrayY[x] = arrayY[x] - 2.0 + 2.0 * randn(seed, x);
-		}
-		//particle filter likelihood
-		long long error = get_time();
-		printf("TIME TO SET ERROR TOOK: %f\n", elapsed_time(set_arrays, error));
-		for (x = 0; x < Nparticles; x++) {
-
-			//compute the likelihood: remember our assumption is that you know
-			// foreground and the background image intensity distribution.
-			// Notice that we consider here a likelihood ratio, instead of
-			// p(z|x). It is possible in this case. why? a hometask for you.            
-			//calc ind
-			for (y = 0; y < countOnes; y++) {
-				indX = roundDouble(arrayX[x]) + objxy[y * 2 + 1];
-				indY = roundDouble(arrayY[x]) + objxy[y * 2];
-				ind[y] = fabs(indX * IszY * Nfr + indY * Nfr + k);
-				if (ind[y] >= max_size)
-					ind[y] = 0;
-			}
-			likelihood[x] = calcLikelihoodSum(I, ind, countOnes);
-			likelihood[x] = likelihood[x] / countOnes;
-		}
-		long long likelihood_time = get_time();
-		printf("TIME TO GET LIKELIHOODS TOOK: %f\n", elapsed_time(error, likelihood_time));
-		// update & normalize weights
-		// using equation (63) of Arulampalam Tutorial          
-		for (x = 0; x < Nparticles; x++) {
-			weights[x] = weights[x] * exp(likelihood[x]);
-		}
-		long long exponential = get_time();
-		printf("TIME TO GET EXP TOOK: %f\n", elapsed_time(likelihood_time, exponential));
-		double sumWeights = 0;
-		for (x = 0; x < Nparticles; x++) {
-			sumWeights += weights[x];
-		}
-		long long sum_time = get_time();
-		printf("TIME TO SUM WEIGHTS TOOK: %f\n", elapsed_time(exponential, sum_time));
-		for (x = 0; x < Nparticles; x++) {
-			weights[x] = weights[x] / sumWeights;
-		}
-		long long normalize = get_time();
-		printf("TIME TO NORMALIZE WEIGHTS TOOK: %f\n", elapsed_time(sum_time, normalize));
-		xe = 0;
-		ye = 0;
-		// estimate the object location by expected values
-		for (x = 0; x < Nparticles; x++) {
-			xe += arrayX[x] * weights[x];
-			ye += arrayY[x] * weights[x];
-		}
-		long long move_time = get_time();
-		printf("TIME TO MOVE OBJECT TOOK: %f\n", elapsed_time(normalize, move_time));
-		printf("XE: %lf\n", xe);
-		printf("YE: %lf\n", ye);
-		double distance = sqrt(pow((double) (xe - (int) roundDouble(IszY / 2.0)), 2) + pow((double) (ye - (int) roundDouble(IszX / 2.0)), 2));
-		printf("%lf\n", distance);
-		//display(hold off for now)
-
-		//pause(hold off for now)
-
-		//resampling
-
-
-		CDF[0] = weights[0];
-		for (x = 1; x < Nparticles; x++) {
-			CDF[x] = weights[x] + CDF[x - 1];
-		}
-		long long cum_sum = get_time();
-		printf("TIME TO CALC CUM SUM TOOK: %f\n", elapsed_time(move_time, cum_sum));
-		double u1 = (1 / ((double) (Nparticles))) * randu(seed, 0);
-		for (x = 0; x < Nparticles; x++) {
-			u[x] = u1 + x / ((double) (Nparticles));
-		}
-		long long u_time = get_time();
-		printf("TIME TO CALC U TOOK: %f\n", elapsed_time(cum_sum, u_time));
-		long long start_copy = get_time();
-
-		//OpenCL memory allocation
-		cl_int err = clEnqueueWriteBuffer(cmd_queue, arrayX_GPU, 1, 0, sizeof (double) *Nparticles, arrayX, 0, 0, 0);
-		if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer arrayX_GPU (size:%d) => %d\n", Nparticles, err); return -1; }
-		err = clEnqueueWriteBuffer(cmd_queue, arrayY_GPU, 1, 0, sizeof (double) *Nparticles, arrayY, 0, 0, 0);
-		if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer arrayY_GPU (size:%d) => %d\n", Nparticles, err); return -1; }
-		err = clEnqueueWriteBuffer(cmd_queue, CDF_GPU, 1, 0, sizeof (double) *Nparticles, CDF, 0, 0, 0);
-		if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer CDF_GPU (size:%d) => %d\n", Nparticles, err); return -1; }
-		err = clEnqueueWriteBuffer(cmd_queue, u_GPU, 1, 0, sizeof (double) *Nparticles, u, 0, 0, 0);
-		if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer u_GPU (size:%d) => %d\n", Nparticles, err); return -1; }
-
-
-		long long end_copy = get_time();
-		//Set number of threads
-		int num_blocks = ceil((double) Nparticles / (double) threads_per_block);
-
-		clSetKernelArg(kernel_s, 0, sizeof (void *), (void*) &arrayX_GPU);
-		clSetKernelArg(kernel_s, 1, sizeof (void *), (void*) &arrayY_GPU);
-		clSetKernelArg(kernel_s, 2, sizeof (void *), (void*) &CDF_GPU);
-		clSetKernelArg(kernel_s, 3, sizeof (void *), (void*) &u_GPU);
-		clSetKernelArg(kernel_s, 4, sizeof (void *), (void*) &xj_GPU);
-		clSetKernelArg(kernel_s, 5, sizeof (void *), (void*) &yj_GPU);
-		clSetKernelArg(kernel_s, 6, sizeof (cl_int), (void*) &Nparticles);
-
-
-		//KERNEL FUNCTION CALL
-		size_t global_work[3] = {num_blocks*threads_per_block, 1, 1};
-		err = clEnqueueNDRangeKernel(cmd_queue, kernel_s, 1, NULL, global_work, NULL, 0, 0, 0);
-		clFinish(cmd_queue);
-		long long start_copy_back = get_time();
-                
+  int max_size = IszX * IszY*Nfr;
+  long long start = get_time();
+  //original particle centroid
+  double xe = roundDouble(IszY / 2.0);
+  double ye = roundDouble(IszX / 2.0);
+
+  //expected object locations, compared to center
+  int radius = 5;
+  int diameter = radius * 2 - 1;
+  int * disk = (int *) malloc(diameter * diameter * sizeof (int));
+  strelDisk(disk, radius);
+  int countOnes = 0;
+  int x, y;
+  for (x = 0; x < diameter; x++) {
+  for (y = 0; y < diameter; y++) {
+  if (disk[x * diameter + y] == 1)
+  countOnes++;
+  }
+  }
+  double * objxy = (double *) malloc(countOnes * 2 * sizeof (double));
+  getneighbors(disk, countOnes, objxy, radius);
+
+  long long get_neighbors = get_time();
+  printf("TIME TO GET NEIGHBORS TOOK: %f\n", elapsed_time(start, get_neighbors));
+  //initial weights are all equal (1/Nparticles)
+  double * weights = (double *) malloc(sizeof (double) *Nparticles);
+  for (x = 0; x < Nparticles; x++) {
+  weights[x] = 1 / ((double) (Nparticles));
+  }
+  long long get_weights = get_time();
+  printf("TIME TO GET WEIGHTSTOOK: %f\n", elapsed_time(get_neighbors, get_weights));
+  //initial likelihood to 0.0
+
+  //allocate all of the memory for the computation
+  allocate(Nparticles, countOnes, platform_id, device_id, use_gpu);
+
+  for (x = 0; x < Nparticles; x++) {
+  arrayX[x] = xe;
+  arrayY[x] = ye;
+  }
+  int k;
+  int indX, indY;
+  for (k = 1; k < Nfr; k++) {
+  long long set_arrays = get_time();
+  //printf("TIME TO SET ARRAYS TOOK: %f\n", elapsed_time(get_weights, set_arrays));
+  //apply motion model
+  //draws sample from motion model (random walk). The only prior information
+  //is that the object moves 2x as fast as in the y direction
+
+  for (x = 0; x < Nparticles; x++) {
+  arrayX[x] = arrayX[x] + 1.0 + 5.0 * randn(seed, x);
+  arrayY[x] = arrayY[x] - 2.0 + 2.0 * randn(seed, x);
+  }
+  //particle filter likelihood
+  long long error = get_time();
+  printf("TIME TO SET ERROR TOOK: %f\n", elapsed_time(set_arrays, error));
+  for (x = 0; x < Nparticles; x++) {
+
+  //compute the likelihood: remember our assumption is that you know
+  // foreground and the background image intensity distribution.
+  // Notice that we consider here a likelihood ratio, instead of
+  // p(z|x). It is possible in this case. why? a hometask for you.
+  //calc ind
+  for (y = 0; y < countOnes; y++) {
+  indX = roundDouble(arrayX[x]) + objxy[y * 2 + 1];
+  indY = roundDouble(arrayY[x]) + objxy[y * 2];
+  ind[y] = fabs(indX * IszY * Nfr + indY * Nfr + k);
+  if (ind[y] >= max_size)
+  ind[y] = 0;
+  }
+  likelihood[x] = calcLikelihoodSum(I, ind, countOnes);
+  likelihood[x] = likelihood[x] / countOnes;
+  }
+  long long likelihood_time = get_time();
+  printf("TIME TO GET LIKELIHOODS TOOK: %f\n", elapsed_time(error, likelihood_time));
+  // update & normalize weights
+  // using equation (63) of Arulampalam Tutorial
+  for (x = 0; x < Nparticles; x++) {
+  weights[x] = weights[x] * exp(likelihood[x]);
+  }
+  long long exponential = get_time();
+  printf("TIME TO GET EXP TOOK: %f\n", elapsed_time(likelihood_time, exponential));
+  double sumWeights = 0;
+  for (x = 0; x < Nparticles; x++) {
+  sumWeights += weights[x];
+  }
+  long long sum_time = get_time();
+  printf("TIME TO SUM WEIGHTS TOOK: %f\n", elapsed_time(exponential, sum_time));
+  for (x = 0; x < Nparticles; x++) {
+  weights[x] = weights[x] / sumWeights;
+  }
+  long long normalize = get_time();
+  printf("TIME TO NORMALIZE WEIGHTS TOOK: %f\n", elapsed_time(sum_time, normalize));
+  xe = 0;
+  ye = 0;
+  // estimate the object location by expected values
+  for (x = 0; x < Nparticles; x++) {
+  xe += arrayX[x] * weights[x];
+  ye += arrayY[x] * weights[x];
+  }
+  long long move_time = get_time();
+  printf("TIME TO MOVE OBJECT TOOK: %f\n", elapsed_time(normalize, move_time));
+  printf("XE: %lf\n", xe);
+  printf("YE: %lf\n", ye);
+  double distance = sqrt(pow((double) (xe - (int) roundDouble(IszY / 2.0)), 2) + pow((double) (ye - (int) roundDouble(IszX / 2.0)), 2));
+  printf("%lf\n", distance);
+  //display(hold off for now)
+
+  //pause(hold off for now)
+
+  //resampling
+
+
+  CDF[0] = weights[0];
+  for (x = 1; x < Nparticles; x++) {
+  CDF[x] = weights[x] + CDF[x - 1];
+  }
+  long long cum_sum = get_time();
+  printf("TIME TO CALC CUM SUM TOOK: %f\n", elapsed_time(move_time, cum_sum));
+  double u1 = (1 / ((double) (Nparticles))) * randu(seed, 0);
+  for (x = 0; x < Nparticles; x++) {
+  u[x] = u1 + x / ((double) (Nparticles));
+  }
+  long long u_time = get_time();
+  printf("TIME TO CALC U TOOK: %f\n", elapsed_time(cum_sum, u_time));
+  long long start_copy = get_time();
+
+  //OpenCL memory allocation
+  cl_int err = clEnqueueWriteBuffer(cmd_queue, arrayX_GPU, 1, 0, sizeof (double) *Nparticles, arrayX, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer arrayX_GPU (size:%d) => %d\n", Nparticles, err); return -1; }
+  err = clEnqueueWriteBuffer(cmd_queue, arrayY_GPU, 1, 0, sizeof (double) *Nparticles, arrayY, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer arrayY_GPU (size:%d) => %d\n", Nparticles, err); return -1; }
+  err = clEnqueueWriteBuffer(cmd_queue, CDF_GPU, 1, 0, sizeof (double) *Nparticles, CDF, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer CDF_GPU (size:%d) => %d\n", Nparticles, err); return -1; }
+  err = clEnqueueWriteBuffer(cmd_queue, u_GPU, 1, 0, sizeof (double) *Nparticles, u, 0, 0, 0);
+  if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer u_GPU (size:%d) => %d\n", Nparticles, err); return -1; }
+
+
+  long long end_copy = get_time();
+  //Set number of threads
+  int num_blocks = ceil((double) Nparticles / (double) threads_per_block);
+
+  clSetKernelArg(kernel_s, 0, sizeof (void *), (void*) &arrayX_GPU);
+  clSetKernelArg(kernel_s, 1, sizeof (void *), (void*) &arrayY_GPU);
+  clSetKernelArg(kernel_s, 2, sizeof (void *), (void*) &CDF_GPU);
+  clSetKernelArg(kernel_s, 3, sizeof (void *), (void*) &u_GPU);
+  clSetKernelArg(kernel_s, 4, sizeof (void *), (void*) &xj_GPU);
+  clSetKernelArg(kernel_s, 5, sizeof (void *), (void*) &yj_GPU);
+  clSetKernelArg(kernel_s, 6, sizeof (cl_int), (void*) &Nparticles);
+
+
+  //KERNEL FUNCTION CALL
+  size_t global_work[3] = {num_blocks*threads_per_block, 1, 1};
+  err = clEnqueueNDRangeKernel(cmd_queue, kernel_s, 1, NULL, global_work, NULL, 0, 0, 0);
+  clFinish(cmd_queue);
+  long long start_copy_back = get_time();
+
                 if (err != CL_SUCCESS) {
-			printf("ERROR: clEnqueueNDRangeKernel()=>%d failed\n", err);
-			return -1;
-		}
-		//OpenCL memory copying back from GPU to CPU memory
-		err = clEnqueueReadBuffer(cmd_queue, yj_GPU, 1, 0, sizeof (double) *Nparticles, yj, 0, 0, 0);
-		if (err != CL_SUCCESS) {
-			printf("ERROR: Memcopy Out\n");
-			return -1;
-		}
-		err = clEnqueueReadBuffer(cmd_queue, xj_GPU, 1, 0, sizeof (double) *Nparticles, xj, 0, 0, 0);
-		if (err != CL_SUCCESS) {
-			printf("ERROR: Memcopy Out\n");
-			return -1;
-		}
-
-		long long end_copy_back = get_time();
-
-		printf("SENDING TO GPU TOOK: %lf\n", elapsed_time(start_copy, end_copy));
+  printf("ERROR: clEnqueueNDRangeKernel()=>%d failed\n", err);
+  return -1;
+  }
+  //OpenCL memory copying back from GPU to CPU memory
+  err = clEnqueueReadBuffer(cmd_queue, yj_GPU, 1, 0, sizeof (double) *Nparticles, yj, 0, 0, 0);
+  if (err != CL_SUCCESS) {
+  printf("ERROR: Memcopy Out\n");
+  return -1;
+  }
+  err = clEnqueueReadBuffer(cmd_queue, xj_GPU, 1, 0, sizeof (double) *Nparticles, xj, 0, 0, 0);
+  if (err != CL_SUCCESS) {
+  printf("ERROR: Memcopy Out\n");
+  return -1;
+  }
+
+  long long end_copy_back = get_time();
+
+  printf("SENDING TO GPU TOOK: %lf\n", elapsed_time(start_copy, end_copy));
         printf("OPEN_CL EXEC TOOK: %lf\n", elapsed_time(end_copy, start_copy_back));
         printf("SENDING BACK FROM GPU TOOK: %lf\n", elapsed_time(start_copy_back, end_copy_back));
         long long xyj_time = get_time();
         printf("TIME TO CALC NEW ARRAY X AND Y TOOK: %f\n", elapsed_time(u_time, xyj_time));
         //reassign arrayX and arrayY
-        
+
         //FIXED: this used to be a memory leak where arrayX was assigned the address of xj
         for (x = 0; x < Nparticles; x++) {
             arrayX[x] = xj[x];
@@ -748,7 +748,7 @@ int particleFilter(int * I, int IszX, int IszY, int Nfr, int * seed, int Npartic
         printf("TIME TO RESET WEIGHTS TOOK: %f\n", elapsed_time(xyj_time, reset));
     }
 
-    
+
     //OpenCL freeing of memory
 
     clReleaseMemObject(u_GPU);
@@ -789,7 +789,7 @@ int main(int argc, char * argv[]) {
     }
 
     int IszX, IszY, Nfr, Nparticles;
-    
+
     // Additional variables for platform and device selection
     int platform_id = 0;
     int device_id = 0;
@@ -838,22 +838,22 @@ int main(int argc, char * argv[]) {
         printf("Number of particles must be > 0\n");
         return 0;
     }
-    
+
     if (sscanf(argv[10], "%d", &platform_id) == EOF) {
         printf("ERROR: platform_id is incorrect");
         return 0;
     }
-    
+
     if (sscanf(argv[12], "%d", &device_id) == EOF) {
         printf("ERROR: device_id is incorrect");
         return 0;
     }
-    
+
     if (sscanf(argv[14], "%d", &use_gpu) == EOF) {
         printf("ERROR: use_gpu is incorrect, r, 1 for GPU and 0 for CPU");
         return 0;
     }
-    
+
     //establish seed
     int * seed = (int *) malloc(sizeof (int) *Nparticles);
     int i;

+ 10 - 10
opencl/particlefilter/ex_particle_OCL_single_seq.cpp

@@ -285,7 +285,7 @@ void strelDisk(int * disk, int radius) {
             float distance = sqrt(pow((float) (x - radius + 1), 2) + pow((float) (y - radius + 1), 2));
             if (distance < radius)
                 disk[x * diameter + y] = 1;
-	    else
+      else
                 disk[x * diameter + y] = 0;
         }
     }
@@ -706,7 +706,7 @@ int particleFilter(unsigned char * I, int IszX, int IszY, int Nfr, int * seed, i
         return -1;
     }
 
-	//Donnie - this loop is different because in this kernel, arrayX and arrayY
+  //Donnie - this loop is different because in this kernel, arrayX and arrayY
     //  are set equal to xj before every iteration, so effectively, arrayX and
     //  arrayY will be set to xe and ye before the first iteration.
     for (x = 0; x < Nparticles; x++) {
@@ -790,7 +790,7 @@ int particleFilter(unsigned char * I, int IszX, int IszY, int Nfr, int * seed, i
         clFinish(cmd_queue);
         if (err != CL_SUCCESS) {
             printf("ERROR: clEnqueueNDRangeKernel(kernel_likelihood)=>%d failed\n", err);
-	    //check_error(err, __FILE__, __LINE__);
+      //check_error(err, __FILE__, __LINE__);
             return -1;
         }
         /****************** E N D    L I K E L I H O O D **********************/
@@ -803,7 +803,7 @@ int particleFilter(unsigned char * I, int IszX, int IszY, int Nfr, int * seed, i
         clFinish(cmd_queue);
         if (err != CL_SUCCESS) {
             printf("ERROR: clEnqueueNDRangeKernel(kernel_sum)=>%d failed\n", err);
-	    //check_error(err, __FILE__, __LINE__);
+      //check_error(err, __FILE__, __LINE__);
             return -1;
         }/*************************** E N D   S U M ****************************/
 
@@ -822,12 +822,12 @@ int particleFilter(unsigned char * I, int IszX, int IszY, int Nfr, int * seed, i
         clFinish(cmd_queue);
         if (err != CL_SUCCESS) {
             printf("ERROR: clEnqueueNDRangeKernel(normalize_weights)=>%d failed\n", err);
-	    //check_error(err, __FILE__, __LINE__);
+      //check_error(err, __FILE__, __LINE__);
             return -1;
         }
         /************* E N D    N O R M A L I Z E     W E I G H T S ***********/
 
-	  //	ocl_print_float_array(cmd_queue, partial_sums, 40);
+    //	ocl_print_float_array(cmd_queue, partial_sums, 40);
         // /********* I N T E R M E D I A T E     R E S U L T S ***************/
         // //OpenCL memory copying back from GPU to CPU memory
          err = clEnqueueReadBuffer(cmd_queue, arrayX_GPU, 1, 0, sizeof (float) *Nparticles, arrayX, 0, 0, 0);
@@ -838,10 +838,10 @@ int particleFilter(unsigned char * I, int IszX, int IszY, int Nfr, int * seed, i
          ye = 0;
          float total=0.0;
          // estimate the object location by expected values
-	 for (x = 0; x < Nparticles; x++) {
+   for (x = 0; x < Nparticles; x++) {
             // if( 0.0000000 < arrayX[x]*weights[x]) printf("arrayX[%d]:%f, arrayY[%d]:%f, weights[%d]:%0.10f\n",x,arrayX[x], x, arrayY[x], x, weights[x]);
-	//	printf("arrayX[%d]:%f | arrayY[%d]:%f | weights[%d]:%f\n",
- 	//		x, arrayX[x], x, arrayY[x], x, weights[x]); 
+  //	printf("arrayX[%d]:%f | arrayY[%d]:%f | weights[%d]:%f\n",
+ 	//		x, arrayX[x], x, arrayY[x], x, weights[x]);
              xe += arrayX[x] * weights[x];
              ye += arrayY[x] * weights[x];
              total+= weights[x];
@@ -869,7 +869,7 @@ int particleFilter(unsigned char * I, int IszX, int IszY, int Nfr, int * seed, i
         clFinish(cmd_queue);
         if (err != CL_SUCCESS) {
             printf("ERROR: clEnqueueNDRangeKernel(find_index)=>%d failed\n", err);
-	    //check_error(err, __FILE__, __LINE__);
+      //check_error(err, __FILE__, __LINE__);
             return -1;
         }
         /******************* E N D    F I N D    I N D E X ********************/

+ 236 - 236
opencl/pathfinder/OpenCL.cpp

@@ -3,297 +3,297 @@
 
 OpenCL::OpenCL(int displayOutput)
 {
-	VERBOSE = displayOutput;
+  VERBOSE = displayOutput;
 }
 
 OpenCL::~OpenCL()
 {
-	// Flush and kill the command queue...
-	clFlush(command_queue);
-	clFinish(command_queue);
-	
-	// Release each kernel in the map kernelArray
-	map<string, cl_kernel>::iterator it;
-	for ( it=kernelArray.begin() ; it != kernelArray.end(); it++ )
-		clReleaseKernel( (*it).second );
-		
-	// Now the program...
-	clReleaseProgram(program);
-	
-	// ...and finally, the queue and context.
-	clReleaseCommandQueue(command_queue);
-	clReleaseContext(context);
+  // Flush and kill the command queue...
+  clFlush(command_queue);
+  clFinish(command_queue);
+
+  // Release each kernel in the map kernelArray
+  map<string, cl_kernel>::iterator it;
+  for ( it=kernelArray.begin() ; it != kernelArray.end(); it++ )
+  clReleaseKernel( (*it).second );
+
+  // Now the program...
+  clReleaseProgram(program);
+
+  // ...and finally, the queue and context.
+  clReleaseCommandQueue(command_queue);
+  clReleaseContext(context);
 }
 
 size_t OpenCL::localSize()
 {
-	return this->lwsize;
+  return this->lwsize;
 }
 
 cl_command_queue OpenCL::q()
 {
-	return this->command_queue;
+  return this->command_queue;
 }
 
 void OpenCL::launch(string toLaunch)
 {
-	// Launch the kernel (or at least enqueue it).
-	ret = clEnqueueNDRangeKernel(command_queue, 
-	                             kernelArray[toLaunch],
-	                             1,
-	                             NULL,
-	                             &gwsize,
-	                             &lwsize,
-	                             0, 
-	                             NULL, 
-	                             NULL);
-	
-	if (ret != CL_SUCCESS)
-	{
-		printf("\nError attempting to launch %s. Error in clCreateProgramWithSource with error code %i\n\n", toLaunch.c_str(), ret);
-		exit(1);
-	}
+  // Launch the kernel (or at least enqueue it).
+  ret = clEnqueueNDRangeKernel(command_queue,
+                               kernelArray[toLaunch],
+                               1,
+                               NULL,
+                               &gwsize,
+                               &lwsize,
+                               0,
+                               NULL,
+                               NULL);
+
+  if (ret != CL_SUCCESS)
+  {
+  printf("\nError attempting to launch %s. Error in clCreateProgramWithSource with error code %i\n\n", toLaunch.c_str(), ret);
+  exit(1);
+  }
 }
 
 void OpenCL::gwSize(size_t theSize)
 {
-	this->gwsize = theSize;
+  this->gwsize = theSize;
 }
 
 cl_context OpenCL::ctxt()
 {
-	return this->context;
+  return this->context;
 }
 
 cl_kernel OpenCL::kernel(string kernelName)
 {
-	return this->kernelArray[kernelName];
+  return this->kernelArray[kernelName];
 }
 
 void OpenCL::createKernel(string kernelName, int device_idx)
 {
-	cl_kernel kernel = clCreateKernel(this->program, kernelName.c_str(), NULL);
-	kernelArray[kernelName] = kernel;
-	
-	// Get the kernel work group size.
-	clGetKernelWorkGroupInfo(kernelArray[kernelName], device_id[device_idx], CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &lwsize, NULL);
-	if (lwsize == 0)
-	{
-		cout << "Error: clGetKernelWorkGroupInfo() returned a max work group size of zero!" << endl;
-		exit(1);
-	}
-	
-	// Local work size must divide evenly into global work size.
-	size_t howManyThreads = lwsize;
-	if (lwsize > gwsize)
-	{
-		lwsize = gwsize;
-		printf("Using %zu for local work size. \n", lwsize);
-	}
-	else
-	{
-		while (gwsize % howManyThreads != 0)
-		{
-			howManyThreads--;
-		}
-		if (VERBOSE)
-			printf("Max local threads is %zu. Using %zu for local work size. \n", lwsize, howManyThreads);
-
-		this->lwsize = howManyThreads;
-	}
+  cl_kernel kernel = clCreateKernel(this->program, kernelName.c_str(), NULL);
+  kernelArray[kernelName] = kernel;
+
+  // Get the kernel work group size.
+  clGetKernelWorkGroupInfo(kernelArray[kernelName], device_id[device_idx], CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &lwsize, NULL);
+  if (lwsize == 0)
+  {
+  cout << "Error: clGetKernelWorkGroupInfo() returned a max work group size of zero!" << endl;
+  exit(1);
+  }
+
+  // Local work size must divide evenly into global work size.
+  size_t howManyThreads = lwsize;
+  if (lwsize > gwsize)
+  {
+  lwsize = gwsize;
+  printf("Using %zu for local work size. \n", lwsize);
+  }
+  else
+  {
+  while (gwsize % howManyThreads != 0)
+  {
+  howManyThreads--;
+  }
+  if (VERBOSE)
+  printf("Max local threads is %zu. Using %zu for local work size. \n", lwsize, howManyThreads);
+
+  this->lwsize = howManyThreads;
+  }
 }
 
 void OpenCL::buildKernel(int device_idx)
 {
-	/* Load the source code for all of the kernels into the array source_str */
-	FILE*  theFile;
-	char*  source_str;
-	size_t source_size;
-	
-	theFile = fopen("kernels.cl", "r");
-	if (!theFile)
-	{
-		fprintf(stderr, "Failed to load kernel file.\n");
-		exit(1);
-	}
-	// Obtain length of source file.
-	fseek(theFile, 0, SEEK_END);
-	source_size = ftell(theFile);
-	rewind(theFile);
-	// Read in the file.
-	source_str = (char*) malloc(sizeof(char) * (source_size + 1));
-	fread(source_str, 1, source_size, theFile);
-	fclose(theFile);
-	source_str[source_size] = '\0';
-
-	// Create a program from the kernel source.
-	program = clCreateProgramWithSource(context,
-	                                    1,
-	                                    (const char **) &source_str,
-	                                    NULL,           // Number of chars in kernel src. NULL means src is null-terminated.
-	                                    &ret);          // Return status message in the ret variable.
-
-	if (ret != CL_SUCCESS)
-	{
-		printf("\nError at clCreateProgramWithSource! Error code %i\n\n", ret);
-		exit(1);
-	}
-
-	// Memory cleanup for the variable used to hold the kernel source.
-	free(source_str);
-	
-	// Build (compile) the program.
-	ret = clBuildProgram(program, NULL, NULL, NULL, NULL, NULL);
-	
-	if (ret != CL_SUCCESS)
-	{
-		printf("\nError at clBuildProgram! Error code %i\n\n", ret);
-		cout << "\n*************************************************" << endl;
-		cout << "***   OUTPUT FROM COMPILING THE KERNEL FILE   ***" << endl;
-		cout << "*************************************************" << endl;
-		// Shows the log
-		char*  build_log;
-		size_t log_size;
-		// First call to know the proper size
-		clGetProgramBuildInfo(program, device_id[device_idx], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
-		build_log = new char[log_size + 1];
-		// Second call to get the log
-		clGetProgramBuildInfo(program, device_id[device_idx], CL_PROGRAM_BUILD_LOG, log_size, build_log, NULL);
-		build_log[log_size] = '\0';
-		cout << build_log << endl;
-		delete[] build_log;
-		cout << "\n*************************************************" << endl;
-		cout << "*** END OUTPUT FROM COMPILING THE KERNEL FILE ***" << endl;
-		cout << "*************************************************\n\n" << endl;
-		exit(1);
-	}
-
-
-	/* Show error info from building the program. */
-	if (VERBOSE)
-	{
-		cout << "\n*************************************************" << endl;
-		cout << "***   OUTPUT FROM COMPILING THE KERNEL FILE   ***" << endl;
-		cout << "*************************************************" << endl;
-		// Shows the log
-		char*  build_log;
-		size_t log_size;
-		// First call to know the proper size
-		clGetProgramBuildInfo(program, device_id[device_idx], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
-		build_log = new char[log_size + 1];
-		// Second call to get the log
-		clGetProgramBuildInfo(program, device_id[device_idx], CL_PROGRAM_BUILD_LOG, log_size, build_log, NULL);
-		build_log[log_size] = '\0';
-		cout << build_log << endl;
-		delete[] build_log;
-		cout << "\n*************************************************" << endl;
-		cout << "*** END OUTPUT FROM COMPILING THE KERNEL FILE ***" << endl;
-		cout << "*************************************************\n\n" << endl;
-	}
+  /* Load the source code for all of the kernels into the array source_str */
+  FILE*  theFile;
+  char*  source_str;
+  size_t source_size;
+
+  theFile = fopen("kernels.cl", "r");
+  if (!theFile)
+  {
+  fprintf(stderr, "Failed to load kernel file.\n");
+  exit(1);
+  }
+  // Obtain length of source file.
+  fseek(theFile, 0, SEEK_END);
+  source_size = ftell(theFile);
+  rewind(theFile);
+  // Read in the file.
+  source_str = (char*) malloc(sizeof(char) * (source_size + 1));
+  fread(source_str, 1, source_size, theFile);
+  fclose(theFile);
+  source_str[source_size] = '\0';
+
+  // Create a program from the kernel source.
+  program = clCreateProgramWithSource(context,
+                                      1,
+                                      (const char **) &source_str,
+                                      NULL,           // Number of chars in kernel src. NULL means src is null-terminated.
+                                      &ret);          // Return status message in the ret variable.
+
+  if (ret != CL_SUCCESS)
+  {
+  printf("\nError at clCreateProgramWithSource! Error code %i\n\n", ret);
+  exit(1);
+  }
+
+  // Memory cleanup for the variable used to hold the kernel source.
+  free(source_str);
+
+  // Build (compile) the program.
+  ret = clBuildProgram(program, NULL, NULL, NULL, NULL, NULL);
+
+  if (ret != CL_SUCCESS)
+  {
+  printf("\nError at clBuildProgram! Error code %i\n\n", ret);
+  cout << "\n*************************************************" << endl;
+  cout << "***   OUTPUT FROM COMPILING THE KERNEL FILE   ***" << endl;
+  cout << "*************************************************" << endl;
+  // Shows the log
+  char*  build_log;
+  size_t log_size;
+  // First call to know the proper size
+  clGetProgramBuildInfo(program, device_id[device_idx], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
+  build_log = new char[log_size + 1];
+  // Second call to get the log
+  clGetProgramBuildInfo(program, device_id[device_idx], CL_PROGRAM_BUILD_LOG, log_size, build_log, NULL);
+  build_log[log_size] = '\0';
+  cout << build_log << endl;
+  delete[] build_log;
+  cout << "\n*************************************************" << endl;
+  cout << "*** END OUTPUT FROM COMPILING THE KERNEL FILE ***" << endl;
+  cout << "*************************************************\n\n" << endl;
+  exit(1);
+  }
+
+
+  /* Show error info from building the program. */
+  if (VERBOSE)
+  {
+  cout << "\n*************************************************" << endl;
+  cout << "***   OUTPUT FROM COMPILING THE KERNEL FILE   ***" << endl;
+  cout << "*************************************************" << endl;
+  // Shows the log
+  char*  build_log;
+  size_t log_size;
+  // First call to know the proper size
+  clGetProgramBuildInfo(program, device_id[device_idx], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
+  build_log = new char[log_size + 1];
+  // Second call to get the log
+  clGetProgramBuildInfo(program, device_id[device_idx], CL_PROGRAM_BUILD_LOG, log_size, build_log, NULL);
+  build_log[log_size] = '\0';
+  cout << build_log << endl;
+  delete[] build_log;
+  cout << "\n*************************************************" << endl;
+  cout << "*** END OUTPUT FROM COMPILING THE KERNEL FILE ***" << endl;
+  cout << "*************************************************\n\n" << endl;
+  }
 }
 
 void OpenCL::getDevices(int platform_idx, int device_idx, cl_device_type deviceType)
 {
-	cl_uint         platforms_n = 0;
-	cl_uint         devices_n   = 0;
+  cl_uint         platforms_n = 0;
+  cl_uint         devices_n   = 0;
 
   // create OpenCL context
   clGetPlatformIDs(0, NULL, &platforms_n);
-  
+
   cl_platform_id *platforms_ids;
   platforms_ids = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platforms_n);
-	
-	/* The following code queries the number of platforms and devices, and
-	 * lists the information about both.
-	 */
-	clGetPlatformIDs(platforms_n, platforms_ids, NULL);
-	if (VERBOSE)
-	{
-		printf("\n=== %d OpenCL platform(s) found: ===\n", platforms_n);
-		for (int i = 0; i < platforms_n; i++)
-		{
-			char buffer[10240];
-			printf("  -- %d --\n", i);
-			clGetPlatformInfo(platforms_ids[i], CL_PLATFORM_PROFILE, 10240, buffer,
-			                  NULL);
-			printf("  PROFILE = %s\n", buffer);
-			clGetPlatformInfo(platforms_ids[i], CL_PLATFORM_VERSION, 10240, buffer,
-			                  NULL);
-			printf("  VERSION = %s\n", buffer);
-			clGetPlatformInfo(platforms_ids[i], CL_PLATFORM_NAME, 10240, buffer, NULL);
-			printf("  NAME = %s\n", buffer);
-			clGetPlatformInfo(platforms_ids[i], CL_PLATFORM_VENDOR, 10240, buffer, NULL);
-			printf("  VENDOR = %s\n", buffer);
-			clGetPlatformInfo(platforms_ids[i], CL_PLATFORM_EXTENSIONS, 10240, buffer,
-			                  NULL);
-			printf("  EXTENSIONS = %s\n", buffer);
-		}
-	}
-  
+
+  /* The following code queries the number of platforms and devices, and
+   * lists the information about both.
+   */
+  clGetPlatformIDs(platforms_n, platforms_ids, NULL);
+  if (VERBOSE)
+  {
+  printf("\n=== %d OpenCL platform(s) found: ===\n", platforms_n);
+  for (int i = 0; i < platforms_n; i++)
+  {
+  char buffer[10240];
+  printf("  -- %d --\n", i);
+  clGetPlatformInfo(platforms_ids[i], CL_PLATFORM_PROFILE, 10240, buffer,
+                    NULL);
+  printf("  PROFILE = %s\n", buffer);
+  clGetPlatformInfo(platforms_ids[i], CL_PLATFORM_VERSION, 10240, buffer,
+                    NULL);
+  printf("  VERSION = %s\n", buffer);
+  clGetPlatformInfo(platforms_ids[i], CL_PLATFORM_NAME, 10240, buffer, NULL);
+  printf("  NAME = %s\n", buffer);
+  clGetPlatformInfo(platforms_ids[i], CL_PLATFORM_VENDOR, 10240, buffer, NULL);
+  printf("  VENDOR = %s\n", buffer);
+  clGetPlatformInfo(platforms_ids[i], CL_PLATFORM_EXTENSIONS, 10240, buffer,
+                    NULL);
+  printf("  EXTENSIONS = %s\n", buffer);
+  }
+  }
+
   printf("DEBUG %d \n", platform_idx);
-	
-	clGetDeviceIDs(platforms_ids[platform_idx], deviceType, 100, device_id, &devices_n);
-	if (VERBOSE)
-	{
-		printf("=== %d OpenCL device(s) found on platform:\n", devices_n);
-		for (int i = 0; i < devices_n; i++)
-		{
-			char buffer[10240];
-			cl_uint buf_uint;
-			cl_ulong buf_ulong;
-			printf("  -- %d --\n", i);
-			clGetDeviceInfo(device_id[i], CL_DEVICE_NAME, sizeof(buffer), buffer,
-			                NULL);
-			printf("  DEVICE_NAME = %s\n", buffer);
-			clGetDeviceInfo(device_id[i], CL_DEVICE_VENDOR, sizeof(buffer), buffer,
-			                NULL);
-			printf("  DEVICE_VENDOR = %s\n", buffer);
-			clGetDeviceInfo(device_id[i], CL_DEVICE_VERSION, sizeof(buffer), buffer,
-			                NULL);
-			printf("  DEVICE_VERSION = %s\n", buffer);
-			clGetDeviceInfo(device_id[i], CL_DRIVER_VERSION, sizeof(buffer), buffer,
-			                NULL);
-			printf("  DRIVER_VERSION = %s\n", buffer);
-			clGetDeviceInfo(device_id[i], CL_DEVICE_MAX_COMPUTE_UNITS,
-			                sizeof(buf_uint), &buf_uint, NULL);
-			printf("  DEVICE_MAX_COMPUTE_UNITS = %u\n", (unsigned int) buf_uint);
-			clGetDeviceInfo(device_id[i], CL_DEVICE_MAX_CLOCK_FREQUENCY,
-			                sizeof(buf_uint), &buf_uint, NULL);
-			printf("  DEVICE_MAX_CLOCK_FREQUENCY = %u\n", (unsigned int) buf_uint);
-			clGetDeviceInfo(device_id[i], CL_DEVICE_GLOBAL_MEM_SIZE,
-			                sizeof(buf_ulong), &buf_ulong, NULL);
-			printf("  DEVICE_GLOBAL_MEM_SIZE = %llu\n",
-			       (unsigned long long) buf_ulong);
-			clGetDeviceInfo(device_id[i], CL_DEVICE_LOCAL_MEM_SIZE,
-			                sizeof(buf_ulong), &buf_ulong, NULL);
-			printf("  CL_DEVICE_LOCAL_MEM_SIZE = %llu\n",
-			       (unsigned long long) buf_ulong);
-		}
-		printf("\n");
-	}
-	
+
+  clGetDeviceIDs(platforms_ids[platform_idx], deviceType, 100, device_id, &devices_n);
+  if (VERBOSE)
+  {
+  printf("=== %d OpenCL device(s) found on platform:\n", devices_n);
+  for (int i = 0; i < devices_n; i++)
+  {
+  char buffer[10240];
+  cl_uint buf_uint;
+  cl_ulong buf_ulong;
+  printf("  -- %d --\n", i);
+  clGetDeviceInfo(device_id[i], CL_DEVICE_NAME, sizeof(buffer), buffer,
+                  NULL);
+  printf("  DEVICE_NAME = %s\n", buffer);
+  clGetDeviceInfo(device_id[i], CL_DEVICE_VENDOR, sizeof(buffer), buffer,
+                  NULL);
+  printf("  DEVICE_VENDOR = %s\n", buffer);
+  clGetDeviceInfo(device_id[i], CL_DEVICE_VERSION, sizeof(buffer), buffer,
+                  NULL);
+  printf("  DEVICE_VERSION = %s\n", buffer);
+  clGetDeviceInfo(device_id[i], CL_DRIVER_VERSION, sizeof(buffer), buffer,
+                  NULL);
+  printf("  DRIVER_VERSION = %s\n", buffer);
+  clGetDeviceInfo(device_id[i], CL_DEVICE_MAX_COMPUTE_UNITS,
+                  sizeof(buf_uint), &buf_uint, NULL);
+  printf("  DEVICE_MAX_COMPUTE_UNITS = %u\n", (unsigned int) buf_uint);
+  clGetDeviceInfo(device_id[i], CL_DEVICE_MAX_CLOCK_FREQUENCY,
+                  sizeof(buf_uint), &buf_uint, NULL);
+  printf("  DEVICE_MAX_CLOCK_FREQUENCY = %u\n", (unsigned int) buf_uint);
+  clGetDeviceInfo(device_id[i], CL_DEVICE_GLOBAL_MEM_SIZE,
+                  sizeof(buf_ulong), &buf_ulong, NULL);
+  printf("  DEVICE_GLOBAL_MEM_SIZE = %llu\n",
+         (unsigned long long) buf_ulong);
+  clGetDeviceInfo(device_id[i], CL_DEVICE_LOCAL_MEM_SIZE,
+                  sizeof(buf_ulong), &buf_ulong, NULL);
+  printf("  CL_DEVICE_LOCAL_MEM_SIZE = %llu\n",
+         (unsigned long long) buf_ulong);
+  }
+  printf("\n");
+  }
+
   cl_context_properties ctxprop[] = {CL_CONTEXT_PLATFORM, (cl_context_properties) platforms_ids[platform_idx], 0};
   context = clCreateContextFromType(ctxprop, deviceType, NULL, NULL, NULL);
   if (!context) {
       printf("ERROR: clCreateContextFromType failed\n");
       exit(1);
   }
- 
-	// Create a command queue.
-	command_queue = clCreateCommandQueue(context, device_id[device_idx], 0, &ret);
-	if (ret != CL_SUCCESS)
-	{
-		printf("\nError at clCreateCommandQueue! Error code %i\n\n", ret);
-		exit(1);
-	}
+
+  // Create a command queue.
+  command_queue = clCreateCommandQueue(context, device_id[device_idx], 0, &ret);
+  if (ret != CL_SUCCESS)
+  {
+  printf("\nError at clCreateCommandQueue! Error code %i\n\n", ret);
+  exit(1);
+  }
 }
 
 void OpenCL::init(int platform_idx, int device_idx, int use_gpu)
 {
-	if (use_gpu)
-		getDevices(platform_idx, device_idx, CL_DEVICE_TYPE_GPU);
-	else
-		getDevices(platform_idx, device_idx, CL_DEVICE_TYPE_CPU);
+  if (use_gpu)
+  getDevices(platform_idx, device_idx, CL_DEVICE_TYPE_GPU);
+  else
+  getDevices(platform_idx, device_idx, CL_DEVICE_TYPE_CPU);
 
-	buildKernel(device_idx);
+  buildKernel(device_idx);
 }

+ 24 - 24
opencl/pathfinder/OpenCL.h

@@ -22,29 +22,29 @@ using namespace std;
 class OpenCL
 {
 public:
-	OpenCL(int displayOutput);
-	~OpenCL();
-	void init(int platform_idx, int device_idx, int use_gpu);
-	void createKernel(string kernelName, int device_idx);
-	cl_kernel kernel(string kernelName);
-	void gwSize(size_t theSize);
-	cl_context ctxt();
-	cl_command_queue q();
-	void launch(string toLaunch);
-	size_t localSize();
-	
+  OpenCL(int displayOutput);
+  ~OpenCL();
+  void init(int platform_idx, int device_idx, int use_gpu);
+  void createKernel(string kernelName, int device_idx);
+  cl_kernel kernel(string kernelName);
+  void gwSize(size_t theSize);
+  cl_context ctxt();
+  cl_command_queue q();
+  void launch(string toLaunch);
+  size_t localSize();
+
 private:
-	int                     VERBOSE;           // Display output text from various functions?
-	size_t                  lwsize;            // Local work size.
-	size_t                  gwsize;            // Global work size.
-	cl_int                  ret;               // Holds the error code returned by cl functions.
-	//cl_platform_id          platform_id[100];
-	cl_device_id            device_id[100];
-	map<string, cl_kernel>  kernelArray;
-	cl_context              context;
-	cl_command_queue        command_queue;
-	cl_program              program;
-	
-	void getDevices(int platform_idx, int device_idx, cl_device_type deviceType);
-	void buildKernel(int device_idx);
+  int                     VERBOSE;           // Display output text from various functions?
+  size_t                  lwsize;            // Local work size.
+  size_t                  gwsize;            // Global work size.
+  cl_int                  ret;               // Holds the error code returned by cl functions.
+  //cl_platform_id          platform_id[100];
+  cl_device_id            device_id[100];
+  map<string, cl_kernel>  kernelArray;
+  cl_context              context;
+  cl_command_queue        command_queue;
+  cl_program              program;
+
+  void getDevices(int platform_idx, int device_idx, cl_device_type deviceType);
+  void buildKernel(int device_idx);
 };

+ 171 - 171
opencl/pathfinder/main.cpp

@@ -40,208 +40,208 @@ int   pyramid_height;
 
 void init(int argc, char** argv, int *platform_idx, int *device_idx, int *use_gpu)
 {
-  
+
   // Rewritten parameters parsing for selecting platform and device and old
   // parameters
-    
+
   // The lengths of the two sequences should be able to divided by 16.
-	// And at current stage  max_rows needs to equal max_cols
+  // And at current stage  max_rows needs to equal max_cols
   int opt;
   extern char *optarg;
-	while ((opt = getopt(argc, argv, "r:c:h:p:d:g:")) != -1 ) {
-		switch(opt){
-			case 'r':
-			   rows = atoi(optarg);
-			   break;
-			case 'c':
-			   cols = atoi(optarg);
-			   break;
+  while ((opt = getopt(argc, argv, "r:c:h:p:d:g:")) != -1 ) {
+  switch(opt){
+  case 'r':
+     rows = atoi(optarg);
+     break;
+  case 'c':
+     cols = atoi(optarg);
+     break;
       case 'h':
-			   pyramid_height = atoi(optarg);
+     pyramid_height = atoi(optarg);
          break;
       case 'p':
-			   *platform_idx = atoi(optarg);
-			   break;
+     *platform_idx = atoi(optarg);
+     break;
       case 'd':
   		   *device_idx = atoi(optarg);
   		   break;
       case 'g':
          *use_gpu = atoi(optarg);
-         break;            
+         break;
       case ':':
-			   fprintf(stderr, "missing argument\n");
-			   break;
+     fprintf(stderr, "missing argument\n");
+     break;
       default:
-			   fprintf(stderr, "Usage: %s -r <row_len> -c <col_len> -h <pyramid_height> -p <platform> -d <device> -g <use_gpu>\n",
+     fprintf(stderr, "Usage: %s -r <row_len> -c <col_len> -h <pyramid_height> -p <platform> -d <device> -g <use_gpu>\n",
                  argv[0]);
-			   exit(EXIT_FAILURE);
-		}
-	}
-  
-	data = new int[rows * cols];
-	wall = new int*[rows];
-	for (int n = 0; n < rows; n++)
-	{
-		// wall[n] is set to be the nth row of the data array.
-		wall[n] = data + cols * n;
-	}
-	result = new int[cols];
-
-	int seed = M_SEED;
-	srand(seed);
-
-	for (int i = 0; i < rows; i++)
-	{
-		for (int j = 0; j < cols; j++)
-		{
-			wall[i][j] = rand() % 10;
-		}
-	}
+     exit(EXIT_FAILURE);
+  }
+  }
+
+  data = new int[rows * cols];
+  wall = new int*[rows];
+  for (int n = 0; n < rows; n++)
+  {
+  // wall[n] is set to be the nth row of the data array.
+  wall[n] = data + cols * n;
+  }
+  result = new int[cols];
+
+  int seed = M_SEED;
+  srand(seed);
+
+  for (int i = 0; i < rows; i++)
+  {
+  for (int j = 0; j < cols; j++)
+  {
+  wall[i][j] = rand() % 10;
+  }
+  }
 #ifdef BENCH_PRINT
-	for (int i = 0; i < rows; i++)
-	{
-		for (int j = 0; j < cols; j++)
-		{
-			printf("%d ", wall[i][j]);
-		}
-		printf("\n");
-	}
+  for (int i = 0; i < rows; i++)
+  {
+  for (int j = 0; j < cols; j++)
+  {
+  printf("%d ", wall[i][j]);
+  }
+  printf("\n");
+  }
 #endif
 }
 
 void fatal(char *s)
 {
-	fprintf(stderr, "error: %s\n", s);
+  fprintf(stderr, "error: %s\n", s);
 }
 
 int main(int argc, char** argv)
 {
-  
+
   // Variables to store information on platform and device to use_gpu
   int platform_idx = 0;
   int device_idx = 0;
   int use_gpu = 0;
-  
-	init(argc, argv, &platform_idx, &device_idx, &use_gpu);
-	
-	// Pyramid parameters.
-	int borderCols = (pyramid_height) * HALO;
-	// int smallBlockCol = ?????? - (pyramid_height) * HALO * 2;
-	// int blockCols = cols / smallBlockCol + ((cols % smallBlockCol == 0) ? 0 : 1);
-
-	
-	/* printf("pyramidHeight: %d\ngridSize: [%d]\nborder:[%d]\nblockSize: %d\nblockGrid:[%d]\ntargetBlock:[%d]\n",
-	   pyramid_height, cols, borderCols, NUMBER_THREADS, blockCols, smallBlockCol); */
-
-	int size = rows * cols;
-
-	// Create and initialize the OpenCL object.
-	OpenCL cl(1);  // 1 means to display output (debugging mode).
-	cl.init(platform_idx, device_idx, use_gpu);    // 1 means to use GPU. 0 means use CPU.
-	cl.gwSize(rows * cols);
-
-	// Create and build the kernel.
-	string kn = "dynproc_kernel";  // the kernel name, for future use.
-	cl.createKernel(kn, device_idx);
-
-	// Allocate device memory.
-	cl_mem d_gpuWall = clCreateBuffer(cl.ctxt(),
-	                                  CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
-	                                  sizeof(cl_int)*(size-cols),
-	                                  (data + cols),
-	                                  NULL);
-
-	cl_mem d_gpuResult[2];
-
-	d_gpuResult[0] = clCreateBuffer(cl.ctxt(),
-	                                CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
-	                                sizeof(cl_int)*cols,
-	                                data,
-	                                NULL);
-
-	d_gpuResult[1] = clCreateBuffer(cl.ctxt(),
-	                                CL_MEM_READ_WRITE,
-	                                sizeof(cl_int)*cols,
-	                                NULL,
-	                                NULL);
-
-	cl_int* h_outputBuffer = (cl_int*)malloc(16384*sizeof(cl_int));
-	for (int i = 0; i < 16384; i++)
-	{
-		h_outputBuffer[i] = 0;
-	}
-	cl_mem d_outputBuffer = clCreateBuffer(cl.ctxt(),
-	                                       CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
-	                                       sizeof(cl_int)*16384,
-	                                       h_outputBuffer,
-	                                       NULL);
-
-	int src = 1, final_ret = 0;
-	for (int t = 0; t < rows - 1; t += pyramid_height)
-	{
-		int temp = src;
-		src = final_ret;
-		final_ret = temp;
-
-		// Calculate this for the kernel argument...
-		int arg0 = MIN(pyramid_height, rows-t-1);
-		int theHalo = HALO;
-
-		// Set the kernel arguments.
-		clSetKernelArg(cl.kernel(kn), 0,  sizeof(cl_int), (void*) &arg0);
-		clSetKernelArg(cl.kernel(kn), 1,  sizeof(cl_mem), (void*) &d_gpuWall);
-		clSetKernelArg(cl.kernel(kn), 2,  sizeof(cl_mem), (void*) &d_gpuResult[src]);
-		clSetKernelArg(cl.kernel(kn), 3,  sizeof(cl_mem), (void*) &d_gpuResult[final_ret]);
-		clSetKernelArg(cl.kernel(kn), 4,  sizeof(cl_int), (void*) &cols);
-		clSetKernelArg(cl.kernel(kn), 5,  sizeof(cl_int), (void*) &rows);
-		clSetKernelArg(cl.kernel(kn), 6,  sizeof(cl_int), (void*) &t);
-		clSetKernelArg(cl.kernel(kn), 7,  sizeof(cl_int), (void*) &borderCols);
-		clSetKernelArg(cl.kernel(kn), 8,  sizeof(cl_int), (void*) &theHalo);
-		clSetKernelArg(cl.kernel(kn), 9,  sizeof(cl_int) * (cl.localSize()), 0);
-		clSetKernelArg(cl.kernel(kn), 10, sizeof(cl_int) * (cl.localSize()), 0);
-		clSetKernelArg(cl.kernel(kn), 11, sizeof(cl_mem), (void*) &d_outputBuffer);
-		cl.launch(kn);
-	}
-
-	// Copy results back to host.
-	clEnqueueReadBuffer(cl.q(),                   // The command queue.
-	                    d_gpuResult[final_ret],   // The result on the device.
-	                    CL_TRUE,                  // Blocking? (ie. Wait at this line until read has finished?)
-	                    0,                        // Offset. None in this case.
-	                    sizeof(cl_int)*cols,      // Size to copy.
-	                    result,                   // The pointer to the memory on the host.
-	                    0,                        // Number of events in wait list. Not used.
-	                    NULL,                     // Event wait list. Not used.
-	                    NULL);                    // Event object for determining status. Not used.
-
-
-	// Copy string buffer used for debugging from device to host.
-	clEnqueueReadBuffer(cl.q(),                   // The command queue.
-	                    d_outputBuffer,           // Debug buffer on the device.
-	                    CL_TRUE,                  // Blocking? (ie. Wait at this line until read has finished?)
-	                    0,                        // Offset. None in this case.
-	                    sizeof(cl_char)*16384,    // Size to copy.
-	                    h_outputBuffer,           // The pointer to the memory on the host.
-	                    0,                        // Number of events in wait list. Not used.
-	                    NULL,                     // Event wait list. Not used.
-	                    NULL);                    // Event object for determining status. Not used.
-	
-	// Tack a null terminator at the end of the string.
-	h_outputBuffer[16383] = '\0';
-	
+
+  init(argc, argv, &platform_idx, &device_idx, &use_gpu);
+
+  // Pyramid parameters.
+  int borderCols = (pyramid_height) * HALO;
+  // int smallBlockCol = ?????? - (pyramid_height) * HALO * 2;
+  // int blockCols = cols / smallBlockCol + ((cols % smallBlockCol == 0) ? 0 : 1);
+
+
+  /* printf("pyramidHeight: %d\ngridSize: [%d]\nborder:[%d]\nblockSize: %d\nblockGrid:[%d]\ntargetBlock:[%d]\n",
+     pyramid_height, cols, borderCols, NUMBER_THREADS, blockCols, smallBlockCol); */
+
+  int size = rows * cols;
+
+  // Create and initialize the OpenCL object.
+  OpenCL cl(1);  // 1 means to display output (debugging mode).
+  cl.init(platform_idx, device_idx, use_gpu);    // 1 means to use GPU. 0 means use CPU.
+  cl.gwSize(rows * cols);
+
+  // Create and build the kernel.
+  string kn = "dynproc_kernel";  // the kernel name, for future use.
+  cl.createKernel(kn, device_idx);
+
+  // Allocate device memory.
+  cl_mem d_gpuWall = clCreateBuffer(cl.ctxt(),
+                                    CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
+                                    sizeof(cl_int)*(size-cols),
+                                    (data + cols),
+                                    NULL);
+
+  cl_mem d_gpuResult[2];
+
+  d_gpuResult[0] = clCreateBuffer(cl.ctxt(),
+                                  CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
+                                  sizeof(cl_int)*cols,
+                                  data,
+                                  NULL);
+
+  d_gpuResult[1] = clCreateBuffer(cl.ctxt(),
+                                  CL_MEM_READ_WRITE,
+                                  sizeof(cl_int)*cols,
+                                  NULL,
+                                  NULL);
+
+  cl_int* h_outputBuffer = (cl_int*)malloc(16384*sizeof(cl_int));
+  for (int i = 0; i < 16384; i++)
+  {
+  h_outputBuffer[i] = 0;
+  }
+  cl_mem d_outputBuffer = clCreateBuffer(cl.ctxt(),
+                                         CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR,
+                                         sizeof(cl_int)*16384,
+                                         h_outputBuffer,
+                                         NULL);
+
+  int src = 1, final_ret = 0;
+  for (int t = 0; t < rows - 1; t += pyramid_height)
+  {
+  int temp = src;
+  src = final_ret;
+  final_ret = temp;
+
+  // Calculate this for the kernel argument...
+  int arg0 = MIN(pyramid_height, rows-t-1);
+  int theHalo = HALO;
+
+  // Set the kernel arguments.
+  clSetKernelArg(cl.kernel(kn), 0,  sizeof(cl_int), (void*) &arg0);
+  clSetKernelArg(cl.kernel(kn), 1,  sizeof(cl_mem), (void*) &d_gpuWall);
+  clSetKernelArg(cl.kernel(kn), 2,  sizeof(cl_mem), (void*) &d_gpuResult[src]);
+  clSetKernelArg(cl.kernel(kn), 3,  sizeof(cl_mem), (void*) &d_gpuResult[final_ret]);
+  clSetKernelArg(cl.kernel(kn), 4,  sizeof(cl_int), (void*) &cols);
+  clSetKernelArg(cl.kernel(kn), 5,  sizeof(cl_int), (void*) &rows);
+  clSetKernelArg(cl.kernel(kn), 6,  sizeof(cl_int), (void*) &t);
+  clSetKernelArg(cl.kernel(kn), 7,  sizeof(cl_int), (void*) &borderCols);
+  clSetKernelArg(cl.kernel(kn), 8,  sizeof(cl_int), (void*) &theHalo);
+  clSetKernelArg(cl.kernel(kn), 9,  sizeof(cl_int) * (cl.localSize()), 0);
+  clSetKernelArg(cl.kernel(kn), 10, sizeof(cl_int) * (cl.localSize()), 0);
+  clSetKernelArg(cl.kernel(kn), 11, sizeof(cl_mem), (void*) &d_outputBuffer);
+  cl.launch(kn);
+  }
+
+  // Copy results back to host.
+  clEnqueueReadBuffer(cl.q(),                   // The command queue.
+                      d_gpuResult[final_ret],   // The result on the device.
+                      CL_TRUE,                  // Blocking? (ie. Wait at this line until read has finished?)
+                      0,                        // Offset. None in this case.
+                      sizeof(cl_int)*cols,      // Size to copy.
+                      result,                   // The pointer to the memory on the host.
+                      0,                        // Number of events in wait list. Not used.
+                      NULL,                     // Event wait list. Not used.
+                      NULL);                    // Event object for determining status. Not used.
+
+
+  // Copy string buffer used for debugging from device to host.
+  clEnqueueReadBuffer(cl.q(),                   // The command queue.
+                      d_outputBuffer,           // Debug buffer on the device.
+                      CL_TRUE,                  // Blocking? (ie. Wait at this line until read has finished?)
+                      0,                        // Offset. None in this case.
+                      sizeof(cl_char)*16384,    // Size to copy.
+                      h_outputBuffer,           // The pointer to the memory on the host.
+                      0,                        // Number of events in wait list. Not used.
+                      NULL,                     // Event wait list. Not used.
+                      NULL);                    // Event object for determining status. Not used.
+
+  // Tack a null terminator at the end of the string.
+  h_outputBuffer[16383] = '\0';
+
 #ifdef BENCH_PRINT
-	for (int i = 0; i < cols; i++)
-		printf("%d ", data[i]);
-	printf("\n");
-	for (int i = 0; i < cols; i++)
-		printf("%d ", result[i]);
-	printf("\n");
+  for (int i = 0; i < cols; i++)
+  printf("%d ", data[i]);
+  printf("\n");
+  for (int i = 0; i < cols; i++)
+  printf("%d ", result[i]);
+  printf("\n");
 #endif
 
-	// Memory cleanup here.
-	delete[] data;
-	delete[] wall;
-	delete[] result;
-	
-	return EXIT_SUCCESS;
+  // Memory cleanup here.
+  delete[] data;
+  delete[] wall;
+  delete[] result;
+
+  return EXIT_SUCCESS;
 }

+ 1122 - 1122
opencl/srad/kernel/kernel_gpu_opencl_wrapper.c

@@ -41,163 +41,163 @@
 //	KERNEL_GPU_CUDA_WRAPPER FUNCTION
 //========================================================================================================================================================================================================200
 
-void 
+void
 kernel_gpu_opencl_wrapper(fp* image,											// input image
-							            int Nr,												// IMAGE nbr of rows
-							            int Nc,												// IMAGE nbr of cols
-							            long Ne,											// IMAGE nbr of elem
-							            int niter,											// nbr of iterations
-							            fp lambda,											// update step size
-							            long NeROI,											// ROI nbr of elements
-							            int* iN,
-							            int* iS,
-							            int* jE,
-							            int* jW,
-							            int iter,											// primary loop
-							            int mem_size_i,
-							            int mem_size_j,
+              int Nr,												// IMAGE nbr of rows
+              int Nc,												// IMAGE nbr of cols
+              long Ne,											// IMAGE nbr of elem
+              int niter,											// nbr of iterations
+              fp lambda,											// update step size
+              long NeROI,											// ROI nbr of elements
+              int* iN,
+              int* iS,
+              int* jE,
+              int* jW,
+              int iter,											// primary loop
+              int mem_size_i,
+              int mem_size_j,
                           int platform_idx,
                           int device_idx,
                           int use_gpu)
 {
 
-	//======================================================================================================================================================150
-	//	GPU SETUP
-	//======================================================================================================================================================150
-
-	//====================================================================================================100
-	//	COMMON VARIABLES
-	//====================================================================================================100
-
-	// common variables
-	cl_int error;
-
-	//====================================================================================================100
-	//	GET PLATFORMS (Intel, AMD, NVIDIA, based on provided library), SELECT ONE
-	//====================================================================================================100
-
-	// Get the number of available platforms
-	cl_uint num_platforms;
-	error = clGetPlatformIDs(	0, 
-								NULL, 
-								&num_platforms);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// Get the list of available platforms
-	cl_platform_id *platforms = (cl_platform_id *)malloc(sizeof(cl_platform_id) * num_platforms);
-	error = clGetPlatformIDs(	num_platforms, 
-								platforms, 
-								NULL);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// Select the platform using the parameter
-	cl_platform_id platform = platforms[platform_idx];
-
-	// Get the name of the selected platform and print it (if there are multiple platforms, choose the first one)
-	char pbuf[100];
-	error = clGetPlatformInfo(	platform, 
-								CL_PLATFORM_VENDOR, 
-								sizeof(pbuf), 
-								pbuf, 
-								NULL);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	printf("Platform: %s\n", pbuf);
-
-	//====================================================================================================100
-	//	CREATE CONTEXT FOR THE PLATFORM
-	//====================================================================================================100
-
-	// Create context properties for selected platform
-	cl_context_properties context_properties[3] = {	CL_CONTEXT_PLATFORM, 
-													(cl_context_properties) platform, 
-													0};
+  //======================================================================================================================================================150
+  //	GPU SETUP
+  //======================================================================================================================================================150
+
+  //====================================================================================================100
+  //	COMMON VARIABLES
+  //====================================================================================================100
+
+  // common variables
+  cl_int error;
+
+  //====================================================================================================100
+  //	GET PLATFORMS (Intel, AMD, NVIDIA, based on provided library), SELECT ONE
+  //====================================================================================================100
+
+  // Get the number of available platforms
+  cl_uint num_platforms;
+  error = clGetPlatformIDs(	0,
+  NULL,
+  &num_platforms);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // Get the list of available platforms
+  cl_platform_id *platforms = (cl_platform_id *)malloc(sizeof(cl_platform_id) * num_platforms);
+  error = clGetPlatformIDs(	num_platforms,
+  platforms,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // Select the platform using the parameter
+  cl_platform_id platform = platforms[platform_idx];
+
+  // Get the name of the selected platform and print it (if there are multiple platforms, choose the first one)
+  char pbuf[100];
+  error = clGetPlatformInfo(	platform,
+  CL_PLATFORM_VENDOR,
+  sizeof(pbuf),
+  pbuf,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  printf("Platform: %s\n", pbuf);
+
+  //====================================================================================================100
+  //	CREATE CONTEXT FOR THE PLATFORM
+  //====================================================================================================100
+
+  // Create context properties for selected platform
+  cl_context_properties context_properties[3] = {	CL_CONTEXT_PLATFORM,
+  (cl_context_properties) platform,
+  0};
 
   // Selector for the device type in accordance to what passed as parameter
   cl_device_type device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
-  
-	// Create context for selected platform being GPU/CPU
-	cl_context context;
-	context = clCreateContextFromType(	context_properties, 
-										device_type, 
-										NULL, 
-										NULL, 
-										&error);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	//	GET DEVICES AVAILABLE FOR THE CONTEXT, SELECT ONE
-	//====================================================================================================100
-
-	// Get the number of devices (previousely selected for the context)
-	size_t devices_size;
-	error = clGetContextInfo(	context, 
-								CL_CONTEXT_DEVICES, 
-								0, 
-								NULL, 
-								&devices_size);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// Get the list of devices (previousely selected for the context)
-	cl_device_id *devices = (cl_device_id *) malloc(devices_size);
-	error = clGetContextInfo(	context, 
-								CL_CONTEXT_DEVICES, 
-								devices_size, 
-								devices, 
-								NULL);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// Select the device in accordance to the paramter
-	cl_device_id device;
-	device = devices[device_idx];
-
-	// Get the name of the selected device (previousely selected for the context) and print it
-	error = clGetDeviceInfo(device, 
-							CL_DEVICE_NAME, 
-							sizeof(pbuf), 
-							pbuf, 
-							NULL);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	printf("Device: %s\n", pbuf);
-
-	//====================================================================================================100
-	//	CREATE COMMAND QUEUE FOR THE DEVICE
-	//====================================================================================================100
-
-	// Create a command queue
-	cl_command_queue command_queue;
-	command_queue = clCreateCommandQueue(	context, 
-											device, 
-											0, 
-											&error);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	//	CREATE PROGRAM, COMPILE IT
-	//====================================================================================================100
-
-	// Load kernel source code from file
-	const char *source = load_kernel_source("./kernel/kernel_gpu_opencl.cl");
-	size_t sourceSize = strlen(source);
-
-	// Create the program
-	cl_program program = clCreateProgramWithSource(	context, 
-													1, 
-													&source, 
-													&sourceSize, 
-													&error);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
+
+  // Create context for selected platform being GPU/CPU
+  cl_context context;
+  context = clCreateContextFromType(	context_properties,
+  device_type,
+  NULL,
+  NULL,
+  &error);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	GET DEVICES AVAILABLE FOR THE CONTEXT, SELECT ONE
+  //====================================================================================================100
+
+  // Get the number of devices (previousely selected for the context)
+  size_t devices_size;
+  error = clGetContextInfo(	context,
+  CL_CONTEXT_DEVICES,
+  0,
+  NULL,
+  &devices_size);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // Get the list of devices (previousely selected for the context)
+  cl_device_id *devices = (cl_device_id *) malloc(devices_size);
+  error = clGetContextInfo(	context,
+  CL_CONTEXT_DEVICES,
+  devices_size,
+  devices,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // Select the device in accordance to the paramter
+  cl_device_id device;
+  device = devices[device_idx];
+
+  // Get the name of the selected device (previousely selected for the context) and print it
+  error = clGetDeviceInfo(device,
+  CL_DEVICE_NAME,
+  sizeof(pbuf),
+  pbuf,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  printf("Device: %s\n", pbuf);
+
+  //====================================================================================================100
+  //	CREATE COMMAND QUEUE FOR THE DEVICE
+  //====================================================================================================100
+
+  // Create a command queue
+  cl_command_queue command_queue;
+  command_queue = clCreateCommandQueue(	context,
+  device,
+  0,
+  &error);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	CREATE PROGRAM, COMPILE IT
+  //====================================================================================================100
+
+  // Load kernel source code from file
+  const char *source = load_kernel_source("./kernel/kernel_gpu_opencl.cl");
+  size_t sourceSize = strlen(source);
+
+  // Create the program
+  cl_program program = clCreateProgramWithSource(	context,
+  1,
+  &source,
+  &sourceSize,
+  &error);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
 
   char clOptions[150];
-//  sprintf(clOptions,"-I../../src");                                                    
+//  sprintf(clOptions,"-I../../src");
   sprintf(clOptions,"-I.");
 #ifdef RD_WG_SIZE
   sprintf(clOptions + strlen(clOptions), " -DRD_WG_SIZE=%d", RD_WG_SIZE);
@@ -209,982 +209,982 @@ kernel_gpu_opencl_wrapper(fp* image,											// input image
   sprintf(clOptions + strlen(clOptions), " -DRD_WG_SIZE_0_0=%d", RD_WG_SIZE_0_0);
 #endif
 
-	// Compile the program
-	error = clBuildProgram(	program, 
-							1, 
-							&device, 
-							clOptions, 
-							NULL, 
-							NULL);
-	// Print warnings and errors from compilation
-	static char log[65536]; 
-	memset(log, 0, sizeof(log));
-	clGetProgramBuildInfo(	program, 
-							device, 
-							CL_PROGRAM_BUILD_LOG, 
-							sizeof(log)-1, 
-							log, 
-							NULL);
-	printf("-----OpenCL Compiler Output-----\n");
-	if (strstr(log,"warning:") || strstr(log, "error:")) 
-		printf("<<<<\n%s\n>>>>\n", log);
-	printf("--------------------------------\n");
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	//	CREATE Kernels
-	//====================================================================================================100
-
-	// Extract kernel
-	cl_kernel extract_kernel;
-	extract_kernel = clCreateKernel(program, 
-									"extract_kernel", 
-									&error);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// Prepare kernel
-	cl_kernel prepare_kernel;
-	prepare_kernel = clCreateKernel(program, 
-									"prepare_kernel", 
-									&error);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// Reduce kernel
-	cl_kernel reduce_kernel;
-	reduce_kernel = clCreateKernel(	program, 
-									"reduce_kernel", 
-									&error);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// SRAD kernel
-	cl_kernel srad_kernel;
-	srad_kernel = clCreateKernel(	program, 
-									"srad_kernel", 
-									&error);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// SRAD2 kernel
-	cl_kernel srad2_kernel;
-	srad2_kernel = clCreateKernel(	program, 
-									"srad2_kernel", 
-									&error);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// Compress kernel
-	cl_kernel compress_kernel;
-	compress_kernel = clCreateKernel(	program, 
-										"compress_kernel", 
-										&error);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	//	TRIGGERING INITIAL DRIVER OVERHEAD
-	//====================================================================================================100
-
-	// cudaThreadSynchronize();		// the above does it
-
-	//======================================================================================================================================================150
-	// 	GPU VARIABLES
-	//======================================================================================================================================================150
-
-	// CUDA kernel execution parameters
-	int blocks_x;
-
-	//======================================================================================================================================================150
-	// 	ALLOCATE MEMORY IN GPU
-	//======================================================================================================================================================150
-
-	//====================================================================================================100
-	// common memory size
-	//====================================================================================================100
-
-	int mem_size;															// matrix memory size
-	mem_size = sizeof(fp) * Ne;												// get the size of float representation of input IMAGE
-
-	//====================================================================================================100
-	// allocate memory for entire IMAGE on DEVICE
-	//====================================================================================================100
-
-	cl_mem d_I;
-	d_I = clCreateBuffer(	context, 
-							CL_MEM_READ_WRITE, 
-							mem_size,
-							NULL, 
-							&error );
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	// allocate memory for coordinates on DEVICE
-	//====================================================================================================100
-
-	cl_mem d_iN;
-	d_iN = clCreateBuffer(	context, 
-							CL_MEM_READ_WRITE, 
-							mem_size_i,
-							NULL, 
-							&error );
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	cl_mem d_iS;
-	d_iS = clCreateBuffer(	context, 
-							CL_MEM_READ_WRITE, 
-							mem_size_i,
-							NULL, 
-							&error );
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	cl_mem d_jE;
-	d_jE = clCreateBuffer(	context, 
-							CL_MEM_READ_WRITE, 
-							mem_size_j,
-							NULL, 
-							&error );
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	cl_mem d_jW;
-	d_jW = clCreateBuffer(	context, 
-							CL_MEM_READ_WRITE, 
-							mem_size_j,
-							NULL, 
-							&error );
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	// allocate memory for derivatives
-	//====================================================================================================100
-
-	cl_mem d_dN;
-	d_dN = clCreateBuffer(	context, 
-							CL_MEM_READ_WRITE, 
-							mem_size,
-							NULL, 
-							&error );
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	cl_mem d_dS;
-	d_dS = clCreateBuffer(	context, 
-							CL_MEM_READ_WRITE, 
-							mem_size,
-							NULL, 
-							&error );
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	cl_mem d_dW;
-	d_dW = clCreateBuffer(	context, 
-							CL_MEM_READ_WRITE, 
-							mem_size,
-							NULL, 
-							&error );
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	cl_mem d_dE;
-	d_dE = clCreateBuffer(	context, 
-							CL_MEM_READ_WRITE, 
-							mem_size,
-							NULL, 
-							&error );
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	// allocate memory for coefficient on DEVICE
-	//====================================================================================================100
-
-	cl_mem d_c;
-	d_c = clCreateBuffer(	context, 
-							CL_MEM_READ_WRITE, 
-							mem_size,
-							NULL, 
-							&error );
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	// allocate memory for partial sums on DEVICE
-	//====================================================================================================100
-
-	cl_mem d_sums;
-	d_sums = clCreateBuffer(	context, 
-							CL_MEM_READ_WRITE, 
-							mem_size,
-							NULL, 
-							&error );
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	cl_mem d_sums2;
-	d_sums2 = clCreateBuffer(	context, 
-							CL_MEM_READ_WRITE, 
-							mem_size,
-							NULL, 
-							&error );
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	// End
-	//====================================================================================================100
-
-	//======================================================================================================================================================150
-	// 	COPY INPUT TO CPU
-	//======================================================================================================================================================150
-
-	//====================================================================================================100
-	// Image
-	//====================================================================================================100
-
-	error = clEnqueueWriteBuffer(	command_queue, 
-									d_I, 
-									1, 
-									0, 
-									mem_size, 
-									image, 
-									0, 
-									0, 
-									0);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	// coordinates
-	//====================================================================================================100
-
-	error = clEnqueueWriteBuffer(	command_queue, 
-									d_iN, 
-									1, 
-									0, 
-									mem_size_i, 
-									iN, 
-									0, 
-									0, 
-									0);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	error = clEnqueueWriteBuffer(	command_queue, 
-									d_iS, 
-									1, 
-									0, 
-									mem_size_i, 
-									iS, 
-									0, 
-									0, 
-									0);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	error = clEnqueueWriteBuffer(	command_queue, 
-									d_jE, 
-									1, 
-									0, 
-									mem_size_j, 
-									jE, 
-									0, 
-									0, 
-									0);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	error = clEnqueueWriteBuffer(	command_queue, 
-									d_jW, 
-									1, 
-									0, 
-									mem_size_j, 
-									jW, 
-									0, 
-									0, 
-									0);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	// End
-	//====================================================================================================100
-
-	//======================================================================================================================================================150
-	// 	KERNEL EXECUTION PARAMETERS
-	//======================================================================================================================================================150
-
-	// threads
-	size_t local_work_size[1];
-	local_work_size[0] = NUMBER_THREADS;
-
-	// workgroups
-	int blocks_work_size;
-	size_t global_work_size[1];
-	blocks_x = Ne/(int)local_work_size[0];
-	if (Ne % (int)local_work_size[0] != 0){												// compensate for division remainder above by adding one grid
-		blocks_x = blocks_x + 1;																	
-	}
-	blocks_work_size = blocks_x;
-	global_work_size[0] = blocks_work_size * local_work_size[0];						// define the number of blocks in the grid
-
-	printf("max # of workgroups = %d, # of threads/workgroup = %d (ensure that device can handle)\n", (int)(global_work_size[0]/local_work_size[0]), (int)local_work_size[0]);
-
-	//======================================================================================================================================================150
-	// 	Extract Kernel - SCALE IMAGE DOWN FROM 0-255 TO 0-1 AND EXTRACT
-	//======================================================================================================================================================150
-
-	//====================================================================================================100
-	//	set arguments
-	//====================================================================================================100
-
-	error = clSetKernelArg(	extract_kernel, 
-							0, 
-							sizeof(long)*2, 
-							(void *) &Ne);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	extract_kernel, 
-							1, 
-							sizeof(cl_mem), 
-							(void *) &d_I);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	//	launch kernel
-	//====================================================================================================100
-
-	error = clEnqueueNDRangeKernel(	command_queue, 
-									extract_kernel, 
-									1, 
-									NULL, 
-									global_work_size, 
-									local_work_size, 
-									0, 
-									NULL, 
-									NULL);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	//	Synchronization - wait for all operations in the command queue so far to finish
-	//====================================================================================================100
-
-	// error = clFinish(command_queue);
-	// if (error != CL_SUCCESS) 
-		// fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	//	End
-	//====================================================================================================100
-
-	//======================================================================================================================================================150
-	// 	WHAT IS CONSTANT IN COMPUTATION LOOP
-	//======================================================================================================================================================150
-
-	//====================================================================================================100
-	//	Prepare Kernel
-	//====================================================================================================100
-
-	error = clSetKernelArg(	prepare_kernel, 
-							0, 
-							sizeof(long)*2, 
-							(void *) &Ne);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	prepare_kernel, 
-							1, 
-							sizeof(cl_mem), 
-							(void *) &d_I);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	prepare_kernel, 
-							2, 
-							sizeof(cl_mem), 
-							(void *) &d_sums);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	prepare_kernel, 
-							3, 
-							sizeof(cl_mem), 
-							(void *) &d_sums2);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	//	Reduce Kernel
-	//====================================================================================================100
-
-	int blocks2_x;
-	int blocks2_work_size;
-	size_t global_work_size2[1];
-	long no;
-	int mul;
-	int mem_size_single = sizeof(fp) * 1;
-	fp total;
-	fp total2;
-	fp meanROI;
-	fp meanROI2;
-	fp varROI;
-	fp q0sqr;
-
-	error = clSetKernelArg(	reduce_kernel, 
-							0, 
-							sizeof(long)*2, 
-							(void *) &Ne);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	reduce_kernel, 
-							3, 
-							sizeof(cl_mem), 
-							(void *) &d_sums);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	reduce_kernel, 
-							4, 
-							sizeof(cl_mem), 
-							(void *) &d_sums2);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	//	SRAD Kernel
-	//====================================================================================================100
-
-	error = clSetKernelArg(	srad_kernel, 
-							0, 
-							sizeof(fp), 
-							(void *) &lambda);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad_kernel, 
-							1, 
-							sizeof(int), 
-							(void *) &Nr);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad_kernel, 
-							2, 
-							sizeof(int), 
-							(void *) &Nc);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad_kernel, 
-							3, 
-							sizeof(long)*2, 
-							(void *) &Ne);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad_kernel, 
-							4, 
-							sizeof(cl_mem), 
-							(void *) &d_iN);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad_kernel, 
-							5, 
-							sizeof(cl_mem), 
-							(void *) &d_iS);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad_kernel, 
-							6, 
-							sizeof(cl_mem), 
-							(void *) &d_jE);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad_kernel, 
-							7, 
-							sizeof(cl_mem), 
-							(void *) &d_jW);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad_kernel, 
-							8, 
-							sizeof(cl_mem), 
-							(void *) &d_dN);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad_kernel, 
-							9, 
-							sizeof(cl_mem), 
-							(void *) &d_dS);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad_kernel, 
-							10, 
-							sizeof(cl_mem), 
-							(void *) &d_dW);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad_kernel, 
-							11, 
-							sizeof(cl_mem), 
-							(void *) &d_dE);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad_kernel, 
-							13, 
-							sizeof(cl_mem), 
-							(void *) &d_c);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad_kernel, 
-							14, 
-							sizeof(cl_mem), 
-							(void *) &d_I);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	//	SRAD2 Kernel
-	//====================================================================================================100
-
-	error = clSetKernelArg(	srad2_kernel, 
-							0, 
-							sizeof(fp), 
-							(void *) &lambda);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad2_kernel, 
-							1, 
-							sizeof(int), 
-							(void *) &Nr);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad2_kernel, 
-							2, 
-							sizeof(int), 
-							(void *) &Nc);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad2_kernel, 
-							3, 
-							sizeof(long)*2, 
-							(void *) &Ne);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad2_kernel, 
-							4, 
-							sizeof(cl_mem), 
-							(void *) &d_iN);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad2_kernel, 
-							5, 
-							sizeof(cl_mem), 
-							(void *) &d_iS);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad2_kernel, 
-							6, 
-							sizeof(cl_mem), 
-							(void *) &d_jE);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad2_kernel, 
-							7, 
-							sizeof(cl_mem), 
-							(void *) &d_jW);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad2_kernel, 
-							8, 
-							sizeof(cl_mem), 
-							(void *) &d_dN);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad2_kernel, 
-							9, 
-							sizeof(cl_mem), 
-							(void *) &d_dS);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad2_kernel, 
-							10, 
-							sizeof(cl_mem), 
-							(void *) &d_dW);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad2_kernel, 
-							11, 
-							sizeof(cl_mem), 
-							(void *) &d_dE);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad2_kernel, 
-							12, 
-							sizeof(cl_mem), 
-							(void *) &d_c);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	srad2_kernel, 
-							13, 
-							sizeof(cl_mem), 
-							(void *) &d_I);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	//	End
-	//====================================================================================================100
-
-	//======================================================================================================================================================150
-	// 	COMPUTATION
-	//======================================================================================================================================================150
-
-	printf("Iterations Progress: ");
-
-	// execute main loop
-	for (iter=0; iter<niter; iter++){										// do for the number of iterations input parameter
-
-		printf("%d ", iter);
-		fflush(NULL);
-
-		//====================================================================================================100
-		// Prepare kernel
-		//====================================================================================================100
-
-		// launch kernel
-		error = clEnqueueNDRangeKernel(	command_queue, 
-										prepare_kernel, 
-										1, 
-										NULL, 
-										global_work_size, 
-										local_work_size, 
-										0, 
-										NULL, 
-										NULL);
-		if (error != CL_SUCCESS) 
-			fatal_CL(error, __LINE__);
-
-		// synchronize
-		// error = clFinish(command_queue);
-		// if (error != CL_SUCCESS) 
-			// fatal_CL(error, __LINE__);
-
-		//====================================================================================================100
-		//	Reduce Kernel - performs subsequent reductions of sums
-		//====================================================================================================100
-
-		// initial values
-		blocks2_work_size = blocks_work_size;							// original number of blocks
-		global_work_size2[0] = global_work_size[0];
-		no = Ne;														// original number of sum elements
-		mul = 1;														// original multiplier
-
-		// loop
-		while(blocks2_work_size != 0){
-
-			// set arguments that were uptaded in this loop
-			error = clSetKernelArg(	reduce_kernel, 
-									1, 
-									sizeof(long)*2, 
-									(void *) &no);
-			if (error != CL_SUCCESS) 
-				fatal_CL(error, __LINE__);
-			error = clSetKernelArg(	reduce_kernel, 
-									2, 
-									sizeof(int), 
-									(void *) &mul);
-			if (error != CL_SUCCESS) 
-				fatal_CL(error, __LINE__);
-
-			error = clSetKernelArg(	reduce_kernel, 
-									5, 
-									sizeof(int), 
-									(void *) &blocks2_work_size);
-			if (error != CL_SUCCESS) 
-				fatal_CL(error, __LINE__);
-
-			// launch kernel
-			error = clEnqueueNDRangeKernel(	command_queue, 
-											reduce_kernel, 
-											1, 
-											NULL, 
-											global_work_size2, 
-											local_work_size, 
-											0, 
-											NULL, 
-											NULL);
-			if (error != CL_SUCCESS) 
-				fatal_CL(error, __LINE__);
-
-			// synchronize
-			// error = clFinish(command_queue);
-			// if (error != CL_SUCCESS) 
-				// fatal_CL(error, __LINE__);
-
-			// update execution parameters
-			no = blocks2_work_size;												// get current number of elements
-			if(blocks2_work_size == 1){
-				blocks2_work_size = 0;
-			}
-			else{
-				mul = mul * NUMBER_THREADS;										// update the increment
-				blocks_x = blocks2_work_size/(int)local_work_size[0];			// number of blocks
-				if (blocks2_work_size % (int)local_work_size[0] != 0){			// compensate for division remainder above by adding one grid
-					blocks_x = blocks_x + 1;
-				}
-				blocks2_work_size = blocks_x;
-				global_work_size2[0] = blocks2_work_size * (int)local_work_size[0];
-			}
-
-		}
-
-		// copy total sums to device
-		error = clEnqueueReadBuffer(command_queue,
-									d_sums,
-									CL_TRUE,
-									0,
-									mem_size_single,
-									&total,
-									0,
-									NULL,
-									NULL);
-		if (error != CL_SUCCESS) 
-			fatal_CL(error, __LINE__);
-
-		error = clEnqueueReadBuffer(command_queue,
-									d_sums2,
-									CL_TRUE,
-									0,
-									mem_size_single,
-									&total2,
-									0,
-									NULL,
-									NULL);
-		if (error != CL_SUCCESS) 
-			fatal_CL(error, __LINE__);
-
-		//====================================================================================================100
-		// calculate statistics
-		//====================================================================================================100
-		
-		meanROI	= total / (fp)(NeROI);										// gets mean (average) value of element in ROI
-		meanROI2 = meanROI * meanROI;										//
-		varROI = (total2 / (fp)(NeROI)) - meanROI2;							// gets variance of ROI								
-		q0sqr = varROI / meanROI2;											// gets standard deviation of ROI
-
-		//====================================================================================================100
-		// execute srad kernel
-		//====================================================================================================100
-
-		// set arguments that were uptaded in this loop
-		error = clSetKernelArg(	srad_kernel, 
-							12, 
-							sizeof(fp), 
-							(void *) &q0sqr);
-		if (error != CL_SUCCESS) 
-			fatal_CL(error, __LINE__);
-
-		// launch kernel
-		error = clEnqueueNDRangeKernel(	command_queue, 
-										srad_kernel, 
-										1, 
-										NULL, 
-										global_work_size, 
-										local_work_size, 
-										0, 
-										NULL, 
-										NULL);
-		if (error != CL_SUCCESS) 
-			fatal_CL(error, __LINE__);
-
-		// synchronize
-		// error = clFinish(command_queue);
-		// if (error != CL_SUCCESS) 
-			// fatal_CL(error, __LINE__);
-
-		//====================================================================================================100
-		// execute srad2 kernel
-		//====================================================================================================100
-
-		// launch kernel
-		error = clEnqueueNDRangeKernel(	command_queue, 
-										srad2_kernel, 
-										1, 
-										NULL, 
-										global_work_size, 
-										local_work_size, 
-										0, 
-										NULL, 
-										NULL);
-		if (error != CL_SUCCESS) 
-			fatal_CL(error, __LINE__);
-
-		// synchronize
-		// error = clFinish(command_queue);
-		// if (error != CL_SUCCESS) 
-			// fatal_CL(error, __LINE__);
-
-		//====================================================================================================100
-		// End
-		//====================================================================================================100
-
-	}
-
-	printf("\n");
-
-	//======================================================================================================================================================150
-	// 	Compress Kernel - SCALE IMAGE UP FROM 0-1 TO 0-255 AND COMPRESS
-	//======================================================================================================================================================150
-
-	//====================================================================================================100
-	// set parameters
-	//====================================================================================================100
-
-	error = clSetKernelArg(	compress_kernel, 
-							0, 
-							sizeof(long)*2, 
-							(void *) &Ne);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clSetKernelArg(	compress_kernel, 
-							1, 
-							sizeof(cl_mem), 
-							(void *) &d_I);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	// launch kernel
-	//====================================================================================================100
-
-	error = clEnqueueNDRangeKernel(	command_queue, 
-									compress_kernel, 
-									1, 
-									NULL, 
-									global_work_size, 
-									local_work_size, 
-									0, 
-									NULL, 
-									NULL);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	// synchronize
-	//====================================================================================================100
-
-	error = clFinish(command_queue);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//====================================================================================================100
-	//	End
-	//====================================================================================================100
-
-	//======================================================================================================================================================150
-	// 	COPY RESULTS BACK TO CPU
-	//======================================================================================================================================================150
-
-	error = clEnqueueReadBuffer(command_queue,
-								d_I,
-								CL_TRUE,
-								0,
-								mem_size,
-								image,
-								0,
-								NULL,
-								NULL);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// int i;
-	// for(i=0; i<100; i++){
-		// printf("%f ", image[i]);
-	// }
-
-	//======================================================================================================================================================150
-	// 	FREE MEMORY
-	//======================================================================================================================================================150
-
-	// OpenCL structures
-	error = clReleaseKernel(extract_kernel);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clReleaseKernel(prepare_kernel);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clReleaseKernel(reduce_kernel);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clReleaseKernel(srad_kernel);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clReleaseKernel(srad2_kernel);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clReleaseKernel(compress_kernel);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clReleaseProgram(program);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// common_change
-	error = clReleaseMemObject(d_I);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clReleaseMemObject(d_c);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	error = clReleaseMemObject(d_iN);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clReleaseMemObject(d_iS);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clReleaseMemObject(d_jE);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clReleaseMemObject(d_jW);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	error = clReleaseMemObject(d_dN);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clReleaseMemObject(d_dS);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clReleaseMemObject(d_dE);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clReleaseMemObject(d_dW);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	error = clReleaseMemObject(d_sums);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clReleaseMemObject(d_sums2);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	// OpenCL structures
-	error = clFlush(command_queue);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clReleaseCommandQueue(command_queue);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-	error = clReleaseContext(context);
-	if (error != CL_SUCCESS) 
-		fatal_CL(error, __LINE__);
-
-	//======================================================================================================================================================150
-	// 	End
-	//======================================================================================================================================================150
+  // Compile the program
+  error = clBuildProgram(	program,
+  1,
+  &device,
+  clOptions,
+  NULL,
+  NULL);
+  // Print warnings and errors from compilation
+  static char log[65536];
+  memset(log, 0, sizeof(log));
+  clGetProgramBuildInfo(	program,
+  device,
+  CL_PROGRAM_BUILD_LOG,
+  sizeof(log)-1,
+  log,
+  NULL);
+  printf("-----OpenCL Compiler Output-----\n");
+  if (strstr(log,"warning:") || strstr(log, "error:"))
+  printf("<<<<\n%s\n>>>>\n", log);
+  printf("--------------------------------\n");
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	CREATE Kernels
+  //====================================================================================================100
+
+  // Extract kernel
+  cl_kernel extract_kernel;
+  extract_kernel = clCreateKernel(program,
+  "extract_kernel",
+  &error);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // Prepare kernel
+  cl_kernel prepare_kernel;
+  prepare_kernel = clCreateKernel(program,
+  "prepare_kernel",
+  &error);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // Reduce kernel
+  cl_kernel reduce_kernel;
+  reduce_kernel = clCreateKernel(	program,
+  "reduce_kernel",
+  &error);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // SRAD kernel
+  cl_kernel srad_kernel;
+  srad_kernel = clCreateKernel(	program,
+  "srad_kernel",
+  &error);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // SRAD2 kernel
+  cl_kernel srad2_kernel;
+  srad2_kernel = clCreateKernel(	program,
+  "srad2_kernel",
+  &error);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // Compress kernel
+  cl_kernel compress_kernel;
+  compress_kernel = clCreateKernel(	program,
+  "compress_kernel",
+  &error);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	TRIGGERING INITIAL DRIVER OVERHEAD
+  //====================================================================================================100
+
+  // cudaThreadSynchronize();		// the above does it
+
+  //======================================================================================================================================================150
+  // 	GPU VARIABLES
+  //======================================================================================================================================================150
+
+  // CUDA kernel execution parameters
+  int blocks_x;
+
+  //======================================================================================================================================================150
+  // 	ALLOCATE MEMORY IN GPU
+  //======================================================================================================================================================150
+
+  //====================================================================================================100
+  // common memory size
+  //====================================================================================================100
+
+  int mem_size;															// matrix memory size
+  mem_size = sizeof(fp) * Ne;												// get the size of float representation of input IMAGE
+
+  //====================================================================================================100
+  // allocate memory for entire IMAGE on DEVICE
+  //====================================================================================================100
+
+  cl_mem d_I;
+  d_I = clCreateBuffer(	context,
+  CL_MEM_READ_WRITE,
+  mem_size,
+  NULL,
+  &error );
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  // allocate memory for coordinates on DEVICE
+  //====================================================================================================100
+
+  cl_mem d_iN;
+  d_iN = clCreateBuffer(	context,
+  CL_MEM_READ_WRITE,
+  mem_size_i,
+  NULL,
+  &error );
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  cl_mem d_iS;
+  d_iS = clCreateBuffer(	context,
+  CL_MEM_READ_WRITE,
+  mem_size_i,
+  NULL,
+  &error );
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  cl_mem d_jE;
+  d_jE = clCreateBuffer(	context,
+  CL_MEM_READ_WRITE,
+  mem_size_j,
+  NULL,
+  &error );
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  cl_mem d_jW;
+  d_jW = clCreateBuffer(	context,
+  CL_MEM_READ_WRITE,
+  mem_size_j,
+  NULL,
+  &error );
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  // allocate memory for derivatives
+  //====================================================================================================100
+
+  cl_mem d_dN;
+  d_dN = clCreateBuffer(	context,
+  CL_MEM_READ_WRITE,
+  mem_size,
+  NULL,
+  &error );
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  cl_mem d_dS;
+  d_dS = clCreateBuffer(	context,
+  CL_MEM_READ_WRITE,
+  mem_size,
+  NULL,
+  &error );
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  cl_mem d_dW;
+  d_dW = clCreateBuffer(	context,
+  CL_MEM_READ_WRITE,
+  mem_size,
+  NULL,
+  &error );
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  cl_mem d_dE;
+  d_dE = clCreateBuffer(	context,
+  CL_MEM_READ_WRITE,
+  mem_size,
+  NULL,
+  &error );
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  // allocate memory for coefficient on DEVICE
+  //====================================================================================================100
+
+  cl_mem d_c;
+  d_c = clCreateBuffer(	context,
+  CL_MEM_READ_WRITE,
+  mem_size,
+  NULL,
+  &error );
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  // allocate memory for partial sums on DEVICE
+  //====================================================================================================100
+
+  cl_mem d_sums;
+  d_sums = clCreateBuffer(	context,
+  CL_MEM_READ_WRITE,
+  mem_size,
+  NULL,
+  &error );
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  cl_mem d_sums2;
+  d_sums2 = clCreateBuffer(	context,
+  CL_MEM_READ_WRITE,
+  mem_size,
+  NULL,
+  &error );
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  // End
+  //====================================================================================================100
+
+  //======================================================================================================================================================150
+  // 	COPY INPUT TO CPU
+  //======================================================================================================================================================150
+
+  //====================================================================================================100
+  // Image
+  //====================================================================================================100
+
+  error = clEnqueueWriteBuffer(	command_queue,
+  d_I,
+  1,
+  0,
+  mem_size,
+  image,
+  0,
+  0,
+  0);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  // coordinates
+  //====================================================================================================100
+
+  error = clEnqueueWriteBuffer(	command_queue,
+  d_iN,
+  1,
+  0,
+  mem_size_i,
+  iN,
+  0,
+  0,
+  0);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  error = clEnqueueWriteBuffer(	command_queue,
+  d_iS,
+  1,
+  0,
+  mem_size_i,
+  iS,
+  0,
+  0,
+  0);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  error = clEnqueueWriteBuffer(	command_queue,
+  d_jE,
+  1,
+  0,
+  mem_size_j,
+  jE,
+  0,
+  0,
+  0);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  error = clEnqueueWriteBuffer(	command_queue,
+  d_jW,
+  1,
+  0,
+  mem_size_j,
+  jW,
+  0,
+  0,
+  0);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  // End
+  //====================================================================================================100
+
+  //======================================================================================================================================================150
+  // 	KERNEL EXECUTION PARAMETERS
+  //======================================================================================================================================================150
+
+  // threads
+  size_t local_work_size[1];
+  local_work_size[0] = NUMBER_THREADS;
+
+  // workgroups
+  int blocks_work_size;
+  size_t global_work_size[1];
+  blocks_x = Ne/(int)local_work_size[0];
+  if (Ne % (int)local_work_size[0] != 0){												// compensate for division remainder above by adding one grid
+  blocks_x = blocks_x + 1;
+  }
+  blocks_work_size = blocks_x;
+  global_work_size[0] = blocks_work_size * local_work_size[0];						// define the number of blocks in the grid
+
+  printf("max # of workgroups = %d, # of threads/workgroup = %d (ensure that device can handle)\n", (int)(global_work_size[0]/local_work_size[0]), (int)local_work_size[0]);
+
+  //======================================================================================================================================================150
+  // 	Extract Kernel - SCALE IMAGE DOWN FROM 0-255 TO 0-1 AND EXTRACT
+  //======================================================================================================================================================150
+
+  //====================================================================================================100
+  //	set arguments
+  //====================================================================================================100
+
+  error = clSetKernelArg(	extract_kernel,
+  0,
+  sizeof(long)*2,
+  (void *) &Ne);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	extract_kernel,
+  1,
+  sizeof(cl_mem),
+  (void *) &d_I);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	launch kernel
+  //====================================================================================================100
+
+  error = clEnqueueNDRangeKernel(	command_queue,
+  extract_kernel,
+  1,
+  NULL,
+  global_work_size,
+  local_work_size,
+  0,
+  NULL,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	Synchronization - wait for all operations in the command queue so far to finish
+  //====================================================================================================100
+
+  // error = clFinish(command_queue);
+  // if (error != CL_SUCCESS)
+  // fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	End
+  //====================================================================================================100
+
+  //======================================================================================================================================================150
+  // 	WHAT IS CONSTANT IN COMPUTATION LOOP
+  //======================================================================================================================================================150
+
+  //====================================================================================================100
+  //	Prepare Kernel
+  //====================================================================================================100
+
+  error = clSetKernelArg(	prepare_kernel,
+  0,
+  sizeof(long)*2,
+  (void *) &Ne);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	prepare_kernel,
+  1,
+  sizeof(cl_mem),
+  (void *) &d_I);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	prepare_kernel,
+  2,
+  sizeof(cl_mem),
+  (void *) &d_sums);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	prepare_kernel,
+  3,
+  sizeof(cl_mem),
+  (void *) &d_sums2);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	Reduce Kernel
+  //====================================================================================================100
+
+  int blocks2_x;
+  int blocks2_work_size;
+  size_t global_work_size2[1];
+  long no;
+  int mul;
+  int mem_size_single = sizeof(fp) * 1;
+  fp total;
+  fp total2;
+  fp meanROI;
+  fp meanROI2;
+  fp varROI;
+  fp q0sqr;
+
+  error = clSetKernelArg(	reduce_kernel,
+  0,
+  sizeof(long)*2,
+  (void *) &Ne);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	reduce_kernel,
+  3,
+  sizeof(cl_mem),
+  (void *) &d_sums);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	reduce_kernel,
+  4,
+  sizeof(cl_mem),
+  (void *) &d_sums2);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	SRAD Kernel
+  //====================================================================================================100
+
+  error = clSetKernelArg(	srad_kernel,
+  0,
+  sizeof(fp),
+  (void *) &lambda);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad_kernel,
+  1,
+  sizeof(int),
+  (void *) &Nr);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad_kernel,
+  2,
+  sizeof(int),
+  (void *) &Nc);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad_kernel,
+  3,
+  sizeof(long)*2,
+  (void *) &Ne);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad_kernel,
+  4,
+  sizeof(cl_mem),
+  (void *) &d_iN);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad_kernel,
+  5,
+  sizeof(cl_mem),
+  (void *) &d_iS);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad_kernel,
+  6,
+  sizeof(cl_mem),
+  (void *) &d_jE);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad_kernel,
+  7,
+  sizeof(cl_mem),
+  (void *) &d_jW);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad_kernel,
+  8,
+  sizeof(cl_mem),
+  (void *) &d_dN);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad_kernel,
+  9,
+  sizeof(cl_mem),
+  (void *) &d_dS);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad_kernel,
+  10,
+  sizeof(cl_mem),
+  (void *) &d_dW);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad_kernel,
+  11,
+  sizeof(cl_mem),
+  (void *) &d_dE);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad_kernel,
+  13,
+  sizeof(cl_mem),
+  (void *) &d_c);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad_kernel,
+  14,
+  sizeof(cl_mem),
+  (void *) &d_I);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	SRAD2 Kernel
+  //====================================================================================================100
+
+  error = clSetKernelArg(	srad2_kernel,
+  0,
+  sizeof(fp),
+  (void *) &lambda);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad2_kernel,
+  1,
+  sizeof(int),
+  (void *) &Nr);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad2_kernel,
+  2,
+  sizeof(int),
+  (void *) &Nc);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad2_kernel,
+  3,
+  sizeof(long)*2,
+  (void *) &Ne);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad2_kernel,
+  4,
+  sizeof(cl_mem),
+  (void *) &d_iN);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad2_kernel,
+  5,
+  sizeof(cl_mem),
+  (void *) &d_iS);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad2_kernel,
+  6,
+  sizeof(cl_mem),
+  (void *) &d_jE);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad2_kernel,
+  7,
+  sizeof(cl_mem),
+  (void *) &d_jW);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad2_kernel,
+  8,
+  sizeof(cl_mem),
+  (void *) &d_dN);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad2_kernel,
+  9,
+  sizeof(cl_mem),
+  (void *) &d_dS);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad2_kernel,
+  10,
+  sizeof(cl_mem),
+  (void *) &d_dW);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad2_kernel,
+  11,
+  sizeof(cl_mem),
+  (void *) &d_dE);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad2_kernel,
+  12,
+  sizeof(cl_mem),
+  (void *) &d_c);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	srad2_kernel,
+  13,
+  sizeof(cl_mem),
+  (void *) &d_I);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	End
+  //====================================================================================================100
+
+  //======================================================================================================================================================150
+  // 	COMPUTATION
+  //======================================================================================================================================================150
+
+  printf("Iterations Progress: ");
+
+  // execute main loop
+  for (iter=0; iter<niter; iter++){										// do for the number of iterations input parameter
+
+  printf("%d ", iter);
+  fflush(NULL);
+
+  //====================================================================================================100
+  // Prepare kernel
+  //====================================================================================================100
+
+  // launch kernel
+  error = clEnqueueNDRangeKernel(	command_queue,
+  prepare_kernel,
+  1,
+  NULL,
+  global_work_size,
+  local_work_size,
+  0,
+  NULL,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // synchronize
+  // error = clFinish(command_queue);
+  // if (error != CL_SUCCESS)
+  // fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	Reduce Kernel - performs subsequent reductions of sums
+  //====================================================================================================100
+
+  // initial values
+  blocks2_work_size = blocks_work_size;							// original number of blocks
+  global_work_size2[0] = global_work_size[0];
+  no = Ne;														// original number of sum elements
+  mul = 1;														// original multiplier
+
+  // loop
+  while(blocks2_work_size != 0){
+
+  // set arguments that were uptaded in this loop
+  error = clSetKernelArg(	reduce_kernel,
+  1,
+  sizeof(long)*2,
+  (void *) &no);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	reduce_kernel,
+  2,
+  sizeof(int),
+  (void *) &mul);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  error = clSetKernelArg(	reduce_kernel,
+  5,
+  sizeof(int),
+  (void *) &blocks2_work_size);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // launch kernel
+  error = clEnqueueNDRangeKernel(	command_queue,
+  reduce_kernel,
+  1,
+  NULL,
+  global_work_size2,
+  local_work_size,
+  0,
+  NULL,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // synchronize
+  // error = clFinish(command_queue);
+  // if (error != CL_SUCCESS)
+  // fatal_CL(error, __LINE__);
+
+  // update execution parameters
+  no = blocks2_work_size;												// get current number of elements
+  if(blocks2_work_size == 1){
+  blocks2_work_size = 0;
+  }
+  else{
+  mul = mul * NUMBER_THREADS;										// update the increment
+  blocks_x = blocks2_work_size/(int)local_work_size[0];			// number of blocks
+  if (blocks2_work_size % (int)local_work_size[0] != 0){			// compensate for division remainder above by adding one grid
+  blocks_x = blocks_x + 1;
+  }
+  blocks2_work_size = blocks_x;
+  global_work_size2[0] = blocks2_work_size * (int)local_work_size[0];
+  }
+
+  }
+
+  // copy total sums to device
+  error = clEnqueueReadBuffer(command_queue,
+  d_sums,
+  CL_TRUE,
+  0,
+  mem_size_single,
+  &total,
+  0,
+  NULL,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  error = clEnqueueReadBuffer(command_queue,
+  d_sums2,
+  CL_TRUE,
+  0,
+  mem_size_single,
+  &total2,
+  0,
+  NULL,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  // calculate statistics
+  //====================================================================================================100
+
+  meanROI	= total / (fp)(NeROI);										// gets mean (average) value of element in ROI
+  meanROI2 = meanROI * meanROI;										//
+  varROI = (total2 / (fp)(NeROI)) - meanROI2;							// gets variance of ROI
+  q0sqr = varROI / meanROI2;											// gets standard deviation of ROI
+
+  //====================================================================================================100
+  // execute srad kernel
+  //====================================================================================================100
+
+  // set arguments that were uptaded in this loop
+  error = clSetKernelArg(	srad_kernel,
+  12,
+  sizeof(fp),
+  (void *) &q0sqr);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // launch kernel
+  error = clEnqueueNDRangeKernel(	command_queue,
+  srad_kernel,
+  1,
+  NULL,
+  global_work_size,
+  local_work_size,
+  0,
+  NULL,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // synchronize
+  // error = clFinish(command_queue);
+  // if (error != CL_SUCCESS)
+  // fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  // execute srad2 kernel
+  //====================================================================================================100
+
+  // launch kernel
+  error = clEnqueueNDRangeKernel(	command_queue,
+  srad2_kernel,
+  1,
+  NULL,
+  global_work_size,
+  local_work_size,
+  0,
+  NULL,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // synchronize
+  // error = clFinish(command_queue);
+  // if (error != CL_SUCCESS)
+  // fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  // End
+  //====================================================================================================100
+
+  }
+
+  printf("\n");
+
+  //======================================================================================================================================================150
+  // 	Compress Kernel - SCALE IMAGE UP FROM 0-1 TO 0-255 AND COMPRESS
+  //======================================================================================================================================================150
+
+  //====================================================================================================100
+  // set parameters
+  //====================================================================================================100
+
+  error = clSetKernelArg(	compress_kernel,
+  0,
+  sizeof(long)*2,
+  (void *) &Ne);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clSetKernelArg(	compress_kernel,
+  1,
+  sizeof(cl_mem),
+  (void *) &d_I);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  // launch kernel
+  //====================================================================================================100
+
+  error = clEnqueueNDRangeKernel(	command_queue,
+  compress_kernel,
+  1,
+  NULL,
+  global_work_size,
+  local_work_size,
+  0,
+  NULL,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  // synchronize
+  //====================================================================================================100
+
+  error = clFinish(command_queue);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //====================================================================================================100
+  //	End
+  //====================================================================================================100
+
+  //======================================================================================================================================================150
+  // 	COPY RESULTS BACK TO CPU
+  //======================================================================================================================================================150
+
+  error = clEnqueueReadBuffer(command_queue,
+  d_I,
+  CL_TRUE,
+  0,
+  mem_size,
+  image,
+  0,
+  NULL,
+  NULL);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // int i;
+  // for(i=0; i<100; i++){
+  // printf("%f ", image[i]);
+  // }
+
+  //======================================================================================================================================================150
+  // 	FREE MEMORY
+  //======================================================================================================================================================150
+
+  // OpenCL structures
+  error = clReleaseKernel(extract_kernel);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clReleaseKernel(prepare_kernel);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clReleaseKernel(reduce_kernel);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clReleaseKernel(srad_kernel);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clReleaseKernel(srad2_kernel);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clReleaseKernel(compress_kernel);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clReleaseProgram(program);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // common_change
+  error = clReleaseMemObject(d_I);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clReleaseMemObject(d_c);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  error = clReleaseMemObject(d_iN);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clReleaseMemObject(d_iS);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clReleaseMemObject(d_jE);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clReleaseMemObject(d_jW);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  error = clReleaseMemObject(d_dN);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clReleaseMemObject(d_dS);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clReleaseMemObject(d_dE);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clReleaseMemObject(d_dW);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  error = clReleaseMemObject(d_sums);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clReleaseMemObject(d_sums2);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  // OpenCL structures
+  error = clFlush(command_queue);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clReleaseCommandQueue(command_queue);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+  error = clReleaseContext(context);
+  if (error != CL_SUCCESS)
+  fatal_CL(error, __LINE__);
+
+  //======================================================================================================================================================150
+  // 	End
+  //======================================================================================================================================================150
 
 }
 

+ 14 - 14
opencl/srad/kernel/kernel_gpu_opencl_wrapper.h

@@ -6,21 +6,21 @@ extern "C" {
 //	KERNEL_GPU_CUDA_WRAPPER HEADER
 //========================================================================================================================================================================================================200
 
-void 
+void
 kernel_gpu_opencl_wrapper(fp* image,											// input image
-							            int Nr,												// IMAGE nbr of rows
-							            int Nc,												// IMAGE nbr of cols
-							            long Ne,											// IMAGE nbr of elem
-							            int niter,											// nbr of iterations
-							            fp lambda,											// update step size
-							            long NeROI,											// ROI nbr of elements
-							            int* iN,
-							            int* iS,
-							            int* jE,
-							            int* jW,
-							            int iter,											// primary loop
-							            int mem_size_i,
-							            int mem_size_j,
+              int Nr,												// IMAGE nbr of rows
+              int Nc,												// IMAGE nbr of cols
+              long Ne,											// IMAGE nbr of elem
+              int niter,											// nbr of iterations
+              fp lambda,											// update step size
+              long NeROI,											// ROI nbr of elements
+              int* iN,
+              int* iS,
+              int* jE,
+              int* jW,
+              int iter,											// primary loop
+              int mem_size_i,
+              int mem_size_j,
                           int platform_idx,
                           int device_idx,
                           int use_gpu);

+ 219 - 219
opencl/srad/main.c

@@ -46,252 +46,252 @@
 //	MAIN FUNCTION
 //========================================================================================================================================================================================================200
 
-int 
-main(	int argc, 
-		char* argv []){
+int
+main(	int argc,
+  char* argv []){
   printf("WG size of kernel = %d \n", NUMBER_THREADS);
-	//======================================================================================================================================================150
-	// 	VARIABLES
-	//======================================================================================================================================================150
-
-	// time
-	long long time0;
-	long long time1;
-	long long time2;
-	long long time3;
-	long long time4;
-	long long time5;
-	long long time6;
-
-	// inputs image, input paramenters
-	fp* image_ori;																// originalinput image
-	int image_ori_rows;
-	int image_ori_cols;
-	long image_ori_elem;
-
-	// inputs image, input paramenters
-	fp* image;															// input image
-	int Nr,Nc;													// IMAGE nbr of rows/cols/elements
-	long Ne;
-
-	// algorithm parameters
-	int niter;																// nbr of iterations
-	fp lambda;															// update step size
-
-	// size of IMAGE
-	int r1,r2,c1,c2;												// row/col coordinates of uniform ROI
-	long NeROI;														// ROI nbr of elements
-
-	// surrounding pixel indicies
-	int* iN;
-	int* iS;
-	int* jE;
-	int* jW;    
-
-	// counters
-	int iter;   // primary loop
-	long i;    // image row
-	long j;    // image col
-
-	// memory sizes
-	int mem_size_i;
-	int mem_size_j;
-
-	time0 = get_time();
-
-	//======================================================================================================================================================150
-	//	INPUT ARGUMENTS
-	//======================================================================================================================================================150
+  //======================================================================================================================================================150
+  // 	VARIABLES
+  //======================================================================================================================================================150
+
+  // time
+  long long time0;
+  long long time1;
+  long long time2;
+  long long time3;
+  long long time4;
+  long long time5;
+  long long time6;
+
+  // inputs image, input paramenters
+  fp* image_ori;																// originalinput image
+  int image_ori_rows;
+  int image_ori_cols;
+  long image_ori_elem;
+
+  // inputs image, input paramenters
+  fp* image;															// input image
+  int Nr,Nc;													// IMAGE nbr of rows/cols/elements
+  long Ne;
+
+  // algorithm parameters
+  int niter;																// nbr of iterations
+  fp lambda;															// update step size
+
+  // size of IMAGE
+  int r1,r2,c1,c2;												// row/col coordinates of uniform ROI
+  long NeROI;														// ROI nbr of elements
+
+  // surrounding pixel indicies
+  int* iN;
+  int* iS;
+  int* jE;
+  int* jW;
+
+  // counters
+  int iter;   // primary loop
+  long i;    // image row
+  long j;    // image col
+
+  // memory sizes
+  int mem_size_i;
+  int mem_size_j;
+
+  time0 = get_time();
+
+  //======================================================================================================================================================150
+  //	INPUT ARGUMENTS
+  //======================================================================================================================================================150
 
   // Rewritten parameters parsing for selecting platform and device and old
   // parameters
-    
+
   // Additional variables for platform and device selection
   int platform_idx = 0;
   int device_idx = 0;
   int use_gpu = 0;
-  
+
   int opt;
   extern char *optarg;
-	while ((opt = getopt(argc, argv, "i:l:r:c:p:d:g:")) != -1 ) {
-		switch(opt){
-			case 'i':
-			   niter = atoi(optarg);
-			   break;
-			case 'l':
-			   lambda = atoi(optarg);
-			   break;
+  while ((opt = getopt(argc, argv, "i:l:r:c:p:d:g:")) != -1 ) {
+  switch(opt){
+  case 'i':
+     niter = atoi(optarg);
+     break;
+  case 'l':
+     lambda = atoi(optarg);
+     break;
       case 'r':
-			   Nr = atoi(optarg);
+     Nr = atoi(optarg);
          break;
       case 'c':
-			   Nc = atoi(optarg);
+     Nc = atoi(optarg);
          break;
       case 'p':
-			   platform_idx = atoi(optarg);
-			   break;
+     platform_idx = atoi(optarg);
+     break;
       case 'd':
   		   device_idx = atoi(optarg);
   		   break;
       case 'g':
          use_gpu = atoi(optarg);
-         break;            
+         break;
       case ':':
-			   fprintf(stderr, "missing argument\n");
-			   break;
+     fprintf(stderr, "missing argument\n");
+     break;
       default:
-			   fprintf(stderr, "Usage: %s - <iteration> -l <lambda> -r <rows> -c <columns> -p <platform> -d <device> -g <use_gpu>\n",
+     fprintf(stderr, "Usage: %s - <iteration> -l <lambda> -r <rows> -c <columns> -p <platform> -d <device> -g <use_gpu>\n",
                  argv[0]);
-			   exit(EXIT_FAILURE);
-		}
-	}
-
-	time1 = get_time();
-
-	//======================================================================================================================================================150
-	// 	READ INPUT FROM FILE
-	//======================================================================================================================================================150
-
-	//====================================================================================================100
-	// 	READ IMAGE (SIZE OF IMAGE HAS TO BE KNOWN)
-	//====================================================================================================100
-
-	image_ori_rows = 502;
-	image_ori_cols = 458;
-	image_ori_elem = image_ori_rows * image_ori_cols;
-
-	image_ori = (fp*)malloc(sizeof(fp) * image_ori_elem);
-
-	read_graphics(	"../../data/srad/image.pgm",
-					image_ori,
-					image_ori_rows,
-					image_ori_cols,
-					1);
-
-	//====================================================================================================100
-	// 	RESIZE IMAGE (ASSUMING COLUMN MAJOR STORAGE OF image_orig)
-	//====================================================================================================100
-
-	Ne = Nr*Nc;
-
-	image = (fp*)malloc(sizeof(fp) * Ne);
-
-	resize(	image_ori,
-				image_ori_rows,
-				image_ori_cols,
-				image,
-				Nr,
-				Nc,
-				1);
-
-	//====================================================================================================100
-	// 	End
-	//====================================================================================================100
-
-	time2 = get_time();
-
-	//======================================================================================================================================================150
-	// 	SETUP
-	//======================================================================================================================================================150
-
-	// variables
-	r1     = 0;											// top row index of ROI
-	r2     = Nr - 1;									// bottom row index of ROI
-	c1     = 0;											// left column index of ROI
-	c2     = Nc - 1;									// right column index of ROI
-
-	// ROI image size
-	NeROI = (r2-r1+1)*(c2-c1+1);											// number of elements in ROI, ROI size
-
-	// allocate variables for surrounding pixels
-	mem_size_i = sizeof(int) * Nr;											//
-	iN = (int *)malloc(mem_size_i) ;										// north surrounding element
-	iS = (int *)malloc(mem_size_i) ;										// south surrounding element
-	mem_size_j = sizeof(int) * Nc;											//
-	jW = (int *)malloc(mem_size_j) ;										// west surrounding element
-	jE = (int *)malloc(mem_size_j) ;										// east surrounding element
-
-	// N/S/W/E indices of surrounding pixels (every element of IMAGE)
-	for (i=0; i<Nr; i++) {
-		iN[i] = i-1;														// holds index of IMAGE row above
-		iS[i] = i+1;														// holds index of IMAGE row below
-	}
-	for (j=0; j<Nc; j++) {
-		jW[j] = j-1;														// holds index of IMAGE column on the left
-		jE[j] = j+1;														// holds index of IMAGE column on the right
-	}
-
-	// N/S/W/E boundary conditions, fix surrounding indices outside boundary of image
-	iN[0]    = 0;															// changes IMAGE top row index from -1 to 0
-	iS[Nr-1] = Nr-1;														// changes IMAGE bottom row index from Nr to Nr-1 
-	jW[0]    = 0;															// changes IMAGE leftmost column index from -1 to 0
-	jE[Nc-1] = Nc-1;														// changes IMAGE rightmost column index from Nc to Nc-1
-
-	time3= get_time();
-
-	//======================================================================================================================================================150
-	// 	KERNEL
-	//======================================================================================================================================================150
-
-	kernel_gpu_opencl_wrapper(image,											// input image
-								            Nr,												// IMAGE nbr of rows
-								            Nc,												// IMAGE nbr of cols
-								            Ne,												// IMAGE nbr of elem
-								            niter,											// nbr of iterations
-								            lambda,											// update step size
-								            NeROI,											// ROI nbr of elements
-								            iN,
-								            iS,
-								            jE,
-								            jW,
-								            iter,											// primary loop
-								            mem_size_i,
-								            mem_size_j,
+     exit(EXIT_FAILURE);
+  }
+  }
+
+  time1 = get_time();
+
+  //======================================================================================================================================================150
+  // 	READ INPUT FROM FILE
+  //======================================================================================================================================================150
+
+  //====================================================================================================100
+  // 	READ IMAGE (SIZE OF IMAGE HAS TO BE KNOWN)
+  //====================================================================================================100
+
+  image_ori_rows = 502;
+  image_ori_cols = 458;
+  image_ori_elem = image_ori_rows * image_ori_cols;
+
+  image_ori = (fp*)malloc(sizeof(fp) * image_ori_elem);
+
+  read_graphics(	"../../data/srad/image.pgm",
+  image_ori,
+  image_ori_rows,
+  image_ori_cols,
+  1);
+
+  //====================================================================================================100
+  // 	RESIZE IMAGE (ASSUMING COLUMN MAJOR STORAGE OF image_orig)
+  //====================================================================================================100
+
+  Ne = Nr*Nc;
+
+  image = (fp*)malloc(sizeof(fp) * Ne);
+
+  resize(	image_ori,
+  image_ori_rows,
+  image_ori_cols,
+  image,
+  Nr,
+  Nc,
+  1);
+
+  //====================================================================================================100
+  // 	End
+  //====================================================================================================100
+
+  time2 = get_time();
+
+  //======================================================================================================================================================150
+  // 	SETUP
+  //======================================================================================================================================================150
+
+  // variables
+  r1     = 0;											// top row index of ROI
+  r2     = Nr - 1;									// bottom row index of ROI
+  c1     = 0;											// left column index of ROI
+  c2     = Nc - 1;									// right column index of ROI
+
+  // ROI image size
+  NeROI = (r2-r1+1)*(c2-c1+1);											// number of elements in ROI, ROI size
+
+  // allocate variables for surrounding pixels
+  mem_size_i = sizeof(int) * Nr;											//
+  iN = (int *)malloc(mem_size_i) ;										// north surrounding element
+  iS = (int *)malloc(mem_size_i) ;										// south surrounding element
+  mem_size_j = sizeof(int) * Nc;											//
+  jW = (int *)malloc(mem_size_j) ;										// west surrounding element
+  jE = (int *)malloc(mem_size_j) ;										// east surrounding element
+
+  // N/S/W/E indices of surrounding pixels (every element of IMAGE)
+  for (i=0; i<Nr; i++) {
+  iN[i] = i-1;														// holds index of IMAGE row above
+  iS[i] = i+1;														// holds index of IMAGE row below
+  }
+  for (j=0; j<Nc; j++) {
+  jW[j] = j-1;														// holds index of IMAGE column on the left
+  jE[j] = j+1;														// holds index of IMAGE column on the right
+  }
+
+  // N/S/W/E boundary conditions, fix surrounding indices outside boundary of image
+  iN[0]    = 0;															// changes IMAGE top row index from -1 to 0
+  iS[Nr-1] = Nr-1;														// changes IMAGE bottom row index from Nr to Nr-1
+  jW[0]    = 0;															// changes IMAGE leftmost column index from -1 to 0
+  jE[Nc-1] = Nc-1;														// changes IMAGE rightmost column index from Nc to Nc-1
+
+  time3= get_time();
+
+  //======================================================================================================================================================150
+  // 	KERNEL
+  //======================================================================================================================================================150
+
+  kernel_gpu_opencl_wrapper(image,											// input image
+              Nr,												// IMAGE nbr of rows
+              Nc,												// IMAGE nbr of cols
+              Ne,												// IMAGE nbr of elem
+              niter,											// nbr of iterations
+              lambda,											// update step size
+              NeROI,											// ROI nbr of elements
+              iN,
+              iS,
+              jE,
+              jW,
+              iter,											// primary loop
+              mem_size_i,
+              mem_size_j,
                             platform_idx,
                             device_idx,
                             use_gpu);
 
-	time4 = get_time();
-
-	//======================================================================================================================================================150
-	// 	WRITE OUTPUT IMAGE TO FILE
-	//======================================================================================================================================================150
-
-	write_graphics(	"./output/image_out.pgm",
-					image,
-					Nr,
-					Nc,
-					1,
-					255);
-
-	time5 = get_time();
-
-	//======================================================================================================================================================150
-	// 	FREE MEMORY
-	//======================================================================================================================================================150
-
-	free(image_ori);
-	free(image);
-	free(iN); 
-	free(iS); 
-	free(jW); 
-	free(jE);
-
-	time6 = get_time();
-
-	//======================================================================================================================================================150
-	//	DISPLAY TIMING
-	//======================================================================================================================================================150
-
-	printf("Time spent in different stages of the application:\n");
-	printf("%.12f s, %.12f % : READ COMMAND LINE PARAMETERS\n", 						(fp) (time1-time0) / 1000000, (fp) (time1-time0) / (fp) (time5-time0) * 100);
-	printf("%.12f s, %.12f % : READ AND RESIZE INPUT IMAGE FROM FILE\n", 				(fp) (time2-time1) / 1000000, (fp) (time2-time1) / (fp) (time5-time0) * 100);
-	printf("%.12f s, %.12f % : SETUP\n", 												(fp) (time3-time2) / 1000000, (fp) (time3-time2) / (fp) (time5-time0) * 100);
-	printf("%.12f s, %.12f % : KERNEL\n", 												(fp) (time4-time3) / 1000000, (fp) (time4-time3) / (fp) (time5-time0) * 100);
-	printf("%.12f s, %.12f % : WRITE OUTPUT IMAGE TO FILE\n", 							(fp) (time5-time4) / 1000000, (fp) (time5-time4) / (fp) (time5-time0) * 100);
-	printf("%.12f s, %.12f % : FREE MEMORY\n", 											(fp) (time6-time5) / 1000000, (fp) (time6-time5) / (fp) (time5-time0) * 100);
-	printf("Total time:\n");
-	printf("%.12f s\n", 																(fp) (time5-time0) / 1000000);
+  time4 = get_time();
+
+  //======================================================================================================================================================150
+  // 	WRITE OUTPUT IMAGE TO FILE
+  //======================================================================================================================================================150
+
+  write_graphics(	"./output/image_out.pgm",
+  image,
+  Nr,
+  Nc,
+  1,
+  255);
+
+  time5 = get_time();
+
+  //======================================================================================================================================================150
+  // 	FREE MEMORY
+  //======================================================================================================================================================150
+
+  free(image_ori);
+  free(image);
+  free(iN);
+  free(iS);
+  free(jW);
+  free(jE);
+
+  time6 = get_time();
+
+  //======================================================================================================================================================150
+  //	DISPLAY TIMING
+  //======================================================================================================================================================150
+
+  printf("Time spent in different stages of the application:\n");
+  printf("%.12f s, %.12f % : READ COMMAND LINE PARAMETERS\n", 						(fp) (time1-time0) / 1000000, (fp) (time1-time0) / (fp) (time5-time0) * 100);
+  printf("%.12f s, %.12f % : READ AND RESIZE INPUT IMAGE FROM FILE\n", 				(fp) (time2-time1) / 1000000, (fp) (time2-time1) / (fp) (time5-time0) * 100);
+  printf("%.12f s, %.12f % : SETUP\n", 												(fp) (time3-time2) / 1000000, (fp) (time3-time2) / (fp) (time5-time0) * 100);
+  printf("%.12f s, %.12f % : KERNEL\n", 												(fp) (time4-time3) / 1000000, (fp) (time4-time3) / (fp) (time5-time0) * 100);
+  printf("%.12f s, %.12f % : WRITE OUTPUT IMAGE TO FILE\n", 							(fp) (time5-time4) / 1000000, (fp) (time5-time4) / (fp) (time5-time0) * 100);
+  printf("%.12f s, %.12f % : FREE MEMORY\n", 											(fp) (time6-time5) / 1000000, (fp) (time6-time5) / (fp) (time5-time0) * 100);
+  printf("Total time:\n");
+  printf("%.12f s\n", 																(fp) (time5-time0) / 1000000);
 
 }
 

+ 975 - 975
opencl/streamcluster/CLHelper.h

@@ -37,59 +37,59 @@ double KC;  //: the kernel compilation time
 //#pragma OPENCL EXTENSION cl_nv_compiler_options:enable
 #define WORK_DIM 2	//work-items dimensions
 /*------------------------------------------------------------
-	@struct:	the structure of device properties
-	@date:		24/03/2011
+  @struct:	the structure of device properties
+  @date:		24/03/2011
 ------------------------------------------------------------*/
 struct _clDeviceProp{
-/*CL_DEVICE_ADDRESS_BITS                       
-CL_DEVICE_AVAILABLE                          
-CL_DEVICE_COMPILER_AVAILABLE                 
-CL_DEVICE_ENDIAN_LITTLE                      
-CL_DEVICE_ERROR_CORRECTION_SUPPORT           
-CL_DEVICE_EXECUTION_CAPABILITIES             
+/*CL_DEVICE_ADDRESS_BITS
+CL_DEVICE_AVAILABLE
+CL_DEVICE_COMPILER_AVAILABLE
+CL_DEVICE_ENDIAN_LITTLE
+CL_DEVICE_ERROR_CORRECTION_SUPPORT
+CL_DEVICE_EXECUTION_CAPABILITIES
 CL_DEVICE_EXTENSIONS
-CL_DEVICE_GLOBAL_MEM_CACHE_SIZE              
-CL_DEVICE_GLOBAL_MEM_CACHE_TYPE              
-CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE          
-CL_DEVICE_GLOBAL_MEM_SIZE                    
-CL_DEVICE_IMAGE_SUPPORT                      
-CL_DEVICE_IMAGE2D_MAX_HEIGHT                 
-CL_DEVICE_IMAGE2D_MAX_WIDTH                  
-CL_DEVICE_IMAGE3D_MAX_DEPTH                  
-CL_DEVICE_IMAGE3D_MAX_HEIGHT                 
-CL_DEVICE_IMAGE3D_MAX_WIDTH                  
-CL_DEVICE_LOCAL_MEM_SIZE                     
-CL_DEVICE_LOCAL_MEM_TYPE                     
-CL_DEVICE_MAX_CLOCK_FREQUENCY                
-CL_DEVICE_MAX_COMPUTE_UNITS                  
-CL_DEVICE_MAX_CONSTANT_ARGS                  
-CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE           
-CL_DEVICE_MAX_MEM_ALLOC_SIZE                 
-CL_DEVICE_MAX_PARAMETER_SIZE                 
-CL_DEVICE_MAX_READ_IMAGE_ARGS                
-CL_DEVICE_MAX_SAMPLERS                       
-CL_DEVICE_MAX_WORK_GROUP_SIZE                
-CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS           
-CL_DEVICE_MAX_WORK_ITEM_SIZES                
-CL_DEVICE_MAX_WRITE_IMAGE_ARGS               
-CL_DEVICE_MEM_BASE_ADDR_ALIGN                
-CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE           
-CL_DEVICE_NAME                               
+CL_DEVICE_GLOBAL_MEM_CACHE_SIZE
+CL_DEVICE_GLOBAL_MEM_CACHE_TYPE
+CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE
+CL_DEVICE_GLOBAL_MEM_SIZE
+CL_DEVICE_IMAGE_SUPPORT
+CL_DEVICE_IMAGE2D_MAX_HEIGHT
+CL_DEVICE_IMAGE2D_MAX_WIDTH
+CL_DEVICE_IMAGE3D_MAX_DEPTH
+CL_DEVICE_IMAGE3D_MAX_HEIGHT
+CL_DEVICE_IMAGE3D_MAX_WIDTH
+CL_DEVICE_LOCAL_MEM_SIZE
+CL_DEVICE_LOCAL_MEM_TYPE
+CL_DEVICE_MAX_CLOCK_FREQUENCY
+CL_DEVICE_MAX_COMPUTE_UNITS
+CL_DEVICE_MAX_CONSTANT_ARGS
+CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE
+CL_DEVICE_MAX_MEM_ALLOC_SIZE
+CL_DEVICE_MAX_PARAMETER_SIZE
+CL_DEVICE_MAX_READ_IMAGE_ARGS
+CL_DEVICE_MAX_SAMPLERS
+CL_DEVICE_MAX_WORK_GROUP_SIZE
+CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS
+CL_DEVICE_MAX_WORK_ITEM_SIZES
+CL_DEVICE_MAX_WRITE_IMAGE_ARGS
+CL_DEVICE_MEM_BASE_ADDR_ALIGN
+CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE
+CL_DEVICE_NAME
 CL_DEVICE_PLATFORM
-CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR        
-CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE      
-CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT       
-CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT         
-CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG        
-CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT       
-CL_DEVICE_PROFILE                           
-CL_DEVICE_PROFILING_TIMER_RESOLUTION         
-CL_DEVICE_QUEUE_PROPERTIES                   
-CL_DEVICE_SINGLE_FP_CONFIG                   
-CL_DEVICE_TYPE                              
-CL_DEVICE_VENDOR_ID                          
-CL_DEVICE_VENDOR                            
-CL_DEVICE_VERSION                      
+CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR
+CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE
+CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT
+CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT
+CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG
+CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT
+CL_DEVICE_PROFILE
+CL_DEVICE_PROFILING_TIMER_RESOLUTION
+CL_DEVICE_QUEUE_PROPERTIES
+CL_DEVICE_SINGLE_FP_CONFIG
+CL_DEVICE_TYPE
+CL_DEVICE_VENDOR_ID
+CL_DEVICE_VENDOR
+CL_DEVICE_VERSION
 CL_DRIVER_VERSION*/
 char device_name[100];
 };
@@ -121,47 +121,47 @@ double gettime() {
   return t.tv_sec+t.tv_usec*1e-6;
 }
 /*------------------------------------------------------------
-	@function:	select device to use
-	@params:	
-		size: 	the index of device to be used
-	@return:	NULL
-	@date:		24/03/2011
+  @function:	select device to use
+  @params:
+  size: 	the index of device to be used
+  @return:	NULL
+  @date:		24/03/2011
 ------------------------------------------------------------*/
 void _clSetDevice(int idx) throw(string){
-	if(idx>(number_devices-1))
-	  throw(string(":invalid device ID:"));
-	device_id_inused = idx;
-	
+  if(idx>(number_devices-1))
+    throw(string(":invalid device ID:"));
+  device_id_inused = idx;
+
 }
 
 /*------------------------------------------------------------
-	@function:	get device properties indexed by 'idx'
-	@params:	
-		idx:	device index
-		prop:	output properties
-	@return:	prop
-	@date:		24/03/2011
+  @function:	get device properties indexed by 'idx'
+  @params:
+  idx:	device index
+  prop:	output properties
+  @return:	prop
+  @date:		24/03/2011
 ------------------------------------------------------------*/
 void _clGetDeviceProperties(int idx, _clDeviceProp *prop) throw(string){
-   
+
    oclHandles.cl_status= clGetDeviceInfo(oclHandles.devices[idx], CL_DEVICE_NAME, 100, prop->device_name, NULL);
-   
+
 #ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS){
-	  oclHandles.error_str = "exception in _clGetDeviceProperties-> ";
-	  switch(oclHandles.cl_status){
-	  	case CL_INVALID_DEVICE:
-	  		oclHandles.error_str += "CL_INVALID_DEVICE";
-	  		break;
-	  	case CL_INVALID_VALUE:
-	  		oclHandles.error_str += "CL_INVALID_VALUE";
-	  		break;	   
-	  	default:
-	  		oclHandles.error_str += "unknown reasons";
-	  		break;	  		
-	  }
-	throw(oclHandles.error_str);
-	}	
+  if(oclHandles.cl_status != CL_SUCCESS){
+    oclHandles.error_str = "exception in _clGetDeviceProperties-> ";
+    switch(oclHandles.cl_status){
+    	case CL_INVALID_DEVICE:
+    		oclHandles.error_str += "CL_INVALID_DEVICE";
+    		break;
+    	case CL_INVALID_VALUE:
+    		oclHandles.error_str += "CL_INVALID_VALUE";
+    		break;
+    	default:
+    		oclHandles.error_str += "unknown reasons";
+    		break;
+    }
+  throw(oclHandles.error_str);
+  }
 #endif
 }
 
@@ -188,7 +188,7 @@ string FileToString(const string fileName){
             f.read(str, fileSize);
             f.close();
             str[size] = '\0';
-        
+
             s = str;
             delete [] str;
             return s;
@@ -210,28 +210,28 @@ string FileToString(const string fileName){
 }
 
 /*------------------------------------------------------------
-	@function:	Read command line parameters
-	@params:	NULL
-	@return:
-	@date:		24/03/2011
+  @function:	Read command line parameters
+  @params:	NULL
+  @return:
+  @date:		24/03/2011
 ------------------------------------------------------------*/
 char device_type[3];
 static int platform_idx;
 static int device_idx = 0;
 
 void _clCmdParams(int argc, char* argv[]){
-	for (int i = 0; i < argc; ++i){
-		switch (argv[i][1]){
-			case 't':	//--t stands for device type
-				if (++i < argc){
-					sscanf(argv[i], "%s", device_type);
-				}
-				else{
-					std::cerr << "Could not read argument after option " << argv[i-1] << std::endl;
-					throw;
-				}
-				break;
-		  case 'p':	 //--p stands for platform id
+  for (int i = 0; i < argc; ++i){
+  switch (argv[i][1]){
+  case 't':	//--t stands for device type
+  if (++i < argc){
+  sscanf(argv[i], "%s", device_type);
+  }
+  else{
+  std::cerr << "Could not read argument after option " << argv[i-1] << std::endl;
+  throw;
+  }
+  break;
+    case 'p':	 //--p stands for platform id
   			if (++i < argc){
   				sscanf(argv[i], "%d", &platform_idx);
   			}
@@ -249,49 +249,49 @@ void _clCmdParams(int argc, char* argv[]){
   				throw;
   			}
   			break;
-			default:
-				;
-		}
-	}
+  default:
+  ;
+  }
+  }
 }
 
 /*------------------------------------------------------------
-	@function:	Initlize CL objects
-	@params:	
-		device_id: device id
-		device_type: the types of devices, e.g. CPU, GPU, ACCERLERATOR,...	
-		(1) -t cpu/gpu/acc -d 0/1/2/...
-		(2) -t cpu/gpu/acc [-d 0]
-		(3) [-t default] -d 0/1/2/...
-		(4) NULL [-d 0]
-	@return:
-	@description:
-		there are 5 steps to initialize all the OpenCL objects needed,
-	@revised: 
-		get the number of devices and devices have no relationship with context
-	@date:		24/03/2011
+  @function:	Initlize CL objects
+  @params:
+  device_id: device id
+  device_type: the types of devices, e.g. CPU, GPU, ACCERLERATOR,...
+  (1) -t cpu/gpu/acc -d 0/1/2/...
+  (2) -t cpu/gpu/acc [-d 0]
+  (3) [-t default] -d 0/1/2/...
+  (4) NULL [-d 0]
+  @return:
+  @description:
+  there are 5 steps to initialize all the OpenCL objects needed,
+  @revised:
+  get the number of devices and devices have no relationship with context
+  @date:		24/03/2011
 ------------------------------------------------------------*/
 void _clInit(int platform_idx, int device_idx, string device_type)throw(string){
 
 #ifdef PROFILE_
-	TE = 0;
-	CC = 0;
-	CR = 0;
-	MA = 0;
-	MF = 0;
-	H2D = 0;
-	D2H = 0;
-	D2D = 0;
-	KE = 0;
-	KC = 0;
+  TE = 0;
+  CC = 0;
+  CR = 0;
+  MA = 0;
+  MF = 0;
+  H2D = 0;
+  D2H = 0;
+  D2D = 0;
+  KE = 0;
+  KC = 0;
 #endif
-	int DEVICE_ID_INUSED = 0;	
-	_clDeviceProp prop;
+  int DEVICE_ID_INUSED = 0;
+  _clDeviceProp prop;
 #ifdef PROFILE_
-	double t1 = gettime();
+  double t1 = gettime();
 #endif
 
-    cl_int resultCL;    
+    cl_int resultCL;
     oclHandles.context = NULL;
     oclHandles.devices = NULL;
     oclHandles.queue = NULL;
@@ -309,7 +309,7 @@ void _clInit(int platform_idx, int device_idx, string device_type)throw(string){
         throw (string("InitCL()::Error: Getting number of platforms (clGetPlatformIDs)"));
     //printf("number of platforms:%d\n",numPlatforms);	//by cambine
 #ifdef	DEV_INFO
-	std::cout<<"--cambine: number of platforms: "<<numPlatforms<<std::endl;
+  std::cout<<"--cambine: number of platforms: "<<numPlatforms<<std::endl;
 #endif
 
     if (!(numPlatforms > 0))
@@ -335,82 +335,82 @@ void _clInit(int platform_idx, int device_idx, string device_type)throw(string){
         if (resultCL != CL_SUCCESS)
             throw (string("InitCL()::Error: Getting platform info (clGetPlatformInfo)"));
 
-		//printf("vedor is %s\n",pbuff);
+  //printf("vedor is %s\n",pbuff);
 #ifdef	DEV_INFO
-	std::cout<<"--cambine: vedor is: "<<pbuff<<std::endl;
+  std::cout<<"--cambine: vedor is: "<<pbuff<<std::endl;
 #endif
 
     }
     free(allPlatforms);
     //-----------------------------------------------
-    //--cambine-2: detect OpenCL devices	
-    // First, get the size of device list 
+    //--cambine-2: detect OpenCL devices
+    // First, get the size of device list
     if(device_type.compare("")!=0){
-		if(device_type.compare("cpu")==0){
-		   oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_CPU, 0, NULL, &deviceListSize);
-		   if(oclHandles.cl_status!=CL_SUCCESS){
-			   throw(string("exception in _clInit -> clGetDeviceIDs -> CPU"));   				
-	       }
-		}	
-		if(device_type.compare("gpu")==0){		   
-		   oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_GPU, 0, NULL, &deviceListSize);
-		   if(oclHandles.cl_status!=CL_SUCCESS){
-			   throw(string("exception in _clInit -> clGetDeviceIDs -> GPU"));   	
-	       }
-		}
-		if(device_type.compare("acc")==0){
-		   oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_ACCELERATOR, 0, NULL, &deviceListSize);
-		   if(oclHandles.cl_status!=CL_SUCCESS){
-			   throw(string("exception in _clInit -> clGetDeviceIDs -> ACCELERATOR"));   	
-	       }
-		}	 	
+  if(device_type.compare("cpu")==0){
+     oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_CPU, 0, NULL, &deviceListSize);
+     if(oclHandles.cl_status!=CL_SUCCESS){
+     throw(string("exception in _clInit -> clGetDeviceIDs -> CPU"));
+         }
+  }
+  if(device_type.compare("gpu")==0){
+     oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_GPU, 0, NULL, &deviceListSize);
+     if(oclHandles.cl_status!=CL_SUCCESS){
+     throw(string("exception in _clInit -> clGetDeviceIDs -> GPU"));
+         }
+  }
+  if(device_type.compare("acc")==0){
+     oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_ACCELERATOR, 0, NULL, &deviceListSize);
+     if(oclHandles.cl_status!=CL_SUCCESS){
+     throw(string("exception in _clInit -> clGetDeviceIDs -> ACCELERATOR"));
+         }
+  }
     }
     else{
-		   oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_ALL, 0, NULL, &deviceListSize);
-		   if(oclHandles.cl_status!=CL_SUCCESS){
-			   throw(string("exception in _clInit -> clGetDeviceIDs -> ALL"));   	
-	       }
+     oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_ALL, 0, NULL, &deviceListSize);
+     if(oclHandles.cl_status!=CL_SUCCESS){
+     throw(string("exception in _clInit -> clGetDeviceIDs -> ALL"));
+         }
     }
-    
+
    if (deviceListSize == 0)
         throw(string("InitCL()::Error: No devices found."));
-        
+
 #ifdef	DEV_INFO
-	std::cout<<"--cambine: number of device="<<deviceListSize<<std::endl;
+  std::cout<<"--cambine: number of device="<<deviceListSize<<std::endl;
 #endif
-	number_devices = deviceListSize;
-    // Now, allocate the device list 
-	//    oclHandles.devices = (cl_device_id *)malloc(deviceListSize);
+  number_devices = deviceListSize;
+    // Now, allocate the device list
+  //    oclHandles.devices = (cl_device_id *)malloc(deviceListSize);
     oclHandles.devices = (cl_device_id *)malloc(sizeof(cl_device_id)*deviceListSize);
 
     if (oclHandles.devices == 0)
         throw(string("InitCL()::Error: Could not allocate memory."));
 
-    // Next, get the device list data 
-	if(device_type.compare("")!=0){
-	   if(device_type.compare("cpu")==0){
-		   oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_CPU, deviceListSize, oclHandles.devices, NULL);
-		   if(oclHandles.cl_status!=CL_SUCCESS){
-		   		throw(string("exception in _clInit -> clGetDeviceIDs -> CPU ->2"));   		   	
-	   	   }
-	   }
+    // Next, get the device list data
+  if(device_type.compare("")!=0){
+     if(device_type.compare("cpu")==0){
+     oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_CPU, deviceListSize, oclHandles.devices, NULL);
+     if(oclHandles.cl_status!=CL_SUCCESS){
+     		throw(string("exception in _clInit -> clGetDeviceIDs -> CPU ->2"));
+     	   }
+     }
    	   if(device_type.compare("gpu")==0){
-		   oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_GPU, deviceListSize, oclHandles.devices, NULL);
-		   if(oclHandles.cl_status!=CL_SUCCESS){
-		   	throw(string("exception in _clInit -> clGetDeviceIDs -> GPU -> 2"));   		   	
-	   	   }
+     oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_GPU, deviceListSize, oclHandles.devices, NULL);
+     if(oclHandles.cl_status!=CL_SUCCESS){
+     	throw(string("exception in _clInit -> clGetDeviceIDs -> GPU -> 2"));
+     	   }
    	 	}
    	   if(device_type.compare("acc")==0){
-		   oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_ACCELERATOR, deviceListSize, oclHandles.devices, NULL);
-		   if(oclHandles.cl_status!=CL_SUCCESS){
-		   	throw(string("exception in _clInit -> clGetDeviceIDs -> ACCELERATOR -> 2"));   		   	
-	   	   }
-   	   }  	 	
+     oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_ACCELERATOR, deviceListSize, oclHandles.devices, NULL);
+     if(oclHandles.cl_status!=CL_SUCCESS){
+     	throw(string("exception in _clInit -> clGetDeviceIDs -> ACCELERATOR -> 2"));
+     	   }
+   	   }
    	}
    	else{
-	   	 oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_ALL, deviceListSize, oclHandles.devices, NULL);
-	   	 if(oclHandles.cl_status!=CL_SUCCESS){
-	   	 	throw(string("exception in _clInit -> clGetDeviceIDs -> ALL -> 2"));   		   	
+     	 oclHandles.cl_status = clGetDeviceIDs(targetPlatform, CL_DEVICE_TYPE_ALL, deviceListSize, oclHandles.devices, NULL);
+     	 if(oclHandles.cl_status!=CL_SUCCESS){
+     	 	throw(string("exception in _clInit -> clGetDeviceIDs -> ALL -> 2"));
    	   	}
    	}
    if(device_idx!=0){
@@ -418,18 +418,18 @@ void _clInit(int platform_idx, int device_idx, string device_type)throw(string){
    		throw(string("Invalidate device id"));
    	DEVICE_ID_INUSED = device_idx;
    }
-     
+
    	_clGetDeviceProperties(DEVICE_ID_INUSED, &prop);
-	std::cout<<"--cambine: device name="<<prop.device_name<<std::endl;
-	   	
+  std::cout<<"--cambine: device name="<<prop.device_name<<std::endl;
+
 #ifdef	DEV_INFO
-	std::cout<<"--cambine: return device list successfully!"<<std::endl;
-#endif  
- 
+  std::cout<<"--cambine: return device list successfully!"<<std::endl;
+#endif
+
     //-----------------------------------------------
     //--cambine-3: create an OpenCL context
 #ifdef	DEV_INFO
-	std::cout<<"--cambine: before creating context"<<std::endl;
+  std::cout<<"--cambine: before creating context"<<std::endl;
 #endif
 
     cl_device_type device_type_cl = (device_type == "gpu") ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
@@ -440,21 +440,21 @@ void _clInit(int platform_idx, int device_idx, string device_type)throw(string){
         exit(1);
     }
 #ifdef	DEV_INFO
-	std::cout<<"--cambine: create OCL context successfully!"<<std::endl;
+  std::cout<<"--cambine: create OCL context successfully!"<<std::endl;
 #endif
 
    //-----------------------------------------------
-   //--cambine-4: Create an OpenCL command queue    
-    oclHandles.queue = clCreateCommandQueue(oclHandles.context, 
-                                            oclHandles.devices[DEVICE_ID_INUSED], 
-                                            0, 
+   //--cambine-4: Create an OpenCL command queue
+    oclHandles.queue = clCreateCommandQueue(oclHandles.context,
+                                            oclHandles.devices[DEVICE_ID_INUSED],
+                                            0,
                                             &resultCL);
 
     if ((resultCL != CL_SUCCESS) || (oclHandles.queue == NULL))
         throw(string("InitCL()::Creating Command Queue. (clCreateCommandQueue)"));
 #ifdef PROFILE_
-	double t2 = gettime();
-	CC += t2 - t1;
+  double t2 = gettime();
+  CC += t2 - t1;
 #endif
     //-----------------------------------------------
     //--cambine-5: Load CL file, build CL program object, create CL kernel object
@@ -462,68 +462,68 @@ void _clInit(int platform_idx, int device_idx, string device_type)throw(string){
     const char * source    = source_str.c_str();
     size_t sourceSize[]    = { source_str.length() };
 
-    oclHandles.program = clCreateProgramWithSource(oclHandles.context, 
-                                                    1, 
+    oclHandles.program = clCreateProgramWithSource(oclHandles.context,
+                                                    1,
                                                     &source,
                                                     sourceSize,
                                                     &resultCL);
 
     if ((resultCL != CL_SUCCESS) || (oclHandles.program == NULL))
-        throw(string("InitCL()::Error: Loading Binary into cl_program. (clCreateProgramWithBinary)"));    
+        throw(string("InitCL()::Error: Loading Binary into cl_program. (clCreateProgramWithBinary)"));
     //insert debug information
     std::string options= "";
     //options += " -cl-nv-opt-level=3";
     resultCL = clBuildProgram(oclHandles.program, deviceListSize, oclHandles.devices, options.c_str(), NULL,  NULL);
-	
+
     if ((resultCL != CL_SUCCESS) || (oclHandles.program == NULL)){
         cerr << "InitCL()::Error: In clBuildProgram" << endl;
 
-		size_t length;
-        resultCL = clGetProgramBuildInfo(oclHandles.program, 
-                                        oclHandles.devices[DEVICE_ID_INUSED], 
-                                        CL_PROGRAM_BUILD_LOG, 
-                                        0, 
-                                        NULL, 
+  size_t length;
+        resultCL = clGetProgramBuildInfo(oclHandles.program,
+                                        oclHandles.devices[DEVICE_ID_INUSED],
+                                        CL_PROGRAM_BUILD_LOG,
+                                        0,
+                                        NULL,
                                         &length);
-        if(resultCL != CL_SUCCESS) 
+        if(resultCL != CL_SUCCESS)
             throw(string("InitCL()::Error: Getting Program build info(clGetProgramBuildInfo)"));
 
-		char* buffer = (char*)malloc(length);
-        resultCL = clGetProgramBuildInfo(oclHandles.program, 
-                                        oclHandles.devices[DEVICE_ID_INUSED], 
-                                        CL_PROGRAM_BUILD_LOG, 
-                                        length, 
-                                        buffer, 
+  char* buffer = (char*)malloc(length);
+        resultCL = clGetProgramBuildInfo(oclHandles.program,
+                                        oclHandles.devices[DEVICE_ID_INUSED],
+                                        CL_PROGRAM_BUILD_LOG,
+                                        length,
+                                        buffer,
                                         NULL);
-        if(resultCL != CL_SUCCESS) 
+        if(resultCL != CL_SUCCESS)
             throw(string("InitCL()::Error: Getting Program build info(clGetProgramBuildInfo)"));
 
-		cerr << buffer << endl;
-		FILE * fp = fopen("errinfo", "w");
-		fprintf(fp, "%s\n", buffer);
-		fclose(fp);
+  cerr << buffer << endl;
+  FILE * fp = fopen("errinfo", "w");
+  fprintf(fp, "%s\n", buffer);
+  fclose(fp);
         free(buffer);
 
         throw(string("InitCL()::Error: Building Program (clBuildProgram)"));
-    } 
+    }
 #ifdef PROFILE_
-	double t3 = gettime();
-	KC += t3 - t2;
+  double t3 = gettime();
+  KC += t3 - t2;
 #endif
     //get program information in intermediate representation
-#ifdef PTX_MSG    
+#ifdef PTX_MSG
     size_t binary_sizes[deviceListSize];
     char * binaries[deviceListSize];
-    //figure out number of devices and the sizes of the binary for each device. 
+    //figure out number of devices and the sizes of the binary for each device.
     oclHandles.cl_status = clGetProgramInfo(oclHandles.program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*deviceListSize, &binary_sizes, NULL );
     if(oclHandles.cl_status!=CL_SUCCESS){
         throw(string("--cambine:exception in _InitCL -> clGetProgramInfo-2"));
     }
 
     std::cout<<"--cambine:"<<binary_sizes<<std::endl;
-    //copy over all of the generated binaries. 
+    //copy over all of the generated binaries.
     for(int i=0;i<deviceListSize;i++)
-	binaries[i] = (char *)malloc( sizeof(char)*(binary_sizes[i]+1));
+  binaries[i] = (char *)malloc( sizeof(char)*(binary_sizes[i]+1));
     oclHandles.cl_status = clGetProgramInfo(oclHandles.program, CL_PROGRAM_BINARIES, sizeof(char *)*deviceListSize, binaries, NULL );
     if(oclHandles.cl_status!=CL_SUCCESS){
         throw(string("--cambine:exception in _InitCL -> clGetProgramInfo-3"));
@@ -533,18 +533,18 @@ void _clInit(int platform_idx, int device_idx, string device_type)throw(string){
     std::cout<<"--cambine:writing ptd information..."<<std::endl;
     FILE * ptx_file = fopen("cl.ptx","w");
     if(ptx_file==NULL){
-	throw(string("exceptions in allocate ptx file."));
+  throw(string("exceptions in allocate ptx file."));
     }
     fprintf(ptx_file,"%s",binaries[DEVICE_ID_INUSED]);
     fclose(ptx_file);
     std::cout<<"--cambine:writing ptd information done."<<std::endl;
     for(int i=0;i<deviceListSize;i++)
-	free(binaries[i]);
+  free(binaries[i]);
 #endif
 
     for (int nKernel = 0; nKernel < total_kernels; nKernel++)
     {
-        // get a kernel object handle for a kernel with the given name 
+        // get a kernel object handle for a kernel with the given name
         cl_kernel kernel = clCreateKernel(oclHandles.program,
                                             (kernel_names[nKernel]).c_str(),
                                             &resultCL);
@@ -563,35 +563,35 @@ void _clInit(int platform_idx, int device_idx, string device_type)throw(string){
     size_t ret_val_size;
     oclHandles.cl_status = clGetProgramBuildInfo(oclHandles.program, oclHandles.devices[DEVICE_ID_INUSED], CL_PROGRAM_BUILD_LOG, 0, NULL, &ret_val_size);
     if(oclHandles.cl_status!=CL_SUCCESS){
-		throw(string("exceptions in _InitCL -> getting resource information"));
-    }    
+  throw(string("exceptions in _InitCL -> getting resource information"));
+    }
 
     build_log = (char *)malloc(ret_val_size+1);
     oclHandles.cl_status = clGetProgramBuildInfo(oclHandles.program, oclHandles.devices[DEVICE_ID_INUSED], CL_PROGRAM_BUILD_LOG, ret_val_size, build_log, NULL);
     if(oclHandles.cl_status!=CL_SUCCESS){
-		throw(string("exceptions in _InitCL -> getting resources allocation information-2"));
+  throw(string("exceptions in _InitCL -> getting resources allocation information-2"));
     }
     build_log[ret_val_size] = '\0';
     std::cout<<"--cambine:"<<build_log<<std::endl;
     free(build_log);
 #endif
 #ifdef PROFILE_
-	double t4 = gettime();
-	CC += t4 - t3;
+  double t4 = gettime();
+  CC += t4 - t3;
 #endif
 }
 
 
 /*------------------------------------------------------------
-	@function:	release CL objects
-	@params:	NULL
-	@return:
-	@date:		24/03/2011
+  @function:	release CL objects
+  @params:	NULL
+  @return:
+  @date:		24/03/2011
 ------------------------------------------------------------*/
 void _clRelease()
 {
 #ifdef PROFILE_
-	double t1 = gettime();
+  double t1 = gettime();
 #endif
     bool errorFlag = false;
 
@@ -639,890 +639,890 @@ void _clRelease()
 
     if (errorFlag) throw(string("ReleaseCL()::Error encountered."));
 #ifdef PROFILE_
-	double t2 = gettime();
-	CR += t2 - t1;
+  double t2 = gettime();
+  CR += t2 - t1;
 #endif
 }
 
 /*------------------------------------------------------------
-	@function:	create read and write buffer for devices
-	@params:	
-		size: 	the size of device memory to be allocated
-	@return:	mem_d
-	@date:		24/03/2011
+  @function:	create read and write buffer for devices
+  @params:
+  size: 	the size of device memory to be allocated
+  @return:	mem_d
+  @date:		24/03/2011
 ------------------------------------------------------------*/
 cl_mem _clMalloc(int size) throw(string){
 #ifdef PROFILE_
-	double t1 = gettime();
+  double t1 = gettime();
 #endif
  	cl_mem d_mem;
-	d_mem = clCreateBuffer(oclHandles.context, CL_MEM_READ_WRITE, size, NULL, &oclHandles.cl_status);
+  d_mem = clCreateBuffer(oclHandles.context, CL_MEM_READ_WRITE, size, NULL, &oclHandles.cl_status);
 #ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS){
-	  oclHandles.error_str = "excpetion in _clMalloc -> ";
-	  switch(oclHandles.cl_status){
-	  	case CL_INVALID_CONTEXT:
-	  		oclHandles.error_str += "CL_INVALID_CONTEXT";
-	  		break;
-	  	case CL_INVALID_VALUE:
-	  		oclHandles.error_str += "CL_INVALID_VALUE";
-	  		break;
-	   	case CL_INVALID_BUFFER_SIZE:
-	  		oclHandles.error_str += "CL_INVALID_BUFFER_SIZE";
-	  		break;
-	  	case CL_INVALID_HOST_PTR:
-	  		oclHandles.error_str += "CL_INVALID_HOST_PTR";
-	  		break;
-	  	case CL_MEM_OBJECT_ALLOCATION_FAILURE:
-	  		oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
-	  		break;
-	  	case CL_OUT_OF_HOST_MEMORY:
-	  		oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-	  		break;
-	  	default:
-	  		oclHandles.error_str += "unknown reasons";
-	  		break;	  		
-	  }
-	throw(oclHandles.error_str);
-	}	
+  if(oclHandles.cl_status != CL_SUCCESS){
+    oclHandles.error_str = "excpetion in _clMalloc -> ";
+    switch(oclHandles.cl_status){
+    	case CL_INVALID_CONTEXT:
+    		oclHandles.error_str += "CL_INVALID_CONTEXT";
+    		break;
+    	case CL_INVALID_VALUE:
+    		oclHandles.error_str += "CL_INVALID_VALUE";
+    		break;
+     	case CL_INVALID_BUFFER_SIZE:
+    		oclHandles.error_str += "CL_INVALID_BUFFER_SIZE";
+    		break;
+    	case CL_INVALID_HOST_PTR:
+    		oclHandles.error_str += "CL_INVALID_HOST_PTR";
+    		break;
+    	case CL_MEM_OBJECT_ALLOCATION_FAILURE:
+    		oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
+    		break;
+    	case CL_OUT_OF_HOST_MEMORY:
+    		oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+    		break;
+    	default:
+    		oclHandles.error_str += "unknown reasons";
+    		break;
+    }
+  throw(oclHandles.error_str);
+  }
 #endif
 #ifdef PROFILE_
-	double t2 = gettime();
-	MA += t2 - t1;
+  double t2 = gettime();
+  MA += t2 - t1;
 #endif
-	return d_mem;
+  return d_mem;
 }
 /*------------------------------------------------------------
-	@function:	malloc pinned memoty
-	@params:
-		size: 	the size of data to be transferred in bytes
-	@return:	the pointer of host adress
-	@date:		06/04/2011
+  @function:	malloc pinned memoty
+  @params:
+  size: 	the size of data to be transferred in bytes
+  @return:	the pointer of host adress
+  @date:		06/04/2011
 ------------------------------------------------------------*/
 
 void* _clMallocHost(int size)throw(string){
-	void * mem_h;
-	oclHandles.pinned_mem_out = clCreateBuffer(oclHandles.context, CL_MEM_READ_WRITE|CL_MEM_ALLOC_HOST_PTR, size, NULL, &oclHandles.cl_status);
+  void * mem_h;
+  oclHandles.pinned_mem_out = clCreateBuffer(oclHandles.context, CL_MEM_READ_WRITE|CL_MEM_ALLOC_HOST_PTR, size, NULL, &oclHandles.cl_status);
 #ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS){
-	  oclHandles.error_str = "excpetion in _clMallocHost -> clCreateBuffer";
-	  switch(oclHandles.cl_status){
-	  	case CL_INVALID_CONTEXT:
-	  		oclHandles.error_str += "CL_INVALID_CONTEXT";
-	  		break;
-	  	case CL_INVALID_VALUE:
-	  		oclHandles.error_str += "CL_INVALID_VALUE";
-	  		break;
-	   	case CL_INVALID_BUFFER_SIZE:
-	  		oclHandles.error_str += "CL_INVALID_BUFFER_SIZE";
-	  		break;
-	  	case CL_INVALID_HOST_PTR:
-	  		oclHandles.error_str += "CL_INVALID_HOST_PTR";
-	  		break;
-	  	case CL_MEM_OBJECT_ALLOCATION_FAILURE:
-	  		oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
-	  		break;
-	  	case CL_OUT_OF_HOST_MEMORY:
-	  		oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-	  		break;
-	  	default:
-	  		oclHandles.error_str += "unknown reasons";
-	  		break;	  		
-	  }
-	throw(oclHandles.error_str);
-	}	
+  if(oclHandles.cl_status != CL_SUCCESS){
+    oclHandles.error_str = "excpetion in _clMallocHost -> clCreateBuffer";
+    switch(oclHandles.cl_status){
+    	case CL_INVALID_CONTEXT:
+    		oclHandles.error_str += "CL_INVALID_CONTEXT";
+    		break;
+    	case CL_INVALID_VALUE:
+    		oclHandles.error_str += "CL_INVALID_VALUE";
+    		break;
+     	case CL_INVALID_BUFFER_SIZE:
+    		oclHandles.error_str += "CL_INVALID_BUFFER_SIZE";
+    		break;
+    	case CL_INVALID_HOST_PTR:
+    		oclHandles.error_str += "CL_INVALID_HOST_PTR";
+    		break;
+    	case CL_MEM_OBJECT_ALLOCATION_FAILURE:
+    		oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
+    		break;
+    	case CL_OUT_OF_HOST_MEMORY:
+    		oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+    		break;
+    	default:
+    		oclHandles.error_str += "unknown reasons";
+    		break;
+    }
+  throw(oclHandles.error_str);
+  }
 #endif
 
-	mem_h = clEnqueueMapBuffer(oclHandles.queue, oclHandles.pinned_mem_out, CL_TRUE, CL_MAP_READ, 0, size, 0, NULL, NULL, &oclHandles.cl_status);
-	
+  mem_h = clEnqueueMapBuffer(oclHandles.queue, oclHandles.pinned_mem_out, CL_TRUE, CL_MAP_READ, 0, size, 0, NULL, NULL, &oclHandles.cl_status);
+
 #ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS||mem_h==NULL){
-	  oclHandles.error_str = "excpetion in _clMallocHost -> clEnqueueMapBuffer";
-	  switch(oclHandles.cl_status){
-	  	case CL_INVALID_COMMAND_QUEUE:
-	  		oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-	  		break;
-	  	case CL_INVALID_CONTEXT:
-	  		oclHandles.error_str += "CL_INVALID_CONTEXT";
-	  		break;
-	  	case CL_INVALID_MEM_OBJECT:
-	  		oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
-	  		break;
-	  	case CL_INVALID_VALUE:
-	  		oclHandles.error_str += "CL_INVALID_VALUE";
-	  		break;
-	   	case CL_INVALID_EVENT_WAIT_LIST:
-	  		oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
-	  		break;
-	  	case CL_MAP_FAILURE:
-	  		oclHandles.error_str += "CL_MAP_FAILURE";
-	  		break;
-	  	case CL_MEM_OBJECT_ALLOCATION_FAILURE:
-	  		oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
-	  		break;
-	  	case CL_OUT_OF_HOST_MEMORY:
-	  		oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-	  		break;
-	  	default:
-	  		oclHandles.error_str += "unknown reasons";
-	  		break;	  		
-	  }
-	throw(oclHandles.error_str);
-	}	
+  if(oclHandles.cl_status != CL_SUCCESS||mem_h==NULL){
+    oclHandles.error_str = "excpetion in _clMallocHost -> clEnqueueMapBuffer";
+    switch(oclHandles.cl_status){
+    	case CL_INVALID_COMMAND_QUEUE:
+    		oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+    		break;
+    	case CL_INVALID_CONTEXT:
+    		oclHandles.error_str += "CL_INVALID_CONTEXT";
+    		break;
+    	case CL_INVALID_MEM_OBJECT:
+    		oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
+    		break;
+    	case CL_INVALID_VALUE:
+    		oclHandles.error_str += "CL_INVALID_VALUE";
+    		break;
+     	case CL_INVALID_EVENT_WAIT_LIST:
+    		oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
+    		break;
+    	case CL_MAP_FAILURE:
+    		oclHandles.error_str += "CL_MAP_FAILURE";
+    		break;
+    	case CL_MEM_OBJECT_ALLOCATION_FAILURE:
+    		oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
+    		break;
+    	case CL_OUT_OF_HOST_MEMORY:
+    		oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+    		break;
+    	default:
+    		oclHandles.error_str += "unknown reasons";
+    		break;
+    }
+  throw(oclHandles.error_str);
+  }
 #endif
-	return mem_h;
+  return mem_h;
 }
 /*------------------------------------------------------------
-	@function:	free pinned memory
-	@params:
-		io:		to free pinned-in or pinned-out memory
-		mem_h: 	the host address
-	@return:	NULL
-	@date:		06/04/2011
+  @function:	free pinned memory
+  @params:
+  io:		to free pinned-in or pinned-out memory
+  mem_h: 	the host address
+  @return:	NULL
+  @date:		06/04/2011
 ------------------------------------------------------------*/
 void _clFreeHost(int io, void * mem_h){
-	if(io==0){	//in
-		if(mem_h){
-			oclHandles.cl_status = clEnqueueUnmapMemObject(oclHandles.queue, oclHandles.pinned_mem_in, (void*)mem_h, 0, NULL, NULL);
+  if(io==0){	//in
+  if(mem_h){
+  oclHandles.cl_status = clEnqueueUnmapMemObject(oclHandles.queue, oclHandles.pinned_mem_in, (void*)mem_h, 0, NULL, NULL);
 #ifdef ERRMSG
-			if(oclHandles.cl_status != CL_SUCCESS){
-			  oclHandles.error_str = "excpetion in _clFreeHost -> clEnqueueUnmapMemObject(in)";
-			  switch(oclHandles.cl_status){
-			  	case CL_INVALID_COMMAND_QUEUE:
-			  		oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-			  		break;
-			  	case CL_INVALID_MEM_OBJECT:
-			  		oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
-			  		break;
-			  	case CL_INVALID_VALUE:
-			  		oclHandles.error_str += "CL_INVALID_VALUE";
-			  		break;
-			   	case CL_OUT_OF_RESOURCES:
-			  		oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-			  		break;
-			  	case CL_OUT_OF_HOST_MEMORY:
-			  		oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-			  		break;
-			  	case CL_INVALID_CONTEXT:
-			  		oclHandles.error_str += "CL_INVALID_CONTEXT";
-			  		break;
-			  	default:
-			  		oclHandles.error_str += "unknown reasons";
-			  		break;	  		
-			  }
-				throw(oclHandles.error_str);
-			 }
+  if(oclHandles.cl_status != CL_SUCCESS){
+    oclHandles.error_str = "excpetion in _clFreeHost -> clEnqueueUnmapMemObject(in)";
+    switch(oclHandles.cl_status){
+    	case CL_INVALID_COMMAND_QUEUE:
+    		oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+    		break;
+    	case CL_INVALID_MEM_OBJECT:
+    		oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
+    		break;
+    	case CL_INVALID_VALUE:
+    		oclHandles.error_str += "CL_INVALID_VALUE";
+    		break;
+     	case CL_OUT_OF_RESOURCES:
+    		oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+    		break;
+    	case CL_OUT_OF_HOST_MEMORY:
+    		oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+    		break;
+    	case CL_INVALID_CONTEXT:
+    		oclHandles.error_str += "CL_INVALID_CONTEXT";
+    		break;
+    	default:
+    		oclHandles.error_str += "unknown reasons";
+    		break;
+    }
+  throw(oclHandles.error_str);
+   }
 #endif
-	 	}
-	}
-	else if(io==1){	//out
-			if(mem_h){
-				oclHandles.cl_status = clEnqueueUnmapMemObject(oclHandles.queue, oclHandles.pinned_mem_out, (void*)mem_h, 0, NULL, NULL);
-	#ifdef ERRMSG
-				if(oclHandles.cl_status != CL_SUCCESS){
-				  oclHandles.error_str = "excpetion in _clFreeHost -> clEnqueueUnmapMemObject(in)";
-				  switch(oclHandles.cl_status){
-				  	case CL_INVALID_COMMAND_QUEUE:
-				  		oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-				  		break;
-				  	case CL_INVALID_MEM_OBJECT:
-				  		oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
-				  		break;
-				  	case CL_INVALID_VALUE:
-				  		oclHandles.error_str += "CL_INVALID_VALUE";
-				  		break;
-				   	case CL_OUT_OF_RESOURCES:
-				  		oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-				  		break;
-				  	case CL_OUT_OF_HOST_MEMORY:
-				  		oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-				  		break;
-				  	case CL_INVALID_CONTEXT:
-				  		oclHandles.error_str += "CL_INVALID_CONTEXT";
-				  		break;
-				  	default:
-				  		oclHandles.error_str += "unknown reasons";
-				  		break;	  		
-				  }
-					throw(oclHandles.error_str);
-			   }
+   	}
+  }
+  else if(io==1){	//out
+  if(mem_h){
+  oclHandles.cl_status = clEnqueueUnmapMemObject(oclHandles.queue, oclHandles.pinned_mem_out, (void*)mem_h, 0, NULL, NULL);
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS){
+    oclHandles.error_str = "excpetion in _clFreeHost -> clEnqueueUnmapMemObject(in)";
+    switch(oclHandles.cl_status){
+    	case CL_INVALID_COMMAND_QUEUE:
+    		oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+    		break;
+    	case CL_INVALID_MEM_OBJECT:
+    		oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
+    		break;
+    	case CL_INVALID_VALUE:
+    		oclHandles.error_str += "CL_INVALID_VALUE";
+    		break;
+     	case CL_OUT_OF_RESOURCES:
+    		oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+    		break;
+    	case CL_OUT_OF_HOST_MEMORY:
+    		oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+    		break;
+    	case CL_INVALID_CONTEXT:
+    		oclHandles.error_str += "CL_INVALID_CONTEXT";
+    		break;
+    	default:
+    		oclHandles.error_str += "unknown reasons";
+    		break;
+    }
+  throw(oclHandles.error_str);
+     }
 #endif
-	 	 }		
-	}
-	else
-		throw(string("encounter invalid choice when freeing pinned memmory"));
+   	 }
+  }
+  else
+  throw(string("encounter invalid choice when freeing pinned memmory"));
 }
 /*------------------------------------------------------------
-	@function:	transfer data from host to device
-	@params:
-		dest:	the destination device memory
-		src:	the source host memory	
-		size: 	the size of data to be transferred in bytes
-	@return:	NULL
-	@date:		17/01/2011
+  @function:	transfer data from host to device
+  @params:
+  dest:	the destination device memory
+  src:	the source host memory
+  size: 	the size of data to be transferred in bytes
+  @return:	NULL
+  @date:		17/01/2011
 ------------------------------------------------------------*/
 void _clMemcpyH2D(cl_mem dst, const void *src, int size) throw(string){
 #ifdef PROFILE_
-	double t1 = gettime();
+  double t1 = gettime();
 #endif
-	oclHandles.cl_status = clEnqueueWriteBuffer(oclHandles.queue, dst, CL_TRUE, 0, size, src, 0, NULL, NULL);
+  oclHandles.cl_status = clEnqueueWriteBuffer(oclHandles.queue, dst, CL_TRUE, 0, size, src, 0, NULL, NULL);
 #ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS){
-		oclHandles.error_str = "excpetion in _clMemcpyH2D -> ";
-		switch(oclHandles.cl_status){
-			case CL_INVALID_COMMAND_QUEUE:
-				oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-				break;
-			case CL_INVALID_CONTEXT:
-				oclHandles.error_str += "CL_INVALID_CONTEXT";
-				break;	
-			case CL_INVALID_MEM_OBJECT:
-				oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
-				break;
-			case CL_INVALID_VALUE:
-				oclHandles.error_str += "CL_INVALID_VALUE";
-				break;	
-			case CL_INVALID_EVENT_WAIT_LIST:
-				oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
-				break;
-			case CL_MEM_OBJECT_ALLOCATION_FAILURE:
-				oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
-				break;	
-			case CL_OUT_OF_HOST_MEMORY:
-				oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-				break;		
-			default:
-				oclHandles.error_str += "Unknown reason";
-				break;
-		}		
-		throw(oclHandles.error_str);
-	}		
+  if(oclHandles.cl_status != CL_SUCCESS){
+  oclHandles.error_str = "excpetion in _clMemcpyH2D -> ";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_COMMAND_QUEUE:
+  oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+  break;
+  case CL_INVALID_CONTEXT:
+  oclHandles.error_str += "CL_INVALID_CONTEXT";
+  break;
+  case CL_INVALID_MEM_OBJECT:
+  oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
+  break;
+  case CL_INVALID_VALUE:
+  oclHandles.error_str += "CL_INVALID_VALUE";
+  break;
+  case CL_INVALID_EVENT_WAIT_LIST:
+  oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
+  break;
+  case CL_MEM_OBJECT_ALLOCATION_FAILURE:
+  oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unknown reason";
+  break;
+  }
+  throw(oclHandles.error_str);
+  }
 #endif
 #ifdef PROFILE_
-	double t2 = gettime();
-	H2D += t2 - t1;
+  double t2 = gettime();
+  H2D += t2 - t1;
 #endif
 }
 
 /*------------------------------------------------------------
-	@function:	transfer data from device to host
-	@params:
-		dest:	the destination device memory
-		src:	the source host memory	
-		size: 	the size of data to be transferred in bytes
-	@return:	NULL
-	@date:		17/01/2011
+  @function:	transfer data from device to host
+  @params:
+  dest:	the destination device memory
+  src:	the source host memory
+  size: 	the size of data to be transferred in bytes
+  @return:	NULL
+  @date:		17/01/2011
 ------------------------------------------------------------*/
 void _clMemcpyD2H(void * dst, cl_mem src, int size) throw(string){
 #ifdef PROFILE_
-	double t1 = gettime();
+  double t1 = gettime();
 #endif
-	oclHandles.cl_status = clEnqueueReadBuffer(oclHandles.queue, src, CL_TRUE, 0, size, dst, 0,0,0);
+  oclHandles.cl_status = clEnqueueReadBuffer(oclHandles.queue, src, CL_TRUE, 0, size, dst, 0,0,0);
 #ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS){
-		oclHandles.error_str = "excpetion in _clMemCpyD2H -> ";
-		switch(oclHandles.cl_status){
-			case CL_INVALID_COMMAND_QUEUE:
-				oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-				break;
-			case CL_INVALID_CONTEXT:
-				oclHandles.error_str += "CL_INVALID_CONTEXT";
-				break;	
-			case CL_INVALID_MEM_OBJECT:
-				oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
-				break;
-			case CL_INVALID_VALUE:
-				oclHandles.error_str += "CL_INVALID_VALUE";
-				break;	
-			case CL_INVALID_EVENT_WAIT_LIST:
-				oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
-				break;
-			case CL_MEM_OBJECT_ALLOCATION_FAILURE:
-				oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
-				break;	
-			case CL_OUT_OF_HOST_MEMORY:
-				oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-				break;		
-			default:
-				oclHandles.error_str += "Unknown reason";
-				break;
-		}		
-		throw(oclHandles.error_str);
-	}		
+  if(oclHandles.cl_status != CL_SUCCESS){
+  oclHandles.error_str = "excpetion in _clMemCpyD2H -> ";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_COMMAND_QUEUE:
+  oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+  break;
+  case CL_INVALID_CONTEXT:
+  oclHandles.error_str += "CL_INVALID_CONTEXT";
+  break;
+  case CL_INVALID_MEM_OBJECT:
+  oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
+  break;
+  case CL_INVALID_VALUE:
+  oclHandles.error_str += "CL_INVALID_VALUE";
+  break;
+  case CL_INVALID_EVENT_WAIT_LIST:
+  oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
+  break;
+  case CL_MEM_OBJECT_ALLOCATION_FAILURE:
+  oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unknown reason";
+  break;
+  }
+  throw(oclHandles.error_str);
+  }
 #endif
 #ifdef PROFILE_
-	double t2 = gettime();
-	D2H += t2 - t1;
+  double t2 = gettime();
+  D2H += t2 - t1;
 #endif
 }
 /*------------------------------------------------------------
-	@function:	transfer data from device to device
-	@params:
-		dest:	the destination device memory
-		src:	the source device memory	
-		size: 	the size of data to be transferred in bytes
-	@return:	NULL
-	@date:		27/03/2011
+  @function:	transfer data from device to device
+  @params:
+  dest:	the destination device memory
+  src:	the source device memory
+  size: 	the size of data to be transferred in bytes
+  @return:	NULL
+  @date:		27/03/2011
 ------------------------------------------------------------*/
 void _clMemcpyD2D(cl_mem dst, cl_mem src, int size) throw(string){
 #ifdef PROFILE_
-	double t1 = gettime();
+  double t1 = gettime();
 #endif
-	oclHandles.cl_status = clEnqueueCopyBuffer(oclHandles.queue, src, dst, 0, 0, size, 0, NULL, NULL);
+  oclHandles.cl_status = clEnqueueCopyBuffer(oclHandles.queue, src, dst, 0, 0, size, 0, NULL, NULL);
 #ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS){
-		oclHandles.error_str = "excpetion in _clCpyMemD2D -> ";
-		switch(oclHandles.cl_status){
-			case CL_INVALID_COMMAND_QUEUE:
-				oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-				break;
-			case CL_INVALID_CONTEXT:
-				oclHandles.error_str += "CL_INVALID_CONTEXT";
-				break;	
-			case CL_INVALID_MEM_OBJECT:
-				oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
-				break;
-			case CL_INVALID_VALUE:
-				oclHandles.error_str += "CL_INVALID_VALUE";
-				break;	
-			case CL_INVALID_EVENT_WAIT_LIST:
-				oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
-				break;
-			case CL_MISALIGNED_SUB_BUFFER_OFFSET:
-				oclHandles.error_str += "CL_MISALIGNED_SUB_BUFFER_OFFSET";
-				break;	
-			case CL_MEM_COPY_OVERLAP:
-				oclHandles.error_str += "CL_MEM_COPY_OVERLAP";
-				break;	
-			case CL_MEM_OBJECT_ALLOCATION_FAILURE:
-				oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
-				break;	
-			case CL_OUT_OF_RESOURCES:
-				oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-				break;	
-			case CL_OUT_OF_HOST_MEMORY:
-				oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-				break;
-			default:
-				oclHandles.error_str += "Unknown reason";
-				break;
-		}		
-		throw(oclHandles.error_str);
-	}		
+  if(oclHandles.cl_status != CL_SUCCESS){
+  oclHandles.error_str = "excpetion in _clCpyMemD2D -> ";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_COMMAND_QUEUE:
+  oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+  break;
+  case CL_INVALID_CONTEXT:
+  oclHandles.error_str += "CL_INVALID_CONTEXT";
+  break;
+  case CL_INVALID_MEM_OBJECT:
+  oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
+  break;
+  case CL_INVALID_VALUE:
+  oclHandles.error_str += "CL_INVALID_VALUE";
+  break;
+  case CL_INVALID_EVENT_WAIT_LIST:
+  oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
+  break;
+  case CL_MISALIGNED_SUB_BUFFER_OFFSET:
+  oclHandles.error_str += "CL_MISALIGNED_SUB_BUFFER_OFFSET";
+  break;
+  case CL_MEM_COPY_OVERLAP:
+  oclHandles.error_str += "CL_MEM_COPY_OVERLAP";
+  break;
+  case CL_MEM_OBJECT_ALLOCATION_FAILURE:
+  oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unknown reason";
+  break;
+  }
+  throw(oclHandles.error_str);
+  }
 #endif
 #ifdef PROFILE_
-	double t2 = gettime();
-	D2D += t2 - t1;
+  double t2 = gettime();
+  D2D += t2 - t1;
 #endif
 }
 
 /*------------------------------------------------------------
-	@function:	set kernel arguments
-	@params:
-		kernel_id:	the index of kernel to set
-		arg_idx:	the index of argument
-		d_mem: 		the variable of device memory
-		size:		the size of device memory
-	@return:	NULL
-	@date:		03/04/2011
+  @function:	set kernel arguments
+  @params:
+  kernel_id:	the index of kernel to set
+  arg_idx:	the index of argument
+  d_mem: 		the variable of device memory
+  size:		the size of device memory
+  @return:	NULL
+  @date:		03/04/2011
 ------------------------------------------------------------*/
 void _clSetArgs(int kernel_id, int arg_idx, void * d_mem, int size = 0) throw(string){
-	if(!size){ // normal device memory object
-		oclHandles.cl_status = clSetKernelArg(oclHandles.kernel[kernel_id], arg_idx, sizeof(d_mem), &d_mem);
+  if(!size){ // normal device memory object
+  oclHandles.cl_status = clSetKernelArg(oclHandles.kernel[kernel_id], arg_idx, sizeof(d_mem), &d_mem);
 #ifdef ERRMSG
-		if(oclHandles.cl_status != CL_SUCCESS){
-			oclHandles.error_str = "excpetion in _clSetKernelArg() ";
-			switch(oclHandles.cl_status){
-				case CL_INVALID_KERNEL:
-					oclHandles.error_str += "CL_INVALID_KERNEL";
-					break;
-				case CL_INVALID_ARG_INDEX:
-					oclHandles.error_str += "CL_INVALID_ARG_INDEX";
-					break;	
-				case CL_INVALID_ARG_VALUE:
-					oclHandles.error_str += "CL_INVALID_ARG_VALUE";
-					break;
-				case CL_INVALID_MEM_OBJECT:
-					oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
-					break;	
-				case CL_INVALID_SAMPLER:
-					oclHandles.error_str += "CL_INVALID_SAMPLER";
-					break;
-				case CL_INVALID_ARG_SIZE:
-					oclHandles.error_str += "CL_INVALID_ARG_SIZE";
-					break;	
-				case CL_OUT_OF_RESOURCES:
-					oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-					break;
-				case CL_OUT_OF_HOST_MEMORY:
-					oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-					break;
-				default:
-					oclHandles.error_str += "Unknown reason";
-					break;
-			}
-			throw(oclHandles.error_str);
-		}
+  if(oclHandles.cl_status != CL_SUCCESS){
+  oclHandles.error_str = "excpetion in _clSetKernelArg() ";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_KERNEL:
+  oclHandles.error_str += "CL_INVALID_KERNEL";
+  break;
+  case CL_INVALID_ARG_INDEX:
+  oclHandles.error_str += "CL_INVALID_ARG_INDEX";
+  break;
+  case CL_INVALID_ARG_VALUE:
+  oclHandles.error_str += "CL_INVALID_ARG_VALUE";
+  break;
+  case CL_INVALID_MEM_OBJECT:
+  oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
+  break;
+  case CL_INVALID_SAMPLER:
+  oclHandles.error_str += "CL_INVALID_SAMPLER";
+  break;
+  case CL_INVALID_ARG_SIZE:
+  oclHandles.error_str += "CL_INVALID_ARG_SIZE";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unknown reason";
+  break;
+  }
+  throw(oclHandles.error_str);
+  }
 #endif
-	}
-	else{	//special device object:(1) local memory; (2) single word
-		oclHandles.cl_status = clSetKernelArg(oclHandles.kernel[kernel_id], arg_idx, size, d_mem);
+  }
+  else{	//special device object:(1) local memory; (2) single word
+  oclHandles.cl_status = clSetKernelArg(oclHandles.kernel[kernel_id], arg_idx, size, d_mem);
 #ifdef ERRMSG
-		if(oclHandles.cl_status != CL_SUCCESS){
-				oclHandles.error_str = "excpetion in _clSetKernelArg() ";
-			switch(oclHandles.cl_status){
-				case CL_INVALID_KERNEL:
-					oclHandles.error_str += "CL_INVALID_KERNEL";
-					break;
-				case CL_INVALID_ARG_INDEX:
-					oclHandles.error_str += "CL_INVALID_ARG_INDEX";
-					break;	
-				case CL_INVALID_ARG_VALUE:
-					oclHandles.error_str += "CL_INVALID_ARG_VALUE";
-					break;
-				case CL_INVALID_MEM_OBJECT:
-					oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
-					break;	
-				case CL_INVALID_SAMPLER:
-					oclHandles.error_str += "CL_INVALID_SAMPLER";
-					break;
-				case CL_INVALID_ARG_SIZE:
-					oclHandles.error_str += "CL_INVALID_ARG_SIZE";
-					break;	
-				case CL_OUT_OF_RESOURCES:
-					oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-					break;
-				case CL_OUT_OF_HOST_MEMORY:
-					oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-					break;
-				default:
-					oclHandles.error_str += "Unknown reason";
-					break;
-			}
-			throw(oclHandles.error_str);
-		}
+  if(oclHandles.cl_status != CL_SUCCESS){
+  oclHandles.error_str = "excpetion in _clSetKernelArg() ";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_KERNEL:
+  oclHandles.error_str += "CL_INVALID_KERNEL";
+  break;
+  case CL_INVALID_ARG_INDEX:
+  oclHandles.error_str += "CL_INVALID_ARG_INDEX";
+  break;
+  case CL_INVALID_ARG_VALUE:
+  oclHandles.error_str += "CL_INVALID_ARG_VALUE";
+  break;
+  case CL_INVALID_MEM_OBJECT:
+  oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
+  break;
+  case CL_INVALID_SAMPLER:
+  oclHandles.error_str += "CL_INVALID_SAMPLER";
+  break;
+  case CL_INVALID_ARG_SIZE:
+  oclHandles.error_str += "CL_INVALID_ARG_SIZE";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unknown reason";
+  break;
+  }
+  throw(oclHandles.error_str);
+  }
 #endif
-	}
+  }
 }
 void _clFinish() throw(string){
-	oclHandles.cl_status = clFinish(oclHandles.queue);	
+  oclHandles.cl_status = clFinish(oclHandles.queue);
 #ifdef ERRMSG
-	if(oclHandles.cl_status!=CL_SUCCESS){
-		oclHandles.error_str = "excpetion in _clFinish";
-		switch(oclHandles.cl_status){
-			case CL_INVALID_COMMAND_QUEUE:
-				oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-				break;
-			case CL_OUT_OF_RESOURCES:
-				oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-				break;		
-			case CL_OUT_OF_HOST_MEMORY:
-				oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-				break;
-			default:
-				oclHandles.error_str += "Unknown reasons";
-				break;
-		}
-		throw(oclHandles.error_str);
-	}
+  if(oclHandles.cl_status!=CL_SUCCESS){
+  oclHandles.error_str = "excpetion in _clFinish";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_COMMAND_QUEUE:
+  oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unknown reasons";
+  break;
+  }
+  throw(oclHandles.error_str);
+  }
 #endif
 }
 /*------------------------------------------------------------
-	@function:	entry of invoke the kernel function
-	@params:
-		kernel_id:	the index of kernel to set
-		work_items:	the number of working items
-		work_group_size: the size of each work group
-	@return:	NULL
-	@date:		03/04/2011
+  @function:	entry of invoke the kernel function
+  @params:
+  kernel_id:	the index of kernel to set
+  work_items:	the number of working items
+  work_group_size: the size of each work group
+  @return:	NULL
+  @date:		03/04/2011
 ------------------------------------------------------------*/
 void _clInvokeKernel(int kernel_id, int work_items, int work_group_size) throw(string){
 #ifdef PROFILE_
-	double t1 = gettime();
+  double t1 = gettime();
 #endif
-	cl_uint work_dim = WORK_DIM;
-	cl_event e[1];
-	if(work_items%work_group_size != 0)	//process situations that work_items cannot be divided by work_group_size
-	  work_items = work_items + (work_group_size-(work_items%work_group_size));
+  cl_uint work_dim = WORK_DIM;
+  cl_event e[1];
+  if(work_items%work_group_size != 0)	//process situations that work_items cannot be divided by work_group_size
+    work_items = work_items + (work_group_size-(work_items%work_group_size));
   	size_t local_work_size[] = {work_group_size, 1};
-	size_t global_work_size[] = {work_items, 1};
-	oclHandles.cl_status = clEnqueueNDRangeKernel(oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0, \
-											global_work_size, local_work_size, 0 , 0, &(e[0]) );	
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS){
-		oclHandles.error_str = "excpetion in _clInvokeKernel() -> ";
-		switch(oclHandles.cl_status){
-			case CL_INVALID_PROGRAM_EXECUTABLE:
-				oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE";
-				break;
-			case CL_INVALID_COMMAND_QUEUE:
-				oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-				break;
-			case CL_INVALID_KERNEL:
-				oclHandles.error_str += "CL_INVALID_KERNEL";
-				break;
-			case CL_INVALID_CONTEXT:
-				oclHandles.error_str += "CL_INVALID_CONTEXT";
-				break;
-			case CL_INVALID_KERNEL_ARGS:
-				oclHandles.error_str += "CL_INVALID_KERNEL_ARGS";
-				break;
-			case CL_INVALID_WORK_DIMENSION:
-				oclHandles.error_str += "CL_INVALID_WORK_DIMENSION";
-				break;
-			case CL_INVALID_GLOBAL_WORK_SIZE:
-				oclHandles.error_str += "CL_INVALID_GLOBAL_WORK_SIZE";
-				break;
-			case CL_INVALID_WORK_GROUP_SIZE:
-				oclHandles.error_str += "CL_INVALID_WORK_GROUP_SIZE";
-				break;
-			case CL_INVALID_WORK_ITEM_SIZE:
-				oclHandles.error_str += "CL_INVALID_WORK_ITEM_SIZE";
-				break;
-			case CL_INVALID_GLOBAL_OFFSET:
-				oclHandles.error_str += "CL_INVALID_GLOBAL_OFFSET";
-				break;
-			case CL_OUT_OF_RESOURCES:
-				oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-				break;
-			case CL_MEM_OBJECT_ALLOCATION_FAILURE:
-				oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
-				break;
-			case CL_INVALID_EVENT_WAIT_LIST:
-				oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
-				break;
-			case CL_OUT_OF_HOST_MEMORY:
-				oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-				break;
-			default: 
-				oclHandles.error_str += "Unkown reseason";
-				break;		
-		}
-	
-		throw(oclHandles.error_str);	
-	}
-	#endif
-	//_clFinish();
-	oclHandles.cl_status = clWaitForEvents(1, &e[0]);
-	#ifdef ERRMSG
+  size_t global_work_size[] = {work_items, 1};
+  oclHandles.cl_status = clEnqueueNDRangeKernel(oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0, \
+  global_work_size, local_work_size, 0 , 0, &(e[0]) );
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS){
+  oclHandles.error_str = "excpetion in _clInvokeKernel() -> ";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_PROGRAM_EXECUTABLE:
+  oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE";
+  break;
+  case CL_INVALID_COMMAND_QUEUE:
+  oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+  break;
+  case CL_INVALID_KERNEL:
+  oclHandles.error_str += "CL_INVALID_KERNEL";
+  break;
+  case CL_INVALID_CONTEXT:
+  oclHandles.error_str += "CL_INVALID_CONTEXT";
+  break;
+  case CL_INVALID_KERNEL_ARGS:
+  oclHandles.error_str += "CL_INVALID_KERNEL_ARGS";
+  break;
+  case CL_INVALID_WORK_DIMENSION:
+  oclHandles.error_str += "CL_INVALID_WORK_DIMENSION";
+  break;
+  case CL_INVALID_GLOBAL_WORK_SIZE:
+  oclHandles.error_str += "CL_INVALID_GLOBAL_WORK_SIZE";
+  break;
+  case CL_INVALID_WORK_GROUP_SIZE:
+  oclHandles.error_str += "CL_INVALID_WORK_GROUP_SIZE";
+  break;
+  case CL_INVALID_WORK_ITEM_SIZE:
+  oclHandles.error_str += "CL_INVALID_WORK_ITEM_SIZE";
+  break;
+  case CL_INVALID_GLOBAL_OFFSET:
+  oclHandles.error_str += "CL_INVALID_GLOBAL_OFFSET";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_MEM_OBJECT_ALLOCATION_FAILURE:
+  oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
+  break;
+  case CL_INVALID_EVENT_WAIT_LIST:
+  oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unkown reseason";
+  break;
+  }
+
+  throw(oclHandles.error_str);
+  }
+  #endif
+  //_clFinish();
+  oclHandles.cl_status = clWaitForEvents(1, &e[0]);
+  #ifdef ERRMSG
     if (oclHandles.cl_status!= CL_SUCCESS){
     	oclHandles.error_str = "excpetion in _clEnqueueNDRange() -> clWaitForEvents ->";
    		switch(oclHandles.cl_status){
     	case CL_INVALID_VALUE:
-			oclHandles.error_str += "CL_INVALID_VALUE";
-			break;
-		case CL_INVALID_CONTEXT:
-			oclHandles.error_str += "CL_INVALID_CONTEXT";
-			break;
-		case CL_INVALID_EVENT:
-			oclHandles.error_str += "CL_INVALID_EVENT";
-			break;
-		case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST:
-			oclHandles.error_str += "CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST";
-			break;
-		case CL_OUT_OF_RESOURCES:
-			oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-			break;
-		case CL_OUT_OF_HOST_MEMORY:
-			oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-			break;
-		default:
-			oclHandles.error_str += "Unkown Reason";
-			break;
+  oclHandles.error_str += "CL_INVALID_VALUE";
+  break;
+  case CL_INVALID_CONTEXT:
+  oclHandles.error_str += "CL_INVALID_CONTEXT";
+  break;
+  case CL_INVALID_EVENT:
+  oclHandles.error_str += "CL_INVALID_EVENT";
+  break;
+  case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST:
+  oclHandles.error_str += "CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unkown Reason";
+  break;
     	}
     	throw(oclHandles.error_str);
     }
     #endif
 #ifdef PROFILE_
-	double t2 = gettime();
-	KE += t2 - t1;
+  double t2 = gettime();
+  KE += t2 - t1;
 #endif
 }
 
 /*------------------------------------------------------------
-	@function:	set device memory in an easy manner
-	@params:	
-		mem_d: the device memory to be set;
-		val:   set the selected memory to 'val';
-		number_elements:	the number of elements in the selected memory
-	@return:	NULL
-	@date:		03/04/2011
+  @function:	set device memory in an easy manner
+  @params:
+  mem_d: the device memory to be set;
+  val:   set the selected memory to 'val';
+  number_elements:	the number of elements in the selected memory
+  @return:	NULL
+  @date:		03/04/2011
 ------------------------------------------------------------*/
 
 void _clMemset(cl_mem mem_d, short val, int number_bytes)throw(string){
 #ifdef PROFILE_
-	double t1 = gettime();
+  double t1 = gettime();
 #endif
-	int kernel_id = 0;
-	int arg_idx = 0;
-	_clSetArgs(kernel_id, arg_idx++, mem_d);
-	_clSetArgs(kernel_id, arg_idx++, &val, sizeof(short));
-	_clSetArgs(kernel_id, arg_idx++, &number_bytes, sizeof(int));	
-	cl_uint work_dim = WORK_DIM;
-	int work_items = number_bytes;
-	cl_event e[1];
-	if(work_items%work_group_size != 0)	//process situations that work_items cannot be divided by work_group_size
-	  work_items = work_items + (work_group_size-(work_items%work_group_size));
+  int kernel_id = 0;
+  int arg_idx = 0;
+  _clSetArgs(kernel_id, arg_idx++, mem_d);
+  _clSetArgs(kernel_id, arg_idx++, &val, sizeof(short));
+  _clSetArgs(kernel_id, arg_idx++, &number_bytes, sizeof(int));
+  cl_uint work_dim = WORK_DIM;
+  int work_items = number_bytes;
+  cl_event e[1];
+  if(work_items%work_group_size != 0)	//process situations that work_items cannot be divided by work_group_size
+    work_items = work_items + (work_group_size-(work_items%work_group_size));
   	size_t local_work_size[] = {work_group_size, 1};
-	size_t global_work_size[] = {work_items, 1};
-	oclHandles.cl_status = clEnqueueNDRangeKernel(oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0, \
-											global_work_size, local_work_size, 0 , 0, &(e[0]) );	
+  size_t global_work_size[] = {work_items, 1};
+  oclHandles.cl_status = clEnqueueNDRangeKernel(oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0, \
+  global_work_size, local_work_size, 0 , 0, &(e[0]) );
 #ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS){
-		oclHandles.error_str = "excpetion in _clMemset() -> ";
-		switch(oclHandles.cl_status){
-			case CL_INVALID_PROGRAM_EXECUTABLE:
-				oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE";
-				break;
-			case CL_INVALID_COMMAND_QUEUE:
-				oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-				break;
-			case CL_INVALID_KERNEL:
-				oclHandles.error_str += "CL_INVALID_KERNEL";
-				break;
-			case CL_INVALID_CONTEXT:
-				oclHandles.error_str += "CL_INVALID_CONTEXT";
-				break;
-			case CL_INVALID_KERNEL_ARGS:
-				oclHandles.error_str += "CL_INVALID_KERNEL_ARGS";
-				break;
-			case CL_INVALID_WORK_DIMENSION:
-				oclHandles.error_str += "CL_INVALID_WORK_DIMENSION";
-				break;
-			case CL_INVALID_GLOBAL_WORK_SIZE:
-				oclHandles.error_str += "CL_INVALID_GLOBAL_WORK_SIZE";
-				break;
-			case CL_INVALID_WORK_GROUP_SIZE:
-				oclHandles.error_str += "CL_INVALID_WORK_GROUP_SIZE";
-				break;
-			case CL_INVALID_WORK_ITEM_SIZE:
-				oclHandles.error_str += "CL_INVALID_WORK_ITEM_SIZE";
-				break;
-			case CL_INVALID_GLOBAL_OFFSET:
-				oclHandles.error_str += "CL_INVALID_GLOBAL_OFFSET";
-				break;
-			case CL_OUT_OF_RESOURCES:
-				oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-				break;
-			case CL_MEM_OBJECT_ALLOCATION_FAILURE:
-				oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
-				break;
-			case CL_INVALID_EVENT_WAIT_LIST:
-				oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
-				break;
-			case CL_OUT_OF_HOST_MEMORY:
-				oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-				break;
-			default: 
-				oclHandles.error_str += "Unkown reseason";
-				break;		
-		}
-	
-		throw(oclHandles.error_str);	
-	}
+  if(oclHandles.cl_status != CL_SUCCESS){
+  oclHandles.error_str = "excpetion in _clMemset() -> ";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_PROGRAM_EXECUTABLE:
+  oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE";
+  break;
+  case CL_INVALID_COMMAND_QUEUE:
+  oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+  break;
+  case CL_INVALID_KERNEL:
+  oclHandles.error_str += "CL_INVALID_KERNEL";
+  break;
+  case CL_INVALID_CONTEXT:
+  oclHandles.error_str += "CL_INVALID_CONTEXT";
+  break;
+  case CL_INVALID_KERNEL_ARGS:
+  oclHandles.error_str += "CL_INVALID_KERNEL_ARGS";
+  break;
+  case CL_INVALID_WORK_DIMENSION:
+  oclHandles.error_str += "CL_INVALID_WORK_DIMENSION";
+  break;
+  case CL_INVALID_GLOBAL_WORK_SIZE:
+  oclHandles.error_str += "CL_INVALID_GLOBAL_WORK_SIZE";
+  break;
+  case CL_INVALID_WORK_GROUP_SIZE:
+  oclHandles.error_str += "CL_INVALID_WORK_GROUP_SIZE";
+  break;
+  case CL_INVALID_WORK_ITEM_SIZE:
+  oclHandles.error_str += "CL_INVALID_WORK_ITEM_SIZE";
+  break;
+  case CL_INVALID_GLOBAL_OFFSET:
+  oclHandles.error_str += "CL_INVALID_GLOBAL_OFFSET";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_MEM_OBJECT_ALLOCATION_FAILURE:
+  oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
+  break;
+  case CL_INVALID_EVENT_WAIT_LIST:
+  oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unkown reseason";
+  break;
+  }
+
+  throw(oclHandles.error_str);
+  }
 #endif
-	//_clFinish();
-	oclHandles.cl_status = clWaitForEvents(1, &e[0]);
+  //_clFinish();
+  oclHandles.cl_status = clWaitForEvents(1, &e[0]);
 #ifdef ERRMSG
     if (oclHandles.cl_status!= CL_SUCCESS){
     	oclHandles.error_str = "excpetion in _clMemset() -> clWaitForEvents ->";
    		switch(oclHandles.cl_status){
     	case CL_INVALID_VALUE:
-			oclHandles.error_str += "CL_INVALID_VALUE";
-			break;
-		case CL_INVALID_CONTEXT:
-			oclHandles.error_str += "CL_INVALID_CONTEXT";
-			break;
-		case CL_INVALID_EVENT:
-			oclHandles.error_str += "CL_INVALID_EVENT";
-			break;
-		case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST:
-			oclHandles.error_str += "CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST";
-			break;
-		case CL_OUT_OF_RESOURCES:
-			oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-			break;
-		case CL_OUT_OF_HOST_MEMORY:
-			oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-			break;
-		default:
-			oclHandles.error_str += "Unkown Reason";
-			break;
+  oclHandles.error_str += "CL_INVALID_VALUE";
+  break;
+  case CL_INVALID_CONTEXT:
+  oclHandles.error_str += "CL_INVALID_CONTEXT";
+  break;
+  case CL_INVALID_EVENT:
+  oclHandles.error_str += "CL_INVALID_EVENT";
+  break;
+  case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST:
+  oclHandles.error_str += "CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unkown Reason";
+  break;
     	}
     	throw(oclHandles.error_str);
     }
 #endif
 #ifdef PROFILE_
-	double t2 = gettime();
-	H2D += t2 - t1;
+  double t2 = gettime();
+  H2D += t2 - t1;
 #endif
 }
 /*------------------------------------------------------------
-	@function:	entry of invoke the kernel function using 2d working items
-	@params:
-		kernel_id:	the index of kernel to set
-		range_x:	the number of working items in x direction
-		range_y:	the number of working items in y direction
-		group_x:	the number of working items in each work group in x direction
-		group_y:	the number of working items	in each work group in y direction	 
-	@return:	NULL
-	@date:		03/04/2011
+  @function:	entry of invoke the kernel function using 2d working items
+  @params:
+  kernel_id:	the index of kernel to set
+  range_x:	the number of working items in x direction
+  range_y:	the number of working items in y direction
+  group_x:	the number of working items in each work group in x direction
+  group_y:	the number of working items	in each work group in y direction
+  @return:	NULL
+  @date:		03/04/2011
 ------------------------------------------------------------*/
 void _clInvokeKernel2D(int kernel_id, int range_x, int range_y, int group_x, int group_y) throw(string){
 #ifdef PROFILE_
-	double t1 = gettime();
+  double t1 = gettime();
 #endif
-	cl_uint work_dim = WORK_DIM;
-	size_t local_work_size[] = {group_x, group_y};
-	size_t global_work_size[] = {range_x, range_y};
-	cl_event e[1];
-	/*if(work_items%work_group_size != 0)	//process situations that work_items cannot be divided by work_group_size
-	  work_items = work_items + (work_group_size-(work_items%work_group_size));*/
-	oclHandles.cl_status = clEnqueueNDRangeKernel(oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0, \
-											global_work_size, local_work_size, 0 , 0, &(e[0]) );	
-	#ifdef ERRMSG
-	if(oclHandles.cl_status != CL_SUCCESS){
-		oclHandles.error_str = "excpetion in _clInvokeKernel() -> ";
-		switch(oclHandles.cl_status){
-			case CL_INVALID_PROGRAM_EXECUTABLE:
-				oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE";
-				break;
-			case CL_INVALID_COMMAND_QUEUE:
-				oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
-				break;
-			case CL_INVALID_KERNEL:
-				oclHandles.error_str += "CL_INVALID_KERNEL";
-				break;
-			case CL_INVALID_CONTEXT:
-				oclHandles.error_str += "CL_INVALID_CONTEXT";
-				break;
-			case CL_INVALID_KERNEL_ARGS:
-				oclHandles.error_str += "CL_INVALID_KERNEL_ARGS";
-				break;
-			case CL_INVALID_WORK_DIMENSION:
-				oclHandles.error_str += "CL_INVALID_WORK_DIMENSION";
-				break;
-			case CL_INVALID_GLOBAL_WORK_SIZE:
-				oclHandles.error_str += "CL_INVALID_GLOBAL_WORK_SIZE";
-				break;
-			case CL_INVALID_WORK_GROUP_SIZE:
-				oclHandles.error_str += "CL_INVALID_WORK_GROUP_SIZE";
-				break;
-			case CL_INVALID_WORK_ITEM_SIZE:
-				oclHandles.error_str += "CL_INVALID_WORK_ITEM_SIZE";
-				break;
-			case CL_INVALID_GLOBAL_OFFSET:
-				oclHandles.error_str += "CL_INVALID_GLOBAL_OFFSET";
-				break;
-			case CL_OUT_OF_RESOURCES:
-				oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-				break;
-			case CL_MEM_OBJECT_ALLOCATION_FAILURE:
-				oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
-				break;
-			case CL_INVALID_EVENT_WAIT_LIST:
-				oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
-				break;
-			case CL_OUT_OF_HOST_MEMORY:
-				oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-				break;
-			default: 
-				oclHandles.error_str += "Unkown reseason";
-				break;		
-		}	
-		throw(oclHandles.error_str);	
-	}
-	#endif
-		
-	oclHandles.cl_status = clWaitForEvents(1, &e[0]);
+  cl_uint work_dim = WORK_DIM;
+  size_t local_work_size[] = {group_x, group_y};
+  size_t global_work_size[] = {range_x, range_y};
+  cl_event e[1];
+  /*if(work_items%work_group_size != 0)	//process situations that work_items cannot be divided by work_group_size
+    work_items = work_items + (work_group_size-(work_items%work_group_size));*/
+  oclHandles.cl_status = clEnqueueNDRangeKernel(oclHandles.queue, oclHandles.kernel[kernel_id], work_dim, 0, \
+  global_work_size, local_work_size, 0 , 0, &(e[0]) );
+  #ifdef ERRMSG
+  if(oclHandles.cl_status != CL_SUCCESS){
+  oclHandles.error_str = "excpetion in _clInvokeKernel() -> ";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_PROGRAM_EXECUTABLE:
+  oclHandles.error_str += "CL_INVALID_PROGRAM_EXECUTABLE";
+  break;
+  case CL_INVALID_COMMAND_QUEUE:
+  oclHandles.error_str += "CL_INVALID_COMMAND_QUEUE";
+  break;
+  case CL_INVALID_KERNEL:
+  oclHandles.error_str += "CL_INVALID_KERNEL";
+  break;
+  case CL_INVALID_CONTEXT:
+  oclHandles.error_str += "CL_INVALID_CONTEXT";
+  break;
+  case CL_INVALID_KERNEL_ARGS:
+  oclHandles.error_str += "CL_INVALID_KERNEL_ARGS";
+  break;
+  case CL_INVALID_WORK_DIMENSION:
+  oclHandles.error_str += "CL_INVALID_WORK_DIMENSION";
+  break;
+  case CL_INVALID_GLOBAL_WORK_SIZE:
+  oclHandles.error_str += "CL_INVALID_GLOBAL_WORK_SIZE";
+  break;
+  case CL_INVALID_WORK_GROUP_SIZE:
+  oclHandles.error_str += "CL_INVALID_WORK_GROUP_SIZE";
+  break;
+  case CL_INVALID_WORK_ITEM_SIZE:
+  oclHandles.error_str += "CL_INVALID_WORK_ITEM_SIZE";
+  break;
+  case CL_INVALID_GLOBAL_OFFSET:
+  oclHandles.error_str += "CL_INVALID_GLOBAL_OFFSET";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_MEM_OBJECT_ALLOCATION_FAILURE:
+  oclHandles.error_str += "CL_MEM_OBJECT_ALLOCATION_FAILURE";
+  break;
+  case CL_INVALID_EVENT_WAIT_LIST:
+  oclHandles.error_str += "CL_INVALID_EVENT_WAIT_LIST";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unkown reseason";
+  break;
+  }
+  throw(oclHandles.error_str);
+  }
+  #endif
+
+  oclHandles.cl_status = clWaitForEvents(1, &e[0]);
 
 #ifdef ERRMSG
     if (oclHandles.cl_status!= CL_SUCCESS){
     	oclHandles.error_str = "excpetion in _clInvokeKernel2D() -> clWaitForEvents ->";
    		switch(oclHandles.cl_status){
     	case CL_INVALID_VALUE:
-			oclHandles.error_str += "CL_INVALID_VALUE";
-			break;
-		case CL_INVALID_CONTEXT:
-			oclHandles.error_str += "CL_INVALID_CONTEXT";
-			break;
-		case CL_INVALID_EVENT:
-			oclHandles.error_str += "CL_INVALID_EVENT";
-			break;
-		case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST:
-			oclHandles.error_str += "CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST";
-			break;
-		case CL_OUT_OF_RESOURCES:
-			oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-			break;
-		case CL_OUT_OF_HOST_MEMORY:
-			oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-			break;
-		default:
-			oclHandles.error_str += "Unkown Reason";
-			break;
+  oclHandles.error_str += "CL_INVALID_VALUE";
+  break;
+  case CL_INVALID_CONTEXT:
+  oclHandles.error_str += "CL_INVALID_CONTEXT";
+  break;
+  case CL_INVALID_EVENT:
+  oclHandles.error_str += "CL_INVALID_EVENT";
+  break;
+  case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST:
+  oclHandles.error_str += "CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unkown Reason";
+  break;
     	}
     	throw(oclHandles.error_str);
     }
 #endif
 #ifdef PROFILE_
-	double t2 = gettime();
-	KE += t2 - t1;
+  double t2 = gettime();
+  KE += t2 - t1;
 #endif
 }
 
 /*------------------------------------------------------------
-	@function:	release OpenCL memory objects
-	@params:
-		ob:	the memory object to free or release
-	@return:	NULL
-	@date:		03/04/2011
+  @function:	release OpenCL memory objects
+  @params:
+  ob:	the memory object to free or release
+  @return:	NULL
+  @date:		03/04/2011
 ------------------------------------------------------------*/
 
 void _clFree(cl_mem ob) throw(string){
 #ifdef PROFILE_
-	double t1 = gettime();	
+  double t1 = gettime();
 #endif
-	if(ob!=NULL)
-		oclHandles.cl_status = clReleaseMemObject(ob);	
+  if(ob!=NULL)
+  oclHandles.cl_status = clReleaseMemObject(ob);
 #ifdef ERRMSG
-	if (oclHandles.cl_status!= CL_SUCCESS){
-		oclHandles.error_str = "excpetion in _clFree() ->";
-		switch(oclHandles.cl_status){
-			case CL_INVALID_MEM_OBJECT:
-				oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
-				break;
-			case CL_OUT_OF_RESOURCES:
-				oclHandles.error_str += "CL_OUT_OF_RESOURCES";
-				break;
-			case CL_OUT_OF_HOST_MEMORY:
-				oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
-				break;			
-			default: 
-				oclHandles.error_str += "Unkown reseason";
-				break;		
-		}
-	throw(oclHandles.error_str);
-	}
+  if (oclHandles.cl_status!= CL_SUCCESS){
+  oclHandles.error_str = "excpetion in _clFree() ->";
+  switch(oclHandles.cl_status){
+  case CL_INVALID_MEM_OBJECT:
+  oclHandles.error_str += "CL_INVALID_MEM_OBJECT";
+  break;
+  case CL_OUT_OF_RESOURCES:
+  oclHandles.error_str += "CL_OUT_OF_RESOURCES";
+  break;
+  case CL_OUT_OF_HOST_MEMORY:
+  oclHandles.error_str += "CL_OUT_OF_HOST_MEMORY";
+  break;
+  default:
+  oclHandles.error_str += "Unkown reseason";
+  break;
+  }
+  throw(oclHandles.error_str);
+  }
 #endif
 #ifdef PROFILE_
-	double t2 = gettime();
-	MF += t2 - t1;
+  double t2 = gettime();
+  MF += t2 - t1;
 #endif
 }
 
 /*------------------------------------------------------------
-	@function:	output time profiling information
-	@params:	NULL
-	@return:	NULL
-	@date:		03/04/2011
+  @function:	output time profiling information
+  @params:	NULL
+  @return:	NULL
+  @date:		03/04/2011
 ------------------------------------------------------------*/
 void _clStatistics(){
 #ifdef PROFILE_
-	FILE *fp_pd = fopen("PD_OCL.txt", "a");
-	fprintf(fp_pd, "%lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf\n", CC, CR, MA, MF, H2D, D2H, D2D, KE, KC);
-	fclose(fp_pd);
-#endif	
-	return ;
+  FILE *fp_pd = fopen("PD_OCL.txt", "a");
+  fprintf(fp_pd, "%lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf\n", CC, CR, MA, MF, H2D, D2H, D2D, KE, KC);
+  fclose(fp_pd);
+#endif
+  return ;
 }
 #endif //_CL_HELPER_

+ 146 - 146
opencl/streamcluster/streamcluster.cpp

@@ -1,22 +1,22 @@
 /***********************************************
-	streamcluster.cpp
-	: original source code of streamcluster with minor
-		modification regarding function calls
-	
-	- original code from PARSEC Benchmark Suite
-	- parallelization with CUDA API has been applied by
-	
-	Sang-Ha (a.k.a Shawn) Lee - sl4ge@virginia.edu
-	University of Virginia
-	Department of Electrical and Computer Engineering
-	Department of Computer Science
-	:revised by
-	Jianbin Fang - j.fang@tudelft.nl
-	Delft University of Technology
-	Faculty of Electrical Engineering, Mathematics and Computer Science
-	Department of Software Technology
-	Parallel and Distributed Systems Group
-	on 15/03/2011
+  streamcluster.cpp
+  : original source code of streamcluster with minor
+  modification regarding function calls
+
+  - original code from PARSEC Benchmark Suite
+  - parallelization with CUDA API has been applied by
+
+  Sang-Ha (a.k.a Shawn) Lee - sl4ge@virginia.edu
+  University of Virginia
+  Department of Electrical and Computer Engineering
+  Department of Computer Science
+  :revised by
+  Jianbin Fang - j.fang@tudelft.nl
+  Delft University of Technology
+  Faculty of Electrical Engineering, Mathematics and Computer Science
+  Department of Software Technology
+  Parallel and Distributed Systems Group
+  on 15/03/2011
 ***********************************************/
 
 #include "streamcluster.h"
@@ -69,12 +69,12 @@ double time_shuffle;
 double time_gain_dist;
 double time_gain_init;
 double time_FL;
-#endif 
+#endif
 
 void inttofile(int data, char *filename){
-	FILE *fp = fopen(filename, "w");
-	fprintf(fp, "%d ", data);
-	fclose(fp);	
+  FILE *fp = fopen(filename, "w");
+  fprintf(fp, "%d ", data);
+  fclose(fp);
 }
 
 int isIdentical(float *i, float *j, int D){
@@ -184,9 +184,9 @@ cnt_speedy++;
   long k2 = k1 + bsize;
   if( pid == nproc-1 ) k2 = points->num;
   static float totalcost;
-  
+
   static bool open = false;
-  static float* costs; //cost for each thread. 
+  static float* costs; //cost for each thread.
   static int i;
 
 #ifdef ENABLE_THREADS
@@ -211,7 +211,7 @@ cnt_speedy++;
     *kcenter = 1;
     costs = (float*)malloc(sizeof(float)*nproc);
   }
-    
+
   if( pid != 0 ) { // we are not the master threads. we wait until a center is opened.
     while(1) {
 #ifdef ENABLE_THREADS
@@ -221,46 +221,46 @@ cnt_speedy++;
 #endif
       if( i >= points->num ) break;
       for( int k = k1; k < k2; k++ )
-	{
-	  float distance = dist(points->p[i],points->p[k],points->dim);
-	  if( distance*points->p[k].weight < points->p[k].cost )
-	    {
-	      points->p[k].cost = distance * points->p[k].weight;
-	      points->p[k].assign=i;
-	    }
-	}
+  {
+    float distance = dist(points->p[i],points->p[k],points->dim);
+    if( distance*points->p[k].weight < points->p[k].cost )
+      {
+        points->p[k].cost = distance * points->p[k].weight;
+        points->p[k].assign=i;
+      }
+  }
 #ifdef ENABLE_THREADS
       pthread_barrier_wait(barrier);
       pthread_barrier_wait(barrier);
 #endif
-    } 
+    }
   }
-  else  { // I am the master thread. I decide whether to open a center and notify others if so. 
+  else  { // I am the master thread. I decide whether to open a center and notify others if so.
     for(i = 1; i < points->num; i++ )  {
       bool to_open = ((float)lrand48()/(float)INT_MAX)<(points->p[i].cost/z); //--cambine: what standard?
       if( to_open )  {
-	(*kcenter)++;
+  (*kcenter)++;
 #ifdef ENABLE_THREADS
-	pthread_mutex_lock(&mutex);
+  pthread_mutex_lock(&mutex);
 #endif
-	open = true;
+  open = true;
 #ifdef ENABLE_THREADS
-	pthread_mutex_unlock(&mutex);
-	pthread_cond_broadcast(&cond);
-#endif
-	for( int k = k1; k < k2; k++ )  {	//--cambine: for a new open, compute new cost and center.
-	  float distance = dist(points->p[i],points->p[k],points->dim);
-	  if( distance*points->p[k].weight < points->p[k].cost )  {
-	    points->p[k].cost = distance * points->p[k].weight;
-	    points->p[k].assign=i;
-	  }
-	}
+  pthread_mutex_unlock(&mutex);
+  pthread_cond_broadcast(&cond);
+#endif
+  for( int k = k1; k < k2; k++ )  {	//--cambine: for a new open, compute new cost and center.
+    float distance = dist(points->p[i],points->p[k],points->dim);
+    if( distance*points->p[k].weight < points->p[k].cost )  {
+      points->p[k].cost = distance * points->p[k].weight;
+      points->p[k].assign=i;
+    }
+  }
 #ifdef ENABLE_THREADS
-	pthread_barrier_wait(barrier);
+  pthread_barrier_wait(barrier);
 #endif
-	open = false;
+  open = false;
 #ifdef ENABLE_THREADS
-	pthread_barrier_wait(barrier);
+  pthread_barrier_wait(barrier);
 #endif
       }
     }
@@ -290,9 +290,9 @@ cnt_speedy++;
     {
       totalcost=z*(*kcenter);
       for( int i = 0; i < nproc; i++ )
-	{
-	  totalcost += costs[i];
-	} 
+  {
+    totalcost += costs[i];
+  }
       free(costs);
     }
 #ifdef ENABLE_THREADS
@@ -303,7 +303,7 @@ cnt_speedy++;
   if( pid == 0 )
     {
       fprintf(stderr, "Speedy opened %d facilities for total cost %lf\n",
-	      *kcenter, totalcost);
+        *kcenter, totalcost);
       fprintf(stderr, "Distance Cost %lf\n", totalcost - z*(*kcenter));
     }
 #endif
@@ -326,11 +326,11 @@ cnt_speedy++;
 /* feasible is an array of numfeasible points which may be centers */
 
 float pFL(Points *points, int *feasible, int numfeasible,
-	  float z, long *k, int kmax, float cost, long iter, float e, 
-	  int pid, pthread_barrier_t* barrier)
+    float z, long *k, int kmax, float cost, long iter, float e,
+    int pid, pthread_barrier_t* barrier)
 {
 #ifdef PROFILE_TMP
-	double t1 = gettime();
+  double t1 = gettime();
 #endif
 #ifdef ENABLE_THREADS
   pthread_barrier_wait(barrier);
@@ -355,16 +355,16 @@ float pFL(Points *points, int *feasible, int numfeasible,
     pthread_barrier_wait(barrier);
 #endif
     for (i=0;i<iter;i++) {
-	    x = i%numfeasible;
-	    //printf("--cambine: feasible x=%ld, z=%f, k=%ld, kmax=%d\n", x, z, *k, kmax);
-	    change += pgain(feasible[x], points, z, k, kmax, is_center, center_table, switch_membership,
-												&serial, &cpu_gpu_memcpy, &memcpy_back, &gpu_malloc, &kernel);
-    }		
+      x = i%numfeasible;
+      //printf("--cambine: feasible x=%ld, z=%f, k=%ld, kmax=%d\n", x, z, *k, kmax);
+      change += pgain(feasible[x], points, z, k, kmax, is_center, center_table, switch_membership,
+  &serial, &cpu_gpu_memcpy, &memcpy_back, &gpu_malloc, &kernel);
+    }
     cost -= change;
 #ifdef PRINTINFO
     if( pid == 0 ) {
       fprintf(stderr, "%d centers, cost %lf, total distance %lf\n",
-	      *k, cost, cost - z*(*k));
+        *k, cost, cost - z*(*k));
     }
 #endif
 #ifdef ENABLE_THREADS
@@ -372,8 +372,8 @@ float pFL(Points *points, int *feasible, int numfeasible,
 #endif
   }
 #ifdef PROFILE_TMP
-	double t2 = gettime();
-	time_FL += t2 - t1;
+  double t2 = gettime();
+  time_FL += t2 - t1;
 #endif
   return(cost);
 }
@@ -388,18 +388,18 @@ int selectfeasible_fast(Points *points, int **feasible, int kmin, int pid, pthre
   if (numfeasible > (ITER*kmin*log((float)kmin)))
     numfeasible = (int)(ITER*kmin*log((float)kmin));
   *feasible = (int *)malloc(numfeasible*sizeof(int));
-  
+
   float* accumweight;
   float totalweight;
 
-  /* 
-     Calcuate my block. 
-     For now this routine does not seem to be the bottleneck, so it is not parallelized. 
-     When necessary, this can be parallelized by setting k1 and k2 to 
+  /*
+     Calcuate my block.
+     For now this routine does not seem to be the bottleneck, so it is not parallelized.
+     When necessary, this can be parallelized by setting k1 and k2 to
      proper values and calling this routine from all threads ( it is called only
-     by thread 0 for now ). 
+     by thread 0 for now ).
      Note that when parallelized, the randomization might not be the same and it might
-     not be difficult to measure the parallel speed-up for the whole program. 
+     not be difficult to measure the parallel speed-up for the whole program.
    */
   //  long bsize = numfeasible;
   long k1 = 0;
@@ -428,22 +428,22 @@ int selectfeasible_fast(Points *points, int **feasible, int kmin, int pid, pthre
     //binary search
     l=0;
     r=points->num-1;
-    if( accumweight[0] > w )  { 
-      (*feasible)[i]=0; 
+    if( accumweight[0] > w )  {
+      (*feasible)[i]=0;
       continue;
     }
     while( l+1 < r ) {
       k = (l+r)/2;
       if( accumweight[k] > w ) {
-	r = k;
-      } 
+  r = k;
+      }
       else {
-	l=k;
+  l=k;
       }
     }
     (*feasible)[i]=r;
   }
-  free(accumweight); 
+  free(accumweight);
 #ifdef PROFILE_TMP
   double t2 = gettime();
   time_select_feasible += t2-t1;
@@ -453,7 +453,7 @@ int selectfeasible_fast(Points *points, int **feasible, int kmin, int pid, pthre
 
 /* compute approximate kmedian on the points */
 float pkmedian(Points *points, long kmin, long kmax, long* kfinal,
-	       int pid, pthread_barrier_t* barrier )
+         int pid, pthread_barrier_t* barrier )
 {
   int i;
   float cost;
@@ -491,11 +491,11 @@ float pkmedian(Points *points, long kmin, long kmax, long* kfinal,
   float myhiz = 0;
   for (long kk=k1;kk < k2; kk++ ) {
     myhiz += dist(points->p[kk], points->p[0],
-		      ptDimension)*points->p[kk].weight;
+        ptDimension)*points->p[kk].weight;
   }
   hizs[pid] = myhiz;
 
-#ifdef ENABLE_THREADS  
+#ifdef ENABLE_THREADS
   pthread_barrier_wait(barrier);
 #endif
 
@@ -513,7 +513,7 @@ float pkmedian(Points *points, long kmin, long kmax, long* kfinal,
     }
     cost = 0;
     if( pid== 0 ) {
-      free(hizs); 
+      free(hizs);
       *kfinal = k;
     }
     return cost;
@@ -535,7 +535,7 @@ float pkmedian(Points *points, long kmin, long kmax, long* kfinal,
 #ifdef PRINTINFO
   if( pid==0)
     printf("thread %d: second call to speedy, cost=%lf, k=%d\n",pid,cost,k);
-#endif 
+#endif
   /* if still not enough facilities, assume z is too high */
   while (k < kmin) {
 #ifdef PRINTINFO
@@ -549,20 +549,20 @@ float pkmedian(Points *points, long kmin, long kmax, long* kfinal,
     cost = pspeedy(points, z, &k, pid, barrier);
     i++;
   }
- 
+
   /* now we begin the binary search for real */
   /* must designate some points as feasible centers */
   /* this creates more consistancy between FL runs */
   /* helps to guarantee correct # of centers at the end */
-  
+
   if( pid == 0 )
     {
       numfeasible = selectfeasible_fast(points,&feasible,kmin,pid,barrier); //--cambine?
       for( int i = 0; i< points->num; i++ ) {
-	is_center[points->p[i].assign]= true;
+  is_center[points->p[i].assign]= true;
       }
     }
-	
+
 #ifdef ENABLE_THREADS
   pthread_barrier_wait(barrier);
 #endif
@@ -571,29 +571,29 @@ float pkmedian(Points *points, long kmin, long kmax, long* kfinal,
 #ifdef PRINTINFO
     if( pid==0 )
       {
-	printf("loz = %lf, hiz = %lf\n", loz, hiz);
-	printf("Running Local Search...\n");
+  printf("loz = %lf, hiz = %lf\n", loz, hiz);
+  printf("Running Local Search...\n");
       }
 #endif
     /* first get a rough estimate on the FL solution */
-    //    pthread_barrier_wait(barrier);		
+    //    pthread_barrier_wait(barrier);
     lastcost = cost;
     cost = pFL(points, feasible, numfeasible,
-	       z, &k, kmax, cost, (long)(ITER*kmax*log((float)kmax)), 0.1, pid, barrier);
+         z, &k, kmax, cost, (long)(ITER*kmax*log((float)kmax)), 0.1, pid, barrier);
     /* if number of centers seems good, try a more accurate FL */
     if (((k <= (1.1)*kmax)&&(k >= (0.9)*kmin))||
-	((k <= kmax+2)&&(k >= kmin-2))) {
+  ((k <= kmax+2)&&(k >= kmin-2))) {
 #ifdef PRINTINFO
       if( pid== 0)
-	{
-	  printf("Trying a more accurate local search...\n");
-	}
+  {
+    printf("Trying a more accurate local search...\n");
+  }
 #endif
       /* may need to run a little longer here before halting without
-	 improvement */
-	 
+   improvement */
+
       cost = pFL(points, feasible, numfeasible,
-		 z, &k, kmax, cost, (long)(ITER*kmax*log((float)kmax)), 0.001, pid, barrier);
+   z, &k, kmax, cost, (long)(ITER*kmax*log((float)kmax)), 0.001, pid, barrier);
     }
 
     if (k > kmax) {
@@ -612,8 +612,8 @@ float pkmedian(Points *points, long kmin, long kmax, long* kfinal,
     /* if k is good, return the result */
     /* if we're stuck, just give up and return what we have */
     if (((k <= kmax)&&(k >= kmin))||((loz >= (0.999)*hiz)) )
-      { 
-	break;
+      {
+  break;
       }
 #ifdef ENABLE_THREADS
     pthread_barrier_wait(barrier);
@@ -622,7 +622,7 @@ float pkmedian(Points *points, long kmin, long kmax, long* kfinal,
 
   //clean up...
   if( pid==0 ) {
-    free(feasible); 
+    free(feasible);
     free(hizs);
     *kfinal = k;
   }
@@ -643,14 +643,14 @@ int contcenters(Points *points)
 
       relweight = points->p[i].weight/relweight;
       for (ii=0;ii<points->dim;ii++) {
-				points->p[points->p[i].assign].coord[ii]*=1.0-relweight;
-				points->p[points->p[i].assign].coord[ii]+=
-				points->p[i].coord[ii]*relweight;
+  points->p[points->p[i].assign].coord[ii]*=1.0-relweight;
+  points->p[points->p[i].assign].coord[ii]+=
+  points->p[i].coord[ii]*relweight;
       }
       points->p[points->p[i].assign].weight += points->p[i].weight;
     }
   }
-  
+
   return 0;
 }
 
@@ -736,7 +736,7 @@ void localSearch( Points* points, long kmin, long kmax, long* kfinal ) {
   double t2 = gettime();
   time_local_search += t2-t1;
 #endif
- 
+
 }
 
 
@@ -756,7 +756,7 @@ void outcenterIDs( Points* centers, long* centerIDs, char* outfile ) {
       fprintf(fp, "%u\n", centerIDs[i]);
       fprintf(fp, "%lf\n", centers->p[i].weight);
       for( int k = 0; k < centers->dim; k++ ) {
-	fprintf(fp, "%lf ", centers->p[i].coord[k]);
+  fprintf(fp, "%lf ", centers->p[i].coord[k]);
       }
       fprintf(fp,"\n\n");
     }
@@ -764,16 +764,16 @@ void outcenterIDs( Points* centers, long* centerIDs, char* outfile ) {
   fclose(fp);
 }
 
-void streamCluster( PStream* stream, 
-		    long kmin, long kmax, int dim,
-		    long chunksize, long centersize, char* outfile )
+void streamCluster( PStream* stream,
+      long kmin, long kmax, int dim,
+      long chunksize, long centersize, char* outfile )
 {
 
   float* block = (float*)malloc( chunksize*dim*sizeof(float) );
   float* centerBlock = (float*)malloc(centersize*dim*sizeof(float) );
   long* centerIDs = (long*)malloc(centersize*dim*sizeof(long));
 
-  if( block == NULL ) { 
+  if( block == NULL ) {
     fprintf(stderr,"not enough memory for a chunk!\n");
     exit(1);
   }
@@ -783,9 +783,9 @@ void streamCluster( PStream* stream,
   points.num = chunksize;
   points.p = (Point *)malloc(chunksize*sizeof(Point));
   for( int i = 0; i < chunksize; i++ ) {
-    points.p[i].coord = &block[i*dim];		
+    points.p[i].coord = &block[i*dim];
   }
-	
+
 
   Points centers;
   centers.dim = dim;
@@ -801,7 +801,7 @@ void streamCluster( PStream* stream,
   long kfinal;
   while(1) {
 
-    size_t numRead  = stream->read(block, dim, chunksize ); 
+    size_t numRead  = stream->read(block, dim, chunksize );
     fprintf(stderr,"read %d points\n",numRead);
 
     if( stream->ferror() || numRead < (unsigned int)chunksize && !stream->feof() ) {
@@ -823,7 +823,7 @@ void streamCluster( PStream* stream,
     fprintf(stderr,"finish local search\n");
     contcenters(&points);
     if( kfinal + centers.num > centersize ) {
-      //here we don't handle the situation where # of centers gets too large. 
+      //here we don't handle the situation where # of centers gets too large.
       fprintf(stderr,"oops! no more space for centers\n");
       exit(1);
     }
@@ -836,7 +836,7 @@ void streamCluster( PStream* stream,
     IDoffset += numRead;
 
 #ifdef PRINTINFO
-    printf("finish copy centers\n"); 
+    printf("finish copy centers\n");
 #endif
     free(is_center);
     free(switch_membership);
@@ -866,10 +866,10 @@ int main(int argc, char **argv)
 #define __PARSEC_STRING(x) #x
 #define __PARSEC_XSTRING(x) __PARSEC_STRING(x)
         printf("PARSEC Benchmark Suite Version "__PARSEC_XSTRING(PARSEC_VERSION)"\n");
-	fflush(NULL);
+  fflush(NULL);
 #else
         printf("PARSEC Benchmark Suite\n");
-	fflush(NULL);
+  fflush(NULL);
 #endif //PARSEC_VERSION
 #ifdef ENABLE_PARSEC_HOOKS
   __parsec_bench_begin(__parsec_streamcluster);
@@ -877,7 +877,7 @@ int main(int argc, char **argv)
 
   if (argc<11) {
     fprintf(stderr,"usage: %s k1 k2 d n chunksize clustersize infile outfile nproc\n",
-	    argv[0]);
+      argv[0]);
     fprintf(stderr,"  k1:          Min. number of centers allowed\n");
     fprintf(stderr,"  k2:          Max. number of centers allowed\n");
     fprintf(stderr,"  d:           Dimension of each data point\n");
@@ -902,7 +902,7 @@ int main(int argc, char **argv)
   nproc = atoi(argv[9]);
   _clCmdParams(argc, argv);
   try{
-	  _clInit(platform_idx, device_idx, device_type);
+    _clInit(platform_idx, device_idx, device_type);
    }
    catch(std::string msg){
    	std::cout<<"exception caught in main function->"<<msg<<std::endl;
@@ -923,25 +923,25 @@ int main(int argc, char **argv)
   __parsec_roi_begin();
 #endif
 #ifdef PROFILE_TMP
-	serial = 0.0;
-	cpu_gpu_memcpy = 0.0;
-	gpu_malloc = 0.0;
-	gpu_free = 0.0;
-	kernel = 0.0;
-	time_FL = 0.0;
-	cnt_speedy = 0;
+  serial = 0.0;
+  cpu_gpu_memcpy = 0.0;
+  gpu_malloc = 0.0;
+  gpu_free = 0.0;
+  kernel = 0.0;
+  time_FL = 0.0;
+  cnt_speedy = 0;
 #endif
   std::cout<<"before sc"<<std::endl;
   streamCluster(stream, kmin, kmax, dim, chunksize, clustersize, outfilename );
   std::cout<<"after sc"<<std::endl;
-#ifdef PROFILE_TMP 
-	gpu_free = gettime();
+#ifdef PROFILE_TMP
+  gpu_free = gettime();
 #endif
-	freeDevMem();
+  freeDevMem();
 #ifdef PROFILE_TMP
-	gpu_free = gettime() - gpu_free;
+  gpu_free = gettime() - gpu_free;
 #endif
-	_clRelease();
+  _clRelease();
 #ifdef ENABLE_PARSEC_HOOKS
   __parsec_roi_end();
 #endif
@@ -952,7 +952,7 @@ int main(int argc, char **argv)
 #endif
 
   delete stream;
-  
+
 #ifdef PROFILE_TMP
   printf("time pgain = %lf\n", time_gain);
   printf("time pgain_dist = %lf\n", time_gain_dist);
@@ -962,23 +962,23 @@ int main(int argc, char **argv)
   printf("time pshuffle = %lf\n", time_shuffle);
   printf("time FL = %lf\n", time_FL);
   printf("time localSearch = %lf\n", time_local_search);
-	printf("\n");
-	printf("====GPU Timing info====\n");
-	printf("time serial = %lf\n", serial);
-	printf("time CPU to GPU memory copy = %lf\n", cpu_gpu_memcpy);
-	printf("time GPU to CPU memory copy back = %lf\n", memcpy_back);
-	printf("time GPU malloc = %lf\n", gpu_malloc);
-	printf("time GPU free = %lf\n", gpu_free);
-	printf("time kernel = %lf\n", kernel);
-	
+  printf("\n");
+  printf("====GPU Timing info====\n");
+  printf("time serial = %lf\n", serial);
+  printf("time CPU to GPU memory copy = %lf\n", cpu_gpu_memcpy);
+  printf("time GPU to CPU memory copy back = %lf\n", memcpy_back);
+  printf("time GPU malloc = %lf\n", gpu_malloc);
+  printf("time GPU free = %lf\n", gpu_free);
+  printf("time kernel = %lf\n", kernel);
+
   FILE *fp = fopen("PD.txt", "w");
   fprintf(fp, "%lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf\n", time_FL, cpu_gpu_memcpy, memcpy_back, kernel, gpu_malloc, gpu_free, 0.0);
-  fclose(fp);	
+  fclose(fp);
  #endif
- _clStatistics(); 
+ _clStatistics();
 #ifdef ENABLE_PARSEC_HOOKS
   __parsec_bench_end();
 #endif
-  
+
   return 0;
 }

+ 21 - 21
opencl/streamcluster/streamcluster.h

@@ -1,22 +1,22 @@
 /***********************************************
-	streamcluster.h
-	: header file to streamcluster
-	
-	- original code from PARSEC Benchmark Suite
-	- parallelization with CUDA API has been applied by
-	
-	Sang-Ha (a.k.a Shawn) Lee - sl4ge@virginia.edu
-	University of Virginia
-	Department of Electrical and Computer Engineering
-	Department of Computer Science
-	
-	:revised by
-	Jianbin Fang - j.fang@tudelft.nl
-	Delft University of Technology
-	Faculty of Electrical Engineering, Mathematics and Computer Science
-	Department of Software Technology
-	Parallel and Distributed Systems Group
-	on 15/03/2011
+  streamcluster.h
+  : header file to streamcluster
+
+  - original code from PARSEC Benchmark Suite
+  - parallelization with CUDA API has been applied by
+
+  Sang-Ha (a.k.a Shawn) Lee - sl4ge@virginia.edu
+  University of Virginia
+  Department of Electrical and Computer Engineering
+  Department of Computer Science
+
+  :revised by
+  Jianbin Fang - j.fang@tudelft.nl
+  Delft University of Technology
+  Faculty of Electrical Engineering, Mathematics and Computer Science
+  Department of Software Technology
+  Parallel and Distributed Systems Group
+  on 15/03/2011
 ***********************************************/
 
 #ifndef STREAMCLUSTER_H
@@ -85,7 +85,7 @@ public:
     size_t count = 0;
     for( int i = 0; i < num && n > 0; i++ ) {
       for( int k = 0; k < dim; k++ ) {
-	dest[i*dim + k] = lrand48()/(float)INT_MAX;
+  dest[i*dim + k] = lrand48()/(float)INT_MAX;
       }
       n--;
       count++;
@@ -98,7 +98,7 @@ public:
   int feof() {
     return n <= 0;
   }
-  ~SimStream() { 
+  ~SimStream() {
   }
 private:
   long n;
@@ -114,7 +114,7 @@ public:
     }
   }
   size_t read( float* dest, int dim, int num ) {
-    return std::fread(dest, sizeof(float)*dim, num, fp); 
+    return std::fread(dest, sizeof(float)*dim, num, fp);
   }
   int ferror() {
     return std::ferror(fp);

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác