queue.c 16.6 KB
Newer Older
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
/*******************************************************************************
 * This file is part of GadgetSMP.
 * Coypright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 ******************************************************************************/

/* Config parameters. */
#include "../config.h"

/* Some standard headers. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <omp.h>
#include <sched.h>

/* Local headers. */
#include "cycle.h"
#include "lock.h"
#include "task.h"
#include "cell.h"
#include "queue.h"

/* Error macro. */
42
#define error(s) { fprintf( stderr , "%s:%s:%i: %s\n" , __FILE__ , __FUNCTION__ , __LINE__ , s ); abort(); }
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

/* Define the timer macros. */
#ifdef TIMER_VERBOSE
    #define TIMER
#endif
#ifdef TIMER
    #define TIMER_TIC ticks tic = getticks();
    #define TIMER_TOC(t) timer_toc( t , tic )
    #define TIMER_TIC2 ticks tic2 = getticks();
    #define TIMER_TOC2(t) timer_toc( t , tic2 )
    #ifndef INLINE
    # if __GNUC__ && !__GNUC_STDC_INLINE__
    #  define INLINE extern inline
    # else
    #  define INLINE inline
    # endif
    #endif
    INLINE ticks timer_toc ( int t , ticks tic ) {
        ticks d = (getticks() - tic);
        __sync_add_and_fetch( &queue_timer[t] , d );
        return d;
        }
#else
    #define TIMER_TIC
    #define TIMER_TOC(t)
#endif


/* Counter macros. */
#ifdef COUNTER
    #define COUNT(c) ( __sync_add_and_fetch( &queue_counter[ c ] , 1 ) )
#else
    #define COUNT(c)
#endif


/* The timers. */
ticks queue_timer[ queue_timer_count ];

/* The counters. */
int queue_counter[ queue_counter_count ];

        

Pedro Gonnet's avatar
Pedro Gonnet committed
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
/**
 * @brief Insert a used tasks into the given queue.
 *
 * @param q The #queue.
 * @param t The #task.
 */
 
void queue_insert ( struct queue *q , struct task *t ) {

    int k;

    /* Lock the queue. */
    if ( lock_lock( &q->lock ) != 0 )
        error( "Failed to get queue lock." );
        
    /* Bubble-up the tasks. */
    for ( k = q->count ; k > q->next ; k-- )
        q->tid[k] = q->tid[k-1];
    q->tid[ q->next ] = t - q->tasks;
    q->count += 1;
    q->next += 1;
    
    /* Unlock the queue. */
    if ( lock_unlock( &q->lock ) != 0 )
        error( "Failed to unlock queue." );
    }


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
/** 
 * @brief Initialize the given queue.
 *
 * @param q The #queue.
 * @param size The maximum size of the queue.
 * @param tasks List of tasks to which the queue indices refer to.
 */
 
void queue_init ( struct queue *q , int size , struct task *tasks ) {
    
    /* Allocate the task list. */
    q->size = size;
    if ( ( q->tid = (int *)malloc( sizeof(int) * size ) ) == NULL )
        error( "Failed to allocate queue tids." );
    q->tasks = tasks;
        
    /* Init counters. */
    q->count = 0;
    q->next = 0;
    
    /* Init the queue lock. */
    if ( lock_init( &q->lock ) != 0 )
        error( "Failed to init queue lock." );

    }


/**
 * @brief Get a task free of dependencies and conflicts.
 *
 * @param q The task #queue.
 * @param blocking Block until access to the queue is granted.
 * @param keep Remove the returned task from this queue.
 */
 
150
struct task *queue_gettask_old ( struct queue *q , int blocking , int keep ) {
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

    int k, tid = -1, qcount, *qtid = q->tid;
    lock_type *qlock = &q->lock;
    struct task *qtasks = q->tasks, *res = NULL;
    TIMER_TIC
    
    /* If there are no tasks, leave immediately. */
    if ( q->next >= q->count ) {
        TIMER_TOC(queue_timer_gettask);
        return NULL;
        }

