-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigramPar.cpp
66 lines (52 loc) · 1.45 KB
/
bigramPar.cpp
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
#include "include/Bigrams.hpp"
#include "FileUtility.cpp"
#include "threadsafe_unordered_map.hpp"
#include <cstdlib>
threadsafe_unordered_map<string> hashMap;
boost::container::vector<string> words;
void countFunction(size_t bottom, size_t edge);
void parallelBigram(size_t nCores);
int main(int argc, char**argv) {
FileUtility readFile;
size_t cores = 0;
if (argc >= 2){
string path = argv[1];
words = readFile.readInputFile(path);
if (argc == 3) cores = atoi(argv[2]);
}
parallelBigram(cores);
return 0;
}
void parallelBigram(size_t nCores){
hashMap.rehash(words.size());
vector<thread> threads;
int bottom = 0;
int edge = 0;
size_t cores = 0;
if (nCores > 0){
cores = nCores;
}else cores = std::thread::hardware_concurrency();
Timer timer;
timer.start();
for (size_t i = 0; i < cores; i++) {
edge = words.size()*(i+1)/cores;
threads.push_back(std::thread(countFunction,bottom,edge));
bottom = edge;
}
for (auto& th : threads) th.join();
timer.stop();
std::cout << timer.getElapsedTimeInSec() << "\n";
hashMap.writeHtmlFile("./bigrams.html");
}
void countFunction(size_t bottom, size_t edge){
string bigram;
for (size_t i = bottom; words.size() >= (edge-1) && i < edge; i++) {
for (size_t j = 0; j < (words[i].length()-1); j++) {
bigram = words[i].substr(j,2);
if (hashMap.count(bigram)==0)
hashMap.insert(bigram);
else
hashMap.add(bigram);
}
}
}