100 lines
2.0 KiB
C++

#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;
}