From 59053ba109e05624923d7d1a47abbd4ed8239418 Mon Sep 17 00:00:00 2001 From: norangebit Date: Sun, 21 Mar 2021 14:28:25 +0100 Subject: [PATCH] add calculate_absolute_indices function --- main.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/main.c b/main.c index 22c75da..4f209e6 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 calculate_absolute_indices(int match_number, int *matches); + void collect_results(int *match_number, int *matches); int rank, size; @@ -58,29 +60,19 @@ int main() { int lps[pattern_len]; create_lps(pattern, pattern_len, lps); + // search for matches int match_number = 0; int residue; - - // search for matches int *matches = search_pattern(private_text, pattern, lps, &match_number, &residue); search_for_splitted_pattern(&residue, &match_number, &matches); - // sending the remain to the last process - // this is necessary for the calculation of shift amount - if (rank == MASTER) - MPI_Send(&remain, 1, MPI_INT, size - 1, DEFAULT_TAG, MPI_COMM_WORLD); - else if (rank == size - 1) - MPI_Recv(&remain, 1, MPI_INT, MASTER, DEFAULT_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); - - // transformation of match indices from relative to absolute position. - int shift = rank * (private_text_len - remain); // the rest is zero for all but the last process - apply_shift(shift, matches, match_number); + calculate_absolute_indices(match_number, matches); printf("%d -> result number: %d\n", rank, match_number); collect_results(&match_number, matches); - + MPI_Finalize(); if (rank == MASTER) { @@ -191,6 +183,19 @@ void search_for_splitted_pattern(int *residue, int *match_number, int **matches) } +void calculate_absolute_indices(int match_number, int *matches) { + // sending the remain to the last process + // this is necessary for the calculation of shift amount + if (rank == MASTER) + MPI_Send(&remain, 1, MPI_INT, size - 1, DEFAULT_TAG, MPI_COMM_WORLD); + else if (rank == size - 1) + MPI_Recv(&remain, 1, MPI_INT, MASTER, DEFAULT_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + + // transformation of match indices from relative to absolute position. + int shift = rank * (private_text_len - remain); // the rest is zero for all but the last process + apply_shift(shift, matches, match_number); +} + void collect_results(int *match_number, int *matches) { if (rank == MASTER) { // allocation for the array containing the number of matches of each process