MPI_File_read错误 [英] MPI_File_read Error
问题描述
我正在尝试使用MPI_File_read从输入文件中读取.但是它没有正确读取值.这是我的输入文件:
I am trying to use MPI_File_read to read from an input file. But it is not correctly reading the values. Here is my input file:
11 3 4 5 2
这是我用来尝试阅读的代码:
And here is the code i am using to try to read it:
char *filename = "input/8";
int n;
if (MPI_File_open(comm_2d, filename, MPI_MODE_RDONLY, MPI_INFO_NULL, &f) != MPI_SUCCESS) {
fprintf(stderr, "Cannot open file %s\n", filename);
MPI_Abort(comm_2d, FILE_NOT_FOUND);
MPI_Finalize();
return 1;
}
MPI_File_seek(f, 0, MPI_SEEK_SET);
MPI_File_read(f, &n, 1, MPI_INT, &status);
cout << "n :" << n << endl;
MPI_File_close(&f);
根据我的理解,这应该从input.txt中读取第一个int"11".但是,如果给我一个很大的数字.
From my understanding this should read the first int "11" from the input.txt. But if gives me a very large number instead.
我在做什么错了?
推荐答案
MPI_File_Read从文件中读取MPI_INT的二进制表示形式.如果给它一个ASCII(文本)文件,它可能会将前4个字符解释为一个32位整数,并按照您的描述返回非常大的数字".
MPI_File_Read reads a binary representation of an MPI_INT from your file. If you give it an ASCII (text-) -file it'll probably interpret the first 4 characters as one 32-bit integer and return a "very large number" just as you describe.
在这种情况下,您可以例如将整个文件读取到缓冲区中,然后使用sscanf()或任何其他可用的文本解析功能.
In that case, you could for example read the whole file into a buffer and then use sscanf() or any other text parsing function you have available.
这篇关于MPI_File_read错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!