Shared-Memory Programming with OpenMP

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值