多个显卡同时跑多个实验 - 脚本模板

# 所有可用显卡
export CUDA_VISIBLE_DEVICES=$1
# 转成了数组形式
all_cuda_devices=($(echo $CUDA_VISIBLE_DEVICES | sed 's/,/ /g'))

# 你要跑的任务(我这里以GLUE的几个任务为例)
if (($# > 1));then tasks=$2;else tasks='qnli qqp mnli sst2' ;fi

task_nums=$(echo $tasks | awk -F ' ' '{print NF}')

if ((${#all_cuda_devices[*]} < $task_nums));then
    echo 显卡比任务少
    exit 1
fi


now_use=0
for task_name in  $tasks
do
	export CUDA_VISIBLE_DEVICES=${all_cuda_devices[${now_use}]}
	now_use=$((${now_use}+1))

	python -u train.py $task_name ..... 2>&1 > $task_name.log &
	echo start $task_name on GPU: $CUDA_VISIBLE_DEVICES
done

执行:

bash run.sh 0,1,2,3 ....
posted @ 2022-12-01 21:38  王冰冰  阅读(78)  评论(0编辑  收藏  举报