space.c 62 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
#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
78
79

/* 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
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
/**
 * @breif Recursively dismantle a cell tree.
 *
 */
 
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;
                }
    
    }

/**
 * @breif Recursively rebuild a cell tree.
 *
 */
 
int space_rebuild_recurse ( struct space *s , struct cell *c ) {
    
    int k, count, changes = 0, wasmt[8];
    float h, h_limit, h_max = 0.0f;
    struct cell *temp;
    
    /* 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++ ) {
122
            h = c->cparts[k].h;
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
            if ( h <= h_limit )
                count += 1;
            if ( h > h_max )
                h_max = h;
            }
        c->h_max = h_max;
            
        /* 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;
    
    }

/**
 * @breif Re-build the cells as well as the tasks.
 *
 * @param s The #space in which to update the cells.
 * @param force Flag to force re-building the cells and tasks.
 *
 * @return 1 if changes to the cells and/or tasks were made.
 */
 
Pedro Gonnet's avatar
Pedro Gonnet committed
210
int space_rebuild ( struct space *s , int force , double cell_max ) {
211

212
    float h_max = s->parts[0].h, h_min = s->parts[0].h;
213
214
    int i, j, k, cdim[3];
    struct cell *c;
215
    struct part *finger;
216
    struct cpart *cfinger;
217
    int *ind, changes = 0;
218
219
    
    /* Run through the parts and get the current h_max. */
220
    for ( k = 0 ; k < s->nr_parts ; k++ ) {
221
222
        if ( s->parts[k].h > h_max )
            h_max = s->parts[k].h;
223
224
        else if ( s->parts[k].h < h_min )
            h_min = s->parts[k].h;
225
        }
226
227
    s->h_min = h_min;
    s->h_max = h_max;
228
229
230
    
    /* Get the new putative cell dimensions. */
    for ( k = 0 ; k < 3 ; k++ )
Pedro Gonnet's avatar
Pedro Gonnet committed
231
        cdim[k] = floor( s->dim[k] / fmax( h_max*space_stretch , cell_max ) );
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
        
    /* Do we need to re-build the upper-level cells? */
    if ( force || cdim[0] < s->cdim[0] || cdim[1] < s->cdim[1] || cdim[2] < s->cdim[2] ) {
    
        /* Free the old cells, if they were allocated. */
        if ( s->cells != NULL ) {
            for ( k = 0 ; k < s->nr_cells ; k++ )
                space_rebuild_recycle( s , &s->cells[k] );
            free( s->cells );
            s->maxdepth = 0;
            }
            
        /* Set the new cell dimensions. */
        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];
            }
    
        /* Allocate the highest level of cells. */
Pedro Gonnet's avatar
Pedro Gonnet committed
252
        s->tot_cells = s->nr_cells = cdim[0] * cdim[1] * cdim[2];
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
        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];
                    c->depth = 0;
                    }
                    
        /* There were massive changes. */
        changes = 1;
        
        } /* re-build upper-level cells? */
        
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
        
    /* Run through the particles and get their cell index. */
    ind = (int *)alloca( sizeof(int) * s->nr_parts );
    for ( k = 0 ; k < s->nr_cells ; k++ )
        s->cells[ k ].count = 0;
    for ( k = 0 ; k < s->nr_parts ; k++ )  {
        ind[k] = cell_getid( s->cdim , s->parts[k].x[0]*s->ih[0] , s->parts[k].x[1]*s->ih[1] , s->parts[k].x[2]*s->ih[2] );
        s->cells[ ind[k] ].count += 1;
        }

    /* Sort the parts according to their cells. */
    parts_sort( s->parts , ind , s->nr_parts , 0 , s->nr_cells );        

    /* Update the condensed particle data. */         
    for ( k = 0 ; k < s->nr_parts ; k++ ) {
        s->cparts[k].x[0] = s->parts[k].x[0];
        s->cparts[k].x[1] = s->parts[k].x[1];
        s->cparts[k].x[2] = s->parts[k].x[2];
        s->cparts[k].h = s->parts[k].h;
        s->cparts[k].dt = s->parts[k].dt;
        }

    /* Hook the cells up to the parts. */
    for ( finger = s->parts , cfinger = s->cparts , k = 0 ; k < s->nr_cells ; k++ ) {
        c = &s->cells[ k ];
        c->parts = finger;
        c->cparts = cfinger;
        finger = &finger[ c->count ];
        cfinger = &cfinger[ c->count ];
        }
        
        
