-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiDriver.cpp
More file actions
179 lines (142 loc) · 4.48 KB
/
multiDriver.cpp
File metadata and controls
179 lines (142 loc) · 4.48 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
#include "hash.hpp"
#include <iostream>
#include <random>
#include <chrono>
#include <getopt.h>
#include <pthread.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
namespace {
int Verbosity;
int NIters;
int NThreads;
int NEntries;
int Distribution;
#define ALIGN64 __attribute__ ((aligned (64)))
typedef void* (*ThreadWorker_t)(void *);
unsigned int KeyMax;
pthread_t *WorkerThreads;
typedef struct {
int threadId_;
KaLib::HashTable<int>* htPtr_;
} WorkerArgs_t;
void
ProcessOptions(int argc, char** argv)
{
while (1) {
static struct option long_options[] =
{
{"verbose", no_argument, &Verbosity, 'v'},
{"threads", required_argument, &NThreads, 't'},
{"entries", required_argument, &NEntries, 'e'},
{"count", required_argument, &NIters, 'c'},
{"distr", required_argument, &Distribution, 'd'},
{ 0, 0, 0, 0}
};
int option_index = 0;
int c = getopt_long(argc, argv, "vt:e:c:", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'v':
Verbosity = true;
cout << ("option -verbose ") << Verbosity << endl;
break;
case 't':
NThreads = atoi(optarg);
cout << ("option -threads ") << NThreads << endl;
break;
case 'e':
NEntries = atoi(optarg);
cout << ("option -entries ") << NEntries << endl;
break;
case 'c':
NIters = atoi(optarg);
cout << ("option -count ") << NIters << endl;
break;
case 'd':
Distribution = atoi(optarg);
cout << ("option -distribution ") << Distribution << endl;
break;
case 'r':
NRetries = atoi(optarg);
cout << ("option -retries ") << Retries << endl;
break;
default:
exit(1);
}
}
}
void*
NeedleWorkUnif(void* threadArgs)
{
WorkerArgs_t *wArg = (WorkerArgs_t*)threadArgs;
srand( time(NULL) );
long tId = (long) wArg->threadId_;
int lnEntries = NEntries;
while (lnEntries--) {
int k = rand();
int v = rand();
wArg->htPtr_->insert(k, v);
}
pthread_exit((void*) tId);
return threadArgs;
}
#ifdef NORMAL_DISTRIBUTION
void*
NeedleWorkNorm(void* threadArgs)
{
WorkerArgs_t *wArg = (WorkerArgs_t*)threadArgs;
long tId = (long) wArg->threadId_;
srand( time(NULL) );
std::default_random_engine generator;
generator.seed( time(NULL) );
std::normal_distribution<double> distribution(0.5, 0.50);
int lnEntries = NEntries;
while (lnEntries--) {
double ks = distribution(generator);
int k = ks * KeyMax;
int v = rand();
wArg->htPtr_->insert(k, v);
}
pthread_exit((void*) tId);
return threadArgs;
}
#endif // NORMAL_DISTRIBUTION
void
Threading(int nThreads)
{
KaLib::HashTable<int> MyTable(NEntries * nThreads);
WorkerThreads = new pthread_t[NThreads+1];
using namespace std::chrono;
std::chrono::time_point<std::chrono::system_clock> start =
std::chrono::system_clock::now();
for (int t = 1; t <= NThreads; t = t + 1) {
WorkerArgs_t tArg = {t, &MyTable};
pthread_create(&(WorkerThreads[t]), NULL, NeedleWorkUnif, (void*)&tArg);
}
for (int t = 1; t <= NThreads; t = t + 1) {
pthread_join(WorkerThreads[t], NULL);
cout << "Thread #" << t << " finished\n";
}
std::chrono::time_point<std::chrono::system_clock> end =
std::chrono::system_clock::now();
int elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds>
(end - start).count();
// typedef std::chrono::duration<int, std::chrono::seconds> seconds_t;
// seconds_t duration( std::chrono::duration_cast<seconds_t>(end - start) );
MyTable.printStats(std::cout);
// cout << "Elapsed time (secs)spent in the Workers: " << duration.count();
cout << "Elapsed time (secs)spent in the Workers: " << elapsed_seconds
<< endl;
pthread_exit(NULL);
}
} // anonymous
int
main(int argc, char** argv)
{
ProcessOptions(argc, argv);
KeyMax = (NEntries * NThreads);
Threading(NThreads);
}