我是 MPI 的新手。我想使用 MPI_File_read_at() 逐行读取 txt 文件中的数据。每一行的长度是不同的,所以当我读取一行时(设置缓冲区的长度),有时它也会读取下一行,将下一行的一部分发送到缓冲区中,这确实会导致问题......所以,我想知道有什么方法可以使用 MPI_File_read_at() 逐行读取数据?让它在每行末尾遇到“\ n”时停止?或者您是否有更好的建议通过使用 MPI_File_read_at() 以外的 MPI 函数逐行读取数据?
我想我的问题是如何使用 MPI_File_read_at() 做与 fgets 相同的事情
/*the traditional way to read a file line by line:*/
for(i=0;i<nlens;i++)
{
fgets(line, 20, fp);
sscanf(line,"%d%d",&e1,&e2);
}
/*the way I am doing this by MPI*/
int offset = 0;
int count = 15;
char line[15];
for(i=0;i<nlens;i++)
{
offset=i*count;
MPI_File_read_at(fp, offset, line, count, MPI_CHAR, &status);
printf("line%d:/n%s/n",i,line);
}
/*So, if I have a file looks like:*/
0 2
0 44353
3 423
4 012312
5 2212
5 476
/*the output of mpi version will be:*/
line0:
0 2
0
line1:
44353
3
line2:
423
4
line3:
012312
5
line4:
2212
5
line5:
476
2
5
void read_data(char address_data[], int num_edges, int link[][100])
{
int i,e1,e2;
FILE *fp;
char line[30];
int totalTaskNum, rankID;
MPI_Comm_rank(MPI_COMM_WORLD, &rankID);
MPI_Comm_size(MPI_COMM_WORLD, &totalTaskNum);
MPI_Status status;
if(rankID == 0) /*to avoid different processors access to the file simultaneously*/
{
fp = fopen(address_data, "r");
for(i=0;i<num_edges;i++)
{
fgets(line, 20, fp);
sscanf(line,"%d%d",&e1,&e2);
link[e1][e2]++;
}
fclose(fp);
}
}