space.c 67.4 KB
Newer Older
Pedro Gonnet's avatar
Pedro Gonnet committed
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
/*******************************************************************************
 * 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 <float.h>
#include <limits.h>
#include <math.h>

/* Local headers. */
#include "cycle.h"
#include "lock.h"
35
36
37
#include "task.h"
#include "part.h"
#include "cell.h"
Pedro Gonnet's avatar
Pedro Gonnet committed
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
#include "space.h"
#include "runner.h"

/* Error macro. */
#define error(s) { printf( "%s:%s:%i: %s\n" , __FILE__ , __FUNCTION__ , __LINE__ , s ); abort(); }

/* Split size. */
int space_splitsize = space_splitsize_default;

/* Map shift vector to sortlist. */
const int sortlistID[27] = {
    /* ( -1 , -1 , -1 ) */   0 ,
    /* ( -1 , -1 ,  0 ) */   1 , 
    /* ( -1 , -1 ,  1 ) */   2 ,
    /* ( -1 ,  0 , -1 ) */   3 ,
    /* ( -1 ,  0 ,  0 ) */   4 , 
    /* ( -1 ,  0 ,  1 ) */   5 ,
    /* ( -1 ,  1 , -1 ) */   6 ,
    /* ( -1 ,  1 ,  0 ) */   7 , 
    /* ( -1 ,  1 ,  1 ) */   8 ,
    /* (  0 , -1 , -1 ) */   9 ,
    /* (  0 , -1 ,  0 ) */   10 , 
    /* (  0 , -1 ,  1 ) */   11 ,
    /* (  0 ,  0 , -1 ) */   12 ,
    /* (  0 ,  0 ,  0 ) */   0 , 
    /* (  0 ,  0 ,  1 ) */   12 ,
    /* (  0 ,  1 , -1 ) */   11 ,
    /* (  0 ,  1 ,  0 ) */   10 , 
    /* (  0 ,  1 ,  1 ) */   9 ,
    /* (  1 , -1 , -1 ) */   8 ,
    /* (  1 , -1 ,  0 ) */   7 , 
    /* (  1 , -1 ,  1 ) */   6 ,
    /* (  1 ,  0 , -1 ) */   5 ,
    /* (  1 ,  0 ,  0 ) */   4 , 
    /* (  1 ,  0 ,  1 ) */   3 ,
    /* (  1 ,  1 , -1 ) */   2 ,
    /* (  1 ,  1 ,  0 ) */   1 , 
    /* (  1 ,  1 ,  1 ) */   0 
    };
    
    
/**
80
 * @brief Mapping function to free the sorted indices buffers.
Pedro Gonnet's avatar
Pedro Gonnet committed
81
82
83
84
85
86
87
88
89
90
91
92
 */

void space_map_clearsort ( struct cell *c , void *data ) {

    if ( c->sort != NULL ) {
        free( c->sort );
        c->sort = NULL;
        }

    }


93
94
/**
 * @brief Mapping function to append a ghost task to each cell.
Pedro Gonnet's avatar
Pedro Gonnet committed
95
96
97
98
 *
 * Looks for the super cell, e.g. the highest-level cell above each
 * cell for which a pair is defined. All ghosts below this cell will
 * depend on the ghost of their parents (sounds spooky, but it isn't).
99
100
101
102
103
 */

void space_map_mkghosts ( struct cell *c , void *data ) {

    struct space *s = (struct space *)data;
Pedro Gonnet's avatar
Pedro Gonnet committed
104
    struct cell *finger;
105

Pedro Gonnet's avatar
Pedro Gonnet committed
106
107
108
109
110
111
112
    /* Find the super cell, i.e. the highest cell hierarchically above
       this one to still have at least one task associated with it. */
    c->super = c;
    for ( finger = c->parent ; finger != NULL ; finger = finger->parent )
        if ( finger->nr_tasks > 0 )
            c->super = finger;
            
Pedro Gonnet's avatar
Pedro Gonnet committed
113
    /* Make the ghost task */
Pedro Gonnet's avatar
Pedro Gonnet committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    if ( c->super != c || c->nr_tasks > 0 )
        c->ghost = space_addtask( s , task_type_ghost , task_subtype_none , 0 , 0 , c , NULL , NULL , 0 , NULL , 0 );

    /* If we are not the super cell ourselves, make our ghost depend
       on our parent cell. */
    if ( c->super != c )
        task_addunlock( c->parent->ghost , c->ghost );
    
    }


/**
 * @brief Mapping function to clear the number of tasks in each cell.
 */

void space_map_clearnrtasks ( struct cell *c , void *data ) {

    c->nr_tasks = 0;
132
    c->nr_density = 0;
133
134
135
136

    }


Pedro Gonnet's avatar
Pedro Gonnet committed
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
/**
 * @brief Get a task free of dependencies and conflicts.
 *
 * @param s The #space.
 */
 
struct task *space_gettask ( struct space *s ) {

    int k, tid = -1;
    struct task *res = NULL;
    struct cell *c;

    /* Main loop, while there are tasks... */
    while ( s->next_task < s->nr_tasks ) {
    
        /* Grab the task lock. */
        if ( lock_lock( &s->task_lock ) != 0 )
            error( "Locking the task_lock failed.\n" );
            
        /* Loop over the remaining task IDs. */
        for ( k = s->next_task ; k < s->nr_tasks ; k++ ) {
        
            /* Put a finger on the task. */
            res = &s->tasks[ s->tasks_ind[k] ];
            
            /* Is this task blocked? */
            if ( res->wait )
                continue;
            
            /* Different criteria for different types. */
            switch ( res->type ) {
168
                case task_type_self:
Pedro Gonnet's avatar
Pedro Gonnet committed
169
170
171
                    if ( res->ci->lock || res->ci->wait > 0 )
                        continue;
                    break;
172
                case task_type_pair:
Pedro Gonnet's avatar
Pedro Gonnet committed
173
174
175
                    if ( res->ci->lock || res->cj->lock || res->ci->wait || res->cj->wait )
                        continue;
                    break;
176
                case task_type_sort:
Pedro Gonnet's avatar
Pedro Gonnet committed
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
                    if ( res->ci->lock )
                        continue;
                    break;
                }
            
            /* If we made it this far, we're safe. */
            break;
        
            } /* loop over the task IDs. */
            
        /* Did we get a task? */
        if ( k < s->nr_tasks ) {
        
            // /* Swap to front. */
            // tid = s->tasks_ind[k];
            // s->tasks_ind[k] = s->tasks_ind[ s->next_task ];
            // s->tasks_ind[ s->next_task ] = tid;
            
            /* Bubble-down the task. */
            tid = s->tasks_ind[k];
            while ( k > s->next_task ) {
                s->tasks_ind[ k ] = s->tasks_ind[ k-1 ];
                k -= 1;
                }
            s->tasks_ind[ s->next_task ] = tid;
            
            /* Lock the cells, if needed. */
204
            if ( s->tasks[tid].type != task_type_sort ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
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
                for ( c = res->ci ; c != NULL ; c = c->parent )
                    __sync_fetch_and_add( &c->lock , 1 );
                for ( c = res->cj ; c != NULL ; c = c->parent )
                    __sync_fetch_and_add( &c->lock , 1 );
                }
            
            /* up the counter. */
            s->next_task += 1;
        
            }
    
        /* Release the task lock. */
        if ( lock_unlock( &s->task_lock ) != 0 )
            error( "Locking the task_lock failed.\n" );
            
        /* Leave? */
        if ( tid >= 0 )
            return &s->tasks[tid];
    
        } /* while there are tasks. */
        
    /* No beef. */
    return NULL;

    }


/**
 * @brief Map a function to all particles in a aspace.
 *
 * @param s The #space we are working in.
 */
 
void space_map_parts ( struct space *s , void (*fun)( struct part *p , struct cell *c , void *data ) , void *data ) {

    int i;

    void rec_map ( struct cell *c ) {
    
        int k;
        
        /* No progeny? */
        if ( !c->split )
            for ( k = 0 ; k < c->count ; k++ )
                fun( &c->parts[k] , c , data );
                
        /* Otherwise, recurse. */
        else
            for ( k = 0 ; k < 8 ; k++ )
                if ( c->progeny[k] != NULL )
                    rec_map( c->progeny[k] );
                
        }
        
    /* Call the recursive function on all higher-level cells. */
    for ( i = 0 ; i < s->nr_cells ; i++ )
        rec_map( &s->cells[i] );

    }


