-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·82 lines (67 loc) · 1.5 KB
/
main.c
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
#include "nbody_header.h"
long nBodies;
int nthreads;
int nIters;
double dt;
void setArgs(int argc, char** argv) {
/* getopt_long stores the option index here. */
int option_index = 0;
int ch;
static struct option long_options[] = {
//{"abc", 0|no_argument|required_argument|optional_argument, flag, 'a'},
{"particles", required_argument, 0, 'n'},
{"iterations", optional_argument, 0, 'i'},
{"threads", optional_argument, 0, 't'},
{0, 0, 0, 0}
};
/* Detect the end of the options. */
while ( (ch = getopt_long(argc, argv, "n:i:t:", long_options, &option_index)) != -1 ) {
switch (ch) {
case 'n':
nBodies = atoi(optarg);
break;
case 'i':
nIters = atoi(optarg);
break;
case 't':
nthreads = atoi(optarg);
break;
case '?':
printf("Unknown option\n");
break;
case 0:
break;
}
}
}
int main(int argc, char* argv[]) {
// Input Parameters
nBodies = 1000;
nthreads = 1;
nIters = 1000;
dt = 0.2;
setArgs(argc, argv);
// Initialize RNG
srand(42);
// Set number of OMP threads if necessary
#ifdef OPENMP
omp_set_num_threads(nthreads);
#endif
// Initialize MPI
#ifdef MPI
MPI_Init(&argc, &argv);
#endif
// Print Inputs
print_inputs(nBodies, dt, nIters, nthreads);
// Run Problem
#ifdef MPI
run_parallel_problem(nBodies, dt, nIters, "data-nbody-parallel.dat");
#else
run_serial_problem(nBodies, dt, nIters, "data-nbody-serial.dat", randomizeBodies_spiral);
#endif
// Finalize MPI
#ifdef MPI
MPI_Finalize();
#endif
return 0;
}