-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
170 lines (136 loc) · 4.57 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
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "mpi.h"
#include "main.h"
#include "odd_even_sort.h"
#include "shearsort.h"
#define INPUT_FILE_NAME "../src/cuboids.dat"
#define OUTPUT_FILE_NAME "../src/result.dat"
#define ROOT 0
int main(int argc, char* argv[])
{
int my_rank; // rank of process
int num_processes; // number of processes
MPI_Init(&argc, &argv); // start MPI
/* create a type for struct Cuboid */
MPI_Datatype mpi_cuboid_type;
// number of blocks for each parameter
int blocklengths[NUM_OF_PARAMS] = {1, 1, 1, 1};
// offset of each parameter, calculated by size of previous parameters
MPI_Aint displacements[NUM_OF_PARAMS] = { 0, sizeof(int), sizeof(int) + sizeof(float), sizeof(int) + 2 * sizeof(float) };
MPI_Datatype types[NUM_OF_PARAMS] = { MPI_INT, MPI_FLOAT, MPI_FLOAT, MPI_FLOAT };
MPI_Type_create_struct(NUM_OF_PARAMS, blocklengths, displacements, types, &mpi_cuboid_type);
MPI_Type_commit(&mpi_cuboid_type);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); // get process rank
MPI_Comm_size(MPI_COMM_WORLD, &num_processes); // get number of processes
Cuboid* arr = NULL;
Cuboid my_cuboid; // cuboid struct for each process
int size = 0; // size of array
int order = 0; // order of sorting
// ROOT process
if (my_rank == ROOT)
{
FILE* input = fopen(INPUT_FILE_NAME, "r");
if (!input)
{
printf("Could not open input file '%s'\n", INPUT_FILE_NAME);
MPI_Type_free(&mpi_cuboid_type);
MPI_Abort(MPI_COMM_WORLD, 2);
exit(-1);
}
arr = read_cuboids_from_file(input, &size, &order);
fclose(input);
if (!arr)
{
printf("Error reading input file or allocating space\n");
MPI_Type_free(&mpi_cuboid_type);
MPI_Abort(MPI_COMM_WORLD, 2);
exit(-1);
}
if (size != num_processes)
{
printf("Number of processes (%d) is different from number of cuboids in the file (%d)\n", num_processes, size);
free(arr);
MPI_Type_free(&mpi_cuboid_type);
MPI_Abort(MPI_COMM_WORLD, 2);
exit(-1);
}
else if (size % 2 != 0)
{
printf("Can not work with an odd amount of processes at this time\n");
free(arr);
MPI_Type_free(&mpi_cuboid_type);
MPI_Abort(MPI_COMM_WORLD, 2);
exit(-1);
}
}
// broadcast the size to all processes so they could send it to `MPI_Dims_create`
MPI_Bcast(&size, 1, MPI_INT, ROOT, MPI_COMM_WORLD);
// broadcast the orientation to all processess
MPI_Bcast(&order, 1, MPI_INT, ROOT, MPI_COMM_WORLD);
Orientation orientation = order;
// ROOT process send the array of cubiods to children (one for each)
MPI_Scatter(arr, 1, mpi_cuboid_type, &my_cuboid, 1, mpi_cuboid_type, ROOT, MPI_COMM_WORLD);
// initiate cartesian coordinates
int dims[NUM_DIMS] = { 0 };
int periods[NUM_DIMS] = { 0 };
MPI_Comm comm_2d;
MPI_Dims_create(size, NUM_DIMS, dims);
MPI_Cart_create(MPI_COMM_WORLD, NUM_DIMS, dims, periods, 0, &comm_2d);
int my_coords[NUM_DIMS] = { 0 }; // store coordinates of each process
MPI_Cart_coords(comm_2d, my_rank, NUM_DIMS, my_coords);
if (my_rank == 0)
{
printf("Before sorting:\n");
print_cuboids_arr_as_mat(arr, dims[0], dims[1]);
printf("\n");
}
//perform shearsort of the cuboids array
shearsort(num_processes, my_coords[0], my_coords[1], &my_cuboid, orientation, &mpi_cuboid_type, &comm_2d, &get_min_cuboid, &get_max_cuboid);
// Gather back all the cuboids to the ROOT process
MPI_Gather(&my_cuboid, 1, mpi_cuboid_type, arr, 1, mpi_cuboid_type, ROOT, MPI_COMM_WORLD);
if (my_rank == 0)
{
// allocating space to store the sorted values
Cuboid* sorted = malloc(sizeof(Cuboid) * num_processes);
if (!sorted)
{
printf("Error allocating space\n");
MPI_Type_free(&mpi_cuboid_type);
free(arr);
MPI_Abort(MPI_COMM_WORLD, 2);
exit(-1);
}
// collect the values by snake shape
collect_values(sorted, arr, sizeof(Cuboid), dims[0], dims[1], &comm_2d);
printf("After sorting:\n");
print_cuboids_arr_as_mat(arr, dims[0], dims[1]);
printf("\n");
printf("The sorted array:\n");
print_cuboids_arr(sorted, num_processes);
printf("\n");
// open the output file to write the cuboids ids
FILE* output = fopen(OUTPUT_FILE_NAME, "w");
if (!output)
{
printf("Could not open output file '%s'\n", OUTPUT_FILE_NAME);
MPI_Type_free(&mpi_cuboid_type);
free(sorted);
free(arr);
MPI_Abort(MPI_COMM_WORLD, 2);
exit(-1);
}
// if writing ids failed
if (!write_cuboids_ids_to_file(output, sorted, num_processes))
{
printf("Error writing to output file '%s'\n", OUTPUT_FILE_NAME);
}
fclose(output);
free(sorted);
free(arr);
}
MPI_Type_free(&mpi_cuboid_type);
MPI_Finalize();
exit(0);
}