space.c 58.8 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
122
123
124
125
126
127
/**
 * @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;

    }


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
/**
 * @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];
155
    float h, h_limit, h_max = 0.0f, dt_min = c->parts[0].dt, dt_max = dt_min;
156
157
158
159
160
161
162
163
164
165
166
167
168
169
    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++ ) {
170
            h = c->cparts[k].h;
171
172
173
174
            if ( h <= h_limit )
                count += 1;
            if ( h > h_max )
                h_max = h;
175
176
177
178
            if ( c->cparts[k].dt < dt_min )
                dt_min = c->cparts[k].dt;
            if ( c->cparts[k].dt > dt_max )
                dt_max = c->cparts[k].dt;
179
180
            }
        c->h_max = h_max;
181
182
        c->dt_min = dt_min;
        c->dt_max = dt_max;
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
            
        /* 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
264
int space_rebuild ( struct space *s , int force , double cell_max ) {
265

266
    float h_max = s->cell_min, h_min = s->parts[0].h;
267
268
    int i, j, k, cdim[3];
    struct cell *c;
269
    struct part *finger;
270
    struct cpart *cfinger;
271
    int *ind, changes = 0;
272
    // ticks tic;
273
274
    
    /* Run through the parts and get the current h_max. */
275
    // tic = getticks();
276
    for ( k = 0 ; k < s->nr_parts ; k++ ) {
277
278
        if ( s->parts[k].h > h_max )
            h_max = s->parts[k].h;
279
280
        else if ( s->parts[k].h < h_min )
            h_min = s->parts[k].h;
281
        }
282
283
    s->h_min = h_min;
    s->h_max = h_max;
284
    // printf( "space_rebuild: getting h_min and h_max took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
285
286
287
    
    /* Get the new putative cell dimensions. */
    for ( k = 0 ; k < 3 ; k++ )
Pedro Gonnet's avatar
Pedro Gonnet committed
288
        cdim[k] = floor( s->dim[k] / fmax( h_max*space_stretch , cell_max ) );
289
290
        
    /* Do we need to re-build the upper-level cells? */
291
    // tic = getticks();
292
293
294
295
    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 ) {
296
            for ( k = 0 ; k < s->nr_cells ; k++ ) {
297
                space_rebuild_recycle( s , &s->cells[k] );
298
299
300
                if ( s->cells[k].sort != NULL )
                    free( s->cells[k].sort );
                }
301
302
303
304
            free( s->cells );
            s->maxdepth = 0;
            }
            
305
        /* Set the new cell dimensions only if smaller. */
306
307
308
309
310
        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];
            }
311

312
        /* Allocate the highest level of cells. */
Pedro Gonnet's avatar
Pedro Gonnet committed
313
        s->tot_cells = s->nr_cells = cdim[0] * cdim[1] * cdim[2];
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
        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? */
335
    // printf( "space_rebuild: rebuilding upper-level cells took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
336
        
337
338
        
    /* Run through the particles and get their cell index. */
339
    // tic = getticks();
340
341
    if ( ( ind = (int *)malloc( sizeof(int) * s->nr_parts ) ) == NULL )
        error( "Failed to allocate temporary particle indices." );
342
343
344
345
346
347
    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;
        }
348
    // printf( "space_rebuild: getting particle indices took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
349
350

    /* Sort the parts according to their cells. */
351
    // tic = getticks();
352
    parts_sort( s->parts , ind , s->nr_parts , 0 , s->nr_cells );    
353
    // printf( "space_rebuild: parts_sort took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
354
355
356
    
    /* We no longer need the indices as of here. */
    free( ind );    
357
358

    /* Update the condensed particle data. */         
359
360
    // tic = getticks();
    #pragma omp parallel for schedule(static)
361
362
363
364
365
366
367
    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;
        }
368
    // printf( "space_rebuild: creating condensed parts took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
369
370

    /* Hook the cells up to the parts. */
371
    // tic = getticks();
372
373
374
    finger = s->parts;
    cfinger = s->cparts;
    for ( k = 0 ; k < s->nr_cells ; k++ ) {
375
376
377
378
379
380
        c = &s->cells[ k ];
        c->parts = finger;
        c->cparts = cfinger;
        finger = &finger[ c->count ];
        cfinger = &cfinger[ c->count ];
        }
381
    // printf( "space_rebuild: hooking up cells took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
382
383
        
        
384
385
    /* At this point, we have the upper-level cells, old or new. Now make
       sure that the parts in each cell are ok. */
386
387
    // tic = getticks();
    #pragma omp parallel for schedule(dynamic) shared(s) reduction(+:changes)
388
389
    for ( k = 0 ; k < s->nr_cells ; k++ )
        changes += space_rebuild_recurse( s , &s->cells[k] );
