Back
Close

OpenMP

krisrak
640 views
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
hello.cpp

Compiling OpenMP code

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

source /opt/intel/inteloneapi/setvars.sh

icpx -fiopenmp -fopenmp-targets=spir64 hello.cpp
Check OpenMP Compiler Version
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants