space.c 56 KB
Newer Older
Pedro Gonnet's avatar
Pedro Gonnet committed
1
/*******************************************************************************
2
 * This file is part of SWIFT.
Pedro Gonnet's avatar
Pedro Gonnet committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 * 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"
34
#include "atomic.h"
Pedro Gonnet's avatar
Pedro Gonnet committed
35
#include "lock.h"
36
#include "task.h"
37
#include "kernel.h"
38
39
#include "part.h"
#include "cell.h"
Pedro Gonnet's avatar
Pedro Gonnet committed
40
41
#include "space.h"
#include "runner.h"
42
#include "error.h"
Pedro Gonnet's avatar
Pedro Gonnet committed
43
44
45

/* Split size. */
int space_splitsize = space_splitsize_default;
46
int space_subsize = space_subsize_default;
Pedro Gonnet's avatar
Pedro Gonnet committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

/* Map shift vector to sortlist. */
const int sortlistID[27] = {
    /* ( -1 , -1 , -1 ) */   0 ,
    /* ( -1 , -1 ,  0 ) */   1 , 
    /* ( -1 , -1 ,  1 ) */   2 ,
    /* ( -1 ,  0 , -1 ) */   3 ,
    /* ( -1 ,  0 ,  0 ) */   4 , 
    /* ( -1 ,  0 ,  1 ) */   5 ,
    /* ( -1 ,  1 , -1 ) */   6 ,
    /* ( -1 ,  1 ,  0 ) */   7 , 
    /* ( -1 ,  1 ,  1 ) */   8 ,
    /* (  0 , -1 , -1 ) */   9 ,
    /* (  0 , -1 ,  0 ) */   10 , 
    /* (  0 , -1 ,  1 ) */   11 ,
    /* (  0 ,  0 , -1 ) */   12 ,
    /* (  0 ,  0 ,  0 ) */   0 , 
    /* (  0 ,  0 ,  1 ) */   12 ,
    /* (  0 ,  1 , -1 ) */   11 ,
    /* (  0 ,  1 ,  0 ) */   10 , 
    /* (  0 ,  1 ,  1 ) */   9 ,
    /* (  1 , -1 , -1 ) */   8 ,
    /* (  1 , -1 ,  0 ) */   7 , 
    /* (  1 , -1 ,  1 ) */   6 ,
    /* (  1 ,  0 , -1 ) */   5 ,
    /* (  1 ,  0 ,  0 ) */   4 , 
    /* (  1 ,  0 ,  1 ) */   3 ,
    /* (  1 ,  1 , -1 ) */   2 ,
    /* (  1 ,  1 ,  0 ) */   1 , 
    /* (  1 ,  1 ,  1 ) */   0 
    };
78
79
80
81
82
83
84
85
86
87
    
    
/**
 * @brief Mark tasks to be skipped and set the sort flags accordingly.
 * 
 * @return 1 if the space has to be rebuilt, 0 otherwise.
 */
 
int space_marktasks ( struct space *s ) {

88
89
    int k, nr_tasks = s->nr_tasks, *ind = s->tasks_ind;
    struct task *t, *tasks = s->tasks;
90
    float dt_step = s->dt_step;
91
    struct cell *ci, *cj;
92
    
93
94
    /* Run through the tasks and mark as skip or not. */
    for ( k = 0 ; k < nr_tasks ; k++ ) {
95
96
    
        /* Get a handle on the kth task. */
97
        t = &tasks[ ind[k] ];
98
        
99
100
        /* Sort-task? Note that due to the task ranking, the sorts
           will all come before the pairs and/or subs. */
101
102
103
104
        if ( t->type == task_type_sort ) {
        
            /* Re-set the flags. */
            t->flags = 0;
105
106
            t->skip = 1;
        
107
108
109
            }
        
        /* Single-cell task? */
110
        else if ( t->type == task_type_self ||
111
112
                  t->type == task_type_ghost ||
                ( t->type == task_type_sub && t->cj == NULL ) ) {
113
114
115
116
117
118
119
120
121
             
            /* Set this task's skip. */
            t->skip = ( t->ci->dt_min > dt_step );
            
            }
        
        /* Pair? */
        else if ( t->type == task_type_pair || ( t->type == task_type_sub && t->cj != NULL ) ) {
            
122
123
124
125
            /* Local pointers. */
            ci = t->ci;
            cj = t->cj;
            
Pedro Gonnet's avatar
Pedro Gonnet committed
126
127
128
            /* Set this task's skip. */
            t->skip = ( ci->dt_min > dt_step && cj->dt_min > dt_step );
            
129
            /* Too much particle movement? */
130
            if ( t->tight &&
Pedro Gonnet's avatar
Pedro Gonnet committed
131
                 ( fmaxf( ci->h_max , cj->h_max ) + ci->dx_max + cj->dx_max > cj->dmin || 
132
                   ci->dx_max > space_maxreldx*ci->h_max || cj->dx_max > space_maxreldx*cj->h_max ) )
Pedro Gonnet's avatar
Pedro Gonnet committed
133
                return 1;
134
135
                
            /* Set the sort flags. */
136
            if ( !t->skip && t->type == task_type_pair ) {
137
138
139
140
                ci->sorts->flags |= (1 << t->flags);
                ci->sorts->skip = 0;
                cj->sorts->flags |= (1 << t->flags);
                cj->sorts->skip = 0;
141
142
143
144
                }
                
            }
            
Pedro Gonnet's avatar
Pedro Gonnet committed
145
146
147
148
        /* Kick2? */
        else if ( t->type == task_type_kick2 )
            t->skip = 0;
            
149
150
151
152
153
154
155
156
157
158
        /* None? */
        else if ( t->type == task_type_none )
            t->skip = 1;
            
        }
        
    /* All is well... */
    return 0;
    
    }
