Ignore OpenMP on machine that does not have it

后端 未结 5 1371
暖寄归人
暖寄归人 2021-02-03 20:21

I have a C++ program using OpenMP, which will run on several machines that may have or not have OpenMP installed.

How could I make my program know if a machine has no O

5条回答
  •  天命终不由人
    2021-02-03 21:08

    Compilers are supposed to ignore #pragma directives they don't understand; that's the whole point of the syntax. And the functions defined in openmp.h have simple well-defined meanings on a non-parallel system -- in particular, the header file will check for whether the compiler defines ENABLE_OPENMP and, if it's not enabled, provide the right fallbacks.

    So, all you need is a copy of openmp.h to link to. Here's one: http://cms.mcc.uiuc.edu/qmcdev/docs/html/OpenMP_8h-source.html .

    The relevant portion of the code, though, is just this:

    #if defined(ENABLE_OPENMP)
    #include 
    #else
    typedef int omp_int_t;
    inline omp_int_t omp_get_thread_num() { return 0;}
    inline omp_int_t omp_get_max_threads() { return 1;}
    #endif
    

    At worst, you can just take those three lines and put them in a dummy openmp.h file, and use that. The rest will just work.

提交回复
热议问题