openmp:结合2个for循环和循环的区别不结合

发布于 2024-10-06 06:06:53 字数 456 浏览 0 评论 0原文

组合 2 个 for 循环并一起并行化和单独并行化有什么区别

示例 1. 不并联在一起

#pragma omp parallel for
for(i = 0; i < 100; i++) {
//.... some code
}

#pragma omp parallel for
for(i = 0; i < 1000; i++) {
//.... some code
}

2. 并联在一起

#pragma omp parallel
{
#pragma omp for
for(i = 0; i < 100; i++) {
//.... some code
}

#pragma omp for
for(i = 0; i < 1000; i++) {
//.... some code
}
}

哪个代码更好,为什么???

What is the difference in combining 2 for loops and parallizing together and parallizing separately

Example
1. not paralleling together

#pragma omp parallel for
for(i = 0; i < 100; i++) {
//.... some code
}

#pragma omp parallel for
for(i = 0; i < 1000; i++) {
//.... some code
}

2. paralleling together

#pragma omp parallel
{
#pragma omp for
for(i = 0; i < 100; i++) {
//.... some code
}

#pragma omp for
for(i = 0; i < 1000; i++) {
//.... some code
}
}

which code is better and why????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹎☆浅夏丿初晴 2024-10-13 06:06:53

人们可能期望在第二次中取得一点小小的胜利,因为一次是对 OMP 线程进行分叉/加入(或功能等效)两次,而不是一次。它是否对您的代码产生任何实际影响是一个经验问题,最好通过测量来回答。

如果两个循环中的工作是独立的,那么第二个循环也可以具有更显着的优势,并且您可以随时启动第二个循环,并且有理由预期第一个循环中会出现一些负载不平衡。在这种情况下,您可以将 nowait 子句添加到第一个 omp for 中,这样,无论谁先完成,都可以立即继续,而不是所有线程都等待直到 for 循环结束开始第二个循环的工作。或者,可以将这两块代码分别放在一个 sectiontask 中。一般来说,您可以很好地控制线程的功能以及它们在并行部分中的执行方式;而一旦你结束了平行部分,你就失去了灵活性——一切都必须连接在一起,然后你就完成了。

One might expect a small win in the second, because one is fork/joining (or the functional equivalent) the OMP threads twice, rather than once. Whether it makes any actual difference for your code is an empirical question best answered by measurement.

The second can also have a more significant advantage if the work in the two loops are independant, and you can start the second at any time, and there's reason to expect some load imbalance in the first loop. In that case, you can add a nowait clause to the firs tomp for and, rather than all threads waiting until the for loop ends, whoever's done first can immediately go on to start working on the second loop. Or, one could put the two chunks of codes each in a section, or task. In general, you have a lot of control over what threads do and how they do it within a parallel section; whereas once you end the parallel section, you lose that flexibility - everything has to join together and you're done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文