#!/bin/bash

filename="128.txt"  # 请替换成你的文件路径
start_power=3       # 从 2^3 = 8 行开始
max_power=14        # 最大尝试到 2^14 = 16384 行
output_log="output.txt"

if [ "$(tail -c 1 "$filename" | wc -l)" -eq 0 ]; then
  echo "" >> "$filename"
fi
# 获取原始文件总行数
lines=$(wc -l < "$filename")

# 初始化 np_total
np_total=0

rm -rf $output_log

# 封装生成mpirun命令的函数
generate_command() {
    local np_total=$1
    local filepath=$2
    command1="LD_LIBRARY_PATH=/tmp/jinglong/lib:/tmp/jinglong/openmpi/lib:/tmp/jinglong/ucx/lib \
mpirun --prefix /tmp/jinglong/openmpi --allow-run-as-root \
-np $np_total --hostfile $filepath \
-x NCCL_P2P=PCI -mca plm_rsh_no_tree_spawn 1 \
-mca btl_tcp_if_include bond0 -mca pml ob1 -mca btl ^openib \
-x MPI_HOME -x LD_LIBRARY_PATH -x PATH -x NCCL_DEBUG=INFO \
-x NCCL_IB_GID_INDEX=3 -x NCCL_SOCKET_IFNAME=bond0 -x NCCL_IB_HCA=mlx5_10 \
-x NCCL_IB_QPS_PER_CONNECTION=2 -x NCCL_PXN_DISABLE=0 \
-x NCCL_MIN_NCHANNELS=24 -x NCCL_DEBUG_FILE=/tmp/debug.log \
-x NCCL_ALGO=Ring /tmp/jinglong/nccl-test/all_reduce_perf \
-b 16G -e 16G -g 1 -f 2 -T 120"
    echo "$command1"
#    eval "$command1" >>$output_log 2>&1 | tee -a $output_log
    eval "$command1" 2>&1 | tee -a "$output_log"


}

for (( i=start_power; i<=max_power; i++ )); do
    value=$((2**i))

    if (( lines > value )); then
        # 截取前 value 行
        head -n "$value" "$filename" > "$filename.${i}.tmp"

        # 计算 slots 总和
        np_total=$(awk -F'slots=' '{sum += $2} END {print sum}' "$filename.${i}.tmp")

        # 调用函数生成命令并输出
        generate_command "$np_total" "$filename.${i}.tmp"

        # 删除临时生成的文件
        rm -f "$filename.${i}.tmp"
    else
        # 如果文件行数不足，处理剩余行
        head -n "$lines" "$filename" > "$filename.tmp"

        # 计算剩余 slots 总和
        np_total=$(awk -F'slots=' '{sum += $2} END {print sum}' "$filename.tmp")

        # 调用函数生成命令并输出
        generate_command "$np_total" "$filename.tmp"

        # 删除临时生成的文件
        rm -f "$filename.tmp"

        # 输出文件行数不足的提示信息
	echo "===exit"
        break
    fi
done
