consolidate all repos to one for archive
This commit is contained in:
127
semester_5/paralelno_racunanje/naloga_2/main.cpp
Normal file
127
semester_5/paralelno_racunanje/naloga_2/main.cpp
Normal file
@@ -0,0 +1,127 @@
|
||||
#include <mpi.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
#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;
|
||||
}
|
25
semester_5/paralelno_racunanje/naloga_2/makefile
Normal file
25
semester_5/paralelno_racunanje/naloga_2/makefile
Normal file
@@ -0,0 +1,25 @@
|
||||
CC=mpic++
|
||||
|
||||
INCLUDE_DIR= include
|
||||
|
||||
SRC_DIR= src
|
||||
|
||||
# Get all cpp files from src directory
|
||||
SRCS := $(wildcard $(SRC_DIR)/*.cpp)
|
||||
|
||||
all: main run
|
||||
|
||||
main: main.cpp $(SRCS)
|
||||
$(CC) -o3 -I$(INCLUDE_DIR) $(SRCS) main.cpp -o main
|
||||
|
||||
debug: main.cpp $(SRCS)
|
||||
$(CC) -I$(INCLUDE_DIR) $(SRCS) -g main.cpp -o main
|
||||
|
||||
run: main
|
||||
mpirun -n 4 ./main
|
||||
|
||||
zip:
|
||||
zip -r main.zip main.cpp makefile shakespeare.txt
|
||||
|
||||
clean:
|
||||
rm main
|
124456
semester_5/paralelno_racunanje/naloga_2/shakespeare.txt
Normal file
124456
semester_5/paralelno_racunanje/naloga_2/shakespeare.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user