Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda