-
Notifications
You must be signed in to change notification settings - Fork 0
/
particles.c
384 lines (309 loc) · 8.99 KB
/
particles.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
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
/**
* University of Pittsburgh
* Department of Computer Science
* CS1645: Introduction to HPC Systems
* Student: Nathan Spangler
* Instructor: Bryan Mills, University of Pittsburgh
* MPI particle-interaction code.
*/
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define TAG 7
#define CONSTANT 777
// Particle-interaction constants
#define A 10250000.0
#define B 726515000.5
#define MASS 0.1
#define DELTA 1
// Random initialization constants
#define POSITION 0
#define VELOCITY 1
// Structure for shared properties of a particle (to be included in messages)
struct Particle{
float x;
float y;
float mass;
float fx;
float fy;
};
// Headers for auxiliar functions
float random_value(int type);
void print_particles(struct Particle *particles, int n);
void interact(struct Particle *source, struct Particle *destination);
void compute_interaction(struct Particle *source, struct Particle *destination, int limit);
void compute_self_interaction(struct Particle *set, int size);
void merge(struct Particle *first, struct Particle *second, int limit);
int read_file(struct Particle *set, int size, char *file_name);
int get_next_rank(int rank, int p);
int get_prev_rank(int rank, int p);
int get_OG_rank(int rank, int p);
// Main function
int main(int argc, char** argv){
int myRank;// Rank of process
int p;// Number of processes
int n;// Number of total particles
int previous;// Previous rank in the ring
int next;// Next rank in the ring
int tag = TAG;// Tag for message
int number;// Number of local particles
int count_mpi;
struct Particle *globals;// Array of all particles in the system
struct Particle *locals;// Array of local particles
struct Particle *remotes;// Array of foreign particles
char *file_name;// File name
MPI_Status status;// Return status for receive
int j, rounds, initiator, sender;
double start_time, end_time;
// checking the number of parameters
if(argc < 2){
printf("ERROR: Not enough parameters\n");
printf("Usage: %s <number of particles> [<file>]\n", argv[0]);
exit(1);
}
// getting number of particles
n = atoi(argv[1]);
// initializing MPI structures and checking p is odd
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
if(p % 2 == 0){
p = p - 1;
if(myRank == p){
MPI_Finalize();
return 0;
}
}
srand(myRank+myRank*CONSTANT);
// acquiring memory for particle arrays
//**changed to make it right*******
number = ceil(1.0 * n / p);
locals = (struct Particle *) calloc(number , sizeof(struct Particle));
remotes = (struct Particle *) calloc(number , sizeof(struct Particle));
// checking for file information
if(argc == 3){
if(myRank == 0){
globals = (struct Particle *) calloc((ceil((double)n/p))*p , sizeof(struct Particle));
// YOUR CODE GOES HERE (reading particles from file)
read_file(globals, n, argv[2]);
}
// To send/recv (or scatter/gather) you will need to learn how to
// transfer structs of floats, treat it as a contiguous block of
// floats. Here is an example:
// MPI_Send(locals,
// number * (sizeof (struct Particle)) / sizeof(float),
// MPI_FLOAT,
// next_rank,
// tag,
// MPI_COMM_WORLD)
// MPI_Recv(remotes,
// number * (sizeof (struct Particle)) / sizeof(float),
// MPI_FLOAT,
// previous_rank,
// tag,
// MPI_COMM_WORLD,
// &status);
// hint: because your nodes need to both send and receive you
// might consider asyncronous send/recv.
// YOUR CODE GOES HERE (distributing particles among processors)
count_mpi = number * (sizeof(struct Particle)) /sizeof(float);
MPI_Scatter(globals,
count_mpi,
MPI_FLOAT,
locals,
count_mpi,
MPI_FLOAT,
0,
MPI_COMM_WORLD);
} else {
// random initialization of local particle array
for(j = 0; j < number; j++){
locals[j].x = random_value(POSITION);
locals[j].y = random_value(POSITION);
locals[j].fx = 0.0;
locals[j].fy = 0.0;
locals[j].mass = MASS;
}
}
// starting timer
if(myRank == 0){
start_time = MPI_Wtime();
}
// YOUR CODE GOES HERE (ring algorithm)
MPI_Request request_send[4];
int nextRank = (myRank + 1) % p;
int prevRank = (myRank - 1 + p) % p;
MPI_Isend(locals,
count_mpi,
MPI_FLOAT,
nextRank,
tag,
MPI_COMM_WORLD,
&request_send[0]);
MPI_Recv(remotes,
count_mpi,
MPI_FLOAT,
prevRank,
tag,
MPI_COMM_WORLD,
&status);
compute_interaction(locals, remotes, number);
MPI_Isend(remotes,
count_mpi,
MPI_FLOAT,
nextRank,
tag,
MPI_COMM_WORLD,
&request_send[1]);
for(int i=1; i<(p-1)/2; i++){
MPI_Recv(remotes,
count_mpi,
MPI_FLOAT,
prevRank,
tag,
MPI_COMM_WORLD,
&status);
compute_interaction(locals, remotes, number);
MPI_Isend(remotes,
count_mpi,
MPI_FLOAT,
nextRank,
tag,
MPI_COMM_WORLD,
&request_send[2]);
}
int OG_rank = ((myRank - (p-1)/2 + p) % p); //maybe call get_OG_rank function here and not sure where to increment rank_count
int final_rank = ((myRank + (p-1)/2 + p) % p);
//send remote back to original process
MPI_Isend(remotes,
count_mpi,
MPI_FLOAT,
OG_rank,
tag,
MPI_COMM_WORLD,
&request_send[3]);
MPI_Recv(remotes,
count_mpi,
MPI_FLOAT,
final_rank,
tag,
MPI_COMM_WORLD,
&status);
merge(locals, remotes, number);
compute_self_interaction(locals, number); //not sure about second parameter ******
//Step 8 of algo
MPI_Barrier(MPI_COMM_WORLD); //*********************************double check this****
// stopping timer
if(myRank == 0){
end_time = MPI_Wtime();
printf("Duration: %f seconds\n", (end_time-start_time));
}
// printing information on particles
if(argc == 3){
// YOUR CODE GOES HERE (collect particles at rank 0)
count_mpi = number * (sizeof(struct Particle)) /sizeof(float);
MPI_Gather(locals,
count_mpi,
MPI_FLOAT,
globals,
count_mpi,
MPI_FLOAT,
0,
MPI_COMM_WORLD);
if(myRank == 0) {
print_particles(globals,n);
}
}
// finalizing MPI structures
MPI_Finalize();
}
// Function for random value generation
float random_value(int type){
float value;
switch(type){
case POSITION:
value = (float)rand() / (float)RAND_MAX * 100.0;
break;
case VELOCITY:
value = (float)rand() / (float)RAND_MAX * 10.0;
break;
default:
value = 1.1;
}
return value;
}
// Function for printing out the particle array
void print_particles(struct Particle *particles, int n){
int j;
printf("Index\tx\ty\tmass\tfx\tfy\n");
for(j = 0; j < n; j++){
printf("%d\t%f\t%f\t%f\t%f\t%f\n",j,particles[j].x,particles[j].y,particles[j].mass,particles[j].fx,particles[j].fy);
}
}
// Function for computing interaction among two particles
// There is an extra test for interaction of identical particles, in which case there is no effect over the destination
void interact(struct Particle *first, struct Particle *second){
float rx,ry,r,fx,fy,f;
// computing base values
rx = first->x - second->x;
ry = first->y - second->y;
r = sqrt(rx*rx + ry*ry);
if(r == 0.0)
return;
f = A / pow(r,6) - B / pow(r,12);
fx = f * rx / r;
fy = f * ry / r;
// updating sources's structure
first->fx = first->fx + fx;
first->fy = first->fy + fy;
// updating destination's structure
second->fx = second->fx - fx;
second->fy = second->fy - fy;
}
// Function for computing interaction between two sets of particles
void compute_interaction(struct Particle *first, struct Particle *second, int limit){
int j,k;
for(j = 0; j < limit; j++){
for(k = 0; k < limit; k++){
interact(&first[j],&second[k]);
}
}
}
// Function for computing interaction between two sets of particles
void compute_self_interaction(struct Particle *set, int size){
int j,k;
for(j = 0; j < size; j++){
for(k = j+1; k < size; k++){
interact(&set[j],&set[k]);
}
}
}
// Function to merge two particle arrays
// Permanent changes reside only in first array
void merge(struct Particle *first, struct Particle *second, int limit){
int j;
for(j = 0; j < limit; j++){
first[j].fx += second[j].fx;
first[j].fy += second[j].fy;
}
}
// Reads particle information from a text file
int read_file(struct Particle *set, int size, char *file_name){
FILE *ifp, *ofp;
char *mode = "r";
ifp = fopen(file_name, mode);
if (ifp == NULL) {
fprintf(stderr, "Can't open input file!\n");
return 1;
}
// reading particle values
for(int i=0; i<size; i++){
fscanf(ifp, "%f\t%f\t%f", &set[i].x, &set[i].y, &set[i].mass);
set[i].fx = 0.0;
set[i].fy = 0.0;
}
// closing file
fclose(ifp);
return 0;
}