1

Several posts explain that it is best to assign names to OpenMP critical sections so that their synchronizations do not conflict. For example, see "critical" an entire function or openMP, atomic vs critical?.

Is it possible to define a macro to create an OpenMP critical section such that it forms a unique name each time the macro is instanced? (Possibly this could use __FILE__ and __LINE__? However, __FILE__ is a string?)

Something like:

#if defined(_MSC_VER)
#define PRAGMA(...) __pragma(__VA_ARGS__)
#else
#define PRAGMA(...) _Pragma(#__VA_ARGS__) // C++11
#endif

#define BEGIN_LOCK PRAGMA(omp critical (some_incantation_for_a_unique_name))

BEGIN_LOCK { some_code(); }

And, why doesn't OpenMP make this the default behavior? Is there some drawback to this?

Community
  • 1
  • 1
Hugues
  • 2,865
  • 1
  • 27
  • 39

1 Answers1

0

If all that you need is that it will be unique in one file, you can use something like the following:

#define BEGIN_CRITICAL BEGIN_CRITICAL_1( CRITICAL_, __LINE__)
#define BEGIN_CRITICAL_1(F,L) BEGIN_CRITICAL_2(F,L)
#define BEGIN_CRITICAL_2(F,L)  omp critical(F##L)

the first define, adds the 2 parameters (the constant name, and the line number to male it unique. the second define, expands them. The 3rd one, concatenates them.

Making it unique in a project, is more difficult, because you need to "un-stringify" __FILE__ (not sure if this is even possible in pre-processor)...

another option is to have support from your building system: Add to the compilation line -D with the file mane(or any other unique file ID). For example, if you are compiling foo.cpp, add -DMY_FILE=foo.cpp, and replace the CRITICAL in the first macro, with MY_FILE

yosim
  • 503
  • 2
  • 8
  • Thanks. I was thinking along those lines. However, this still requires specifying a parameter to the macro (the first parameter `CRITICAL`) that varies per file. – Hugues Dec 18 '13 at 18:28
  • Or, your build system can do this for you automatically. see update in my answer. – yosim Dec 18 '13 at 20:15