307
308
    /* At this point, we have the upper-level cells, old or new. Now make
       sure that the parts in each cell are ok. */
309
    #pragma omp parallel for shared(s) reduction(+:changes)
310
311
312
313
314
315
316
317
318
319
320
321
322
    for ( k = 0 ; k < s->nr_cells ; k++ )
        changes += space_rebuild_recurse( s , &s->cells[k] );
        
    /* Now that we have the cell structre, re-build the tasks. */
    if ( changes )
        space_maketasks( s , 1 );
    
    /* Return the number of changes. */
    return changes;

    }


323
/**
324
 * @brief Sort the particles and condensed particles according to the given indices.
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
 *
 * @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;
    
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
    /* 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];
                temp_p = parts[j];
                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;
                }
    
357
358
        }
        
359
360
361
362
363
364
365
366
367
368
369
370
371
    /* 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;
                }
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

        /* Verify sort. */
        for ( int k = 0 ; k <= j ; 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)." );
                }
        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)." );
                }

        /* Try to recurse in parallel. */
        if ( N < 100 ) {

            /* Recurse on the left? */
            if ( j > 0 && pivot > min )
                parts_sort( parts , ind , j+1 , min , pivot );

            /* Recurse on the right? */
            if ( i < N && pivot+1 < max )
                parts_sort( &parts[i], &ind[i], N-i , pivot+1 , max );

397
            }
398

399
400
401
        else
        #pragma omp parallel sections
        {
402

403
404
405
406
            /* Recurse on the left? */
            #pragma omp section
            if ( j > 0 && pivot > min )
                parts_sort( parts , ind , j+1 , min , pivot );
407

408
409
410
411
            /* Recurse on the right? */
            #pragma omp section
            if ( i < N && pivot+1 < max )
                parts_sort( &parts[i], &ind[i], N-i , pivot+1 , max );
412

413
414
            }
            
415
416
        }
    
417
418
419
    }


Pedro Gonnet's avatar
Pedro Gonnet committed
420
/**
421
 * @brief Mapping function to free the sorted indices buffers.
Pedro Gonnet's avatar
Pedro Gonnet committed
422
423
424
425
426
427
428
429
430
431
432
433
 */

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

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

    }


434
435
/**
 * @brief Mapping function to append a ghost task to each cell.
Pedro Gonnet's avatar
Pedro Gonnet committed
436
437
438
439
 *
 * 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).
440
441
442
443
444
 */

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

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

Pedro Gonnet's avatar
Pedro Gonnet committed
447
448
449
450
451
452
453
    /* 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
454
    /* Make the ghost task */
Pedro Gonnet's avatar
Pedro Gonnet committed
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
    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;
473
    c->nr_density = 0;
474
475
476
477

    }


Pedro Gonnet's avatar
Pedro Gonnet committed
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/**
 * @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.
516
 * @param full Map to all cells, including cells with sub-cells.
Pedro Gonnet's avatar
Pedro Gonnet committed
517
518
 */
 
