0% found this document useful (0 votes)
58 views12 pages

2.3-DD2356-OpenMP Definitions

This document defines OpenMP, an API that supports shared-memory multiprocessing programming. OpenMP uses compiler directives to express parallelism and is used for shared memory systems like multicore computers. It provides functions and environment variables to control thread numbers and scheduling. The examples demonstrate basic OpenMP programs and compilation.

Uploaded by

Daniel Araújo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views12 pages

2.3-DD2356-OpenMP Definitions

This document defines OpenMP, an API that supports shared-memory multiprocessing programming. OpenMP uses compiler directives to express parallelism and is used for shared memory systems like multicore computers. It provides functions and environment variables to control thread numbers and scheduling. The examples demonstrate basic OpenMP programs and compilation.

Uploaded by

Daniel Araújo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

DD2356 – OpenMP Definitions

Stefano Markidis
OpenMP

• OpenMP (Open Multi-Processing) is an API that supports shared-memory


multiprocessing programming in C, C++, and Fortran on several systems.
– Only used for shared memory systems, e.g. one node of supercomputer
– Can’t be used for distributed memory
– MPI for that; MPI can be used with OpenMP
– Mostly a compiler technology
– We need a compiler to support it

2
OpenMP - The Whole Cathedral

End User
layer
User
Application

Directives, Environment
Prog.
layer

OpenMP library
Compiler variables
System

OpenMP Runtime library


layer

OS/system support for shared memory and threading


Proc1 Proc2 Proc3 ProcN
HW

Shared Address Space

3
Our Focus

End User
layer
User

Application
Prog. Layer

Directives, Environment
OpenMP library
Compiler variables

4
Compiler Directives

A directive pragma (from "pragmatic") is a language construct instructing


compiler how process its input.
Examples of directives to C/C++ compiler:
• #define substitutes a preprocessor macro
• #include inserts a particular header from another file
• …
• #pragma issues special commands to the compiler, using a standardized
method

5
OpenMP Directives

Directives are used to express parallelism in OpenMP:


• Directives for C and C++ begin with #pragma omp construct
• Directives in Fortran begin with !$omp construct

6
OpenMP Library
OpenMP Function prototypes and types in the file: #include <omp.h>

Some of the library functions we will use are to:


• modify/check the number of threads:
• omp_set_num_threads(), omp_get_num_threads(),
omp_get_thread_num(), omp_get_max_threads()
• check if we are in active parallel region:
• omp_in_parallel()
• dynamically vary the number of threads
• omp_set_dynamic, omp_get_dynamic()
• check the number of processors in the system
• omp_num_procs()

7
OpenMP Environment Variables

The most important environment variables you could use when running a code compiled
with OpenMP are to:
• Set the default number of threads to use
• OMP_NUM_THREADS int_literal
• Process binding is enabled if this variable is true … i.e. if true the runtime will not
move threads around between processors.
• OMP_PROC_BIND true | false
• Control how “omp for schedule(RUNTIME)” loop iterations are scheduled
• OMP_SCHEDULE schedule [chunk_size]

8
How to set an environment variable in Linux/Mac?
We can set the number of threads using the environment variable OMP_NUM_THREADS.

export OMP_NUM_THREADS=<number of threads to use>


(bash shell)

setenv OMP_NUM_THREADS <number of threads to use>


(csh or tcsh shell)

9
Exercise 1: our first OpenMP code

#include “omp.h” OpenMP include file


void main()
{
Parallel region with default
#pragma omp parallel number of threads
{
Library function to return thread ID
int ID = omp_get_thread_num();
printf(“ hello(%d) ”, ID);
printf(“ world(%d) \n”, ID);
} end of parallel region
}

10
Exercise 1: Compile OpenMP with OpenMP flag

Different compilers have different flags for OpenMP

Switches for compiling and linking

gcc/g++ –fopenmp hello_world.c gcc


icc/icpc –openmp hello_world.c intel (linux)
cc –openmp hello_world.c Cray
pgcc –mp hello_world.c pgi

11
Exercise 1

• How do I change the number of threads ?


• What happens if I forget the OpenMP flag at the compilation ?

12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy