queue.c 17.1 KB
Newer Older
1
/*******************************************************************************
2
 * This file is part of SWIFT.
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
 * 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"
40
41
#include "error.h"
#include "inline.h"
42
43
44

/* Define the timer macros. */
#ifdef TIMER_VERBOSE
45
46
47
    #ifndef TIMER
        #define TIMER
    #endif
48
49
50
51
52
53
#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 )
54
    INLINE static ticks timer_toc ( int t , ticks tic ) {
55
56
57
58
59
60
61
        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
62
63
    #define TIMER_TIC2
    #define TIMER_TOC2(t)
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#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
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
/**
 * @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." );
    }


111
112
113
114
115
116
117
118
119
120
/** 
 * @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 ) {
    
121
122
123
124
125
126
127
128
129
130
    /* 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. */
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    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.
 */
 
152
struct task *queue_gettask_old ( struct queue *q , int blocking , int keep ) {
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

    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. */
190
            if ( res->type == task_type_self || (res->type == task_type_sub && res->cj == NULL) ) {
191
192
193
                if ( res->ci->hold || cell_locktree( res->ci ) != 0 )
                    continue;
                }
194
            else if ( res->type == task_type_pair || (res->type == task_type_sub && res->cj != NULL) ) {
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
                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
260
261
262
263
264
265
266
267
268
269
270
271
        else if ( !blocking )
            break;
    
        } /* while there are tasks. */
        
    /* No beef. */
    TIMER_TOC(queue_timer_gettask);
    return NULL;

    }


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

274
    int k, tid = -1, qcount, *qtid = q->tid, hits;
Pedro Gonnet's avatar
Pedro Gonnet committed
275
276
    lock_type *qlock = &q->lock;
    struct task *qtasks = q->tasks, *res = NULL;
277
    struct cell *ci_best = NULL, *cj_best = NULL;
Pedro Gonnet's avatar
Pedro Gonnet committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
    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 )
293
                error( "Locking the qlock failed.\n" );
Pedro Gonnet's avatar
Pedro Gonnet committed
294
295
296
297
298
299
300
        //     }
        // else {
        //     if ( lock_trylock( qlock ) != 0 )
        //         break;
        //     }
            
        /* Loop over the remaining task IDs. */
301
        qcount = q->count; ind_best = -1; hits = 0;
302
        for ( k = q->next ; k < qcount && hits < queue_maxhits ; k++ ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
303
304
305
306
307
308
309
310
311
        
            /* Put a finger on the task. */
            res = &qtasks[ qtid[k] ];
            
            /* Is this task blocked? */
            if ( res->wait )
                continue;
                
            /* Get the score for this task. */
312
            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
313
314
315
316
317
                score = ( res->ci->owner == rid );
            else
                score = ( res->ci->owner == rid ) + ( res->cj->owner == rid );
            if ( score <= score_best )
                continue;
318
319
                
            /* Try to lock ci. */
320
            if ( res->type == task_type_self || (res->type == task_type_sub && res->cj == NULL) ) {
321
                if ( res->ci != ci_best && res->ci != cj_best && cell_locktree( res->ci ) != 0 )
Pedro Gonnet's avatar
Pedro Gonnet committed
322
323
                    continue;
                }
324
            else if ( res->type == task_type_pair || (res->type == task_type_sub && res->cj != NULL) ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
325
326
                if ( res->ci->hold || res->cj->hold || res->ci->wait || res->cj->wait )
                    continue;
327
                if ( res->ci != ci_best && res->ci != cj_best && cell_locktree( res->ci ) != 0 )
Pedro Gonnet's avatar
Pedro Gonnet committed
328
                    continue;
329
330
331
                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
332
333
334
                    continue;
                    }
                }
335
            
