space.c 74.7 KB
Newer Older
Pedro Gonnet's avatar
Pedro Gonnet committed
1
/*******************************************************************************
2
 * This file is part of SWIFT.
Pedro Gonnet's avatar
Pedro Gonnet committed
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
 * 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
#include "space.h"
#include "runner.h"

/* Error macro. */
42
#define error(s) { fprintf( stderr , "%s:%s:%i: %s\n" , __FILE__ , __FUNCTION__ , __LINE__ , s ); abort(); }
Pedro Gonnet's avatar
Pedro Gonnet committed
43
44
45

/* Split size. */
int space_splitsize = space_splitsize_default;
46
int space_subsize = space_subsize_default;
Pedro Gonnet's avatar
Pedro Gonnet committed
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

/* 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 
    };
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
    
    
/**
 * @brief Mark tasks to be skipped and set the sort flags accordingly.
 * 
 * @return 1 if the space has to be rebuilt, 0 otherwise.
 */
 
int space_marktasks ( struct space *s ) {

    int k;
    struct task *t;
    float dt_step = s->dt_step;
    
    /* Run through the tasks and clear the sort flags. */
    for ( k = 0 ; k < s->nr_tasks ; k++ ) {
    
        /* Get a handle on the kth task. */
        t = &s->tasks[k];
        
        /* Sort task? */
        if ( t->type == task_type_sort ) {
        
            /* Re-set the flags. */
            t->flags = 0;
            
            }
            
        }
            
    /* Run through the tasks and mark as skip or not. */
    for ( k = 0 ; k < s->nr_tasks ; k++ ) {
    
        /* Get a handle on the kth task. */
        t = &s->tasks[k];
        
        /* Single-cell task? */
        if ( t->type == task_type_self ||
             t->type == task_type_ghost ||
             ( t->type == task_type_sub && t->cj == NULL ) ) {
             
            /* Set this task's skip. */
            t->skip = ( t->ci->dt_min > dt_step );
            
            /* Set the sort flags. */
            if ( !t->skip && t->type == task_type_sub )
                space_addsorts( s , t , t->ci , t->cj , t->flags );
            
            }
        
        /* Pair? */
        else if ( t->type == task_type_pair || ( t->type == task_type_sub && t->cj != NULL ) ) {
            
            /* Set this task's skip. */
            t->skip = ( t->ci->dt_min > dt_step && t->cj->dt_min > dt_step );
            
            /* Too much particle movement? */
            if ( !t->skip && t->tight &&
                 ( t->ci->dx_max > t->ci->dmin || t->cj->dx_max > t->cj->dmin ) )
                break;
                
            /* Set the sort flags. */
            if ( !t->skip ) {
                if ( t->type == task_type_pair ) {
                    t->ci->sorts->flags |= (1 << t->flags);
                    t->cj->sorts->flags |= (1 << t->flags);
                    }
                else
                    space_addsorts( s , t , t->ci , t->cj , t->flags );
                }
                
            }
            
        /* None? */
        else if ( t->type == task_type_none )
            t->skip = 1;
            
        }
        
    /* Did this not go through? */
    if ( k < s->nr_tasks )
        return 1;
        
    /* Run through the tasks and mark as skip or not. */
    for ( k = 0 ; k < s->nr_tasks ; k++ ) {
    
        /* Get a handle on the kth task. */
        t = &s->tasks[k];
        
        /* Sort task? */
        if ( t->type == task_type_sort ) {
        
            /* If this sort has no flags, skip it. */
            t->skip = ( t->flags == 0 );

            }
                        
        }
        
    /* All is well... */
    return 0;
    
    }
181
182
183
184
185
186
187
188
189
190


/**
 * @brief Mapping function to set dt_min and dt_max.
 */

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

    int k;
    float dt_min, dt_max, h_max, dx_max;
191
192
193
    struct part *restrict p;
    struct xpart *restrict xp;
    struct cpart *restrict cp;
194
195
196
197
198
199

    /* No children? */
    if ( !c->split ) {
    
        /* Init with first part. */
        p = &c->parts[0];
200
        xp = p->xtras;
201
202
        cp = &c->cparts[0];
        
203
204
205
        dt_min = p->dt;
        dt_max = p->dt;
        h_max = p->h;
206
207
208
        dx_max = sqrtf( (p->x[0] - xp->x_old[0])*(p->x[0] - xp->x_old[0]) +
                        (p->x[1] - xp->x_old[1])*(p->x[1] - xp->x_old[1]) +
                        (p->x[2] - xp->x_old[2])*(p->x[2] - xp->x_old[2]) )*2 + p->h;
209
210
211
212
213
        cp->x[0] = p->x[0];
        cp->x[1] = p->x[1];
        cp->x[2] = p->x[2];
        cp->h = p->h;
        cp->dt = p->dt;
214
215
216
217
    
        /* Loop over parts. */
        for ( k = 1 ; k < c->count ; k++ ) {
            p = &c->parts[k];
218
            xp = p->xtras;
219
            cp = &c->cparts[k];
220
221
222
            dt_min = fminf( dt_min , p->dt );
            dt_max = fmaxf( dt_max , p->dt );
            h_max = fmaxf( h_max , p->h );
223
224
225
            dx_max = fmaxf( dx_max , sqrtf( (p->x[0] - xp->x_old[0])*(p->x[0] - xp->x_old[0]) +
                                            (p->x[1] - xp->x_old[1])*(p->x[1] - xp->x_old[1]) +
                                            (p->x[2] - xp->x_old[2])*(p->x[2] - xp->x_old[2]) )*2 + p->h );
226
227
228
229
230
            cp->x[0] = p->x[0];
            cp->x[1] = p->x[1];
            cp->x[2] = p->x[2];
            cp->h = p->h;
            cp->dt = p->dt;
231
232
233
234
235
236
            }
            
        }
        
    /* Otherwise, agregate from children. */
    else {
Pedro Gonnet's avatar
Pedro Gonnet committed
237
    
238
        /* Init with the first non-null child. */
239
        for ( k = 0 ; c->progeny[k] == NULL ; k++ );
240
241
242
243
244
245
246
247
248
249
250
251
252
        dt_min = c->progeny[k]->dt_min;
        dt_max = c->progeny[k]->dt_max;
        h_max = c->progeny[k]->h_max;
        dx_max = c->progeny[k]->dx_max;
        
        /* Loop over the remaining progeny. */
        for ( k += 1 ; k < 8 ; k++ )
            if ( c->progeny[k] != NULL ) {
                dt_min = fminf( dt_min , c->progeny[k]->dt_min );
                dt_max = fmaxf( dt_max , c->progeny[k]->dt_max );
                h_max = fmaxf( h_max , c->progeny[k]->h_max );
                dx_max = fmaxf( dx_max , c->progeny[k]->dx_max );
                }
Pedro Gonnet's avatar
Pedro Gonnet committed
253
    
254
255
256
257
258
259
260
        }

    /* Store the values. */
    c->dt_min = dt_min;
    c->dt_max = dt_max;
    c->h_max = h_max;
    c->dx_max = dx_max;