159
160
161
162
163
164
165
166
167
168
169
170


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

173
174
175
176
    int k, rebuild;
    // struct task *t;
    // float dt_step = s->dt_step;
    float dx_max = 0.0f;
177
    // int counts[ task_type_count + 1 ];
178
    ticks tic;
179
    
180
181
182
    /* Get the maximum displacement in the whole system. */
    for ( k = 0 ; k < s->nr_cells ; k++ )
        dx_max = fmaxf( dx_max , s->cells[k].dx_max );
183
    // printf( "space_prepare: dx_max is %e.\n" , dx_max );
184
    
185
    /* Run through the tasks and mark as skip or not. */
186
    // tic = getticks();
187
    rebuild = space_marktasks( s );
188
    // printf( "space_prepare: space_marktasks took %.3f ms.\n" , (double)(getticks() - tic)/CPU_TPS*1000 );
189
190
        
    /* Did this not go through? */
191
    if ( rebuild ) {
192
193
    
        /* Re-build the space. */
194
        tic = getticks();
195
        space_rebuild( s , 0.0 );
196
        printf( "space_prepare: space_rebuild took %.3f ms.\n" , (double)(getticks() - tic)/CPU_TPS*1000 );
197
    
198
        /* Run through the tasks and mark as skip or not. */
199
        // tic = getticks();
Pedro Gonnet's avatar
Pedro Gonnet committed
200
201
        if ( space_marktasks( s ) )
            error( "space_marktasks failed after space_rebuild." );
202
        // printf( "space_prepare: space_marktasks took %.3f ms.\n" , (double)(getticks() - tic)/CPU_TPS*1000 );
203
        
204
205
206
        }

    
Pedro Gonnet's avatar
Pedro Gonnet committed
207
208
209
    /* Let whoever cares know if we rebuilt. */
    return rebuild;
    
210
211
212
213
214
215
216
217
218
219
220
221
    }
    
    
/** 
 * @brief Sort the tasks in topological order over all queues.
 *
 * @param s The #space.
 */
 
void space_ranktasks ( struct space *s ) {

    int i, j = 0, k, temp, left = 0, rank;
222
223
    struct task *t, *tasks = s->tasks;
    int *tid = s->tasks_ind, nr_tasks = s->nr_tasks;
224
225
    
    /* Run throught the tasks and get all the waits right. */
226
    for ( i = 0 , k = 0 ; k < nr_tasks ; k++ ) {
227
        tid[k] = k;
228
229
        for ( j = 0 ; j < tasks[k].nr_unlock_tasks ; j++ )
            tasks[k].unlock_tasks[j]->wait += 1;
230
231
232
        }
        
    /* Main loop. */
233
    for ( j = 0 , rank = 0 ; left < nr_tasks ; rank++ ) {
234
235
        
        /* Load the tids of tasks with no waits. */
236
237
        for ( k = left ; k < nr_tasks ; k++ )
            if ( tasks[ tid[k] ].wait == 0 ) {
238
239
240
                temp = tid[j]; tid[j] = tid[k]; tid[k] = temp;
                j += 1;
                }
241
242
243
244
                
        /* Did we get anything? */
        if ( j == left )
            error( "Unsatisfiable task dependencies detected." );
245

246
        /* Unlock the next layer of tasks. */
247
        for ( i = left ; i < j ; i++ ) {
248
            t = &tasks[ tid[i] ];
249
            t->rank = rank;
250
251
252
            tid[i] = t - tasks;
            if ( tid[i] >= nr_tasks )
                error( "Task index overshoot." );
253
254
255
256
257
258
259
260
261
262
263
264
265
266
            /* printf( "engine_ranktasks: task %i of type %s has rank %i.\n" , i , 
                (t->type == task_type_self) ? "self" : (t->type == task_type_pair) ? "pair" : "sort" , rank ); */
            for ( k = 0 ; k < t->nr_unlock_tasks ; k++ )
                t->unlock_tasks[k]->wait -= 1;
            }
            
        /* The new left (no, not tony). */
        left = j;
            
        }
        
    }


