/* * cl-utils.c * * OpenCL utility functions * * Copyright © 2012-2014 Deutsches Elektronen-Synchrotron DESY, * a research centre of the Helmholtz Association. * * Authors: * 2010-2014 Thomas White * * This file is part of CrystFEL. * * CrystFEL is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * CrystFEL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with CrystFEL. If not, see . * */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #ifdef HAVE_CL_CL_H #include #else #include #endif #include "utils.h" const char *clError(cl_int err) { switch ( err ) { case CL_SUCCESS : return "no error"; case CL_INVALID_PLATFORM : return "invalid platform"; case CL_INVALID_KERNEL : return "invalid kernel"; case CL_INVALID_ARG_INDEX : return "invalid argument index"; case CL_INVALID_ARG_VALUE : return "invalid argument value"; case CL_INVALID_MEM_OBJECT : return "invalid memory object"; case CL_INVALID_SAMPLER : return "invalid sampler"; case CL_INVALID_ARG_SIZE : return "invalid argument size"; case CL_INVALID_COMMAND_QUEUE : return "invalid command queue"; case CL_INVALID_CONTEXT : return "invalid context"; case CL_INVALID_VALUE : return "invalid value"; case CL_INVALID_EVENT_WAIT_LIST : return "invalid wait list"; case CL_MAP_FAILURE : return "map failure"; case CL_MEM_OBJECT_ALLOCATION_FAILURE : return "object allocation failure"; case CL_OUT_OF_HOST_MEMORY : return "out of host memory"; case CL_OUT_OF_RESOURCES : return "out of resources"; case CL_INVALID_KERNEL_NAME : return "invalid kernel name"; case CL_INVALID_KERNEL_ARGS : return "invalid kernel arguments"; case CL_INVALID_WORK_GROUP_SIZE : return "invalid work group size"; case CL_IMAGE_FORMAT_NOT_SUPPORTED : return "image format not supported"; case CL_INVALID_WORK_DIMENSION : return "invalid work dimension"; default : return "unknown error"; } } static char *get_device_string(cl_device_id dev, cl_device_info info) { 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; /* 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