#include <stdio.h>
#include "mpi.h"

/* This example handles a 12 x 12 mesh, on 4 processors only. */
#define maxn 12

int main( argc, argv )
int argc;
char **argv;
{
    int        rank, value, size, i, j;
    double     x,y;
    MPI_Status status;
    double     xlocal[(12/4)+2][12];
    double     lexact[(12/4)+2][12];
    double     gexact[maxn][maxn];

    MPI_Init( &argc, &argv );

    MPI_Comm_rank( MPI_COMM_WORLD, &rank );
    MPI_Comm_size( MPI_COMM_WORLD, &size );

    if (size != 4) MPI_Abort( MPI_COMM_WORLD, 1 );


    /* Initialize the exact solution */

    for (i=1; i<=maxn/size; i++) 
    {
      y = (double)(rank*(maxn/size)) + (double)(i - 1);
      for (j=0; j<maxn; j++) 
      {
        x = (double)j;
        lexact[i][j] = x*x - y*y;
      }
    }
   
    MPI_Gather( lexact[1], maxn * (maxn/size), MPI_DOUBLE,
                gexact, maxn * (maxn/size), MPI_DOUBLE, 
                0, MPI_COMM_WORLD );

    if(rank == 0)
    {
      for (i = 0; i < maxn; i++)
      {
        for (j=0; j<maxn; j++)   
        {
          printf("%6.1f", gexact[i][j]);
        }
        printf("\n");
      }
    }
    MPI_Finalize( );
    return 0;
}
