-
Notifications
You must be signed in to change notification settings - Fork 0
/
collisions-2.c
235 lines (188 loc) · 7.07 KB
/
collisions-2.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
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
/*
Autor: Mikołaj Błaż
Nr indeksu: 346862
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <mpi.h>
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include "collisions-common.h"
MPI_Datatype MPI_STAR;
#define NEIGHBOURS_CNT 9
static inline int gridIdToRank(int myGridIdX, int myGridIdY, int gridSizeX) {
return myGridIdY * gridSizeX + myGridIdX;
}
void gatherAllStars(int numProcesses, nstars_info_t * myStars, nstars_info_t * allStars, int numAllStars) {
if (allStars->n != numAllStars) {
freeStars(allStars);
allStars->n = numAllStars;
allStars->stars = malloc(numAllStars * sizeof(star_t));
}
int n = myStars->n;
int * countInData = malloc(numProcesses * sizeof(int));
FAIL_IF_NULL(countInData);
int * dispIn = malloc(numProcesses * sizeof(int));
FAIL_IF_NULL(dispIn);
MPI_Allgather(&n, 1, MPI_INT,
countInData, 1, MPI_INT, MPI_COMM_WORLD);
calculateDisplacements(countInData, dispIn, numProcesses);
MPI_Allgatherv(myStars->stars, n, MPI_STAR,
allStars->stars, countInData, dispIn, MPI_STAR, MPI_COMM_WORLD);
free(countInData);
free(dispIn);
}
static int initializeNeighbours(int * neighRanks, int * myGridId, int * gridSize) {
int xId = myGridId[0];
int yId = myGridId[1];
int maxX = gridSize[0] - 1;
int maxY = gridSize[1] - 1;
int gridWidth = gridSize[0];
int neighCnt = 0;
neighRanks[neighCnt++] = gridIdToRank(xId, yId, gridWidth); // send to myself
if (xId > 0) {
neighRanks[neighCnt++] = gridIdToRank(xId - 1, yId, gridWidth);
if (yId > 0) {
neighRanks[neighCnt++] = gridIdToRank(xId - 1, yId - 1, gridWidth);
}
if (yId < maxY) {
neighRanks[neighCnt++] = gridIdToRank(xId - 1, yId + 1, gridWidth);
}
}
if (xId < maxX) {
neighRanks[neighCnt++] = gridIdToRank(xId + 1, yId, gridWidth);
if (yId > 0) {
neighRanks[neighCnt++] = gridIdToRank(xId + 1, yId - 1, gridWidth);
}
if (yId < maxY) {
neighRanks[neighCnt++] = gridIdToRank(xId + 1, yId + 1, gridWidth);
}
}
if (yId > 0) {
neighRanks[neighCnt++] = gridIdToRank(xId, yId - 1, gridWidth);
}
if (yId < maxY) {
neighRanks[neighCnt++] = gridIdToRank(xId, yId + 1, gridWidth);
}
return neighCnt;
}
void prepareOutCount(int numProcesses, int starsCnt, int * countOutData, int * myGridId, int * gridSize) {
static int neighCnt = -1;
static int neighRanks[NEIGHBOURS_CNT];
if (neighCnt == -1) {
neighCnt = initializeNeighbours(neighRanks, myGridId, gridSize);
}
memset(countOutData, 0, numProcesses * sizeof(int)); // initialize to 0
// send data to neigbours (and myself)
for (int neigh = 0; neigh < neighCnt; neigh++) {
countOutData[neighRanks[neigh]] = starsCnt;
}
}
void gatherStars(int numProcesses, nstars_info_t * myStars, nstars_info_t * allStars, int * myGridId, int * gridSize) {
int sumInData;
int * countOutData = malloc(numProcesses * sizeof(int));
FAIL_IF_NULL(countOutData);
int * countInData = malloc(numProcesses * sizeof(int));
FAIL_IF_NULL(countInData);
int * dispOut = malloc(numProcesses * sizeof(int));
FAIL_IF_NULL(dispOut);
int * dispIn = malloc(numProcesses * sizeof(int));
FAIL_IF_NULL(dispIn);
prepareOutCount(numProcesses, myStars->n, countOutData, myGridId, gridSize);
MPI_Alltoall(countOutData, 1, MPI_INT,
countInData, 1, MPI_INT, MPI_COMM_WORLD);
memset(dispOut, 0, numProcesses * sizeof(int)); // all processes receive the same data from array myStars->stars
calculateDisplacements(countInData, dispIn, numProcesses);
sumInData = 0;
for (int i = 0; i < numProcesses; i++) {
sumInData += countInData[i];
}
star_t * starsTmp = malloc(sumInData * sizeof(star_t));
FAIL_IF_NULL(starsTmp);
MPI_Alltoallv(myStars->stars, countOutData, dispOut, MPI_STAR,
starsTmp, countInData, dispIn, MPI_STAR, MPI_COMM_WORLD);
free(countInData);
free(countOutData);
free(dispIn);
free(dispOut);
freeStars(allStars);
allStars->n = sumInData;
allStars->stars = starsTmp;
}
int main(int argc, char * argv[]) {
// stars
int numStars[2];
nstars_info_t myStars[2];
nstars_info_t allStars[2];
float initVelocities[2][2]; // [galaxy][x, y]
float masses[2];
// coordinates
float minPosition[2];
float maxPosition[2];
float worldSize[2];
float blockSize[2];
int gridSize[2]; // {width, height}
int myGridId[2];
int numProcesses; // numProcesses = gridSize[0] * gridSize[1]
int myRank;
// computation
int iter;
int iterNum;
float timeStep;
float maxSimulationTime;
// other
int ret;
bool verbose;
char * filenameGal[2];
/************************* INITIALIZATION ********************************/
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numProcesses);
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
initializeMpiStarType(&MPI_STAR);
ret = parseArguments(argc, argv, gridSize, filenameGal, &timeStep, &maxSimulationTime, &verbose);
if (ret == 0 && numProcesses != gridSize[0] * gridSize[1]) {
fprintf(stderr, "ERROR: Number of active processes must be equal to (%d * %d)!\n", gridSize[0], gridSize[1]);
ret = 7;
}
if (ret != 0) {
MPI_Finalize();
return ret;
}
// Here arguments are correct
readInput(numProcesses, myRank, filenameGal, numStars, initVelocities, masses, myStars);
// now everyone has filled and initialized: numStars, initVelocities, masses, myStars
// process 0 has all stars
computeWorldSize(numProcesses, myRank, gridSize, minPosition, maxPosition, worldSize, blockSize, myGridId, myStars);
allStars[0] = initStars(numStars[0], 0);
allStars[1] = initStars(numStars[1], 1);
// now all processes know all parameters
// we are ready for computation
/************************* COMPUTATION ********************************/
iterNum = (int) (maxSimulationTime / timeStep);
for (iter = 0; iter < iterNum; iter++){
exchangeStars(numProcesses, myRank, &myStars[0], minPosition, blockSize, gridSize[0]);
exchangeStars(numProcesses, myRank, &myStars[1], minPosition, blockSize, gridSize[0]); // gridSize[0]!
if (verbose) {
gatherAllStars(numProcesses, &myStars[0], &allStars[0], numStars[0]);
gatherAllStars(numProcesses, &myStars[1], &allStars[1], numStars[1]);
outputPositions(myRank, allStars, 0, iter);
outputPositions(myRank, allStars, 1, iter);
}
gatherStars(numProcesses, &myStars[0], &allStars[0], myGridId, gridSize);
gatherStars(numProcesses, &myStars[1], &allStars[1], myGridId, gridSize);
computeCoordinates(&myStars[0], allStars, timeStep, masses, (iter == 0 ? initVelocities[0] : NULL), minPosition, maxPosition, worldSize);
computeCoordinates(&myStars[1], allStars, timeStep, masses, (iter == 0 ? initVelocities[1] : NULL), minPosition, maxPosition, worldSize);
}
gatherAllStars(numProcesses, &myStars[0], &allStars[0], numStars[0]);
gatherAllStars(numProcesses, &myStars[1], &allStars[1], numStars[1]);
outputFinalPositions(myRank, allStars);
freeStars(&myStars[0]);
freeStars(&myStars[1]);
freeStars(&allStars[0]);
freeStars(&allStars[1]);
MPI_Finalize();
return 0;
}