在 MPI-IO 上交织来自不同处理器的二进制数据
Interleaving binary data from different processors on MPI-IO
我正在尝试使用 MPI I/O(MPI-2.0、mpich2)编写二进制文件。
下面是一个最小示例,其中 2 个文件 '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
0000005
np=3:
> od -i ints
0000000 131328 0
0000006
等等
是我的代码有误,还是 'od -i' 根本不适合显示带有整数的二进制文件?
谢谢,
树里
除非你设置了基本类型的文件视图,否则偏移索引实际上是以字节为单位的——也就是说,默认的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
也有效。通常,您会希望使用不同的文件视图,而不是为每个处理器使用显式偏移量。
我正在尝试使用 MPI I/O(MPI-2.0、mpich2)编写二进制文件。
下面是一个最小示例,其中 2 个文件 '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
0000005
np=3:
> od -i ints
0000000 131328 0
0000006
等等
是我的代码有误,还是 'od -i' 根本不适合显示带有整数的二进制文件?
谢谢,
树里
除非你设置了基本类型的文件视图,否则偏移索引实际上是以字节为单位的——也就是说,默认的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
也有效。通常,您会希望使用不同的文件视图,而不是为每个处理器使用显式偏移量。