如何在 makefile 中使用 OpenMP?

How to use OpenMP in a makefile?

我正在使用 Makefile 来设置 mo 代码所需的环境。我刚刚学习并行化,非常感谢您的帮助。

# The list of packages used by the macro:
USED_PKGS = xAODRootAccess xAODTruth xAODJet xAODMissingET
test: test.o
    `root-config --ld` -o $@ `root-config --libs` \
        -L$(ROOTCOREDIR)/lib `rc get_ldflags $(USED_PKGS)` $^
clean:
    rm -f test.o
    rm -f test
.SUFFIXES: .C .o
.C.o:
    `root-config --cxx` -c -o $@ `root-config --cflags` \
        -I$(ROOTCOREDIR)/include `rc get_cxxflags $(USED_PKGS)` $<

我已经安装了 OpenMPI 并将其添加到 PATH 和 LD_LIBRARY_PATH。

我的代码非常简单,只是想根据 Makefile 检查它:

int main()
{
    int i;
#pragma omp parallel for
    for ( i = 0; i < 1e8; i++ )
    {
        int y = 2*i;
    }
}

好的,所以我在玩弄之后自己弄明白了。如果其他人正在寻找相同的东西,Makefile 应该看起来像:

# The list of packages used by the macro:
USED_PKGS = xAODRootAccess xAODTruth xAODJet xAODMissingET
test: test.o
    `root-config --ld` -o $@ `root-config --libs` \
        -L$(ROOTCOREDIR)/lib `rc get_ldflags $(USED_PKGS)` $^ -fopenmp
clean:
    rm -f test.o
    rm -f test
.SUFFIXES: .C .o
.C.o:
    `root-config --cxx` -c -o $@ `root-config --cflags` \
        -I$(ROOTCOREDIR)/include `rc get_cxxflags $(USED_PKGS)` $< -fopenmp

-fopenmp 应添加到两行。