    /* Main loop, while there are tasks... */
    while ( q->next < q->count ) {
    
        /* Grab the task lock. */
        // if ( blocking ) {
            if ( lock_lock( qlock ) != 0 )
                error( "Locking the task_lock failed.\n" );
        //     }
        // else {
        //     if ( lock_trylock( qlock ) != 0 )
        //         break;
        //     }
            
        /* Loop over the remaining task IDs. */
        qcount = q->count;
        for ( k = q->next ; k < qcount ; k++ ) {
        
            /* Put a finger on the task. */
            res = &qtasks[ qtid[k] ];
            
            /* Is this task blocked? */
            if ( res->wait )
                continue;
            
            /* Different criteria for different types. */
188
            if ( res->type == task_type_self || (res->type == task_type_sub && res->cj == NULL) ) {
189
190
191
                if ( res->ci->hold || cell_locktree( res->ci ) != 0 )
                    continue;
                }
192
            else if ( res->type == task_type_pair || (res->type == task_type_sub && res->cj != NULL) ) {
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
                if ( res->ci->hold || res->cj->hold || res->ci->wait || res->cj->wait )
                    continue;
                if ( cell_locktree( res->ci ) != 0 )
                    continue;
                if ( cell_locktree( res->cj ) != 0 ) {
                    cell_unlocktree( res->ci );
                    continue;
                    }
                }
            
            /* If we made it this far, we're safe. */
            break;
        
            } /* loop over the task IDs. */
            
        /* Did we get a task? */
        if ( k < qcount ) {
        
            /* Do we need to swap? */
            if ( k != q->next )
                COUNT(queue_counter_swap);
        
            /* get the task ID. */
            tid = qtid[k];
        
            /* Remove the task? */
            if ( keep ) {
            
                /* Bubble-up. */
                q->count = qcount - 1;
                for ( ; k < qcount - 1 ; k++ )
                    qtid[k] = qtid[k+1];
            
                }
                
            /* No, leave it in the queue. */
            else {
            
                TIMER_TIC2

                /* Bubble-down the task. */
                while ( k > q->next ) {
                    qtid[ k ] = qtid[ k-1 ];
                    k -= 1;
                    }
                qtid[ q->next ] = tid;
                
                /* up the counter. */
                q->next += 1;
                
                TIMER_TOC2(queue_timer_bubble);
            
                }
            
            }
    
        /* Release the task lock. */
        if ( lock_unlock( qlock ) != 0 )
            error( "Unlocking the task_lock failed.\n" );
            
        /* Leave? */
        if ( tid >= 0 ) {
            TIMER_TOC(queue_timer_gettask);
            return &qtasks[tid];
            }
Pedro Gonnet's avatar
Pedro Gonnet committed
258
259
260
261
262
263
264
265
266
267
268
269
        else if ( !blocking )
            break;
    
        } /* while there are tasks. */
        
    /* No beef. */
    TIMER_TOC(queue_timer_gettask);
    return NULL;

    }


