#include #include #include #include #include #include #include int main(int argc, char** argv) { MPI_Init(&argc, &argv); int nof_processes; MPI_Comm_size(MPI_COMM_WORLD, &nof_processes); int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); assert(nof_processes == 2); using namespace hpc::matvec; using namespace hpc::mpi; std::size_t nof_rows = 3; std::size_t nof_cols = 7; if (rank == 0) { GeMatrix A(nof_rows, nof_cols, Order::RowMajor); for (auto [i, j, Aij]: A) { Aij = i * 100 + j; } auto row = A.row(2, 0); auto col = A.col(0, 0); MPI_Datatype row_type = get_type(row); MPI_Datatype col_type = get_type(col); MPI_Send(&row(0), 1, row_type, 1, 0, MPI_COMM_WORLD); MPI_Send(&col(0), 1, col_type, 1, 0, MPI_COMM_WORLD); /* receive it back for verification */ DenseVector vec1(nof_cols), vec2(nof_rows); MPI_Datatype vec1_type = get_type(vec1); MPI_Datatype vec2_type = get_type(vec2); MPI_Status status; MPI_Recv(&vec1(0), 1, vec1_type, 1, 0, MPI_COMM_WORLD, &status); MPI_Recv(&vec2(0), 1, vec2_type, 1, 0, MPI_COMM_WORLD, &status); /* verify it */ for (auto [i, xi]: vec1) { if (vec1(i) != row(i)) { fmt::printf("verification failed for row(%d): %lg vs %lg\n", i, vec1(i), row(i)); } } for (auto [i, xi]: vec2) { if (vec2(i) != col(i)) { fmt::printf("verification failed for col(%d): %lg vs %lg\n", i, vec2(i), col(i)); } } } else { DenseVector vec1(nof_cols), vec2(nof_rows); MPI_Datatype vec1_type = get_type(vec1); MPI_Datatype vec2_type = get_type(vec2); MPI_Status status; MPI_Recv(&vec1(0), 1, vec1_type, 0, 0, MPI_COMM_WORLD, &status); MPI_Recv(&vec2(0), 1, vec2_type, 0, 0, MPI_COMM_WORLD, &status); /* send it back for verification */ MPI_Send(&vec1(0), 1, vec1_type, 0, 0, MPI_COMM_WORLD); MPI_Send(&vec2(0), 1, vec2_type, 0, 0, MPI_COMM_WORLD); } MPI_Finalize(); }