Introduction to OpenCL

Introduction

OpenCL (Open Computing Language) is a framework for writing programs that execute across heterogeneous platforms consisting of central processing units (CPUs), graphics processing units (GPUs), digital signal processors (DSPs), field-programmable gate arrays (FPGAs) and other processors or hardware accelerators. OpenCL specifies programming languages (based on C99, C++14 and C++17) for programming these devices and application programming interfaces (APIs) to control the platform and execute programs on the compute devices. OpenCL provides a standard interface for parallel computing using task- and data-based parallelism.

Setting up a development environment

To set up a development environment for OpenCL, you will need the following:

  • A computer with a supported GPU
  • A compiler that supports OpenCL
  • An OpenCL development toolkit

The following are some popular compilers and development toolkits:

  • NVIDIA CUDA Toolkit
  • AMD APP SDK
  • Intel OpenCL SDK

Once you have installed the compiler and development toolkit, you can create a new project and start writing OpenCL programs.

Simple example

The following is a simple example of an OpenCL program that prints “Hello, World!” to the console:

#include <stdio.h>
#include <stdlib.h>

#include <CL/cl.h>

int main() {

  // Create a context
  cl_context context = clCreateContext(NULL, 1, &device, NULL, NULL, NULL);

  // Create a command queue
  cl_command_queue command_queue = clCreateCommandQueue(context, device, 0, NULL);

  // Create a buffer for the string "Hello, World!"
  size_t string_size = sizeof("Hello, World!");
  cl_mem string_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_HOST_NO_ACCESS, string_size, NULL, NULL);

  // Write the string to the buffer
  clEnqueueWriteBuffer(command_queue, string_buffer, CL_TRUE, 0, string_size, "Hello, World!", 0, NULL, NULL);

  // Create a kernel
  const char *kernel_source =
    "void print_string(void) {"
    "  printf(\"Hello, World!\n\");"
    "}";

  cl_program program = clCreateProgramWithSource(context, 1, &kernel_source, NULL, NULL);

  // Build the kernel
  clBuildProgram(program, 1, &device, NULL, NULL, NULL);

  // Create a kernel object
  cl_kernel kernel = clCreateKernel(program, "print_string", NULL);

  // Execute the kernel
  clSetKernelArg(kernel, 0, sizeof(cl_mem), &string_buffer);
  clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &string_size, NULL, 0, NULL, NULL);

  // Finish the command queue
  clFinish(command_queue);

  // Release resources
  clReleaseMemObject(string_buffer);
  clReleaseProgram(program);
  clReleaseKernel(kernel);
  clReleaseCommandQueue(command_queue);
  clReleaseContext(context);

  return 0;
}

This program will print “Hello, World!” to the console.