Nothing Special   »   [go: up one dir, main page]

PDC Presentation Update

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

Synchronization Constructs in OpenMP

Data Handling in OpenMP

GROUP MEMBERS:
Haris Mushtaq (21-ARID-576)
Raja Usama Naveed (21-ARID-659 )
Zohaib Qaisar Kayani (21-ARID-687 )
OpenMP: What is it?

OpenMP is an Application Program Interface (API) for:


• explicit
• portable
• shared-memory parallel programming
• in C/C++ and Fortran.
OpenMP consists of:
• compiler directives,
• library routines,
• runtime calls
• environment variables,
It is supported by all major compilers on Unix and Windows platforms:
GNU, IBM, Oracle, Intel, PGI, Absoft, Lahey/Fujitsu, PathScale, HP, MS, Cray
OpenMP Programming Model

• Designed for multi-processor/core, shared memory machines.


• OpenMP programs accomplish parallelism exclusively through
the use of
• threads.
• Programmers has full control over parallelization.
• Consists of a set of #pragmas (Compiler Instructions/Directives
that control how the
• consist of set of programs that control how the program works.
OpenMP Run Time Variables

• Modify/check/get info about the number of threads


omp_get_num_threads() //number of threads in use
omp_get_thread_num() //tells which thread you are
omp_get_max_threads() //max threads that can be used

• Are we in a parallel region?


omp_in_parallel()
• How many processors in the system?
omp_get_num_procs()
• Explicit locks
omp_[set unset]_lock()
And several more...
I hope this is helpful!
OpenMP: Few Syntax Details
• Most of the constructs in OpenMP are compiler directives or
pragmas
For C/C++ the pragmas take the form
#pragma omp construct [clause [clause]...]
For Fortran, the directives take one of the forms
C$OMP construct [clause [clause]...]
!$OMP construct [clause [clause]...]
*$OMP construct [clause [clause]...]
• Header File or Fortran 90 module
#include omp.h
use omp_lib
SOME CODE HINTS
Compiling OpenMP code
• Same code can run on single-core or multi-core machines.
• Compiler directives are picked up ONLY when the program is
instructed to be compiled in OpenMP mode.
• Method depends on the compiler.
G++
$ g++ -o foo foo.c -fopenmp
ICC
$ icc -o foo foo.c -fopenmp
•)

Running OpenMP code


• Controlling the number of threads at runtime
o The default number of threads = number of online processors on the machine
Environment variables:
• C shell: setenv OMP_NUM_THREADS number
• Bash shell: export OMP_NUM_THREADS = number
• Runtime OpenMP function: omp_set_num_threads(number)
• Clause in #pragma omp parallel region: num_threads(number)
Execution Timing:
#include omp.h
stime = omp_get_wtime();
longfunction();
etime = omp_get_wtime();
total = etime-stime;
Thread Creation
Schedule Clause
OpenMp for Parallelization
Sychronization construct
Synchronization Barriers
Data Dependencies

•OpenMP assumes that there is NO data-dependency across jobs


running in parallel.
•When the omp parallel directive is placed around a code block,
it is the
• programmer's responsibility to make sure data dependency is
ruled out.
Synchronization Constructs
Synchronization Constructs
OpenMp Data Scoping
OpenMp private Clause
OpenMP Parallel Programming
• Start with a parallelizable algorithm (Loop level parallelism)
Loop level parallelism
• Implement Serially: Optimized Serial Program
• Test, Debug & Time to solution
• Annotate the code with parallelization and synchronization directives
• Remove Race Conditions, False Sharing
• Test and Debug
• Measure speed-up
Serial Code:

You might also like