Making OpenCL? Simple with Haskell - AMD Developer Central
Making OpenCL? Simple with Haskell - AMD Developer Central Making OpenCL? Simple with Haskell - AMD Developer Central
HELLO WORLD - HOST PROGRAM // create the OpenCL context on a GPU device cl_context = clCreateContextFromType(0, CL_DEVICE_TYPE_GPU, NULL, NULL, NULL); // get the list of GPU devices associated with context clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, devices = malloc(cb); 8 | Making OpenCL Simple | January, 2011 | Public NULL, &cb); clGetContextInfo(context, CL_CONTEXT_DEVICES, cb, devices, NULL); // create a command-queue cmd_queue = clCreateCommandQueue(context, devices[0], 0, NULL); memobjs[0] = clCreateBuffer(context,CL_MEM_WRITE_ONLY, sizeof(cl_char)*strlen(“Hello World”, NULL, NULL); // create the program program = clCreateProgramWithSource(context, 1, &program_source, NULL, NULL); // build the program err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL); // create the kernel kernel = clCreateKernel(program, “vec_add”, NULL); // set the args values err = clSetKernelArg(kernel, 0, (void *) &memobjs[0], sizeof(cl_mem)); // set work-item dimensions global_work_size[0] = strlen(“Hello World”);; // execute kernel err = clEnqueueNDRangeKernel(cmd_queue, kernel, 1, NULL, global_work_size, NULL, 0, NULL, NULL); // read output array err = clEnqueueReadBuffer(cmd_queue, memobjs[0], CL_TRUE, 0, strlen(“Hello World”) *sizeof(cl_char), dst, 0, NULL, NULL);
HELLO WORLD - HOST PROGRAM // create the OpenCL context on a GPU device cl_context = clCreateContextFromType(0, CL_DEVICE_TYPE_GPU, NULL, NULL, NULL); Define platform and queues // get the list of GPU devices associated with context clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, devices = malloc(cb); 9 | Making OpenCL Simple | January, 2011 | Public NULL, &cb); clGetContextInfo(context, CL_CONTEXT_DEVICES, cb, devices, NULL); // create a command-queue Define Memory objects cmd_queue = clCreateCommandQueue(context, devices[0], 0, NULL); // allocate the buffer memory objects memobjs[0] = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(cl_char)*strlen(“Hello World”), srcA, NULL);} // create the program program = clCreateProgramWithSource(context, 1, &program_source, NULL, NULL); Create the program Build the program // build the program err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL); // create the kernel kernel = clCreateKernel(program, “vec_add”, NULL); Create and setup kernel // set the args values err = clSetKernelArg(kernel, 0, (void *) &memobjs[0], sizeof(cl_mem)); // set work-item dimensions global_work_size[0] = n; // execute kernel err = clEnqueueNDRangeKernel(cmd_queue, kernel, 1, NULL, global_work_size, NULL, 0, NULL, NULL); // read output array err = clEnqueueReadBuffer(context, memobjs[2], CL_TRUE, 0, n*sizeof(cl_float), Execute the dst, kernel 0, NULL, NULL); Read results on the host
- Page 1 and 2: Making OpenCL Simple with Haskell B
- Page 3 and 4: AGENDA Motivation Whistle stop intr
- Page 5 and 6: OPENCL PROGRAM STRUCTURE 5 | Making
- Page 7: HELLO WORLD OPENCL C SOURCE __const
- Page 11 and 12: USING HASKELL OUR GOAL IS TO WRITE
- Page 13 and 14: LEARN FROM COMMON USES In OpenCL w
- Page 15 and 16: IT’S A HETEROGENEOUS WORLD A mod
- Page 17 and 18: OPENCL EXECUTION MODEL An OpenCL a
- Page 19 and 20: THE BIG IDEA BEHIND OPENCL OpenCL e
- Page 21 and 22: OPENCL MEMORY MODEL •Private Memo
- Page 23 and 24: OPENCL SUMMARY __kernel void dp_mul
- Page 25 and 26: PROGRAMMING KERNELS: OPENCL C Deri
- Page 27 and 28: PROGRAMMING KERNELS: DATA TYPES Sca
- Page 29 and 30: THE BASICS - HELLO WORLD main = do
- Page 31 and 32: MAPPING OPENCL API getPlatforms ::
- Page 33 and 34: Lifting to something more in the sp
- Page 35 and 36: MOVE TO AN IMPLICIT MODEL 35 | Maki
- Page 37 and 38: CONTEXTUAL/QUEUED MONADS Function l
- Page 39 and 40: EMBEDDING MONADS In fact, the only
- Page 41 and 42: 41 | Making OpenCL Simple | January
- Page 43 and 44: HELLO WORLD REVISITED import Langua
- Page 45 and 46: REALLY WE WOULD LIKE TO WRITE impor
- Page 47 and 48: WHAT WE REALLY WANT Build a delaye
- Page 49 and 50: Issues we face using Haskell at AMD
- Page 51 and 52: USING HASKELL AT AMD Many Universi
- Page 53: DISCLAIMER The information presente
HELLO WORLD - HOST PROGRAM<br />
// create the <strong>OpenCL</strong> context on a GPU device<br />
cl_context = clCreateContextFromType(0,<br />
CL_DEVICE_TYPE_GPU, NULL, NULL, NULL);<br />
// get the list of GPU devices associated <strong>with</strong> context<br />
clGetContextInfo(context, CL_CONTEXT_DEVICES, 0,<br />
devices = malloc(cb);<br />
8 | <strong>Making</strong> <strong>OpenCL</strong> <strong>Simple</strong> | January, 2011 | Public<br />
NULL, &cb);<br />
clGetContextInfo(context, CL_CONTEXT_DEVICES, cb,<br />
devices, NULL);<br />
// create a command-queue<br />
cmd_queue = clCreateCommandQueue(context, devices[0],<br />
0, NULL);<br />
memobjs[0] = clCreateBuffer(context,CL_MEM_WRITE_ONLY,<br />
sizeof(cl_char)*strlen(“Hello World”, NULL,<br />
NULL);<br />
// create the program<br />
program = clCreateProgramWithSource(context, 1,<br />
&program_source, NULL, NULL);<br />
// build the program<br />
err = clBuildProgram(program, 0, NULL, NULL, NULL,<br />
NULL);<br />
// create the kernel<br />
kernel = clCreateKernel(program, “vec_add”, NULL);<br />
// set the args values<br />
err = clSetKernelArg(kernel, 0, (void *) &memobjs[0],<br />
sizeof(cl_mem));<br />
// set work-item dimensions<br />
global_work_size[0] = strlen(“Hello World”);;<br />
// execute kernel<br />
err = clEnqueueNDRangeKernel(cmd_queue, kernel, 1,<br />
NULL, global_work_size, NULL, 0, NULL, NULL);<br />
// read output array<br />
err = clEnqueueReadBuffer(cmd_queue, memobjs[0],<br />
CL_TRUE, 0, strlen(“Hello World”) *sizeof(cl_char),<br />
dst, 0, NULL, NULL);