From f19c43bb67ddd93177f7adbd48b621be83bb3c84 Mon Sep 17 00:00:00 2001 From: norangebit Date: Sun, 21 Mar 2021 14:06:28 +0100 Subject: [PATCH] Add distribute_text function --- main.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/main.c b/main.c index 95a89d9..862e159 100644 --- a/main.c +++ b/main.c @@ -16,6 +16,8 @@ int sum_array(int *, int); void initialize(); +void distribute_text(); + int rank, size; int text_len; // length of all text int private_text_len; // length of private text @@ -25,6 +27,7 @@ char *pattern; int remain = 0; int *text_piece; int *displacements; +char *private_text; int main() { int *match_numbers; @@ -39,25 +42,7 @@ int main() { if (rank == MASTER) initialize(); - // distribution of the length of the text portion to each process - MPI_Scatter(text_piece, 1, MPI_INT, &private_text_len, 1, MPI_INT, MASTER, MPI_COMM_WORLD); - - // allocation of space for text - char *private_text = (char *) malloc(sizeof(char) * (private_text_len + 1)); - - // distribution of the text portion to each process - // scatterv is necessary because the last process receives a larger portion of text in case it is not perfectly divisible. - MPI_Scatterv( - text, - text_piece, - displacements, - MPI_CHAR, - private_text, - private_text_len, - MPI_CHAR, MASTER, - MPI_COMM_WORLD - ); - private_text[private_text_len] = '\0'; + distribute_text(); // sending the remain to the last trial // this is necessary for the calculation of the offset @@ -198,6 +183,28 @@ void initialize() { text_piece[size - 1] = private_text_len + remain; } +void distribute_text() { + // distribution of the length of the text portion to each process + MPI_Scatter(text_piece, 1, MPI_INT, &private_text_len, 1, MPI_INT, MASTER, MPI_COMM_WORLD); + + // allocation of space for text + private_text = (char *) malloc(sizeof(char) * (private_text_len + 1)); + + // distribution of the text portion to each process + // scatterv is necessary because the last process receives a larger portion of text in case it is not perfectly divisible. + MPI_Scatterv( + text, + text_piece, + displacements, + MPI_CHAR, + private_text, + private_text_len, + MPI_CHAR, MASTER, + MPI_COMM_WORLD + ); + private_text[private_text_len] = '\0'; +} + void apply_shift(int shift, int *array, int size) { for (int i = 0; i < size; ++i) { array[i] += shift;