-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdllcb_funcs.h
274 lines (209 loc) · 6.48 KB
/
dllcb_funcs.h
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
/**
* @file dllcb_funcs.h
* @brief Functions for live communication buffers
* @author Dominique LaSalle <[email protected]>
* Copyright (c) 2013-2015, Dominique LaSalle
* @version 1
* @date 2013-10-05
*/
/* prefixing ugliness */
#define DLLCB_PRE2(prefix,suffix) prefix ## _ ## suffix
#define DLLCB_PRE1(prefix,suffix) DLLCB_PRE2(prefix,suffix)
#define DLLCB_PUB(name) DLLCB_PRE1(DLLCB_PREFIX,name)
#define DLLCB_PRI(name) DLLCB_PRE1(_,DLLCB_PRE1(DLLCB_PREFIX,name))
#define DLLCB_RPRI(name) DLLCB_PRE1(r__,DLLCB_PRE1(DLLCB_PREFIX,name))
/******************************************************************************
* PUBLIC FUNCTIONS ************************************************************
******************************************************************************/
#ifndef DLLCB_VISIBILITY
#define DLLCB_DEFVIS
#define DLLCB_VISIBILITY
#endif
DLLCB_VISIBILITY DLLCB_PUB(combuffer_t) * DLLCB_PUB(combuffer_create)(
size_t const maxmsgs,
dlthread_comm_t comm)
{
size_t i;
DLLCB_PUB(combuffer_t) * com;
size_t const nthreads = dlthread_get_nthreads(comm);
size_t const myid = dlthread_get_id(comm);
com = dlthread_get_shmem(sizeof(DLLCB_PUB(combuffer_t)),comm);
if (myid == 0) {
com->comm = comm;
com->infos = malloc(sizeof(DLLCB_PRI(info_t))*nthreads);
com->lists = malloc(sizeof(DLLCB_PRI(list_t)*)*nthreads);
}
dlthread_barrier(comm);
com->infos[myid].active = 0;
com->infos[myid].finished = 0;
com->infos[myid].maxmsgs = maxmsgs;
com->infos[myid].lastmsg = 0;
com->infos[myid].nodes = malloc(sizeof(DLLCB_PRI(node_t))*maxmsgs);
com->lists[myid] = malloc(sizeof(DLLCB_PRI(list_t))*nthreads);
/* init lists */
for (i=0;i<nthreads;++i) {
com->lists[myid][i].start = NULL;
com->lists[myid][i].laststart = NULL;
com->lists[myid][i].end = NULL;
}
dlthread_barrier(comm);
return com;
}
DLLCB_VISIBILITY void DLLCB_PUB(combuffer_add)(
size_t const dst,
DLLCB_TYPE_T const val,
DLLCB_PUB(combuffer_t) * const com)
{
DLLCB_PRI(node_t) * msg, * last;
size_t const myid = dlthread_get_id(com->comm);
DLLCB_PRI(info_t) * const info = com->infos+myid;
DL_ASSERT(info->lastmsg < info->maxmsgs,"Overflowed combuffer");
msg = info->nodes + (info->lastmsg++);
msg->next = NULL;
msg->val = val;
if (com->lists[dst][myid].end == NULL) {
com->lists[dst][myid].end = msg;
com->lists[dst][myid].start = msg;
} else {
last = com->lists[dst][myid].end;
DL_ASSERT(last != NULL,"Last element in list is null");
last->next = msg;
com->lists[dst][myid].end = msg;
}
/* mark this thread as active and unfinished */
com->infos[dst].finished = 0;
com->infos[dst].active = 1;
}
DLLCB_VISIBILITY int DLLCB_PUB(combuffer_next)(
DLLCB_TYPE_T * const r_next,
DLLCB_PUB(combuffer_t) * const com)
{
size_t i;
DLLCB_PRI(node_t) * msg;
size_t const myid = dlthread_get_id(com->comm);
size_t const nthreads = dlthread_get_nthreads(com->comm);
DLLCB_PRI(info_t) * const info = com->infos+myid;
if (1 || info->active) {
/* initial mark me as inactive */
info->active = 0;
for (i=0;i<nthreads;++i) {
if ((msg = com->lists[myid][i].start) != NULL || \
(com->lists[myid][i].laststart != NULL && \
(msg = com->lists[myid][i].laststart->next) != NULL)) {
/* found an msg, mark me as actie */
info->active = 1;
if (r_next) {
com->lists[myid][i].laststart = msg;
/* if they passed in null, they just want to check */
com->lists[myid][i].start = msg->next;
*r_next = msg->val;
} else {
/* fix anything broken */
com->lists[myid][i].start = msg;
}
return 1;
}
}
}
return 0;
}
DLLCB_VISIBILITY void DLLCB_PUB(combuffer_clear)(
DLLCB_PUB(combuffer_t) * const com)
{
size_t i;
size_t const myid = dlthread_get_id(com->comm);
size_t const nthreads = dlthread_get_nthreads(com->comm);
DLLCB_PRI(info_t) * const info = com->infos+myid;
dlthread_barrier(com->comm);
info->lastmsg = 0;
info->finished = 0;
info->active = 0;
for (i=0;i<nthreads;++i) {
com->lists[myid][i].start = NULL;
com->lists[myid][i].laststart = NULL;
com->lists[myid][i].end = NULL;
}
dlthread_barrier(com->comm);
}
DLLCB_VISIBILITY int DLLCB_PUB(combuffer_finish)(
DLLCB_PUB(combuffer_t) * const com)
{
size_t t;
size_t const myid = dlthread_get_id(com->comm);
size_t const nthreads = dlthread_get_nthreads(com->comm);
DLLCB_PRI(info_t) * const info = com->infos+myid;
/* check to make sure I really don't have anymore work */
if (DLLCB_PUB(combuffer_next)(NULL,com)) {
/* I'm not finished */
info->finished = 0;
return 0;
} else if (info->finished == 0) {
info->finished = 1;
}
/* I'm finished, wait for others */
for (t=(myid+1)%nthreads;t!=myid;t=((t+1)%nthreads)) {
while (com->infos[t].finished == 0) {
/* found someone who's not finished */
if (DLLCB_PUB(combuffer_next)(NULL,com)) {
/* I have work I can do */
info->finished = 0;
return 0;
} else if (info->finished == 0) {
/* if someone invalidated me -- re-validate myself */
info->finished = 1;
}
_mm_pause(); /* don't max out the cpu */
}
}
/* do a final check to make sure I didn't overwrite myself */
if (DLLCB_PUB(combuffer_next)(NULL,com)) {
info->finished = 0;
return 0;
}
/* everbody is finished -- move to second stage */
info->finished = 2;
if (DLLCB_PUB(combuffer_next)(NULL,com)) {
/* someone gave me work */
info->finished = 0;
return 0;
}
/* I'm finished, wait for others */
t = myid;
do {
t=(t+1)%nthreads;
while (com->infos[t].finished != 2) {
if (DLLCB_PUB(combuffer_next)(NULL,com)) {
info->finished = 0;
return 0;
}
_mm_pause(); /* don't max out the cpu */
}
} while (t != myid);
return 1;
}
DLLCB_VISIBILITY void DLLCB_PUB(combuffer_free)(
DLLCB_PUB(combuffer_t) * com)
{
dlthread_comm_t comm;
size_t const myid = dlthread_get_id(com->comm);
comm = com->comm;
dlthread_barrier(comm);
dl_free(com->lists[myid]);
dl_free(com->infos[myid].nodes);
dlthread_barrier(com->comm);
if (myid == 0) {
dl_free(com->lists);
dl_free(com->infos);
}
dlthread_free_shmem(com,comm);
}
#ifdef DLLCB_DEFVIS
#undef DLLCB_DEFVIS
#undef DLLCB_VISIBILITY
#endif
#undef DLLCB_PRE
#undef DLLCB_PRE
#undef DLLCB_PUB
#undef DLLCB_PRI
#undef DLLCB_RPRI
#undef DLLCB_BUFFER_FUNC