我在 MPI 时遇到问题,当我从wmpiexec运行我的MPI程序时,无法生成任何文件。但是当我在 visual studio 中运行时,它运行正常。
我试过通常看到fopen()
以及mpi API没有任何作用。
(该文件已存在)
/* 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;
char x='x';
if (rank == 0) {
MPI_File_open(MPI_COMM_SELF, "test.txt", MPI_MODE_RDONLY, 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_read(fh,&x,sizeof(char), MPI_CHAR, &status);
printf("%c",x);
}
getchar();
// fclose(f);
MPI_File_close(&fh);
}
else {
// do nothing
}
MPI_Finalize();
return 0;
}
答案 0 :(得分:0)
可以测试函数MPI_File_open()
返回的错误代码,以了解文件打开失败的原因。实际上,正如该函数的文档中强调的那样:
对于MPI I / O函数错误,默认错误处理程序设置为MPI_ERRORS_RETURN。可以使用MPI_File_set_errhandler更改错误处理程序;预定义的错误处理程序MPI_ERRORS_ARE_FATAL可用于使I / O错误致命。请注意,MPI不保证MPI程序可以继续发生错误。
因此 MPI_File_open()
的默认行为不会报告错误并尝试继续,即使文件不存在也是如此。因此,检索错误代码的值并调整错误处理程序似乎是调查问题的一种有希望的方式。以下是基于您的代码示例代码。它可以由mpicc main.c -o main -std=c99 -Wall
编译并由mpirun -np 2 main
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// A custom error handler
void file_errhandler_fn(MPI_File * file, int * errorcode, ... );
void file_errhandler_fn(MPI_File * file, int * errorcode, ... ){
if(*errorcode!=MPI_SUCCESS){
//your error handler here...
char error[MPI_MAX_ERROR_STRING+1];
int errorlen;
MPI_Error_string(*errorcode, error, &errorlen);
fprintf(stderr,"error handler: %s\n",error);
fflush(stderr);
//visible painful death, easily spotted
exit(1);
}
}
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;
//creating an error handler for the file, calling your custom function:
MPI_Errhandler errhandler;
MPI_File_create_errhandler(file_errhandler_fn, &errhandler);
char x='x';
if (rank == 0) {
int errorcode=MPI_File_open(MPI_COMM_SELF, "test.txt", MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
//in case the opening of the file went wrong, handle the error:
if(errorcode!=MPI_SUCCESS){
//your error handler here...
char error[MPI_MAX_ERROR_STRING+1];
int errorlen;
MPI_Error_string(errorcode, error, &errorlen);
fprintf(stderr,"opening file: %s\n",error);
fflush(stderr);
//visible painful death, easily spotted
exit(1);
}
//setting custom error handlerr for further operations
errorcode=MPI_File_set_errhandler(fh,errhandler);
//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);
errorcode=MPI_File_read(fh,&x,sizeof(char), MPI_CHAR, &status);
printf("index %d got %c",i,x);
// the following line should compile fine, but triggers MPI_ERR_COUNT, captured by the custom error handler.
//errorcode=MPI_File_write(fh, &i, -1, MPI_INT, MPI_STATUS_IGNORE);
}
getchar();
// fclose(f);
MPI_File_close(&fh);
}
else {
// do nothing
}
MPI_Errhandler_free(&errhandler);
MPI_Finalize();
return 0;
}
这样做可能无法完全解决您的问题,但它提供了一种调查问题的方法。错误代码的价值是多少?如果它对您没有帮助,请告诉我,以便我可以取消我的答案,让您的问题无法解答!