#include #include #include #ifdef __APPLE__ #include #else #include #endif void pfn_notify(const char *errinfo, const void *private_info, size_t cb, void *user_data) { fprintf(stderr, "OpenCL Error (via pfn_notify): %s\n", errinfo); } int main(int argc, char **argv){ int plat = 0; int dev = 0; /* set up CL */ cl_int err; cl_platform_id platforms[100]; cl_uint platforms_n; cl_device_id devices[100]; cl_uint devices_n ; cl_context context; cl_command_queue queue; cl_device_id device; /* get list of platform IDs (platform == implementation of OpenCL) */ clGetPlatformIDs(100, platforms, &platforms_n); if( plat > platforms_n) { printf("ERROR: platform %d unavailable \n", plat); exit(-1); } // find all available device IDs on chosen platform (could restrict to CPU or GPU) cl_uint dtype = CL_DEVICE_TYPE_ALL; clGetDeviceIDs( platforms[plat], dtype, 100, devices, &devices_n); printf("devices_n = %d\n", devices_n); if(dev>=devices_n){ printf("invalid device number for this platform\n"); exit(0); } // choose user specified device device = devices[dev]; // make compute context on device context = clCreateContext((cl_context_properties *)NULL, 1, &device, &pfn_notify, (void*)NULL, &err); // create command queue queue = clCreateCommandQueue(context, device, CL_QUEUE_PROFILING_ENABLE, &err); #if 0 // build kernel function const char *sourceFileName = "foo.cl"; const char *functionName = "foo"; // read in text from source file struct stat statbuf; FILE *fh = fopen(sourceFileName, "r"); if (fh == 0){ printf("Failed to open: %s\n", sourceFileName); throw 1; } /* get stats for source file */ stat(sourceFileName, &statbuf); /* read text from source file and add terminator */ char *source = (char *) malloc(statbuf.st_size + 1); fread(source, statbuf.st_size, 1, fh); source[statbuf.st_size] = '\0'; #else const char *source = "__kernel void foo(int N, __global float *x){" " " " int id = get_global_id(0); " " " " if(id