Add distribute_oattern & search_for_splitted_patte

This commit is contained in:
Raffaele Mignone 2021-03-21 14:16:20 +01:00
parent 5cdbeea5db
commit 10ce1f04e3
Signed by: norangebit
GPG Key ID: F5255658CB220573

63
main.c
View File

@ -18,6 +18,10 @@ void initialize();
void distribute_text();
void distribute_pattern();
void search_for_splitted_pattern(int *residue, int *match_number, int **matches);
int rank, size;
int text_len; // length of all text
int private_text_len; // length of private text
@ -47,38 +51,19 @@ int main() {
printf("%d -> input: %s\n", rank, private_text);
printf("%d -> text len: %d\n", rank, private_text_len);
// distribution of the length of the pattern to each process
MPI_Bcast(&pattern_len, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
// allocation of space for the pattern
// the master has already done so previously
if (rank != MASTER) {
pattern = (char *) malloc(sizeof(char) * (pattern_len + 1));
}
// distribution of the pattern to each process
MPI_Bcast(pattern, pattern_len + 1, MPI_CHAR, MASTER, MPI_COMM_WORLD);
distribute_pattern();
// create and build lps array
int lps[pattern_len];
create_lps(pattern, pattern_len, lps);
int match_number = 0, residue;
int match_number = 0;
int residue;
// search for matches
int *matches = search_pattern(private_text, pattern, lps, &match_number, &residue);
// send the residue to the next process
if (rank + 1 != size) {
MPI_Send(&residue, 1, MPI_INT, rank + 1, DEFAULT_TAG, MPI_COMM_WORLD);
}
// receiving the residue from the previous process
if (rank != 0) {
MPI_Recv(&residue, 1, MPI_INT, rank - 1, DEFAULT_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (residue != 0)
// search for a split match
find_end(residue, pattern, private_text, &matches, &match_number);
}
search_for_splitted_pattern(&residue, &match_number, &matches);
// sending the remain to the last process
// this is necessary for the calculation of shift amount
@ -205,6 +190,36 @@ void distribute_text() {
private_text[private_text_len] = '\0';
}
void distribute_pattern() {
// distribution of the length of the pattern to each process
MPI_Bcast(&pattern_len, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
// allocation of space for the pattern
// the master has already done so previously
if (rank != MASTER) {
pattern = (char *) malloc(sizeof(char) * (pattern_len + 1));
}
// distribution of the pattern to each process
MPI_Bcast(pattern, pattern_len + 1, MPI_CHAR, MASTER, MPI_COMM_WORLD);
}
void search_for_splitted_pattern(int *residue, int *match_number, int **matches) {
// send the residue to the next process
if (rank + 1 != size) {
MPI_Send(residue, 1, MPI_INT, rank + 1, DEFAULT_TAG, MPI_COMM_WORLD);
}
// receiving the residue from the previous process
if (rank != 0) {
MPI_Recv(residue, 1, MPI_INT, rank - 1, DEFAULT_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (*residue != 0)
// search for a split match
find_end(*residue, pattern, private_text, matches, match_number);
}
}
void apply_shift(int shift, int *array, int size) {
for (int i = 0; i < size; ++i) {
array[i] += shift;