#include #include #include #include #define RECICIVING 0 void functionA(std::ifstream &file) { std::ofstream out("out.txt"); int r1, r2, r3; std::string line = ""; while (std::getline(file, line)) { MPI_Recv(&r1, 1, MPI_INT, 1, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE); MPI_Recv(&r2, 1, MPI_INT, 2, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE); MPI_Recv(&r3, 1, MPI_INT, 3, 3, MPI_COMM_WORLD, MPI_STATUS_IGNORE); out << r1 + r2 + r3 << std::endl; } out.close(); } // Funkcija f1 izračuna dolžino najdaljše besede v nizu bj. void functionB(std::ifstream &file) { std::string line = ""; int max = 0; int current = 0; while (std::getline(file, line)) { for (size_t i = 0; i < line.size(); i++) { if (line[i] == ' ') { if (current > max) max = current; current = 0; } else current++; } current = 0; MPI_Send(&max, 1, MPI_INT, RECICIVING, 1, MPI_COMM_WORLD); max = 0; } } // Funkcija f2 vrne število samoglasnikov v nizu bj. void functionC(std::ifstream &file) { std::string line = ""; int count = 0; while (std::getline(file, line)) { for (size_t i = 0; i < line.size(); i++) { if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u') count++; else if (line[i] == 'A' || line[i] == 'E' || line[i] == 'I' || line[i] == 'O' || line[i] == 'U') count++; } MPI_Send(&count, 1, MPI_INT, RECICIVING, 2, MPI_COMM_WORLD); count = 0; } } // Funkcija f3 vrne število presledkov v nizu bj. void functionD(std::ifstream &file) { std::string line = ""; int count = 0; while (std::getline(file, line)) { for (size_t i = 0; i < line.size(); i++) { if (line[i] == ' ') count++; } MPI_Send(&count, 1, MPI_INT, RECICIVING, 3, MPI_COMM_WORLD); count = 0; } } int main(int argc, char *argv[]) { MPI_Init(&argc, &argv); int rank, size; MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); if (size < 4) { std::cout << "Program can only run with 4 processes" << std::endl; MPI_Finalize(); return 0; } std::ifstream file("shakespeare.txt"); switch (rank) { case 0: functionA(file); break; case 1: functionB(file); break; case 2: functionC(file); break; case 3: functionD(file); break; default: break; } MPI_Finalize(); return 0; }