add coolect_results function

This commit is contained in:
Raffaele Mignone 2021-03-21 14:23:56 +01:00
parent 10ce1f04e3
commit 4f62118a78
Signed by: norangebit
GPG Key ID: F5255658CB220573

71
main.c
View File

@ -22,6 +22,8 @@ void distribute_pattern();
void search_for_splitted_pattern(int *residue, int *match_number, int **matches); void search_for_splitted_pattern(int *residue, int *match_number, int **matches);
void collect_results(int *match_number, int *matches);
int rank, size; int rank, size;
int text_len; // length of all text int text_len; // length of all text
int private_text_len; // length of private text int private_text_len; // length of private text
@ -32,12 +34,11 @@ int remain = 0;
int *text_piece; int *text_piece;
int *displacements; int *displacements;
char *private_text; char *private_text;
int main() {
int *match_numbers; int *match_numbers;
int *total_matches; int *total_matches;
int total_match_number; int total_match_number;
int main() {
MPI_Init(NULL, NULL); MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_size(MPI_COMM_WORLD, &size);
@ -78,37 +79,7 @@ int main() {
printf("%d -> result number: %d\n", rank, match_number); printf("%d -> result number: %d\n", rank, match_number);
if (rank == MASTER) { collect_results(&match_number, matches);
// allocation for the array containing the number of matches of each process
match_numbers = (int *) malloc(sizeof(int) * size);
}
// collection of the number of matches from each process
MPI_Gather(&match_number, 1, MPI_INT, match_numbers, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
if (rank == MASTER) {
// preparation of the data structures needed to receive match indices from all processes
total_match_number = sum_array(match_numbers, size);
total_matches = (int *) malloc(sizeof(int) * total_match_number);
displacements[0] = 0;
for (int i = 0; i < size - 1; ++i) {
displacements[i + 1] = displacements[i] + match_numbers[i];
}
}
// collection of match indices from each process
// gatherv is necessary because each process will have a different number of matches
MPI_Gatherv(
matches,
match_number,
MPI_INT,
total_matches,
match_numbers,
displacements,
MPI_INT, MASTER,
MPI_COMM_WORLD
);
MPI_Finalize(); MPI_Finalize();
@ -220,6 +191,40 @@ void search_for_splitted_pattern(int *residue, int *match_number, int **matches)
} }
void collect_results(int *match_number, int *matches) {
if (rank == MASTER) {
// allocation for the array containing the number of matches of each process
match_numbers = (int *) malloc(sizeof(int) * size);
}
// collection of the number of matches from each process
MPI_Gather(match_number, 1, MPI_INT, match_numbers, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
if (rank == MASTER) {
// preparation of the data structures needed to receive match indices from all processes
total_match_number = sum_array(match_numbers, size);
total_matches = (int *) malloc(sizeof(int) * total_match_number);
displacements[0] = 0;
for (int i = 0; i < size - 1; ++i) {
displacements[i + 1] = displacements[i] + match_numbers[i];
}
}
// collection of match indices from each process
// gatherv is necessary because each process will have a different number of matches
MPI_Gatherv(
matches,
*match_number,
MPI_INT,
total_matches,
match_numbers,
displacements,
MPI_INT, MASTER,
MPI_COMM_WORLD
);
}
void apply_shift(int shift, int *array, int size) { void apply_shift(int shift, int *array, int size) {
for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) {
array[i] += shift; array[i] += shift;