Add distribute_text function

This commit is contained in:
Raffaele Mignone 2021-03-21 14:06:28 +01:00
parent 06a6ded2e7
commit f19c43bb67
Signed by: norangebit
GPG Key ID: F5255658CB220573

45
main.c
View File

@ -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;