Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
Next: Playground

Hello OpenMP

The sections below shows how to write a simple OpenMP program.

Include Header File

OpenMP program will have to include the omp.h header file

#include <omp.h>

OpenMP Offload

#pragma omp target map(from:is_cpu) map(tofrom:data[0:N])

Loop Parallelism

#pragma omp parallel for

Hands-on Demo

The OpenMP example shown on the right will allocate memory for an array which is initialized to some values, next the task of doubling the array values is offloaded to a device and then finally the result is printed out on the host.

Input array is inialized to:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Expected output after computation on device:

0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30

Compiling OpenMP code

To compile a OpenMP program, initialize environment variables and then use icpx to compile as shown below:

source /opt/intel/inteloneapi/

icpx -fiopenmp -fopenmp-targets=spir64 hello.cpp
Check OpenMP Compiler Version
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content