/* Create an OpenCL program from a string and compile it. */ cl_program opencl_build_program_from_string (cl_context ctx, cl_device_id dev, const char *program_source, size_t program_size, const char *opencl_options) { int err; cl_program program; char *program_log; size_t log_size; program = clCreateProgramWithSource (ctx, 1, &program_source, &program_size, &err); if (err < 0) { fprintf (stderr, "Could not create the program\n"); exit (EXIT_FAILURE); } err = clBuildProgram (program, 0, NULL, opencl_options, NULL, NULL); if (err < 0) { fprintf (stderr, "Could not build the program.\n"); CL_ERR (clGetProgramBuildInfo (program, dev, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size)); program_log = (char *) malloc (log_size + 1); program_log[log_size] = '\0'; CL_ERR (clGetProgramBuildInfo (program, dev, CL_PROGRAM_BUILD_LOG, log_size + 1, program_log, NULL)); fprintf (stderr, "%s\n", program_log); free (program_log); exit (EXIT_FAILURE); } return program; }
cl_device_id opencl_create_device_2 (cl_device_type dev_type) { cl_platform_id platform; cl_device_id dev; CL_ERR (clGetPlatformIDs (1, &platform, NULL)); CL_ERR (clGetDeviceIDs (platform, dev_type, 1, &dev, NULL)); return dev; }
/* Find a GPU or a CPU associated with the first available platform. * If use_gpu is set, then this function first tries to look for a GPU * in the first available platform. * If this fails or if use_gpu is not set, then it tries to use the CPU. */ cl_device_id opencl_create_device (int use_gpu) { cl_platform_id platform; cl_device_id dev; int err; CL_ERR (clGetPlatformIDs (1, &platform, NULL)); err = CL_DEVICE_NOT_FOUND; if (use_gpu) err = clGetDeviceIDs (platform, CL_DEVICE_TYPE_GPU, 1, &dev, NULL); if (err == CL_DEVICE_NOT_FOUND) CL_ERR (clGetDeviceIDs (platform, CL_DEVICE_TYPE_CPU, 1, &dev, NULL)); return dev; }
static int cdev_setup(struct vir_device *vir_device,dev_t dev,char *node_name) { int ret = 0; cdev_init(&vir_device->cdev,dev); vir_device->head = vir_device->tail = 0; vir_device->cdev.ops=&cdev_fops; vir_device->cdev.owner = THIS_MODULE; vir_device->buf_len = BUF_LEN; vir_device->buf = vmalloc(BUF_LEN); memset(vir_device->buf,0,vir_device->buf_len); ret = cdev_add(&vir_device->cdev, dev, 1); if (ret) { CL_ERR("cdev: cdev_add failed!\n"); cdev_del(&vir_device->cdev); return ret; } sema_init(&vir_device->sem,1); dev = device_create(vir_cdev_class,NULL,dev,NULL,node_name); if(dev == NULL) { ret = -1; cdev_del(&vir_device->cdev); } return ret; }
static int __init cdev_vir_init(void) { int ret=0; dev_t dev; int i; char node_name[NAME_LEN]; printk(KERN_ERR "cdev ringbuffer world!\n"); ret = alloc_chrdev_region(&dev, 0, DEV_NUM, device_name); if (ret) { CL_ERR("cdev: alloc_chardev_region failed!\n"); ret = -1; return ret; } vir_cdev_class = class_create(THIS_MODULE,device_name); if(IS_ERR(vir_cdev_class)) { CL_ERR(" failed in creating class.\n"); ret = -1; goto err_class_create; } CL_PRINT("class_create\n"); vir_devices = kzalloc(sizeof(struct vir_device)*DEV_NUM,GFP_KERNEL); if(vir_devices == NULL) { CL_ERR("kmalloc vir_device \n"); ret = -1; goto err_kzalloc_err; } for(i=0;i<DEV_NUM;i++) { sprintf(node_name,"%s%d",device_name,i); cdev_setup(&vir_devices[i],dev+i,node_name); } goto err_chrdev_region; err_kzalloc_err: class_destroy(vir_cdev_class); err_class_create: unregister_chrdev_region(dev, DEV_NUM); err_chrdev_region: return ret; }
cl_device_id opencl_choose_device (cl_device_type dev_type) { #define MAX_PLATFORMS 10 #define MAX_DEVICES 10 cl_platform_id platforms[MAX_PLATFORMS]; cl_uint platforms_n = 0; cl_device_id devices[MAX_DEVICES]; cl_uint devices_n = 0; // devices per platform CL_ERR (clGetPlatformIDs (MAX_PLATFORMS, platforms, &platforms_n)); if (platforms_n < 1) { printf ("Error: no platforms found while looking for a platform.\n"); exit (EXIT_FAILURE); } cl_uint p; for (p = 0; p < platforms_n; ++p) { print_platform_info (platforms[p]); CL_ERR (clGetDeviceIDs (platforms[p], CL_DEVICE_TYPE_ALL, MAX_DEVICES, devices, &devices_n)); //CL_ERR (clGetDeviceIDs (platforms[p], dev_type, MAX_DEVICES, devices, &devices_n)); cl_uint d; for (d = 0; d < devices_n; ++d) { print_device_info (devices[d]); if (dev_type == opencl_device_type (devices[d])) { printf ("Choose device %i\n", devices[d]); return devices[d]; } } } printf ("No requested type of device found in the system\n"); exit (EXIT_FAILURE); }