267
268
269
270
271
272
273
274
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
307
308
309
310
311
312
313
314
/**
 * @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;

    }


315
/**
316
 * @brief Recursively dismantle a cell tree.
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
 *
 */
 
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;
                }
    
    }

/**
335
 * @brief Re-build the cells as well as the tasks.
336
337
 *
 * @param s The #space in which to update the cells.
338
 * @param cell_max Maximal cell size.
339
340
341
 *
 */
 
342
void space_rebuild ( struct space *s , double cell_max ) {
343

344
    float h_max = s->cell_min / kernel_gamma, dmin;
345
    int i, j, k, cdim[3], nr_parts = s->nr_parts;
346
    struct cell *restrict c;
347
348
    struct part *restrict finger, *restrict p, *parts = s->parts;
    struct cpart *restrict cfinger;
349
    int *ind;
Pedro Gonnet's avatar
Pedro Gonnet committed
350
    double ih[3], dim[3];
351
    // ticks tic;
352
    
353
354
355
    /* Be verbose about this. */
    printf( "space_rebuild: (re)building space...\n" ); fflush(stdout);
    
356
    /* Run through the parts and get the current h_max. */
357
    // tic = getticks();
358
359
360
361
362
363
364
365
366
367
368
369
    if ( s->cells != NULL ) {
        for ( k = 0 ; k < s->nr_cells ; k++ ) {
            if ( s->cells[k].h_max > h_max )
                h_max = s->cells[k].h_max;
            }
        }
    else {
        for ( k = 0 ; k < nr_parts ; k++ ) {
            if ( s->parts[k].h > h_max )
                h_max = s->parts[k].h;
            }
        s->h_max = h_max;
370
        }
371
    // printf( "space_rebuild: h_max is %.3e (cell_max=%.3e).\n" , h_max , cell_max );
372
    // printf( "space_rebuild: getting h_min and h_max took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
373
374
375
    
    /* Get the new putative cell dimensions. */
    for ( k = 0 ; k < 3 ; k++ )
376
        cdim[k] = floor( s->dim[k] / fmax( h_max*kernel_gamma*space_stretch , cell_max ) );
377
378
        
    /* Do we need to re-build the upper-level cells? */
379
    // tic = getticks();
Pedro Gonnet's avatar
Pedro Gonnet committed
380
    if ( s->cells == NULL ||
381
         cdim[0] < s->cdim[0] || cdim[1] < s->cdim[1] || cdim[2] < s->cdim[2] ) {
382
383
384
    
        /* Free the old cells, if they were allocated. */
        if ( s->cells != NULL ) {
385
            for ( k = 0 ; k < s->nr_cells ; k++ ) {
386
                space_rebuild_recycle( s , &s->cells[k] );
387
388
389
                if ( s->cells[k].sort != NULL )
                    free( s->cells[k].sort );
                }
390
391
392
393
            free( s->cells );
            s->maxdepth = 0;
            }
            
394
        /* Set the new cell dimensions only if smaller. */
395
396
397
398
399
        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];
            }
400
        dmin = fminf( s->h[0] , fminf( s->h[1] , s->h[2] ) );
401

402
        /* Allocate the highest level of cells. */
Pedro Gonnet's avatar
Pedro Gonnet committed
403
        s->tot_cells = s->nr_cells = cdim[0] * cdim[1] * cdim[2];
404
405
406
407
408
409
410
411
412
413
414
415
416
417
        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];
418
                    c->dmin = dmin;
419
                    c->depth = 0;
Pedro Gonnet's avatar
Pedro Gonnet committed
420
                    c->count = 0;
421
                    lock_init( &c->lock );
422
                    }
423
424
425
           
        /* Be verbose about the change. */         
        printf( "space_rebuild: set cell dimensions to [ %i %i %i ].\n" , cdim[0] , cdim[1] , cdim[2] ); fflush(stdout);
426
427
                    
        } /* re-build upper-level cells? */
428
    // printf( "space_rebuild: rebuilding upper-level cells took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
429
        