390
    // printf( "space_rebuild: space_rebuild_recurse took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
391
392
        
    /* Now that we have the cell structre, re-build the tasks. */
393
    // tic = getticks();
394
395
    if ( changes )
        space_maketasks( s , 1 );
396
    // printf( "space_rebuild: maketasks took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
397
398
399
400
401
402
403
    
    /* Return the number of changes. */
    return changes;

    }


404
/**
405
 * @brief Sort the particles and condensed particles according to the given indices.
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
 *
 * @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;
    
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
    /* 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;
                }
    
438
439
        }
        
440
441
442
443
444
445
446
447
448
449
450
451
452
    /* 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;
                }
453
            }
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477

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

478
            }
479

480
        else
481
        // #pragma omp parallel sections
482
        {
483

484
            /* Recurse on the left? */
485
            // #pragma omp section
486
487
            if ( j > 0 && pivot > min )
                parts_sort( parts , ind , j+1 , min , pivot );
488

489
            /* Recurse on the right? */
490
            // #pragma omp section
491
492
            if ( i < N && pivot+1 < max )
                parts_sort( &parts[i], &ind[i], N-i , pivot+1 , max );
493

494
495
            }
            
496
497
        }
    
498
499
500
    }


Pedro Gonnet's avatar
Pedro Gonnet committed
501
/**
502
 * @brief Mapping function to free the sorted indices buffers.
Pedro Gonnet's avatar
Pedro Gonnet committed
503
504
505
506
507
508
509
510
511
512
513
514
 */

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

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

    }


515
516
/**
 * @brief Mapping function to append a ghost task to each cell.
Pedro Gonnet's avatar
Pedro Gonnet committed
517
518
519
520
 *
 * 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).
521
522
523
524
525
 */

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

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

Pedro Gonnet's avatar
Pedro Gonnet committed
528
529
530
531
532
533
534
    /* 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
535
    /* Make the ghost task */
Pedro Gonnet's avatar
Pedro Gonnet committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
    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;
554
    c->nr_density = 0;
555
556
557
558

    }


Pedro Gonnet's avatar
Pedro Gonnet committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/**
 * @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.
597
 * @param full Map to all cells, including cells with sub-cells.
Pedro Gonnet's avatar
Pedro Gonnet committed
598
599
 */
 
600
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
601
602
603
604
605
606
607
608

    int i;

    void rec_map ( struct cell *c ) {
    
        int k;
        
        /* No progeny? */
609
        if ( full || !c->split )
Pedro Gonnet's avatar
Pedro Gonnet committed
610
611
            fun( c , data );
                
612
613
        /* Recurse. */
        if ( c->split )
Pedro Gonnet's avatar
Pedro Gonnet committed
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
            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.
 */
 
633
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
634

635
636
637
638
639
640
641
    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
642
643
644
    
    /* Copy the data. */
    t->type = type;
645
    t->subtype = subtype;
Pedro Gonnet's avatar
Pedro Gonnet committed
646
647
648
649
650
651
652
653
654
655
656
657
658
659
    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;
    
660
661
662
    /* Unock the space. */
    lock_unlock_blind( &s->lock );
    
Pedro Gonnet's avatar
Pedro Gonnet committed
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
    /* 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 ) {

678
    int j, k, sid, tid;
Pedro Gonnet's avatar
Pedro Gonnet committed
679
680
681
    struct cell *ci, *cj;
    double hi, hj, shift[3];
    struct task *t;
682
    float dt_max = s->dt_max;
683
684
685
686
687
688
689
    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
690
691
692
693
694
695

    /* Loop through the tasks... */
    for ( tid = 0 ; tid < s->nr_tasks ; tid++ ) {
    
        /* Get a pointer on the task. */
        t = &s->tasks[tid];
696
697
698
699
700
701
        
        /* 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
702
            
703
704
705
706
707
708
            /* Ingore this task? */
            if ( ci->dt_min > dt_max ) {
                t->type = task_type_none;
                continue;
                }
            
709
710
711
            /* Is this cell even split? */
            if ( !ci->split )
                continue;
Pedro Gonnet's avatar
Pedro Gonnet committed
712
            
713
            /* Make a sub? */
714
            if ( space_dosub && ci->count < space_subsize ) {
715
716
717
718
719
720
            
                /* 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. */
721
                space_addsorts( s , t , ci , NULL , -1 );
722
723
724
725
726
727
728
729
730
731
732
733
734
735
            
                }
                
            /* 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 )
736
                        space_addtask( s , task_type_self , task_subtype_density , 0 , 0 , ci->progeny[k] , NULL , NULL , 0 , NULL , 0 );
737
738
739
            
                /* Make a task for each pair of progeny. */
                for ( j = 0 ; j < 8 ; j++ )
740
                    if ( ci->progeny[j] != NULL )
741
                        for ( k = j + 1 ; k < 8 ; k++ )
742
743
                            if ( ci->progeny[k] != NULL )
                                space_addtask( s , task_type_pair , task_subtype_density , pts[j][k] , 0 , ci->progeny[j] , ci->progeny[k] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
744
                }
745
746
747
748
749
750
751
752
753
754
755
756
        
            }
    
        /* 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] ) );

757
758
759
760
761
762
763
764
765
766
            /* Ingore this task? */
            if ( ci->dt_min > dt_max && cj->dt_min > dt_max ) {
                t->type = task_type_none;
                continue;
                }
            
            /* 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 );
                
767
768
769
770
771
            /* 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 ) {
                 
                /* Replace by a single sub-task? */
772
773
                if ( space_dosub &&
                     ci->count < space_subsize && cj->count < space_subsize &&
774
                     sid != 0 && sid != 2 && sid != 6 && sid != 8 ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
775
                
776
777
778
                    /* Make this task a sub task. */
                    t->type = task_type_sub;
                    t->flags = sid;
779
                    t->ci = ci; t->cj = cj;
Pedro Gonnet's avatar
Pedro Gonnet committed
780
                    
781
782
                    /* Create the sorts recursively. */
                    space_addsorts( s , t , ci , cj , sid );
783
784
785
786
787
788
789
790
791
792
793
794
795
                    
                    /* 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 ) */
796
                        t->ci = ci->progeny[7]; t->cj = cj->progeny[0]; t->flags = 0;
797
798
799
                        break;

                    case 1: /* (  1 ,  1 ,  0 ) */
800
801
                        t->ci = ci->progeny[6]; t->cj = cj->progeny[0]; t->flags = 1;
                        t = space_addtask( s , task_type_pair , t->subtype , 1 , 0 , ci->progeny[7] , cj->progeny[1] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
802
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[6] , cj->progeny[1] , NULL , 0 , NULL , 0 );
803
                        t = space_addtask( s , task_type_pair , t->subtype , 2 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
804
805
806
                        break;

                    case 2: /* (  1 ,  1 , -1 ) */
807
                        t->ci = ci->progeny[6]; t->cj = cj->progeny[1]; t->flags = 2;
808
809
810
                        break;

                    case 3: /* (  1 ,  0 ,  1 ) */
811
812
                        t->ci = ci->progeny[5]; t->cj = cj->progeny[0]; t->flags = 3;
                        t = space_addtask( s , task_type_pair , t->subtype , 3 , 0 , ci->progeny[7] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
813
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[5] , cj->progeny[2] , NULL , 0 , NULL , 0 );
814
                        t = space_addtask( s , task_type_pair , t->subtype , 6 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
815
816
817
                        break;

                    case 4: /* (  1 ,  0 ,  0 ) */
818
819
820
821
822
823
824
825
826
827
828
829
                        t->ci = ci->progeny[4]; t->cj = cj->progeny[0]; t->flags = 4;
                        t = space_addtask( s , task_type_pair , t->subtype , 5 , 0 , ci->progeny[5] , cj->progeny[0] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 7 , 0 , ci->progeny[6] , cj->progeny[0] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 8 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 3 , 0 , ci->progeny[4] , cj->progeny[1] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 4 , 0 , ci->progeny[5] , cj->progeny[1] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 6 , 0 , ci->progeny[6] , cj->progeny[1] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 7 , 0 , ci->progeny[7] , cj->progeny[1] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 1 , 0 , ci->progeny[4] , cj->progeny[2] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 2 , 0 , ci->progeny[5] , cj->progeny[2] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 4 , 0 , ci->progeny[6] , cj->progeny[2] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 5 , 0 , ci->progeny[7] , cj->progeny[2] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
830
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[4] , cj->progeny[3] , NULL , 0 , NULL , 0 );
831
832
833
                        t = space_addtask( s , task_type_pair , t->subtype , 1 , 0 , ci->progeny[5] , cj->progeny[3] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 3 , 0 , ci->progeny[6] , cj->progeny[3] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 4 , 0 , ci->progeny[7] , cj->progeny[3] , NULL , 0 , NULL , 0 );
834
835
836
                        break;

                    case 5: /* (  1 ,  0 , -1 ) */
837
838
839
840
                        t->ci = ci->progeny[4]; t->cj = cj->progeny[1]; t->flags = 5;
                        t = space_addtask( s , task_type_pair , t->subtype , 5 , 0 , ci->progeny[6] , cj->progeny[3] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 2 , 0 , ci->progeny[4] , cj->progeny[3] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 8 , 0 , ci->progeny[6] , cj->progeny[1] , NULL , 0 , NULL , 0 );
841
842
843
                        break;

                    case 6: /* (  1 , -1 ,  1 ) */
844
                        t->ci = ci->progeny[5]; t->cj = cj->progeny[2]; t->flags = 6;
845
846
847
                        break;

                    case 7: /* (  1 , -1 ,  0 ) */
848
849
850
851
                        t->ci = ci->progeny[4]; t->cj = cj->progeny[3]; t->flags = 6;
                        t = space_addtask( s , task_type_pair , t->subtype , 8 , 0 , ci->progeny[5] , cj->progeny[2] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 7 , 0 , ci->progeny[4] , cj->progeny[2] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 7 , 0 , ci->progeny[5] , cj->progeny[3] , NULL , 0 , NULL , 0 );
852
853
854
                        break;

                    case 8: /* (  1 , -1 , -1 ) */
855
                        t->ci = ci->progeny[4]; t->cj = cj->progeny[3]; t->flags = 8;
856
857
858
                        break;

                    case 9: /* (  0 ,  1 ,  1 ) */
859
860
                        t->ci = ci->progeny[3]; t->cj = cj->progeny[0]; t->flags = 9;
                        t = space_addtask( s , task_type_pair , t->subtype , 9 , 0 , ci->progeny[7] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
861
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[3] , cj->progeny[4] , NULL , 0 , NULL , 0 );
862
                        t = space_addtask( s , task_type_pair , t->subtype , 8 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
863
864
865
                        break;

                    case 10: /* (  0 ,  1 ,  0 ) */
866
867
868
869
870
871
872
873
874
875
876
877
                        t->ci = ci->progeny[2]; t->cj = cj->progeny[0]; t->flags = 10;
                        t = space_addtask( s , task_type_pair , t->subtype , 11 , 0 , ci->progeny[3] , cj->progeny[0] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 7 , 0 , ci->progeny[6] , cj->progeny[0] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 6 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 9 , 0 , ci->progeny[2] , cj->progeny[1] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 10 , 0 , ci->progeny[3] , cj->progeny[1] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 8 , 0 , ci->progeny[6] , cj->progeny[1] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 7 , 0 , ci->progeny[7] , cj->progeny[1] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 1 , 0 , ci->progeny[2] , cj->progeny[4] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 2 , 0 , ci->progeny[3] , cj->progeny[4] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 10 , 0 , ci->progeny[6] , cj->progeny[4] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 11 , 0 , ci->progeny[7] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
878
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[2] , cj->progeny[5] , NULL , 0 , NULL , 0 );
879
880
881
                        t = space_addtask( s , task_type_pair , t->subtype , 1 , 0 , ci->progeny[3] , cj->progeny[5] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 9 , 0 , ci->progeny[6] , cj->progeny[5] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 10 , 0 , ci->progeny[7] , cj->progeny[5] , NULL , 0 , NULL , 0 );
882
883
884
                        break;

                    case 11: /* (  0 ,  1 , -1 ) */
885
886
887
888
                        t->ci = ci->progeny[2]; t->cj = cj->progeny[1]; t->flags = 11;
                        t = space_addtask( s , task_type_pair , t->subtype , 11 , 0 , ci->progeny[6] , cj->progeny[5] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 2 , 0 , ci->progeny[2] , cj->progeny[5] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 6 , 0 , ci->progeny[6] , cj->progeny[1] , NULL , 0 , NULL , 0 );
889
890
891
                        break;

                    case 12: /* (  0 ,  0 ,  1 ) */
892
893
894
895
896
897
898
899
900
901
902
903
                        t->ci = ci->progeny[1]; t->cj = cj->progeny[0]; t->flags = 12;
                        t = space_addtask( s , task_type_pair , t->subtype , 11 , 0 , ci->progeny[3] , cj->progeny[0] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 5 , 0 , ci->progeny[5] , cj->progeny[0] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 2 , 0 , ci->progeny[7] , cj->progeny[0] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 9 , 0 , ci->progeny[1] , cj->progeny[2] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 12 , 0 , ci->progeny[3] , cj->progeny[2] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 8 , 0 , ci->progeny[5] , cj->progeny[2] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 5 , 0 , ci->progeny[7] , cj->progeny[2] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 3 , 0 , ci->progeny[1] , cj->progeny[4] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 6 , 0 , ci->progeny[3] , cj->progeny[4] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 12 , 0 , ci->progeny[5] , cj->progeny[4] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 11 , 0 , ci->progeny[7] , cj->progeny[4] , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
904
                        t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[1] , cj->progeny[6] , NULL , 0 , NULL , 0 );
905
906
907
                        t = space_addtask( s , task_type_pair , t->subtype , 3 , 0 , ci->progeny[3] , cj->progeny[6] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 9 , 0 , ci->progeny[5] , cj->progeny[6] , NULL , 0 , NULL , 0 );
                        t = space_addtask( s , task_type_pair , t->subtype , 12 , 0 , ci->progeny[7] , cj->progeny[6] , NULL , 0 , NULL , 0 );
908
909
910
911
912
                        break;

                    }

                } /* split this task? */
Pedro Gonnet's avatar
Pedro Gonnet committed
913
                
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
            /* Otherwise, if not spilt, stitch-up the sorting. */
            else {
            
                /* Create the sort for ci. */
                if ( ci->sorts[0] == NULL )
                    ci->sorts[0] = space_addtask( s , task_type_sort , 0 , 1 << sid , 0 , ci , NULL , NULL , 0 , NULL , 0 );
                ci->sorts[0]->flags |= (1 << sid);
                task_addunlock( ci->sorts[0] , t );
                
                /* Create the sort for cj. */
                if ( cj->sorts[0] == NULL )
                    cj->sorts[0] = space_addtask( s , task_type_sort , 0 , 1 << sid , 0 , cj , NULL , NULL , 0 , NULL , 0 );
                cj->sorts[0]->flags |= (1 << sid);
                task_addunlock( cj->sorts[0] , t );
                
                }
                
931
            } /* pair interaction? */
Pedro Gonnet's avatar
Pedro Gonnet committed
932
933
934
935
936
937
938
    
        } /* loop over all tasks. */
        
    }
    
    
