diff options
author | Thomas White <taw@physics.org> | 2010-11-18 11:04:05 +0100 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2012-02-22 15:27:06 +0100 |
commit | d6ae8065f7c422300dba3b6256f4ace53b39a092 (patch) | |
tree | 25ffd2c30e20d002c9b92c1910f60427471010d6 /src/cl-utils.c | |
parent | 322bcc8d927078b1d672faf94c507c44f8104b1a (diff) |
Handle multiple GPUs
Diffstat (limited to 'src/cl-utils.c')
-rw-r--r-- | src/cl-utils.c | 77 |
1 files changed, 73 insertions, 4 deletions
diff --git a/src/cl-utils.c b/src/cl-utils.c index a7d23371..1b7b9811 100644 --- a/src/cl-utils.c +++ b/src/cl-utils.c @@ -48,18 +48,87 @@ const char *clError(cl_int err) } -cl_device_id get_first_dev(cl_context ctx) +static char *get_device_string(cl_device_id dev, cl_device_info info) { - cl_device_id dev; + int r; + size_t size; + char *val; + + r = clGetDeviceInfo(dev, info, 0, NULL, &size); + if ( r != CL_SUCCESS ) { + ERROR("Couldn't get device vendor size: %s\n", + clError(r)); + return NULL; + } + val = malloc(size); + r = clGetDeviceInfo(dev, info, size, val, NULL); + if ( r != CL_SUCCESS ) { + ERROR("Couldn't get dev vendor: %s\n", clError(r)); + return NULL; + } + + return val; +} + + +cl_device_id get_cl_dev(cl_context ctx, int n) +{ + cl_device_id *dev; cl_int r; + size_t size; + int i, num_devs; - r = clGetContextInfo(ctx, CL_CONTEXT_DEVICES, sizeof(dev), &dev, NULL); + /* Get the required size of the array */ + r = clGetContextInfo(ctx, CL_CONTEXT_DEVICES, 0, NULL, &size); + if ( r != CL_SUCCESS ) { + ERROR("Couldn't get array size for devices: %s\n", clError(r)); + return 0; + } + + dev = malloc(size); + r = clGetContextInfo(ctx, CL_CONTEXT_DEVICES, size, dev, NULL); if ( r != CL_SUCCESS ) { ERROR("Couldn't get device: %s\n", clError(r)); return 0; } + num_devs = size/sizeof(cl_device_id); + + if ( n >= num_devs ) { + ERROR("Device ID out of range\n"); + return 0; + } + + if ( n < 0 ) { + + STATUS("Available devices:\n"); + for ( i=0; i<num_devs; i++ ) { + + char *vendor; + char *name; + + vendor = get_device_string(dev[i], CL_DEVICE_VENDOR); + name = get_device_string(dev[i], CL_DEVICE_NAME); + + STATUS("Device %i: %s %s\n", i, vendor, name); + + } + n = 0; + + STATUS("Using device 0. Use --gpu-dev to choose another.\n"); + + } else { + + char *vendor; + char *name; + + vendor = get_device_string(dev[n], CL_DEVICE_VENDOR); + name = get_device_string(dev[n], CL_DEVICE_NAME); + + STATUS("Using device %i: %s %s\n", n, vendor, name); + + } - return dev; + return dev[n]; } |