如何确定bash脚本中调用进程的等级?

问题描述 投票:0回答:1

我正在开发一个 bash 脚本,使用 MPI 压缩大量文件,为超级计算机上的每个处理器分配文件集。

这是我的 bash 脚本:

#!/bin/bash

# Source directory containing subfolders
SOURCE_DIR="../data"

# Destination directory to store compressed files
DEST_DIR="../zipFiles"

# Create the destination directory if it doesn't exist
mkdir -p "$DEST_DIR"

# Count the number of subfolders
SUBFOLDER_COUNT=$(find "$SOURCE_DIR" -maxdepth 1 -type d -not -path "$SOURCE_DIR" | wc -l)

# Print the number of subdirectories:
echo "The number of subdirectories is $SUBFOLDER_COUNT"

# Set the number of subfolders to compress in each batch
echo "The maximum number of subdirectories per batch is $DirPerBatch"

# COMPRESSED FILE NUMBER
k=1

cd "$SOURCE_DIR"

rank=$OMPI_COMM_WORLD_RANK

# Calculate the start and end index of files for this rank
start=$((rank * DirPerBatch))
end=$((start + DirPerBatch - 1))
echo "DBG:      rank:$rank"
echo "DBG:      sta: $start"
echo "DBG:      end: $end"
# Initialize the 'files' variable
files=""

# Loop to gather files for this rank
for ((i = start; i <= end && i < SUBFOLDER_COUNT; i++)); do
    subfolder_index=$i
    subfolder_name=$(printf "%05d" "$subfolder_index")

    # Check if the subfolder exists before adding it
    if [ -d "$subfolder_name" ]; then
        files+="$subfolder_name "  # Use += to append to 'files'
    fi
done

# Create the compressed file for this rank
tar czf $DEST_DIR/$((rank + 1)).tar.gz $files
if [ $? -eq 0 ]; then
    echo "Rank $rank: Successfully compressed $rank.tar.gz"
else
    echo "Rank $rank: Failed to compress $rank.tar.gz"
fi

注 1:变量

DirPerBatch
在提交给超级计算机的作业 bash 脚本中声明并导出。

注 2:作业脚本加载

intel/2019 module
,它使用以下命令来执行 mpi 程序:
mpiexec.hydra

问题:这一行

rank=$OMPI_COMM_WORLD_RANK
无法正确获取进程的排名,事实上,变量
OMPI_COMM_WORLD_RANK
没有返回任何内容来存储在变量rank中。 我查看了intel网站,但找不到任何存储排名值的变量。 我需要获取排名值,以便能够为每个排名分配正确的文件进行压缩。

mpi slurm
1个回答
0
投票

查尔斯·达菲和吉尔斯·古亚尔代特先生

我感谢您富有洞察力的评论,这帮助我了解更多信息并扩展我对问题解决方案的了解。

我在这个网站上找到了一个解决方案,其中包括创建一个Python脚本来获取并打印出正在使用的MPI等级,Python脚本的输出是bash脚本中等级变量的输入。

© www.soinside.com 2019 - 2024. All rights reserved.