Support odd text

This commit is contained in:
Raffaele Mignone 2021-03-18 18:47:24 +01:00
parent f6cfc87ce1
commit bee0b9ef57
Signed by: norangebit
GPG Key ID: F5255658CB220573

24
main.c
View File

@ -20,6 +20,9 @@ int sum_array(int *, int);
int main() { int main() {
int rank, size, private_text_len, text_len, pattern_len; int rank, size, private_text_len, text_len, pattern_len;
int *text_piece;
int *displacements;
int remain;
char *pattern, *text; char *pattern, *text;
MPI_Init(NULL, NULL); MPI_Init(NULL, NULL);
@ -34,14 +37,27 @@ int main() {
printf("pattern: %s\n", pattern); printf("pattern: %s\n", pattern);
text_len = strlen(text); text_len = strlen(text);
pattern_len = strlen(pattern); pattern_len = strlen(pattern);
private_text_len = text_len / size; // TODO: size non divisibile private_text_len = text_len / size;
remain = text_len % size;
text_piece = (int *) malloc(sizeof(int) * size);
displacements = (int *) malloc(sizeof(int) * size);
displacements[0] = 0;
for (int i = 0; i < size - 1; ++i) {
text_piece[i] = private_text_len;
displacements[i + 1] = displacements[i] + private_text_len;
} }
MPI_Bcast(&private_text_len, 1, MPI_INT, MASTER, MPI_COMM_WORLD); text_piece[size - 1] = private_text_len + remain;
}
MPI_Scatter(text_piece, 1, MPI_INT, &private_text_len, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
char *private_text = (char *) malloc(sizeof(char) * (private_text_len + 1)); char *private_text = (char *) malloc(sizeof(char) * (private_text_len + 1));
MPI_Scatter(text, private_text_len, MPI_CHAR, private_text, private_text_len, MPI_CHAR, MASTER, MPI_COMM_WORLD); 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'; private_text[private_text_len] = '\0';
printf("%d -> input: %s\n", rank, private_text); printf("%d -> input: %s\n", rank, private_text);
@ -116,7 +132,7 @@ int main() {
char *read_text() { char *read_text() {
// TODO // TODO
return "abc"; return "ciaoooooo mamma come stai???";
} }
char *read_pattern() { char *read_pattern() {