-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_ms.c
More file actions
126 lines (110 loc) · 3.5 KB
/
parallel_ms.c
File metadata and controls
126 lines (110 loc) · 3.5 KB
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
/**
* Copyright 2020 Ali Cherry <cmcrc@alicherry.net>
* This file is part of Simple Parallel Algorithms (SPA).
*
* SPA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* SPA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SPA. If not, see <https://www.gnu.org/licenses/>.
*/
#include <mpi.h>
#include <stdio.h>
#include "utils/read_utils.h"
#include "utils/sort_utils.h"
#include "utils/redtree_utils.h"
#include "utils/print_utils.h"
#include "utils/mpi_utils.h"
int main(int argc, char** argv) {
int res;
int i;
int *array;
int len;
int pre_len;
int levels;
int neighbor;
int processors;
int rank;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &processors);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (argc < 2) {
printf("Missing file name argument.");
return 1;
}
res = redtree_levels(processors, &levels);
if (res != 0) {
printf("unable to get levels: %d\n", res);
return res;
}
if (rank == 0) {
res = read_integers_from_file(argv[1], &array, &len);
if (res != 0) {
printf("Unable to read from file: %d\n", res);
return res;
}
printf("This is the master, levels: %d\n", levels);
printf("This is the master, intial len: %d\n", len);
res = mpi_utils_master_distribute_array(processors, array, len);
if (res != 0) {
printf("Unable to distribute array: %d\n", res);
free(array);
return res;
}
array = realloc(array, sizeof(int) * len / processors);
len = len / processors;
} else {
res = mpi_utils_receive_array(0, &array, &len);
if (res != 0) {
printf("unable to receive slice: %d\n", res);
return res;
}
}
// Everybody sort your arrays
res = merge_sort(array, len);
if (res != 0) {
printf("unable to sort my array: %d", res);
free(array);
return res;
}
//printf("Rank %d: my len %d\n", rank, len);
for (i = 0; i < levels; i++) {
neighbor = redtree_get_neighbor(i, rank);
if (!redtree_is_receiver(i, rank)) {
//printf("Rank %d: sending len %d\n", rank, len);
res = mpi_utils_send_array(neighbor, array, len);
if (res != 0) {
printf("Unable to send array: %d\n", res);
free(array);
return res;
}
break;
}
pre_len = len;
res = mpi_utils_combine_arrays(neighbor, &array, &len);
if (res != 0) {
printf("Unable to combine arrays: %d\n", res);
free(array);
return res;
}
res = merge_sorted_partitions(array, pre_len, len - pre_len);
if (res != 0) {
printf("Unable to merge partitions: %d\n", res);
free(array);
return res;
}
}
if (rank == 0) {
printf("This is the master, final len: %d\n", len);
print_integers(array, len);
}
free(array);
MPI_Finalize();
}