我在编写将以特定模式编写的并行 MPI I/O 程序时遇到问题。我能够让进程 0 写入整数 0-9,进程 1 写入整数 10-19,进程 2 写入整数 20-29,等等。
proc 0: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
proc 1: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
proc 2: [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
proc 3: [30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
以下是完成此操作的代码:
int main(int argc, char *argv[]) {
// MPI_Finalize();
int i, rank, size, offset;
MPI_File fhw;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int N = size * 10;
int buf[N];
for ( i = 0; i < N; ++i ) {
buf[i] = rank * 10 + i;
}
offset = rank * (N/size) * sizeof(int);
MPI_File_open(MPI_COMM_WORLD, "datafile", MPI_MODE_CREATE|MPI_MODE_WRONLY,
MPI_INFO_NULL, &fhw);
printf("(%d) Writing to file...\n", rank);
printf("\nRank: (%d), Offset: %d\n", rank, offset);
MPI_File_write_at(fhw, offset, buf, (N/size), MPI_INT, &status);
MPI_File_close(&fhw);
MPI_Finalize();
return 0;
}
但是,我对如何产生以下结果感到困惑:
// starting out:
proc 0: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
proc 1: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
proc 2: [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
proc 3: [30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
// proc 0 writes first 2 slots, then proc 1 writes next 2, etc.
result: [0, 1, 10, 11, 20, 21, 30, 31, 2, 3, 12, 13, 22, 23, ..., 8, 9, 18, 19, 28, 29, 38, 29]
在过去的几个小时里,我在在线查找示例和文档时尝试使用MPI_File_set_view
,但无法使其正常工作。有人可以指导我正确的方向吗?