From 10ce1f04e339d3970529fbaf564bc2701a37fef7 Mon Sep 17 00:00:00 2001 From: norangebit Date: Sun, 21 Mar 2021 14:16:20 +0100 Subject: [PATCH] Add distribute_oattern & search_for_splitted_patte --- main.c | 63 ++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/main.c b/main.c index 0d6a80e..c0307f2 100644 --- a/main.c +++ b/main.c @@ -18,6 +18,10 @@ void initialize(); void distribute_text(); +void distribute_pattern(); + +void search_for_splitted_pattern(int *residue, int *match_number, int **matches); + int rank, size; int text_len; // length of all text int private_text_len; // length of private text @@ -47,38 +51,19 @@ int main() { printf("%d -> input: %s\n", rank, private_text); printf("%d -> text len: %d\n", rank, private_text_len); - // distribution of the length of the pattern to each process - MPI_Bcast(&pattern_len, 1, MPI_INT, MASTER, MPI_COMM_WORLD); - - // allocation of space for the pattern - // the master has already done so previously - if (rank != MASTER) { - pattern = (char *) malloc(sizeof(char) * (pattern_len + 1)); - } - - // distribution of the pattern to each process - MPI_Bcast(pattern, pattern_len + 1, MPI_CHAR, MASTER, MPI_COMM_WORLD); + distribute_pattern(); // create and build lps array int lps[pattern_len]; create_lps(pattern, pattern_len, lps); - int match_number = 0, residue; + int match_number = 0; + int residue; + // search for matches int *matches = search_pattern(private_text, pattern, lps, &match_number, &residue); - // send the residue to the next process - if (rank + 1 != size) { - MPI_Send(&residue, 1, MPI_INT, rank + 1, DEFAULT_TAG, MPI_COMM_WORLD); - } - - // receiving the residue from the previous process - if (rank != 0) { - MPI_Recv(&residue, 1, MPI_INT, rank - 1, DEFAULT_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); - if (residue != 0) - // search for a split match - find_end(residue, pattern, private_text, &matches, &match_number); - } + search_for_splitted_pattern(&residue, &match_number, &matches); // sending the remain to the last process // this is necessary for the calculation of shift amount @@ -205,6 +190,36 @@ void distribute_text() { private_text[private_text_len] = '\0'; } +void distribute_pattern() { + // distribution of the length of the pattern to each process + MPI_Bcast(&pattern_len, 1, MPI_INT, MASTER, MPI_COMM_WORLD); + + // allocation of space for the pattern + // the master has already done so previously + if (rank != MASTER) { + pattern = (char *) malloc(sizeof(char) * (pattern_len + 1)); + } + + // distribution of the pattern to each process + MPI_Bcast(pattern, pattern_len + 1, MPI_CHAR, MASTER, MPI_COMM_WORLD); +} + +void search_for_splitted_pattern(int *residue, int *match_number, int **matches) { + // send the residue to the next process + if (rank + 1 != size) { + MPI_Send(residue, 1, MPI_INT, rank + 1, DEFAULT_TAG, MPI_COMM_WORLD); + } + + // receiving the residue from the previous process + if (rank != 0) { + MPI_Recv(residue, 1, MPI_INT, rank - 1, DEFAULT_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + if (*residue != 0) + // search for a split match + find_end(*residue, pattern, private_text, matches, match_number); + } + +} + void apply_shift(int shift, int *array, int size) { for (int i = 0; i < size; ++i) { array[i] += shift;