c++ - 写入文件 PBS MPI 时出错

标签 c++ mpi pbs

我在使用 PBS 的集群上使用 MPI 将一些数据写入文件时遇到了很大的麻烦。这是简单的问题模拟程序的示例。

#include <mpi.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>


#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <unistd.h>

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

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);


// Define hostname
char hostname[128];
gethostname(hostname, 128);

// check and create dump directory
  struct stat buf;
  int rc;
  char *dir="Res";

  rc = stat( dir, &buf );
  if( rc ) // no dir, create
  { if( rank == 0 )
    {
      rc = mkdir( dir, 0771);
      if( rc )
      {std::ostringstream oss;
       oss << "Can't create dump directory \""
          << dir
          << "\"";
      }
    }
    else {
       sleep (2);
    }
  }
  else if( !S_ISDIR( buf.st_mode ) )
  {std::ostringstream oss;
   oss << "Path \""
       << dir
       << "\" is not directory for dump";
  }


   MPI_Barrier(MPI_COMM_WORLD);
// Every process defines name of file for output (res_0, res_1, res_2.....)
std::ostringstream filename;
filename << dir << "/res_"<< rank;

// Open file 
std::ofstream file(filename.str().c_str());

// Output to file . Output seems like "I am 0 from 24. hostname"
file  << "I am " << rank << " from " << size << ".   " << hostname  << std::endl;

file.close();

MPI_Finalize();

return 0;
}

我用 openmpi_intel-1.4.2 编译它,使用 comand

mpicxx -Wall test.cc -o test

然后我用脚本将这个程序排队:

#!/bin/bash

#PBS -N test
#PBS -l select=8:ncpus=6:mpiprocs=6
#PBS -l walltime=00:01:30
#PBS -m n
#PBS -e stderr.txt
#PBS -o stdout.txt

cd $PBS_O_WORKDIR
echo "I run on node: `uname -n`"
echo "My working directory is: $PBS_O_WORKDIR"
echo "Assigned to me nodes are:"
cat $PBS_NODEFILE

mpirun -hostfile $PBS_NODEFILE ./test 

我期望这样的结果:

1. New directory "Res" to be created

2. 8*6 different files (res_0, res_1, res_2, ...) to be written to the Res dir

但只有来自第一个节点的 res_* 文件被写入 (res_{0..5}) 而其余的则没有。

问题是什么?

谢谢!

最佳答案

好的,让我们假设您在一个跨所有计算节点一致安装的文件系统上运行。是这样的吧? 因此,我在您的代码片段中看到的主要问题是所有进程都同时声明目录,然后在目录不存在时尝试创建它。我不确定到底发生了什么,但我敢肯定这不是有史以来最聪明的想法。

既然本质上你想要的是目录的连续完整性检查和/或它的创建(如果需要),为什么不让等级 0 的 MPI 进程来做呢?

这会给你这样的东西:

if ( rank == 0 ) { // Only master manages the directory creation
    int rc = stat( dir, &buf );
    ... // sanity check goes here and directory creation as well
    // calling MPI_Abort() in case of failure seems also a good idea
}
// all other processes wait here
MPI_Barrier( MPI_COMM_WORLD );
// now we know the directory exists and is accessible
// let's do our stuff

这对你有用吗?

关于c++ - 写入文件 PBS MPI 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32052876/

相关文章:

cluster-computing - 并行运行多个串行相关的作业

queue - 如何找到可用扭矩Pbs队列的完整列表?

c++ - 在 OpenCV 中将 YUV 转换为 BGR 或 RGB

c++ - 另一个与设计相关的 C++ 问题

c++ - 在没有 malloc 和 free 的情况下存储和回收堆分配的可变大小对象

c - mpi.h : Using a type w/o defining it?

c++ - 减去两个元素(来自同一结构)的地址?

python-3.x - 使用 SLURM 和 MPI(4PY) : Cannot allocate requested resources

c++ - 如何调用所有基类的析构函数? (或一个共同的功能)

logging - PBS 脚本 -o 文件到多个位置