【问题标题】:Intel c++ compiler: how to compiler and link openmp pragma in macro definition/define?英特尔 c++ 编译器:如何在宏定义/定义中编译和链接 openmp pragma?
【发布时间】:2021-02-07 21:56:22
【问题描述】:

我有一个在宏定义中使用 pragma 的简单代码。代码编译运行良好,使用 QtMinGW 就好了。

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

#define NUMEL 1024

#define OMP_PARALLEL _Pragma("omp parallel")
#define OMP_FOR _Pragma("omp for")

#define main_func(par)                                      \
int main(){                                                 \
    int a[NUMEL];                                           \
    OMP_PARALLEL                                            \
    {                                                       \
        int i;                                              \
        OMP_FOR                                             \
        for (i=0; i<NUMEL; i++){                            \
            printf("THRD : %d \n", omp_get_thread_num());   \
            a[i] = tan(i*2);                                \
        }                                                   \
    }                                                       \
    return 0;                                               \
}

main_func(par)

但是,如果我使用 Intel C++ Compiler (ICC)(windows 上的 icl.exe),它会输出以下错误:

 error: expected a ";"
  main_func(par)

通过尝试和错误,我注意到 if add ;在下面的 of 行,错误变为链接错误:

#define OMP_PARALLEL _Pragma("omp parallel");
#define OMP_FOR _Pragma("omp for");

链接错误

test_omp.obj : error LNK2019: unresolved external symbol _Pragma referenced in function main
test_omp.exe : fatal error LNK1120: 1 unresolved externals
make: *** [Makefile:16: test_omp] Error 1120

任何评论都非常感谢。

【问题讨论】:

    标签: c++ openmp intel icc


    【解决方案1】:

    在 icc 论坛上的一些 Q/A 试错之后,结果是

    1. _Pragma 应替换为 __pragma
    2. 不需要引号

    因此在宏定义中使用 omp pragma 的 intel/icc 语法应更正如下:

    #define OMP_PARALLEL __pragma(omp parallel)
    #define OMP_FOR __pragma(omp for)
    

    我希望它可以帮助其他人......我留下了测试代码和makefile,所以你也试试自己。我花了 3 天时间才解决这个问题。

    如果您觉得我的 Q/A 有帮助,请随时投票。

    #include <omp.h>
    #include <stdio.h>
    
    #define NUMEL 1024
    
    #define OMP_PARALLEL __pragma(omp parallel)
    #define OMP_FOR __pragma(omp for)
    
    #define main_func(par)                                      \
    int main(){                                                 \
        int a[NUMEL];                                           \
        OMP_PARALLEL                                            \
        {                                                       \
            int i;                                              \
            OMP_FOR                                             \
            for (i=0; i<NUMEL; i++){                            \
                printf("THRD : %d \n", omp_get_thread_num());   \
                a[i] = tan(i*2);                                \
            }                                                   \
        }                                                       \
        return 0;                                               \
    }
    
    main_func(par)
    
    
    int main2(){
        int a[NUMEL];
        #pragma omp parallel
        {
            int i;
            #pragma omp for
            for (i=0; i<NUMEL; i++){
                printf("THRD : %d \n", omp_get_thread_num());
                a[i] = tan(i*2);
            }
        }
        return 0;
    }
    
    
    PROJECT_ROOT = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
    CXX=icl
    CC=icl
    LD =xilink
    
    LIBDIR=Debug/lib/
    OBJDIR=Debug/obj/
    
    CFLAGS=/Qopenmp
    LDFLAGS= /nodefaultlib:vcomp libiomp5md.lib
    OBJS = $(OBJDIR)test_omp.obj 
    
    all: test_omp
    
    test_omp: $(OBJS)  
        $(LD) $(LDFLAGS) $(OBJS) -Fe:test_omp.exe
    
    
    $(OBJDIR)%.obj: $(PROJECT_ROOT)%.cpp
        $(CXX) -c $(CFLAGS) $(CXXFLAGS) $(CPPFLAGS) -Fo:$@ $<
    
    $(OBJDIR)%.obj: $(PROJECT_ROOT)%.c
        $(CC) $(CFLAGS) $(CPPFLAGS) -Fo:$@ -c $<
    
    
    clean:
        rm -fr test_icl $(OBJS) test_omp.exe
    
    

    【讨论】:

    • 如果您要在您正在构建的同一台机器上运行,您可能需要-march=native,以启用它具有的 ISA 扩展。而不仅仅是基线 SSE2。我认为 ICL 默认启用优化(与其他编译器不同),因此省略 -O3 可能很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多