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

  • Home
  • SEARCH
  • 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 6928399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:10:57+00:00 2026-05-27T11:10:57+00:00

I use an ugly workaround to compile the same source code once for OpenMP

  • 0

I use an ugly workaround to compile the same source code once for OpenMP and once for single-threaded code. It includes compiler macros and a double #include of a C file. I’m looking for a better solution.

OpenMP and Mac threads don’t mix. This is apparently well-known. Quoting from http://www.imagemagick.org/script/architecture.php#threads .

The OpenMP committee has not defined the behavior of mixing OpenMP
with other threading models such as Posix threads. However, using
modern releases of Linux, OpenMP and Posix threads appear to
interoperate without complaint. If you want to use Posix threads …
from Mac OS X or an older Linux release, you may need to disable
OpenMP support within ImageMagick.

I have a Python library with a C extension which scales very nicely with OpenMP, and it really involved only a scattering of #pragma statements and the right compiler flags.

I also develop on Macs. When I use my library in something other than the main thread, I get a segfault. For example, if I use OpenMP support then I can’t use my library in Django.

I could of course compile without OpenMP support, but then the single-threaded batch code, which does well with OpenMP, wouldn’t work.

I instead want a user-defined function to select at run-time between the OpenMP implementation and the single-threaded implementation.

The only solution I’ve come up with is a horrible hack where I #include the same C code twice:

#ifdef _OPENMP

#define MYFUNC static int _myfunc_openmp
#include "algorithm.c"
#undef MYFUNC
#define MYFUNC static int _myfunc_single
#include "algorithm.c"

int myfunc(int arg) {
  if (omp_get_num_threads() <= 1) {
      return _myfunc_single(arg);
  } else {
      return _myfunc_openmp(arg);
  }
}

#else

#define MYFUNC int myfunc
#include "algorithm.c"

#endif

and where “algorithm.c” looks like

MYFUNC(int arg) {
#ifdef _OPENMP
  #pragma omp for ...
#endif
   ... algorithm here ...
}

This does work, but it feels like I’m taking the wrong approach.

I tried using OpenMP’s “if()” clause, as in

  int in_parallel = (omp_get_num_threads() > 1);
  #pragma omp for ... if(in_parallel)

but that still causes OpenMP on a thread to segfault my program. I presume that it’s allocating space for the lock in the critical section of my code, even though it’s not needed.

Do you know a better way to have the same algorithm used for both the OpenMP and non-OpenMP code paths?

  • 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-27T11:10:58+00:00Added an answer on May 27, 2026 at 11:10 am

    If you have C99 (gcc and clang do for that case) you can use the _Pragma operator. It is useful especially inside macros:

    #ifdef WITH_OPENMP
    # define PARALLEL_FOR _Pragma("omp for")
    #else
    # define PARALLEL_FOR
    #endif
    

    Now you can just prefix the appropriate for loops with that macro, and thus avoid all other the ugly #if/#else in the rest of the code, which make it difficult to maintain.

    The double inclusion per se of the file doesn’t look wrong to me. Having a special case that is compiled statically without OpenMp avoids any run time decision that OpenMp could try to take.

    What I wouldn’t do, is passing storage specification, return type and name in one macro. In any case having the two functions visible by the linker costs you nothing, so the static is superfluous. The only thing that you should watch is that linker symbols starting with an underscore are reserved, better rename them to something decent.

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

Sidebar

Related Questions

I use the dark-blue2 color theme but it seems ugly under console. So I
Here's a ugly bit of Javascript it would be nice to find a workaround.
ReturnUrl is kind of ugly. I'd like to use redirect instead. How can I
A customers ERP solution has (imho) a really ugly database structure. It doesn't use
'''use Jython''' import shutil print dir(shutil) There is no, shutil.move, how does one move
Use case: A does something on his box and gots stuck. He asks B
Use case: 3rd party application wants to programatically monitor a text file being generated
Use case: user clicks the link on a webpage - boom! load of files
use LWP::Simple; use Parallel::ForkManager; @links=( [http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-windows.exe,SweetHome3D-2.1-windows.exe], [http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-macosx.dmg,SweetHome3D-2.1-macosx.dmg], [http://prdownloads.sourceforge.net/sweethome3d/SweetHome3DViewer-2.1.zip,SweetHome3DViewer-2.1.zip], ); # Max 30 processes for
use this website a lot but first time posting. My program creates a number

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.