Back
Featured image of post Openmpi

Openmpi

hpc

Openmpi

初步使用

安装与测试

直接官网下载release包

wget https://download.open-mpi.org/release/open-mpi/v4.1/openmpi-4.1.1.tar.gz

linux下解压:

tar -zxf openmpi-4.1.1.tar.gz

进入开始configure: prefix 为指定安装路径

cd openmpi-4.1.1/

./configure --prefix=/usr/local/openmpi

安装:


make

sudo make install

设置环境变量


sudo vim /etc/profile

加入:


export PATH=/usr/local/openmpi/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/openmpi/lib:$LD_LIBRARY_PATH

生效:


source /etc/profile

测试

mpicc --version

写代码测试:hello.c


#include <stdio.h>
#include "mpi.h"

int main(int argc, char* argv[])
{
    int rank, size, len;
    char version[MPI_MAX_LIBRARY_VERSION_STRING];

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Get_library_version(version, &len);
    printf("Hello, world, I am %d of %d, (%s, %d)\n",
           rank, size, version, len);
    MPI_Finalize();

    return 0;
}

编译并运行,我这里是四核虚拟机


mpicc hello.c -o hello

mpirun -np 4 hello

管理

使用cmake管理

模板:


cmake_minimum_required(VERSION 3.5.1)

SET(SRC_LIST  hello_c.c)

find_package(MPI REQUIRED)

include_directories(${MPI_INCLUDE_PATH})

add_executable(hello_c ${SRC_LIST})
target_link_libraries(hello_c ${MPI_LIBRARIES})

if(MPI_COMPILE_FLAGS)
  set_target_properties(hello_c PROPERTIES
    COMPILE_FLAGS "${MPI_COMPILE_FLAGS}")
endif()

if(MPI_LINK_FLAGS)
  set_target_properties(hello_c PROPERTIES
    LINK_FLAGS "${MPI_LINK_FLAGS}")
endif()

编译与运行:

mkdir build

cd build

cmake ..

make

mpirun -np 4 hello_c

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy