Ofi*_*r N 5 c parallel-processing mpi text-files
我正在尝试使用 MPI 写入文本文件,但未创建该文件。我只需要在主人处写(等级 = 0),但没有任何效果。它仅在我在控制台中运行程序(并保存损坏的元素)而不是在 Mpich2 中运行并且我附加了代码时才起作用。谢谢你的帮助。
/* -*- Mode: C; c-basic-offset:4 ; -*- */
/*
* (C) 2001 by Argonne National Laboratory.
* See COPYRIGHT in top-level directory.
*/
/* This is an interactive version of cpi */
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
int namelen, numprocs, rank;
char processor_name[MPI_MAX_PROCESSOR_NAME];
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Get_processor_name(processor_name,&namelen);
MPI_Status status;
FILE* f = fopen("test.txt","wb+");
if (rank == 0) {
for (int i=0; i < 5; i++){
fprintf(f,"%d \n",i);
}
fclose(f);
}
else {
// do nothing
}
MPI_Finalize();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在您发布的示例代码中,所有进程都打开该文件,只有进程 0 关闭它。您可以尝试以下修改吗?
if (rank == 0) {
FILE* f = fopen("test.txt","wb+");
if(f==NULL){printf("failed to open file: permission issue ?\n");exit(1);}
for (int i=0; i < 5; i++){
fprintf(f,"%d \n",i);
}
fclose(f);
}
Run Code Online (Sandbox Code Playgroud)
由于您的代码似乎来自阿贡国家实验室,我认为它是在使用特定文件系统的集群上运行的。以下代码基于您的代码。它在单个进程上使用MPI_File_open()和,使用.MPI_File_write()MPI_COMM_SELF
/* This is an interactive version of cpi */
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[])
{
int namelen, numprocs, rank;
char processor_name[MPI_MAX_PROCESSOR_NAME];
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Get_processor_name(processor_name,&namelen);
MPI_Status status;
MPI_File fh;
if (rank == 0) {
MPI_File_open(MPI_COMM_SELF, "test.txt",MPI_MODE_CREATE | MPI_MODE_WRONLY,MPI_INFO_NULL,&fh);
//FILE* f = fopen("test.txt","wb+");
//if(f==NULL){
//printf("failed to open file\n");exit(1);
//}
for (int i=0; i < 5; i++){
char buf[42];
//fprintf(f,"%d \n",i);
snprintf(buf,42,"%d \n",i);
MPI_File_write(fh,buf,strlen(buf), MPI_CHAR,&status);
}
// fclose(f);
MPI_File_close(&fh);
}
else {
// do nothing
}
MPI_Finalize();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请确保您拥有在所考虑的文件夹中写入的权限。确保所有节点都可以访问该文件夹!尝试一些文件夹,例如您的文件夹/tmp或/scratch...您的集群可能在某处有某种文档,告诉您可以在哪里写入文件!
| 归档时间: |
|
| 查看次数: |
4890 次 |
| 最近记录: |