From cb1f33c6ae840e8dc0f43518daf76e6ed01034f0 Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Mon, 08 Dec 2014 19:48:57 +0000
Subject: [PATCH] Fixed race condition in server

---
 src/opencl.c |   60 +++++++++++++++++++++++++++++++-----------------------------
 1 files changed, 31 insertions(+), 29 deletions(-)

diff --git a/src/opencl.c b/src/opencl.c
index fc7310c..994b8d6 100644
--- a/src/opencl.c
+++ b/src/opencl.c
@@ -4,25 +4,30 @@
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
-//#include <clBLAS.h>
+
+#ifdef CLBLAS
+#include <clBLAS.h>
+#endif
 
 #include "opencl.h"
 #include "utils.h"
+#include "activations.h"
 
 cl_info cl = {0};
 
 void check_error(cl_info info)
 {
-    clFinish(cl.queue);
+   // clFinish(cl.queue);
     if (info.error != CL_SUCCESS) {
         printf("\n Error number %d", info.error);
+        abort();
         exit(1);
     }
 }
 
 #define MAX_DEVICES 10
 
-cl_info cl_init()
+cl_info cl_init(int index)
 {
     cl_info info;
     info.initialized = 0;
@@ -69,6 +74,8 @@
         printf("  DEVICE_MAX_CLOCK_FREQUENCY = %u\n", (unsigned int)buf_uint);
         clGetDeviceInfo(devices[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(devices[i], CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(buf_ulong), &buf_ulong, NULL);
+        printf("  DEVICE_MAX_MEM_ALLOC_SIZE = %llu\n", (unsigned long long)buf_ulong);
         clGetDeviceInfo(devices[i], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(buf_ulong), &buf_ulong, NULL);
         printf("  DEVICE_MAX_WORK_GROUP_SIZE = %llu\n", (unsigned long long)buf_ulong);
         cl_uint items;
@@ -80,8 +87,7 @@
         printf("  DEVICE_MAX_WORK_ITEM_SIZES = %u / %u / %u \n", (unsigned int)workitem_size[0], (unsigned int)workitem_size[1], (unsigned int)workitem_size[2]);
 
     }
-    int index = getpid()%num_devices;
-    index = 1;
+    index = index%num_devices;
     printf("%d rand, %d devices, %d index\n", getpid(), num_devices, index);
     info.device = devices[index];
     fprintf(stderr, "Found %d device(s)\n", num_devices);
@@ -95,24 +101,14 @@
     check_error(info);
     info.queue = clCreateCommandQueue(info.context, info.device, 0, &info.error);
     check_error(info);
-    for(i = 0; i < NUM_QUEUES; ++i){
-        info.queues[i] = clCreateCommandQueue(info.context, info.device, 0, &info.error);
-        check_error(info);
-    }
-    //info.error = clblasSetup();
+    #ifdef CLBLAS
+    info.error = clblasSetup();
+    #endif
     check_error(info);
     info.initialized = 1;
     return info;
 }
 
-void wait_for_queues()
-{
-    int i;
-    for(i = 0; i < NUM_QUEUES; ++i){
-        clFinish(cl.queues[i]);
-    }
-}
-
 cl_program cl_fprog(char *filename, char *options, cl_info info)
 {
 	size_t srcsize;
@@ -138,17 +134,16 @@
 	return prog;
 }
 
-void cl_setup()
+void cl_setup(int index)
 {
 	if(!cl.initialized){
         printf("initializing\n");
-		cl = cl_init();
+		cl = cl_init(index);
 	}
 }
 
 cl_kernel get_kernel(char *filename, char *kernelname, char *options)
 {
-	cl_setup();
 	cl_program prog = cl_fprog(filename, options, cl);
 	cl_kernel kernel=clCreateKernel(prog, kernelname, &cl.error);
 	check_error(cl);
@@ -157,22 +152,29 @@
 
 void cl_read_array(cl_mem mem, float *x, int n)
 {
-    cl_setup();
-    clEnqueueReadBuffer(cl.queue, mem, CL_TRUE, 0, sizeof(float)*n,x,0,0,0);
+    cl.error = clEnqueueReadBuffer(cl.queue, mem, CL_TRUE, 0, sizeof(float)*n,x,0,0,0);
     check_error(cl);
 }
 
+float cl_checksum(cl_mem mem, int n)
+{
+    
+    float *x = calloc(n, sizeof(float));
+    cl_read_array(mem, x, n);
+    float sum = sum_array(x, n);
+    free(x);
+    return sum;
+}
+
 void cl_write_array(cl_mem mem, float *x, int n)
 {
-    cl_setup();
-    clEnqueueWriteBuffer(cl.queue, mem, CL_TRUE, 0,sizeof(float)*n,x,0,0,0);
+    cl.error = clEnqueueWriteBuffer(cl.queue, mem, CL_TRUE, 0,sizeof(float)*n,x,0,0,0);
     check_error(cl);
 }
 
 void cl_copy_array(cl_mem src, cl_mem dst, int n)
 {
-    cl_setup();
-    clEnqueueCopyBuffer(cl.queue, src, dst, 0, 0, sizeof(float)*n,0,0,0);
+    cl.error = clEnqueueCopyBuffer(cl.queue, src, dst, 0, 0, sizeof(float)*n,0,0,0);
     check_error(cl);
 }
 
@@ -186,19 +188,19 @@
     return sub;
 }
 
+
 cl_mem cl_make_array(float *x, int n)
 {
-    cl_setup();
     cl_mem mem = clCreateBuffer(cl.context,
             CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR,
             sizeof(float)*n, x, &cl.error);
     check_error(cl);
+    activate_array_ongpu(mem, n, LINEAR);
     return mem;
 }
 
 cl_mem cl_make_int_array(int *x, int n)
 {
-    cl_setup();
     cl_mem mem = clCreateBuffer(cl.context,
             CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR,
             sizeof(int)*n, x, &cl.error);

--
Gitblit v1.10.0