519
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
520
521
522
523
524
525
526
527

    int i;

    void rec_map ( struct cell *c ) {
    
        int k;
        
        /* No progeny? */
528
        if ( full || !c->split )
Pedro Gonnet's avatar
Pedro Gonnet committed
529
530
            fun( c , data );
                
531
532
        /* Recurse. */
        if ( c->split )
Pedro Gonnet's avatar
Pedro Gonnet committed
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
            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.
 */
 
552
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
553

554
555
556
557
558
559
560
    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
561
562
563
    
    /* Copy the data. */
    t->type = type;
564
    t->subtype = subtype;
Pedro Gonnet's avatar
Pedro Gonnet committed
565
566
567
568
569
570
571
572
573
574
575
576
577
578
    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;
    
579
580
581
    /* Unock the space. */
    lock_unlock_blind( &s->lock );
    
Pedro Gonnet's avatar
Pedro Gonnet committed
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
    /* 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 ) {

597
    int j, k, sid, tid;
Pedro Gonnet's avatar
Pedro Gonnet committed
598
599
600
    struct cell *ci, *cj;
    double hi, hj, shift[3];
    struct task *t;
601
602
603
604
605
606
607
    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 } };
Pedro Gonnet's avatar
Pedro Gonnet committed
608
609
610
611
612
613

    /* Loop through the tasks... */
    for ( tid = 0 ; tid < s->nr_tasks ; tid++ ) {
    
        /* Get a pointer on the task. */
        t = &s->tasks[tid];
614
615
616
617
618
619
        
        /* 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
620
            
621
622
623
            /* Is this cell even split? */
            if ( !ci->split )
                continue;
Pedro Gonnet's avatar
Pedro Gonnet committed
624
            
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
            /* Make a sub? */
            if ( ci->count < space_subsize ) {
            
                /* 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. */
                for ( k = 0 ; k < 14 ; k++ )
                    if ( k == 0 || ci->sorts[k] != ci->sorts[k-1] )
                        task_addunlock( ci->sorts[k] , t );
            
                }
                
            /* 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 )
                        t = space_addtask( s , task_type_self , task_subtype_density , 0 , 0 , ci->progeny[k] , NULL , NULL , 0 , NULL , 0 );
            
                /* Make a task for each pair of progeny. */
                for ( j = 0 ; j < 8 ; j++ )
                    if ( ci->progeny[j] != NULL && ci->progeny[j]->count > 0 )
                        for ( k = j + 1 ; k < 8 ; k++ )
                            if ( ci->progeny[k] != NULL && ci->progeny[k]->count > 0 ) {
                                t = space_addtask( s , task_type_pair , task_subtype_density , 0 , 0 , ci->progeny[j] , ci->progeny[k] , NULL , 0 , NULL , 0 );
                                task_addunlock( ci->progeny[j]->sorts[ pts[j][k] ] , t );
                                task_addunlock( ci->progeny[k]->sorts[ pts[j][k] ] , t );
                                ci->progeny[k]->nr_pairs += 1;
                                ci->progeny[j]->nr_pairs += 1;
                                }
Pedro Gonnet's avatar
Pedro Gonnet committed
663
                }
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
        
            }
    
        /* Pair interaction? */
        else if ( t->type == task_type_pair ) {
            
            /* 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? */
            if ( ci->split && cj->split &&
                 ci->h_max*space_stretch < hi/2 && cj->h_max*space_stretch < hj/2 ) {
                 
                /* 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;
                    }
Pedro Gonnet's avatar
Pedro Gonnet committed
689

690
691
692
                /* 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 );
Pedro Gonnet's avatar
Pedro Gonnet committed
693

694
695
696
697
698
699
700
701
702
703
704
705
                /* Flip? */
                if ( sid < 13 ) {
                    cj = t->ci;
                    ci = t->cj;
                    t->ci = ci; t->cj = cj;
                    }
                else
                    sid = 26 - sid;

                /* Replace by a single sub-task? */
                if ( ci->count < space_subsize && cj->count < space_subsize &&
                     sid != 0 && sid != 2 && sid != 6 && sid != 8 ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
706
                
707
708
709
                    /* Make this task a sub task. */
                    t->type = task_type_sub;
                    t->flags = sid;
Pedro Gonnet's avatar
Pedro Gonnet committed
710
                    
711
712
713
714
715
716
717
718
                    /* Make it depend on all the sorts of its sub-cells. */
                    for ( j = 0 ; j < 8 ; j++ ) {
                        if ( ci->progeny[j] != NULL )
                            for ( k = 0 ; k < 14 ; k++ )
                                task_addunlock( ci->progeny[j]->sorts[k] , t );
                        if ( cj->progeny[j] != NULL )
                            for ( k = 0 ; k < 14 ; k++ )
                                task_addunlock( cj->progeny[j]->sorts[k] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
719
                        }
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
                    
                    /* Don't go any further. */
                    continue;
                
                    }

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

                /* 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 ) */
Pedro Gonnet's avatar
Pedro Gonnet committed
747
748
                        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 );
Pedro Gonnet's avatar
Pedro Gonnet committed
749
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
750
                        task_addunlock( ci->progeny[7]->sorts[1] , t ); task_addunlock( cj->progeny[1]->sorts[1] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
751
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[6] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
752
                        task_addunlock( ci->progeny[6]->sorts[0] , t ); task_addunlock( cj->progeny[1]->sorts[0] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
753
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
754
                        task_addunlock( ci->progeny[7]->sorts[2] , t ); task_addunlock( cj->progeny[0]->sorts[2] , t );
755
756
757
758
759
760
761
762
763
764
765
766
767
768
                        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 ) */
Pedro Gonnet's avatar
Pedro Gonnet committed
769
770
                        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 );
Pedro Gonnet's avatar
Pedro Gonnet committed
771
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
772
                        task_addunlock( ci->progeny[7]->sorts[3] , t ); task_addunlock( cj->progeny[2]->sorts[3] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
773
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[5] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
774
                        task_addunlock( ci->progeny[5]->sorts[0] , t ); task_addunlock( cj->progeny[2]->sorts[0] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
775
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
776
                        task_addunlock( ci->progeny[7]->sorts[6] , t ); task_addunlock( cj->progeny[0]->sorts[6] , t );
777
778
779
780
781
782
783
                        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 ) */
Pedro Gonnet's avatar
Pedro Gonnet committed
784
785
                        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 );
Pedro Gonnet's avatar
Pedro Gonnet committed
786
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[5] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
787
                        task_addunlock( ci->progeny[5]->sorts[5] , t ); task_addunlock( cj->progeny[0]->sorts[5] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
788
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[6] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
789
                        task_addunlock( ci->progeny[6]->sorts[7] , t ); task_addunlock( cj->progeny[0]->sorts[7] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
790
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
791
                        task_addunlock( ci->progeny[7]->sorts[8] , t ); task_addunlock( cj->progeny[0]->sorts[8] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
792
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[4] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
793
                        task_addunlock( ci->progeny[4]->sorts[3] , t ); task_addunlock( cj->progeny[1]->sorts[3] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
794
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[5] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
795
                        task_addunlock( ci->progeny[5]->sorts[4] , t ); task_addunlock( cj->progeny[1]->sorts[4] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
796
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[6] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
797
                        task_addunlock( ci->progeny[6]->sorts[6] , t ); task_addunlock( cj->progeny[1]->sorts[6] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
798
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
799
                        task_addunlock( ci->progeny[7]->sorts[7] , t ); task_addunlock( cj->progeny[1]->sorts[7] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
800
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[4] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
801
                        task_addunlock( ci->progeny[4]->sorts[1] , t ); task_addunlock( cj->progeny[2]->sorts[1] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
802
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[5] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
803
                        task_addunlock( ci->progeny[5]->sorts[2] , t ); task_addunlock( cj->progeny[2]->sorts[2] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
804
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[6] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
805
                        task_addunlock( ci->progeny[6]->sorts[4] , t ); task_addunlock( cj->progeny[2]->sorts[4] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
806
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
807
                        task_addunlock( ci->progeny[7]->sorts[5] , t ); task_addunlock( cj->progeny[2]->sorts[5] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
808
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[4] , cj->progeny[3] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
809
                        task_addunlock( ci->progeny[4]->sorts[0] , t ); task_addunlock( cj->progeny[3]->sorts[0] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
810
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[5] , cj->progeny[3] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
811
                        task_addunlock( ci->progeny[5]->sorts[1] , t ); task_addunlock( cj->progeny[3]->sorts[1] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
812
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[6] , cj->progeny[3] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
813
                        task_addunlock( ci->progeny[6]->sorts[3] , t ); task_addunlock( cj->progeny[3]->sorts[3] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
814
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[3] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
815
                        task_addunlock( ci->progeny[7]->sorts[4] , t ); task_addunlock( cj->progeny[3]->sorts[4] , t );
816
817
818
819
820
821
822
823
824
825
826
                        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 ) */
Pedro Gonnet's avatar
Pedro Gonnet committed
827
828
                        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 );
Pedro Gonnet's avatar
Pedro Gonnet committed
829
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[6] , cj->progeny[3] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
830
                        task_addunlock( ci->progeny[6]->sorts[5] , t ); task_addunlock( cj->progeny[3]->sorts[5] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
831
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[4] , cj->progeny[3] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
832
                        task_addunlock( ci->progeny[4]->sorts[2] , t ); task_addunlock( cj->progeny[3]->sorts[2] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
833
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[6] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
834
                        task_addunlock( ci->progeny[6]->sorts[8] , t ); task_addunlock( cj->progeny[1]->sorts[8] , t );
835
836
837
838
839
840
841
842
843
844
845
846
847
848
                        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 ) */
Pedro Gonnet's avatar
Pedro Gonnet committed
849
850
                        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 );
Pedro Gonnet's avatar
Pedro Gonnet committed
851
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[5] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
852
                        task_addunlock( ci->progeny[5]->sorts[8] , t ); task_addunlock( cj->progeny[2]->sorts[8] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
853
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[4] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
854
                        task_addunlock( ci->progeny[4]->sorts[7] , t ); task_addunlock( cj->progeny[2]->sorts[7] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
855
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[5] , cj->progeny[3] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
856
                        task_addunlock( ci->progeny[5]->sorts[7] , t ); task_addunlock( cj->progeny[3]->sorts[7] , t );
857
858
859
860
861
862
863
864
865
866
867
868
869
870
                        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 ) */
Pedro Gonnet's avatar
Pedro Gonnet committed
871
872
                        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 );
Pedro Gonnet's avatar
Pedro Gonnet committed
873
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
874
                        task_addunlock( ci->progeny[7]->sorts[9] , t ); task_addunlock( cj->progeny[4]->sorts[9] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
875
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[3] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
876
                        task_addunlock( ci->progeny[3]->sorts[0] , t ); task_addunlock( cj->progeny[4]->sorts[0] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
877
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
878
                        task_addunlock( ci->progeny[7]->sorts[8] , t ); task_addunlock( cj->progeny[0]->sorts[8] , t );
879
880
881
882
883
884
885
                        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
886
887
                        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 );
Pedro Gonnet's avatar
Pedro Gonnet committed
888
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[3] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
889
                        task_addunlock( ci->progeny[3]->sorts[11] , t ); task_addunlock( cj->progeny[0]->sorts[11] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
890
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[6] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
891
                        task_addunlock( ci->progeny[6]->sorts[7] , t ); task_addunlock( cj->progeny[0]->sorts[7] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
892
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
893
                        task_addunlock( ci->progeny[7]->sorts[6] , t ); task_addunlock( cj->progeny[0]->sorts[6] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
894
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[2] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
895
                        task_addunlock( ci->progeny[2]->sorts[9] , t ); task_addunlock( cj->progeny[1]->sorts[9] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
896
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[3] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
897
                        task_addunlock( ci->progeny[3]->sorts[10] , t ); task_addunlock( cj->progeny[1]->sorts[10] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
898
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[6] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
899
                        task_addunlock( ci->progeny[6]->sorts[8] , t ); task_addunlock( cj->progeny[1]->sorts[8] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
900
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
901
                        task_addunlock( ci->progeny[7]->sorts[7] , t ); task_addunlock( cj->progeny[1]->sorts[7] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
902
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[2] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
903
                        task_addunlock( ci->progeny[2]->sorts[1] , t ); task_addunlock( cj->progeny[4]->sorts[1] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
904
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[3] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
905
                        task_addunlock( ci->progeny[3]->sorts[2] , t ); task_addunlock( cj->progeny[4]->sorts[2] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
906
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[6] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
907
                        task_addunlock( ci->progeny[6]->sorts[10] , t ); task_addunlock( cj->progeny[4]->sorts[10] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
908
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
909
                        task_addunlock( ci->progeny[7]->sorts[11] , t ); task_addunlock( cj->progeny[4]->sorts[11] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
910
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[2] , cj->progeny[5] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
911
                        task_addunlock( ci->progeny[2]->sorts[0] , t ); task_addunlock( cj->progeny[5]->sorts[0] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
912
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[3] , cj->progeny[5] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
913
                        task_addunlock( ci->progeny[3]->sorts[1] , t ); task_addunlock( cj->progeny[5]->sorts[1] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
914
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[6] , cj->progeny[5] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
915
                        task_addunlock( ci->progeny[6]->sorts[9] , t ); task_addunlock( cj->progeny[5]->sorts[9] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
916
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[5] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
917
                        task_addunlock( ci->progeny[7]->sorts[10] , t ); task_addunlock( cj->progeny[5]->sorts[10] , t );
918
919
920
921
922
923
924
925
926
927
928
                        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 ) */
Pedro Gonnet's avatar
Pedro Gonnet committed
929
930
                        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 );
Pedro Gonnet's avatar
Pedro Gonnet committed
931
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[6] , cj->progeny[5] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
932
                        task_addunlock( ci->progeny[6]->sorts[11] , t ); task_addunlock( cj->progeny[5]->sorts[11] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
933
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[2] , cj->progeny[5] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
934
                        task_addunlock( ci->progeny[2]->sorts[2] , t ); task_addunlock( cj->progeny[5]->sorts[2] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
935
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[6] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
936
                        task_addunlock( ci->progeny[6]->sorts[6] , t ); task_addunlock( cj->progeny[1]->sorts[6] , t );
937
938
939
940
941
942
943
                        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 ) */
Pedro Gonnet's avatar
Pedro Gonnet committed
944
945
                        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 );
Pedro Gonnet's avatar
Pedro Gonnet committed
946
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[3] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
947
                        task_addunlock( ci->progeny[3]->sorts[11] , t ); task_addunlock( cj->progeny[0]->sorts[11] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
948
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[5] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
949
                        task_addunlock( ci->progeny[5]->sorts[5] , t ); task_addunlock( cj->progeny[0]->sorts[5] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
950
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
951
                        task_addunlock( ci->progeny[7]->sorts[2] , t ); task_addunlock( cj->progeny[0]->sorts[2] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
952
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[1] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
953
                        task_addunlock( ci->progeny[1]->sorts[9] , t ); task_addunlock( cj->progeny[2]->sorts[9] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
954
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[3] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
955
                        task_addunlock( ci->progeny[3]->sorts[12] , t ); task_addunlock( cj->progeny[2]->sorts[12] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
956
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[5] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
957
                        task_addunlock( ci->progeny[5]->sorts[8] , t ); task_addunlock( cj->progeny[2]->sorts[8] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
958
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
959
                        task_addunlock( ci->progeny[7]->sorts[5] , t ); task_addunlock( cj->progeny[2]->sorts[5] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
960
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[1] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
961
                        task_addunlock( ci->progeny[1]->sorts[3] , t ); task_addunlock( cj->progeny[4]->sorts[3] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
962
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[3] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
963
                        task_addunlock( ci->progeny[3]->sorts[6] , t ); task_addunlock( cj->progeny[4]->sorts[6] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
964
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[5] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
965
                        task_addunlock( ci->progeny[5]->sorts[12] , t ); task_addunlock( cj->progeny[4]->sorts[12] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
966
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
967
                        task_addunlock( ci->progeny[7]->sorts[11] , t ); task_addunlock( cj->progeny[4]->sorts[11] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
968
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[1] , cj->progeny[6] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
969
                        task_addunlock( ci->progeny[1]->sorts[0] , t ); task_addunlock( cj->progeny[6]->sorts[0] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
970
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[3] , cj->progeny[6] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
971
                        task_addunlock( ci->progeny[3]->sorts[3] , t ); task_addunlock( cj->progeny[6]->sorts[3] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
972
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[5] , cj->progeny[6] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
973
                        task_addunlock( ci->progeny[5]->sorts[9] , t ); task_addunlock( cj->progeny[6]->sorts[9] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
974
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[7] , cj->progeny[6] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
975
                        task_addunlock( ci->progeny[7]->sorts[12] , t ); task_addunlock( cj->progeny[6]->sorts[12] , t );
976
977
978
979
980
981
982
983
984
985
986
987
988
                        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;

                    }

                } /* split this task? */
Pedro Gonnet's avatar
Pedro Gonnet committed
989
                
990
            } /* pair interaction? */
Pedro Gonnet's avatar
Pedro Gonnet committed
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
    
        } /* 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;
Pedro Gonnet's avatar
Pedro Gonnet committed
1008
    struct task *t , *t2;
1009
    int counts[task_type_count];
Pedro Gonnet's avatar
Pedro Gonnet committed
1010

1011
1012
    /* Recursive function to generate sorting tasks in the cell tree. */
    void maketasks_sort_rec ( struct cell *c ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
1013

1014
1015
        int j, k;
        struct task *t;
Pedro Gonnet's avatar
Pedro Gonnet committed
1016
1017
1018
1019
1020
1021
1022
1023
1024

        /* Clear the waits on this cell. */
        c->wait = 0;
        
        /* Start by generating the sort task. */
        if ( c->count > 0 ) {
        
            if ( do_sort ) {
                if ( c->count < 1000 ) {
1025
                    t = space_addtask( s , task_type_sort , task_subtype_none , 0x1fff , 0 , c , NULL , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
1026
                    for ( k = 0 ; k < 13 ; k++ )
1027
                        c->sorts[k] = t;
Pedro Gonnet's avatar
Pedro Gonnet committed
1028
1029
                    }
                else if ( c->count < 5000 ) {
1030
                    t = space_addtask( s , task_type_sort , task_subtype_none , 0x7f , 0 , c , NULL , NULL , 0 , NULL , 0 );
1031
                    for ( k = 0 ; k < 7 ; k++ )
1032
1033
                        c->sorts[k] = t;
                    t = space_addtask( s , task_type_sort , task_subtype_none , 0x1f80 , 0 , c , NULL , NULL , 0 , NULL , 0 );
1034
                    for ( k = 7 ; k < 14 ; k++ )
1035
                        c->sorts[k] = t;
Pedro Gonnet's avatar
Pedro Gonnet committed
1036
1037
                    }
                else {
1038
1039
1040
1041
1042
1043
1044
                    c->sorts[0] = c->sorts[1] = space_addtask( s , task_type_sort , task_subtype_none , 0x1 + 0x2 , 0 , c , NULL , NULL , 0 , NULL , 0 );
                    c->sorts[2] = c->sorts[3] = space_addtask( s , task_type_sort , task_subtype_none , 0x4 + 0x8 , 0 , c , NULL , NULL , 0 , NULL , 0 );
                    c->sorts[4] = c->sorts[5] = space_addtask( s , task_type_sort , task_subtype_none , 0x10 + 0x20 , 0 , c , NULL , NULL , 0 , NULL , 0 );
                    c->sorts[6] = c->sorts[7] = space_addtask( s , task_type_sort , task_subtype_none , 0x40 + 0x80 , 0 , c , NULL , NULL , 0 , NULL , 0 );
                    c->sorts[8] = c->sorts[9] = space_addtask( s , task_type_sort , task_subtype_none , 0x100 + 0x200 , 0 , c , NULL , NULL , 0 , NULL , 0 );
                    c->sorts[10] = c->sorts[11] = space_addtask( s , task_type_sort , task_subtype_none , 0x400 + 0x800 , 0 , c , NULL , NULL , 0 , NULL , 0 );
                    c->sorts[12] = c->sorts[13] = space_addtask( s , task_type_sort , task_subtype_none , 0x1000 , 0 , c , NULL , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
1045
1046
1047
1048
1049
1050
1051
1052
                    }
                }

            }
            
        /* Otherwise, add the interactions between progeny. */
        if ( c->split ) {
        
1053
            /* Loop over the progeny. */
Pedro Gonnet's avatar
Pedro Gonnet committed
1054
            for ( k = 0 ; k < 8 ; k++ )
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
                if ( c->progeny[k] != NULL ) {
                    
                    /* Recurse. */
                    maketasks_sort_rec( c->progeny[k] );
                    
                    /* Add dependencies between the sorts. */
                    for ( j = 0 ; j < 14 ; j++ )
                        if ( j == 0 || c->sorts[j] != c->sorts[j-1] )
                            task_addunlock( c->progeny[k]->sorts[j] , c->sorts[j] );
                            
                    }
Pedro Gonnet's avatar
Pedro Gonnet committed
1066
1067
1068
                        
            }

1069
        } /* void maketasks_sort_rec. */
Pedro Gonnet's avatar
Pedro Gonnet committed
1070
        
Pedro Gonnet's avatar
Pedro Gonnet committed
1071
1072
        
    /* Allocate the task-list, if needed. */
Pedro Gonnet's avatar
Pedro Gonnet committed
1073
    if ( s->tasks == NULL || s->tasks_size < s->tot_cells * 43 ) {
1074
1075
1076
1077
        if ( s->tasks != NULL )
            free( s->tasks );
        if ( s->tasks_ind != NULL )
            free( s->tasks_ind );
Pedro Gonnet's avatar
Pedro Gonnet committed
1078
        s->tasks_size = s->tot_cells * 43;
1079
        if ( posix_memalign( (void *)&s->tasks , 64 , sizeof(struct task) * s->tasks_size ) != 0 )
Pedro Gonnet's avatar
Pedro Gonnet committed
1080
            error( "Failed to allocate task list." );
1081
1082
1083
        if ( ( s->tasks_ind = (int *)malloc( sizeof(int) * s->tasks_size ) ) == NULL )
            error( "Failed to allocate task indices." );
        }
Pedro Gonnet's avatar
Pedro Gonnet committed
1084
1085
    s->nr_tasks = 0;
    
1086
    /* Loop over the cells and generate their sorting tasks. */
Pedro Gonnet's avatar
Pedro Gonnet committed
1087
    for ( k = 0 ; k < s->nr_cells ; k++ )
1088
        maketasks_sort_rec( &s->cells[k] );
Pedro Gonnet's avatar
Pedro Gonnet committed
1089
1090
1091
1092
1093
1094
1095
1096

    /* 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;
1097
                space_addtask( s , task_type_self , task_subtype_density , 0 , 0 , &s->cells[cid] , NULL , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
                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;
1118
                            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
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
                            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 );
1130
    
Pedro Gonnet's avatar
Pedro Gonnet committed
1131
1132
1133
1134
1135
1136
1137
1138
1139
    /* 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
1140
                                task_rmunlock_blind( t->ci->progeny[j]->sorts[i] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
1141
1142
1143
1144
1145
1146
                        t->ci->sorts[i] = NULL;
                        }
            t->type = task_type_none;
            }
        }
        
1147
1148
    /* Count the number of tasks associated with each cell and
       store the density tasks in each cell. */
Pedro Gonnet's avatar
Pedro Gonnet committed
1149
    space_map_cells( s , 1 , &space_map_clearnrtasks , NULL );
Pedro Gonnet's avatar
Pedro Gonnet committed
1150
1151
    for ( k = 0 ; k < s->nr_tasks ; k++ ) {
        t = &s->tasks[k];
1152
        if ( t->type == task_type_self ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
1153
            t->ci->nr_tasks += 1;
1154
1155
1156
1157
1158
            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
1159
1160
1161
        else if ( t->type == task_type_pair ) {
            t->ci->nr_tasks += 1;
            t->cj->nr_tasks += 1;
1162
1163
1164
1165
1166
1167
            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
1168
1169
1170
1171
1172
            }
        else if ( t->type == task_type_sub ) {
            t->ci->nr_tasks += 1;
            if ( t->cj != NULL )
                t->cj->