-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmortonsort.cc
97 lines (84 loc) · 2.16 KB
/
mortonsort.cc
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
#ifdef _OPENMP
#include <parallel/algorithm>
#include <parallel/numeric>
#else
#include <algorithm>
#endif
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <getopt.h>
#include "MortonKeyCalculator.hh"
using namespace std;
void makeCluster(Body *bodies, int N, double cx, double cy, double range){
for(int i = 0; i < N; i++){
double x;
double y;
do{
x = -1.0 + 2.0 * rand()/(RAND_MAX+1.0);
y = -1.0 + 2.0 * rand()/(RAND_MAX+1.0);
}while(x * x + y * y > 1.0);
x = cx + range * x;
y = cy + range * y;
double vx = 0.0;
double vy = 0.0;
double m = 1.0e-5;
bodies[i] = Body(x,y,vx,vy,m);
}
}
#define USAGE \
"usage:\n" \
" driver [options]\n" \
"options:\n" \
" -n [nbodies] Number of bodies (Default: 0.1)\n" \
" -h Show this help message\n"
int main(int argc, char **argv){
int nbodies = 10;
double max_range = 2.0e-5;
int seed = 5;
char *filename = NULL;
int option_char;
while ((option_char = getopt(argc, argv, "o:n:s:r:h")) != -1) {
switch (option_char) {
case 'o':
filename = optarg;
break;
case 'n':
nbodies = atoi(optarg);
break;
case 's':
seed = atoi(optarg);
break;
case 'r':
max_range = atof(optarg);
break;
case 'h':
fprintf(stderr, "%s\n", USAGE);
exit(EXIT_SUCCESS);
break;
default:
fprintf(stderr, "%s\n", USAGE);
exit(EXIT_FAILURE);
}
}
Body *bodies = new Body[nbodies];
srand(seed);
makeCluster(bodies, nbodies, 0.0, 0.0, max_range);
MortonKeyCalculator mkcalc(bodies, nbodies);
#ifdef _OPENMP
__gnu_parallel::sort(bodies, bodies + nbodies, mkcalc);
#else
std::sort(bodies, bodies + nbodies, mkcalc);
#endif
if(!filename){
for(int i = 0; i < nbodies; i++)
mkcalc.printKey(cout, bodies[i]);
}
else{
std::ofstream os(filename);
for(int i = 0; i < nbodies; i++)
mkcalc.printKey(os, bodies[i]);
}
delete[] bodies;
return EXIT_SUCCESS;
}