Pedro Gonnet's avatar
Pedro Gonnet committed
430
431
432
433
434
435
436
437
438
439
    /* Otherwise, just clean up the cells. */
    else {
    
        /* Free the old cells, if they were allocated. */
        for ( k = 0 ; k < s->nr_cells ; k++ ) {
            space_rebuild_recycle( s , &s->cells[k] );
            s->cells[k].sorts = NULL;
            s->cells[k].nr_tasks = 0;
            s->cells[k].nr_density = 0;
            s->cells[k].dx_max = 0.0f;
440
            s->cells[k].sorted = 0;
Pedro Gonnet's avatar
Pedro Gonnet committed
441
442
            s->cells[k].count = 0;
            s->cells[k].kick2 = NULL;
Pedro Gonnet's avatar
Pedro Gonnet committed
443
444
445
446
            }
        s->maxdepth = 0;
    
        }
447
448
        
    /* Run through the particles and get their cell index. */
449
    // tic = getticks();
450
451
    if ( ( ind = (int *)malloc( sizeof(int) * s->nr_parts ) ) == NULL )
        error( "Failed to allocate temporary particle indices." );
Pedro Gonnet's avatar
Pedro Gonnet committed
452
453
    ih[0] = s->ih[0]; ih[1] = s->ih[1]; ih[2] = s->ih[2];
    dim[0] = s->dim[0]; dim[1] = s->dim[1]; dim[2] = s->dim[2];
Pedro Gonnet's avatar
Pedro Gonnet committed
454
    cdim[0] = s->cdim[0]; cdim[1] = s->cdim[1]; cdim[2] = s->cdim[2];
Pedro Gonnet's avatar
Pedro Gonnet committed
455
    // #pragma omp parallel for private(p,j)
456
457
    for ( k = 0 ; k < nr_parts ; k++ )  {
        p = &parts[k];
458
459
        for ( j = 0 ; j < 3 ; j++ )
            if ( p->x[j] < 0.0 )
Pedro Gonnet's avatar
Pedro Gonnet committed
460
461
462
463
                p->x[j] += dim[j];
            else if ( p->x[j] >= dim[j] )
                p->x[j] -= dim[j];
        ind[k] = cell_getid( cdim , p->x[0]*ih[0] , p->x[1]*ih[1] , p->x[2]*ih[2] );
464
        atomic_inc( &s->cells[ ind[k] ].count );
465
        }
466
    // printf( "space_rebuild: getting particle indices took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
467
468

    /* Sort the parts according to their cells. */
469
    // tic = getticks();
Pedro Gonnet's avatar
Pedro Gonnet committed
470
    parts_sort( parts , ind , s->nr_parts , 0 , s->nr_cells-1 );
471
    // printf( "space_rebuild: parts_sort took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
472
    
Pedro Gonnet's avatar
Pedro Gonnet committed
473
474
475
476
477
478
479
480
481
    /* Verify sort. */
    /* for ( k = 1 ; k < nr_parts ; k++ ) {
        if ( ind[k-1] > ind[k] ) {
            error( "Sort failed!" );
            }
        else if ( ind[k] != cell_getid( cdim , parts[k].x[0]*ih[0] , parts[k].x[1]*ih[1] , parts[k].x[2]*ih[2] ) )
            error( "Incorrect indices!" );
        } */
    
482
483
    /* We no longer need the indices as of here. */
    free( ind );    
484
485

    /* Hook the cells up to the parts. */
486
    // tic = getticks();
487
488
489
    finger = s->parts;
    cfinger = s->cparts;
    for ( k = 0 ; k < s->nr_cells ; k++ ) {
490
491
492
493
494
495
        c = &s->cells[ k ];
        c->parts = finger;
        c->cparts = cfinger;
        finger = &finger[ c->count ];
        cfinger = &cfinger[ c->count ];
        }
496
    // printf( "space_rebuild: hooking up cells took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
497
        
498
499
    /* At this point, we have the upper-level cells, old or new. Now make
       sure that the parts in each cell are ok. */
500
    // tic = getticks();
Pedro Gonnet's avatar
Pedro Gonnet committed
501
502
503
504
505
506
507
508
509
510
511
512
513
    k = 0;
    #pragma omp parallel shared(s,k)
    {
        while ( 1 ) {
            int myk;
            #pragma omp critical
            myk = k++;
            if ( myk < s->nr_cells )
                space_split( s , &s->cells[myk] );
            else
                break;
            }
        }
514
    // printf( "space_rebuild: space_rebuild_recurse took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
515
        
516
    /* Now that we have the cell structre, re-build the tasks. */
517
    // tic = getticks();
518
    space_maketasks( s , 1 );
519
    // printf( "space_rebuild: maketasks took %.3f ms.\n" , (double)(getticks() - tic) / CPU_TPS * 1000 );
520
    
521
522
523
    }


