Specific thread order in C using GCC and OMP

蹲街弑〆低调 提交于 2019-12-24 09:27:19

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/14167265/specific-thread-order-in-c-using-gcc-and-omp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!