backprop_ocl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // includes, system
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <sys/time.h>
  7. #include "backprop.h"
  8. #ifdef NV //NVIDIA
  9. #include <oclUtils.h>
  10. #else
  11. #include <CL/cl.h>
  12. #endif
  13. ////////////////////////////////////////////////////////////////////////////////
  14. // local variables
  15. static cl_context context;
  16. static cl_command_queue cmd_queue;
  17. static cl_device_type device_type;
  18. static cl_device_id * device_list;
  19. static cl_int num_devices;
  20. static int initialize(int use_gpu)
  21. {
  22. cl_int result;
  23. size_t size;
  24. // create OpenCL context
  25. cl_platform_id platform_id;
  26. if (clGetPlatformIDs(1, &platform_id, NULL) != CL_SUCCESS) { printf("ERROR: clGetPlatformIDs(1,*,0) failed\n"); return -1; }
  27. cl_context_properties ctxprop[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id, 0};
  28. device_type = use_gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU;
  29. context = clCreateContextFromType( ctxprop, device_type, NULL, NULL, NULL );
  30. if( !context ) { printf("ERROR: clCreateContextFromType(%s) failed\n", use_gpu ? "GPU" : "CPU"); return -1; }
  31. // get the list of GPUs
  32. result = clGetContextInfo( context, CL_CONTEXT_DEVICES, 0, NULL, &size );
  33. num_devices = (int) (size / sizeof(cl_device_id));
  34. printf("num_devices = %d\n", num_devices);
  35. if( result != CL_SUCCESS || num_devices < 1 ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
  36. device_list = new cl_device_id[num_devices];
  37. //device_list = (cl_device_id *)malloc(sizeof(cl_device_id)*num_devices);
  38. if( !device_list ) { printf("ERROR: new cl_device_id[] failed\n"); return -1; }
  39. result = clGetContextInfo( context, CL_CONTEXT_DEVICES, size, device_list, NULL );
  40. if( result != CL_SUCCESS ) { printf("ERROR: clGetContextInfo() failed\n"); return -1; }
  41. // create command queue for the first device
  42. cmd_queue = clCreateCommandQueue( context, device_list[0], 0, NULL );
  43. if( !cmd_queue ) { printf("ERROR: clCreateCommandQueue() failed\n"); return -1; }
  44. return 0;
  45. }
  46. static int shutdown()
  47. {
  48. // release resources
  49. if( cmd_queue ) clReleaseCommandQueue( cmd_queue );
  50. if( context ) clReleaseContext( context );
  51. if( device_list ) delete[] device_list;
  52. // reset all variables
  53. cmd_queue = 0;
  54. context = 0;
  55. device_list = 0;
  56. num_devices = 0;
  57. device_type = 0;
  58. return 0;
  59. }
  60. double gettime() {
  61. struct timeval t;
  62. gettimeofday(&t,NULL);
  63. return t.tv_sec+t.tv_usec*1e-6;
  64. }
  65. unsigned int num_threads = 0;
  66. unsigned int num_blocks = 0;
  67. ////////////////////////////////////////////////////////////////////////////////
  68. // Program main
  69. ////////////////////////////////////////////////////////////////////////////////
  70. int
  71. main( int argc, char** argv)
  72. {
  73. setup(argc, argv);
  74. }
  75. int bpnn_train_kernel(BPNN *net, float *eo, float *eh)
  76. {
  77. int in, hid, out;
  78. float out_err, hid_err;
  79. in = net->input_n;
  80. hid = net->hidden_n;
  81. out = net->output_n;
  82. int sourcesize = 1024*1024;
  83. char * source = (char *)calloc(sourcesize, sizeof(char));
  84. if(!source) { printf("ERROR: calloc(%d) failed\n", sourcesize); return -1; }
  85. // read the kernel core source
  86. char * kernel_bp1 = "bpnn_layerforward_ocl";
  87. char * kernel_bp2 = "bpnn_adjust_weights_ocl";
  88. char * tempchar = "./backprop_kernel.cl";
  89. FILE * fp = fopen(tempchar, "rb");
  90. if(!fp) { printf("ERROR: unable to open '%s'\n", tempchar); return -1; }
  91. fread(source + strlen(source), sourcesize, 1, fp);
  92. fclose(fp);
  93. int use_gpu = 1;
  94. if(initialize(use_gpu)) return -1;
  95. // compile kernel
  96. cl_int err = 0;
  97. const char * slist[2] = { source, 0 };
  98. cl_program prog = clCreateProgramWithSource(context, 1, slist, NULL, &err);
  99. if(err != CL_SUCCESS) { printf("ERROR: clCreateProgramWithSource() => %d\n", err); return -1; }
  100. err = clBuildProgram(prog, 0, NULL, NULL, NULL, NULL);
  101. { // show warnings/errors
  102. //static char log[65536]; memset(log, 0, sizeof(log));
  103. //cl_device_id device_id = 0;
  104. //err = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(device_id), &device_id, NULL);
  105. //clGetProgramBuildInfo(prog, device_id, CL_PROGRAM_BUILD_LOG, sizeof(log)-1, log, NULL);
  106. //if(err || strstr(log,"warning:") || strstr(log, "error:")) printf("<<<<\n%s\n>>>>\n", log);
  107. }
  108. if(err != CL_SUCCESS) { printf("ERROR: clBuildProgram() => %d\n", err); return -1; }
  109. cl_kernel kernel1;
  110. cl_kernel kernel2;
  111. kernel1 = clCreateKernel(prog, kernel_bp1, &err);
  112. kernel2 = clCreateKernel(prog, kernel_bp2, &err);
  113. if(err != CL_SUCCESS) { printf("ERROR: clCreateKernel() 0 => %d\n", err); return -1; }
  114. clReleaseProgram(prog);
  115. float *input_weights_one_dim;
  116. float *input_weights_prev_one_dim;
  117. float * partial_sum;
  118. float sum;
  119. float num_blocks = in / BLOCK_SIZE;
  120. input_weights_one_dim = (float *) malloc((in + 1)* (hid + 1) * sizeof(float));
  121. input_weights_prev_one_dim = (float *) malloc((in + 1)* (hid + 1) * sizeof(float));
  122. partial_sum = (float *) malloc(num_blocks * WIDTH * sizeof(float));
  123. // set global and local workitems
  124. size_t global_work[3] = { BLOCK_SIZE, BLOCK_SIZE * num_blocks, 1 };
  125. size_t local_work[3] = { BLOCK_SIZE, BLOCK_SIZE, 1 };
  126. // this preprocessing stage is temporarily added to correct the bug of wrong memcopy using two-dimensional net->inputweights
  127. // todo: fix mem allocation
  128. int m = 0;
  129. for (int k = 0; k <= in; k++) {
  130. for (int j = 0; j <= hid; j++) {
  131. input_weights_one_dim[m] = net->input_weights[k][j];
  132. input_weights_prev_one_dim[m] = net-> input_prev_weights[k][j];
  133. m++;
  134. }
  135. }
  136. cl_mem input_hidden_ocl;
  137. cl_mem input_ocl;
  138. cl_mem output_hidden_ocl;
  139. cl_mem hidden_partial_sum;
  140. cl_mem hidden_delta_ocl;
  141. cl_mem input_prev_weights_ocl;
  142. input_ocl = clCreateBuffer(context, CL_MEM_READ_WRITE, (in + 1) * sizeof(float), NULL, &err );
  143. if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer input_ocl\n"); return -1;}
  144. input_hidden_ocl = clCreateBuffer(context, CL_MEM_READ_WRITE, (in + 1) * (hid + 1) * sizeof(float), NULL, &err );
  145. if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer input_hidden_ocl\n"); return -1;}
  146. output_hidden_ocl = clCreateBuffer(context, CL_MEM_READ_WRITE, (hid + 1) * sizeof(float), NULL, &err );
  147. if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer output_hidden_ocl\n"); return -1;}
  148. hidden_partial_sum = clCreateBuffer(context, CL_MEM_READ_WRITE, num_blocks * WIDTH * sizeof(float), NULL, &err );
  149. if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer hidden_partial_sum\n"); return -1;}
  150. hidden_delta_ocl = clCreateBuffer(context, CL_MEM_READ_WRITE, (hid + 1) * sizeof(float), NULL, &err );
  151. if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer hidden_delta_ocl\n"); return -1;}
  152. input_prev_weights_ocl = clCreateBuffer(context, CL_MEM_READ_WRITE, (in + 1) * (hid + 1) * sizeof(float), NULL, &err );
  153. if(err != CL_SUCCESS) { printf("ERROR: clCreateBuffer input_prev_weights_ocl\n"); return -1;}
  154. printf("Performing GPU computation\n");
  155. //write buffers
  156. err = clEnqueueWriteBuffer(cmd_queue, input_ocl, 1, 0, (in + 1) * sizeof(float), net->input_units, 0, 0, 0);
  157. if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer input_ocl\n"); return -1; }
  158. err = clEnqueueWriteBuffer(cmd_queue, input_hidden_ocl, 1, 0, (in + 1) * (hid + 1) * sizeof(float), input_weights_one_dim, 0, 0, 0);
  159. if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer input_hidden_ocl\n"); return -1; }
  160. clSetKernelArg(kernel1, 0, sizeof(void *), (void*) &input_ocl);
  161. clSetKernelArg(kernel1, 1, sizeof(void *), (void*) &output_hidden_ocl);
  162. clSetKernelArg(kernel1, 2, sizeof(void *), (void*) &input_hidden_ocl);
  163. clSetKernelArg(kernel1, 3, sizeof(void *), (void*) &hidden_partial_sum );
  164. clSetKernelArg(kernel1, 4, sizeof(float) * HEIGHT, (void*)NULL );
  165. clSetKernelArg(kernel1, 5, sizeof(float ) * HEIGHT * WIDTH, (void*)NULL );
  166. clSetKernelArg(kernel1, 6, sizeof(cl_int), (void*) &in);
  167. clSetKernelArg(kernel1, 7, sizeof(cl_int), (void*) &hid);
  168. err = clEnqueueNDRangeKernel(cmd_queue, kernel1, 2, NULL, global_work, local_work, 0, 0, 0);
  169. if(err != CL_SUCCESS) { printf("ERROR: 1 clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }
  170. err = clEnqueueReadBuffer(cmd_queue, hidden_partial_sum, 1, 0, num_blocks * WIDTH * sizeof(float), partial_sum, 0, 0, 0);
  171. if(err != CL_SUCCESS) { printf("ERROR: 1 clEnqueueReadBuffer: partial sum\n"); return -1; }
  172. for (int j = 1; j <= hid; j++) {
  173. sum = 0.0;
  174. for (int k = 0; k < num_blocks; k++) {
  175. sum += partial_sum[k * hid + j-1] ;
  176. }
  177. sum += net->input_weights[0][j];
  178. net-> hidden_units[j] = float(1.0 / (1.0 + exp(-sum)));
  179. }
  180. bpnn_layerforward(net->hidden_units, net->output_units, net->hidden_weights, hid, out);
  181. bpnn_output_error(net->output_delta, net->target, net->output_units, out, &out_err);
  182. bpnn_hidden_error(net->hidden_delta, hid, net->output_delta, out, net->hidden_weights, net->hidden_units, &hid_err);
  183. bpnn_adjust_weights(net->output_delta, out, net->hidden_units, hid, net->hidden_weights, net->hidden_prev_weights);
  184. err = clEnqueueWriteBuffer(cmd_queue, hidden_delta_ocl, 1, 0, (hid + 1) * sizeof(float), net->hidden_delta, 0, 0, 0);
  185. if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer hidden_delta_ocl\n"); return -1; }
  186. err = clEnqueueWriteBuffer(cmd_queue, input_prev_weights_ocl, 1, 0, (in + 1) * (hid + 1) * sizeof(float), input_weights_prev_one_dim, 0, 0, 0);
  187. if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer input_prev_weights_ocl\n"); return -1; }
  188. err = clEnqueueWriteBuffer(cmd_queue, input_hidden_ocl, 1, 0, (in + 1) * (hid + 1) * sizeof(float), input_weights_one_dim, 0, 0, 0);
  189. if(err != CL_SUCCESS) { printf("ERROR: clEnqueueWriteBuffer input_hidden_ocl\n"); return -1; }
  190. clSetKernelArg(kernel2, 0, sizeof(void *), (void*) &hidden_delta_ocl);
  191. clSetKernelArg(kernel2, 1, sizeof(cl_int), (void*) &hid);
  192. clSetKernelArg(kernel2, 2, sizeof(void *), (void*) &input_ocl);
  193. clSetKernelArg(kernel2, 3, sizeof(cl_int), (void*) &in);
  194. clSetKernelArg(kernel2, 4, sizeof(void *), (void*) &input_hidden_ocl);
  195. clSetKernelArg(kernel2, 5, sizeof(void *), (void*) &input_prev_weights_ocl );
  196. err = clEnqueueNDRangeKernel(cmd_queue, kernel2, 2, NULL, global_work, local_work, 0, 0, 0);
  197. if(err != CL_SUCCESS) { printf("ERROR: 1 clEnqueueNDRangeKernel()=>%d failed\n", err); return -1; }
  198. err = clEnqueueReadBuffer(cmd_queue, input_ocl, 1, 0, (in + 1) * sizeof(float), net->input_units, 0, 0, 0);
  199. if(err != CL_SUCCESS) { printf("ERROR: 1 clEnqueueReadBuffer: input_ocl\n"); return -1; }
  200. err = clEnqueueReadBuffer(cmd_queue, input_hidden_ocl, 1, 0, (in + 1) * (hid + 1) * sizeof(float), input_weights_one_dim, 0, 0, 0);
  201. if(err != CL_SUCCESS) { printf("ERROR: 1 clEnqueueReadBuffer: input_hidden_ocl\n"); return -1; }
  202. clReleaseMemObject(input_ocl);
  203. clReleaseMemObject(output_hidden_ocl);
  204. clReleaseMemObject(input_hidden_ocl);
  205. clReleaseMemObject(hidden_partial_sum);
  206. clReleaseMemObject(input_prev_weights_ocl);
  207. free(input_weights_prev_one_dim);
  208. free(partial_sum);
  209. free(input_weights_one_dim);
  210. }