270
struct task *queue_gettask ( struct queue *q , int rid , int blocking , int keep ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
271
272
273
274

    int k, tid = -1, qcount, *qtid = q->tid;
    lock_type *qlock = &q->lock;
    struct task *qtasks = q->tasks, *res = NULL;
275
    struct cell *ci_best = NULL, *cj_best = NULL;
Pedro Gonnet's avatar
Pedro Gonnet committed
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
    int ind_best, score_best = -1, score;
    TIMER_TIC
    
    /* If there are no tasks, leave immediately. */
    if ( q->next >= q->count ) {
        TIMER_TOC(queue_timer_gettask);
        return NULL;
        }

    /* Main loop, while there are tasks... */
    while ( q->next < q->count ) {
    
        /* Grab the task lock. */
        // if ( blocking ) {
            if ( lock_lock( qlock ) != 0 )
                error( "Locking the task_lock failed.\n" );
        //     }
        // else {
        //     if ( lock_trylock( qlock ) != 0 )
        //         break;
        //     }
            
        /* Loop over the remaining task IDs. */
        qcount = q->count; ind_best = -1;
        for ( k = q->next ; k < qcount ; k++ ) {
        
            /* Put a finger on the task. */
            res = &qtasks[ qtid[k] ];
            
            /* Is this task blocked? */
            if ( res->wait )
                continue;
                
            /* Get the score for this task. */
310
            if ( res->type == task_type_self || res->type == task_type_ghost || res->type == task_type_sort || ( res->type == task_type_sub && res->cj == NULL ) )
Pedro Gonnet's avatar
Pedro Gonnet committed
311
312
313
314
315
                score = ( res->ci->owner == rid );
            else
                score = ( res->ci->owner == rid ) + ( res->cj->owner == rid );
            if ( score <= score_best )
                continue;
316
317
                
            /* Try to lock ci. */
318
            if ( res->type == task_type_self || (res->type == task_type_sub && res->cj == NULL) ) {
319
                if ( res->ci != ci_best && res->ci != cj_best && cell_locktree( res->ci ) != 0 )
Pedro Gonnet's avatar
Pedro Gonnet committed
320
321
                    continue;
                }
322
            else if ( res->type == task_type_pair || (res->type == task_type_sub && res->cj != NULL) ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
323
324
                if ( res->ci->hold || res->cj->hold || res->ci->wait || res->cj->wait )
                    continue;
325
                if ( res->ci != ci_best && res->ci != cj_best && cell_locktree( res->ci ) != 0 )
Pedro Gonnet's avatar
Pedro Gonnet committed
326
                    continue;
327
328
329
                if ( res->cj != ci_best && res->cj != cj_best && cell_locktree( res->cj ) != 0 ) {
                    if ( res->ci != ci_best && res->ci != cj_best )
                        cell_unlocktree( res->ci );
Pedro Gonnet's avatar
Pedro Gonnet committed
330
331
332
                    continue;
                    }
                }
333
            
Pedro Gonnet's avatar
Pedro Gonnet committed
334
335
336
            /* If we owned a previous task, unlock it. */
            if ( ind_best >= 0 ) {
                res = &qtasks[ qtid[ ind_best ] ];
337
                if ( res->type == task_type_self || res->type == task_type_pair || res->type == task_type_sub )
338
339
                    if ( res->ci != ci_best && res->ci != cj_best )
                        cell_unlocktree( res->ci );
340
                if ( res->type == task_type_pair || (res->type == task_type_sub && res->cj != NULL) )
341
342
                    if ( res->cj != ci_best && res->cj != cj_best )
                        cell_unlocktree( res->cj );
Pedro Gonnet's avatar
Pedro Gonnet committed
343
344
345
346
                }
            
            /* If we made it this far, we're safe. */
            ind_best = k;
347
348
            ci_best = qtasks[ qtid[ k ] ].ci;
            cj_best = qtasks[ qtid[ k ] ].cj;
Pedro Gonnet's avatar
Pedro Gonnet committed
349
350
351
            score_best = score;
            
            /* Should we bother looking any farther? */
Pedro Gonnet's avatar
Pedro Gonnet committed
352
353
            if ( ( qtasks[ qtid[ ind_best ] ].cj == NULL && score_best == 1 ) ||
                score_best == 2 );
Pedro Gonnet's avatar
Pedro Gonnet committed
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
                break;
        
            } /* loop over the task IDs. */
            
        /* Did we get a task? */
        if ( ind_best >= 0 ) {
        
            /* Do we need to swap? */
            if ( ind_best != q->next )
                COUNT(queue_counter_swap);
        
            /* get the task ID. */
            tid = qtid[ ind_best ];
            
            /* Own the cells involved. */
            qtasks[ tid ].ci->owner = rid;
            if ( qtasks[ tid ].cj != NULL )
                qtasks[ tid ].cj->owner = rid;
        
            /* Remove the task? */
            if ( keep ) {
            
                /* Bubble-up. */
                q->count = qcount - 1;
                for ( k = ind_best ; k < qcount - 1 ; k++ )
                    qtid[k] = qtid[k+1];
            
                }
                
            /* No, leave it in the queue. */
            else {
            
                TIMER_TIC2

                /* Bubble-down the task. */
                for ( k = ind_best ; k > q->next ; k-- )
                    qtid[ k ] = qtid[ k-1 ];
                qtid[ q->next ] = tid;
                
                /* up the counter. */
                q->next += 1;
                
                TIMER_TOC2(queue_timer_bubble);
            
                }
            
            }
    
        /* Release the task lock. */
        if ( lock_unlock( qlock ) != 0 )
            error( "Unlocking the task_lock failed.\n" );
            
        /* Leave? */
        if ( tid >= 0 ) {
            TIMER_TOC(queue_timer_gettask);
            return &qtasks[tid];
            }
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
        else if ( !blocking )
            break;
    
        } /* while there are tasks. */
        
    /* No beef. */
    TIMER_TOC(queue_timer_gettask);
    return NULL;

    }