261
    c->sorted = 0;
262
263
    
    /* Clean out the task pointers. */
264
265
266
    // c->sorts = NULL;
    // c->nr_tasks = 0;
    // c->nr_density = 0;
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
    
    }


/**
 * @brief Check the integrity of the space and rebuild if necessary.
 *
 * @param s The #space.
 *
 * Runs through the tasks and marks those as "skip" which have no
 * effect for the current @c dt_max. Verifies the integrity of the
 * cell tree for those tasks and triggers a rebuild if necessary.
 */
 
void space_prepare ( struct space *s ) {

283
284
285
286
    int k, rebuild;
    // struct task *t;
    // float dt_step = s->dt_step;
    float dx_max = 0.0f;
287
    int counts[ task_type_count + 1 ];
288
    ticks tic;
289
    
290
291
292
293
294
    /* Get the maximum displacement in the whole system. */
    for ( k = 0 ; k < s->nr_cells ; k++ )
        dx_max = fmaxf( dx_max , s->cells[k].dx_max );
    printf( "space_prepare: dx_max is %e.\n" , dx_max );
    
295
    /* Run through the tasks and mark as skip or not. */
296
    tic = getticks();
297
298
    rebuild = space_marktasks( s );
    printf( "space_prepare: space_marktasks tasks took %.3f ms.\n" , (double)(getticks() - tic)/CPU_TPS*1000 );
299
300
        
    /* Did this not go through? */
301
    if ( rebuild ) {
302
303
    
        /* Re-build the space. */
304
        tic = getticks();
305
        space_rebuild( s , 0.0 );
306
        printf( "space_prepare: space_rebuild took %.3f ms.\n" , (double)(getticks() - tic)/CPU_TPS*1000 );
307
    
308
        /* Traverse the cells and set their dt_min and dx_max. */
309
        tic = getticks();
310
        space_map_cells_post( s , 1 , &space_map_prepare , NULL );
311
        printf( "space_prepare: space_map_prepare took %.3f ms.\n" , (double)(getticks() - tic)/CPU_TPS*1000 );
312
    
313
314
315
316
317
        /* Run through the tasks and mark as skip or not. */
        tic = getticks();
        rebuild = space_marktasks( s );
        printf( "space_prepare: space_marktasks tasks took %.3f ms.\n" , (double)(getticks() - tic)/CPU_TPS*1000 );
        
318
319
320
        }

    /* Now that we have the cell structre, re-build the tasks. */
321
322
323
    // tic = getticks();
    // space_maketasks( s , 1 );
    // printf( "space_prepare: maketasks took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
324
325
    
    /* Count the number of each task type. */
326
    tic = getticks();
327
328
329
330
331
332
333
334
335
336
337
    for ( k = 0 ; k <= task_type_count ; k++ )
        counts[k] = 0;
    for ( k = 0 ; k < s->nr_tasks ; k++ )
        if ( !s->tasks[k].skip )
            counts[ (int)s->tasks[k].type ] += 1;
        else
            counts[ task_type_count ] += 1;
    printf( "space_prepare: task counts are [ %s=%i" , taskID_names[0] , counts[0] );
    for ( k = 1 ; k < task_type_count ; k++ )
        printf( " %s=%i" , taskID_names[k] , counts[k] );
    printf( " skipped=%i ]\n" , counts[ task_type_count ] ); fflush(stdout);
338
    printf( "space_prepare: task counting took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
339
340
341
342
343
344
345
346
347
348
349
350
351
    
    }
    
    
/** 
 * @brief Sort the tasks in topological order over all queues.
 *
 * @param s The #space.
 */
 
void space_ranktasks ( struct space *s ) {

    int i, j = 0, k, temp, left = 0, rank;
352
353
    struct task *t, *tasks = s->tasks;
    int *tid = s->tasks_ind, nr_tasks = s->nr_tasks;
354
355
    
    /* Run throught the tasks and get all the waits right. */
356
    for ( i = 0 , k = 0 ; k < nr_tasks ; k++ ) {
357
        tid[k] = k;
358
359
        for ( j = 0 ; j < tasks[k].nr_unlock_tasks ; j++ )
            tasks[k].unlock_tasks[j]->wait += 1;
360
361
362
        }
        
    /* Main loop. */
363
    for ( j = 0 , rank = 0 ; left < nr_tasks ; rank++ ) {
364
365
        
        /* Load the tids of tasks with no waits. */
366
367
        for ( k = left ; k < nr_tasks ; k++ )
            if ( tasks[ tid[k] ].wait == 0 ) {
368
369
370
                temp = tid[j]; tid[j] = tid[k]; tid[k] = temp;
                j += 1;
                }
371
372
373
374
                
        /* Did we get anything? */
        if ( j == left )
            error( "Unsatisfiable task dependencies detected." );
375

376
        /* Unlock the next layer of tasks. */
377
        for ( i = left ; i < j ; i++ ) {
378
            t = &tasks[ tid[i] ];
379
            t->rank = rank;
380
381
382
            tid[i] = t - tasks;
            if ( tid[i] >= nr_tasks )
                error( "Task index overshoot." );
383
384
385
386
387
388
389
390
391
392
393
394
395
396
            /* printf( "engine_ranktasks: task %i of type %s has rank %i.\n" , i , 
                (t->type == task_type_self) ? "self" : (t->type == task_type_pair) ? "pair" : "sort" , rank ); */
            for ( k = 0 ; k < t->nr_unlock_tasks ; k++ )
                t->unlock_tasks[k]->wait -= 1;
            }
            
        /* The new left (no, not tony). */
        left = j;
            
        }
        
    }


397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/**
 * @brief Get the shift-id of the given pair of cells, swapping them
 *      if need be.
 *
 * @param s The space
 * @param ci Pointer to first #cell.
 * @param cj Pointer second #cell.
 * @param shift Vector from ci to cj.
 *
 * @return The shift ID and set shift, may or may not swap ci and cj.
 */
 
int space_getsid ( struct space *s , struct cell **ci , struct cell **cj , double *shift ) {

    int k, sid = 0;
    struct cell *temp;
    double dx[3];

    /* Get the relative distance between the pairs, wrapping. */
    for ( k = 0 ; k < 3 ; k++ ) {
        dx[k] = (*cj)->loc[k] - (*ci)->loc[k];
        if ( dx[k] < -s->dim[k]/2 )
            shift[k] = s->dim[k];
        else if ( dx[k] > s->dim[k]/2 )
            shift[k] = -s->dim[k];
        else
            shift[k] = 0.0;
        dx[k] += shift[k];
        }
        
    /* Get the sorting index. */
    for ( k = 0 ; k < 3 ; k++ )
        sid = 3*sid + ( (dx[k] < 0.0) ? 0 : ( (dx[k] > 0.0) ? 2 : 1 ) );

    /* Switch the cells around? */
    if ( runner_flip[sid] ) {
        temp = *ci; *ci = *cj; *cj = temp;
        for ( k = 0 ; k < 3 ; k++ )
            shift[k] = -shift[k];
        }
    sid = sortlistID[sid];
    
    /* Return the sort ID. */
    return sid;

    }


445
/**
446
 * @brief Recursively dismantle a cell tree.
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
 *
 */
 
void space_rebuild_recycle ( struct space *s , struct cell *c ) {
    
    int k;
    
    if ( c->split )
        for ( k = 0 ; k < 8 ; k++ )
            if ( c->progeny[k] != NULL ) {
                space_rebuild_recycle( s , c->progeny[k] );
                space_recycle( s , c->progeny[k] );
                c->progeny[k] = NULL;
                }
    
    }

/**
465
 * @brief Recursively rebuild a cell tree.
466
467
468
469
470
471
 *
 */
 
int space_rebuild_recurse ( struct space *s , struct cell *c ) {
    
    int k, count, changes = 0, wasmt[8];
472
    float h, h_limit, h_max = 0.0f, dt_min = c->parts[0].dt, dt_max = dt_min;
473
474
    struct cell *temp;
    
475
476
477
478
479
480
    /* Clean out the task pointers. */
    c->sorts = NULL;
    c->nr_tasks = 0;
    c->nr_density = 0;
    c->dx_max = 0.0;
    
481
482
483
484
485
486
487
488
489
490
491
492
    /* If the cell is already split, check that the split is still ok. */
    if ( c->split ) {
    
        /* Check the depth. */
        if ( c->depth > s->maxdepth )
            s->maxdepth = c->depth;

        /* Set the minimum cutoff. */
        h_limit = fmin( c->h[0] , fmin( c->h[1] , c->h[2] ) ) / 2;

        /* Count the particles below that. */
        for ( count = 0 , k = 0 ; k < c->count ; k++ ) {
493
            h = c->parts[k].h;
494
495
496
497
            if ( h <= h_limit )
                count += 1;
            if ( h > h_max )
                h_max = h;
498
499
500
501
            if ( c->parts[k].dt < dt_min )
                dt_min = c->parts[k].dt;
            if ( c->parts[k].dt > dt_max )
                dt_max = c->parts[k].dt;
502
503
            }
        c->h_max = h_max;
504
505
        c->dt_min = dt_min;
        c->dt_max = dt_max;
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
            
        /* Un-split? */
        if ( count < c->count*space_splitratio || c->count < space_splitsize ) {
        
            /* Get rid of the progeny. */
            space_rebuild_recycle( s , c );
            
            /* Re-set the split flag. */
            c->split = 0;
        
            }
        
        /* Otherwise, recurse on the kids. */
        else {
        
            /* Populate all progeny. */
            for ( k = 0 ; k < 8 ; k++ )
                if ( ( wasmt[k] = ( c->progeny[k] == NULL ) ) ) {
                    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;
                    temp->h_max = 0.0;
                    temp->parent = c;
                    c->progeny[k] = temp;
                    }
        
            /* Make sure each part is in its place. */
            cell_split( c );
            
            /* Remove empty progeny. */
            for ( k = 0 ; k < 8 ; k++ )
                if ( c->progeny[k]->count == 0 ) {
                    changes += !wasmt[k];
                    space_recycle( s , c->progeny[k] );
                    c->progeny[k] = NULL;
                    }
                else
                    changes += wasmt[k];
        
            /* Recurse. */
            for ( k = 0 ; k < 8 ; k++ )
                if ( c->progeny[k] != NULL )
                    changes += space_rebuild_recurse( s , c->progeny[k] );
                    
            }
    
        }
        
    /* Otherwise, try to split it anyway. */
    else {
        space_split( s , c );
        changes += c->split;
        }
        
    /* Return the grand total. */
    return changes;
    
    }

/**
579
 * @brief Re-build the cells as well as the tasks.
580
581
 *
 * @param s The #space in which to update the cells.
582
 * @param cell_max Maximal cell size.
583
584
585
 *
 */
 
586
void space_rebuild ( struct space *s , double cell_max ) {
587

588
    float h_max = s->cell_min, h_min = s->parts[0].h, dmin;
589
    int i, j, k, cdim[3];
590
591
592
593
    struct cell *restrict c;
    struct part *restrict finger, *restrict p;
    struct cpart *restrict cfinger;
    int *ind;
594
    ticks tic;
595
    
596
597
598
    /* Be verbose about this. */
    printf( "space_rebuild: (re)building space...\n" ); fflush(stdout);
    
599
    /* Run through the parts and get the current h_max. */
600
    // tic = getticks();
601
    for ( k = 0 ; k < s->nr_parts ; k++ ) {
602
603
        if ( s->parts[k].h > h_max )
            h_max = s->parts[k].h;
604
605
        else if ( s->parts[k].h < h_min )
            h_min = s->parts[k].h;
606
        }
607
608
    s->h_min = h_min;
    s->h_max = h_max;
609
    printf( "space_rebuild: h_min/h_max is %.3e/%.3e.\n" , h_min , h_max );
610
    // printf( "space_rebuild: getting h_min and h_max took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
611
612
613
    
    /* Get the new putative cell dimensions. */
    for ( k = 0 ; k < 3 ; k++ )
Pedro Gonnet's avatar
Pedro Gonnet committed
614
        cdim[k] = floor( s->dim[k] / fmax( h_max*space_stretch , cell_max ) );
615
616
        
    /* Do we need to re-build the upper-level cells? */
617
    // tic = getticks();
618
619
    if ( s->cells == NULL ||
         cdim[0] < s->cdim[0] || cdim[1] < s->cdim[1] || cdim[2] < s->cdim[2] ) {
620
621
622
    
        /* Free the old cells, if they were allocated. */
        if ( s->cells != NULL ) {
623
            for ( k = 0 ; k < s->nr_cells ; k++ ) {
624
                space_rebuild_recycle( s , &s->cells[k] );
625
626
627
                if ( s->cells[k].sort != NULL )
                    free( s->cells[k].sort );
                }
628
629
630
631
            free( s->cells );
            s->maxdepth = 0;
            }
            
632
        /* Set the new cell dimensions only if smaller. */
633
634
635
636
637
        for ( k = 0 ; k < 3 ; k++ ) {
            s->cdim[k] = cdim[k];
            s->h[k] = s->dim[k] / cdim[k];
            s->ih[k] = 1.0 / s->h[k];
            }
638
        dmin = fminf( s->h[0] , fminf( s->h[1] , s->h[2] ) );
639

640
        /* Allocate the highest level of cells. */
Pedro Gonnet's avatar
Pedro Gonnet committed
641
        s->tot_cells = s->nr_cells = cdim[0] * cdim[1] * cdim[2];
642
643
644
645
646
647
648
649
650
651
652
653
654
655
        if ( posix_memalign( (void *)&s->cells , 64 , s->nr_cells * sizeof(struct cell) ) != 0 )
            error( "Failed to allocate cells." );
        bzero( s->cells , s->nr_cells * sizeof(struct cell) );
        for ( k = 0 ; k < s->nr_cells ; k++ )
            if ( lock_init( &s->cells[k].lock ) != 0 )
                error( "Failed to init spinlock." );

        /* Set the cell location and sizes. */
        for ( i = 0 ; i < cdim[0] ; i++ )
            for ( j = 0 ; j < cdim[1] ; j++ )
                for ( k = 0 ; k < cdim[2] ; k++ ) {
                    c = &s->cells[ cell_getid( cdim , i , j , k ) ];
                    c->loc[0] = i*s->h[0]; c->loc[1] = j*s->h[1]; c->loc[2] = k*s->h[2];
                    c->h[0] = s->h[0]; c->h[1] = s->h[1]; c->h[2] = s->h[2];
656
                    c->dmin = dmin;
657
                    c->depth = 0;
658
                    lock_init( &c->lock );
659
                    }
660
661
662
           
        /* Be verbose about the change. */         
        printf( "space_rebuild: set cell dimensions to [ %i %i %i ].\n" , cdim[0] , cdim[1] , cdim[2] ); fflush(stdout);
663
664
                    
        } /* re-build upper-level cells? */
665
    // printf( "space_rebuild: rebuilding upper-level cells took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
666
        
667
668
        
    /* Run through the particles and get their cell index. */
669
    // tic = getticks();
670
671
    if ( ( ind = (int *)malloc( sizeof(int) * s->nr_parts ) ) == NULL )
        error( "Failed to allocate temporary particle indices." );
672
673
674
    for ( k = 0 ; k < s->nr_cells ; k++ )
        s->cells[ k ].count = 0;
    for ( k = 0 ; k < s->nr_parts ; k++ )  {
675
676
677
678
679
680
681
        p = &s->parts[k];
        for ( j = 0 ; j < 3 ; j++ )
            if ( p->x[j] < 0.0 )
                p->x[j] += s->dim[j];
            else if ( p->x[j] >= s->dim[j] )
                p->x[j] -= s->dim[j];
        ind[k] = cell_getid( s->cdim , p->x[0]*s->ih[0] , p->x[1]*s->ih[1] , p->x[2]*s->ih[2] );
682
683
        s->cells[ ind[k] ].count += 1;
        }
684
    // printf( "space_rebuild: getting particle indices took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
685
686

    /* Sort the parts according to their cells. */
687
    // tic = getticks();
Pedro Gonnet's avatar
Pedro Gonnet committed
688
    parts_sort( s->parts , ind , s->nr_parts , 0 , s->nr_cells );
689
    // printf( "space_rebuild: parts_sort took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
690
691
692
    
    /* We no longer need the indices as of here. */
    free( ind );    
693

694
    /* Store the current positions. */         
695
696
    // tic = getticks();
    #pragma omp parallel for schedule(static)
697
    for ( k = 0 ; k < s->nr_parts ; k++ ) {
698
699
700
        s->parts[k].xtras->x_old[0] = s->parts[k].x[0];
        s->parts[k].xtras->x_old[1] = s->parts[k].x[1];
        s->parts[k].xtras->x_old[2] = s->parts[k].x[2];
701
        }
702
    // printf( "space_rebuild: storing old positions took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
703
704

    /* Hook the cells up to the parts. */
705
    // tic = getticks();
706
707
708
    finger = s->parts;
    cfinger = s->cparts;
    for ( k = 0 ; k < s->nr_cells ; k++ ) {
709
710
711
712
713
714
        c = &s->cells[ k ];
        c->parts = finger;
        c->cparts = cfinger;
        finger = &finger[ c->count ];
        cfinger = &cfinger[ c->count ];
        }
715
    // printf( "space_rebuild: hooking up cells took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
716
717
        
        
718
719
    /* At this point, we have the upper-level cells, old or new. Now make
       sure that the parts in each cell are ok. */
720
    // tic = getticks();
721
    #pragma omp parallel for schedule(dynamic,1) shared(s)
722
    for ( k = 0 ; k < s->nr_cells ; k++ )
723
        space_rebuild_recurse( s , &s->cells[k] );
724
    // printf( "space_rebuild: space_rebuild_recurse took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
725
        
726
727
728
729
730
    /* Now that we have the cell structre, re-build the tasks. */
    tic = getticks();
    space_maketasks( s , 1 );
    printf( "space_rebuild: maketasks took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
    
731
732
733
    }


734
/**
735
 * @brief Sort the particles and condensed particles according to the given indices.
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
 *
 * @param parts The list of #part
 * @param ind The indices with respect to which the parts are sorted.
 * @param N The number of parts
 * @param min Lowest index.
 * @param max highest index.
 *
 * This function calls itself recursively.
 */
 
void parts_sort ( struct part *parts , int *ind , int N , int min , int max ) {

    int pivot = (min + max) / 2;
    int i = 0, j = N-1;
    int temp_i;
    struct part temp_p;
    
753
754
755
756
757
758
    /* If N is small enough, just do insert sort. */
    if ( N < 16 ) {
    
        for ( i = 1 ; i < N ; i++ )
            if ( ind[i] < ind[i-1] ) {
                temp_i = ind[i];
Pedro Gonnet's avatar
Pedro Gonnet committed
759
                temp_p = parts[i];
760
761
762
763
764
765
766
767
                for ( j = i ; j > 0 && ind[j-1] > temp_i ; j-- ) {
                    ind[j] = ind[j-1];
                    parts[j] = parts[j-1];
                    }
                ind[j] = temp_i;
                parts[j] = temp_p;
                }
    
768
769
        }
        
770
771
772
773
774
775
776
777
778
779
780
781
782
    /* Otherwise, recurse with Quicksort. */
    else {
    
        /* One pass of quicksort. */
        while ( i < j ) {
            while ( i < N && ind[i] <= pivot )
                i++;
            while ( j >= 0 && ind[j] > pivot )
                j--;
            if ( i < j ) {
                temp_i = ind[i]; ind[i] = ind[j]; ind[j] = temp_i;
                temp_p = parts[i]; parts[i] = parts[j]; parts[j] = temp_p;
                }
783
            }
784
785

        /* Verify sort. */
Pedro Gonnet's avatar
Pedro Gonnet committed
786
        /* for ( int k = 0 ; k <= j ; k++ )
787
788
789
790
791
792
793
794
            if ( ind[k] > pivot ) {
                printf( "parts_sort: sorting failed at k=%i, ind[k]=%i, pivot=%i, i=%i, j=%i, N=%i.\n" , k , ind[k] , pivot , i , j , N );
                error( "Sorting failed (<=pivot)." );
                }
        for ( int k = j+1 ; k < N ; k++ )
            if ( ind[k] <= pivot ) {
                printf( "parts_sort: sorting failed at k=%i, ind[k]=%i, pivot=%i, i=%i, j=%i, N=%i.\n" , k , ind[k] , pivot , i , j , N );
                error( "Sorting failed (>pivot)." );
Pedro Gonnet's avatar
Pedro Gonnet committed
795
                } */
796

Pedro Gonnet's avatar
Pedro Gonnet committed
797
798
799
        /* Recurse on the left? */
        if ( j > 0  && pivot > min )
            parts_sort( parts , ind , j+1 , min , pivot );
800

Pedro Gonnet's avatar
Pedro Gonnet committed
801
802
803
        /* Recurse on the right? */
        if ( i < N && pivot+1 < max )
            parts_sort( &parts[i], &ind[i], N-i , pivot+1 , max );
804
            
805
806
        }
    
807
808
809
    }


Pedro Gonnet's avatar
Pedro Gonnet committed
810
/**
811
 * @brief Mapping function to free the sorted indices buffers.
Pedro Gonnet's avatar
Pedro Gonnet committed
812
813
814
815
816
817
818
819
820
821
822
823
 */

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

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

    }


824
825
/**
 * @brief Mapping function to append a ghost task to each cell.
Pedro Gonnet's avatar
Pedro Gonnet committed
826
827
828
829
 *
 * 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).
830
831
832
833
834
 */

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

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

Pedro Gonnet's avatar
Pedro Gonnet committed
837
838
839
840
841
842
843
    /* 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
844
    /* Make the ghost task */
Pedro Gonnet's avatar
Pedro Gonnet committed
845
    if ( c->super != c || c->nr_tasks > 0 )
846
        c->ghost = space_addtask( s , task_type_ghost , task_subtype_none , 0 , 0 , c , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
847
848
849
850
851
852
853
854
855

    /* 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 );
    
    }


Pedro Gonnet's avatar
Pedro Gonnet committed
856
857
858
859
/**
 * @brief Map a function to all particles in a aspace.
 *
 * @param s The #space we are working in.
860
861
 * @param fun Function pointer to apply on the cells.
 * @param data Data passed to the function fun.
Pedro Gonnet's avatar
Pedro Gonnet committed
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
 */
 
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. */
886
    #pragma omp parallel for schedule(dynamic,1)
Pedro Gonnet's avatar
Pedro Gonnet committed
887
888
889
890
891
892
893
894
895
896
    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.
897
 * @param full Map to all cells, including cells with sub-cells.
898
899
 * @param fun Function pointer to apply on the cells.
 * @param data Data passed to the function fun.
Pedro Gonnet's avatar
Pedro Gonnet committed
900
901
 */
 
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
void space_map_cells_post ( struct space *s , int full , void (*fun)( struct cell *c , void *data ) , void *data ) {

    int i;

    void rec_map ( struct cell *c ) {
    
        int k;
        
        /* Recurse. */
        if ( c->split )
            for ( k = 0 ; k < 8 ; k++ )
                if ( c->progeny[k] != NULL )
                    rec_map( c->progeny[k] );
                
        /* No progeny? */
        if ( full || !c->split )
            fun( c , data );
                
        }
        
    /* Call the recursive function on all higher-level cells. */
923
    #pragma omp parallel for schedule(dynamic,1)
924
925
926
927
928
929
930
    for ( i = 0 ; i < s->nr_cells ; i++ )
        rec_map( &s->cells[i] );

    }


void space_map_cells_pre ( struct space *s , int full , void (*fun)( struct cell *c , void *data ) , void *data ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
931
932
933
934
935
936
937
938

    int i;

    void rec_map ( struct cell *c ) {
    
        int k;
        
        /* No progeny? */
939
        if ( full || !c->split )
Pedro Gonnet's avatar
Pedro Gonnet committed
940
941
            fun( c , data );
                
942
943
        /* Recurse. */
        if ( c->split )
Pedro Gonnet's avatar
Pedro Gonnet committed
944
945
946
947
948
949
950
            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. */
951
    #pragma omp parallel for schedule(dynamic,1)
Pedro Gonnet's avatar
Pedro Gonnet committed
952
953
954
955
956
957
958
959
960
961
    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.
962
963
964
965
966
967
968
 * @param type The type of the task.
 * @param subtype The sub-type of the task.
 * @param flags The flags of the task.
 * @param wait 
 * @param ci The first cell to interact.
 * @param cj The second cell to interact.
 * @param tight
Pedro Gonnet's avatar
Pedro Gonnet committed
969
970
 */
 
971
struct task *space_addtask ( struct space *s , int type , int subtype , int flags , int wait , struct cell *ci , struct cell *cj , int tight ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
972

973
974
975
976
977
978
979
    struct task *t;
    
    /* Lock the space. */
    lock_lock( &s->lock );
    
    /* Get the next free task. */
    t = &s->tasks[ s->nr_tasks ];
Pedro Gonnet's avatar
Pedro Gonnet committed
980
981
982
    
    /* Copy the data. */
    t->type = type;
983
    t->subtype = subtype;
Pedro Gonnet's avatar
Pedro Gonnet committed
984
985
986
987
    t->flags = flags;
    t->wait = wait;
    t->ci = ci;
    t->cj = cj;
988
    t->skip = 0;
989
    t->tight = tight;
990
991
    t->nr_unlock_tasks = 0;
    t->nr_unlock_cells = 0;
Pedro Gonnet's avatar
Pedro Gonnet committed
992
    
993
994
995
996
997
998
    /* Init the lock. */
    lock_init( &t->lock );
    
    /* Add an index for it. */
    s->tasks_ind[ s->nr_tasks ] = s->nr_tasks;
    
Pedro Gonnet's avatar
Pedro Gonnet committed
999
1000
1001
    /* Increase the task counter. */
    s->nr_tasks += 1;
    
1002
1003
1004
    /* Unock the space. */
    lock_unlock_blind( &s->lock );
    
Pedro Gonnet's avatar
Pedro Gonnet committed
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
    /* 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 ) {

1020
    int j, k, sid, tid;
Pedro Gonnet's avatar
Pedro Gonnet committed
1021
1022
1023
    struct cell *ci, *cj;
    double hi, hj, shift[3];
    struct task *t;
1024
    // float dt_step = s->dt_step;
1025
1026
1027
1028
1029
    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 } ,
1030
1031
                      { -1 , -1 , -1 , -1 , -1 , -1 , 11 , 10 } ,
                      { -1 , -1 , -1 , -1 , -1 , -1 , -1 , 12 } };
Pedro Gonnet's avatar
Pedro Gonnet committed
1032
1033
1034
1035
1036
1037

    /* Loop through the tasks... */
    for ( tid = 0 ; tid < s->nr_tasks ; tid++ ) {
    
        /* Get a pointer on the task. */
        t = &s->tasks[tid];
1038
        
1039
1040
1041
1042
1043
1044
1045
        /* Empty task? */
        if ( t->ci == NULL || ( t->type == task_type_pair && t->cj == NULL ) ) {
            t->type = task_type_none;
            t->skip = 1;
            continue;
            }
        
1046
1047
1048
1049
1050
        /* Self-interaction? */
        if ( t->type == task_type_self ) {
        
            /* Get a handle on the cell involved. */
            ci = t->ci;
Pedro Gonnet's avatar
Pedro Gonnet committed
1051
            
1052
            /* Ingore this task? */
1053
            /* if ( ci->dt_min > dt_step ) {
1054
                t->skip = 1;
1055
                continue;
1056
                } */
1057
            
1058
1059
1060
            /* Is this cell even split? */
            if ( !ci->split )
                continue;
Pedro Gonnet's avatar
Pedro Gonnet committed
1061
            
1062
            /* Make a sub? */
1063
            if ( space_dosub && ci->count < space_subsize ) {
1064
1065
1066
1067
1068
1069
            
                /* convert to a self-subtask. */
                t->type = task_type_sub;
                
                /* Wait for this tasks sorts, as we will now have pairwise
                   components in this sub. */
1070
                space_addsorts( s , t , ci , NULL , -1 );
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
            
                }
                
            /* Otherwise, make tasks explicitly. */
            else {
            
                /* Take a step back (we're going to recycle the current task)... */
                tid -= 1;

                /* Add the self taks. */
                for ( k = 0 ; ci->progeny[k] == NULL ; k++ );
                t->ci = ci->progeny[k];
                for ( k += 1 ; k < 8 ; k++ )
                    if ( ci->progeny[k] != NULL )
1085
                        space_addtask( s , task_type_self , task_subtype_density , 0 , 0 , ci->progeny[k] , NULL , 0 );
1086
1087
1088
            
                /* Make a task for each pair of progeny. */
                for ( j = 0 ; j < 8 ; j++ )
1089
                    if ( ci->progeny[j] != NULL )
1090
                        for ( k = j + 1 ; k < 8 ; k++ )
1091
                            if ( ci->progeny[k] != NULL )
1092
                                space_addtask( s , task_type_pair , task_subtype_density , pts[j][k] , 0 , ci->progeny[j] , ci->progeny[k] , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
1093
                }
1094
1095
1096
1097
1098
1099
1100
1101
1102
        
            }
    
        /* Pair interaction? */
        else if ( t->type == task_type_pair ) {
            
            /* Get a handle on the cells involved. */
            ci = t->ci;
            cj = t->cj;
1103
1104
            hi = ci->dmin;
            hj = cj->dmin;
1105

1106
            /* Ingore this task? */
1107
            /* if ( ci->dt_min > dt_step && cj->dt_min > dt_step ) {
1108
                t->skip = 1;
1109
                continue;
1110
                } */
1111
1112
1113
1114
1115
            
            /* Get the sort ID, use space_getsid and not t->flags
               to make sure we get ci and cj swapped if needed. */
            sid = space_getsid( s , &ci , &cj , shift );
                
1116
1117
            /* Should this task be split-up? */
            if ( ci->split && cj->split &&
1118
                 ci->h_max < hi/2 && cj->h_max < hj/2 ) {
1119
1120
                 
                /* Replace by a single sub-task? */
1121
1122
                if ( space_dosub &&
                     ci->count < space_subsize && cj->count < space_subsize &&
1123
                     sid != 0 && sid != 2 && sid != 6 && sid != 8 ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
1124
                
1125
1126
1127
                    /* Make this task a sub task. */
                    t->type = task_type_sub;
                    t->flags = sid;
1128
                    t->ci = ci; t->cj = cj;
Pedro Gonnet's avatar
Pedro Gonnet committed
1129
                    
1130
1131
                    /* Create the sorts recursively. */
                    space_addsorts( s , t , ci , cj , sid );
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
                    
                    /* Don't go any further. */
                    continue;
                
                    }

                /* Take a step back (we're going to recycle the current task)... */
                tid -= 1;

                /* For each different sorting type... */
                switch ( sid ) {

                    case 0: /* (  1 ,  1 ,  1 ) */
1145
                        t->ci = ci->progeny[7]; t->cj = cj->progeny[0]; t->flags = 0;
1146
1147
1148
                        break;

                    case 1: /* (  1 ,  1 ,  0 ) */
1149
1150
1151
1152
                        t->ci = ci->progeny[6]; t->cj = cj->progeny[0]; t->flags = 1; t->tight = 1;
                        t = space_addtask( s , task_type_pair , t->subtype , 1 , 0 , ci->progeny[7] , cj->progeny[1] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[6] , cj->progeny[1] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 2 , 0 , ci->progeny[7] , cj->progeny[0] , 1 );
1153
1154
1155
                        break;

                    case 2: /* (  1 ,  1 , -1 ) */
1156
                        t->ci = ci->progeny[6]; t->cj = cj->progeny[1]; t->flags = 2; t->tight = 1;
1157
1158
1159
                        break;

                    case 3: /* (  1 ,  0 ,  1 ) */
1160
1161
1162
1163
                        t->ci = ci->progeny[5]; t->cj = cj->progeny[0]; t->flags = 3; t->tight = 1;
                        t = space_addtask( s , task_type_pair , t->subtype , 3 , 0 , ci->progeny[7] , cj->progeny[2] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[5] , cj->progeny[2] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 6 , 0 , ci->progeny[7] , cj->progeny[0] , 1 );
1164
1165
1166
                        break;

                    case 4: /* (  1 ,  0 ,  0 ) */
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
                        t->ci = ci->progeny[4]; t->cj = cj->progeny[0]; t->flags = 4; t->tight = 1;
                        t = space_addtask( s , task_type_pair , t->subtype , 5 , 0 , ci->progeny[5] , cj->progeny[0] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 7 , 0 , ci->progeny[6] , cj->progeny[0] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 8 , 0 , ci->progeny[7] , cj->progeny[0] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 3 , 0 , ci->progeny[4] , cj->progeny[1] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 4 , 0 , ci->progeny[5] , cj->progeny[1] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 6 , 0 , ci->progeny[6] , cj->progeny[1] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 7 , 0 , ci->progeny[7] , cj->progeny[1] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 1 , 0 , ci->progeny[4] , cj->progeny[2] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 2 , 0 , ci->progeny[5] , cj->progeny[2] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 4 , 0 , ci->progeny[6] , cj->progeny[2] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 5 , 0 , ci->progeny[7] , cj->progeny[2] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[4] , cj->progeny[3] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 1 , 0 , ci->progeny[5] , cj->progeny[3] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 3 , 0 , ci->progeny[6] , cj->progeny[3] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 4 , 0 , ci->progeny[7] , cj->progeny[3] , 1 );
1183
1184
1185
                        break;

                    case 5: /* (  1 ,  0 , -1 ) */
1186
1187
1188
1189
                        t->ci = ci->progeny[4]; t->cj = cj->progeny[1]; t->flags = 5; t->tight = 1;
                        t = space_addtask( s , task_type_pair , t->subtype , 5 , 0 , ci->progeny[6] , cj->progeny[3] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 2 , 0 , ci->progeny[4] , cj->progeny[3] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 8 , 0 , ci->progeny[6] , cj->progeny[1] , 1 );
1190
1191
1192
                        break;

                    case 6: /* (  1 , -1 ,  1 ) */
1193
                        t->ci = ci->progeny[5]; t->cj = cj->progeny[2]; t->flags = 6; t->tight = 1;
1194
1195
1196
                        break;

                    case 7: /* (  1 , -1 ,  0 ) */
1197
1198
1199
1200
                        t->ci = ci->progeny[4]; t->cj = cj->progeny[3]; t->flags = 6; t->tight = 1;
                        t = space_addtask( s , task_type_pair , t->subtype , 8 , 0 , ci->progeny[5] , cj->progeny[2] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 7 , 0 , ci->progeny[4] , cj->progeny[2] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 7 , 0 , ci->progeny[5] , cj->progeny[3] , 1 );
1201
1202
1203
                        break;

                    case 8: /* (  1 , -1 , -1 ) */
1204
                        t->ci = ci->progeny[4]; t->cj = cj->progeny[3]; t->flags = 8; t->tight = 1;
1205
1206
1207
                        break;

                    case 9: /* (  0 ,  1 ,  1 ) */
1208
1209
1210
1211
                        t->ci = ci->progeny[3]; t->cj = cj->progeny[0]; t->flags = 9; t->tight = 1;
                        t = space_addtask( s , task_type_pair , t->subtype , 9 , 0 , ci->progeny[7] , cj->progeny[4] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[3] , cj->progeny[4] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 8 , 0 , ci->progeny[7] , cj->progeny[0] , 1 );
1212
1213
1214
                        break;

                    case 10: /* (  0 ,  1 ,  0 ) */
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
                        t->ci = ci->progeny[2]; t->cj = cj->progeny[0]; t->flags = 10; t->tight = 1;
                        t = space_addtask( s , task_type_pair , t->subtype , 11 , 0 , ci->progeny[3] , cj->progeny[0] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 7 , 0 , ci->progeny[6] , cj->progeny[0] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 6 , 0 , ci->progeny[7] , cj->progeny[0] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 9 , 0 , ci->progeny[2] , cj->progeny[1] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 10 , 0 , ci->progeny[3] , cj->progeny[1] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 8 , 0 , ci->progeny[6] , cj->progeny[1] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 7 , 0 , ci->progeny[7] , cj->progeny[1] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 1 , 0 , ci->progeny[2] , cj->progeny[4] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 2 , 0 , ci->progeny[3] , cj->progeny[4] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 10 , 0 , ci->progeny[6] , cj->progeny[4] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 11 , 0 , ci->progeny[7] , cj->progeny[4] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[2] , cj->progeny[5] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 1 , 0 , ci->progeny[3] , cj->progeny[5] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 9 , 0 , ci->progeny[6] , cj->progeny[5] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 10 , 0 , ci->progeny[7] , cj->progeny[5] , 1 );
1231
1232
1233
                        break;

                    case 11: /* (  0 ,  1 , -1 ) */
1234
1235
1236
1237
                        t->ci = ci->progeny[2]; t->cj = cj->progeny[1]; t->flags = 11; t->tight = 1;
                        t = space_addtask( s , task_type_pair , t->subtype , 11 , 0 , ci->progeny[6] , cj->progeny[5] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 2 , 0 , ci->progeny[2] , cj->progeny[5] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 6 , 0 , ci->progeny[6] , cj->progeny[1] , 1 );
1238
1239
1240
                        break;

                    case 12: /* (  0 ,  0 ,  1 ) */
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
                        t->ci = ci->progeny[1]; t->cj = cj->progeny[0]; t->flags = 12; t->tight = 1;
                        t = space_addtask( s , task_type_pair , t->subtype , 11 , 0 , ci->progeny[3] , cj->progeny[0] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 5 , 0 , ci->progeny[5] , cj->progeny[0] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 2 , 0 , ci->progeny[7] , cj->progeny[0] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 9 , 0 , ci->progeny[1] , cj->progeny[2] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 12 , 0 , ci->progeny[3] , cj->progeny[2] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 8 , 0 , ci->progeny[5] , cj->progeny[2] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 5 , 0 , ci->progeny[7] , cj->progeny[2] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 3 , 0 , ci->progeny[1] , cj->progeny[4] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 6 , 0 , ci->progeny[3] , cj->progeny[4] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 12 , 0 , ci->progeny[5] , cj->progeny[4] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 11 , 0 , ci->progeny[7] , cj->progeny[4] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[1] , cj->progeny[6] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 3 , 0 , ci->progeny[3] , cj->progeny[6] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 9 , 0 , ci->progeny[5] , cj->progeny[6] , 1 );
                        t = space_addtask( s , task_type_pair , t->subtype , 12 , 0 , ci->progeny[7] , cj->progeny[6] , 1 );
1257
1258
1259
1260
1261
                        break;

                    }

                } /* split this task? */
Pedro Gonnet's avatar
Pedro Gonnet committed
1262
                
1263
1264
1265
1266
            /* Otherwise, if not spilt, stitch-up the sorting. */
            else {
            
                /* Create the sort for ci. */