I'm a new learner of MPI. I'd like to use MPI_File_read_at() to read the data from a txt file line by line. The length of each line is different, so when I am reading one line (set a length of buffer), sometimes it will also read the next line, send some part of next line into the buffer, which really cause a problem... So, I'm wondering is there any way I can use MPI_File_read_at() to read data line by line? to let it stop when meet "\n" at the end of each line? Or do you have better suggestion to read data line by line by using a MPI function other than MPI_File_read_at() ?
I guess my question is how to use MPI_File_read_at() to do the same thing as fgets does
/*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);
}
}