-
Notifications
You must be signed in to change notification settings - Fork 10
/
fastlwc-mt.c
189 lines (161 loc) · 4.44 KB
/
fastlwc-mt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
#include "simd.h"
#define BUFSIZE (sizeof(simd_vector) * 4096)
struct lwcount
{
size_t lcount, wcount, ccount;
};
// fallback to non-seeking variant (from fastlwc.c) for non-seekable files
// (should also be used for short files less than 10MB or so)
struct lwcount wc(int fd)
{
struct lwcount ret = { 0, 0, 0 };
lcount_state lstate = LCOUNT_INITIAL;
wcount_state wstate = WCOUNT_INITIAL;
size_t rem = 0;
ssize_t len;
simd_vector *buf = aligned_alloc(sizeof(simd_vector), BUFSIZE);
if (!buf) {
perror("fastlwc: alloc");
exit(EXIT_FAILURE);
}
posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
do {
len = read(fd, (char*)buf + rem, BUFSIZE - rem);
if (len > 0) {
rem += len;
ret.ccount += len;
} else if (len == 0) {
if (!rem)
break;
memset((char*)buf + rem, ' ', sizeof(simd_vector) - rem);
rem = sizeof(simd_vector);
} else {
if (errno == EINTR)
continue;
perror("fastlwc: read");
exit(EXIT_FAILURE);
}
simd_vector *vp = buf;
while (rem >= sizeof(simd_vector)) {
ret.lcount += count_lines(*vp, &lstate);
ret.wcount += count_words(*vp, &wstate);
rem -= sizeof(simd_vector);
vp++;
}
if (rem) // move rem leftover bytes to start of buf
memmove(buf, vp, rem);
} while (len);
ret.lcount += count_lines_final(&lstate);
ret.wcount += count_words_final(&wstate);
free(buf);
return ret;
}
// 100 lines of code and 6 levels of nesting... what's that, unreadable mess?
// self-commenting code, right?
struct lwcount wc_mt(int fd, off_t cur, off_t len)
{
const size_t blocks = (len + BUFSIZE - 1) / BUFSIZE;
size_t lcount = 0, wcount = 0;
#pragma omp parallel if(blocks > 1) reduction(+:lcount) reduction(+:wcount)
{
int tid = omp_get_thread_num(),
threads = omp_get_num_threads();
// allocate a bit extra for lookback
char *rbuf = aligned_alloc(sizeof(simd_vector),
BUFSIZE + sizeof(simd_vector));
if (!rbuf) {
perror("malloc");
exit(EXIT_FAILURE);
}
char *buf = rbuf + sizeof(simd_vector);
lcount_state lstate = LCOUNT_INITIAL;
wcount_state wstate = WCOUNT_INITIAL;
for (size_t i = 0, block; (block = i * threads + tid) < blocks; ++i) {
const size_t offset_end = cur + (block + 1) * BUFSIZE;
size_t bytes_left = BUFSIZE; // last block size handled later
ssize_t len, rem = 0;
if (block > 0) { // look back for whitespace for subsequent blocks
bytes_left++;
rem--;
}
do {
len = pread(fd, buf + rem, bytes_left, offset_end - bytes_left);
if (len > 0) {
if (rem < 0 && len >= rem)
wcount_state_set(&wstate, !isspace(buf[-1]));
bytes_left -= len;
rem += len;
} else if (len == 0) {
if (block != blocks - 1 || rem < 0) {
fputs("unexpected end of file\n", stderr);
exit(EXIT_FAILURE);
}
// not an error to reach EOF when processing last block
memset(buf + rem, ' ', sizeof(simd_vector) - rem);
rem = sizeof(simd_vector);
bytes_left = 0;
} else { // len < 0
if (errno == EINTR)
continue;
perror("read");
exit(EXIT_FAILURE);
}
simd_vector *vp = (simd_vector*)buf;
while (rem >= (ssize_t)sizeof(simd_vector)) {
lcount += count_lines(*vp, &lstate);
wcount += count_words(*vp, &wstate);
rem -= sizeof(simd_vector);
vp++;
}
if (rem)
memmove(buf, vp, rem);
} while (bytes_left);
}
lcount += count_lines_final(&lstate);
wcount += count_words_final(&wstate);
free(rbuf);
}
return (struct lwcount){
.lcount = lcount,
.wcount = wcount,
.ccount = len
};
}
int main(int argc, char *argv[])
{
int fd = STDIN_FILENO;
if (argc > 1 && strcmp(argv[1], "-") != 0) {
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
fprintf(stderr, "'%s': %s\n", argv[1], strerror(errno));
exit(EXIT_FAILURE);
}
}
struct lwcount total = { 0, 0, 0 };
off_t cur, end;
if ((cur = lseek(fd, 0, SEEK_CUR)) == -1
|| (end = lseek(fd, 0, SEEK_END)) == -1) {
// it seems we can't seek this file, fallback to linear read
total = wc(fd);
} else if (end > cur) {
total = wc_mt(fd, cur, end - cur);
}
printf(" %7zu %7zu %7zu", total.lcount, total.wcount, total.ccount);
if (argc > 1)
printf(" %s", argv[1]);
putchar('\n');
if (fd != STDIN_FILENO)
close(fd);
}