queue.c 16.8 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

/* 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)
Pedro Gonnet's avatar
Pedro Gonnet committed
68
69
    #define TIMER_TIC2
    #define TIMER_TOC2(t)
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#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
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
/**
 * @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." );
    }


117
118
119
120
121
122
123
124
125
126
/** 
 * @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 ) {
    
127
128
129
130
131
132
133
134
135
136
    /* Allocate the task list if needed. */
    if ( q->tid == NULL || q->size < size ) {
        if ( q->tid != NULL )
            free( q->tid );
        q->size = size;
        if ( ( q->tid = (int *)malloc( sizeof(int) * size ) ) == NULL )
            error( "Failed to allocate queue tids." );
        }
        
    /* Set the tasks pointer. */
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    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.
 */
 
158
struct task *queue_gettask_old ( struct queue *q , int blocking , int keep ) {
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

    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. */
196
            if ( res->type == task_type_self || (res->type == task_type_sub && res->cj == NULL) ) {
197
198
199
                if ( res->ci->hold || cell_locktree( res->ci ) != 0 )
                    continue;
                }
200
            else if ( res->type == task_type_pair || (res->type == task_type_sub && res->cj != NULL) ) {
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
                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
266
267
268
269
270
271
272
273
274
275
276
277
        else if ( !blocking )
            break;
    
        } /* while there are tasks. */
        
    /* No beef. */
    TIMER_TOC(queue_timer_gettask);
    return NULL;

    }


278
struct task *queue_gettask ( struct queue *q , int rid , int blocking , int keep ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
279

280
    int k, tid = -1, qcount, *qtid = q->tid, hits;
Pedro Gonnet's avatar
Pedro Gonnet committed
281
282
    lock_type *qlock = &q->lock;
    struct task *qtasks = q->tasks, *res = NULL;
283
    struct cell *ci_best = NULL, *cj_best = NULL;
Pedro Gonnet's avatar
Pedro Gonnet committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
    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. */
307
        qcount = q->count; ind_best = -1; hits = 0;
308
        for ( k = q->next ; k < qcount && hits < queue_maxhits ; k++ ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
309
310
311
312
313
314
315
316
317
        
            /* Put a finger on the task. */
            res = &qtasks[ qtid[k] ];
            
            /* Is this task blocked? */
            if ( res->wait )
                continue;
                
            /* Get the score for this task. */
318
            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
319
320
321
322
323
                score = ( res->ci->owner == rid );
            else
                score = ( res->ci->owner == rid ) + ( res->cj->owner == rid );
            if ( score <= score_best )
                continue;
324
325
                
            /* Try to lock ci. */
326
            if ( res->type == task_type_self || (res->type == task_type_sub && res->cj == NULL) ) {
327
                if ( res->ci != ci_best && res->ci != cj_best && cell_locktree( res->ci ) != 0 )
Pedro Gonnet's avatar
Pedro Gonnet committed
328
329
                    continue;
                }
330
            else if ( res->type == task_type_pair || (res->type == task_type_sub && res->cj != NULL) ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
331
332
                if ( res->ci->hold || res->cj->hold || res->ci->wait || res->cj->wait )
                    continue;
333
                if ( res->ci != ci_best && res->ci != cj_best && cell_locktree( res->ci ) != 0 )
Pedro Gonnet's avatar
Pedro Gonnet committed
334
                    continue;
335
336
337
                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
338
339
340
                    continue;
                    }
                }
341
            
Pedro Gonnet's avatar
Pedro Gonnet committed
342
343
344
            /* If we owned a previous task, unlock it. */
            if ( ind_best >= 0 ) {
                res = &qtasks[ qtid[ ind_best ] ];
345
                if ( res->type == task_type_self || res->type == task_type_pair || res->type == task_type_sub )
346
347
                    if ( res->ci != ci_best && res->ci != cj_best )
                        cell_unlocktree( res->ci );
348
                if ( res->type == task_type_pair || (res->type == task_type_sub && res->cj != NULL) )
349
350
                    if ( res->cj != ci_best && res->cj != cj_best )
                        cell_unlocktree( res->cj );
Pedro Gonnet's avatar
Pedro Gonnet committed
351
352
353
354
                }
            
            /* If we made it this far, we're safe. */
            ind_best = k;
355
356
            ci_best = qtasks[ qtid[ k ] ].ci;
            cj_best = qtasks[ qtid[ k ] ].cj;
Pedro Gonnet's avatar
Pedro Gonnet committed
357
            score_best = score;
358
            hits += 1;
Pedro Gonnet's avatar
Pedro Gonnet committed
359
360
            
            /* Should we bother looking any farther? */
Pedro Gonnet's avatar
Pedro Gonnet committed
361
362
            if ( ( qtasks[ qtid[ ind_best ] ].cj == NULL && score_best == 1 ) ||
                score_best == 2 );
Pedro Gonnet's avatar
Pedro Gonnet committed
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
411
412
413
414
415
416
417
418
419
                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];
            }
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
447
448
449
450
451
452
453
454
455
        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 ) {
456
            case task_type_self:
457
458
459
                wait[k] = t->rank;
                weight[k] = 0; // t->ci->count * t->ci->count;
                break;
460
            case task_type_pair:
461
462
463
                wait[k] = t->rank;
                weight[k] = 0; // t->ci->count * t->cj->count;
                break;
464
            case task_type_sub:
465
466
467
                wait[k] = t->rank;
                weight[k] = 0; // (t->cj == NULL) ? t->ci->count * t->ci->count : t->ci->count * t->cj->count;
                break;
468
            case task_type_sort:
469
470
471
                wait[k] = t->rank;
                weight[k] = 0; // t->ci->count;
                break;
Pedro Gonnet's avatar
Pedro Gonnet committed
472
473
474
475
            case task_type_ghost:
                wait[k] = t->rank;
                weight[k] = 0; // t->ci->count;
                break;
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
538
539
540
541
542
543
544
545
546
            }
        }
        
    /* 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;
                    }
                }
            }
        }
                
    }