File: treecode.c

package info (click to toggle)
massivethreads 1.02-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,924 kB
  • sloc: ansic: 27,814; sh: 4,559; cpp: 3,334; javascript: 1,799; makefile: 1,745; python: 523; asm: 373; perl: 118; lisp: 9
file content (417 lines) | stat: -rw-r--r-- 9,170 bytes parent folder | download
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
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
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
264
265
266
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
 * treecode.c
 * A treecode implementation to achive best scalability on shared memory machines.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <pthread.h>
#include <assert.h>
#include <sys/time.h>

#ifndef	PARALLELIZE
#define PARALLELIZE	1
#endif

#ifndef USE_MEMPOOL
#define USE_MEMPOOL 1
#endif

#define	N_CHILD		8

typedef double real_t;
typedef long idx_t;
typedef unsigned long mot_t;

typedef struct vec {
  double x, y, z;  
} vec_t;

typedef struct mol {
  idx_t id; 
  mot_t mid;
  double mass;
  vec_t acc;
  vec_t pos;
  vec_t vel;
} mol_t;

/* tree */
typedef struct node {
  struct node *child[N_CHILD];
  mol_t v_mol;  /* value of node */
  idx_t n_mol;  /* # of mols in tree */
  idx_t molarr_l, molarr_h; /* array range */
  idx_t morton_l, morton_h; /* morton range */
#if USE_MEMPOOL
  int ith, depth;
  idx_t memp_idx;
#endif
} node_t;

/* simulation data structure */
typedef struct sim {  // simulation instance
  unsigned long n_mol;
  int n_step;
  int steps;
  int depth;
  mol_t *mols;
  node_t *tree;
  node_t **memp; /* memory pool, indexed by morton id + depth */
  idx_t memp_len;
  long int t_init, t_treebuild, t_treetraversal, t_treefree;
} sim_t;
sim_t sim_instance;
sim_t *sim = &sim_instance;
  

/* uitilities */
inline int curr_time_micro(void)
{
  struct timeval tp[1];
  struct timezone tzp[1];
  gettimeofday(tp, tzp);
  return tp->tv_sec * 1000000 + tp->tv_usec;
}

inline void * xmalloc(size_t size)
{
  void *p = malloc(size);
  if (p == NULL) {
    perror("malloc failed");
    abort();
  }
  return p;
}

inline void * xcmalloc(size_t size)
{
  void *p = malloc(size);
  if (p == NULL) {
    perror("malloc failed");
    abort();
  }
  memset(p, 0x0, size);
  return p;
}

/* tree routines */
inline node_t * memp_get(idx_t idx)
{
  node_t *ptr = sim->memp[idx];
  if (ptr) {
    sim->memp[idx] = NULL;
  } else {
    ptr = xmalloc(sizeof(node_t));
    memset(ptr, 0x0, sizeof(node_t));
    ptr->memp_idx = idx;
  }
  return ptr;
}

inline void memp_put(node_t * ptr)
{
  idx_t idx = ptr->memp_idx;
  if (sim->memp[idx]) {
    printf("warning: memory pool put failed for %ld\n", idx);
  } else {
    sim->memp[idx] = ptr;
  }
}

inline int bin_search(mol_t *mols, mot_t key, 
  idx_t imin, idx_t imax, int low_bound)
{
  idx_t imid;
  
  while (imax >= imin) {
    imid = (imin + imax) / 2;
    if (mols[imid].mid < key)
      imin = imid + 1;
    else if (mols[imid].mid > key)
      imax = imid - 1;
    else
      return imid;
  }
  return low_bound ? imin : imax;
}

// Given ml and mh, find a subrange [il, ih] from original [il, ih]
int select_subarr(mol_t *mols, mot_t ml, mot_t mh, idx_t *il, idx_t *ih)
{
  idx_t _low, _high;

  if (mols[*ih].mid < ml || mols[*il].mid > mh) return 1;
  if ((_low = bin_search(mols, ml, *il, *ih, 1)) == -1) return 1;
  if ((_high = bin_search(mols, mh, *il, *ih, 0)) == -1) return 1;
  if (_low > _high) return 1;
  *il = _low;
  *ih = _high;
  return 0;
}

void tree_build_serial(node_t * node)
{
  idx_t curr_il, curr_ih;
  mot_t curr_ml, curr_mh;
  real_t stride;
  mol_t * mols;
  int i;
  
  mols = sim->mols;
  if (node->n_mol == 1) {
    node->v_mol = mols[node->molarr_l];
  } else if (node->n_mol > 1) {
    stride = (node->morton_h - node->morton_l) / N_CHILD;
    curr_il = node->molarr_l;
    curr_ih = node->molarr_h;
    for (i = 0; i < N_CHILD; i++) {
      curr_ml = node->morton_l + stride * i + i;
      curr_mh = curr_ml + stride;
      if (select_subarr(mols, curr_ml, curr_mh, &curr_il, &curr_ih) == 0) {
        /* subspaces is non-empty */
        node->child[i] = xcmalloc(sizeof(node_t));
        node->child[i]->n_mol = curr_ih - curr_il + 1; /* assert(n_mol>0) */
        node->child[i]->molarr_l = curr_il;
        node->child[i]->molarr_h = curr_ih;
        node->child[i]->morton_l = curr_ml;
        node->child[i]->morton_h = curr_mh;
        curr_il = curr_ih;
        
        tree_build_serial(node->child[i]);
      } else {
        curr_il = node->molarr_l;
      }
      curr_ih = node->molarr_h;
    }
  }
}

void * tree_build_parallel(void *args)
{
  pthread_t ths[N_CHILD];
  idx_t curr_il, curr_ih, base, offset, ith;
  mot_t curr_ml, curr_mh;
  real_t stride;
  mol_t * mols;
  node_t * node;
  int i, n_ths, curr_depth;
  
  n_ths = 0;
  mols = sim->mols;
  node = (node_t *) args;
  if (node->n_mol == 1) {
    node->v_mol = mols[node->molarr_l];
  } else if (node->n_mol > 1) {
    stride = (node->morton_h - node->morton_l) / N_CHILD;
    curr_il = node->molarr_l;
    curr_ih = node->molarr_h;

#if USE_MEMPOOL
    curr_depth = node->depth + 1;
    base = (pow(N_CHILD, curr_depth) - 1) / (N_CHILD - 1);
    offset = node->ith * N_CHILD;
#endif

    for (i = 0; i < N_CHILD; i++) {
      curr_ml = node->morton_l + stride * i + i;
      curr_mh = curr_ml + stride;
      if (select_subarr(mols, curr_ml, curr_mh, &curr_il, &curr_ih) == 0) {
        /* subspaces is non-empty */
#if USE_MEMPOOL
        ith = offset + i;
        node->child[i] = memp_get(base + ith);
        node->child[i]->ith = ith;
        node->child[i]->depth = curr_depth;
#else
        node->child[i] = xcmalloc(sizeof(node_t));
#endif
        node->child[i]->n_mol = curr_ih - curr_il + 1; /* assert(n_mol>0) */
        node->child[i]->molarr_l = curr_il;
        node->child[i]->molarr_h = curr_ih;
        node->child[i]->morton_l = curr_ml;
        node->child[i]->morton_h = curr_mh;
        curr_il = curr_ih;
        pthread_create(&ths[n_ths], NULL, tree_build_parallel, node->child[i]);
        n_ths++;
      } else {
        curr_il = node->molarr_l;
      }
      curr_ih = node->molarr_h;
    }
  }
  for (i = 0; i < n_ths; i++)
    pthread_join(ths[i], NULL);
}

void tree_build(void)
{
  node_t * root;

  sim->t_treebuild = curr_time_micro();
#if USE_MEMPOOL
  root = memp_get(0);
  root->ith = 0;
  root->depth = 0;
  root->memp_idx = 0;
#else
  root = (node_t *) xcmalloc(sizeof(node_t));
#endif

  root->n_mol = sim->n_mol;
  root->molarr_l = 0;
  root->molarr_h = sim->n_mol - 1;
  root->morton_l = sim->mols[root->molarr_l].mid;
  root->morton_h = sim->mols[root->molarr_h].mid;
  sim->tree = root;

#if PARALLELIZE
  tree_build_parallel(sim->tree);
#else
  tree_build_serial(sim->tree);
#endif

  sim->t_treebuild = curr_time_micro() - sim->t_treebuild;
}

void tree_free_serial(node_t * node)
{
  int i;

  if (node == NULL) return;

  for (i=0; i < N_CHILD; i++)
    tree_free_serial(node->child[i]);

  free(node);
}

void * tree_free_parallel(void * args)
{
  pthread_t ths[N_CHILD];
  node_t *p = (node_t *) args;
  int i;

  if (p == NULL) return (void *) 0;

  for (i = 0; i < N_CHILD; i++) {
    if (i < N_CHILD - 1)
      pthread_create(&ths[i], NULL, tree_free_parallel, p->child[i]);
    else
      tree_free_parallel(p->child[i]);
  }

  for (i = 0; i < N_CHILD - 1; i++)
    pthread_join(ths[i], NULL);

#if USE_MEMPOOL
  memp_put(p);
#else
  free(p);
#endif
}

void tree_free(void)
{

  sim->t_treefree = curr_time_micro();

#if PARALLELIZE
  tree_free_parallel(sim->tree);
#else
  tree_free_serial(sim->tree);
#endif
  
  sim->t_treefree = curr_time_micro() - sim->t_treefree;
}

void tree_check_walk(node_t *node, int *n, int depth) {
  int i;
  
  if (node == NULL) return;
  
  (*n)++;
  for (i = 0; i < N_CHILD; i++) {
    tree_check_walk(node->child[i], n, depth + 1);
  }
}

void tree_valid_check(void)
{
  int n_total = 0;

  sim->t_treetraversal = curr_time_micro();
  tree_check_walk(sim->tree, &n_total, 0);
  sim->t_treetraversal = curr_time_micro() - sim->t_treetraversal;
  if (n_total != (pow(N_CHILD, sim->depth + 1) - 1) / (N_CHILD - 1))
    printf(" valid check failed: walked %d nodes\n", n_total);
}

/* simulation routines */
void init(int argc, char *argv[])
{
  idx_t i;
  
  curr_time_micro();  /* dummy call to make first timing accurate, Darwin only? */
  
  if (argc < 3) {
    printf("usage: %s DEPTH STEPS\n", argv[0]);
    exit(0);
  }
  sim->depth = atoi(argv[1]);
  sim->n_step = atoi(argv[2]);
  sim->n_mol = pow(N_CHILD, sim->depth); 
  sim->steps = 0;
  sim->t_treebuild = 0;
  sim->mols = xmalloc(sizeof(mol_t) * sim->n_mol);
  for (i = 0; i < sim->n_mol; i++) {
    sim->mols[i].id = i;
    sim->mols[i].mid = i;
  }
#if USE_MEMPOOL
  sim->memp_len = (pow(N_CHILD, sim->depth + 1) - 1) / (N_CHILD - 1);
  sim->memp = xmalloc(sizeof(node_t *) * sim->memp_len);
  memset(sim->memp, 0x0, sizeof(node_t *) * sim->memp_len);
  printf("N=%ld, STEPS=%d, MEMPOOL=%ld\n", 
    sim->n_mol, sim->n_step, sim->memp_len);
#else
  printf("N=%ld, STEPS=%d\n", sim->n_mol, sim->n_step);
#endif
  
}

void finalize(void)
{
  idx_t i;
  free(sim->mols);
#if USE_MEMPOOL
  for (i = 0; i < sim->memp_len; i++)
    free(sim->memp[i]);
  free(sim->memp);
#endif
}

void status(void)
{
  fprintf(stdout, "[%03d] TreeBuild: %ld, TreeTraveral: %ld, TreeFree: %ld\n", sim->steps,
    sim->t_treebuild, sim->t_treetraversal, sim->t_treefree);
}

int main(int argc, char *argv[])
{
  init(argc, argv);
  
  while (sim->steps < sim->n_step) {
    tree_build();
    tree_valid_check();
    tree_free();
    status();
    sim->steps++;
  }

  finalize();

  return 0;
}