/**
 * @brief Map a function to all particles in a aspace.
 *
 * @param s The #space we are working in.
270
 * @param full Map to all cells, including cells with sub-cells.
Pedro Gonnet's avatar
Pedro Gonnet committed
271
272
 */
 
273
void space_map_cells ( struct space *s , int full , void (*fun)( struct cell *c , void *data ) , void *data ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
274
275
276
277
278
279
280
281

    int i;

    void rec_map ( struct cell *c ) {
    
        int k;
        
        /* No progeny? */
282
        if ( full || !c->split )
Pedro Gonnet's avatar
Pedro Gonnet committed
283
284
            fun( c , data );
                
285
286
        /* Recurse. */
        if ( c->split )
Pedro Gonnet's avatar
Pedro Gonnet committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
            for ( k = 0 ; k < 8 ; k++ )
                if ( c->progeny[k] != NULL )
                    rec_map( c->progeny[k] );
                
        }
        
    /* Call the recursive function on all higher-level cells. */
    for ( i = 0 ; i < s->nr_cells ; i++ )
        rec_map( &s->cells[i] );

    }


/**
 * @brief Add a #task to the #space.
 *
 * @param s The #space we are working in.
 */
 
306
struct task *space_addtask ( struct space *s , int type , int subtype , int flags , int wait , struct cell *ci , struct cell *cj , struct task *unlock_tasks[] , int nr_unlock_tasks , struct cell *unlock_cells[] , int nr_unlock_cells ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
307
308
309
310
311

    struct task *t = &s->tasks[ s->nr_tasks ];
    
    /* Copy the data. */
    t->type = type;
312
    t->subtype = subtype;
Pedro Gonnet's avatar
Pedro Gonnet committed
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
353
    t->flags = flags;
    t->wait = wait;
    t->ci = ci;
    t->cj = cj;
    if ( unlock_tasks != NULL )
        memcpy( t->unlock_tasks , unlock_tasks , sizeof(struct task *) * nr_unlock_tasks );
    t->nr_unlock_tasks = nr_unlock_tasks;
    if ( unlock_cells != NULL )
        memcpy( t->unlock_cells , unlock_cells , sizeof(struct task *) * nr_unlock_cells );
    t->nr_unlock_cells = nr_unlock_cells;
    
    /* Increase the task counter. */
    s->nr_tasks += 1;
    
    /* Return a pointer to the new task. */
    return t;

    }



/**
 * @brief Split tasks that may be too large.
 *
 * @param s The #space we are working in.
 */
 
