Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
Can I conditionally define an openMP parallel region?

I can write some code that looks like this:

if (make_parallel)
{
    #pragma omp parallel for
    for (int i=0; i<n; i++)
    {
        //For loop stuff
    }
}
else
{
    //Identical for loop, minus the parallelisation
    for (int i=0; i<n; i++)
    {
        //For loop stuff
    }
}

Is there a tidier way of doing this, such that the for loop doesn't have to be duplicated?

[Edit] - So this solution works at the preprocessing level

#define USE_OPENMP

//...

#ifdef USE_OPENMP
#pragma omp parallel for
#endif
for(int i=0;i<n;i++)

But that isn't ideal. But the more I think about it, since defining the parallel region is also preprocessing it probably can't be made conditional in a tidy way?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can simply use a if clause like this:

int enabled = 1;

#pragma omp parallel if(enabled)
#pragma omp for nowait
for(int i=0;i<n;i++)

Note that this condition could be evaluated by the runtime and not the compiler (GCC does that for example). As a result, the overhead can be significantly higher than your if-else solution for very small loops or the ones in which each iteration is very cheap. The reason is that compilers often do not propagate constants to the OpenMP callback computing function and so the code can make unnecessary operations. This solution has the benefit of being cleaner, helping compilers to produce a smaller binaries and also help to reduce the compilation time (since the loop is compiled once).

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda