space.h 4.78 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*******************************************************************************
 * 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/>.
 * 
 ******************************************************************************/


Pedro Gonnet's avatar
Pedro Gonnet committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197


/* Some constants. */
#define space_maxdepth                  10
#define space_cellallocchunk            1000
#define space_splitratio                0.875
#define space_splitsize                 800
#define task_maxwait                    3
#define task_maxunlock                  39


/* Map shift vector to sortlist. */
extern const int sortlistID[27];
    
    
/* The different task IDs. */
enum taskIDs {
    tid_none = 0,
    tid_sort,
    tid_self,
    tid_pair,
    tid_sub,
    tid_count
    };
extern const char *taskID_names[];
    
    
/* Data of a task. */
struct task {

    int type, flags, wait, rank, done;
    
    int nr_unlock_tasks;
    struct task *unlock_tasks[ task_maxunlock ];

    int nr_unlock_cells;
    struct cell *ci, *cj, *unlock_cells[2];
    
    } __attribute__((aligned (64)));


/* Entry in a list of sorted indices. */
struct entry {
    float d;
    int i;
    };
    
    
/* Data of a single particle. */
struct part {

    /* Particle position. */
    double x[3];
    
    /* Particle cutoff radius. */
    float r;
    
    /* Particle time-step. */
    float dt;
    
    /* Particle ID. */
    int id;
    
    /* Number of pairwise interactions. */
    float count;
    int icount;
    
    } __attribute__((aligned (32)));
    

/* Structure to store the data of a single cell. */
struct cell {

    /* The cell location on the grid. */
    double loc[3];
    
    /* The cell dimensions. */
    double h[3];
    
    /* Max radii in this cell. */
    double r_max;
    
    /* The depth of this cell in the tree. */
    int depth, split;
    
    /* Nr of parts. */
    int count;
    
    /* Pointers to the particle data. */
    struct part *parts;
    
    /* Pointers for the sorted indices. */
    struct entry *sort;
    
    /* Number of pairs associated with this cell. */
    int nr_pairs;
    
    /* Pointers to the next level of cells. */
    struct cell *progeny[8];
    
    /* Parent cell. */
    struct cell *parent;
    
    /* The tasks computing this cell's sorts. */
    struct task *sorts[14];
    
    /* Number of tasks this cell is waiting for and whether it is in use. */
    int wait;
    
    /* Is the data of this cell being used in a sub-cell? */
    int hold;
    
    /* Spin lock for various uses. */
    lock_type lock;
    
    /* Linking pointer for "memory management". */
    struct cell *next;

    } __attribute__((aligned (64)));


/* The space in which the cells reside. */
struct space {

    /* Spatial extent. */
    double dim[3];
    
    /* Cell widths. */
    double h[3], ih[3];
    
    /* The minimum and maximum cutoff radii. */
    double r_min, r_max;
    
    /* Number of cells. */
    int nr_cells, tot_cells;
    
    /* Space dimensions in number of cells. */
    int maxdepth, cdim[3];
    
    /* The (level 0) cells themselves. */
    struct cell *cells;
    
    /* Buffer of unused cells. */
    struct cell *cells_new;
    
    /* The particle data (cells have pointers to this). */
    struct part *parts;
    
    /* The sortlist data (cells hold pointers to these). */
    struct entry *sortlist;
    
    /* The total number of parts in the space. */
    int nr_parts;
    
    /* Is the space periodic? */
    int periodic;
    
    /* The list of tasks. */
    struct task *tasks;
    int nr_tasks, next_task;
    int *tasks_ind;
    lock_type task_lock;
    
    };


/* function prototypes. */
struct cell *space_getcell ( struct space *s );
struct task *space_gettask ( struct space *s );
void space_init ( struct space *s , double dim[3] , struct part *parts , int N , int periodic , double h_max );
void space_maketasks ( struct space *s , int do_sort );
void space_map_cells ( struct space *s , void (*fun)( struct cell *c , void *data ) , void *data );
void space_map_parts ( struct space *s , void (*fun)( struct part *p , struct cell *c , void *data ) , void *data );
void space_recycle ( struct space *s , struct cell *c );