void space_splittasks ( struct space *s ) {

    int k, sid, tid;
    struct cell *ci, *cj;
    double hi, hj, shift[3];
    struct task *t;

    /* Loop through the tasks... */
    for ( tid = 0 ; tid < s->nr_tasks ; tid++ ) {
    
        /* Get a pointer on the task. */
        t = &s->tasks[tid];
    
        /* If this task isn't a pair, i'm not interested. */
354
        if ( t->type != task_type_pair )
Pedro Gonnet's avatar
Pedro Gonnet committed
355
356
357
358
359
360
361
362
363
            continue;
            
        /* Get a handle on the cells involved. */
        ci = t->ci;
        cj = t->cj;
        hi = fmax( ci->h[0] , fmax( ci->h[1] , ci->h[2] ) );
        hj = fmax( cj->h[0] , fmax( cj->h[1] , cj->h[2] ) );
            
        /* Should this task be split-up? */
Pedro Gonnet's avatar
Pedro Gonnet committed
364
        if ( ci->split && cj->split && ci->h_max < hi/2 && cj->h_max < hj/2 ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
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
        
            /* Get the relative distance between the pairs, wrapping. */
            for ( k = 0 ; k < 3 ; k++ ) {
                if ( cj->loc[k] - ci->loc[k] < -s->dim[k]/2 )
                    shift[k] = s->dim[k];
                else if ( cj->loc[k] - ci->loc[k] > s->dim[k]/2 )
                    shift[k] = -s->dim[k];
                else
                    shift[k] = 0.0;
                }

            /* Get the sorting index. */
            for ( sid = 0 , k = 0 ; k < 3 ; k++ )
                sid = 3*sid + ( (cj->loc[k] - ci->loc[k] + shift[k] < 0) ? 0 : (cj->loc[k] - ci->loc[k] + shift[k] > 0) ? 2 : 1 );

            /* Flip? */
            if ( sid < 13 ) {
                cj = t->ci;
                ci = t->cj;
                t->ci = ci; t->cj = cj;
                }
            else
                sid = 26 - sid;
                
            /* Remove the dependency of this task on the sorts of ci and cj. */
            task_rmunlock( ci->sorts[sid] , t );
            task_rmunlock( cj->sorts[sid] , t );
            ci->nr_pairs -= 1;
            cj->nr_pairs -= 1;
            t->nr_unlock_cells = 0;
                
            /* For each different sorting type... */
            switch ( sid ) {
            
                case 0: /* (  1 ,  1 ,  1 ) */
                    t->ci = ci->progeny[7]; t->cj = cj->progeny[0];
                    task_addunlock( ci->progeny[7]->sorts[0] , t ); task_addunlock( cj->progeny[0]->sorts[0] , t );
                    ci->progeny[7]->nr_pairs += 1;
                    cj->progeny[0]->nr_pairs += 1;
                    break;
                    
                case 1: /* (  1 ,  1 ,  0 ) */
                    if ( space_dosub &&
                         !ci->progeny[6]->split && !ci->progeny[7]->split &&
                         !cj->progeny[0]->split && !cj->progeny[1]->split ) {
410
                        t->type = task_type_sub; t->flags = 1;
Pedro Gonnet's avatar
Pedro Gonnet committed
411
412
413
414
415
416
417
418
                        task_addunlock( ci->progeny[6]->sorts[1] , t ); task_addunlock( cj->progeny[0]->sorts[1] , t );
                        task_addunlock( ci->progeny[7]->sorts[1] , t ); task_addunlock( cj->progeny[1]->sorts[1] , t );
                        task_addunlock( ci->progeny[6]->sorts[0] , t ); task_addunlock( cj->progeny[1]->sorts[0] , t );
                        task_addunlock( ci->progeny[7]->sorts[2] , t ); task_addunlock( cj->progeny[0]->sorts[2] , t );
                        }
                    else {
                        t->ci = ci->progeny[6]; t->cj = cj->progeny[0];
                        task_addunlock( ci->progeny[6]->sorts[1] , t ); task_addunlock( cj->progeny[0]->sorts[1] , t );
419
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
420
                        task_addunlock( ci->progeny[7]->sorts[1] , t ); task_addunlock( cj->progeny[1]->sorts[1] , t );
421
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[6] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
422
                        task_addunlock( ci->progeny[6]->sorts[0] , t ); task_addunlock( cj->progeny[1]->sorts[0] , t );
423
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
                        task_addunlock( ci->progeny[7]->sorts[2] , t ); task_addunlock( cj->progeny[0]->sorts[2] , t );
                        }
                    ci->progeny[6]->nr_pairs += 2;
                    ci->progeny[7]->nr_pairs += 2;
                    cj->progeny[0]->nr_pairs += 2;
                    cj->progeny[1]->nr_pairs += 2;
                    break;
                    
                case 2: /* (  1 ,  1 , -1 ) */
                    t->ci = ci->progeny[6]; t->cj = cj->progeny[1];
                    task_addunlock( ci->progeny[6]->sorts[2] , t ); task_addunlock( cj->progeny[1]->sorts[2] , t );
                    ci->progeny[6]->nr_pairs += 1;
                    cj->progeny[1]->nr_pairs += 1;
                    break;
                    
                case 3: /* (  1 ,  0 ,  1 ) */
                    if ( space_dosub &&
                         !ci->progeny[5]->split && !ci->progeny[7]->split &&
                         !cj->progeny[0]->split && !cj->progeny[2]->split ) {
443
                        t->type = task_type_sub; t->flags = 3;
Pedro Gonnet's avatar
Pedro Gonnet committed
444
445
446
447
448
449
450
451
                        task_addunlock( ci->progeny[5]->sorts[3] , t ); task_addunlock( cj->progeny[0]->sorts[3] , t );
                        task_addunlock( ci->progeny[7]->sorts[3] , t ); task_addunlock( cj->progeny[2]->sorts[3] , t );
                        task_addunlock( ci->progeny[5]->sorts[0] , t ); task_addunlock( cj->progeny[2]->sorts[0] , t );
                        task_addunlock( ci->progeny[7]->sorts[6] , t ); task_addunlock( cj->progeny[0]->sorts[6] , t );
                        }
                    else {
                        t->ci = ci->progeny[5]; t->cj = cj->progeny[0];
                        task_addunlock( ci->progeny[5]->sorts[3] , t ); task_addunlock( cj->progeny[0]->sorts[3] , t );
452
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
453
                        task_addunlock( ci->progeny[7]->sorts[3] , t ); task_addunlock( cj->progeny[2]->sorts[3] , t );
454
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[5] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
455
                        task_addunlock( ci->progeny[5]->sorts[0] , t ); task_addunlock( cj->progeny[2]->sorts[0] , t );
456
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
457
458
459
460
461
462
463
464
465
466
467
468
                        task_addunlock( ci->progeny[7]->sorts[6] , t ); task_addunlock( cj->progeny[0]->sorts[6] , t );
                        }
                    ci->progeny[5]->nr_pairs += 2;
                    ci->progeny[7]->nr_pairs += 2;
                    cj->progeny[0]->nr_pairs += 2;
                    cj->progeny[2]->nr_pairs += 2;
                    break;
                    
                case 4: /* (  1 ,  0 ,  0 ) */
                    if ( space_dosub &&
                         !ci->progeny[4]->split && !ci->progeny[5]->split && !ci->progeny[6]->split && !ci->progeny[7]->split &&
                         !cj->progeny[0]->split && !cj->progeny[1]->split && !cj->progeny[2]->split && !cj->progeny[3]->split ) {
469
                        t->type = task_type_sub; t->flags = 4;
Pedro Gonnet's avatar
Pedro Gonnet committed
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
                        task_addunlock( ci->progeny[4]->sorts[4] , t ); task_addunlock( cj->progeny[0]->sorts[4] , t );
                        task_addunlock( ci->progeny[5]->sorts[5] , t ); task_addunlock( cj->progeny[0]->sorts[5] , t );
                        task_addunlock( ci->progeny[6]->sorts[7] , t ); task_addunlock( cj->progeny[0]->sorts[7] , t );
                        task_addunlock( ci->progeny[7]->sorts[8] , t ); task_addunlock( cj->progeny[0]->sorts[8] , t );
                        task_addunlock( ci->progeny[4]->sorts[3] , t ); task_addunlock( cj->progeny[1]->sorts[3] , t );
                        task_addunlock( ci->progeny[5]->sorts[4] , t ); task_addunlock( cj->progeny[1]->sorts[4] , t );
                        task_addunlock( ci->progeny[6]->sorts[6] , t ); task_addunlock( cj->progeny[1]->sorts[6] , t );
                        task_addunlock( ci->progeny[7]->sorts[7] , t ); task_addunlock( cj->progeny[1]->sorts[7] , t );
                        task_addunlock( ci->progeny[4]->sorts[1] , t ); task_addunlock( cj->progeny[2]->sorts[1] , t );
                        task_addunlock( ci->progeny[5]->sorts[2] , t ); task_addunlock( cj->progeny[2]->sorts[2] , t );
                        task_addunlock( ci->progeny[6]->sorts[4] , t ); task_addunlock( cj->progeny[2]->sorts[4] , t );
                        task_addunlock( ci->progeny[7]->sorts[5] , t ); task_addunlock( cj->progeny[2]->sorts[5] , t );
                        task_addunlock( ci->progeny[4]->sorts[0] , t ); task_addunlock( cj->progeny[3]->sorts[0] , t );
                        task_addunlock( ci->progeny[5]->sorts[1] , t ); task_addunlock( cj->progeny[3]->sorts[1] , t );
                        task_addunlock( ci->progeny[6]->sorts[3] , t ); task_addunlock( cj->progeny[3]->sorts[3] , t );
                        task_addunlock( ci->progeny[7]->sorts[4] , t ); task_addunlock( cj->progeny[3]->sorts[4] , t );
                        }
                    else {
                        t->ci = ci->progeny[4]; t->cj = cj->progeny[0];
                        task_addunlock( ci->progeny[4]->sorts[4] , t ); task_addunlock( cj->progeny[0]->sorts[4] , t );
490
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[5] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
491
                        task_addunlock( ci->progeny[5]->sorts[5] , t ); task_addunlock( cj->progeny[0]->sorts[5] , t );
492
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[6] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
493
                        task_addunlock( ci->progeny[6]->sorts[7] , t ); task_addunlock( cj->progeny[0]->sorts[7] , t );
494
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
495
                        task_addunlock( ci->progeny[7]->sorts[8] , t ); task_addunlock( cj->progeny[0]->sorts[8] , t );
496
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[4] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
497
                        task_addunlock( ci->progeny[4]->sorts[3] , t ); task_addunlock( cj->progeny[1]->sorts[3] , t );
498
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[5] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
499
                        task_addunlock( ci->progeny[5]->sorts[4] , t ); task_addunlock( cj->progeny[1]->sorts[4] , t );
500
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[6] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
501
                        task_addunlock( ci->progeny[6]->sorts[6] , t ); task_addunlock( cj->progeny[1]->sorts[6] , t );
502
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
503
                        task_addunlock( ci->progeny[7]->sorts[7] , t ); task_addunlock( cj->progeny[1]->sorts[7] , t );
504
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[4] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
505
                        task_addunlock( ci->progeny[4]->sorts[1] , t ); task_addunlock( cj->progeny[2]->sorts[1] , t );
506
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[5] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
507
                        task_addunlock( ci->progeny[5]->sorts[2] , t ); task_addunlock( cj->progeny[2]->sorts[2] , t );
508
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[6] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
509
                        task_addunlock( ci->progeny[6]->sorts[4] , t ); task_addunlock( cj->progeny[2]->sorts[4] , t );
510
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
511
                        task_addunlock( ci->progeny[7]->sorts[5] , t ); task_addunlock( cj->progeny[2]->sorts[5] , t );
512
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[4] , cj->progeny[3] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
513
                        task_addunlock( ci->progeny[4]->sorts[0] , t ); task_addunlock( cj->progeny[3]->sorts[0] , t );
514
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[5] , cj->progeny[3] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
515
                        task_addunlock( ci->progeny[5]->sorts[1] , t ); task_addunlock( cj->progeny[3]->sorts[1] , t );
516
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[6] , cj->progeny[3] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
517
                        task_addunlock( ci->progeny[6]->sorts[3] , t ); task_addunlock( cj->progeny[3]->sorts[3] , t );
518
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[3] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
                        task_addunlock( ci->progeny[7]->sorts[4] , t ); task_addunlock( cj->progeny[3]->sorts[4] , t );
                        }
                    ci->progeny[4]->nr_pairs += 4;
                    ci->progeny[5]->nr_pairs += 4;
                    ci->progeny[6]->nr_pairs += 4;
                    ci->progeny[7]->nr_pairs += 4;
                    cj->progeny[0]->nr_pairs += 4;
                    cj->progeny[1]->nr_pairs += 4;
                    cj->progeny[2]->nr_pairs += 4;
                    cj->progeny[3]->nr_pairs += 4;
                    break;
                    
                case 5: /* (  1 ,  0 , -1 ) */
                    if ( space_dosub &&
                         !ci->progeny[4]->split && !ci->progeny[6]->split &&
                         !cj->progeny[1]->split && !cj->progeny[3]->split ) {
535
                        t->type = task_type_sub; t->flags = 5;
Pedro Gonnet's avatar
Pedro Gonnet committed
536
537
538
539
540
541
542
543
                        task_addunlock( ci->progeny[4]->sorts[5] , t ); task_addunlock( cj->progeny[1]->sorts[5] , t );
                        task_addunlock( ci->progeny[6]->sorts[5] , t ); task_addunlock( cj->progeny[3]->sorts[5] , t );
                        task_addunlock( ci->progeny[4]->sorts[2] , t ); task_addunlock( cj->progeny[3]->sorts[2] , t );
                        task_addunlock( ci->progeny[6]->sorts[8] , t ); task_addunlock( cj->progeny[1]->sorts[8] , t );
                        }
                    else {
                        t->ci = ci->progeny[4]; t->cj = cj->progeny[1];
                        task_addunlock( ci->progeny[4]->sorts[5] , t ); task_addunlock( cj->progeny[1]->sorts[5] , t );
544
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[6] , cj->progeny[3] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
545
                        task_addunlock( ci->progeny[6]->sorts[5] , t ); task_addunlock( cj->progeny[3]->sorts[5] , t );
546
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[4] , cj->progeny[3] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
547
                        task_addunlock( ci->progeny[4]->sorts[2] , t ); task_addunlock( cj->progeny[3]->sorts[2] , t );
548
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[6] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
                        task_addunlock( ci->progeny[6]->sorts[8] , t ); task_addunlock( cj->progeny[1]->sorts[8] , t );
                        }
                    ci->progeny[4]->nr_pairs += 2;
                    ci->progeny[6]->nr_pairs += 2;
                    cj->progeny[1]->nr_pairs += 2;
                    cj->progeny[3]->nr_pairs += 2;
                    break;
                    
                case 6: /* (  1 , -1 ,  1 ) */
                    t->ci = ci->progeny[5]; t->cj = cj->progeny[2];
                    task_addunlock( ci->progeny[5]->sorts[6] , t ); task_addunlock( cj->progeny[2]->sorts[6] , t );
                    ci->progeny[5]->nr_pairs += 1;
                    cj->progeny[2]->nr_pairs += 1;
                    break;
                    
                case 7: /* (  1 , -1 ,  0 ) */
                    if ( space_dosub &&
                         !ci->progeny[4]->split && !ci->progeny[5]->split &&
                         !cj->progeny[2]->split && !cj->progeny[3]->split ) {
568
                        t->type = task_type_sub; t->flags = 7;
Pedro Gonnet's avatar
Pedro Gonnet committed
569
570
571
572
573
574
575
576
                        task_addunlock( ci->progeny[4]->sorts[6] , t ); task_addunlock( cj->progeny[3]->sorts[6] , t );
                        task_addunlock( ci->progeny[5]->sorts[8] , t ); task_addunlock( cj->progeny[2]->sorts[8] , t );
                        task_addunlock( ci->progeny[4]->sorts[7] , t ); task_addunlock( cj->progeny[2]->sorts[7] , t );
                        task_addunlock( ci->progeny[5]->sorts[7] , t ); task_addunlock( cj->progeny[3]->sorts[7] , t );
                        }
                    else {
                        t->ci = ci->progeny[4]; t->cj = cj->progeny[3];
                        task_addunlock( ci->progeny[4]->sorts[6] , t ); task_addunlock( cj->progeny[3]->sorts[6] , t );
577
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[5] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
578
                        task_addunlock( ci->progeny[5]->sorts[8] , t ); task_addunlock( cj->progeny[2]->sorts[8] , t );
579
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[4] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
580
                        task_addunlock( ci->progeny[4]->sorts[7] , t ); task_addunlock( cj->progeny[2]->sorts[7] , t );
581
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[5] , cj->progeny[3] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
                        task_addunlock( ci->progeny[5]->sorts[7] , t ); task_addunlock( cj->progeny[3]->sorts[7] , t );
                        }
                    ci->progeny[4]->nr_pairs += 2;
                    ci->progeny[5]->nr_pairs += 2;
                    cj->progeny[2]->nr_pairs += 2;
                    cj->progeny[3]->nr_pairs += 2;
                    break;
                    
                case 8: /* (  1 , -1 , -1 ) */
                    t->ci = ci->progeny[4]; t->cj = cj->progeny[3];
                    task_addunlock( ci->progeny[4]->sorts[8] , t ); task_addunlock( cj->progeny[3]->sorts[8] , t );
                    ci->progeny[4]->nr_pairs += 1;
                    cj->progeny[3]->nr_pairs += 1;
                    break;
                    
                case 9: /* (  0 ,  1 ,  1 ) */
                    if ( space_dosub &&
                         !ci->progeny[3]->split && !ci->progeny[7]->split &&
                         !cj->progeny[0]->split && !cj->progeny[4]->split ) {
601
                        t->type = task_type_sub; t->flags = 9;
Pedro Gonnet's avatar
Pedro Gonnet committed
602
603
604
605
606
607
608
609
                        task_addunlock( ci->progeny[3]->sorts[9] , t ); task_addunlock( cj->progeny[0]->sorts[9] , t );
                        task_addunlock( ci->progeny[7]->sorts[9] , t ); task_addunlock( cj->progeny[4]->sorts[9] , t );
                        task_addunlock( ci->progeny[3]->sorts[0] , t ); task_addunlock( cj->progeny[4]->sorts[0] , t );
                        task_addunlock( ci->progeny[7]->sorts[8] , t ); task_addunlock( cj->progeny[0]->sorts[8] , t );
                        }
                    else {
                        t->ci = ci->progeny[3]; t->cj = cj->progeny[0];
                        task_addunlock( ci->progeny[3]->sorts[9] , t ); task_addunlock( cj->progeny[0]->sorts[9] , t );
610
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
611
                        task_addunlock( ci->progeny[7]->sorts[9] , t ); task_addunlock( cj->progeny[4]->sorts[9] , t );
612
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[3] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
613
                        task_addunlock( ci->progeny[3]->sorts[0] , t ); task_addunlock( cj->progeny[4]->sorts[0] , t );
614
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
615
616
617
618
619
620
621
622
623
                        task_addunlock( ci->progeny[7]->sorts[8] , t ); task_addunlock( cj->progeny[0]->sorts[8] , t );
                        }
                    ci->progeny[3]->nr_pairs += 2;
                    ci->progeny[7]->nr_pairs += 2;
                    cj->progeny[0]->nr_pairs += 2;
                    cj->progeny[4]->nr_pairs += 2;
                    break;
                    
                case 10: /* (  0 ,  1 ,  0 ) */
Pedro Gonnet's avatar
Pedro Gonnet committed
624
625
                    if ( space_dosub &&
                         !ci->progeny[2]->split && !ci->progeny[3]->split && !ci->progeny[6]->split && !ci->progeny[7]->split &&
Pedro Gonnet's avatar
Pedro Gonnet committed
626
                         !cj->progeny[0]->split && !cj->progeny[1]->split && !cj->progeny[4]->split && !cj->progeny[5]->split ) {
627
                        t->type = task_type_sub; t->flags = 10;
Pedro Gonnet's avatar
Pedro Gonnet committed
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
                        task_addunlock( ci->progeny[2]->sorts[10] , t ); task_addunlock( cj->progeny[0]->sorts[10] , t );
                        task_addunlock( ci->progeny[3]->sorts[11] , t ); task_addunlock( cj->progeny[0]->sorts[11] , t );
                        task_addunlock( ci->progeny[6]->sorts[7] , t ); task_addunlock( cj->progeny[0]->sorts[7] , t );
                        task_addunlock( ci->progeny[7]->sorts[6] , t ); task_addunlock( cj->progeny[0]->sorts[6] , t );
                        task_addunlock( ci->progeny[2]->sorts[9] , t ); task_addunlock( cj->progeny[1]->sorts[9] , t );
                        task_addunlock( ci->progeny[3]->sorts[10] , t ); task_addunlock( cj->progeny[1]->sorts[10] , t );
                        task_addunlock( ci->progeny[6]->sorts[8] , t ); task_addunlock( cj->progeny[1]->sorts[8] , t );
                        task_addunlock( ci->progeny[7]->sorts[7] , t ); task_addunlock( cj->progeny[1]->sorts[7] , t );
                        task_addunlock( ci->progeny[2]->sorts[1] , t ); task_addunlock( cj->progeny[4]->sorts[1] , t );
                        task_addunlock( ci->progeny[3]->sorts[2] , t ); task_addunlock( cj->progeny[4]->sorts[2] , t );
                        task_addunlock( ci->progeny[6]->sorts[10] , t ); task_addunlock( cj->progeny[4]->sorts[10] , t );
                        task_addunlock( ci->progeny[7]->sorts[11] , t ); task_addunlock( cj->progeny[4]->sorts[11] , t );
                        task_addunlock( ci->progeny[2]->sorts[0] , t ); task_addunlock( cj->progeny[5]->sorts[0] , t );
                        task_addunlock( ci->progeny[3]->sorts[1] , t ); task_addunlock( cj->progeny[5]->sorts[1] , t );
                        task_addunlock( ci->progeny[6]->sorts[9] , t ); task_addunlock( cj->progeny[5]->sorts[9] , t );
                        task_addunlock( ci->progeny[7]->sorts[10] , t ); task_addunlock( cj->progeny[5]->sorts[10] , t );
                        }
                    else {
                        t->ci = ci->progeny[2]; t->cj = cj->progeny[0];
                        task_addunlock( ci->progeny[2]->sorts[10] , t ); task_addunlock( cj->progeny[0]->sorts[10] , t );
648
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[3] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
649
                        task_addunlock( ci->progeny[3]->sorts[11] , t ); task_addunlock( cj->progeny[0]->sorts[11] , t );
650
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[6] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
651
                        task_addunlock( ci->progeny[6]->sorts[7] , t ); task_addunlock( cj->progeny[0]->sorts[7] , t );
652
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
653
                        task_addunlock( ci->progeny[7]->sorts[6] , t ); task_addunlock( cj->progeny[0]->sorts[6] , t );
654
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[2] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
655
                        task_addunlock( ci->progeny[2]->sorts[9] , t ); task_addunlock( cj->progeny[1]->sorts[9] , t );
656
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[3] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
657
                        task_addunlock( ci->progeny[3]->sorts[10] , t ); task_addunlock( cj->progeny[1]->sorts[10] , t );
658
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[6] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
659
                        task_addunlock( ci->progeny[6]->sorts[8] , t ); task_addunlock( cj->progeny[1]->sorts[8] , t );
660
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
661
                        task_addunlock( ci->progeny[7]->sorts[7] , t ); task_addunlock( cj->progeny[1]->sorts[7] , t );
662
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[2] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
663
                        task_addunlock( ci->progeny[2]->sorts[1] , t ); task_addunlock( cj->progeny[4]->sorts[1] , t );
664
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[3] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
665
                        task_addunlock( ci->progeny[3]->sorts[2] , t ); task_addunlock( cj->progeny[4]->sorts[2] , t );
666
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[6] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
667
                        task_addunlock( ci->progeny[6]->sorts[10] , t ); task_addunlock( cj->progeny[4]->sorts[10] , t );
668
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
669
                        task_addunlock( ci->progeny[7]->sorts[11] , t ); task_addunlock( cj->progeny[4]->sorts[11] , t );
670
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[2] , cj->progeny[5] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
671
                        task_addunlock( ci->progeny[2]->sorts[0] , t ); task_addunlock( cj->progeny[5]->sorts[0] , t );
672
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[3] , cj->progeny[5] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
673
                        task_addunlock( ci->progeny[3]->sorts[1] , t ); task_addunlock( cj->progeny[5]->sorts[1] , t );
674
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[6] , cj->progeny[5] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
675
                        task_addunlock( ci->progeny[6]->sorts[9] , t ); task_addunlock( cj->progeny[5]->sorts[9] , t );
676
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[5] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
                        task_addunlock( ci->progeny[7]->sorts[10] , t ); task_addunlock( cj->progeny[5]->sorts[10] , t );
                        }
                    ci->progeny[2]->nr_pairs += 4;
                    ci->progeny[3]->nr_pairs += 4;
                    ci->progeny[6]->nr_pairs += 4;
                    ci->progeny[7]->nr_pairs += 4;
                    cj->progeny[0]->nr_pairs += 4;
                    cj->progeny[1]->nr_pairs += 4;
                    cj->progeny[4]->nr_pairs += 4;
                    cj->progeny[5]->nr_pairs += 4;
                    break;
                    
                case 11: /* (  0 ,  1 , -1 ) */
                    if ( space_dosub &&
                         !ci->progeny[2]->split && !ci->progeny[6]->split &&
                         !cj->progeny[1]->split && !cj->progeny[5]->split ) {
693
                        t->type = task_type_sub; t->flags = 11;
Pedro Gonnet's avatar
Pedro Gonnet committed
694
695
696
697
698
699
700
701
                        task_addunlock( ci->progeny[2]->sorts[11] , t ); task_addunlock( cj->progeny[1]->sorts[11] , t );
                        task_addunlock( ci->progeny[6]->sorts[11] , t ); task_addunlock( cj->progeny[5]->sorts[11] , t );
                        task_addunlock( ci->progeny[2]->sorts[2] , t ); task_addunlock( cj->progeny[5]->sorts[2] , t );
                        task_addunlock( ci->progeny[6]->sorts[6] , t ); task_addunlock( cj->progeny[1]->sorts[6] , t );
                        }
                    else {
                        t->ci = ci->progeny[2]; t->cj = cj->progeny[1];
                        task_addunlock( ci->progeny[2]->sorts[11] , t ); task_addunlock( cj->progeny[1]->sorts[11] , t );
702
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[6] , cj->progeny[5] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
703
                        task_addunlock( ci->progeny[6]->sorts[11] , t ); task_addunlock( cj->progeny[5]->sorts[11] , t );
704
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[2] , cj->progeny[5] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
705
                        task_addunlock( ci->progeny[2]->sorts[2] , t ); task_addunlock( cj->progeny[5]->sorts[2] , t );
706
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[6] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
707
708
709
710
711
712
713
714
715
716
717
718
                        task_addunlock( ci->progeny[6]->sorts[6] , t ); task_addunlock( cj->progeny[1]->sorts[6] , t );
                        }
                    ci->progeny[2]->nr_pairs += 2;
                    ci->progeny[6]->nr_pairs += 2;
                    cj->progeny[1]->nr_pairs += 2;
                    cj->progeny[5]->nr_pairs += 2;
                    break;
                    
                case 12: /* (  0 ,  0 ,  1 ) */
                    if ( space_dosub &&
                         !ci->progeny[1]->split && !ci->progeny[3]->split && !ci->progeny[5]->split && !ci->progeny[7]->split &&
                         !cj->progeny[0]->split && !cj->progeny[2]->split && !cj->progeny[4]->split && !cj->progeny[6]->split ) {
719
                        t->type = task_type_sub; t->flags = 12;
Pedro Gonnet's avatar
Pedro Gonnet committed
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
                        task_addunlock( ci->progeny[1]->sorts[12] , t ); task_addunlock( cj->progeny[0]->sorts[12] , t );
                        task_addunlock( ci->progeny[3]->sorts[11] , t ); task_addunlock( cj->progeny[0]->sorts[11] , t );
                        task_addunlock( ci->progeny[5]->sorts[5] , t ); task_addunlock( cj->progeny[0]->sorts[5] , t );
                        task_addunlock( ci->progeny[7]->sorts[2] , t ); task_addunlock( cj->progeny[0]->sorts[2] , t );
                        task_addunlock( ci->progeny[1]->sorts[9] , t ); task_addunlock( cj->progeny[2]->sorts[9] , t );
                        task_addunlock( ci->progeny[3]->sorts[12] , t ); task_addunlock( cj->progeny[2]->sorts[12] , t );
                        task_addunlock( ci->progeny[5]->sorts[8] , t ); task_addunlock( cj->progeny[2]->sorts[8] , t );
                        task_addunlock( ci->progeny[7]->sorts[5] , t ); task_addunlock( cj->progeny[2]->sorts[5] , t );
                        task_addunlock( ci->progeny[1]->sorts[3] , t ); task_addunlock( cj->progeny[4]->sorts[3] , t );
                        task_addunlock( ci->progeny[3]->sorts[6] , t ); task_addunlock( cj->progeny[4]->sorts[6] , t );
                        task_addunlock( ci->progeny[5]->sorts[12] , t ); task_addunlock( cj->progeny[4]->sorts[12] , t );
                        task_addunlock( ci->progeny[7]->sorts[11] , t ); task_addunlock( cj->progeny[4]->sorts[11] , t );
                        task_addunlock( ci->progeny[1]->sorts[0] , t ); task_addunlock( cj->progeny[6]->sorts[0] , t );
                        task_addunlock( ci->progeny[3]->sorts[3] , t ); task_addunlock( cj->progeny[6]->sorts[3] , t );
                        task_addunlock( ci->progeny[5]->sorts[9] , t ); task_addunlock( cj->progeny[6]->sorts[9] , t );
                        task_addunlock( ci->progeny[7]->sorts[12] , t ); task_addunlock( cj->progeny[6]->sorts[12] , t );
                        }
                    else {
                        t->ci = ci->progeny[1]; t->cj = cj->progeny[0];
                        task_addunlock( ci->progeny[1]->sorts[12] , t ); task_addunlock( cj->progeny[0]->sorts[12] , t );
740
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[3] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
741
                        task_addunlock( ci->progeny[3]->sorts[11] , t ); task_addunlock( cj->progeny[0]->sorts[11] , t );
742
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[5] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
743
                        task_addunlock( ci->progeny[5]->sorts[5] , t ); task_addunlock( cj->progeny[0]->sorts[5] , t );
744
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
745
                        task_addunlock( ci->progeny[7]->sorts[2] , t ); task_addunlock( cj->progeny[0]->sorts[2] , t );
746
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[1] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
747
                        task_addunlock( ci->progeny[1]->sorts[9] , t ); task_addunlock( cj->progeny[2]->sorts[9] , t );
748
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[3] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
749
                        task_addunlock( ci->progeny[3]->sorts[12] , t ); task_addunlock( cj->progeny[2]->sorts[12] , t );
750
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[5] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
751
                        task_addunlock( ci->progeny[5]->sorts[8] , t ); task_addunlock( cj->progeny[2]->sorts[8] , t );
752
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
753
                        task_addunlock( ci->progeny[7]->sorts[5] , t ); task_addunlock( cj->progeny[2]->sorts[5] , t );
754
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[1] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
755
                        task_addunlock( ci->progeny[1]->sorts[3] , t ); task_addunlock( cj->progeny[4]->sorts[3] , t );
756
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[3] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
757
                        task_addunlock( ci->progeny[3]->sorts[6] , t ); task_addunlock( cj->progeny[4]->sorts[6] , t );
758
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[5] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
759
                        task_addunlock( ci->progeny[5]->sorts[12] , t ); task_addunlock( cj->progeny[4]->sorts[12] , t );
760
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
761
                        task_addunlock( ci->progeny[7]->sorts[11] , t ); task_addunlock( cj->progeny[4]->sorts[11] , t );
762
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[1] , cj->progeny[6] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
763
                        task_addunlock( ci->progeny[1]->sorts[0] , t ); task_addunlock( cj->progeny[6]->sorts[0] , t );
764
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[3] , cj->progeny[6] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
765
                        task_addunlock( ci->progeny[3]->sorts[3] , t ); task_addunlock( cj->progeny[6]->sorts[3] , t );
766
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[5] , cj->progeny[6] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
767
                        task_addunlock( ci->progeny[5]->sorts[9] , t ); task_addunlock( cj->progeny[6]->sorts[9] , t );
768
                        t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[7] , cj->progeny[6] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
                        task_addunlock( ci->progeny[7]->sorts[12] , t ); task_addunlock( cj->progeny[6]->sorts[12] , t );
                        }
                    ci->progeny[1]->nr_pairs += 4;
                    ci->progeny[3]->nr_pairs += 4;
                    ci->progeny[5]->nr_pairs += 4;
                    ci->progeny[7]->nr_pairs += 4;
                    cj->progeny[0]->nr_pairs += 4;
                    cj->progeny[2]->nr_pairs += 4;
                    cj->progeny[4]->nr_pairs += 4;
                    cj->progeny[6]->nr_pairs += 4;
                    break;
            
                }
                
            /* Take a step back... */
            tid -= 1;
            
            } /* split this task? */
    
        } /* loop over all tasks. */
        
    }
    
    
