Ignore OpenMP on machine that does not have it

后端 未结 5 1301
暖寄归人
暖寄归人 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:22

    How could I make my program know if a machine has no OpenMP and ignore those #include , OpenMP directives (like #pragma omp parallel ...) and/or library functions (like tid = omp_get_thread_num();) ?

    Here's a late answer, but we just got a bug report due to use of #pragma omp simd on Microsoft compilers.

    According to OpenMP Specification, section 2.2:

    Conditional Compilation

    In implementations that support a preprocessor, the _OPENMP macro name is defined to have the decimal value yyyymm where yyyy and mm are the year and onth designations of the version of the OpenMP API that the implementation supports.

    It appears modern Microsoft compilers only support OpenMP from sometime between 2000 and 2005. I can only say "sometime between" because OpenMP 2.0 was released in 2000, and OpenMP 2.5 was released in 2005. But Microsoft advertises a version from 2002.

    Here are some _OPENMP numbers...

    • Visual Studio 2012 - OpenMP 200203
    • Visual Studio 2017 - OpenMP 200203
    • IBM XLC 13.01 - OpenMP 201107
    • Clang 7.0 - OpenMP 201107
    • GCC 4.8 - OpenMP 201107
    • GCC 8.2 - OpenMP 201511

    So if you want to use, say #pragma omp simd to guard a loop, and #pragma omp simd is available in OpenMP 4.0, then:

    #if _OPENMP >= 201307
        #pragma omp simd
        for (size_t i = 0; i < 16; ++i)
            data[i] += x[i];
    #else
        for (size_t i = 0; i < 16; ++i)
            data[i] += x[i];
    #endif
    

    which will run on several machines that may have or not have OpenMP installed.

    And to be clear, you probably need to build your program on each of those machines. The x86_64 ABI does not guarantee OpenMP is available on x86, x32 or x86_64 machines. And I have not read you can build on one machine, and then run on another machine.

提交回复
热议问题