# OpenMPI
:::success
1. 載入 編譯器環境
```
[user@ilgn01 ~]$ ml load gcc/8.5.0 openmpi/4.1.6
[user@ilgn01 ~]$ ml list
Currently Loaded Modules:
1) gcc/8.5.0 2) openmpi/4.1.6
```
<font color="blue">備註:Module 採用階層式的設計,要先 load 一個 module 後,才能看到下一層相依的 module,避免搭配錯相依套件。**順序要先load gcc 再 load openmpi。**</font>
2. 確認載入的編譯器
```
[user@ilgn01 ~]$ whereis mpicc
mpicc: /pkg/mpi/gcc-8.5.0/openmpi/4.1.6/bin/mpicc
```
3. 撰寫MPI程式碼 (此以hello.c 為例)
```
[user@ilgn01 ~]$ vi hello.c
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
MPI_Init(NULL, NULL);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
printf("Hello world from processor %s, rank %d out of %d processors\n",
processor_name, world_rank, world_size);
MPI_Finalize();
}
```
4. 使用mpicc編譯hello.c
```
[user@ilgn01 ~]$ mpicc -o hello_openmpi ./hello.c
```
5. 撰寫Slurm Job (以job.sh為例)
```
[user@ilgn01 ~]$ vi job.sh
#!/bin/bash
#SBATCH --account=GOVXXXXX # Project Name
#SBATCH -J test # Job name
#SBATCH -N 2 # Total number of nodes requested
#SBATCH -n 150 # Total number of mpi tasks requested
#SBATCH -t 00:30:00 # Run time (hh:mm:ss) - 1.5 hours
#SBATCH --partition=XXXX # Partition Name
#SBATCH -o job.%j.out # (-o) Path to the standard output file
#SBATCH -e job.%j.err # (-e) Path to the standard error file
#SBATCH --mail-type=END,FAIL # Mail events (NONE, BEGIN, END, FAIL, ALL)
#SBATCH --mail-user=user@example.com # Where to send mail. Set this to your email address
module purge
module load gcc/8.5.0 openmpi/4.1.6
mpirun ./hello_openmpi
```
6. 提交Slurm Job
```
[user@ilgn01 ~]$ sbatch job.sh
Submitted batch job 56448
```
7. 查看執行輸出的結果
```
[user@ilgn01 ~]$ cat job.56448.out
Hello world from processor icpnp335, rank 31 out of 150 processors
Hello world from processor icpnp335, rank 34 out of 150 processors
Hello world from processor icpnp335, rank 39 out of 150 processors
Hello world from processor icpnp335, rank 42 out of 150 processors
Hello world from processor icpnp335, rank 43 out of 150 processors
Hello world from processor icpnp335, rank 50 out of 150 processors
Hello world from processor icpnp335, rank 51 out of 150 processors
Hello world from processor icpnp335, rank 59 out of 150 processors
Hello world from processor icpnp336, rank 132 out of 150 processors
Hello world from processor icpnp336, rank 136 out of 150 processors
Hello world from processor icpnp336, rank 137 out of 150 processors
Hello world from processor icpnp336, rank 139 out of 150 processors
Hello world from processor icpnp336, rank 140 out of 150 processors
Hello world from processor icpnp336, rank 142 out of 150 processors
Hello world from processor icpnp336, rank 143 out of 150 processors
...
```
**<font color="blue">備註:如何撰寫腳本請移至[提交與管理Job範例](https://man.twcc.ai/@f1-manual/slurm_job_example)觀看內容</font>**
<!---
:::
:::info
備註
再執行以上範例之後,若會收到錯誤信息像是
`cpnp330:rank12.openmpi_hello: unknown link speed 0x80`
表明在運行 Open MPI 程序時遇到了未知的鏈接速度問題。
這可能與 UCX 或網絡設備的配置有關。
以下是一些可能的解決方案和排除故障的步驟:
### 可能的原因和解決方案
1. **UCX 配置問題**:
- 確保 UCX 已正確安裝並配置。
- 檢查 UCX 環境變量是否設置正確,特別是 `UCX_NET_DEVICES` 和 `UCX_TLS`。
2. **網絡設備配置問題**:
- 確保網絡設備(例如 Mellanox 網卡)已正確配置和驅動程序已安裝。
- 使用 `ibv_devinfo` 檢查 InfiniBand 設備狀態,確保設備正常運行。
3. **Open MPI 配置問題**:
- 檢查 Open MPI 的版本和配置,確保與 UCX 兼容。
- 確認 Open MPI 的環境變量設置正確,如 `OMPI_MCA_pml` 和 `OMPI_MCA_btl`。
**撰寫新的Slurm Job **
```
[user@ilgn01 ~]$vi job.sh
#!/bin/bash
#SBATCH --account=GOVXXXXX # Project Name
#SBATCH -J test # Job name
#SBATCH -o job.%j.out # output file (%j expands to jobId)
#SBATCH -N 2 # Total number of nodes requested
#SBATCH -n 150 # Total number of mpi tasks requested
#SBATCH -t 00:30:00 # Run time (hh:mm:ss) - 1.5 hours
#SBATCH --partition=XXXX # Partition Name
export UCX_TLS=ud,dc,rc,self
export OMPI_MCA_btl=tcp,self
export OMPI_MCA_pml=ucx
export UCX_NET_DEVICES=mlx5_0:1
module purge
module load gcc/8.5.0 openmpi/4.1.6
mpiexec ./hello_openmpi
```
重複上述步驟5-7,job.xxx.out檔則不會出現"xxx:unknown link speed 0x80"的訊息。
<!---### 排除故障步驟
#### 1\. 檢查 UCX 安裝和配置
確保 UCX 正確安裝並配置:
sh
複製程式碼
`# 檢查 UCX 版本
ucx_info -v`
確保 UCX 環境變量設置正確:
sh
複製程式碼
`export UCX_NET_DEVICES=mlx5_0:1
export UCX_TLS=ud,dc,rc,self`
#### 2\. 檢查網絡設備狀態
使用 `ibv_devinfo` 檢查 InfiniBand 設備狀態:
sh
複製程式碼
`ibv_devinfo`
確保設備狀態正常,如果顯示異常信息,可能需要重新配置網絡設備或更新驅動程序。
#### 3\. 檢查 Open MPI 配置
確保 Open MPI 配置正確:
sh
複製程式碼
`# 檢查 Open MPI 版本
mpirun --version
# 確保環境變量設置正確
export OMPI_MCA_pml=ucx
export OMPI_MCA_btl=tcp,self`
#### 4\. 測試基本 UCX 應用
運行簡單的 UCX 測試應用,確保 UCX 正常工作:
sh
複製程式碼
`ucx_perftest -t tag_lat -d mlx5_0`
#### 5\. 檢查日誌文件
檢查 UCX 和 Open MPI 日誌文件,查找更多錯誤信息或提示。
### 總結
這些步驟應該有助於診斷和解決 `unknown link speed 0x80` 錯誤。關鍵在於確保 UCX 和網絡設備的配置正確,並且 Open MPI 能夠正確使用這些配置。如果問題依然存在,考慮升級 UCX 和 Open MPI 到最新版本,或者查閱相應的社區支持資源以尋求幫助。
--->
:::