/**
 * @brief Sort the tasks IDs according to their weight and constraints.
 *
 * @param q The #queue.
 */
 
void queue_sort ( struct queue *q ) {

    struct {
        short int lo, hi;
        } qstack[20];
    int qpos, i, j, k, lo, hi, imin, temp;
    int pivot_weight, pivot_wait;
    int *weight, *wait;
    int *data = q->tid;
    struct task *t;
        
    /* Allocate and pre-compute each task's weight. */
    if ( ( weight = (int *)alloca( sizeof(int) * q->count ) ) == NULL ||
         ( wait = (int *)alloca( sizeof(int) * q->count ) ) == NULL )
        error( "Failed to allocate weight buffer." );
    for ( k = 0 ; k < q->count ; k++ ) {
        t = &q->tasks[ q->tid[k] ];
        switch ( t->type ) {
447
            case task_type_self:
448
449
450
                wait[k] = t->rank;
                weight[k] = 0; // t->ci->count * t->ci->count;
                break;
451
            case task_type_pair:
452
453
454
                wait[k] = t->rank;
                weight[k] = 0; // t->ci->count * t->cj->count;
                break;
455
            case task_type_sub:
456
457
458
                wait[k] = t->rank;
                weight[k] = 0; // (t->cj == NULL) ? t->ci->count * t->ci->count : t->ci->count * t->cj->count;
                break;
459
            case task_type_sort:
460
461
462
                wait[k] = t->rank;
                weight[k] = 0; // t->ci->count;
                break;
Pedro Gonnet's avatar
Pedro Gonnet committed
463
464
465
466
            case task_type_ghost:
                wait[k] = t->rank;
                weight[k] = 0; // t->ci->count;
                break;
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
            }
        }
        
    /* Sort tasks. */
    qstack[0].lo = 0; qstack[0].hi = q->count - 1; qpos = 0;
    while ( qpos >= 0 ) {
        lo = qstack[qpos].lo; hi = qstack[qpos].hi;
        qpos -= 1;
        if ( hi - lo < 15 ) {
            for ( i = lo ; i < hi ; i++ ) {
                imin = i;
                for ( j = i+1 ; j <= hi ; j++ )
                    if ( ( wait[ j ] < wait[ imin ] ) ||
                         ( wait[ j ] == wait[ imin ] && weight[ j ] > weight[ imin ] ) )
                if ( imin != i ) {
                    temp = data[imin]; data[imin] = data[i]; data[i] = temp;
                    temp = wait[imin]; wait[imin] = wait[i]; wait[i] = temp;
                    temp = weight[imin]; weight[imin] = weight[i]; weight[i] = temp;
                    }
                }
            }
        else {
            pivot_weight = weight[ ( lo + hi ) / 2 ];
            pivot_wait = wait[ ( lo + hi ) / 2 ];
            i = lo; j = hi;
            while ( i <= j ) {
                while ( ( wait[ i ] < pivot_wait ) ||
                        ( wait[ i ] == pivot_wait && weight[ i ] > pivot_weight ) )
                    i++;
                while ( ( wait[ j ] > pivot_wait ) ||
                        ( wait[ j ] == pivot_wait && weight[ j ] < pivot_weight ) )
                    j--;
                if ( i <= j ) {
                    if ( i < j ) {
                        temp = data[i]; data[i] = data[j]; data[j] = temp;
                        temp = wait[i]; wait[i] = wait[j]; wait[j] = temp;
                        temp = weight[i]; weight[i] = weight[j]; weight[j] = temp;
                        }
                    i += 1; j -= 1;
                    }
                }
            if ( j > ( lo + hi ) / 2 ) {
                if ( lo < j ) {
                    qpos += 1;
                    qstack[qpos].lo = lo;
                    qstack[qpos].hi = j;
                    }
                if ( i < hi ) {
                    qpos += 1;
                    qstack[qpos].lo = i;
                    qstack[qpos].hi = hi;
                    }
                }
            else {
                if ( i < hi ) {
                    qpos += 1;
                    qstack[qpos].lo = i;
                    qstack[qpos].hi = hi;
                    }
                if ( lo < j ) {
                    qpos += 1;
                    qstack[qpos].lo = lo;
                    qstack[qpos].hi = j;
                    }
                }
            }
        }
                
    }