-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.cpp
184 lines (148 loc) · 5.18 KB
/
main.cpp
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
#include "common.h"
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <mpi.h>
#include <random>
#include <vector>
// =================
// Helper Functions
// =================
// I/O routines
void save(std::ofstream& fsave, particle_t* parts, int num_parts, double size) {
static bool first = true;
if (first) {
fsave << num_parts << " " << size << "\n";
first = false;
}
for (int i = 0; i < num_parts; ++i) {
fsave << parts[i].x << " " << parts[i].y << "\n";
}
fsave << std::endl;
}
// Particle Initialization
void init_particles(particle_t* parts, int num_parts, double size, int part_seed) {
std::random_device rd;
std::mt19937 gen(part_seed ? part_seed : rd());
int sx = (int)ceil(sqrt((double)num_parts));
int sy = (num_parts + sx - 1) / sx;
std::vector<int> shuffle(num_parts);
for (int i = 0; i < shuffle.size(); ++i) {
shuffle[i] = i;
}
for (int i = 0; i < num_parts; ++i) {
// Make sure particles are not spatially sorted
std::uniform_int_distribution<int> rand_int(0, num_parts - i - 1);
int j = rand_int(gen);
int k = shuffle[j];
shuffle[j] = shuffle[num_parts - i - 1];
// Distribute particles evenly to ensure proper spacing
parts[i].x = size * (1. + (k % sx)) / (1 + sx);
parts[i].y = size * (1. + (k / sx)) / (1 + sy);
// Assign random velocities within a bound
std::uniform_real_distribution<float> rand_real(-1.0, 1.0);
parts[i].vx = rand_real(gen);
parts[i].vy = rand_real(gen);
}
for (int i = 0; i < num_parts; ++i) {
parts[i].id = i + 1;
}
}
// Command Line Option Processing
int find_arg_idx(int argc, char** argv, const char* option) {
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], option) == 0) {
return i;
}
}
return -1;
}
int find_int_arg(int argc, char** argv, const char* option, int default_value) {
int iplace = find_arg_idx(argc, argv, option);
if (iplace >= 0 && iplace < argc - 1) {
return std::stoi(argv[iplace + 1]);
}
return default_value;
}
char* find_string_option(int argc, char** argv, const char* option, char* default_value) {
int iplace = find_arg_idx(argc, argv, option);
if (iplace >= 0 && iplace < argc - 1) {
return argv[iplace + 1];
}
return default_value;
}
MPI_Datatype PARTICLE;
// ==============
// Main Function
// ==============
int main(int argc, char** argv) {
// Parse Args
if (find_arg_idx(argc, argv, "-h") >= 0) {
std::cout << "Options:" << std::endl;
std::cout << "-h: see this help" << std::endl;
std::cout << "-n <int>: set number of particles" << std::endl;
std::cout << "-o <filename>: set the output file name" << std::endl;
std::cout << "-s <int>: set particle initialization seed" << std::endl;
return 0;
}
// Open Output File
char* savename = find_string_option(argc, argv, "-o", nullptr);
std::ofstream fsave(savename);
// Init MPI
int num_procs, rank;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// Create MPI Particle Type
const int nitems = 7;
int blocklengths[7] = {1, 1, 1, 1, 1, 1, 1};
MPI_Datatype types[7] = {MPI_UINT64_T, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE,
MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE};
MPI_Aint offsets[7];
offsets[0] = offsetof(particle_t, id);
offsets[1] = offsetof(particle_t, x);
offsets[2] = offsetof(particle_t, y);
offsets[3] = offsetof(particle_t, vx);
offsets[4] = offsetof(particle_t, vy);
offsets[5] = offsetof(particle_t, ax);
offsets[6] = offsetof(particle_t, ay);
MPI_Type_create_struct(nitems, blocklengths, offsets, types, &PARTICLE);
MPI_Type_commit(&PARTICLE);
// Initialize Particles
int num_parts = find_int_arg(argc, argv, "-n", 1000);
int part_seed = find_int_arg(argc, argv, "-s", 0);
double size = sqrt(density * num_parts);
particle_t* parts = new particle_t[num_parts];
if (rank == 0) {
init_particles(parts, num_parts, size, part_seed);
}
MPI_Bcast(parts, num_parts, PARTICLE, 0, MPI_COMM_WORLD);
// Algorithm
auto start_time = std::chrono::steady_clock::now();
init_simulation(parts, num_parts, size, rank, num_procs);
for (int step = 0; step < nsteps; ++step) {
simulate_one_step(parts, num_parts, size, rank, num_procs);
// Save state if necessary
if (fsave.good() && (step % savefreq) == 0) {
gather_for_save(parts, num_parts, size, rank, num_procs);
if (rank == 0) {
save(fsave, parts, num_parts, size);
}
}
}
auto end_time = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = end_time - start_time;
double seconds = diff.count();
// Finalize
if (rank == 0) {
std::cout << "Simulation Time = " << seconds << " seconds for " << num_parts
<< " particles.\n";
}
if (fsave) {
fsave.close();
}
delete[] parts;
MPI_Finalize();
}