524
/**
525
 * @brief Sort the particles and condensed particles according to the given indices.
526
527
528
529
530
531
532
533
534
535
 *
 * @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.
 */
 
536
void parts_sort_rec ( struct part *parts , int *ind , int N , int min , int max ) {
537
538
539
540
541
542

    int pivot = (min + max) / 2;
    int i = 0, j = N-1;
    int temp_i;
    struct part temp_p;
    
543
544
545
546
547
548
    /* If N is small enough, just do insert sort. */
    if ( N < 16 ) {
    
        for ( i = 1 ; i < N ; i++ )
            if ( ind[i] < ind[i-1] ) {
                temp_i = ind[i];
Pedro Gonnet's avatar
Pedro Gonnet committed
549
                temp_p = parts[i];
550
551
552
553
554
555
556
                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;
                }
Pedro Gonnet's avatar
Pedro Gonnet committed
557
558
559
560
561
                
        /* Verify sort. */
        /* for ( i = 1 ; i < N ; i++ )
            if ( ind[i-1] > ind[i] )
                error( "Insert-sort failed!" ); */
562
    
563
564
        }
        
565
566
567
568
569
570
571
572
573
574
575
576
577
    /* 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;
                }
578
            }
579
580

        /* Verify sort. */
Pedro Gonnet's avatar
Pedro Gonnet committed
581
        /* for ( int k = 0 ; k <= j ; k++ )
582
583
584
585
586
587
588
589
            if ( ind[k] > pivot ) {
                printf( "parts_sort: sorting failed at k=%i, ind[k]=%i, pivot=%i, i=%i, j=%i, N=%i.\n" , k , ind[k] , pivot , i , j , N );
                error( "Sorting failed (<=pivot)." );
                }
        for ( int k = j+1 ; k < N ; k++ )
            if ( ind[k] <= pivot ) {
                printf( "parts_sort: sorting failed at k=%i, ind[k]=%i, pivot=%i, i=%i, j=%i, N=%i.\n" , k , ind[k] , pivot , i , j , N );
                error( "Sorting failed (>pivot)." );
Pedro Gonnet's avatar
Pedro Gonnet committed
590
                } */
591

Pedro Gonnet's avatar
Pedro Gonnet committed
592
593
594
595
596
597
598
599
600
601
602
        /* Bother going 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 );
                
603
            }
Pedro Gonnet's avatar
Pedro Gonnet committed
604
605
606
607
608
609
610
611
            
        else {
        
            /* Recurse on the left? */
            if ( j > 0  && pivot > min ) {
                #pragma omp task untied
                parts_sort( parts , ind , j+1 , min , pivot );
                }
612

Pedro Gonnet's avatar
Pedro Gonnet committed
613
614
615
616
617
618
            /* Recurse on the right? */
            if ( i < N && pivot+1 < max ) {
                #pragma omp task untied
                parts_sort( &parts[i], &ind[i], N-i , pivot+1 , max );
                }
                
619
            }
620
            
621
622
        }
    
623
624
625
    }


626
627
628
629
630
631
632
633
634
635
636
637
void parts_sort ( struct part *parts , int *ind , int N , int min , int max ) {

    /* Call the first sort as an OpenMP task. */
    #pragma omp parallel
    {
        #pragma omp single nowait
        parts_sort_rec( parts , ind , N , min , max );
    }
    
    }


Pedro Gonnet's avatar
Pedro Gonnet committed
638
/**
639
 * @brief Mapping function to free the sorted indices buffers.
Pedro Gonnet's avatar
Pedro Gonnet committed
640
641
642
643
644
645
646
647
648
649
650
651
 */

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

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

    }


652
653
/**
 * @brief Mapping function to append a ghost task to each cell.
Pedro Gonnet's avatar
Pedro Gonnet committed
654
655
656
657
 *
 * 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).
658
659
 *
 * A kick2-task is appended to each super cell.
660
661
662
663
664
 */

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

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

Pedro Gonnet's avatar
Pedro Gonnet committed
667
668
669
670
671
672
673
    /* 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
674
    /* Make the ghost task */
Pedro Gonnet's avatar
Pedro Gonnet committed
675
    if ( c->super != c || c->nr_tasks > 0 )
