在C中使用GCC和OMP的特定线程顺序 [英] Specific thread order in C using GCC and OMP
问题描述
我需要制作4个团队,每个团队有4个线程,每个线程使用连续的处理器。
我期望的结果是,例如:
0团队0主题0处理器:0
团队0主题1处理器:1
团队0主题2处理器:2
团队0主题3处理器:3
团队1主题0处理器: 4
团队1主题1处理器:5
团队1主题2处理器:6
团队1主题3处理器:7
团队2主题0处理器:8
团队2线程1处理器:9
团队2线程2处理器:10
团队2线程3处理器:11
团队3主题0处理器:12
团队3主题1处理器:13
Team 3 Thread 2 Processor:14
Team 3 Thread 3 Processor:15
我可以使用GOMP_CPU_AFFINITY变量处理GCC中的处理器亲和度。
我正在使用:
#pragma omp parallel num_threads(4)
目前我在GOMP_CPU_AFFINITY中有此订单:
0 4 8 12 1 2 3 5 6 7 9 10 11 13 14 15
第一个分叉,父亲分叉,获得:
团队0主题0处理器:0
0处理器:4
小组2主题0处理器:8
小组3主题0处理程序:12
我遇到的问题是第二组叉子没有任何顺序,例如我可以有这种情况(我使用#pragma omp原子,所以只有一个父亲可以要求更多处理器:
团队0主题0处理器:0
团队0主题1处理器:5
Team 0 Thread 2处理器:6
Team 0 Thread 3处理器:7
Team 1主题0处理器:4
Team 1主题1处理器:13
团队1主题2处理器:14
小组1主题3处理器:15
小组2主题0处理器:8
小组2主题1处理器:1
小组2主题2处理器:2
团队2线程3处理器:3
团队3主题0处理器:12
团队3主题1处理器:9
团队3主题2处理器:10
团队3主题3处理器:11
问题是:有没有办法使第二次请愿顺序?
我想我必须使用锁或某事来做一些同步方法。
!
- Javier
最后我可以做这个工作,这是我的代码:
#include< omp.h> ;
#include< stdio.h>
#include< stdlib.h>
#include< sched.h>
int main(int argc,char * argv []){
int padre,hijo;
int contador = 0;
omp_set_nested(1);
int suma;
#pragma omp parallel private(padre)shared(contador)num_threads(4)
{
padre = omp_get_thread_num();
{
while(contador!= padre){
//不知道这里放什么
};
#pragma omp parallel private(hijo)shared(padre,contador)num_threads(4)
{
hijo = omp_get_thread_num();
printf(\\\
Father:%d Son:%d Processor:%d\\\
,padre,hijo,sched_getcpu());
#pragma omp master
{
contador ++;
}
}
}
}
}
$ b b
注意:Padre是父亲,Hijo是儿子,Contador是西班牙语的计数器:P
我现在面临的问题是,如果我编译我的代码与-O3优化,while循环'dissapear',除非我把,例如,printf行在循环内。
li> Javier
I need to make 4 teams with 4 threads each one with contiguous processors.
The result I'm expecting is, for example:
Team 0 Thread 0 Processor: 0
Team 0 Thread 1 Processor: 1
Team 0 Thread 2 Processor: 2
Team 0 Thread 3 Processor: 3
Team 1 Thread 0 Processor: 4
Team 1 Thread 1 Processor: 5
Team 1 Thread 2 Processor: 6
Team 1 Thread 3 Processor: 7
Team 2 Thread 0 Processor: 8
Team 2 Thread 1 Processor: 9
Team 2 Thread 2 Processor: 10
Team 2 Thread 3 Processor: 11
Team 3 Thread 0 Processor: 12
Team 3 Thread 1 Processor: 13
Team 3 Thread 2 Processor: 14
Team 3 Thread 3 Processor: 15
I can handle Processor Affinity in GCC using the GOMP_CPU_AFFINITY variable.
I'm using:
#pragma omp parallel num_threads(4)
twice in order to get 2 fork levels.
At the moment I'm having this order in GOMP_CPU_AFFINITY:
0 4 8 12 1 2 3 5 6 7 9 10 11 13 14 15
So the first fork, the "fathers fork", gets:
Team 0 Thread 0 Processor: 0
Team 1 Thread 0 Processor: 4
Team 2 Thread 0 Processor: 8
Team 3 Thread 0 Processor: 12
The problem I'm having is that the second group of forks make without any order so, for example I could have this situation (I'm using #pragma omp atomic so only one 'father' can ask for more processors at any time):
Team 0 Thread 0 Processor: 0
Team 0 Thread 1 Processor: 5
Team 0 Thread 2 Processor: 6
Team 0 Thread 3 Processor: 7
Team 1 Thread 0 Processor: 4
Team 1 Thread 1 Processor: 13
Team 1 Thread 2 Processor: 14
Team 1 Thread 3 Processor: 15
Team 2 Thread 0 Processor: 8
Team 2 Thread 1 Processor: 1
Team 2 Thread 2 Processor: 2
Team 2 Thread 3 Processor: 3
Team 3 Thread 0 Processor: 12
Team 3 Thread 1 Processor: 9
Team 3 Thread 2 Processor: 10
Team 3 Thread 3 Processor: 11
The question is: Is there any way to make this second petition in order?
I think I would have to make some sinchronization method with locks or something...
Thanks in advance!
- Javier
Finally I could make this works, this is my code:
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
int main(int argc, char *argv[]){
int padre, hijo;
int contador = 0;
omp_set_nested(1);
int suma;
#pragma omp parallel private(padre) shared(contador) num_threads(4)
{
padre = omp_get_thread_num();
{
while(contador != padre){
// Don't know what to put here
};
#pragma omp parallel private(hijo) shared(padre, contador) num_threads(4)
{
hijo = omp_get_thread_num();
printf("\nFather: %d Son: %d Processor: %d\n", padre, hijo, sched_getcpu());
#pragma omp master
{
contador++;
}
}
}
}
}
Note: Padre is Father, Hijo is Son and Contador is Counter in Spanish :P
The problem I'm facing now is that if I compile my code with -O3 optimizations, the while loop 'dissapear' unless I put, for example, a printf line inside the loop. I think I should ask it in another question!
Thanks to you all!
- Javier
这篇关于在C中使用GCC和OMP的特定线程顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!