• 2019 Update 4
  • 03/20/2019
  • Public Content

Using Data Parallelism

The OpenCL™ standard basic data parallelism uses the Single Program Multiple Data (SPMD) technique. SPMD resembles fragment processing with pixel shaders in the context of graphics.
In this programming model, a kernel executes concurrently on multiple elements. Each element has its own data and its own program counter. If elements are vectors of any kind (for example, four-way pixel values for an RGBA image), consider using vector types.
This section describes how to convert regular C code to an OpenCL program using a simple "hello world" example. Consider the following C function:
void scalar_mul(int n, const float *a, const float *b, float *result) { int i; for (i = 0; i < n; ++i) result[i] = a[i] * b[i]; }
This function performs element-wise multiplication of two arrays,
. Each element in result stores the product of the corresponding elements from arrays
Consider the following:
  • The
    loop consists of two parts: the loop statement that defines the range of operation (a single dimension containing
    elements), and the loop body itself.
  • The basic operation is done on scalar variables (
    data types).
  • Loop iterations are independent.
The same function in OpenCL appears as follows:
__kernel void scalar_mul(__global const float *a, __global const float *b, __global float *result) { int i = get_global_id(0); result[i] = a[i] * b[i]; }
The kernel function performs the same basic element-wise multiplication of two scalar variables. The index is provided by use of a built-in function that gives the global ID, a unique number for each work-item within the grid defined by the
The code itself does not imply any parallelism. Only the combination of the code with the execution over a global grid implements the parallelism of the device.
This parallelization method abstracts the details of the underlying hardware. You can write your code according to the native data types of the algorithm. The implementation takes care of the mapping to specific hardware.

Product and Performance Information


Performance varies by use, configuration and other factors. Learn more at www.Intel.com/PerformanceIndex.