676
        c->ghost = space_addtask( s , task_type_ghost , task_subtype_none , 0 , 0 , c , NULL , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
677

678
679
680
681
    /* Append a kick task if we are the active super cell. */
    if ( c->super == c && c->nr_tasks > 0 )
        c->kick2 = space_addtask( s , task_type_kick2 , task_subtype_none , 0 , 0 , c , NULL , 0 );
    
Pedro Gonnet's avatar
Pedro Gonnet committed
682
683
684
685
    /* 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 );
686
        
Pedro Gonnet's avatar
Pedro Gonnet committed
687
688
689
    }


Pedro Gonnet's avatar
Pedro Gonnet committed
690
691
692
693
/**
 * @brief Map a function to all particles in a aspace.
 *
 * @param s The #space we are working in.
694
695
 * @param fun Function pointer to apply on the cells.
 * @param data Data passed to the function fun.
Pedro Gonnet's avatar
Pedro Gonnet committed
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
 */
 
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. */
720
    #pragma omp parallel for schedule(dynamic,1)
Pedro Gonnet's avatar
Pedro Gonnet committed
721
722
723
724
725
726
727
728
729
730
    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.
731
 * @param full Map to all cells, including cells with sub-cells.
732
733
 * @param fun Function pointer to apply on the cells.
 * @param data Data passed to the function fun.
Pedro Gonnet's avatar
Pedro Gonnet committed
734
735
 */
 
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
void space_map_cells_post ( struct space *s , int full , void (*fun)( struct cell *c , void *data ) , void *data ) {

    int i;

    void rec_map ( struct cell *c ) {
    
        int k;
        
        /* Recurse. */
        if ( c->split )
            for ( k = 0 ; k < 8 ; k++ )
                if ( c->progeny[k] != NULL )
                    rec_map( c->progeny[k] );
                
        /* No progeny? */
        if ( full || !c->split )
            fun( c , data );
                
        }
        
    /* Call the recursive function on all higher-level cells. */
757
    #pragma omp parallel for schedule(dynamic,1)
758
759
760
761
762
763
764
    for ( i = 0 ; i < s->nr_cells ; i++ )
        rec_map( &s->cells[i] );

    }


void space_map_cells_pre ( struct space *s , int full , void (*fun)( struct cell *c , void *data ) , void *data ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
765
766
767
768
769
770
771
772

    int i;

    void rec_map ( struct cell *c ) {
    
        int k;
        
        /* No progeny? */
773
        if ( full || !c->split )
Pedro Gonnet's avatar
Pedro Gonnet committed
774
775
            fun( c , data );
                
776
777
        /* Recurse. */
        if ( c->split )
Pedro Gonnet's avatar
Pedro Gonnet committed
778
779
780
781
782
783
784
            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. */
785
    #pragma omp parallel for schedule(dynamic,1)
Pedro Gonnet's avatar
Pedro Gonnet committed
786
787
788
789
790
791
792
793
794
795
    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.
796
797
798
799
800
801
802
 * @param type The type of the task.
 * @param subtype The sub-type of the task.
 * @param flags The flags of the task.
 * @param wait 
 * @param ci The first cell to interact.
 * @param cj The second cell to interact.
 * @param tight
Pedro Gonnet's avatar
Pedro Gonnet committed
803
804
 */
 
805
struct task *space_addtask ( struct space *s , int type , int subtype , int flags , int wait , struct cell *ci , struct cell *cj , int tight ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
806

807
    int ind;
808
809
810
    struct task *t;
    
    /* Get the next free task. */
811
812
    ind = atomic_inc( &s->tasks_next );
    t = &s->tasks[ ind ];
Pedro Gonnet's avatar
Pedro Gonnet committed
813
814
815
    
    /* Copy the data. */
    t->type = type;
816
    t->subtype = subtype;
Pedro Gonnet's avatar
Pedro Gonnet committed
817
818
819
820
    t->flags = flags;
    t->wait = wait;
    t->ci = ci;
    t->cj = cj;
821
    t->skip = 0;
822
    t->tight = tight;
823
    t->nr_unlock_tasks = 0;
Pedro Gonnet's avatar
Pedro Gonnet committed
824
    
825
826
827
828
    /* Init the lock. */
    lock_init( &t->lock );
    
    /* Add an index for it. */
829
    // lock_lock( &s->lock );
Pedro Gonnet's avatar
typo.    
Pedro Gonnet committed
830
    s->tasks_ind[ atomic_inc( &s->nr_tasks ) ] = ind;
831
    // lock_unlock_blind( &s->lock );
832
    
Pedro Gonnet's avatar
Pedro Gonnet committed
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
    /* 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 ) {

848
    int j, k, ind, sid, tid = 0, redo;
Pedro Gonnet's avatar
Pedro Gonnet committed
849
850
    struct cell *ci, *cj;
    double hi, hj, shift[3];
851
    struct task *t, *t_old;
852
    // float dt_step = s->dt_step;
853
854
855
856
857
    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 } ,
858
859
                      { -1 , -1 , -1 , -1 , -1 , -1 , 11 , 10 } ,
                      { -1 , -1 , -1 , -1 , -1 , -1 , -1 , 12 } };
Pedro Gonnet's avatar
Pedro Gonnet committed
860
861

    /* Loop through the tasks... */
Pedro Gonnet's avatar
Pedro Gonnet committed
862
    // #pragma omp parallel default(none) shared(s,tid,pts,space_subsize) private(ind,j,k,t,t_old,redo,ci,cj,hi,hj,sid,shift)
863
864
    {
    redo = 0; t_old = t = NULL;
Pedro Gonnet's avatar
Pedro Gonnet committed
865
    while ( 1 ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
866
867
    
        /* Get a pointer on the task. */
868
869
870
871
872
        if ( redo ) {
            redo = 0;
            t = t_old;
            }
        else {
Pedro Gonnet's avatar
Pedro Gonnet committed
873
874
875
            if ( ( ind = atomic_inc( &tid ) ) < s->nr_tasks )
                t_old = t = &s->tasks[ s->tasks_ind[ ind ] ];
            else
876
877
                break;
            }
878
        
879
880
881
882
883
884
885
        /* Empty task? */
        if ( t->ci == NULL || ( t->type == task_type_pair && t->cj == NULL ) ) {
            t->type = task_type_none;
            t->skip = 1;
            continue;
            }
        
886
887
888
889
890
        /* 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
891
            
892
            /* Ingore this task? */
893
            /* if ( ci->dt_min > dt_step ) {
894
                t->skip = 1;
895
                continue;
896
                } */
897
            
898
            /* Is this cell even split? */
Pedro Gonnet's avatar
Pedro Gonnet committed
899
            if ( ci->split ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
900
            
901
            /* Make a sub? */
902
            if ( space_dosub && ci->count < space_subsize ) {
903
904
905
906
907
908
909
910
911
912
            
                /* convert to a self-subtask. */
                t->type = task_type_sub;
                
                }
                
            /* Otherwise, make tasks explicitly. */
            else {
            
                /* Take a step back (we're going to recycle the current task)... */
913
                redo = 1;
914
915
916
917
918
919

                /* 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 )
920
                        space_addtask( s , task_type_self , task_subtype_density , 0 , 0 , ci->progeny[k] , NULL , 0 );
921
922
923
            
                /* Make a task for each pair of progeny. */
                for ( j = 0 ; j < 8 ; j++ )
924
                    if ( ci->progeny[j] != NULL )
925
                        for ( k = j + 1 ; k < 8 ; k++ )
926
                            if ( ci->progeny[k] != NULL )
927
                                space_addtask( s , task_type_pair , task_subtype_density , pts[j][k] , 0 , ci->progeny[j] , ci->progeny[k] , 0 );
Pedro Gonnet's avatar
Pedro Gonnet committed
928
                }
Pedro Gonnet's avatar
Pedro Gonnet committed
929
930

                }
931
932
933
934
935
936
937
938
939
        
            }
    
        /* Pair interaction? */
        else if ( t->type == task_type_pair ) {
            
            /* Get a handle on the cells involved. */
            ci = t->ci;
            cj = t->cj;
940
941
            hi = ci->dmin;
            hj = cj->dmin;
942

943
            /* Ingore this task? */
944
            /* if ( ci->dt_min > dt_step && cj->dt_min > dt_step ) {
945
                t->skip = 1;
946
                continue;
947
                } */
948
949
950
951
952
            
            /* 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 );
                
953
954
            /* Should this task be split-up? */
            if ( ci->split && cj->split &&
955
956
                 ci->h_max*kernel_gamma*space_stretch < hi/2 &&
                 cj->h_max*kernel_gamma*space_stretch < hj/2 ) {
957
958
                 
                /* Replace by a single sub-task? */
959
960
                if ( space_dosub &&
                     ci->count < space_subsize && cj->count < space_subsize &&
961
                     sid != 0 && sid != 2 && sid != 6 && sid != 8 ) {
Pedro Gonnet's avatar
Pedro Gonnet committed
962
                
963
964
                    /* Make this task a sub task. */
                    t->type = task_type_sub;
Pedro Gonnet's avatar
Pedro Gonnet committed
965

966
                    }
Pedro Gonnet's avatar
Pedro Gonnet committed
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
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
                    
                /* Otherwise, split it. */
                else {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                        case 12: /* (  0 ,  0 ,  1 ) */
                            t->ci = ci->progeny[1]; t->cj = cj->progeny[0]; t->flags = 12; t->tight = 1;
                            t = space_addtask( s , task_type_pair , t->subtype , 11 , 0 , ci->progeny[3] , cj->progeny[0] , 1 );
                            t = space_addtask( s , task_type_pair , t->subtype , 5 , 0 , ci->progeny[5] , cj->progeny[0] , 1 );
                            t = space_addtask( s , task_type_pair , t->subtype , 2 , 0 , ci->progeny[7] , cj->progeny[0] , 1 );
                            t = space_addtask( s , task_type_pair , t->subtype , 9 , 0 , ci->progeny[1] , cj->progeny[2] , 1 );
                            t = space_addtask( s , task_type_pair , t->subtype , 12 , 0 , ci->progeny[3] , cj->progeny[2] , 1 );
                            t = space_addtask( s , task_type_pair , t->subtype , 8 , 0 , ci->progeny[5] , cj->progeny[2] , 1 );
                            t = space_addtask( s , task_type_pair , t->subtype , 5 , 0 , ci->progeny[7] , cj->progeny[2] , 1 );
                            t = space_addtask( s , task_type_pair , t->subtype , 3 , 0 , ci->progeny[1] , cj->progeny[4] , 1 );
                            t = space_addtask( s , task_type_pair , t->subtype , 6 , 0 , ci->progeny[3] , cj->progeny[4] , 1 );
                            t = space_addtask( s , task_type_pair , t->subtype , 12 , 0 , ci->progeny[5] , cj->progeny[4] , 1 );
                            t = space_addtask( s , task_type_pair , t->subtype , 11 , 0 , ci->progeny[7] , cj->progeny[4] , 1 );
                            t = space_addtask( s , task_type_pair , t->subtype , 0 , 0 , ci->progeny[1] , cj->progeny[6] , 1 );
                            t = space_addtask( s , task_type_pair , t->subtype , 3 , 0 , ci->progeny[3] , cj->progeny[6] , 1 );
                            t = space_addtask( s , task_type_pair , t->subtype , 9 , 0 , ci->progeny[5] , cj->progeny[6] , 1 );
                            t = space_addtask( s , task_type_pair , t->subtype , 12 , 0 , ci->progeny[7] , cj->progeny[6] , 1 );
                            break;
1091

Pedro Gonnet's avatar
Pedro Gonnet committed
1092
1093
                        }
                        
1094
1095
1096
                    }

                } /* split this task? */
Pedro Gonnet's avatar
Pedro Gonnet committed
1097
                
1098
1099
1100
1101
            /* Otherwise, if not spilt, stitch-up the sorting. */
            else {
            
                /* Create the sort for ci. */
1102
                // lock_lock( &ci->lock );
1103
1104
1105
                if ( ci->sorts == NULL )
                    ci->sorts = space_addtask( s , task_type_sort , 0 , 1 << sid , 0 , ci , NULL , 0 );
                ci->sorts->flags |= (1 << sid);
1106
                // lock_unlock_blind( &ci->lock );
1107
                task_addunlock( ci->sorts , t );
1108
1109
                
                /* Create the sort for cj. */
1110
                // lock_lock( &cj->lock );
1111
1112
1113
                if ( cj->sorts == NULL )
                    cj->sorts = space_addtask( s , task_type_sort , 0 , 1 << sid , 0 , cj , NULL , 0 );
                cj->sorts->flags |= (1 << sid);
1114
                // lock_unlock_blind( &cj->lock );
1115
                task_addunlock( cj->sorts , t );
1116
1117
1118
                
                }
                
1119
            } /* pair interaction? */
Pedro Gonnet's avatar
Pedro Gonnet committed
1120
1121
1122
    
        } /* loop over all tasks. */
        
1123
1124
        }
        
Pedro Gonnet's avatar
Pedro Gonnet committed
1125
1126
1127
    }
    
    
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
/**
 * @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;
Pedro Gonnet's avatar
Pedro Gonnet committed
1141
1142

    /* Allocate the task-list, if needed. */
1143
    if ( s->tasks == NULL || s->tasks_size < s->tot_cells * space_maxtaskspercell ) {
1144
1145
1146
1147
        if ( s->tasks != NULL )
            free( s->tasks );
        if ( s->tasks_ind != NULL )
            free( s->tasks_ind );
1148
        s->tasks_size = s->tot_cells * space_maxtaskspercell;
1149
        if ( posix_memalign( (void *)&s->tasks , 64 , sizeof(struct task) * s->tasks_size ) != 0 )
Pedro Gonnet's avatar
Pedro Gonnet committed
1150
            error( "Failed to allocate task list." );
1151
1152
1153
        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
1154
    s->nr_tasks = 0;
1155
    s->tasks_next = 0;
Pedro Gonnet's avatar
Pedro Gonnet committed
1156
1157
1158
1159
1160
1161
1162
1163
    
    /* 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;
1164
1165
1166
                ci = &s->cells[cid];
                if ( ci->count == 0 )
                    continue;
Pedro Gonnet's avatar