1 | /* |
---|
2 | * pthread.c - User side pthread related functions implementation. |
---|
3 | * |
---|
4 | * Author Alain Greiner (2016,2017) |
---|
5 | * |
---|
6 | * Copyright (c) UPMC Sorbonne Universites |
---|
7 | * |
---|
8 | * This file is part of ALMOS-MKH. |
---|
9 | * |
---|
10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
11 | * under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 2.0 of the License. |
---|
13 | * |
---|
14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | * General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #include <shared_syscalls.h> |
---|
25 | #include <hal_user.h> |
---|
26 | #include <nostdio.h> |
---|
27 | #include <stdio.h> |
---|
28 | #include <stdlib.h> |
---|
29 | #include <malloc.h> |
---|
30 | #include <pthread.h> |
---|
31 | |
---|
32 | #define PTHREAD_MUTEX_DEBUG 0 |
---|
33 | #define PTHREAD_BARRIER_DEBUG 0 |
---|
34 | |
---|
35 | #define reg_t int |
---|
36 | |
---|
37 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
38 | // Threads |
---|
39 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
40 | |
---|
41 | ///////////////////////////////////////////////// |
---|
42 | int pthread_create( pthread_t * trdid, |
---|
43 | const pthread_attr_t * attr, |
---|
44 | void * start_func, |
---|
45 | void * start_args ) |
---|
46 | { |
---|
47 | return hal_user_syscall( SYS_THREAD_CREATE, |
---|
48 | (reg_t)trdid, |
---|
49 | (reg_t)attr, |
---|
50 | (reg_t)start_func, |
---|
51 | (reg_t)start_args ); |
---|
52 | } |
---|
53 | |
---|
54 | ///////////////////////////////////// |
---|
55 | int pthread_join( pthread_t trdid, |
---|
56 | void ** exit_value ) |
---|
57 | { |
---|
58 | return hal_user_syscall( SYS_THREAD_JOIN, |
---|
59 | (reg_t)exit_value, 0, 0, 0 ); |
---|
60 | } |
---|
61 | |
---|
62 | /////////////////////////////////////// |
---|
63 | int pthread_detach( pthread_t trdid ) |
---|
64 | { |
---|
65 | return hal_user_syscall( SYS_THREAD_DETACH, |
---|
66 | (reg_t)trdid, 0, 0, 0 ); |
---|
67 | } |
---|
68 | |
---|
69 | ///////////////////////////////////// |
---|
70 | int pthread_exit( void * exit_value ) |
---|
71 | { |
---|
72 | return hal_user_syscall( SYS_THREAD_EXIT, |
---|
73 | (reg_t)exit_value, 0, 0, 0 ); |
---|
74 | } |
---|
75 | |
---|
76 | /////////////////// |
---|
77 | int pthread_yield() |
---|
78 | { |
---|
79 | return hal_user_syscall( SYS_THREAD_YIELD, 0, 0, 0, 0 ); |
---|
80 | } |
---|
81 | |
---|
82 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
83 | // Barriers |
---|
84 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
85 | |
---|
86 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
87 | // This recursive function initializes the SQT nodes |
---|
88 | // traversing the SQT from root to bottom |
---|
89 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
90 | static void sqt_barrier_build( pthread_barrier_t * barrier, |
---|
91 | unsigned int x, |
---|
92 | unsigned int y, |
---|
93 | unsigned int level, |
---|
94 | sqt_node_t * parent, |
---|
95 | unsigned int x_size, |
---|
96 | unsigned int y_size, |
---|
97 | unsigned int nthreads ) |
---|
98 | { |
---|
99 | // get target node address |
---|
100 | sqt_node_t * node = barrier->node[x][y][level]; |
---|
101 | |
---|
102 | if (level == 0 ) // terminal case |
---|
103 | { |
---|
104 | // initializes target node |
---|
105 | node->arity = nthreads; |
---|
106 | node->count = nthreads; |
---|
107 | node->sense = 0; |
---|
108 | node->level = 0; |
---|
109 | node->parent = parent; |
---|
110 | node->child[0] = NULL; |
---|
111 | node->child[1] = NULL; |
---|
112 | node->child[2] = NULL; |
---|
113 | node->child[3] = NULL; |
---|
114 | |
---|
115 | #if PTHREAD_BARRIER_DEBUG |
---|
116 | printf("\n[BARRIER] %s : sqt_node[%d][%d][%d] : arity = %d / parent = %x\n" |
---|
117 | "child0 = %x / child1 = %x / child2 = %x / child3 = %x\n", |
---|
118 | __FUNCTION__, x, y, level, node->arity, node->parent, |
---|
119 | node->child[0], node->child[1], node->child[2], node->child[3] ); |
---|
120 | #endif |
---|
121 | |
---|
122 | } |
---|
123 | else // non terminal case |
---|
124 | { |
---|
125 | unsigned int cx[4]; // x coordinate for children |
---|
126 | unsigned int cy[4]; // y coordinate for children |
---|
127 | unsigned int arity = 0; |
---|
128 | unsigned int i; |
---|
129 | |
---|
130 | // the child0 coordinates are equal to the parent coordinates |
---|
131 | // other children coordinates are incremented depending on the level value |
---|
132 | cx[0] = x; |
---|
133 | cy[0] = y; |
---|
134 | |
---|
135 | cx[1] = x; |
---|
136 | cy[1] = y + (1 << (level-1)); |
---|
137 | |
---|
138 | cx[2] = x + (1 << (level-1)); |
---|
139 | cy[2] = y; |
---|
140 | |
---|
141 | cx[3] = x + (1 << (level-1)); |
---|
142 | cy[3] = y + (1 << (level-1)); |
---|
143 | |
---|
144 | // initializes parent node taken into account the actual number of childs |
---|
145 | // child pointer is NULL if coordinates outside the mesh |
---|
146 | for ( i = 0 ; i < 4 ; i++ ) |
---|
147 | { |
---|
148 | if ( (cx[i] < x_size) && (cy[i] < y_size) ) |
---|
149 | { |
---|
150 | node->child[i] = barrier->node[cx[i]][cy[i]][level-1]; |
---|
151 | arity++; |
---|
152 | } |
---|
153 | else node->child[i] = NULL; |
---|
154 | } |
---|
155 | node->arity = arity; |
---|
156 | node->count = arity; |
---|
157 | node->sense = 0; |
---|
158 | node->level = level; |
---|
159 | node->parent = parent; |
---|
160 | |
---|
161 | #if PTHREAD_BARRIER_DEBUG |
---|
162 | printf("\n[BARRIER] %s : sqt_node[%d][%d][%d] : arity = %d / parent = %x\n" |
---|
163 | "child0 = %x / child1 = %x / child2 = %x / child3 = %x\n", |
---|
164 | __FUNCTION__, x, y, level, node->arity, node->parent, |
---|
165 | node->child[0], node->child[1], node->child[2], node->child[3] ); |
---|
166 | #endif |
---|
167 | |
---|
168 | // recursive calls for children nodes |
---|
169 | for ( i = 0 ; i < 4 ; i++ ) |
---|
170 | { |
---|
171 | if ( (cx[i] < x_size) && (cy[i] < y_size) ) |
---|
172 | sqt_barrier_build( barrier, |
---|
173 | cx[i], |
---|
174 | cy[i], |
---|
175 | level-1, |
---|
176 | node, |
---|
177 | x_size, |
---|
178 | y_size, |
---|
179 | nthreads ); |
---|
180 | } |
---|
181 | } |
---|
182 | } // end sqt_barrier_build() |
---|
183 | |
---|
184 | //////////////////////////////////////////////////////////////// |
---|
185 | int pthread_barrier_init( pthread_barrier_t * barrier, |
---|
186 | const pthread_barrierattr_t * attr, |
---|
187 | unsigned int count ) |
---|
188 | { |
---|
189 | unsigned int x_size; |
---|
190 | unsigned int y_size; |
---|
191 | unsigned int nthreads; |
---|
192 | |
---|
193 | if( attr != NULL ) |
---|
194 | { |
---|
195 | x_size = attr->x_size; |
---|
196 | y_size = attr->y_size; |
---|
197 | nthreads = attr->nthreads; |
---|
198 | } |
---|
199 | else |
---|
200 | { |
---|
201 | x_size = 1; |
---|
202 | y_size = 1; |
---|
203 | nthreads = count; |
---|
204 | } |
---|
205 | |
---|
206 | // check attributes |
---|
207 | assert( x_size <= QDT_XMAX ); |
---|
208 | assert( y_size <= QDT_YMAX ); |
---|
209 | assert( x_size * y_size * nthreads == count ); |
---|
210 | |
---|
211 | // compute SQT levels |
---|
212 | unsigned int levels; |
---|
213 | unsigned int z = (x_size > y_size) ? x_size : y_size; |
---|
214 | levels = (z < 2) ? 1 : (z < 3) ? 2 : (z < 5) ? 3 : (z < 9) ? 4 : 5; |
---|
215 | |
---|
216 | #if PTHREAD_BARRIER_DEBUG |
---|
217 | unsigned int side = (z < 2) ? 1 : (z < 3) ? 2 : (z < 5) ? 4 : (z < 9) ? 8 : 16; |
---|
218 | printf("\n[BARRIER] %s : x_size = %d / y_size = %d / levels = %d / side = %d\n", |
---|
219 | __FUNCTION__ , x_size , y_size , levels , side ); |
---|
220 | #endif |
---|
221 | |
---|
222 | // allocates memory for the SQT nodes and initializes SQT nodes pointers array |
---|
223 | // the actual number of SQT nodes in a cluster(x,y) depends on (x,y): |
---|
224 | // At least 1 node / at most 5 nodes |
---|
225 | unsigned int x; // x coordinate for one SQT node |
---|
226 | unsigned int y; // y coordinate for one SQT node |
---|
227 | unsigned int l; // level for one SQT node |
---|
228 | for ( x = 0 ; x < x_size ; x++ ) |
---|
229 | { |
---|
230 | for ( y = 0 ; y < y_size ; y++ ) |
---|
231 | { |
---|
232 | unsigned int cxy = (x<<QDT_YWIDTH) + y; |
---|
233 | |
---|
234 | for ( l = 0 ; l < levels ; l++ ) |
---|
235 | { |
---|
236 | if ( ( (l == 0) && ((x&0x00) == 0) && ((y&0x00) == 0) ) || |
---|
237 | ( (l == 1) && ((x&0x01) == 0) && ((y&0x01) == 0) ) || |
---|
238 | ( (l == 2) && ((x&0x03) == 0) && ((y&0x03) == 0) ) || |
---|
239 | ( (l == 3) && ((x&0x07) == 0) && ((y&0x07) == 0) ) || |
---|
240 | ( (l == 4) && ((x&0x0F) == 0) && ((y&0x0F) == 0) ) ) |
---|
241 | { |
---|
242 | sqt_node_t * node = remote_malloc( sizeof(sqt_node_t) , cxy ); |
---|
243 | |
---|
244 | if( node == NULL ) |
---|
245 | { |
---|
246 | printf("\n[ERROR] in %s : cannot allocate sqt_node in cluster %x\n", |
---|
247 | __FUNCTION__ , cxy ); |
---|
248 | return -1; |
---|
249 | } |
---|
250 | |
---|
251 | barrier->node[x][y][l] = node; |
---|
252 | |
---|
253 | #if PTHREAD_BARRIER_DEBUG |
---|
254 | printf("\n[BARRIER] %s : sqt_node[%d][%d][%d] : vaddr = %x\n", |
---|
255 | __FUNCTION__ , x , y , l , node ); |
---|
256 | #endif |
---|
257 | } |
---|
258 | } |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | // recursively initialize all SQT nodes from root to bottom |
---|
263 | sqt_barrier_build( barrier, |
---|
264 | 0, |
---|
265 | 0, |
---|
266 | levels-1, |
---|
267 | NULL, |
---|
268 | x_size, |
---|
269 | y_size, |
---|
270 | nthreads ); |
---|
271 | |
---|
272 | hal_user_fence(); |
---|
273 | |
---|
274 | return 0; |
---|
275 | |
---|
276 | } // end pthread_barrier_init |
---|
277 | |
---|
278 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
279 | // This recursive function decrements the distributed "count" variables, |
---|
280 | // traversing the SQT from bottom to root. |
---|
281 | // The last arrived thread reset the local node before returning. |
---|
282 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
283 | static void sqt_barrier_decrement( sqt_node_t * node ) |
---|
284 | { |
---|
285 | |
---|
286 | #if PTHREAD_BARRIER_DEBUG |
---|
287 | unsigned int cxy; |
---|
288 | unsigned int lid; |
---|
289 | get_core( &cxy , &lid ); |
---|
290 | printf("\n[BARRIER] %s : core[%x,%d] decrement SQT barrier node %x :\n" |
---|
291 | " level = %d / parent = %x / arity = %d / sense = %d / count = %d\n", |
---|
292 | __FUNCTION__ , cxy , lid , (unsigned int)node , |
---|
293 | node->level , node->parent, node->arity , node->sense , node->count ); |
---|
294 | #endif |
---|
295 | |
---|
296 | unsigned int expected; |
---|
297 | |
---|
298 | // compute expected sense value |
---|
299 | if ( node->sense == 0) expected = 1; |
---|
300 | else expected = 0; |
---|
301 | |
---|
302 | // atomically decrement count |
---|
303 | int count = hal_user_atomic_add( (int *)&node->count , -1 ); |
---|
304 | |
---|
305 | // last arrived thread makes the recursive call |
---|
306 | if ( count == 1 ) // last thread |
---|
307 | { |
---|
308 | // decrement the parent node if the current node is not the root |
---|
309 | if ( node->parent != NULL ) sqt_barrier_decrement( node->parent ); |
---|
310 | |
---|
311 | // reset the current node |
---|
312 | node->sense = expected; |
---|
313 | node->count = node->arity; |
---|
314 | |
---|
315 | #if PTHREAD_BARRIER_DEBUG |
---|
316 | printf("\n[BARRIER] %s : core[%x,%d] reset SQT barrier node %x :\n" |
---|
317 | " level = %d / arity = %d / sense = %d / count = %d\n", |
---|
318 | __FUNCTION__ , cxy , lid , (unsigned int)node , |
---|
319 | node->level , node->arity , node->sense , node->count ); |
---|
320 | #endif |
---|
321 | return; |
---|
322 | } |
---|
323 | else // not the last thread |
---|
324 | { |
---|
325 | // poll sense |
---|
326 | while( 1 ) |
---|
327 | { |
---|
328 | if( node->sense == expected ) break; |
---|
329 | } |
---|
330 | |
---|
331 | return; |
---|
332 | } |
---|
333 | } // end sqt_barrier_decrement() |
---|
334 | |
---|
335 | /////////////////////////////////////////////////////// |
---|
336 | int pthread_barrier_wait( pthread_barrier_t * barrier ) |
---|
337 | { |
---|
338 | // get calling core cluster |
---|
339 | unsigned int cxy; |
---|
340 | unsigned int lid; |
---|
341 | get_core( &cxy , &lid ); |
---|
342 | |
---|
343 | // get calling core coordinate |
---|
344 | unsigned int x = cxy >> QDT_YWIDTH; |
---|
345 | unsigned int y = cxy & QDT_YMASK; |
---|
346 | |
---|
347 | #if PTHREAD_BARRIER_DEBUG |
---|
348 | printf("\n[BARRIER] %s : enter for core[%x,%d] / barrier = %x / node = %x\n", |
---|
349 | __FUNCTION__ , cxy , lid , barrier, barrier->node[x][y][0] ); |
---|
350 | #endif |
---|
351 | |
---|
352 | // recursively decrement count from bottom to root |
---|
353 | sqt_barrier_decrement( barrier->node[x][y][0] ); |
---|
354 | |
---|
355 | hal_user_fence(); |
---|
356 | |
---|
357 | return 0; |
---|
358 | |
---|
359 | } // end pthread_barrier_wait() |
---|
360 | |
---|
361 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
362 | // Mutexes |
---|
363 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
364 | |
---|
365 | ////////////////////////////////////////////////////////// |
---|
366 | int pthread_mutex_init( pthread_mutex_t * mutex, |
---|
367 | const pthread_mutexattr_t * attr ) |
---|
368 | { |
---|
369 | if( attr != NULL ) |
---|
370 | { |
---|
371 | printf("\n[ERROR] in %s : <attr> argument not supported\n", __FUNCTION__); |
---|
372 | return -1; |
---|
373 | } |
---|
374 | |
---|
375 | mutex->current = 0; |
---|
376 | mutex->free = 0; |
---|
377 | |
---|
378 | #if PTHEAD_MUTEX_DEBUG |
---|
379 | unsigned int cxy; |
---|
380 | unsigned int lid; |
---|
381 | get_core( &cxy , &lid ); |
---|
382 | printf("\n[MUTEX DEBUG] %s : core[%x,%d] initializes mutex %x\n", |
---|
383 | __FUNCTION__, cxy, lid, mutex ); |
---|
384 | #endif |
---|
385 | |
---|
386 | return 0; |
---|
387 | } |
---|
388 | |
---|
389 | ///////////////////////////////////////////////// |
---|
390 | int pthread_mutex_lock( pthread_mutex_t * mutex ) |
---|
391 | { |
---|
392 | unsigned int ticket; |
---|
393 | |
---|
394 | // get next free ticket |
---|
395 | ticket = (unsigned int)hal_user_atomic_add( (int *)&mutex->free, 1 ); |
---|
396 | |
---|
397 | #if PTHREAD_MUTEX_DEBUG |
---|
398 | unsigned int cxy; |
---|
399 | unsigned int lid; |
---|
400 | get_core( &cxy , &lid ); |
---|
401 | printf("\n[MUTEX DEBUG] %s : core[%x,%d] get ticket %d\n", |
---|
402 | " / mutex = %x / current = %d / free = %d\n", |
---|
403 | __FUNCTION__, cxy, lid, ticket, mutex, mutex->current, mutex->free ); |
---|
404 | #endif |
---|
405 | |
---|
406 | // poll the current index |
---|
407 | while( 1 ) |
---|
408 | { |
---|
409 | if( mutex->current == ticket) break; |
---|
410 | } |
---|
411 | |
---|
412 | #if PTHREAD_MUTEX_DEBUG |
---|
413 | printf("\n[MUTEX DEBUG] %s : core[%x,%d] get mutex %x / current = %d / free = %d\n", |
---|
414 | __FUNCTION__, cxy, lid, mutex, mutex->current, mutex->free ); |
---|
415 | #endif |
---|
416 | |
---|
417 | return 0; |
---|
418 | } |
---|
419 | |
---|
420 | //////////////////////////////////////////////////// |
---|
421 | int pthread_mutex_trylock( pthread_mutex_t * mutex ) |
---|
422 | { |
---|
423 | unsigned int ticket; |
---|
424 | |
---|
425 | // get next free ticket |
---|
426 | ticket = (unsigned int)hal_user_atomic_add( (int *)&mutex->free, 1 ); |
---|
427 | |
---|
428 | #if PTHREAD_MUTEX_DEBUG |
---|
429 | unsigned int cxy; |
---|
430 | unsigned int lid; |
---|
431 | get_core( &cxy, &lid ); |
---|
432 | printf("\n[MUTEX DEBUG] %s : core[%x,%d] get ticket = %d" |
---|
433 | " / mutex = %x / current = %d / free = %d\n", |
---|
434 | __FUNCTION__, cxy, lid, ticket, mutex, mutex->current, mutex->free ); |
---|
435 | #endif |
---|
436 | |
---|
437 | // test ticket |
---|
438 | if( ticket == mutex->current ) return 0; // success |
---|
439 | else return -1; // failure |
---|
440 | } |
---|
441 | |
---|
442 | /////////////////////////////////////////////////// |
---|
443 | int pthread_mutex_unlock( pthread_mutex_t * mutex ) |
---|
444 | { |
---|
445 | hal_user_fence(); |
---|
446 | |
---|
447 | mutex->current = mutex->current + 1; |
---|
448 | |
---|
449 | #if PTHREAD_MUTEX_DEBUG |
---|
450 | unsigned int cxy; |
---|
451 | unsigned int lid; |
---|
452 | get_core( &cxy , &lid ); |
---|
453 | printf("\n[MUTEX_DEBUG] %s : core[%x,%d] releases mutex %x" |
---|
454 | " / current = %d / free = %d\n", |
---|
455 | __FUNCTION__, cxy, lid, mutex, mutex->current, mutex->free ); |
---|
456 | #endif |
---|
457 | |
---|
458 | return 0; |
---|
459 | } |
---|
460 | |
---|
461 | |
---|
462 | // Local Variables: |
---|
463 | // tab-width: 4 |
---|
464 | // c-basic-offset: 4 |
---|
465 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
466 | // indent-tabs-mode: nil |
---|
467 | // End: |
---|
468 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
469 | |
---|