This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
freq_pmem_cpp.cpp
213 lines (172 loc) · 4.42 KB
/
freq_pmem_cpp.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
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
/*
* freq_pmem_cpp.cpp -- pmem-based word frequency counter, C++ version
*
* create the pool for this program using pmempool, for example:
* pmempool create obj --layout=freq -s 1G freqcount
* freq_pmem_cpp freqcount file1.txt file2.txt...
*/
#include <cctype>
#include <thread>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <iostream>
#include <thread>
#include <libpmemobj++/make_persistent.hpp>
#include <libpmemobj++/make_persistent_array.hpp>
#include <libpmemobj++/p.hpp>
#include <libpmemobj++/pext.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/pool.hpp>
#include <libpmemobj++/transaction.hpp>
#include <libpmemobj++/mutex.hpp>
#define LAYOUT "freq"
#define NBUCKETS 10007
using pmem::obj::p;
using pmem::obj::persistent_ptr;
using pmem::obj::pool;
using pmem::obj::pool_base;
using pmem::obj::make_persistent;
using pmem::obj::delete_persistent;
using pmem::obj::transaction;
using pmem::obj::mutex;
/* entries in a bucket are a linked list of struct entry */
struct entry {
entry(int ct, const char *wrd,
const persistent_ptr<struct entry> &nxt) :
next{nxt},
word{pmemobj_tx_strdup(wrd, pmem::detail::type_num<char>())},
count{ct}
{}
persistent_ptr<struct entry> next;
persistent_ptr<char> word;
mutex mtx; /* protects count field */
p<int> count;
};
/* each bucket contains a pointer to the linked list of entries */
struct bucket {
mutex mtx; /* protects entries field */
persistent_ptr<struct entry> entries;
};
using buckets = persistent_ptr<bucket[NBUCKETS]>;
class freq {
/* hash a string into an index into h[] */
unsigned hash(const char *s)
{
unsigned h = NBUCKETS ^ ((unsigned)*s++ << 2);
unsigned len = 0;
while (*s) {
len++;
h ^= (((unsigned)*s) << (len % 3)) +
((unsigned)*(s - 1) << ((len % 3 + 7)));
s++;
}
h ^= len;
return h % NBUCKETS;
}
/* bump the count for a word */
void count(const char *word)
{
unsigned h = hash(word);
auto &mtx = ht[h].mtx;
mtx.lock();
auto ep = ht[h].entries;
for (; ep != nullptr; ep = ep->next)
if (strcmp(word, ep->word.get()) == 0) {
/* already in table, just bump the count */
/* drop bucket lock */
mtx.unlock();
/* lock entry and update it transactionally */
transaction::run(pop, [&ep]() {
ep->count++;
}, ep->mtx);
return;
}
/* allocate new entry in table */
transaction::run(pop, [&ep, &word, this, &h]() {
/* allocate new entry */
ep = make_persistent<entry>(1, word, ht[h].entries);
/* add it to the front of the linked list */
ht[h].entries = ep;
});
mtx.unlock();
}
public:
freq(buckets bht, pool_base &pool) : ht{bht}, pop{pool}
{}
/* break a test file into words and call count() on each one */
void count_all_words(const char *fname)
{
FILE *fp;
int c;
char word[MAXWORD];
char *ptr;
if ((fp = fopen(fname, "r")) == NULL)
throw std::runtime_error(std::string("fopen: ") +
fname);
ptr = NULL;
while ((c = getc(fp)) != EOF)
if (isalpha(c)) {
if (ptr == NULL) {
/* starting a new word */
ptr = word;
*ptr++ = c;
} else if (ptr < &word[MAXWORD - 1])
/* add character to current word */
*ptr++ = c;
else {
/* word too long, truncate it */
*ptr++ = '\0';
count(word);
ptr = NULL;
}
} else if (ptr != NULL) {
/* word ended, store it */
*ptr++ = '\0';
count(word);
ptr = NULL;
}
/* handle the last word */
if (ptr != NULL) {
/* word ended, store it */
*ptr++ = '\0';
count(word);
}
fclose(fp);
}
private:
buckets ht; /* pointer to the buckets */
pool_base &pop;
static const int MAXWORD = 8192;
};
struct root {
buckets ht; /* word frequencies */
/* ... other things we store in this pool go here... */
};
int main(int argc, char *argv[])
{
int arg = 2; /* index into argv[] for first file name */
if (argc < 3) {
std::cerr << "usage: " << argv[0]
<< " pmemfile wordfiles..." << std::endl;
exit(1);
}
auto pop = pool<root>::open(argv[1], LAYOUT);
auto q = pop.root();
/* before starting, see if buckets have been allocated */
if (q->ht == nullptr) {
transaction::run(pop, [&q]() {
q->ht = make_persistent<bucket[NBUCKETS]>();
});
}
int nfiles = argc - arg;
std::vector<std::thread> threads;
for (int i = 0; i < nfiles; ++i)
threads.emplace_back(&freq::count_all_words, freq(q->ht, pop),
argv[arg++]);
for (auto &t : threads)
t.join();
pop.close();
exit(0);
}