我试图使用MPI /O (MPI-2.0,mpich2)编写一个二进制文件。
下面是一个最小的示例,其中两个文件“chars”和“ints”应该打印为‘0123.’还有‘abcd’分别使用。
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv)
{
// Initialize the MPI environment
MPI_Init(NULL, NULL);
// Get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// Get the rank of the process
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
// MPI I/O
MPI_File file;
MPI_Status status;
int offset = world_rank;
// MPI I/O with integers
MPI_File_open (
MPI_COMM_WORLD, // MPI_Comm comm
"ints", // char *filename
MPI_MODE_CREATE | // int amode
MPI_MODE_RDWR,
MPI_INFO_NULL, // MPI_Info info
&file // MPI_File *fh
);
int buf1 = world_rank;
MPI_File_write_at (
file, // MPI_File fh
offset, // MPI_Offset offset
&buf1, // void *buf
1, // int count
MPI_INT, // MPI_Datatype datatype
&status // MPI_Status *status
);
MPI_File_close (&file);
// MPI I/O with chars
MPI_File_open (
MPI_COMM_WORLD, // MPI_Comm comm
"chars", // char *filename
MPI_MODE_CREATE | // int amode
MPI_MODE_RDWR,
MPI_INFO_NULL, // MPI_Info info
&file // MPI_File *fh
);
char buf2 = 'a' + (char)world_rank;
MPI_File_write_at (
file, // MPI_File fh
offset, // MPI_Offset offset
&buf2, // void *buf
1, // int count
MPI_CHAR, // MPI_Datatype datatype
&status // MPI_Status *status
);
MPI_File_close (&file);
// Finalize the MPI environment.
MPI_Finalize();
return 0;
}我查查的结果是对的,
> od -c chars
0000000 a b c d
0000004但是对于整数,它只有当np = 0时才能工作。当np >0时,我得到了我不明白的结果:
np = 2:
> od -i ints
0000000 256 0
0000005np = 3:
> od -i ints
0000000 131328 0
0000006等。
我的代码是错的,还是“od-i”根本不适合显示带有整数的二进制文件?
谢谢,
尤里
发布于 2015-03-19 14:19:05
除非您已经用基本类型设置了文件视图,否则偏移索引实际上是以字节为单位的--也就是说,默认的etype是单个字节。这实际上是非常明智的,但如果它被记录在手册中是有帮助的。
因此,改变这一行:
MPI_File_write_at (
file, // MPI_File fh
offset, // MPI_Offset offset对此:
MPI_File_write_at (
file, // MPI_File fh
offset*sizeof(int), // MPI_Offset offset给出你想要的答案:
$ od -i ints
0000000 0 1 2
0000014另一方面,如果只在此文件中使用整数,则更容易设置文件视图:
MPI_File_set_view( file, 0, MPI_INT, MPI_INT, "native", MPI_INFO_NULL);
MPI_File_write_at (
file, // MPI_File fh
offset, // MPI_Offset offset也很管用。通常,您希望对每个处理器使用不同的文件视图而不是显式的偏移量。
https://stackoverflow.com/questions/29146343
复制相似问题