2021-03-15 20:04:23 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "kmp.h"
|
|
|
|
#include "util.h"
|
2021-03-18 16:47:32 +00:00
|
|
|
#include <mpi.h>
|
|
|
|
#include <malloc.h>
|
2021-03-15 20:04:23 +00:00
|
|
|
|
2021-03-18 16:47:32 +00:00
|
|
|
#define MASTER 0
|
|
|
|
#define DEFAULT_TAG 17
|
|
|
|
|
|
|
|
void find_end(int, char *, char *, int **, int *);
|
|
|
|
|
|
|
|
void apply_shift(int, int *, int);
|
|
|
|
|
|
|
|
int sum_array(int *, int);
|
2021-03-15 20:04:23 +00:00
|
|
|
|
|
|
|
int main() {
|
2021-03-18 16:47:32 +00:00
|
|
|
int rank, size, private_text_len, text_len, pattern_len;
|
2021-03-18 17:47:24 +00:00
|
|
|
int *text_piece;
|
2021-03-19 17:05:38 +00:00
|
|
|
int *match_numbers;
|
2021-03-18 17:47:24 +00:00
|
|
|
int *displacements;
|
|
|
|
int remain;
|
2021-03-18 16:47:32 +00:00
|
|
|
char *pattern, *text;
|
|
|
|
|
|
|
|
MPI_Init(NULL, NULL);
|
|
|
|
|
|
|
|
MPI_Comm_size(MPI_COMM_WORLD, &size);
|
|
|
|
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
|
|
|
|
|
|
|
if (rank == MASTER) {
|
2021-03-19 16:56:57 +00:00
|
|
|
text = read_file("data/text.txt", &text_len);
|
|
|
|
pattern = read_file("data/pattern.txt", &pattern_len);
|
|
|
|
|
2021-03-18 16:47:32 +00:00
|
|
|
printf("text: %s\n", text);
|
|
|
|
printf("pattern: %s\n", pattern);
|
2021-03-19 16:56:57 +00:00
|
|
|
|
2021-03-18 17:47:24 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
text_piece[size - 1] = private_text_len + remain;
|
2021-03-18 16:47:32 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 17:47:24 +00:00
|
|
|
MPI_Scatter(text_piece, 1, MPI_INT, &private_text_len, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
|
2021-03-18 16:47:32 +00:00
|
|
|
|
|
|
|
char *private_text = (char *) malloc(sizeof(char) * (private_text_len + 1));
|
2021-03-15 20:04:23 +00:00
|
|
|
|
2021-03-18 17:47:24 +00:00
|
|
|
MPI_Scatterv(text, text_piece, displacements, MPI_CHAR, private_text, private_text_len, MPI_CHAR, MASTER,
|
|
|
|
MPI_COMM_WORLD);
|
2021-03-18 16:47:32 +00:00
|
|
|
private_text[private_text_len] = '\0';
|
|
|
|
|
|
|
|
printf("%d -> input: %s\n", rank, private_text);
|
|
|
|
|
|
|
|
MPI_Bcast(&pattern_len, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
|
|
|
|
|
|
|
|
if (rank != MASTER) {
|
|
|
|
pattern = (char *) malloc(sizeof(char) * (pattern_len + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
MPI_Bcast(pattern, pattern_len + 1, MPI_CHAR, MASTER, MPI_COMM_WORLD);
|
2021-03-15 20:04:23 +00:00
|
|
|
|
|
|
|
int lps[pattern_len];
|
|
|
|
|
2021-03-18 16:47:32 +00:00
|
|
|
create_lps(pattern, pattern_len, lps);
|
|
|
|
|
|
|
|
int match_number = 0, residue;
|
|
|
|
int *matches = search_pattern(private_text, pattern, lps, &match_number, &residue);
|
|
|
|
|
|
|
|
|
|
|
|
if (rank + 1 != size) {
|
|
|
|
MPI_Send(&residue, 1, MPI_INT, rank + 1, DEFAULT_TAG, MPI_COMM_WORLD);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rank != 0) {
|
|
|
|
MPI_Recv(&residue, 1, MPI_INT, rank - 1, DEFAULT_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
|
|
|
if (residue != 0)
|
|
|
|
find_end(residue, pattern, private_text, &matches, &match_number);
|
|
|
|
}
|
|
|
|
|
|
|
|
int shift = rank * private_text_len;
|
|
|
|
apply_shift(shift, matches, match_number);
|
|
|
|
|
|
|
|
printf("%d -> result number: %d\n", rank, match_number);
|
2021-03-15 20:04:23 +00:00
|
|
|
|
2021-03-18 16:47:32 +00:00
|
|
|
if (rank == MASTER) {
|
2021-03-19 17:05:38 +00:00
|
|
|
match_numbers = (int *) malloc(sizeof(int) * size);
|
|
|
|
}
|
2021-03-18 16:47:32 +00:00
|
|
|
|
2021-03-19 17:05:38 +00:00
|
|
|
MPI_Gather(&match_number, 1, MPI_INT, match_numbers, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
|
2021-03-18 16:47:32 +00:00
|
|
|
|
2021-03-19 17:05:38 +00:00
|
|
|
if (rank == MASTER) {
|
2021-03-18 16:47:32 +00:00
|
|
|
match_number = sum_array(match_numbers, size);
|
|
|
|
matches = (int *) realloc(matches, sizeof(int) * match_number);
|
|
|
|
|
|
|
|
int offset = 0;
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
if (i == MASTER) {
|
|
|
|
offset += 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_Finalize();
|
|
|
|
|
|
|
|
if (rank == MASTER) {
|
|
|
|
printf("total matches: %d\n", match_number);
|
|
|
|
printf("matches index: ");
|
|
|
|
print_array(matches, match_number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 16:56:57 +00:00
|
|
|
char *read_text(int *len) {
|
|
|
|
return read_file("data/text.txt", len);
|
2021-03-18 16:47:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *read_pattern() {
|
|
|
|
// TODO
|
|
|
|
return "mamma";
|
|
|
|
}
|
|
|
|
|
|
|
|
void find_end(int residue, char *pattern, char *text, int **matches, int *match_number) {
|
|
|
|
int pattern_index = residue;
|
|
|
|
int text_index = 0;
|
|
|
|
int text_len = strlen(text);
|
|
|
|
int pattern_len = strlen(pattern);
|
|
|
|
|
2021-03-19 16:56:57 +00:00
|
|
|
// TODO: match splitted on 3+ node
|
2021-03-18 16:47:32 +00:00
|
|
|
if (pattern_len - residue > text_len) return;
|
|
|
|
|
|
|
|
while (pattern_index < pattern_len && pattern[pattern_index] == text[text_index]) {
|
|
|
|
pattern_index++;
|
|
|
|
text_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pattern_index != pattern_len) return;
|
|
|
|
|
|
|
|
*match_number = *match_number + 1;
|
|
|
|
*matches = (int *) realloc(*matches, *match_number * sizeof(int));
|
|
|
|
(*matches)[*match_number - 1] = -residue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void apply_shift(int shift, int *array, int size) {
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
array[i] += shift;
|
|
|
|
}
|
|
|
|
}
|
2021-03-15 20:04:23 +00:00
|
|
|
|
2021-03-18 16:47:32 +00:00
|
|
|
int sum_array(int *array, int size) {
|
|
|
|
int sum = 0;
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
sum += array[i];
|
|
|
|
}
|
2021-03-15 20:04:23 +00:00
|
|
|
|
2021-03-18 16:47:32 +00:00
|
|
|
return sum;
|
2021-03-15 20:04:23 +00:00
|
|
|
}
|
2021-03-19 16:56:57 +00:00
|
|
|
|