/**
939
 * @brief Generate the sorts for a sub recursively.
Pedro Gonnet's avatar
Pedro Gonnet committed
940
941
942
943
 *
 * @param s The #space we are working in.
 */
 
944
void space_addsorts ( struct space *s , struct task *t , struct cell *ci , struct cell *cj , int sid ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
945

946
947
948
    float h;
    double shift[3];
    int j, k;
Pedro Gonnet's avatar
Pedro Gonnet committed
949

950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
    /* Get the cell dimensions. */
    h = fmin( ci->h[0] , fmin( ci->h[1] , ci->h[2] ) );
    
    /* Single-cell sub? */
    if ( cj == NULL ) {
    
        /* If there is further splitting, add the pairs recursively. */
        if ( ci->split ) {
        
            /* Recurse for each progeny. */
            for ( j = 0 ; j < 8 ; j++ )
                if ( ci->progeny[j] != NULL )
                    space_addsorts( s , t , ci->progeny[j] , NULL , -1 );

            /* Recurse for each pair of progeny. */
            for ( j = 0 ; j < 8 ; j++ )
                if ( ci->progeny[j] != NULL )
                    for ( k = j + 1 ; k < 8 ; k++ )
                        if ( ci->progeny[k] != NULL )
                            space_addsorts( s , t , ci->progeny[j] , ci->progeny[k] , -1 );
Pedro Gonnet's avatar
Pedro Gonnet committed
970

971
            }
Pedro Gonnet's avatar
Pedro Gonnet committed
972

973
        }
Pedro Gonnet's avatar
Pedro Gonnet committed
974
        
975
976
    /* Otherwise, it's a pair. */
    else {
Pedro Gonnet's avatar
Pedro Gonnet committed
977
        
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
        /* Get the sort ID if not specified. */
        // if ( sid < 0 )
            sid = space_getsid( s , &ci , &cj , shift );
        
        /* If there is no further splitting, add the sorts. */
        if ( !ci->split || !cj->split ||
             ci->h_max*2 >= h || cj->h_max*2 >= h ) {
            
            /* Create and add the sort for ci. */
            if ( ci->sorts[0] == NULL )
                ci->sorts[0] = space_addtask( s , task_type_sort , 0 , 1 << sid , 0 , ci , NULL , NULL , 0 , NULL , 0 );
            ci->sorts[0]->flags |= (1 << sid);
            task_addunlock( ci->sorts[0] , t );
            
            /* Create and add the sort for cj. */
            if ( cj->sorts[0] == NULL )
                cj->sorts[0] = space_addtask( s , task_type_sort , 0 , 1 << sid , 0 , cj , NULL , NULL , 0 , NULL , 0 );
            cj->sorts[0]->flags |= (1 << sid);
            task_addunlock( cj->sorts[0] , t );
Pedro Gonnet's avatar
Pedro Gonnet committed
997
998

            }
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123

        /* Otherwise, recurse. */
        else {
                
            /* For each different sorting type... */
            switch ( sid ) {

                case 0: /* (  1 ,  1 ,  1 ) */
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[0] , 0 );
                    break;

                case 1: /* (  1 ,  1 ,  0 ) */
                    space_addsorts( s , t , ci->progeny[6] , cj->progeny[0] , 1 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[1] , 1 );
                    space_addsorts( s , t , ci->progeny[6] , cj->progeny[1] , 0 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[0] , 2 );
                    break;

                case 2: /* (  1 ,  1 , -1 ) */
                    space_addsorts( s , t , ci->progeny[6] , cj->progeny[1] , 2 );
                    break;

                case 3: /* (  1 ,  0 ,  1 ) */
                    space_addsorts( s , t , ci->progeny[5] , cj->progeny[0] , 3 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[2] , 3 );
                    space_addsorts( s , t , ci->progeny[5] , cj->progeny[2] , 0 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[0] , 6 );
                    break;

                case 4: /* (  1 ,  0 ,  0 ) */
                    space_addsorts( s , t , ci->progeny[4] , cj->progeny[0] , 4 );
                    space_addsorts( s , t , ci->progeny[5] , cj->progeny[0] , 5 );
                    space_addsorts( s , t , ci->progeny[6] , cj->progeny[0] , 7 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[0] , 8 );
                    space_addsorts( s , t , ci->progeny[4] , cj->progeny[1] , 3 );
                    space_addsorts( s , t , ci->progeny[5] , cj->progeny[1] , 4 );
                    space_addsorts( s , t , ci->progeny[6] , cj->progeny[1] , 6 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[1] , 7 );
                    space_addsorts( s , t , ci->progeny[4] , cj->progeny[2] , 1 );
                    space_addsorts( s , t , ci->progeny[5] , cj->progeny[2] , 2 );
                    space_addsorts( s , t , ci->progeny[6] , cj->progeny[2] , 4 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[2] , 5 );
                    space_addsorts( s , t , ci->progeny[4] , cj->progeny[3] , 0 );
                    space_addsorts( s , t , ci->progeny[5] , cj->progeny[3] , 1 );
                    space_addsorts( s , t , ci->progeny[6] , cj->progeny[3] , 3 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[3] , 4 );
                    break;

                case 5: /* (  1 ,  0 , -1 ) */
                    space_addsorts( s , t , ci->progeny[4] , cj->progeny[1] , 5 );
                    space_addsorts( s , t , ci->progeny[6] , cj->progeny[3] , 5 );
                    space_addsorts( s , t , ci->progeny[4] , cj->progeny[3] , 2 );
                    space_addsorts( s , t , ci->progeny[6] , cj->progeny[1] , 8 );
                    break;

                case 6: /* (  1 , -1 ,  1 ) */
                    space_addsorts( s , t , ci->progeny[5] , cj->progeny[2] , 6 );
                    break;

                case 7: /* (  1 , -1 ,  0 ) */
                    space_addsorts( s , t , ci->progeny[4] , cj->progeny[3] , 6 );
                    space_addsorts( s , t , ci->progeny[5] , cj->progeny[2] , 8 );
                    space_addsorts( s , t , ci->progeny[4] , cj->progeny[2] , 7 );
                    space_addsorts( s , t , ci->progeny[5] , cj->progeny[3] , 7 );
                    break;

                case 8: /* (  1 , -1 , -1 ) */
                    space_addsorts( s , t , ci->progeny[4] , cj->progeny[3] , 8 );
                    break;

                case 9: /* (  0 ,  1 ,  1 ) */
                    space_addsorts( s , t , ci->progeny[3] , cj->progeny[0] , 9 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[4] , 9 );
                    space_addsorts( s , t , ci->progeny[3] , cj->progeny[4] , 0 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[0] , 8 );
                    break;

                case 10: /* (  0 ,  1 ,  0 ) */
                    space_addsorts( s , t , ci->progeny[2] , cj->progeny[0] , 10 );
                    space_addsorts( s , t , ci->progeny[3] , cj->progeny[0] , 11 );
                    space_addsorts( s , t , ci->progeny[6] , cj->progeny[0] , 7 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[0] , 6 );
                    space_addsorts( s , t , ci->progeny[2] , cj->progeny[1] , 9 );
                    space_addsorts( s , t , ci->progeny[3] , cj->progeny[1] , 10 );
                    space_addsorts( s , t , ci->progeny[6] , cj->progeny[1] , 8 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[1] , 7 );
                    space_addsorts( s , t , ci->progeny[2] , cj->progeny[4] , 1 );
                    space_addsorts( s , t , ci->progeny[3] , cj->progeny[4] , 2 );
                    space_addsorts( s , t , ci->progeny[6] , cj->progeny[4] , 10 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[4] , 11 );
                    space_addsorts( s , t , ci->progeny[2] , cj->progeny[5] , 0 );
                    space_addsorts( s , t , ci->progeny[3] , cj->progeny[5] , 1 );
                    space_addsorts( s , t , ci->progeny[6] , cj->progeny[5] , 9 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[5] , 10 );
                    break;

                case 11: /* (  0 ,  1 , -1 ) */
                    space_addsorts( s , t , ci->progeny[2] , cj->progeny[1] , 11 );
                    space_addsorts( s , t , ci->progeny[6] , cj->progeny[5] , 11 );
                    space_addsorts( s , t , ci->progeny[2] , cj->progeny[5] , 2 );
                    space_addsorts( s , t , ci->progeny[6] , cj->progeny[1] , 6 );
                    break;

                case 12: /* (  0 ,  0 ,  1 ) */
                    space_addsorts( s , t , ci->progeny[1] , cj->progeny[0] , 12 );
                    space_addsorts( s , t , ci->progeny[3] , cj->progeny[0] , 11 );
                    space_addsorts( s , t , ci->progeny[5] , cj->progeny[0] , 5 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[0] , 2 );
                    space_addsorts( s , t , ci->progeny[1] , cj->progeny[2] , 9 );
                    space_addsorts( s , t , ci->progeny[3] , cj->progeny[2] , 12 );
                    space_addsorts( s , t , ci->progeny[5] , cj->progeny[2] , 8 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[2] , 5 );
                    space_addsorts( s , t , ci->progeny[1] , cj->progeny[4] , 3 );
                    space_addsorts( s , t , ci->progeny[3] , cj->progeny[4] , 6 );
                    space_addsorts( s , t , ci->progeny[5] , cj->progeny[4] , 12 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[4] , 11 );
                    space_addsorts( s , t , ci->progeny[1] , cj->progeny[6] , 0 );
                    space_addsorts( s , t , ci->progeny[3] , cj->progeny[6] , 3 );
                    space_addsorts( s , t , ci->progeny[5] , cj->progeny[6] , 9 );
                    space_addsorts( s , t , ci->progeny[7] , cj->progeny[6] , 12 );
                    break;

                } /* switch. */

            } /* recurse. */
Pedro Gonnet's avatar
Pedro Gonnet committed
1124
            
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
        } /* it's a pair. */

    }
    
    
