consolidate all repos to one for archive
This commit is contained in:
99
semester_5/paralelno_racunanje/naloga_3/main.cpp
Normal file
99
semester_5/paralelno_racunanje/naloga_3/main.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
#include <mpi.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
|
||||
#define SIZE 4
|
||||
#define RECICIVING 0
|
||||
#define TIME 15
|
||||
void sum_rows()
|
||||
{
|
||||
std::vector<std::vector<int>> matrix;
|
||||
for (size_t i = 0; i < SIZE; i++)
|
||||
{
|
||||
std::vector<int> row;
|
||||
for (size_t j = 0; j < SIZE; j++)
|
||||
{
|
||||
row.push_back(i + j);
|
||||
}
|
||||
matrix.push_back(row);
|
||||
}
|
||||
|
||||
int result = 1;
|
||||
for (int i = 0; i < matrix.size(); i++)
|
||||
{
|
||||
int sum = 0;
|
||||
for (int j = 0; j < matrix.size(); j++)
|
||||
{
|
||||
sum += matrix[i][j];
|
||||
}
|
||||
result *= sum;
|
||||
}
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
do
|
||||
{
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
} while (std::chrono::duration_cast<std::chrono::seconds>(end - start).count() < TIME);
|
||||
int r1 = 0;
|
||||
MPI_Recv(&r1, 1, MPI_INT, 1, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
||||
printf("Rezultat: %d\n", (result + r1));
|
||||
}
|
||||
|
||||
void sum_columns()
|
||||
{
|
||||
std::vector<std::vector<int>> matrix;
|
||||
for (size_t i = 0; i < SIZE; i++)
|
||||
{
|
||||
std::vector<int> row;
|
||||
for (size_t j = 0; j < SIZE; j++)
|
||||
{
|
||||
row.push_back(i + j);
|
||||
}
|
||||
matrix.push_back(row);
|
||||
}
|
||||
|
||||
int result = 1;
|
||||
for (int j = 0; j < matrix.size(); j++)
|
||||
{
|
||||
int sum = 0;
|
||||
for (int i = 0; i < matrix.size(); i++)
|
||||
{
|
||||
sum += matrix[i][j];
|
||||
}
|
||||
result *= sum;
|
||||
}
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
do
|
||||
{
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
} while (std::chrono::duration_cast<std::chrono::seconds>(end - start).count() < TIME);
|
||||
MPI_Send(&result, 1, MPI_INT, RECICIVING, 1, MPI_COMM_WORLD);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
switch (rank)
|
||||
{
|
||||
case 0:
|
||||
sum_rows();
|
||||
break;
|
||||
case 1:
|
||||
sum_columns();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
MPI_Finalize();
|
||||
return 0;
|
||||
}
|
25
semester_5/paralelno_racunanje/naloga_3/makefile
Normal file
25
semester_5/paralelno_racunanje/naloga_3/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
|
||||
|
||||
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 2 ./main
|
||||
|
||||
zip:
|
||||
zip -r main.zip main.cpp makefile *.jpg
|
||||
|
||||
clean:
|
||||
rm main
|
Reference in New Issue
Block a user