我想使用 MPIMPI_File_read_all
读取input.txt
包含二进制整数的文件0,1,2,...,1000
。
在我的实现中,我使用从 10*10 数组MPI_Type_create_subarray
中创建一个 5*5 子数组。mysub
希望从文件中读取以下矩阵。
0 1 2 3 4
10 11 12 13 14
20 21 22 23 24
30 31 32 33 34
40 41 42 43 44
但是我的程序改为读取以下整数(也会导致程序最后阶段的分段错误):
0 1 2 3 4
22002 -1984077600 22002 0 0
10 11 12 13 14
0 0 0 0 0
20 21 22 23 24
这是我的代码
#include<iostream>
#include<mpi.h>
using namespace std;
void read_subarray(const MPI_File &fh, int rank) {
int bigsize = 10, subsize = 5;
int **A = new int*[subsize];
int *buf = new int[subsize*subsize];
for (int i=0;i<subsize;i++) A[i] = &buf[subsize*i];
int bigsizes[] = {bigsize, bigsize};
int subsizes[] = {subsize, subsize};
int offsets[] = {0, 0};
MPI_Datatype mysub;
MPI_Type_create_subarray(2, bigsizes, subsizes, offsets, MPI_ORDER_C, MPI_INT, &mysub);
MPI_Type_commit(&mysub);
MPI_Status status;
int disp = 0;
MPI_File_set_view(fh, disp, MPI_INT, mysub, "native", MPI_INFO_NULL);
MPI_File_read_all(fh, &(A[0][0]), 1, mysub, &status);
cout<<"array for "<<rank<<endl;
for (int i=0;i<subsize; i++) {
for (int j=0;j<subsize; j++)
cout<<A[i][j]<<"\t";
cout<<endl;
}
MPI_Type_free(&mysub);
}
int main(int argc, char** argv)
{
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_File fh;
MPI_File_open(MPI_COMM_WORLD, "./input.txt", MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
read_subarray(fh, rank);
MPI_File_close(&fh);
MPI_Finalize();
return 0;
}