add initialize

This commit is contained in:
Raffaele Mignone 2021-03-21 12:33:22 +01:00
parent fa007ceef0
commit 954556f28a
Signed by: norangebit
GPG Key ID: F5255658CB220573

77
main.c
View File

@ -14,48 +14,30 @@ void apply_shift(int, int *, int);
int sum_array(int *, int); int sum_array(int *, int);
void initialize();
int rank, size;
int text_len; // length of all text
int private_text_len; // length of private text
int pattern_len; //length of the pattern
char *text;
char *pattern;
int remain = 0;
int *text_piece;
int *displacements;
int main() { int main() {
int rank, size, private_text_len, text_len, pattern_len;
int *text_piece;
int *match_numbers; int *match_numbers;
int *total_matches; int *total_matches;
int total_match_number; int total_match_number;
int *displacements;
int remain = 0;
char *pattern, *text;
MPI_Init(NULL, NULL); MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == MASTER) { if (rank == MASTER)
text = "text";//read_file("data/text.txt", &text_len); initialize();
pattern = "pattern";//read_file("data/pattern.txt", &pattern_len);
text_len = strlen(text);
pattern_len = strlen(pattern);
printf("text: %s\n", text);
printf("pattern: %s\n", pattern);
private_text_len = text_len / size;
remain = text_len % size;
if (pattern_len > private_text_len)
MPI_Abort(MPI_COMM_WORLD, 1);
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;
}
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); MPI_Scatter(text_piece, 1, MPI_INT, &private_text_len, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
@ -71,7 +53,8 @@ int main() {
MPI_Recv(&remain, 1, MPI_INT, MASTER, DEFAULT_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); 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); printf("%d -> text len: %d\n", rank, private_text_len);
printf("%d -> text len_: %d\n", rank, strlen(private_text));
MPI_Bcast(&pattern_len, 1, MPI_INT, MASTER, MPI_COMM_WORLD); MPI_Bcast(&pattern_len, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
@ -155,6 +138,34 @@ void find_end(int residue, char *pattern, char *text, int **matches, int *match_
(*matches)[*match_number - 1] = -residue; (*matches)[*match_number - 1] = -residue;
} }
void initialize() {
text = "questo è un test di esempio. Qui c'è la prima occorenza mamma e qui la seconda mamma";//read_file("data/text.txt", &text_len);
pattern = "mamma";//read_file("data/pattern.txt", &pattern_len);
text_len = strlen(text);
pattern_len = strlen(pattern);
printf("text: %s\n", text);
printf("pattern: %s\n", pattern);
private_text_len = text_len / size;
remain = text_len % size;
if (pattern_len > private_text_len)
MPI_Abort(MPI_COMM_WORLD, 1);
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;
}
text_piece[size - 1] = private_text_len + remain;
}
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;