在使用slurm的计算集群上运行helloworld.py程序

3 投票
1 回答
4754 浏览
提问于 2025-04-18 05:11

这是一个比较长的问题,所以我先给个总结:

我刚接触并行编程和网格系统。我想在我有账号的一个网格上运行一个例子,具体内容可以在这个链接找到:http://jeremybejarano.zzl.org/MPIwithPython/introMPI.html。维护者提供了一个C语言的例子,我可以运行这个。但是用Python版本运行时,我得到的所有进程的排名都是零。这可能是什么问题呢?

下面是问题的详细描述:

我有一段用Python 2.7(加上numpy、matplotlib和mayavi)写的代码,运行起来非常耗时。因为我在某个网格上有账号,所以我想把代码移到那里,这样可以减少等待的时间。

不幸的是,我对并行处理、网格等都很陌生。而且我在这个网格上没有管理员权限。

系统提供了一些文档。这个系统使用的是SLURM。你需要准备一个sbatch文件,然后通过sbatch filename来提交任务。这里有一个用C语言写的“你好,世界”程序的例子:

#include <stdio.h>
#include <mpi.h>


int main (argc, argv)
     int argc;
     char *argv[];
{
  int rank, size;

  MPI_Init (&argc, &argv);      /* starts MPI */
  MPI_Comm_rank (MPI_COMM_WORLD, &rank);        /* get current process id */
  MPI_Comm_size (MPI_COMM_WORLD, &size);        /* get number of processes */
  printf( "Hello world from process %d of %d\n", rank, size );
  MPI_Finalize();
  return 0;
}

还有管理员提供的运行它的slurm文件:

#!/bin/bash
#SBATCH -M linux
#SBATCH -p mid1
#SBATCH -A username
#SBATCH -J mid1-test
#SBATCH -N 1 
#SBATCH -n 4 
#SBATCH --time=2-00:00:00 
#SBATCH --workdir=/truba_scratch/username/test
#SBATCH --output=slurm-%j.out
#SBATCH --error=slurm-%j.err
#SBATCH --mail-type=ALL
#SBATCH --mail-user=who@am.i


. /usr/share/Modules/init/sh
module load somehostithink/library/openmpi-1.4.3/gcc
export OMP_NUM_THREADS=1
echo "SLURM_NODELIST $SLURM_NODELIST"

mpirun helloworld

exit

我可以通过sbatch helloworld.slurm来提交。最后我看到从0到3的“你好,世界”。例如,每个进程的排名值不同。很好!

问题是,系统里没有用Python写的例子。系统里的Python版本比较旧:2.6.x。所以我下载了Anaconda发行版,并在用户空间安装了它。我尝试修改上面的helloworld.slurm例子。我想在这里运行“你好,世界”例子:http://jeremybejarano.zzl.org/MPIwithPython/introMPI.html。我可以提交这个任务,但从输出文件来看,我得到的所有进程的排名都是一样的。例如,这似乎并没有在不同的进程上运行。

注意:我在C语言版本中也遇到同样的错误,但它仍然可以运行并产生不同的排名。

Python版本的“你好,世界”代码:

from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
print "hello world from process ", rank

Python“你好,世界”的slurm文件(main.py):

#!/bin/bash
#SBATCH -M linux
#SBATCH -p mid1
#SBATCH -A username
#SBATCH -J mid1-test
#SBATCH -N 1
#SBATCH -n 4
#SBATCH --time=2-00:00:00
#SBATCH --workdir=/scratch/username/test
#SBATCH --output=slurm-%j.out
#SBATCH --error=slurm-%j.err
#SBATCH --mail-type=ALL
#SBATCH --mail-user=who@am.i

. /usr/share/Modules/init/sh
module load somehost/library/openmpi-1.4.3/gcc
export OMP_NUM_THREADS=1
echo "SLURM_NODELIST $SLURM_NODELIST"

mpirun /scratch/username/anaconda/bin/python /scratch/username/test/main.py

exit

产生的错误文件:

slurmd[shomehostithink]: task/cgroup: plugin not compiled with hwloc support, skipping affinity.

产生的输出文件:

SLURM_NODELIST hostidithink
hello world from process  0
hello world from process  0
hello world from process  0
hello world from process  0

那么,问题可能出在哪里呢?我该如何解决这个问题?

我显然已经给管理员发了消息,但他还没有回复。

1 个回答

1

在Linux系统下,Anaconda的mpi4py是和mpich一起打包的(在OS X上则使用OpenMPI)。因为你正在使用OpenMPI的mpirun,所以可能会出现单例效应。你有两个选择:

  1. 从源代码重新编译Anaconda,使用openmpi-1.4.3和gcc。
  2. 试着找到Anaconda的mpirun程序,使用类似/scratch/username/anaconda/bin/mpirun /scratch/username/anaconda/bin/python /scratch/username/test/main.py的命令。

你在使用Slurm时遇到的错误提示是说cgroup Slurm插件没有编译支持hwloc,因此任务亲和性(把进程固定到特定核心上)不被支持。这应该不是导致单例问题的原因。

撰写回答