Lecture 7-OpenMP-Basics
Lecture 7-OpenMP-Basics
Programming
Presenter: Liangqiong Qu
Assistant Professor
• Send your UID, Email Address, Name for HPC account generation for our
assignment.
- Send UID, email address (with HKU email), and name to Mr. Changbing Zhang
[email protected] for account open before Feb. 21, Friday, 11:59PM!
▪ In C/C++, the directive is based on the sentinel (#pragma omp) construct, and the basic
format of OpenMP directive in C/C++ is as follows (Compiler directive):
#pragma omp directive-name [clause[ [,] clause]...]
structured block of code
• The #pragma omp is a so-called sentinel, which starts an OpenMP directive
• A “structured block of code” refers to a block of code that has a single entry point and a
single exit point. This means the code block must be well-defined and cannot have jumps
(like goto, break, or return) that cause the control flow to exit the block prematurely.
❌ All the above are not a structed block of code due to multiple entrys/exits.
Review of Lecture 6: OpenMP Directives
▪ In C/C++, the directive is based on the sentinel (#pragma omp) construct, and the basic
format of OpenMP directive in C/C++ is as follows (Compiler directive):
#pragma omp directive-name [clause[ [,] clause]...]
structured block of code
• The #pragma omp is a so-called sentinel, which starts an OpenMP directive
• Use the omp_set_num_threads() function in the code to set the number of threads.
▪ omp_get_thread_num routine returns the thread number, within the current team, of the
calling thread. Threads are numbered from 0 (master thread) to N-1.
• Syntax: int omp_get_thread_num(void);
• Data Scope Attribute Clauses are used in conjunction with several directives
(PARALLEL, DO/for, and SECTIONS) to control the scoping of enclosed variables.
• Data Scope Attribute Clauses are effective only within their lexical/static extent,
meaning they are only applicable within the specific scope they are defined in.
❌
Review of Lecture 6: OpenMP Data Scope Attribute Clauses Private
• PRIVATE: private(var) creates a local copy of var for each thread. A new uninitialized
instance is created for each thread.
• The value is uninitialized
• Private copy is not storage-associated with the same variable outside the region
Review of Lecture 6: OpenMP Data Scope Attribute Clauses Firstprivate Clause
• If you want to initialize and assign values to a variable upon entry and exit,
respectively, you can use both `firstprivate` and `lastprivate` for the same
variable.
• But you cannot use the `private` clause and the `firstprivate`/`lastprivate` clauses
together for the same variable.
Thank you very much for choosing this course!
https://forms.gle/zDdrPGCkN7ef3UG5A
28