queue.c 10.4 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
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
272
273
274
275
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*******************************************************************************
 * 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. */
#define error(s) { printf( "%s:%s:%i: %s\n" , __FILE__ , __FUNCTION__ , __LINE__ , s ); abort(); }

/* 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 ];

        

/** 
 * @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.
 */
 
struct task *queue_gettask ( struct queue *q , int blocking , int keep ) {

    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. */
            if ( res->type == tid_self || (res->type == tid_sub && res->cj == NULL) ) {
                if ( res->ci->hold || cell_locktree( res->ci ) != 0 )
                    continue;
                }
            else if ( res->type == tid_pair || (res->type == tid_sub && res->cj != NULL) ) {
                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];
            }
        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 ) {
            case tid_self:
                wait[k] = t->rank;
                weight[k] = 0; // t->ci->count * t->ci->count;
                break;
            case tid_pair:
                wait[k] = t->rank;
                weight[k] = 0; // t->ci->count * t->cj->count;
                break;
            case tid_sub:
                wait[k] = t->rank;
                weight[k] = 0; // (t->cj == NULL) ? t->ci->count * t->ci->count : t->ci->count * t->cj->count;
                break;
            case tid_sort:
                wait[k] = t->rank;
                weight[k] = 0; // t->ci->count;
                break;
            }
        }
        
    /* 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;
                    }
                }
            }
        }
                
    }