您的位置:首页 > 运维架构

Shared-Memory Programming with OpenMP

2018-01-17 14:58 597 查看
In OpenMP parlance, the collection of threads executing the parallel directive– the original thread the new threads– is called a team, the original thread is called the master, and the additional threads are called slaves.

When the block of code is completed,there’s an implicit barrier.

A loop that is parallelized with a parallel for directive, the default scope of the loop variable is private; in our code, each thread in the team has its own copy of i.

In fact, OpenMP will only parallelize for loops that are in canonical form.

5.5.3 data dependence

for(i-2;i<n;i++)
fibro[i] = fibo[i-1] + fibo[i-2];


There is no problem with the parallelization.

pragma omp parallel for num_threads(thread_count)
for(i=0; i<n; i++){
x[i] = a+i*h;
y[i] = exp(x[i]);


Since the computation of x[i] and its subsequent use will always be assigned to the same thread.

5.6.2 Odd-even transposition sort

Like the parallel directive, the parallel for directive has an implicit barrier at the end of the loop, so none of the threads will proceed to the next phase, phase p+1, until all of the threads have completed the current phase, phase p.

5.7 Scheduling Loops

In OpenMP, assigning iterations to threads is called scheduling and the schedule clause can be used to assign iterations in either a parallel for or a for directive.

In general, the schedule clause has the form.

schedule(<type>,[<chunksize>])

For a static schedule, the system assigns chunks of chunksize iterations to each thread in round-robin fashion.

In a dynamic schedule, the iterations are also broken up into chunks of chunksize consecutive iterations. Each thread executes a chunk, and when a thread finishes a chunk, it requests another one form the run-time system. This continues until all the iterations are completed.

In a guide schedule, as chunks are completed, the size of the new chunks decreases.

When schedule(runtime) is specified, the system uses the environment variable OMP_SCHEDULE to determine at run-time how to schedule the loop.

5.8 Producers and Consumers

5.8.6 startup

Fortunately, OpenMP provides one as an explicit barrier.

pragam ompbarriers


5.8.7 The atomic directive

The idea behind the atomic directive is that many processors provides a special load-modify-store instruction, and a critical section that only dose a load-modify-store can be protected much more efficiently by using this special instruction rather than the constructs that are used to protect more general critical sections.

5.10 Thread-safety

A block of code is thread-safe if it can be simultaneously executed by multiple threads without causing problems.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  openmp