-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathomp.cpp
More file actions
278 lines (236 loc) · 7.44 KB
/
omp.cpp
File metadata and controls
278 lines (236 loc) · 7.44 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <fstream>
#include <unordered_map>
#include <string>
#include <cctype>
#include <iostream>
#include <vector>
#include <algorithm>
#include <omp.h>
#include <queue>
using namespace std;
vector<string> readers_q;
vector<vector<pair<string, size_t>>> reducer_queues;
unordered_map<string, size_t> global_counts;
size_t total_words;
size_t files_remain;
int num_reducers;
int num_readers;
int readers_avail;
omp_lock_t readers_lock;
vector<omp_lock_t> reducer_locks;
omp_lock_t global_counts_lock;
void process_word(string &w) {
// Remove punctuation and non-ascii chars at beginning
while (!w.empty()) {
signed char c = w.front();
if (c < 0 || ispunct(c)) {
w.erase(0, 1);
continue;
}
break;
}
// Remove punctuation and non-ascii chars at end
while (!w.empty()) {
signed char c = w.back();
if (c < 0 || ispunct(c)) {
w.pop_back();
continue;
}
break;
}
// Convert all letters to lowercase
for (char &ch : w) {
unsigned char c = static_cast<unsigned char>(ch);
if (isupper(c)) {
ch = tolower(c);
}
}
}
void read_file (char* fname) {
#pragma omp atomic
readers_avail--;
size_t wc = 0;
ifstream fin(fname);
if (!fin) {
fprintf(stderr, "error: unable to open input file: %s\n", fname);
exit(1);
}
// Process words in chunks to reduce locking
const int chunk_size = 1024; // select the best chunk size
vector<string> words;
words.reserve(chunk_size);
string word;
while (fin >> word) {
process_word(word);
if (!word.empty()) { // avoid pushing empty strings
wc++;
words.push_back(word);
// Check if current batch is full, merge to the readers_q if it is
if (words.size() == chunk_size) {
omp_set_lock(&readers_lock);
readers_q.insert(readers_q.end(),
make_move_iterator(words.begin()),
make_move_iterator(words.end()));
omp_unset_lock(&readers_lock);
words.clear();
}
}
}
// Merge any remaining words
omp_set_lock(&readers_lock);
readers_q.insert(readers_q.end(), make_move_iterator(words.begin()), make_move_iterator(words.end()));
omp_unset_lock(&readers_lock);
#pragma omp atomic
total_words += wc;
#pragma omp atomic
files_remain--;
#pragma omp atomic
readers_avail++;
}
int hash_str(string s, int R) {
int sum = 0;
for (unsigned char c : s) {
sum += c;
}
return sum % R;
}
void mapping_step() {
unordered_map<string, size_t> buckets;
// Grab elemnts from the work q in chunks
const int chunk_size = 1024; // find which chunk size works the best
vector<string> working_batch;
working_batch.reserve(chunk_size);
while (true) {
working_batch.clear();
// Lock and grab new chunk of elements if queue is not empty
omp_set_lock(&readers_lock);
for (size_t i = 0; i < chunk_size && !readers_q.empty(); ++i) {
working_batch.push_back(readers_q.back());
readers_q.pop_back();
}
omp_unset_lock(&readers_lock);
if (!working_batch.empty()) {
// Queue not empty -- process new elements
for (size_t i = 0; i < working_batch.size(); ++i) {
buckets[working_batch[i]]++;
}
}
else {
int remaining;
// Shared global variable -- must be read atomically
#pragma omp atomic read
remaining = files_remain;
if (remaining == 0) {
// Queue empty and all files are processed
break;
}
else {
// Mappers are ahead of readers
#pragma omp taskyield
}
}
}
// Push thread's results into the reducer queues
for (auto el : buckets) {
int index = hash_str(el.first, num_reducers);
omp_set_lock(&reducer_locks[index]);
reducer_queues[index].push_back(el);
omp_unset_lock(&reducer_locks[index]);
}
}
void reduce_step(int id) {
// Use local hash table for partial results
unordered_map<string, size_t> local_result;
for(auto &cur_entry : reducer_queues[id]) {
local_result[cur_entry.first] += cur_entry.second;
}
//Merge partial results into global results
omp_set_lock(&global_counts_lock);
for (auto &el : local_result) {
global_counts[el.first] += el.second;
}
omp_unset_lock(&global_counts_lock);
}
int main(int argc, char* argv[]) {
if (argc < 2) {
fprintf(stderr, "usage: %s <input_files>\n", argv[0]);
return 1;
}
int n_threads = omp_get_max_threads();
int num_mappers = n_threads;
num_readers = 2;
num_reducers = n_threads * 2; // Works best on my laptop -- test on ISAAC
files_remain = argc - 1;
readers_avail = num_readers;
cerr << "Testing " << n_threads << " thread(s), " << num_readers << " readers\n";
omp_init_lock(&readers_lock);
omp_init_lock(&global_counts_lock);
reducer_locks.resize(num_reducers);
for (int i = 0; i < num_reducers; ++i) {
omp_init_lock(&reducer_locks[i]);
}
reducer_queues.resize(num_reducers);
double start, end, start_r, start_p;
start = omp_get_wtime();
#pragma omp parallel
{
#pragma omp single
{
// File reading step
size_t f_count = 1;
int r;
while (argv[f_count]) {
#pragma omp atomic read
r = readers_avail;
if(r > 0) {
#pragma omp task firstprivate(f_count)
{
read_file(argv[f_count]);
}
f_count++;
}
}
// Mapping step
for (int i = 0; i < num_mappers; ++i) {
#pragma omp task
{
mapping_step();
}
}
}
}
start_r = omp_get_wtime();
// Reducing step
#pragma omp parallel for
for (int i = 0; i < num_reducers; ++i) {
reduce_step(i);
}
start_p = omp_get_wtime();
vector<pair<string, size_t>> counts;
for (auto &el : global_counts) {
counts.emplace_back(el.first, el.second);
}
// Sort in alphabetical order
sort(counts.begin(), counts.end(),
[](const auto &a, const auto &b) {
return a.first < b.first;
});
// Writing step
ofstream fout("results_omp.txt");
fout << "Filename: " << argv[1] << ", total words: " << total_words << '\n';
for (size_t i = 0; i < counts.size(); ++i) {
fout << "[" << i << "] " << counts[i].first << ": " << counts[i].second << '\n';
}
end = omp_get_wtime();
// Use cerr to always print in terminal
cerr << "OpenMP time: " << (end - start) * 1000 << " ms\n";
cerr << " File read & Map time: " << (start_r - start) * 1000 << " ms\n";
cerr << " Reducing time: " << (start_p - start_r) * 1000 << " ms\n";
cerr << " Sort & Print time: " << (end - start_p) * 1000 << " ms\n";
omp_destroy_lock(&readers_lock);
omp_destroy_lock(&global_counts_lock);
for (int i = 0; i < num_reducers; ++i) {
omp_destroy_lock(&reducer_locks[i]);
}
return 0;
}