queue.c 6.36 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
 * 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>

/* Local headers. */
#include "cycle.h"
#include "lock.h"
#include "task.h"
Pedro Gonnet's avatar
Pedro Gonnet committed
32
#include "timers.h"
33
34
#include "cell.h"
#include "queue.h"
35
36
#include "error.h"
#include "inline.h"
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52



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


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

        

Pedro Gonnet's avatar
Pedro Gonnet committed
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
 * @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 ) {

    /* Lock the queue. */
    if ( lock_lock( &q->lock ) != 0 )
        error( "Failed to get queue lock." );
        
66
67
68
69
70
71
72
73
74
75
76
77
78
    /* Does the queue need to be grown? */
    if ( q->count == q->size ) {
        int *temp;
        q->size *= queue_sizegrow;
        if ( ( temp = (int *)malloc( sizeof(int) * q->size ) ) == NULL )
            error( "Failed to allocate new indices." );
        memcpy( temp , q->tid , sizeof(int) * q->count );
        free( q->tid );
        q->tid = temp;
        }
        
    /* Drop the task at the end of the queue. */
    q->tid[ q->count ] = ( t - q->tasks );
Pedro Gonnet's avatar
Pedro Gonnet committed
79
    q->count += 1;
80
81
    
    /* Shuffle up. */
Pedro Gonnet's avatar
Pedro Gonnet committed
82
    for ( int k = q->count - 1 ; k > 0 ; k = (k-1)/2 )
83
        if ( q->tasks[ q->tid[k] ].weight > q->tasks[ q->tid[(k-1)/2] ].weight ) {
84
            int temp = q->tid[k];
85
86
            q->tid[k] = q->tid[(k-1)/2];
            q->tid[(k-1)/2] = temp;
87
88
            }
        else
Pedro Gonnet's avatar
Pedro Gonnet committed
89
            break;
Pedro Gonnet's avatar
Pedro Gonnet committed
90
91
92
93
94
95
96
    
    /* Unlock the queue. */
    if ( lock_unlock( &q->lock ) != 0 )
        error( "Failed to unlock queue." );
    }


97
98
99
100
101
102
103
104
/** 
 * @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.
 */
 
105
void queue_init ( struct queue *q , struct task *tasks ) {
106
    
107
    /* Allocate the task list if needed. */
108
109
110
    q->size = queue_sizeinit;
    if ( ( q->tid = (int *)malloc( sizeof(int) * q->size ) ) == NULL )
        error( "Failed to allocate queue tids." );
111
112
        
    /* Set the tasks pointer. */
113
114
115
116
117
118
119
120
121
122
    q->tasks = tasks;
        
    /* Init counters. */
    q->count = 0;
    
    /* Init the queue lock. */
    if ( lock_init( &q->lock ) != 0 )
        error( "Failed to init queue lock." );

    }
123
124
    
    
125
126
127
128
129
130
131
132
/**
 * @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.
 */
 
133
struct task *queue_gettask ( struct queue *q , struct cell *super , int blocking ) {
134

Pedro Gonnet's avatar
Pedro Gonnet committed
135
    int k, temp, qcount, *qtid, gotcha;
136
    lock_type *qlock = &q->lock;
137
    struct task *qtasks, *res = NULL;
138
139
    
    /* If there are no tasks, leave immediately. */
Pedro Gonnet's avatar
Pedro Gonnet committed
140
    if ( q->count == 0 )
141
142
        return NULL;

143
144
145
146
147
148
149
150
151
    /* Grab the task lock. */
    if ( blocking ) {
        if ( lock_lock( qlock ) != 0 )
            error( "Locking the qlock failed.\n" );
        }
    else {
        if ( lock_trylock( qlock ) != 0 )
            return NULL;
        }
152

153
154
155
156
157
    /* Set some pointers we will use often. */
    qtid = q->tid;
    qtasks = q->tasks;
    qcount = q->count;
    gotcha = 0;
158

159
160
161
    /* Loop over the task IDs looking for tasks with the same super-cell. */
    if ( super != NULL ) {
        for ( k = 0 ; k < qcount && k < queue_maxsuper ; k++ ) {
162

163
164
            /* Put a finger on the task. */
            res = &qtasks[ qtid[k] ];
165

166
167
168
169
170
171
            /* Try to lock the task and exit if successful. */
            if ( ( res->ci->super == super || ( res->cj != NULL && res->cj->super == super ) ) &&
                 task_lock( res ) ) {
                gotcha = 1;
                break;
                }
Pedro Gonnet's avatar
Pedro Gonnet committed
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
            } /* loop over the task IDs. */
        }

    /* Loop over the task IDs again if nothing was found, take anything. */
    if ( !gotcha ) {
        for ( k = 0 ; k < qcount ; k++ ) {

            /* Put a finger on the task. */
            res = &qtasks[ qtid[k] ];

            /* Try to lock the task and exit if successful. */
            if ( task_lock( res ) )
                break;

            } /* loop over the task IDs. */
        }

    /* Did we get a task? */
    if ( k < qcount ) {

        /* Another one bites the dust. */
        qcount = q->count -= 1;

        /* Swap this task with the last task and re-heap. */
        if ( k < qcount ) {
            qtid[ k ] = qtid[ qcount ];
            while ( qtasks[ qtid[k] ].weight > qtasks[ qtid[(k-1)/2] ].weight ) {
                int temp = q->tid[k];
                q->tid[k] = q->tid[(k-1)/2];
                q->tid[(k-1)/2] = temp;
                k = (k-1)/2;
                }
            int i;
            while ( ( i = 2*k+1 ) < qcount ) {
                if ( i+1 < qcount && qtasks[ qtid[i+1] ].weight > qtasks[ qtid[i] ].weight )
                    i += 1;
                if ( qtasks[ qtid[i] ].weight > qtasks[ qtid[k] ].weight ) {
                    temp = qtid[i];
                    qtid[i] = qtid[k];
                    qtid[k] = temp;
                    k = i;
214
                    }
215
216
                else
                    break;
Pedro Gonnet's avatar
Pedro Gonnet committed
217
                }
Pedro Gonnet's avatar
Pedro Gonnet committed
218
            }
219
220
221
222
223
224
225
226

        }
    else
        res = NULL;

    /* Release the task lock. */
    if ( lock_unlock( qlock ) != 0 )
        error( "Unlocking the qlock failed.\n" );
Pedro Gonnet's avatar
Pedro Gonnet committed
227
            
Pedro Gonnet's avatar
Pedro Gonnet committed
228
    /* Take the money and run. */
229
    return res;
230
231
232
233

    }