/* * Simple OpenCL demo program * * Copyright (C) 2009 Clifford Wolf * * This program 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 2 of the License, or * (at your option) any later version. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * gcc -o cldemo -std=gnu99 -Wall -lOpenCL -I/usr/include/nvidia-current cldemo.c * */ /* Modified by Kees Verstoep, VU University, to allow runtime platform selection */ #include #include #include #include #include #include #define NUM_DATA 100 #define CL_CHECK(_expr) \ do { \ cl_int _err = _expr; \ if (_err == CL_SUCCESS) \ break; \ fprintf(stderr, "OpenCL Error: '%s' returned %d!\n", #_expr, (int)_err); \ exit(1); /* abort(); */ \ } while (0) #define CL_CHECK_ERR(_expr) \ ({ \ cl_int _err = CL_INVALID_VALUE; \ typeof(_expr) _ret = _expr; \ if (_err != CL_SUCCESS) { \ fprintf(stderr, "OpenCL Error: '%s' returned %d!\n", #_expr, (int)_err); \ exit(1); /* abort(); */ \ } \ _ret; \ }) 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 i; cl_platform_id platforms[100]; cl_uint platforms_n = 0; CL_CHECK(clGetPlatformIDs(100, platforms, &platforms_n)); printf("=== %d OpenCL platform(s) found: ===\n", platforms_n); for (i=0; i= devices_n) return 1; printf("Using device %d\n", device_no); cl_context context; context = CL_CHECK_ERR(clCreateContext(NULL, 1, &devices[device_no], &pfn_notify, NULL, &_err)); const char *program_source[] = { "__kernel void simple_demo(__global int *src, __global int *dst, int factor)\n", "{\n", "int i = get_global_id(0);\n", "dst[i] = src[i] * factor;\n", "}\n" }; cl_program program; program = CL_CHECK_ERR(clCreateProgramWithSource(context, sizeof(program_source)/sizeof(*program_source), program_source, NULL, &_err)); if (clBuildProgram(program, 1, &devices[device_no], "", NULL, NULL) != CL_SUCCESS) { char buffer[10240]; clGetProgramBuildInfo(program, devices[device_no], CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, NULL); fprintf(stderr, "CL Compilation failed:\n%s", buffer); exit(2); /* abort(); */ } /* CL_CHECK(clUnloadCompiler()); */ cl_mem input_buffer; input_buffer = CL_CHECK_ERR(clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int)*NUM_DATA, NULL, &_err)); cl_mem output_buffer; output_buffer = CL_CHECK_ERR(clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(int)*NUM_DATA, NULL, &_err)); int factor = 2; cl_kernel kernel; kernel = CL_CHECK_ERR(clCreateKernel(program, "simple_demo", &_err)); CL_CHECK(clSetKernelArg(kernel, 0, sizeof(input_buffer), &input_buffer)); CL_CHECK(clSetKernelArg(kernel, 1, sizeof(output_buffer), &output_buffer)); CL_CHECK(clSetKernelArg(kernel, 2, sizeof(factor), &factor)); cl_command_queue queue; queue = CL_CHECK_ERR(clCreateCommandQueue(context, devices[device_no], 0, &_err)); for (i=0; i