Joseph Redmon
2014-12-07 28e21152728cbea617948671df064ec75c7953e5
src/opencl.c
@@ -5,16 +5,22 @@
#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);
    }
}
@@ -68,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,9 +88,9 @@
    }
    int index = getpid()%num_devices;
    index = 1;
    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);
@@ -94,6 +102,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;
}
@@ -126,6 +138,7 @@
void cl_setup()
{
   if(!cl.initialized){
        printf("initializing\n");
      cl = cl_init();
   }
}
@@ -142,21 +155,31 @@
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);
}
@@ -170,6 +193,7 @@
    return sub;
}
cl_mem cl_make_array(float *x, int n)
{
    cl_setup();
@@ -177,6 +201,17 @@
            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);
    check_error(cl);
    return mem;
}