简体   繁体   English

使用OpenMP并行化for循环

[英]Using OpenMP to parallelize a for loop

I'm new to OpenMP. 我是OpenMP的新手。 When I parallelize a for loop using 当我使用并行化for循环时

  #pragma omp parallel for num_threads(4)
  for(i=0;i<4;i++){
    //some parallelizable code
  }

Is it guaranteed that every thread takes one and only one value of i ? 是否保证每个线程都只取一个i值? How is the loop work divided among the threads in general when num_threads is not equal to or does not evenly divide the total number of times of the for loop? num_threads不等于for循环的总次数或不平均分配for循环的总次数时,通常如何在线程之间分配循环工作? Is there a command I can use to specify that each thread takes only one value of i , or the number of values of i each thread takes? 是否可以使用命令指定每个线程仅使用i一个值,或者每个线程使用i的值个数?

The work division in a loop construct is decided by the schedule . 循环结构中的工作划分由进度表决定。 If no schedule clause is present, the def-sched-var schedule is used, which is implementation defined. 如果不存在schedule子句,则使用def-sched-var调度,它是实现定义的。

You could use schedule (static, 1) , which in your case guarantees that each thread will get exactly one value. 您可以使用schedule (static, 1) ,在您的情况下,它可以确保每个线程都将恰好获得一个值。

I highly recommend to take a look at the OpenMP specification , Table 2.5 and 2.7.1.1. 我强烈建议您看一下OpenMP规范 ,表2.5和2.7.1.1。

There may be legitimate reasons for making this kind of assumptions, but in general the correctness of your loop code should not depend on this. 进行这种假设可能有正当的理由,但总的来说,循环代码的正确性不应该依赖于此。 Primarily I would treat this as a performance-hint. 首先,我会将其视为性能提示。

Depending on your use-case you may want to consider tasks or just parallel constructs. 根据您的用例,您可能需要考虑任务或只是并行构造。 If you rely such details for loops, make sure it is well specified in the standard, and not just works in your particular implementation. 如果您将此类详细信息用于循环,请确保在标准中对循环进行了详细说明,而不仅仅是在您的特定实现中有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM