Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6961441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:31:50+00:00 2026-05-27T15:31:50+00:00

I want to write a function which employs openMP parallelism but should work whether

  • 0

I want to write a function which employs openMP parallelism but should work whether called from within a parallel region or not. So I used the if clause to suppress parallelism, but this doesn’t work as I thought:

#include <omp.h>
#include <stdio.h>

int m=0,s=0;

void func()
{
  bool p = omp_in_parallel();
  // if clause to suppress nested parallelism
#pragma omp parallel if(!p)
  {
    /* do some massive work in parallel */
#pragma omp master
    ++m;
#pragma omp single
    ++s;
  }
}

int main()
{
  fprintf(stderr,"running func() serial:\n");
  m=s=0;
  func();
  fprintf(stderr," m=%d s=%d\n",m,s);

  fprintf(stderr,"running func() parallel:\n");
  m=s=0;
#pragma omp parallel
  func();
  fprintf(stderr," m=%d s=%d\n",m,s);
}

which creates the output

running func() serial:
 m=1 s=1
running func() parallel:
 m=16 s=16

Thus the first call to func() worked fine: m and s obtain the values 1 as they should, but the second call to func() from within a parallel region did create nested parallelism (with 16 teams of 1 thread each) even though this was suppressed. That is the omp master and omp single directives bind to the preceeding omp parallel if(!p) directive rather than to the outside parallel region.

Of course, one can fix this problem by the following code

void work()
{
  /* do some massive work in parallel */
#pragma omp master
  ++m;
#pragma omp single
  ++s;
}

void func()
{
  if(omp_in_parallel())
    work();
  else
#pragma omp parallel
    work();
}

but this requires an additional function to be defined etc. Is it possible to do this within a single function (and without repeating code)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:31:50+00:00Added an answer on May 27, 2026 at 3:31 pm

    The OpenMP constructs will always bind to the innermost containing construct, even if it isn’t active. So I don’t think it’s possible while retaining the #pragma omp parallel for both code paths (At least with the provided informations about the problem).

    Note that it is a good think that it behaves like this, because otherwise the use of conditionals would easily lead to very problematic (read buggy) code. Look at the following example:

    void func(void* data, int size)
    {
       #pragma omp parallel if(size > 1024)
       {
           //do some work
           #pragma omp barrier
           //do some more work
       }
    }
    
    ...
    #pragma omp parallel
    {
       //create foo, bar, bar varies massively between different threads (so sometimes bigger, sometimes smaller then 1024
       func(foo, bar);
       //do more work
    }
    

    In general a programmer should not need to know the implementation details of the called functions, only their behaviour. So I really shouldn’t have to care about whether func creates a nested parallel section or not and under which exact conditions it creates one. However if the barrier would bind to the outer parallel if the inner is inactive this code would be buggy, since some threads of the outer parallel sections encounter the barrier and some don’t. Therefore such details stay hidden inside the innermost containing parallel, even if it isn’t active.

    Personally I have never encountered a situation where I wanted it to behave differently (which would go against information hiding and such), so maybe you should tell us a bit more about what you are trying to accomplish to get better answers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a function which removes elements from an array of integers
I want to write the following function which should be used in an Excel
I want to write a function in haskell which determines whether a boolean function
I want to write a function which can validate a given value (passed as
I want to write a function that accepts a parameter which can be either
i want to write a function that prints multi-dimensional objects which are text (or
I want to write a function like equalp, which gives #t for (equalp Xy
I want to write a function which randomizes the order of a sequence of
I want to write a simple function which splits a ByteString into [ByteString] using
I want to write a function which takes a list of elements l, a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.