-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhledr.cpp
More file actions
320 lines (250 loc) · 7.04 KB
/
hledr.cpp
File metadata and controls
320 lines (250 loc) · 7.04 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <getopt.h>
#include <string.h>
#include <sys/time.h>
#include "Timer.hpp"
#define MAX_THREADS 8
#define MAX_RETRIES 4
#define ALIGN64 __attribute__ ((aligned (64)))
#define CACHE_LINE_BYTES 64
typedef unsigned long long ULong;
int DumpLevel = 1;
int MaxRetries = 5;
int LockType = 1; // By default, use Mutex Lock
int SharedMode = 1;
bool TxSucc = false;
typedef void* (*ThreadWorker_t)(void *);
ULong hLock_[64] ALIGN64;
int NumIters = (0x1 << 12);
int NumThreads = 2;
int VerboseFlag = 0;
int RTMEnabled = 1;
ULong SharedSum ALIGN64;
ULong SharedIndex ALIGN64 = 0;
int* SharedArray ALIGN64 = NULL;
int* SharedArrayPtr = NULL;
typedef struct {
char _padding[60];
int data;
} CLData_t;
pthread_t *ThreadArray;
pthread_mutex_t PmxLock = PTHREAD_MUTEX_INITIALIZER;
pthread_spinlock_t PspLock;
extern "C" {
#include "hLock.h"
};
char
CAS(volatile ULong* loc, ULong oldval, ULong newval)
{
char result;
__asm__ __volatile__(
".byte 0xf2\n"
"lock\n"
"cmpxchgw %2, %1\n"
"sete %0\n"
: "=q" (result), "=m" (*loc)
: "r" (newval), "m" (*loc), "a" (oldval)
: "memory"
);
return result;
}
void AllocHLELock() {
int *rawPtr_ = (int*) malloc(256 * sizeof(ULong));
// hLock_ = (ULong*)((char*) rawPtr_ + (CACHE_LINE_BYTES - ((ULong)rawPtr_ & 0x3f)));
// *hLock_ = Free;
memset(hLock_, 0, 64*sizeof(int));
}
void FreeHLELock() {
if (*hLock_ != Free) {
cerr << " HLE Lock in invalid state upon destruction " << endl;
}
}
int
HLE_Acquire(ULong* lock)
{
int backOff = 4;
while (CAS((ULong*)lock, Free, Held) == 0) {
if (*lock == Held) {
/* Implement better back-off here */
for (int c = backOff; c > 0; --c);
}
backOff <<= 1;
}
return 0;
}
int
HLE_TxTest(void)
{
unsigned char al;
__asm__ __volatile__(".byte 0x0f, 0x01, 0xd6\n"
"setnz %%al\n"
: "=a"(al) : : "cc");
return al;
}
void
HLE_Release(ULong* lck)
{
char result;
__asm__ __volatile__(
".byte 0xf3 \n"
"movw %1, %0\n"
: "=m" (*lck)
: "i" (Free)
: "memory"
);
}
void *
executeThreadLoopShared(void* data)
{
int k, txr = 0;
int threadID = (ULong) data;
int aIndex = 0;
for (k = 0; k < NumIters; ++k) {
HLE_Acquire(hLock_);
SharedArray[aIndex] += threadID;
// *sptr = ((threadID-1) * NumIters) + k;
HLE_Release(hLock_);
}
return NULL;
}
void *
executeThreadLoopPrivate(void* data)
{
int k, txr = 0;
int threadID = (ULong) data;
int aIndex = (threadID) * 16;
for (k = 0; k < NumIters; ++k) {
HLE_Acquire(hLock_);
SharedArray[aIndex] += threadID;
// *sptr = ((threadID-1) * NumIters) + k;
HLE_Release(hLock_);
}
return NULL;
}
void
createLoopThreads(int numThreads, ThreadWorker_t tWorker)
{
int i, tStatus = 0;
SharedSum = 0;
ThreadArray = (pthread_t*) malloc((numThreads+1) * sizeof(pthread_t*));
for (i = 1; i <= numThreads; i++)
{
tStatus = pthread_create(&ThreadArray[i], NULL, tWorker, (void*)i);
if (tStatus) {
printf("return code from pthread_create() is %d\n", tStatus);
exit(-1);
}
}
}
int
waitForThreads(int numThreads)
{
int i, fStatus = 0;
for (i = 1; i <= numThreads; i++)
{
if (pthread_join(ThreadArray[i], NULL)) {
printf("pthread_join failed for %d\n", i);
exit(-1);
} else {
// printf("pthread %d finished\n", i);
}
}
printf("All threads finished\n", i);
return fStatus;
}
void
printCheckSumOfArray(int numThreads, int numIters)
{
int i;
unsigned long sum = 0;
for (i = 0, sum = SharedArray[0]; i <= numThreads * 16; i += 16, sum += SharedArray[i]) {
printf("%d ", SharedArray[i]);
}
printf("\nCheck-Sum of Array: %lu\n", sum);
printf("Shared-Sum : %llu\n", SharedSum);
}
void
printSharedArray(int numThreads, int numIters)
{
int i;
printf("Assembled Array \n");
for (i = 0; i < numThreads * numIters; i++) {
printf(" %llu", SharedArray[i]);
}
printf("\n");
}
int
main(int argc, char *argv[])
{
int c;
ThreadWorker_t worker = NULL;
while (1) {
static struct option longOptions[] = {
{"verbose", no_argument, &VerboseFlag, 1},
{"iters", required_argument, 0, 'i'},
{"threads", required_argument, 0, 't'},
{"dumplevel", required_argument, 0, 'd'},
{"numretries",required_argument, 0, 'r'},
{"locktype", required_argument, 0, 'l'},
{"sharedmode",required_argument, 0, 's'},
{0, 0, 0, 0}
};
int optionIndex = 0;
c = getopt_long(argc, argv, "i:t:d:r:l:s:", longOptions, &optionIndex);
if (c == -1)
break;
switch (c) {
case 0:
if (longOptions[optionIndex].flag != 0)
break;
printf("option %s\n", longOptions[optionIndex].name);
break;
case 'i':
NumIters = atoi(optarg);
break;
case 't':
NumThreads = atoi(optarg);
break;
case 'd':
DumpLevel = atoi(optarg);
break;
case 'r':
MaxRetries = atoi(optarg);
break;
case 'l':
LockType = atoi(optarg);
break;
case 's':
SharedMode = atoi(optarg);
break;
default:
abort();
};
}
printf(" NumIters = %d \n", NumIters);
/* SharedArray = (ULong*) malloc(((8+1) * (NumIters+1) + 1) * sizeof(ULong)); */
int* rawPtr = (int*) malloc(64*1024*1024 * sizeof(ULong));
SharedArray = (int*)((char*) rawPtr + (CACHE_LINE_BYTES -
((ULong)rawPtr & 0x3f)));
memset(SharedArray, 0, 64*256 * sizeof(int));
if (SharedMode) {
worker = executeThreadLoopShared;
} else {
worker = executeThreadLoopPrivate;
}
AllocHLELock();
Timer coreTimer("Worker Threads");
coreTimer.Start();
createLoopThreads(NumThreads, worker);
waitForThreads(NumThreads);
coreTimer.Stop();
printCheckSumOfArray(NumThreads, NumIters);
coreTimer.PrintElapsedTime("Worker threads finished in ");
cout << "Final Index = \n" << SharedIndex;
if (VerboseFlag)
printSharedArray(NumThreads, NumIters);
return 0;
}