Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 921c37c

Browse files
MPI version
1 parent d2fc8b9 commit 921c37c

File tree

13 files changed

+1379
-0
lines changed

13 files changed

+1379
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CC = mpicc
2+
CFLAGS = -O3 -march=native -std=c99 -Wall
3+
LIBS = -lm
4+
5+
all: fc_seq fc_mpi
6+
7+
fc_seq: fc_seq.c auxiliar.c
8+
$(CC) $(CFLAGS) -o fc_seq fc_seq.c auxiliar.c $(LIBS)
9+
10+
fc_mpi: fc_mpi.c auxiliar.c
11+
$(CC) $(CFLAGS) -o fc_mpi fc_mpi.c auxiliar.c $(LIBS)
12+
13+
clean:
14+
rm -f fc_seq fc_mpi
Lines changed: 345 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
#include "auxiliar.h"
2+
3+
void output_planes_seq(const char* filename, PlaneList* list, int N, int M, double x_max, double y_max)
4+
{
5+
FILE *file = fopen(filename, "w");
6+
if (!file) {
7+
perror("Error opening file");
8+
return;
9+
}
10+
11+
fprintf(file, "# Plane Data\n");
12+
fprintf(file, "# Map: %.6lf, %.6lf : %d %d\n", x_max, y_max, N, M);
13+
fprintf(file, "# Number of Planes: X\n");
14+
fprintf(file, "# idx x y vx vy\n");
15+
16+
PlaneNode* current = list->head;
17+
while (current != NULL) {
18+
int index_i = get_index_i(current->x, x_max, N);
19+
int index_j = get_index_j(current->y, y_max, M);
20+
21+
fprintf(file, "%d %.8f %.8f %.8f %.8f %d %d %d %d\n", current->index_plane, current->x, current->y, current->vx, current->vy, index_i, index_j, current->index_map, current->rank);
22+
current = current->next;
23+
}
24+
fclose(file);
25+
}
26+
27+
void filter_planes(PlaneList* list, double x_max, double y_max)
28+
{
29+
PlaneNode* current = list->head;
30+
while (current != NULL) {
31+
if (current->x <= 1e-3 || current->x >= (x_max-1e-3) || current->y <= 1e-3 || current->y >= (y_max-1e-3)) {
32+
PlaneNode* next = current->next;
33+
if (current->prev != NULL) {
34+
current->prev->next = current->next;
35+
} else {
36+
list->head = current->next;
37+
}
38+
if (current->next != NULL) {
39+
current->next->prev = current->prev;
40+
} else {
41+
list->tail = current->prev;
42+
}
43+
free(current);
44+
current = next;
45+
} else {
46+
current = current->next;
47+
}
48+
}
49+
}
50+
51+
void insert_plane(struct PlaneList* list, int index_plane, int index_map, int rank, double x, double y, double vx, double vy)
52+
{
53+
struct PlaneNode* new_node = (struct PlaneNode*) malloc(sizeof(struct PlaneNode));
54+
new_node->index_plane = index_plane;
55+
new_node->index_map = index_map;
56+
new_node->rank = rank;
57+
new_node->x = x;
58+
new_node->y = y;
59+
new_node->vx = vx;
60+
new_node->vy = vy;
61+
new_node->next = NULL;
62+
63+
if (list->head == NULL) {
64+
list->head = new_node;
65+
list->tail = new_node;
66+
new_node->prev = NULL;
67+
} else {
68+
list->tail->next = new_node;
69+
new_node->prev = list->tail;
70+
list->tail = new_node;
71+
}
72+
}
73+
74+
void remove_plane(PlaneList* list, PlaneNode* node)
75+
{
76+
if (node->prev == NULL) {
77+
list->head = node->next;
78+
} else {
79+
node->prev->next = node->next;
80+
}
81+
82+
if (node->next == NULL) {
83+
list->tail = node->prev;
84+
} else {
85+
node->next->prev = node->prev;
86+
}
87+
88+
free(node);
89+
}
90+
91+
PlaneNode* seek_plane(PlaneList* list, int index_plane)
92+
{
93+
PlaneNode* current = list->head;
94+
while (current != NULL) {
95+
if (current->index_plane == index_plane) {
96+
return current;
97+
}
98+
current = current->next;
99+
}
100+
return NULL;
101+
}
102+
103+
PlaneList copy_plane_list(PlaneList* list) {
104+
PlaneList new_list = {NULL, NULL};
105+
PlaneNode* current = list->head;
106+
while (current != NULL) {
107+
insert_plane(&new_list, current->index_plane, current->index_map, current->rank, current->x, current->y, current->vx, current->vy);
108+
current = current->next;
109+
}
110+
return new_list;
111+
}
112+
113+
void print_planes_debug(PlaneList* list)
114+
{
115+
PlaneNode* current = list->head;
116+
while (current != NULL) {
117+
printf("Plane %d: (%.2f, %.2f) -> (%.2f, %.2f) : [%d, %d]\n", current->index_plane, current->x, current->y, current->vx, current->vy, current->index_map, current->rank);
118+
current = current->next;
119+
}
120+
}
121+
122+
int check_planes_internal(PlaneList* owning_planes_t0, PlaneList* owning_planes, int N, int M, double x_max, double y_max, int max_steps, int* tile_displacements, int size, int debug)
123+
{
124+
PlaneNode* current_t0 = owning_planes_t0->head;
125+
while (current_t0 != NULL) {
126+
current_t0->x = current_t0->x + current_t0->vx*(double)max_steps;
127+
current_t0->y = current_t0->y + current_t0->vy*(double)max_steps;
128+
int index_i = get_index_i(current_t0->x, x_max, N);
129+
int index_j = get_index_j(current_t0->y, y_max, M);
130+
current_t0->index_map = get_index(index_i, index_j, N, M);
131+
if (current_t0->index_map >= 0 && current_t0->index_map < N*M)
132+
current_t0->rank = tile_displacements == NULL || size <= 1 ? 0 : get_rank_from_index(current_t0->index_map, tile_displacements, size);
133+
current_t0 = current_t0->next;
134+
}
135+
136+
int error = 0;
137+
current_t0 = owning_planes_t0->head;
138+
while (current_t0 != NULL) {
139+
double current_x = current_t0->x;
140+
double current_y = current_t0->y;
141+
142+
PlaneNode* found = seek_plane(owning_planes, current_t0->index_plane);
143+
144+
if ((current_x <= 1e-3 || current_x >= (x_max-1e-3) || current_y <= 1e-3 || current_y >= (y_max-1e-3)) && found == NULL) {
145+
if(debug)
146+
printf("Ok! Plane %d out of bounds at step %d\n", current_t0->index_plane, max_steps);
147+
}
148+
else if ((current_x <= 0 || current_x >= x_max || current_y <= 0 || current_y >= y_max) && found != NULL) {
149+
printf("Missing Plane %d! It should be out of bounds at step %d\n", current_t0->index_plane, max_steps);
150+
printf(" State: (%.2f, %.2f) (%d, %d) %d %d, %d\n", found->x, found->y,
151+
get_index_i(found->x, x_max, N),
152+
get_index_j(found->x, y_max, M),
153+
get_index(get_index_i(found->x, x_max, N), get_index_j(found->y, y_max, M), N, M),
154+
found->index_map, found->rank);
155+
printf(" Expected: (%.2f, %.2f) (%d, %d) %d %d, %d\n", current_t0->x, current_t0->y,
156+
get_index_i(current_t0->x, x_max, N),
157+
get_index_j(current_t0->x, y_max, M),
158+
get_index(get_index_i(current_t0->x, x_max, N), get_index_j(found->y, y_max, M), N, M),
159+
current_t0->index_map, current_t0->rank);
160+
error++;
161+
}
162+
else if (found == NULL) {
163+
printf("Missing Plane %d! The plane should be inside the map, but it could not be found.\n", current_t0->index_plane);
164+
printf(" Expected: (%.2f, %.2f) %d, %d\n", current_t0->x, current_t0->y, current_t0->index_map, current_t0->rank);
165+
error++;
166+
}
167+
else if (fabs(current_x-found->x) > 1e-6 || fabs(current_y-found->y) > 1e-6 ) {
168+
printf("Missing Plane %d! It should be at (%.5f, %.5f) at step %d, but it is at (%.5f, %.5f)\n", current_t0->index_plane, current_x, current_y, max_steps, found->x, found->y);
169+
printf(" State: (%.2f, %.2f) (%d, %d) %d %d, %d\n", found->x, found->y,
170+
get_index_i(found->x, x_max, N),
171+
get_index_j(found->x, y_max, M),
172+
get_index(get_index_i(found->x, x_max, N), get_index_j(found->y, y_max, M), N, M),
173+
found->index_map, found->rank);
174+
printf(" Expected: (%.2f, %.2f) (%d, %d) %d %d, %d\n", current_t0->x, current_t0->y,
175+
get_index_i(current_t0->x, x_max, N),
176+
get_index_j(current_t0->x, y_max, M),
177+
get_index(get_index_i(current_t0->x, x_max, N), get_index_j(found->y, y_max, M), N, M),
178+
current_t0->index_map, current_t0->rank);
179+
error++;
180+
}
181+
else if (current_t0->rank != found->rank) {
182+
printf("Missing Plane %d! It should be at rank %d at step %d, but it is at rank %d\n", current_t0->index_plane, current_t0->rank, max_steps, found->rank);
183+
printf(" State: (%.2f, %.2f) (%d, %d) %d %d, %d\n", found->x, found->y,
184+
get_index_i(found->x, x_max, N),
185+
get_index_j(found->x, y_max, M),
186+
get_index(get_index_i(found->x, x_max, N), get_index_j(found->y, y_max, M), N, M),
187+
found->index_map, found->rank);
188+
printf(" Expected: (%.2f, %.2f) (%d, %d) %d %d, %d\n", current_t0->x, current_t0->y,
189+
get_index_i(current_t0->x, x_max, N),
190+
get_index_j(current_t0->x, y_max, M),
191+
get_index(get_index_i(current_t0->x, x_max, N), get_index_j(found->y, y_max, M), N, M),
192+
current_t0->index_map, current_t0->rank);
193+
error++;
194+
}
195+
else {
196+
if (debug)
197+
printf("Ok! Plane %d found at (%.2f, %.2f) at step %d\n", current_t0->index_plane, current_x, current_y, max_steps);
198+
}
199+
200+
current_t0 = current_t0->next;
201+
}
202+
203+
return error;
204+
}
205+
206+
int check_planes_seq(PlaneList* owning_planes_t0, PlaneList* owning_planes, int N, int M, double x_max, double y_max, int max_steps, int debug)
207+
{
208+
return check_planes_internal(owning_planes_t0, owning_planes, N, M, x_max, y_max, max_steps, NULL, 0, debug);
209+
}
210+
211+
void read_planes_seq_full(const char* filename, PlaneList* planes, int* N, int* M, double* x_max, double* y_max)
212+
{
213+
FILE *file = fopen(filename, "r");
214+
if (!file) {
215+
perror("Error opening file");
216+
return;
217+
}
218+
219+
char line[MAX_LINE_LENGTH];
220+
int num_planes = 0;
221+
222+
// Read header
223+
fgets(line, sizeof(line), file);
224+
fgets(line, sizeof(line), file);
225+
sscanf(line, "# Map: %lf, %lf : %d %d", &(*x_max), &(*y_max), N, M);
226+
fgets(line, sizeof(line), file);
227+
sscanf(line, "# Number of Planes: %d", &num_planes);
228+
fgets(line, sizeof(line), file);
229+
230+
// Read grid data
231+
int index = 0;
232+
while (fgets(line, sizeof(line), file)) {
233+
int idx, index_i, index_j, index_map, rank;
234+
double x, y, vx, vy;
235+
if (sscanf(line, "%d %lf %lf %lf %lf %d %d %d %d",
236+
&idx, &x, &y, &vx, &vy, &index_i, &index_j, &index_map, &rank) == 9) {
237+
insert_plane(planes, idx, index_map, rank, x, y, vx, vy);
238+
index++;
239+
}
240+
}
241+
242+
fclose(file);
243+
}
244+
245+
#include <mpi.h>
246+
int check_planes_mpi(PlaneList* owning_planes_t0, PlaneList* owning_planes, int N, int M, double x_max, double y_max, int max_steps, int* tile_displacements, int size, int debug)
247+
{
248+
char filename_comp[256];
249+
sprintf(filename_comp, "planes_comp.txt");
250+
char filename_check[256];
251+
sprintf(filename_check, "planes_check.txt");
252+
253+
output_planes_par_debug(filename_comp, owning_planes, N, M, x_max, y_max);
254+
output_planes_par_debug(filename_check, owning_planes_t0, N, M, x_max, y_max);
255+
256+
int rank;
257+
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
258+
int error = 0;
259+
if (rank == 0)
260+
{
261+
PlaneList r0_owning_planes = {NULL, NULL};
262+
read_planes_seq_full(filename_comp, &r0_owning_planes, &N, &M, &x_max, &y_max);
263+
PlaneList r0_owning_planes_t0 = {NULL, NULL};
264+
read_planes_seq_full(filename_check, &r0_owning_planes_t0, &N, &M, &x_max, &y_max);
265+
266+
error = check_planes_internal(&r0_owning_planes_t0, &r0_owning_planes, N, M, x_max, y_max, max_steps, tile_displacements, size, debug);
267+
}
268+
MPI_Bcast(&error, 1, MPI_INT, 0, MPI_COMM_WORLD);
269+
270+
return error;
271+
}
272+
273+
void output_planes_par_debug(const char* filename, PlaneList* list, int N, int M, double x_max, double y_max)
274+
{
275+
int rank;
276+
int size;
277+
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
278+
MPI_Comm_size(MPI_COMM_WORLD, &size);
279+
280+
if (rank == 0)
281+
{
282+
FILE *file = fopen(filename, "w");
283+
if (!file) {
284+
perror("Error opening file");
285+
return;
286+
}
287+
288+
fprintf(file, "# Plane Data\n");
289+
fprintf(file, "# Map: %.2lf, %.2lf : %d %d\n", x_max, y_max, N, M);
290+
fprintf(file, "# Number of Planes: X\n");
291+
fprintf(file, "# idx x y vx vy\n");
292+
fclose(file);
293+
}
294+
295+
MPI_Barrier(MPI_COMM_WORLD);
296+
297+
for (int i = 0; i < size; i++)
298+
{
299+
if (rank == i)
300+
{
301+
FILE *file = fopen(filename, "a");
302+
if (!file) {
303+
perror("Error opening file");
304+
return;
305+
}
306+
307+
PlaneNode* current = list->head;
308+
while (current != NULL) {
309+
int index_i = get_index_i(current->x, x_max, N);
310+
int index_j = get_index_j(current->y, y_max, M);
311+
312+
fprintf(file, "%d %.8f %.8f %.8f %.8f %d %d %d %d\n", current->index_plane, current->x, current->y, current->vx, current->vy, index_i, index_j, current->index_map, current->rank);
313+
current = current->next;
314+
}
315+
fclose(file);
316+
}
317+
MPI_Barrier(MPI_COMM_WORLD);
318+
}
319+
MPI_Barrier(MPI_COMM_WORLD);
320+
}
321+
322+
void print_planes_par_debug(PlaneList* list)
323+
{
324+
int rank;
325+
int size;
326+
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
327+
MPI_Comm_size(MPI_COMM_WORLD, &size);
328+
329+
for (int p = 0; p < size; p++)
330+
{
331+
if (rank == p) {
332+
PlaneNode* current = list->head;
333+
printf("# Rank %d\n", rank);
334+
335+
while (current != NULL) {
336+
printf(" Plane %d: (%.2f, %.2f) -> (%.2f, %.2f) : [%d, %d]\n", current->index_plane, current->x, current->y, current->vx, current->vy, current->index_map, current->rank);
337+
current = current->next;
338+
}
339+
}
340+
MPI_Barrier(MPI_COMM_WORLD);
341+
}
342+
343+
}
344+
345+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /