Región crítica para los hilos del equipo actual

I want a piece of code to be critical for the current team of threads and not global critical. How can i achieve this ??

quick-sort(args)
{
    spawn threads #x
    {
       critical-region
       {
            // code
       }
    }
    quick-sort(args)
    quick-sort(args)
}

Here the open-mp critical-region construct will block all threads before accessing the critical region. But i don't have problem with two threads entering the critical region as long as they are not spawned at the same time. I want a solution for openMP.

preguntado el 01 de febrero de 12 a las 14:02

1 Respuestas

No puedes hacer eso con #pragma omp critical, but you may use OpenMP locks:

quick-sort(args)
{
    declare an instance of OpenMP lock
    omp_init_lock( the lock instance )
    spawn threads #x
    {
       // critical-region
       omp_set_lock( the lock instance )
       {
            // code
       }
       omp_unset_lock( the lock instance )
    }
    omp_destroy_lock( the lock instance )
    quick-sort(args)
    quick-sort(args)
}

Since each invocation of quick-sort will declare its own lock object, it will give you what you want.

However, from your pseudo-code it seems that you will never have two different thread teams running at the same time, unless there are OpenMP parallel regions in other functions. If the only code that has a parallel region ("spawns threads") is in quick-sort, you would need to have a recursive call to that function from inside the parallel region, which you do not.

Respondido 01 Feb 12, 18:02

thanks.. specially for the last line. I think I have figured out the reason for the slow down. - prathmesh.kallurkar

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.