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 |   62 +++++++++++++++++++++++--------
 1 files changed, 46 insertions(+), 16 deletions(-)

diff --git a/src/opencl.c b/src/opencl.c
index bcc0f09..994b8d6 100644
--- a/src/opencl.c
+++ b/src/opencl.c
@@ -1,26 +1,33 @@
 #ifdef GPU
-#include "opencl.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
 #include <unistd.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;
@@ -67,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;
@@ -78,10 +87,9 @@
         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 = index%num_devices;
     printf("%d rand, %d devices, %d index\n", getpid(), num_devices, index);
-    //info.device = devices[index];
-    info.device = devices[0];
+    info.device = devices[index];
     fprintf(stderr, "Found %d device(s)\n", num_devices);
     check_error(info);
 
@@ -93,6 +101,10 @@
     check_error(info);
     info.queue = clCreateCommandQueue(info.context, info.device, 0, &info.error);
     check_error(info);
+    #ifdef CLBLAS
+    info.error = clblasSetup();
+    #endif
+    check_error(info);
     info.initialized = 1;
     return info;
 }
@@ -103,6 +115,7 @@
 	char src[64*1024];
 	memset(src, 0, 64*1024);
 	FILE *fil=fopen(filename,"r");
+    if(fil == 0) file_error(filename);
 	srcsize=fread(src, sizeof src, 1, fil);
 	fclose(fil);
 	const char *srcptr[]={src};
@@ -121,16 +134,16 @@
 	return prog;
 }
 
-void cl_setup()
+void cl_setup(int index)
 {
 	if(!cl.initialized){
-		cl = cl_init();
+        printf("initializing\n");
+		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);
@@ -139,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);
 }
 
@@ -168,13 +188,23 @@
     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_mem mem = clCreateBuffer(cl.context,
+            CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR,
+            sizeof(int)*n, x, &cl.error);
+    check_error(cl);
     return mem;
 }
 

--
Gitblit v1.10.0