/**
 * @brief Fill the #space's task list.
 *
 * @param s The #space we are working in.
 * @param do_sort Flag to add sorting tasks to the list.
 */
 
void space_maketasks ( struct space *s , int do_sort ) {

    int i, j, k, ii, jj, kk, iii, jjj, kkk, cid, cjd;
    int *cdim = s->cdim;
    int nr_tasks_old = s->nr_tasks;
Pedro Gonnet's avatar
Pedro Gonnet committed
805
    struct task *t , *t2;
Pedro Gonnet's avatar
Pedro Gonnet committed
806
807
808
809
810
811
812
    int pts[7][8] = { { -1 , 12 , 10 , 9 , 4 , 3 , 1 , 0 } ,
                      { -1 , -1 , 11 , 10 , 5 , 4 , 2 , 1 } ,
                      { -1 , -1 , -1 , 12 , 7 , 6 , 4 , 3 } , 
                      { -1 , -1 , -1 , -1 , 8 , 7 , 5 , 4 } ,
                      { -1 , -1 , -1 , -1 , -1 , 12 , 10 , 9 } ,
                      { -1 , -1 , -1 , -1 , -1 , -1 , 11 , 10 } ,
                      { -1 , -1 , -1 , -1 , -1 , -1 , -1 , 12 } };
813
    int counts[task_type_count];
Pedro Gonnet's avatar
Pedro Gonnet committed
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829

    /* Recursive function to generate tasks in the cell tree. */
    void maketasks_rec ( struct cell *c , struct task *sort_up[] , int nr_sort_up , struct cell *parent ) {

        int j, k, nr_sort = 0;
        struct task *sort[14], *t;

        /* Clear the waits on this cell. */
        c->wait = 0;
        sort[0] = NULL;
        
        /* Start by generating the sort task. */
        if ( c->count > 0 ) {
        
            if ( do_sort ) {
                if ( c->count < 1000 ) {
830
                    sort[0] = space_addtask( s , task_type_sort , task_subtype_none , 0x1fff , 0 , c , NULL , sort_up , nr_sort_up , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
831
832
833
834
835
                    for ( k = 0 ; k < 13 ; k++ )
                        c->sorts[k] = sort[0];
                    nr_sort = 1;
                    }
                else if ( c->count < 5000 ) {
836
837
838
                    sort[0] = space_addtask( s , task_type_sort , task_subtype_none , 0xf , 0 , c , NULL , sort_up , nr_sort_up , NULL , 0 );
                    sort[1] = space_addtask( s , task_type_sort , task_subtype_none , 0xf0 , 0 , c , NULL , sort_up , nr_sort_up , NULL , 0 );
                    sort[2] = space_addtask( s , task_type_sort , task_subtype_none , 0x1f00 , 0 , c , NULL , sort_up , nr_sort_up , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
839
840
841
842
843
844
845
846
847
                    for ( k = 0 ; k < 4 ; k++ )
                        c->sorts[k] = sort[0];
                    for ( k = 4 ; k < 8 ; k++ )
                        c->sorts[k] = sort[1];
                    for ( k = 8 ; k < 13 ; k++ )
                        c->sorts[k] = sort[2];
                    nr_sort = 3;
                    }
                else {
848
849
850
851
852
853
854
855
856
857
858
859
860
                    c->sorts[0] = sort[0] = space_addtask( s , task_type_sort , task_subtype_none , 0x1 , 0 , c , NULL , sort_up , nr_sort_up , NULL , 0 );
                    c->sorts[1] = sort[1] = space_addtask( s , task_type_sort , task_subtype_none , 0x2 , 0 , c , NULL , sort_up , nr_sort_up , NULL , 0 );
                    c->sorts[2] = sort[2] = space_addtask( s , task_type_sort , task_subtype_none , 0x4 , 0 , c , NULL , sort_up , nr_sort_up , NULL , 0 );
                    c->sorts[3] = sort[3] = space_addtask( s , task_type_sort , task_subtype_none , 0x8 , 0 , c , NULL , sort_up , nr_sort_up , NULL , 0 );
                    c->sorts[4] = sort[4] = space_addtask( s , task_type_sort , task_subtype_none , 0x10 , 0 , c , NULL , sort_up , nr_sort_up , NULL , 0 );
                    c->sorts[5] = sort[5] = space_addtask( s , task_type_sort , task_subtype_none , 0x20 , 0 , c , NULL , sort_up , nr_sort_up , NULL , 0 );
                    c->sorts[6] = sort[6] = space_addtask( s , task_type_sort , task_subtype_none , 0x40 , 0 , c , NULL , sort_up , nr_sort_up , NULL , 0 );
                    c->sorts[7] = sort[7] = space_addtask( s , task_type_sort , task_subtype_none , 0x80 , 0 , c , NULL , sort_up , nr_sort_up , NULL , 0 );
                    c->sorts[8] = sort[8] = space_addtask( s , task_type_sort , task_subtype_none , 0x100 , 0 , c , NULL , sort_up , nr_sort_up , NULL , 0 );
                    c->sorts[9] = sort[9] = space_addtask( s , task_type_sort , task_subtype_none , 0x200 , 0 , c , NULL , sort_up , nr_sort_up , NULL , 0 );
                    c->sorts[10] = sort[10] = space_addtask( s , task_type_sort , task_subtype_none , 0x400 , 0 , c , NULL , sort_up , nr_sort_up , NULL , 0 );
                    c->sorts[11] = sort[11] = space_addtask( s , task_type_sort , task_subtype_none , 0x800 , 0 , c , NULL , sort_up , nr_sort_up , NULL , 0 );
                    c->sorts[12] = sort[12] = space_addtask( s , task_type_sort , task_subtype_none , 0x1000 , 0 , c , NULL , sort_up , nr_sort_up , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
861
862
863
864
865
866
                    nr_sort = 13;
                    }
                }

            /* Generate a self-interaction if not split. */
            if ( !c->split && c->count > 1 )
867
                space_addtask( s , task_type_self , task_subtype_density , 0 , 0 , c , NULL , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
                
            }
            
        /* Otherwise, add the interactions between progeny. */
        if ( c->split ) {
        
            /* Recurse. */
            for ( k = 0 ; k < 8 ; k++ )
                if ( c->progeny[k] != NULL )
                    maketasks_rec( c->progeny[k] , sort , nr_sort , c );
                        
            /* Worth splitting into several tasks? */
            if ( !space_dosub || c->count > 2*space_splitsize ) {
            
                /* Make a task for each pair of progeny. */
                for ( j = 0 ; j < 8 ; j++ )
                    if ( c->progeny[j] != NULL && c->progeny[j]->count > 0 )
                        for ( k = j + 1 ; k < 8 ; k++ )
                            if ( c->progeny[k] != NULL && c->progeny[k]->count > 0 ) {
887
                                t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , c->progeny[j] , c->progeny[k] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
888
889
890
891
892
893
894
895
896
897
898
899
                                task_addunlock( c->progeny[j]->sorts[ pts[j][k] ] , t );
                                task_addunlock( c->progeny[k]->sorts[ pts[j][k] ] , t );
                                c->progeny[k]->nr_pairs += 1;
                                c->progeny[j]->nr_pairs += 1;
                                }
                                
                }
                
            /* Otherwise, dispatch as one large task. */
            else {
            
                /* Add the task. */
900
                t = space_addtask( s , task_type_sub , task_subtype_density , 0 , 0 , c , NULL , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
                
                /* Make it depend on all the sorts of its progeny. */
                for ( k = 0 ; k < 8 ; k++ )
                    for ( j = 0 ; j < 13 ; j++ )
                        if ( c->progeny[k] != NULL )
                            task_addunlock( c->progeny[k]->sorts[j] , t );
            
                }

            }

        }
        
    /* Allocate the task-list, if needed. */
    if ( s->tasks == NULL )
Pedro Gonnet's avatar
Pedro Gonnet committed
916
        if ( posix_memalign( (void *)&s->tasks , 64 , sizeof(struct task) * s->tot_cells * 30 ) != 0 )
Pedro Gonnet's avatar
Pedro Gonnet committed
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
            error( "Failed to allocate task list." );
    s->nr_tasks = 0;
    
    /* Loop over the cells and get their sub-tasks. */
    for ( k = 0 ; k < s->nr_cells ; k++ )
        maketasks_rec( &s->cells[k] , NULL , 0 , NULL );

    /* Run through the highest level of cells and add pairs. */
    for ( i = 0 ; i < cdim[0] ; i++ )
        for ( j = 0 ; j < cdim[1] ; j++ )
            for ( k = 0 ; k < cdim[2] ; k++ ) {
                cid = cell_getid( cdim , i , j , k );
                if ( s->cells[cid].count == 0 )
                    continue;
                for ( ii = -1 ; ii < 2 ; ii++ ) {
                    iii = i + ii;
                    if ( !s->periodic && ( iii < 0 || iii >= cdim[0] ) )
                        continue;
                    iii = ( iii + cdim[0] ) % cdim[0];
                    for ( jj = -1 ; jj < 2 ; jj++ ) {
                        jjj = j + jj;
                        if ( !s->periodic && ( jjj < 0 || jjj >= cdim[1] ) )
                            continue;
                        jjj = ( jjj + cdim[1] ) % cdim[1];
                        for ( kk = -1 ; kk < 2 ; kk++ ) {
                            kkk = k + kk;
                            if ( !s->periodic && ( kkk < 0 || kkk >= cdim[2] ) )
                                continue;
                            kkk = ( kkk + cdim[2] ) % cdim[2];
                            cjd = cell_getid( cdim , iii , jjj , kkk );
                            if ( s->cells[cjd].count == 0 )
                                continue;
                            if ( cid >= cjd )
                                continue;
951
                            t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , &s->cells[cid] , &s->cells[cjd] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
952
953
954
955
956
957
958
959
960
961
962
                            task_addunlock( s->cells[cid].sorts[ sortlistID[ (kk+1) + 3*( (jj+1) + 3*(ii+1) ) ] ] , t );
                            task_addunlock( s->cells[cjd].sorts[ sortlistID[ (kk+1) + 3*( (jj+1) + 3*(ii+1) ) ] ] , t );
                            s->cells[cid].nr_pairs += 1;
                            s->cells[cjd].nr_pairs += 1;
                            }
                        }
                    }
                }

    /* Split the tasks. */
    space_splittasks( s );
963
    
Pedro Gonnet's avatar
Pedro Gonnet committed
964
965
966
967
968
969
970
971
972
    /* Remove sort tasks with no dependencies. */
    for ( k = 0 ; k < s->nr_tasks ; k++ ) {
        t = &s->tasks[k];
        if ( t->type == task_type_sort && t->nr_unlock_tasks == 0 ) {
            if ( t->ci->split )
                for ( i = 0 ; i < 13 ; i++ )
                    if ( t->flags & ( 1 << i ) ) {
                        for ( j = 0 ; j < 8 ; j++ )
                            if ( t->ci->progeny[j] != NULL )
Pedro Gonnet's avatar
Pedro Gonnet committed
973
                                task_rmunlock_blind( t->ci->progeny[j]->sorts[i] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
974
975
976
977
978
979
                        t->ci->sorts[i] = NULL;
                        }
            t->type = task_type_none;
            }
        }
        
980
981
    /* Count the number of tasks associated with each cell and
       store the density tasks in each cell. */
Pedro Gonnet's avatar
Pedro Gonnet committed
982
    space_map_cells( s , 1 , &space_map_clearnrtasks , NULL );
Pedro Gonnet's avatar
Pedro Gonnet committed
983
984
    for ( k = 0 ; k < s->nr_tasks ; k++ ) {
        t = &s->tasks[k];
985
        if ( t->type == task_type_self ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
986
            t->ci->nr_tasks += 1;
987
988
989
990
991
            if ( t->subtype == task_subtype_density ) {
                t->ci->density[ t->ci->nr_density ] = t;
                t->ci->nr_density += 1;
                }
            }
Pedro Gonnet's avatar
Pedro Gonnet committed
992
993
994
        else if ( t->type == task_type_pair ) {
            t->ci->nr_tasks += 1;
            t->cj->nr_tasks += 1;
995
996
997
998
999
1000
            if ( t->subtype == task_subtype_density ) {
                t->ci->density[ t->ci->nr_density ] = t;
                t->ci->nr_density += 1;
                t->cj->density[ t->cj->nr_density ] = t;
                t->cj->nr_density += 1;
                }
Pedro Gonnet's avatar
Pedro Gonnet committed
1001
1002
1003
1004
1005
            }
        else if ( t->type == task_type_sub ) {
            t->ci->nr_tasks += 1;
            if ( t->cj != NULL )
                t->cj->nr_tasks += 1;
1006
1007
1008
1009
1010
1011
1012
1013
            if ( t->subtype == task_subtype_density ) {
                t->ci->density[ t->ci->nr_density ] = t;
                t->ci->nr_density += 1;
                if ( t->cj != NULL ) {
                    t->cj->density[ t->cj->nr_density ] = t;
                    t->cj->nr_density += 1;
                    }
                }
Pedro Gonnet's avatar
Pedro Gonnet committed
1014
1015
            }
        }
1016
        
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
    /* Append a ghost task to each cell. */
    space_map_cells( s , 1 , &space_map_mkghosts , s );
    
    /* Run through the tasks and make iacts for each density task. */
    for ( k = 0 ; k < s->nr_tasks ; k++ ) {
    
        /* Get a pointer to the task. */
        t = &s->tasks[k];
        
        /* Self-interaction? */
        if ( t->type == task_type_self && t->subtype == task_subtype_density ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
1028
            task_addunlock( t , t->ci->super->ghost );
1029
1030
1031
1032
1033
1034
            t2 = space_addtask( s , task_type_self , task_subtype_force , 0 , 0 , t->ci , NULL , NULL , 0 , NULL , 0 );
            task_addunlock( t->ci->ghost , t2 );
            }
            
        /* Otherwise, pair interaction? */
        else if ( t->type == task_type_pair && t->subtype == task_subtype_density ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
1035
1036
            task_addunlock( t , t->ci->super->ghost );
            task_addunlock( t , t->cj->super->ghost );
1037
1038
1039
1040
1041
            t2 = space_addtask( s , task_type_pair , task_subtype_force , 0 , 0 , t->ci , t->cj , NULL , 0 , NULL , 0 );
            task_addunlock( t->ci->ghost , t2 );
            task_addunlock( t->cj->ghost , t2 );
            }

1042
1043
        /* Otherwise, sub interaction? */
        else if ( t->type == task_type_sub && t->subtype == task_subtype_density ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
1044
            task_addunlock( t , t->ci->super->ghost );
1045
            if ( t->cj != NULL )
Pedro Gonnet's avatar
Pedro Gonnet committed
1046
                task_addunlock( t , t->cj->super->ghost );
1047
1048
1049
1050
1051
            t2 = space_addtask( s , task_type_sub , task_subtype_force , t->flags , 0 , t->ci , t->cj , NULL , 0 , NULL , 0 );
            task_addunlock( t->ci->ghost , t2 );
            if ( t->cj != NULL )
                task_addunlock( t->cj->ghost , t2 );
            }
Pedro Gonnet's avatar
Pedro Gonnet committed
1052
            
1053
        }
Pedro Gonnet's avatar
Pedro Gonnet committed
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
        
    /* Did we already create indices? */
    if ( s->tasks_ind == NULL )
        if ( ( s->tasks_ind = (int *)malloc( sizeof(int) * s->nr_tasks ) ) == NULL )
            error( "Failed to allocate task indices." );
    
    /* Did the number of tasks change, i.e. do we have to re-index? */
    if ( nr_tasks_old != s->nr_tasks )
        for ( k = 0 ; k < s->nr_tasks ; k++ )
            s->tasks_ind[k] = k;
            
    /* Count the number of each task type. */
1066
    for ( k = 0 ; k < task_type_count ; k++ )
Pedro Gonnet's avatar
Pedro Gonnet committed
1067
1068
1069
1070
        counts[k] = 0;
    for ( k = 0 ; k < s->nr_tasks ; k++ )
        counts[ s->tasks[k].type ] += 1;
    printf( "space_maketasks: task counts are [ %s=%i" , taskID_names[0] , counts[0] );
1071
    for ( k = 1 ; k < task_type_count ; k++ )
Pedro Gonnet's avatar
Pedro Gonnet committed
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
        printf( " %s=%i" , taskID_names[k] , counts[k] );
    printf( " ]\n" );
        
    /* Re-set the next task pointer. */
    s->next_task = 0;
            
    }
    
    

/**
 * @brief Split cells that contain too many particles.
 *
 * @param s The #space we are working in.
 * @param c The #cell under consideration.
 */
 
void space_split ( struct space *s , struct cell *c ) {

    int k, count;
Pedro Gonnet's avatar
Pedro Gonnet committed
1092
    double h, h_limit, h_max = 0.0;
Pedro Gonnet's avatar
Pedro Gonnet committed
1093
1094
1095
1096
1097
1098
1099
    struct cell *temp;
    
    /* Check the depth. */
    if ( c->depth > s->maxdepth )
        s->maxdepth = c->depth;
    
    /* Set the minimum cutoff. */
1100
    h_limit = fmin( c->h[0] , fmin( c->h[1] , c->h[2] ) ) / 2;
Pedro Gonnet's avatar
Pedro Gonnet committed
1101
1102
1103
    
    /* Count the particles below that. */
    for ( count = 0 , k = 0 ; k < c->count ; k++ ) {
1104
1105
        h = c->parts[k].h;
        if ( h <= h_limit )
Pedro Gonnet's avatar
Pedro Gonnet committed
1106
            count += 1;
Pedro Gonnet's avatar
Pedro Gonnet committed
1107
1108
        if ( h > h_max )
            h_max = h;
Pedro Gonnet's avatar
Pedro Gonnet committed
1109
        }
Pedro Gonnet's avatar
Pedro Gonnet committed
1110
    c->h_max = h_max;
Pedro Gonnet's avatar
Pedro Gonnet committed
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
            
    /* Split or let it be? */
    if ( count > c->count*space_splitratio && c->count > space_splitsize ) {
    
        /* No longer just a leaf. */
        c->split = 1;
        
        /* Create the cell's progeny. */
        for ( k = 0 ; k < 8 ; k++ ) {
            temp = space_getcell( s );
            temp->count = 0;
            temp->loc[0] = c->loc[0];
            temp->loc[1] = c->loc[1];
            temp->loc[2] = c->loc[2];
            temp->h[0] = c->h[0]/2;
            temp->h[1] = c->h[1]/2;
            temp->h[2] = c->h[2]/2;
            if ( k & 4 )
                temp->loc[0] += temp->h[0];
            if ( k & 2 )
                temp->loc[1] += temp->h[1];
            if ( k & 1 )
                temp->loc[2] += temp->h[2];
            temp->depth = c->depth + 1;
            temp->split = 0;
Pedro Gonnet's avatar
Pedro Gonnet committed
1136
            temp->h_max = 0.0;
Pedro Gonnet's avatar
Pedro Gonnet committed
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
            temp->parent = c;
            c->progeny[k] = temp;
            }
            
        /* Split the cell data. */
        cell_split( c );
            
        /* Recurse? */
        for ( k = 0 ; k < 8 ; k++ )
            space_split( s , c->progeny[k] );
            
        /* Remove any progeny with zero parts. */
        for ( k = 0 ; k < 8 ; k++ )
            if ( c->progeny[k]->count == 0 ) {
                space_recycle( s , c->progeny[k] );
                c->progeny[k] = NULL;
                }
            
        }
        
    /* Otherwise, set the progeny to null. */
    else {
        bzero( c->progeny , sizeof(struct cell *) * 8 );
        c->split = 0;
        }

    }


/**
 * @brief Return a used cell to the cell buffer.
 *
 * @param s The #space.
 * @param c The #cell.
 */
 
void space_recycle ( struct space *s , struct cell *c