首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >写入文件PBS MPI时出错

写入文件PBS MPI时出错
EN

Stack Overflow用户
提问于 2015-08-17 22:23:25
回答 1查看 234关注 0票数 0

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

代码语言:javascript
复制
#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命令

代码语言:javascript
复制
mpicxx -Wall test.cc -o test

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

代码语言:javascript
复制
#!/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 

我预计会有这样的结果:

代码语言:javascript
复制
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_{0..5})中的res_*文件,而其余节点不写入。

有什么问题吗?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2015-08-19 05:47:05

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

既然在本质上你想要的是对目录和/或它的创建(如果需要)进行连续的健全性检查,为什么不让排名为0的MPI进程来做这件事呢?

它会给你类似这样的东西:

代码语言:javascript
复制
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

这对你有用吗?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32052876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档