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 initialize();
void distribute_text();
int rank, size; int rank, size;
int text_len; // length of all text int text_len; // length of all text
int private_text_len; // length of private text int private_text_len; // length of private text
@ -25,6 +27,7 @@ char *pattern;
int remain = 0; int remain = 0;
int *text_piece; int *text_piece;
int *displacements; int *displacements;
char *private_text;
int main() { int main() {
int *match_numbers; int *match_numbers;
@ -39,25 +42,7 @@ int main() {
if (rank == MASTER) if (rank == MASTER)
initialize(); initialize();
// distribution of the length of the text portion to each process distribute_text();
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';
// sending the remain to the last trial // sending the remain to the last trial
// this is necessary for the calculation of the offset // this is necessary for the calculation of the offset
@ -198,6 +183,28 @@ void initialize() {
text_piece[size - 1] = private_text_len + remain; 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) { void apply_shift(int shift, int *array, int size) {
for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) {
array[i] += shift; array[i] += shift;