@@ -20,14 +20,16 @@ static string path;
20
20
static string *fnames;
21
21
static int fileAmount = 0 ;
22
22
static int curIndex4Fnames;
23
- static fpos_t curSeek;
23
+ static off_t curSeek;
24
+ static off_t stopSeek;
24
25
static char buffer[BUFFER_SIZE];
25
26
static FILE *infile;
26
27
27
28
int moveToNext () {
28
29
if (NULL != infile)
29
30
fclose (infile);
30
31
// curSeek.__pos = 0;
32
+
31
33
if (curIndex4Fnames + 1 >= fileAmount) {
32
34
cout << " All files are read." << endl;
33
35
return 0 ;
@@ -38,6 +40,24 @@ int moveToNext() {
38
40
cerr << " Unable to open file: " << fname << endl;
39
41
return -1 ;
40
42
}
43
+
44
+ fseek (infile, 0 , SEEK_END);
45
+ off_t totalSize = ftello (infile);
46
+ stopSeek = (thisRank+1 ==host_num)?totalSize: totalSize / host_num * (thisRank+1 );
47
+ if (thisRank == 0 ) {
48
+ curSeek = 0 ;
49
+ fseeko (infile, curSeek, SEEK_SET);
50
+ } else {
51
+ curSeek = totalSize / host_num * thisRank;
52
+ if (curSeek < 1 ) {
53
+ cerr << " Error on computing file position for #" << thisRank << endl;
54
+ return -1 ;
55
+ }
56
+ fseeko (infile, curSeek - 1 , SEEK_SET); // 定位到属于该worker的前一个位置,以防止curSeek就是一条完整的read的开端的情况
57
+ while (getc (infile) != ' \n ' ); // 把不完整的read过滤掉
58
+ curSeek = ftello (infile);
59
+ }
60
+
41
61
return 1 ;
42
62
}
43
63
@@ -65,15 +85,16 @@ int readIOInit(int currank, int world_size, string filepath, string *filenames,
65
85
int getNextRead (string *outread, size_t *readpos) {
66
86
if (fileAmount < 1 ) return -1 ;
67
87
// 先尝试读取,如果不能读取则转向下一个文件
68
- while (fgets (buffer, BUFFER_SIZE, infile) == NULL ) {
88
+ while (curSeek >= stopSeek ) {
69
89
int flag;
70
90
while ((flag = moveToNext ()) == -1 ) ; // 当读到的文件不能打开则马上打开下一个
71
91
if (flag == 0 ) return -1 ; // 全部读完则返回-1
72
92
}
73
- // *readpos = curSeek.__pos; // 读取前的位置
74
- // fgetpos(infile, &curSeek);
93
+ fgets (buffer, BUFFER_SIZE, infile);
94
+ *readpos = curSeek; // 读取前的位置
95
+ curSeek = ftello64 (infile);
75
96
76
- int len = strlen (buffer) - 1 ;
97
+ int len = strlen (buffer) - 1 ; // 去除fgets中的换行符
77
98
*outread = string (buffer, len);
78
99
79
100
return 1 ;
0 commit comments