general fixes

This commit is contained in:
Raffaele Mignone 2021-03-19 18:50:59 +01:00
parent cc30d00aec
commit 13f6786a13
Signed by: norangebit
GPG Key ID: F5255658CB220573
1 changed files with 22 additions and 26 deletions

48
main.c
View File

@ -18,8 +18,10 @@ 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 *text_piece;
int *match_numbers; int *match_numbers;
int *total_matches;
int total_match_number;
int *displacements; int *displacements;
int remain; int remain = 0;
char *pattern, *text; char *pattern, *text;
MPI_Init(NULL, NULL); MPI_Init(NULL, NULL);
@ -57,7 +59,13 @@ int main() {
MPI_COMM_WORLD); MPI_COMM_WORLD);
private_text[private_text_len] = '\0'; private_text[private_text_len] = '\0';
if (rank == MASTER)
MPI_Send(&remain, 1, MPI_INT, size - 1, DEFAULT_TAG, MPI_COMM_WORLD);
else if (rank == size -1)
MPI_Recv(&remain, 1, MPI_INT, MASTER, DEFAULT_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("%d -> input: %s\n", rank, private_text); printf("%d -> input: %s\n", rank, private_text);
printf("%d -> text len:%d\n", rank, private_text_len);
MPI_Bcast(&pattern_len, 1, MPI_INT, MASTER, MPI_COMM_WORLD); MPI_Bcast(&pattern_len, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
@ -85,7 +93,9 @@ int main() {
find_end(residue, pattern, private_text, &matches, &match_number); find_end(residue, pattern, private_text, &matches, &match_number);
} }
int shift = rank * private_text_len; print_array(matches, match_number);
int shift = rank * (private_text_len - remain);
apply_shift(shift, matches, match_number); apply_shift(shift, matches, match_number);
printf("%d -> result number: %d\n", rank, match_number); printf("%d -> result number: %d\n", rank, match_number);
@ -97,41 +107,27 @@ int main() {
MPI_Gather(&match_number, 1, MPI_INT, match_numbers, 1, MPI_INT, MASTER, MPI_COMM_WORLD); MPI_Gather(&match_number, 1, MPI_INT, match_numbers, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
if (rank == MASTER) { if (rank == MASTER) {
match_number = sum_array(match_numbers, size); total_match_number = sum_array(match_numbers, size);
matches = (int *) realloc(matches, sizeof(int) * match_number);
int offset = 0; total_matches = (int *) malloc(sizeof(int) * total_match_number);
for (int i = 0; i < size; ++i) { displacements[0] = 0;
if (i == MASTER) { for (int i = 0; i < size - 1; ++i) {
offset += match_numbers[i]; displacements[i + 1] = displacements[i] + match_numbers[i];
continue;
}
MPI_Recv(matches + offset, match_numbers[i], MPI_INT, i, DEFAULT_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
offset += match_numbers[i];
} }
} else {
MPI_Send(matches, match_number, MPI_INT, MASTER, DEFAULT_TAG, MPI_COMM_WORLD);
} }
MPI_Gatherv(matches, match_number, MPI_INT, total_matches, match_numbers, displacements, MPI_INT, MASTER,
MPI_COMM_WORLD);
MPI_Finalize(); MPI_Finalize();
if (rank == MASTER) { if (rank == MASTER) {
printf("total matches: %d\n", match_number); printf("total matches: %d\n", total_match_number);
printf("matches index: "); printf("matches index: ");
print_array(matches, match_number); print_array(total_matches, total_match_number);
} }
} }
char *read_text(int *len) {
return read_file("data/text.txt", len);
}
char *read_pattern() {
// TODO
return "mamma";
}
void find_end(int residue, char *pattern, char *text, int **matches, int *match_number) { void find_end(int residue, char *pattern, char *text, int **matches, int *match_number) {
int pattern_index = residue; int pattern_index = residue;
int text_index = 0; int text_index = 0;