OpenMP的 - 循环线程分配 [英] OpenMP - for loop thread assignment

查看:133
本文介绍了OpenMP的 - 循环线程分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个指数0到n-1的阵列。有没有一种方法来选择每个线程会处理哪些细胞?例如线程0将处理单元0和5,线程1会处理单元1和6等..

Suppose I have an array with indices 0..n-1. Is there a way to choose which cells each thread would handle? e.g. thread 0 would handle cells 0 and 5 , thread 1 would handle cells 1 and 6 and so on..

推荐答案

您甚至可以更明确:

#pragma omp parallel
{
   int nth = omp_get_num_threads();
   int ith = omp_get_thread_num();
   for (int i=ith; i<n; i+=nth)
   {
      // handle cell i.
   }
}

这应该做的正是你想要的:第i个线程处理单元第i个,第i +第n个,第i + 2 * n次,第i + 3 * n个等

this should do exactly what you want: thread ith handles cell ith, ith+nth, ith+2*nth, ith+3*nth and so on.

这篇关于OpenMP的 - 循环线程分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