Pedro Gonnet's avatar
Pedro Gonnet committed
336
337
338
            /* If we owned a previous task, unlock it. */
            if ( ind_best >= 0 ) {
                res = &qtasks[ qtid[ ind_best ] ];
339
                if ( res->type == task_type_self || res->type == task_type_pair || res->type == task_type_sub )
340
341
                    if ( res->ci != ci_best && res->ci != cj_best )
                        cell_unlocktree( res->ci );
342
                if ( res->type == task_type_pair || (res->type == task_type_sub && res->cj != NULL) )
343
344
                    if ( res->cj != ci_best && res->cj != cj_best )
                        cell_unlocktree( res->cj );
Pedro Gonnet's avatar
Pedro Gonnet committed
345
346
347
348
                }
            
            /* If we made it this far, we're safe. */
            ind_best = k;
349
350
            ci_best = qtasks[ qtid[ k ] ].ci;
            cj_best = qtasks[ qtid[ k ] ].cj;
Pedro Gonnet's avatar
Pedro Gonnet committed
351
            score_best = score;
352
            hits += 1;
Pedro Gonnet's avatar
Pedro Gonnet committed
353
354
            
            /* Should we bother looking any farther? */
Pedro Gonnet's avatar
Pedro Gonnet committed
355
356
            if ( ( qtasks[ qtid[ ind_best ] ].cj == NULL && score_best == 1 ) ||
                score_best == 2 );
Pedro Gonnet's avatar
Pedro Gonnet committed
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
                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. */
380
                /* q->count = qcount - 1;
Pedro Gonnet's avatar
Pedro Gonnet committed
381
                for ( k = ind_best ; k < qcount - 1 ; k++ )
382
383
384
385
386
                    qtid[k] = qtid[k+1]; */
                    
                /* Swap with last task. */
                q->count = qcount - 1;
                qtid[ ind_best ] = qtid[ q->count ];
Pedro Gonnet's avatar
Pedro Gonnet committed
387
388
389
390
391
392
393
394
395
            
                }
                
            /* No, leave it in the queue. */
            else {
            
                TIMER_TIC2

                /* Bubble-down the task. */
396
                /* for ( k = ind_best ; k > q->next ; k-- )
Pedro Gonnet's avatar
Pedro Gonnet committed
397
                    qtid[ k ] = qtid[ k-1 ];
398
399
400
401
402
403
404
                qtid[ q->next ] = tid; */
                
                /* Swap with the first task. */
                if ( ind_best != q->next ) {
                    qtid[ ind_best ] = qtid[ q->next ];
                    qtid[ q->next ] = tid;
                    }
Pedro Gonnet's avatar
Pedro Gonnet committed
405
406
407
408
409
410
411
412
413
414
415
416
                
                /* up the counter. */
                q->next += 1;
                
                TIMER_TOC2(queue_timer_bubble);
            
                }
            
            }
    
        /* Release the task lock. */
        if ( lock_unlock( qlock ) != 0 )
417
            error( "Unlocking the qlock failed.\n" );
Pedro Gonnet's avatar
Pedro Gonnet committed
418
419
420
421
422
423
            
        /* Leave? */
        if ( tid >= 0 ) {
            TIMER_TOC(queue_timer_gettask);
            return &qtasks[tid];
            }
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
        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;
452
453
    
    printf( "queue_sort: sorting queue with %i tasks.\n" , q->count );
454
455
456
457
458
459
460
461
        
    /* 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 ) {
462
            case task_type_self:
463
464
465
                wait[k] = t->rank;
                weight[k] = 0; // t->ci->count * t->ci->count;
                break;
466
            case task_type_pair:
467
468
469
                wait[k] = t->rank;
                weight[k] = 0; // t->ci->count * t->cj->count;
                break;
470
            case task_type_sub:
471
472
473
                wait[k] = t->rank;
                weight[k] = 0; // (t->cj == NULL) ? t->ci->count * t->ci->count : t->ci->count * t->cj->count;
                break;
474
            case task_type_sort:
475
476
477
                wait[k] = t->rank;
                weight[k] = 0; // t->ci->count;
                break;
Pedro Gonnet's avatar
Pedro Gonnet committed
478
479
480
481
            case task_type_ghost:
                wait[k] = t->rank;
                weight[k] = 0; // t->ci->count;
                break;
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
547
548
549
550
551
552
            }
        }
        
    /* 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;
                    }
                }
            }
        }
                
    }