• Aucun résultat trouvé

5 – Introduction to OpenCL

N/A
N/A
Protected

Academic year: 2022

Partager "5 – Introduction to OpenCL"

Copied!
4
0
0

Texte intégral

(1)

Embedded Linux – GSE5

- 1 -

5 – Introduction to OpenCL

Objectives

• Introduction to OpenCL and GPU hardware acceleration.

Achievement

• Compare performance improvements of CPU (C++ code) against GPU acceleration (OpenCL).

1.1 Introduction

1.1.1 OpenCL

The Graphics Processors (also called GPUs) are hardware accelerators that can also be used for General Purpose Graphics Processing Unit (GPGPU) capabilities. OpenCL (Open Computing Language) is an open standard for parallel programming of heterogeneous computational resources at processor level.

Figure 1. OpenCL platform model.

The Platform model (Figure 1) consists of a host connected to one or more OpenCL compute devices. A compute device is divided into one or more compute units (CUs) which are further divided into one or more processing elements (PEs). Computations on a device occur within the processing elements.

The Execution model of an OpenCL program occurs in two parts: a kernel, basic unit of executable code which is executed on one or more OpenCL devices, and a host program. A host program defines the context for the kernels and manages their execution. When a kernel is submitted for execution by the host, an index space is defined. An instance of the kernel executes for each point in this index space. This kernel instance is called a work-item and is identified by its point in the index space, which provides a global ID for the work-item.

1.1.2 Exynos 5422

The Samsung Exynos 5422 SoC is representative of new generations of embedded

heterogeneous high-performance computing devices typically used in current smartphones

(2)

Embedded Linux – GSE5

- 2 -

and tablets. The CPU is made of ARM big.LITTLE architecture combining a Cortex™-A15 2.0GHz quad core cluster and a Cortex™-A7 quad core cluster with a Mali-T628 GPU.

Figure 2. ARM Mali-T628 GPU.

The ARM® Mali™-T628 GPU (Figure 2) offers scalability from one to eight cores (Processing Elements) and supports programming with the OpenCL 1.2 Full profile API.

Description of the openCL API is available in OpenCL 1.2 Reference Pages:

https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/

You can also retrieve useful information on you GPU configuration with command clinfo .

1.2 C++ vector addition

The following code has to be created on the Exynos boards under

Developement/LABS/TP_OPENCL/

The following C++ program vector_add_c.cpp adds two integer arrays.

#include <iostream> /* cout */

using namespace std;

int main(void) {

// Create two input vectors int i, j;

const int VECTOR_SIZE = 1024*1024;

int *inputA = new int[VECTOR_SIZE];

int *inputB = new int[VECTOR_SIZE];

int *outputC = new int[VECTOR_SIZE];

for(i = 0; i < VECTOR_SIZE; i++) { inputA[i] = i;

inputB[i] = VECTOR_SIZE - i;

}

// Computes vector_add

(3)

Embedded Linux – GSE5

- 3 -

for(i = 0; i < VECTOR_SIZE; i++)

outputC[i] = inputA[i] + inputB[i];

// Display last 100 results to the screen for (int i = 0; i < 100 /*VECTOR_SIZE*/; i++)

cout << inputA[VECTOR_SIZE-100+i] << " + " << inputB[VECTOR_SIZE- 100+i] << " = " << outputC[i] << "\n";

delete[] inputA;

delete[] inputB;

delete[] outputC;

return 0;

}

Test this program on the Exynos board and add the necessary code to measure the execution time of vector addition (using gettimeofday ).

1.3 OpenCL vector addition

The objective is to derive an OpenCL accelerated version of this code and to check speedup improvements. The principle is to replace the original piece of C++ code computing the vector addition:

for(i = 0; i < VECTOR_SIZE; i++)

outputC[i] = inputA[i] + inputB[i];

by an OpenCL equivalent.

To let OpenCL process this operation in parallel on the compute device(s), we need to define a kernel. The kernel is the OpenCL function which will run on the compute device(s).

The kernel corresponding to the vector addition operation is given below:

__kernel void vector_add_kernel( __global int* restrict inputA, __global int* restrict inputB, __global int* restrict output) {

/*

* Set i to be the ID of the kernel instance.

* If the global work size (set by clEnqueueNDRangeKernel) is n, * then n kernels will be run and i will be in the range [0, n - 1].

*/

int i = get_global_id(0);

/* Use i as an index into the three arrays. */

output[i] = inputA[i] + inputB[i];

}

This kernel is defined in a separate file named vector_add_opencl.cl .

A new C++ program vector_add_opencl.cpp (i.e. the host CPU program) has to setup

and control the execution of previous OpenCL kernel on the compute device (GPU).

(4)

Embedded Linux – GSE5

- 4 -

It exposes the following basic steps described in program vector_add_opencl.cpp

which remain to be completed:

1. Set up OpenCL environment a. Create Context

b. Create Command Queue c. Create Program

d. Create kernel 2. Set up memory/data

a. Create memory buffers b. Initialize the input data c. Set the kernel arguments 3. Execute the kernel instances

a. Define the number of kernel instances and enqueue the kernel b. Wait for kernel execution completion

4. After execution

a. Retrieve results

b. Release OpenCL objects

Complete program vector_add_opencl.cpp . Run and check the correctness of vector addition results.

Add execution time measurement using clGetEventProfilingInfo (see OpenCL 1.2 Reference Pages). Run and compare with the original C++ code performance.

Complete your lab report with explanations addressing the different steps involved to setup an OpenCL program.

Report execution improvements for different vector sizes. Give a conclusion on the limits

of GPU acceleration.

Références

Documents relatifs

When we compute the one-dimensional algorithm along x, the work- items access memory in a coalescing way and thus the memory bandwidth is optimal.. With the same data organization

to advanced quantum chemistry methods such as Natural Resonance Theory (NRT) based on the Natural Bond Orbitals approach.[8, 9, 10, 11] However, it lacks of physics and does not

Firstly, a program can be implemented on FPGAs using two different kernel types suited for task or data parallelism, and this generic characteristic can be used over a wide range

Multiple creators could be useful for example to create the same successive stream objects multiple times in the same order, instead of storing them in an array and reusing them, or

Gauvreau, Time-domain impedance formulation for transmission line matrix modelling of outdoor sound propagation, J. Miki, Acoustical properties of porous materials - Modifications

With this in mind, FPGA Manufacturers like Xilinx and Intel are pushing for an FPGA resurgence, offering software suites (called SDAccel and Intel FPGA SDK for OpenCL, respectively)

Après avoir identifié les recommandations s’appliquant aux éducateurs physiques, nous allons présenter ci-dessous les points forts et les limites identifiés dans notre travail. Notre

Optimizing OpenCL Implementation of Deep Convolutional Neural Network on FPGA.. Yuran Qiao, Junzhong Shen, Dafei Huang, Qianming Yang, Mei Wen,