/**
 * @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, sid;
    int *cdim = s->cdim;
    struct task *t, *t2;
    struct cell *ci, *cj;
    // float dt_max = s->dt_max;
    int counts[task_type_count];
Pedro Gonnet's avatar
Pedro Gonnet committed
1145
1146

    /* Allocate the task-list, if needed. */
1147
    if ( s->tasks == NULL || s->tasks_size < s->tot_cells * space_maxtaskspercell ) {
1148
1149
1150
1151
        if ( s->tasks != NULL )
            free( s->tasks );
        if ( s->tasks_ind != NULL )
            free( s->tasks_ind );
1152
        s->tasks_size = s->tot_cells * space_maxtaskspercell;
1153
        if ( posix_memalign( (void *)&s->tasks , 64 , sizeof(struct task) * s->tasks_size ) != 0 )
Pedro Gonnet's avatar
Pedro Gonnet committed
1154
            error( "Failed to allocate task list." );
1155
1156
1157
        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
1158
1159
1160
1161
1162
1163
1164
1165
1166
    s->nr_tasks = 0;
    
    /* 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;
1167
1168
1169
1170
1171
                ci = &s->cells[cid];
                if ( ci->count == 0 )
                    continue;
                // if ( ci->dt_min <= dt_max )
                    space_addtask( s , task_type_self , task_subtype_density , 0 , 0 , ci , NULL , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
                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 );
1188
1189
1190
                            cj = &s->cells[cjd];
                            if ( cid >= cjd || cj->count == 0 /* ||
                                 ( ci->dt_min > dt_max && cj->dt_min > dt_max ) */ )
