Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to OpenCL


Published on

Brief introduction to OpenCL. Included a demo showing the source code of a vector addition.

Published in: Technology
  • Be the first to comment

Introduction to OpenCL

  1. 1. INTRODUCTIONTO OPENCLUnai LopezIntelligent Systems GroupDepartment of Computer Architecture & TechnologyUniversity of the Basque Country
  2. 2. Outline1)  Introduction2)  Programming Basics3)  “Hello World”4)  Final remarks
  3. 3. OpenCL•  Standard for the development of data parallel applications•  Most used for the development of GPGPU applications: General Purpose computing on Graphics Processing Units•  A GPU is comprised of hundreds of compute cores nVidia GTX 285 (240 Compute cores) nVidia GT200b Architecture•  Specialized for massively data parallel computation
  4. 4. OpenCL & GPGPU•  GPGPU: Take advantage of GPU’s computing power to make massively parallel applications•  Parallel applications with huge acceleration in Molecular Dynamics, Image Processing, Evolutionary Computation,…•  All cases based on data parallelism: each thread processes a subset of the data•  For example, a vector addition: A + B || C Thread ID 0 1 2 3 4 5 6 7 8 9 10 11
  5. 5. OpenCL•  Furthermore, OpenCL provides portability: same code can run on different architectures•  For example:Intel Core i5 CPU STICell B/E Intel Xeon Phi AMD HD 6950 GPU4 cores @ 2’5 Ghz 8 cores @ 3,2 Ghz 50 cores @ 1 Ghz 1408 cores @ 800 Mhz
  6. 6. OpenCL•  Provides the following abstraction: A compute device is composed by compute units•  OpenCL platform: Host + Compute Devices•  Each manufacturer provides an SDK: •  NVIDIA SDK for GPUs •  AMD APP for CPUs/GPU •  Intel for CPUs •  IBM for PowerPC and Cell B/E
  7. 7. Programming Basics•  Kernel: function that defines the behavior of each thread•  For example, kernel for vector addition: __kernel void sumKernel ( __global int* a, __global int* b, __global int* c) { int i = get_global_id(0); c[i] = a[i] + b[i]; }•  Written in OpenCL-C: ANSI-C + Set of kernel functions, e.g.: •  get_global_id: obtains thread index •  barrier: synchronizes threads
  8. 8. Programming Basics•  An OpenCL applications consists of: Kernel file (OpenCL-C): problem computation Host code(C): kernel management•  Basic host application flow: 1.  Load and Compilation of kernel 2.  Data copy from host to device (e.g. from CPU to GPU) 3.  Execution of kernel 4.  Data copy from device to host 5.  Release kernels and data from device memory•  Execution using command queue in each device
  9. 9. Programming Basics•  Host code: programmed using OpenCL API•  API Calls, such as: •  clCreateProgramWithSource: Load kernel from char* •  clBuildProgram: Compile kernel •  clSetKernelArgs: Set kernel arguments for the device •  clEnqueueWriteBuffer/clEnqueueRead: Copy data vector to device •  clEnqueueNDRangerKernel: Launch kernel in device•  API Types, such as: •  cl_mem: Pointer to device memory objects •  cl_program: Kernel object •  cl_float / cl_int / cl_uint: Redefinition of C types
  10. 10. Hello World•  Implementation of simple vector addition in OpenCL•  Checks for default platform and device in the system•  Modify Makefile with proper paths in each system•  Run: vectorAdd <size_of_vector>
  11. 11. Final Remarks•  OpenCL does not provide performance portability•  Alternative to NVIDIA CUDA: Programming paradigm for NVIDIA GPU cards•  Combinable with other parallel programming models: •  OpenMP for SMPs / MPI for MPPs•  Huge ecosystems for OpenCL, e.g. OpenACC: Develop GPGPU applications using directives #pragma acc kernels for(i = 0; i< N; i++) c[i] = b[i] + a[i];
  12. 12. More about OpenCL•  Before starting to develop take a look at: •  Context, command queues, events,…•  Documentation •  Khronos Group: Maintainers of OpenCL •  OpenCL Best practices guide in CUDA/AMD SDKs •  Programming Massively Parallel Processors (Book for CUDA)•  OpenCL sample applications: •  Most SDKs include example OpenCL applications •  Rodinia: •  Parboil:
  13. 13. INTRODUCTIONTO OPENCLUnai Lopez – ulopez009@ehu.esIntelligent Systems GroupDepartment of Computer Architecture & TechnologyUniversity of the Basque Country