clray
diff src/ocl.cc @ 8:deaf85acf6af
interactive spheres
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 23 Jul 2010 19:48:43 +0100 |
parents | 41d6253492ad |
children | d9a1bab1c3f5 |
line diff
1.1 --- a/src/ocl.cc Fri Jul 23 01:22:03 2010 +0100 1.2 +++ b/src/ocl.cc Fri Jul 23 19:48:43 2010 +0100 1.3 @@ -1,10 +1,14 @@ 1.4 +#define OCL_CC_ 1.5 + 1.6 #include <stdio.h> 1.7 #include <stdlib.h> 1.8 #include <string.h> 1.9 +#include <stdarg.h> 1.10 #include <errno.h> 1.11 #include <alloca.h> 1.12 #include <sys/stat.h> 1.13 #include "ocl.h" 1.14 +#include "ocl_errstr.h" 1.15 1.16 1.17 class InitCL { 1.18 @@ -31,6 +35,7 @@ 1.19 static int devcmp(struct device_info *a, struct device_info *b); 1.20 static const char *devtypestr(cl_device_type type); 1.21 static void print_memsize(FILE *out, unsigned long memsz); 1.22 +static const char *clstrerror(int err); 1.23 1.24 1.25 static InitCL initcl; 1.26 @@ -72,7 +77,7 @@ 1.27 1.28 1.29 if(!(mem = clCreateBuffer(ctx, rdwr | CL_MEM_USE_HOST_PTR, sz, buf, &err))) { 1.30 - fprintf(stderr, "failed to create memory buffer (%d)\n", err); 1.31 + fprintf(stderr, "failed to create memory buffer: %s\n", clstrerror(err)); 1.32 return 0; 1.33 } 1.34 1.35 @@ -98,7 +103,7 @@ 1.36 int err; 1.37 mbuf->ptr = clEnqueueMapBuffer(cmdq, mbuf->mem, 1, rdwr, 0, mbuf->size, 0, 0, 0, &err); 1.38 if(!mbuf->ptr) { 1.39 - fprintf(stderr, "failed to map buffer (%d)\n", err); 1.40 + fprintf(stderr, "failed to map buffer: %s\n", clstrerror(err)); 1.41 return 0; 1.42 } 1.43 return mbuf->ptr; 1.44 @@ -116,7 +121,7 @@ 1.45 1.46 int err; 1.47 if((err = clEnqueueWriteBuffer(cmdq, mbuf->mem, 1, 0, sz, src, 0, 0, 0)) != 0) { 1.48 - fprintf(stderr, "failed to write buffer (%d)\n", err); 1.49 + fprintf(stderr, "failed to write buffer: %s\n", clstrerror(err)); 1.50 return false; 1.51 } 1.52 return true; 1.53 @@ -128,7 +133,7 @@ 1.54 1.55 int err; 1.56 if((err = clEnqueueReadBuffer(cmdq, mbuf->mem, 1, 0, sz, dest, 0, 0, 0)) != 0) { 1.57 - fprintf(stderr, "failed to read buffer (%d)\n", err); 1.58 + fprintf(stderr, "failed to read buffer: %s\n", clstrerror(err)); 1.59 return false; 1.60 } 1.61 return true; 1.62 @@ -250,7 +255,7 @@ 1.63 1.64 char *errlog = (char*)alloca(sz + 1); 1.65 clGetProgramBuildInfo(prog, devinf.id, CL_PROGRAM_BUILD_LOG, sz, errlog, 0); 1.66 - fprintf(stderr, "failed to build program: (%d)\n%s\n", err, errlog); 1.67 + fprintf(stderr, "failed to build program: %s\n%s\n", clstrerror(err), errlog); 1.68 1.69 clReleaseProgram(prog); 1.70 prog = 0; 1.71 @@ -275,14 +280,14 @@ 1.72 switch(args[i].type) { 1.73 case ARGTYPE_INT: 1.74 if((err = clSetKernelArg(kernel, i, sizeof(int), &args[i].v.ival)) != 0) { 1.75 - fprintf(stderr, "failed to bind kernel argument: %d (%d)\n", (int)i, err); 1.76 + fprintf(stderr, "failed to bind kernel argument %d: %s\n", (int)i, clstrerror(err)); 1.77 goto fail; 1.78 } 1.79 break; 1.80 1.81 case ARGTYPE_FLOAT: 1.82 if((err = clSetKernelArg(kernel, i, sizeof(float), &args[i].v.fval)) != 0) { 1.83 - fprintf(stderr, "failed to bind kernel argument: %d (%d)\n", (int)i, err); 1.84 + fprintf(stderr, "failed to bind kernel argument %d: %s\n", (int)i, clstrerror(err)); 1.85 goto fail; 1.86 } 1.87 break; 1.88 @@ -292,7 +297,7 @@ 1.89 CLMemBuffer *mbuf = args[i].v.mbuf; 1.90 1.91 if((err = clSetKernelArg(kernel, i, sizeof mbuf->mem, &mbuf->mem)) != 0) { 1.92 - fprintf(stderr, "failed to bind kernel argument: %d (%d)\n", (int)i, err); 1.93 + fprintf(stderr, "failed to bind kernel argument %d: %s\n", (int)i, clstrerror(err)); 1.94 goto fail; 1.95 } 1.96 } 1.97 @@ -338,7 +343,7 @@ 1.98 1.99 int err; 1.100 if((err = clEnqueueNDRangeKernel(cmdq, kernel, dim, 0, global_size, 0, 0, 0, 0)) != 0) { 1.101 - fprintf(stderr, "error executing kernel (%d)\n", err); 1.102 + fprintf(stderr, "error executing kernel: %s\n", clstrerror(err)); 1.103 return false; 1.104 } 1.105 return true; 1.106 @@ -346,13 +351,36 @@ 1.107 1.108 static int select_device(struct device_info *dev_inf, int (*devcmp)(struct device_info*, struct device_info*)) 1.109 { 1.110 - unsigned int i, j, num_dev, sel; 1.111 + unsigned int i, j, num_dev, num_plat, sel, ret; 1.112 cl_device_id dev[32]; 1.113 + cl_platform_id plat[32]; 1.114 1.115 dev_inf->work_item_sizes = 0; 1.116 1.117 + if((ret = clGetPlatformIDs(32, plat, &num_plat)) != 0) { 1.118 + fprintf(stderr, "clGetPlatformIDs failed: %s\n", clstrerror(ret)); 1.119 + return -1; 1.120 + } 1.121 + if(!num_plat) { 1.122 + fprintf(stderr, "OpenCL not available!\n"); 1.123 + return -1; 1.124 + } 1.125 1.126 - clGetDeviceIDs(0, CL_DEVICE_TYPE_ALL, 32, dev, &num_dev); 1.127 + for(i=0; i<num_plat; i++) { 1.128 + char buf[512]; 1.129 + 1.130 + clGetPlatformInfo(plat[i], CL_PLATFORM_NAME, sizeof buf, buf, 0); 1.131 + printf("[%d]: %s", i, buf); 1.132 + clGetPlatformInfo(plat[i], CL_PLATFORM_VENDOR, sizeof buf, buf, 0); 1.133 + printf(", %s", buf); 1.134 + clGetPlatformInfo(plat[i], CL_PLATFORM_VERSION, sizeof buf, buf, 0); 1.135 + printf(" (%s)\n", buf); 1.136 + } 1.137 + 1.138 + if((ret = clGetDeviceIDs(plat[0], CL_DEVICE_TYPE_ALL, 32, dev, &num_dev)) != 0) { 1.139 + fprintf(stderr, "clGetDeviceIDs failed: %s\n", clstrerror(ret)); 1.140 + return -1; 1.141 + } 1.142 printf("found %d cl devices.\n", num_dev); 1.143 1.144 for(i=0; i<num_dev; i++) { 1.145 @@ -457,3 +485,14 @@ 1.146 memsz /= 1024; 1.147 } 1.148 } 1.149 + 1.150 +static const char *clstrerror(int err) 1.151 +{ 1.152 + if(err > 0) { 1.153 + return "<invalid error code>"; 1.154 + } 1.155 + if(err <= -(int)(sizeof ocl_errstr / sizeof *ocl_errstr)) { 1.156 + return "<unknown error>"; 1.157 + } 1.158 + return ocl_errstr[-err]; 1.159 +}