Pedro Gonnet's avatar
Pedro Gonnet committed
1191
                                continue;
1192
1193
                            sid = sortlistID[ (kk+1) + 3*( (jj+1) + 3*(ii+1) ) ];
                            t = space_addtask( s , task_type_pair , task_subtype_density , sid , 0 , ci , cj , NULL , 0 , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
1194
1195
1196
1197
1198
1199
1200
                            }
                        }
                    }
                }

    /* Split the tasks. */
    space_splittasks( s );
1201
    
1202
    /* Make each sort depend on the sorts of its progeny. */
1203
1204
    for ( k = 0 ; k < s->nr_tasks ; k++ ) {
        t = &s->tasks[k];
1205
1206
1207
1208
        if ( t->type == task_type_sort && t->ci->split )
            for ( j = 0 ; j < 8 ; j++ )
                if ( t->ci->progeny[j] != NULL && t->ci->progeny[j]->sorts[0] != NULL )
                    task_addunlock( t->ci->progeny[j]->sorts[0] , t );
1209
1210
        }
    
1211
1212
    /* Count the number of tasks associated with each cell and
       store the density tasks in each cell. */
Pedro Gonnet's avatar
Pedro Gonnet committed
1213
    space_map_cells( s , 1 , &space_map_clearnrtasks , NULL );
