Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Ubuntu 配置與使用 MPI (mpich2) 環境快速入門

編輯:關於 Linux

MPI (Message Passing Interface) 是平行運算通訊標準。Ubuntu 下透過 mpich2 套件可快速建立 MPI 環境,支援 C/C++/Fortran 程式編譯與多行程執行。本文記錄安裝、編譯、執行三步驟完整流程。

1. 環境搭建

sudo apt-get install mpich2
# 安裝版本:mpich2 1.4.1(Ubuntu 12.04 預設)
# 亦可安裝 openmpi:sudo apt-get install openmpi-bin libopenmpi-dev

2. 編譯 MPI 程式

# 範例程式 mpi_hello.c
#include 
#include 

int main(int argc, char **argv) {
    int rank, size;
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    printf("Hello from rank %d of %d\n", rank, size);
    MPI_Finalize();
    return 0;
}

# 編譯
mpicc mpi_hello.c -g -Wall -o mpi_hello

3. 執行 MPI 程式

# 使用 4 個行程執行
mpiexec -n 4 ./mpi_hello
# 或 mpirun -np 4 ./mpi_hello

# 輸出範例:
# Hello from rank 0 of 4
# Hello from rank 1 of 4
# Hello from rank 2 of 4
# Hello from rank 3 of 4

跨節點執行(叢集環境)

  1. 設定 SSH 免密碼登入所有節點。
  2. 建立 ~/machinefile 列出節點主機名/IP:
node1 slots=4
node2 slots=4
  1. 執行:
mpiexec -f ~/machinefile -n 8 ./mpi_hello

MPI 編譯執行截圖

常見問題

  • mpicc 找不到:確認 mpich2 已安裝,路徑通常在 /usr/bin/mpicc。
  • 無法連線節點:檢查 SSH 金鑰、防火牆、/etc/hosts 主機名解析。
  • 效能調校:使用 MPICH_NEMESIS_NETMOD=tcp、調整 MP_BUFFER_MEM 等環境變數。
Copyright © Windows教程網 All Rights Reserved