-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathRMRS.c
208 lines (168 loc) · 5.35 KB
/
RMRS.c
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
#include <stdbool.h>
//======================================================
#define FREE_NODE N
#define FREE_LOCK N
typedef TYPE QElem_t;
typedef struct CALIGN {
QElem_t * elements;
unsigned int * inQueue;
unsigned int front, rear;
unsigned int capacity;
} Queue;
static unsigned int toursize CALIGN;
static inline bool QnotEmpty( volatile Queue * queue ) {
return queue->front != queue->rear;
} // QnotEmpty
static inline void Qenqueue( volatile Queue * queue, QElem_t element ) {
if ( FASTPATH( element != FREE_NODE && queue->inQueue[element] == false ) ) {
queue->elements[queue->rear] = element;
queue->rear = cycleUp( queue->rear, queue->capacity );
// queue->rear += 1;
// if ( queue->rear == queue->capacity )
// queue->rear=0;
queue->inQueue[element] = true;
} // if
} // Qenqueue
static inline QElem_t Qdequeue( volatile Queue * queue ) {
QElem_t element = queue->elements[queue->front];
queue->front = cycleUp( queue->front, queue->capacity );
// queue->front += 1;
// if ( queue->front == queue->capacity )
// queue->front = 0;
queue->inQueue[element] = false;
return element;
} // Qdequeue
static inline Queue * Qctor( unsigned int maxElements ) {
Queue *queue;
queue = malloc( sizeof(typeof(*queue)) );
queue->capacity = maxElements + 1;
queue->front = queue->rear = 0;
queue->elements = malloc( queue->capacity * sizeof(typeof(queue->elements[0])) );
queue->inQueue = malloc( maxElements * sizeof(typeof(queue->inQueue[0])) );
for ( unsigned int i = 0; i < maxElements; i += 1 ) {
queue->inQueue[i] = false;
} // for
return queue;
} // Qctor
static inline void Qdtor( volatile Queue *queue ) {
free( queue->inQueue );
free( queue->elements );
free( (void *)queue );
} // Qdtor
//======================================================
typedef struct CALIGN {
unsigned int enter;
unsigned int wait;
} Tstate;
static volatile Queue * q CALIGN;
static VTYPE ** tournament CALIGN;
static volatile Tstate * arrState CALIGN;
static VTYPE first CALIGN;
static VTYPE exits CALIGN;
static TYPE PAD CALIGN __attribute__(( unused )); // protect further false sharing
#define await( E ) while ( ! (E) ) Pause()
static void * Worker( void * arg ) {
TYPE id = (size_t)arg;
uint64_t entry;
#ifdef FAST
unsigned int cnt = 0, oid = id;
#endif // FAST
NCS_DECL;
volatile typeof(arrState[0].enter) *tEnter = &arrState[id].enter;
volatile typeof(arrState[0].wait) *tWait = &arrState[id].wait;
for ( int r = 0; r < RUNS; r += 1 ) {
RTYPE randomThreadChecksum = 0;
for ( entry = 0; stop == 0; entry += 1 ) {
NCS;
*tEnter = true;
// up hill
typeof(id) node = id;
for ( typeof(toursize) j = 0; j < toursize; j += 1 ) { // tree register
tournament[j][node] = id;
node >>= 1;
} // for
if ( FASTPATH( ! Cas( first, FREE_LOCK, id ) ) ) {
typeof(exits) e = exits;
await( exits - e >= 2 || first == id || first == FREE_LOCK );
if ( FASTPATH( ! Cas( first, FREE_LOCK, id ) ) ) {
await( *tWait );
} // if
} // if
*tEnter = false;
*tWait = false;
exits += 1 ;
WO( Fence(); );
randomThreadChecksum += CS( id );
WO( Fence(); ); // prevent write floating up
// down hill
TYPE thread = tournament[toursize - 1][0];
if ( FASTPATH( thread != FREE_NODE && thread != id && arrState[thread].enter ) )
Qenqueue( q, thread );
for ( int j = toursize - 2; j >= 0; j -= 1 ) {
node = id >> j;
if ( node & 1 ) {
node -= 1;
} // if
thread = tournament[j][node];
if ( thread != FREE_NODE && thread != id && arrState[thread].enter ) {
Qenqueue( q, thread );
} // if
thread = tournament[j][node + 1];
if ( thread != FREE_NODE && thread != id && arrState[thread].enter ) {
Qenqueue( q, thread );
} // if
} // for
if ( FASTPATH( QnotEmpty( q ) ) ) {
thread = Qdequeue( q );
first = thread;
arrState[thread].wait = true;
} else {
first = FREE_LOCK;
} // if
#ifdef FAST
id = startpoint( cnt ); // different starting point each experiment
cnt = cycleUp( cnt, NoStartPoints );
#endif // FAST
} // for
Fai( sumOfThreadChecksums, randomThreadChecksum );
#ifdef FAST
id = oid;
#endif // FAST
entries[r][id] = entry;
Fai( Arrived, 1 );
while ( stop != 0 ) Pause();
Fai( Arrived, -1 );
} // for
return NULL;
} // Worker
void __attribute__((noinline)) ctor() {
q = Qctor( N );
arrState = malloc( N * sizeof(typeof(arrState[0])) );
for ( typeof(N) i = 0; i < N; i += 1 ) {
arrState[i].enter = arrState[i].wait = false;
} // for
toursize = Clog2( N ) + 1;
tournament = malloc( toursize * sizeof(typeof(tournament[0])) );
unsigned int levelSize = 1 << (toursize - 1); // 2^|log N|
for ( typeof(toursize) i = 0; i < toursize; i += 1 ) {
tournament[i] = malloc( levelSize * sizeof(typeof(tournament[0][0])) );
for ( typeof(levelSize) j = 0; j < levelSize; j += 1 ) {
tournament[i][j] = FREE_NODE;
} // for
levelSize /= 2;
} // for
first = FREE_LOCK;
exits = 0;
} // ctor
void __attribute__((noinline)) dtor() {
Qdtor( q );
for ( typeof(toursize) i = 0; i < toursize; i += 1 ) {
free( (void *)tournament[i] );
} // for
free( tournament );
free( (void *)arrState );
} // dtor
// Local Variables: //
// tab-width: 4 //
// compile-command: "gcc -Wall -Wextra -std=gnu11 -O3 -DNDEBUG -fno-reorder-functions -DPIN -DAlgorithm=RMRS Harness.c -lpthread -lm -D`hostname` -DCFMT -DCNT=0" //
// End: //