Pedro Gonnet's avatar
Pedro Gonnet committed
1214
1215
    for ( k = 0 ; k < s->nr_tasks ; k++ ) {
        t = &s->tasks[k];
1216
        if ( t->type == task_type_self ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
1217
            t->ci->nr_tasks += 1;
1218
1219
1220
1221
1222
            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
1223
1224
1225
        else if ( t->type == task_type_pair ) {
            t->ci->nr_tasks += 1;
            t->cj->nr_tasks += 1;
1226
1227
1228
1229
1230
1231
            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
1232
1233
1234
1235
1236
            }
        else if ( t->type == task_type_sub ) {
            t->ci->nr_tasks += 1;
            if ( t->cj != NULL )
                t->cj->nr_tasks += 1;
1237
1238
1239
1240
1241
1242
1243
1244
            if ( t->subtype == task_subtype_density ) {
                t->ci->density[ t->ci->nr_density ] = t;
                t->ci->nr_density += 1;
                if ( t->cj != NULL ) {
                    t->cj->density[ t->cj->nr_density ] = t;
                    t->cj->nr_density += 1;
                    }
                }
Pedro Gonnet's avatar
Pedro Gonnet committed
1245
1246
            }
        }
1247
        
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
    /* Append a ghost task to each cell. */
    space_map_cells( s , 1 , &space_map_mkghosts , s );
    
    /* Run through the tasks and make iacts for each density task. */
    for ( k = 0 ; k < s->nr_tasks ; k++ ) {
    
        /* Get a pointer to the task. */
        t = &s->tasks[k];
        
        /* Self-interaction? */
        if ( t->type == task_type_self && t->subtype == task_subtype_density ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
1259
            task_addunlock( t , t->ci->super->ghost );
1260
1261
1262
1263
1264
1265
            t2 = space_addtask( s , task_type_self , task_subtype_force , 0 , 0 , t->ci , NULL , NULL , 0 , NULL , 0 );
            task_addunlock( t->ci->ghost , t2 );
            }
            
        /* Otherwise, pair interaction? */
        else if ( t->type == task_type_pair && t->subtype == task_subtype_density ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
1266
1267
            task_addunlock( t , t->ci->super->ghost );
            task_addunlock( t , t->cj->super->ghost );
1268
1269
1270
1271
            t2 = space_addtask( s , task_type_pair , task_subtype_force , 0 , 0 , t->ci , t->cj , NULL , 0 , NULL , 0 );
            task_addunlock( t->ci->ghost , t2 );
            task_addunlock( t->cj->ghost , t2 );
            }
1272
    
1273
1274
        /* Otherwise, sub interaction? */
        else if ( t->type == task_type_sub && t->subtype == task_subtype_density ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
1275
            task_addunlock( t , t->ci->super->ghost );
1276
            if ( t->cj != NULL )
Pedro Gonnet's avatar
Pedro Gonnet committed
1277
                task_addunlock( t , t->cj->super->ghost );
1278
1279
1280
1281
1282
            t2 = space_addtask( s , task_type_sub , task_subtype_force , t->flags , 0 , t->ci , t->cj , NULL , 0 , NULL , 0 );
            task_addunlock( t->ci->ghost , t2 );
            if ( t->cj != NULL )
                task_addunlock( t->cj->ghost , t2 );
            }
Pedro Gonnet's avatar
Pedro Gonnet committed
1283
            
1284
        }
Pedro Gonnet's avatar
Pedro Gonnet committed
1285
        
1286
1287
1288
    /* Re-set the indices. */
    for ( k = 0 ; k < s->nr_tasks ; k++ )
        s->tasks_ind[k] = k;
Pedro Gonnet's avatar
Pedro Gonnet committed
1289
1290
            
    /* Count the number of each task type. */
1291
    for ( k = 0 ; k < task_type_count ; k++ )
Pedro Gonnet's avatar
Pedro Gonnet committed
1292
1293
1294
1295
        counts[k] = 0;
    for ( k = 0 ; k < s->nr_tasks ; k++ )
        counts[ s->tasks[k].type ] += 1;
    printf( "space_maketasks: task counts are [ %s=%i" , taskID_names[0] , counts[0] );
1296
    for ( k = 1 ; k < task_type_count ; k++ )
Pedro Gonnet's avatar
Pedro Gonnet committed
1297
1298
        printf( " %s=%i" , taskID_names[k] , counts[k] );
    printf( " ]\n" );
1299
    
Pedro Gonnet's avatar
Pedro Gonnet committed
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312