#include<iostream>
#include<cstdlib> // for rand() and RAND_MAX
#include<mpi.h>

int main(){
  int i,j,nproc,rank,row_rank,col_rank,row_colour,col_colour;
  double big_matrix[4][4],buffer[4*4],a[2][2],*bptr,sum,dtmp[2];
  MPI_Comm col_comm,row_comm;
  
  MPI_Init(NULL,NULL);
  MPI_Comm_size(MPI_COMM_WORLD,&nproc);
  MPI_Comm_rank(MPI_COMM_WORLD,&rank);

  // We need precisely four processes
  
  if ((rank==0)&&(nproc!=4)) MPI_Abort(MPI_COMM_WORLD,1);

  if (rank==0){

    // Initialise random matrix, with elements in the range 0 to 1
    
    for(i=0;i<4;i++)
      for(j=0;j<4;j++)
	big_matrix[i][j]=rand()/(double)RAND_MAX;

    // The example sheet says "scatter". Does it mean distribute
    // following a method of one's choice, or a single call to
    // MPI_Scatter? The Fortran version of this code assumes
    // distribute, so here we use a single MPI_Scatter instead.

    // The distribute method can be done with multiple sends/recs,
    // or multiple calls to MPI_Scatterv (one per row, with some counts zero)
    // without needing a second copy of the data in a slightly different
    // form.
    
    // Here we first need to get data into a buffer in which the
    // ranks' data follows one another
    
    bptr=&buffer[0];  // or bptr=buffer;

    for(i=0;i<2;i++)
      for(j=0;j<2;j++)
	*(bptr++)=big_matrix[i][j];   // data for rank 0

    for(i=0;i<2;i++)
      for(j=2;j<4;j++)
	*(bptr++)=big_matrix[i][j];  // data for rank 1

    for(i=2;i<4;i++)
      for(j=0;j<2;j++)
	*(bptr++)=big_matrix[i][j];  // data for rank 2

    for(i=2;i<4;i++)
      for(j=2;j<4;j++)
	*(bptr++)=big_matrix[i][j];  // data for rank 3
  }

  // Now the scatter, called from all ranks, does everything we need
  
  MPI_Scatter(buffer,4,MPI_DOUBLE,a,4,MPI_DOUBLE,0,MPI_COMM_WORLD);

  // Make communicators for column and row sums
  
  col_colour=rank%2;
  MPI_Comm_split(MPI_COMM_WORLD,col_colour,1,&col_comm);
  MPI_Comm_rank(col_comm,&col_rank);
    
  row_colour=rank/2;
  MPI_Comm_split(MPI_COMM_WORLD,row_colour,1,&row_comm);
  MPI_Comm_rank(row_comm,&row_rank);

  std::cout << "Rank " << rank << " (world) is column communicator "
	    << col_colour << " rank " << col_rank
	    << " and row communicator " << row_colour
	    << " rank " << row_rank << "\n";

  // We have used barriers liberally to ensure that
  // the output arrives in neat blocks
  
  MPI_Barrier(MPI_COMM_WORLD);

  // Column sums
  
  if (rank==0){
    for(i=0;i<4;i++){
      sum=0;
      for(j=0;j<4;j++) sum+=big_matrix[j][i];
      std::cout << "Global column " << i << " has sum " << sum <<"\n";
    }
  }

  MPI_Barrier(MPI_COMM_WORLD);
  
  for(i=0;i<2;i++){
    sum=a[0][i]+a[1][i];
    if (col_rank==0)
      MPI_Reduce(MPI_IN_PLACE,&sum,1,MPI_DOUBLE,MPI_SUM,0,col_comm);
    else
      MPI_Reduce(&sum,NULL,1,MPI_DOUBLE,MPI_SUM,0,col_comm);
      
    if (col_rank==0)
      std::cout << "Column " << i << " column communicator " << col_colour
		<< " has sum " << sum << " (global column "
		<< i+2*col_colour << " )\n";
  }

  MPI_Barrier(MPI_COMM_WORLD);

  
  // Row sums

  if (rank==0){
    for(i=0;i<4;i++){
      sum=0;
      for(j=0;j<4;j++) sum+=big_matrix[i][j];
      std::cout << "Global row " << i << " has sum " << sum <<"\n";
    }
  }
  
  MPI_Barrier(MPI_COMM_WORLD);
  
  for(i=0;i<2;i++){
    sum=a[i][0]+a[i][1];
    if (row_rank==0)
      MPI_Reduce(MPI_IN_PLACE,&sum,1,MPI_DOUBLE,MPI_SUM,0,row_comm);
    else
      MPI_Reduce(&sum,NULL,1,MPI_DOUBLE,MPI_SUM,0,row_comm);
      
    if (row_rank==0)
      std::cout << "Row " << i << " row communicator " << row_colour
		<< " has sum " << sum << " (global row " << i+2*row_colour
		<< " )\n";
  }

  // Do that again, but rather than sum on a rank, and then reduce,
  // reduce first, and then sum the reduced row. Less efficient, as
  // more data are communicated, but it demonstrates MPI_Reduce with
  // a count other than one.

  if (rank==0)
    std::cout << "Repeating, using reduce followed by a sum, rather than sum"
      " followed by reduce\n";
  
  MPI_Barrier(MPI_COMM_WORLD);

  for(i=0;i<2;i++){
    MPI_Reduce(&(a[i][0]),dtmp,2,MPI_DOUBLE,MPI_SUM,0,row_comm);
    if (row_rank==0){
      sum=dtmp[0]+dtmp[1];
      std::cout << "Row " << i << " row communicator " << row_colour
		<< " has sum " << sum << " (global row "
		<< i+2*row_colour << " )\n";
    }
  }
  
  MPI_Finalize();
}
    
