1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
#include <cassert>
#include <cstdlib>
#include <mpi.h>
#include <printf.hpp>
#include <hpc/matvec/gematrix.hpp>
#include <hpc/matvec/iterators.hpp>
#include <hpc/mpi/vector.hpp>

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<double> 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<double> 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<double> 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();
}