diff --git a/main.c b/main.c index c0307f2..22c75da 100644 --- a/main.c +++ b/main.c @@ -22,6 +22,8 @@ void distribute_pattern(); void search_for_splitted_pattern(int *residue, int *match_number, int **matches); +void collect_results(int *match_number, int *matches); + int rank, size; int text_len; // length of all text int private_text_len; // length of private text @@ -32,12 +34,11 @@ int remain = 0; int *text_piece; int *displacements; char *private_text; +int *match_numbers; +int *total_matches; +int total_match_number; int main() { - int *match_numbers; - int *total_matches; - int total_match_number; - MPI_Init(NULL, NULL); MPI_Comm_size(MPI_COMM_WORLD, &size); @@ -78,38 +79,8 @@ int main() { printf("%d -> result number: %d\n", rank, match_number); - 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 - ); - + collect_results(&match_number, matches); + MPI_Finalize(); if (rank == MASTER) { @@ -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) { for (int i = 0; i < size; ++i) { array[i] += shift;