Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
платформа.docx
Скачиваний:
5
Добавлен:
01.07.2025
Размер:
2 Мб
Скачать
☆

Parameters. Clause is any of the following clauses:

if (exp). When the if argument is specified, the program code executes in parallel only if the scalar expression represented by exp evaluates to a nonzero value at run time.

private (list). Declares the scope of the data variables in list to be private to each thread. Data variables in list are separated by commas.

num_threads (int_exp). The value of int_exp is an integer expression that specifies the number of threads to use for the parallel region.

shared (list). Declares the scope of the comma-separated data variables in list to be shared across all threads.

1.15.Critically evaluate locks (mutexes) in OpenMp

A lock is somewhat similar to a critical section: it guarantees that some instructions can only be performed by one process at a time. However, a critical section is indeed about code; a lock is about data. With a lock you make sure that some data elements can only be touched by one process at a time.

A thread lock is a form of mutual exclusion. A lock in OpenMP is an object (omp_lock_t) that can be held by at most one thread at a time. The four operations are:

omp_init_lock(omp_lock_t *) – initialize a lock

• omp_set_lock(omp_lock_t*) – wait until the lock is available, then set it. No other thread can set the lock until it is released

• omp_unset_lock(omp_lock_t*) – unset (release) the lock

• omp_destroy_lock(omp_lock_t*) – The reverse of omp_init_lock.

1.16.Critically evaluate Nested parallel sections in OpenMP. Directives specify description single and master in OpenMP. OpenMP parallel regions can be nested inside each other. If nested parallelism is disabled, then the new team created by a thread encountering a parallel construct inside a parallel region consists only of the encountering thread. If nested parallelism is enabled, then the new team may consist of more than one thread.

The OpenMP runtime library maintains a pool of threads that can be used as slave threads in parallel regions. When a thread encounters a parallel construct and needs to create a team of more than one thread, the thread will check the pool and grab idle threads from the pool, making them slave threads of the team. The master thread might get fewer slave threads than it needs if there is not a sufficient number of the idle threads in the pool. When the team finishes executing the parallel region, the slave threads return to the pool.

Nested parallelism can be controlled at runtime by setting various environment variables prior to execution of the program.

Nested parallelism can be enabled or disabled by setting the OMP_NESTED environment variable or calling omp_set_nested() function.

#pragma omp single{

}

int main(){

omp_set_dynamic(0);

#pragma omp parallel num_threads(2){

report_num_threads(1);

#pragma omp parallel num_threads(2){

report_num_threads(2);

#pragma omp parallel num_threads(2){

report_num_threads(3);} }}

return(0);}