//
// MPI input for MITgcm
// domain-specific  hack version
//

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <mpi.h>


///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// Magic values.  Currently, these must be filled in by hand

// Number of elements in a single tile
#define tileSizeX   30
#define tileSizeY   40

// Number of tiles in the whole (rectangular) domain
#define numTilesInX   32
#define numTilesInY   32

// Number of ghost cells on the target buffer that is receiving the read.
// If no ghosts, define these as zero
#define xGhosts  0
#define yGhosts  0

///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////


// Base type of a data element.  We currently require that the
// elemental type used for the in-memory buffer be the same as
// the elemental type used in the disk file.

// Produce both float and double versions

#define dataElementType  float
#define mpi_dataElementType  MPI_FLOAT

#include "./readtile_mpiio.h"

#undef dataElementType
#undef mpi_dataElementType
#define dataElementType  double
#define mpi_dataElementType  MPI_DOUBLE

#include "./readtile_mpiio.h"


// In the current version, these do nothing
void initSizesAndTypes(void) { return; }
void initsizesandtypes_(void) {initSizesAndTypes();}





#if 0
/////////////////////////////////////////////////////////////
// Test case

void
dumpOneTileBuf (dataElementType *p,  int sizeZ)
{
  int ii, jj, kk, xg = xGhosts, yg = yGhosts;


  fprintf (stdout, "////////////////////////////////////  \n");
  {
    dataElementType (*tileBuf)[tileSizeY+2*yg][tileSizeX+2*xg] =
               (dataElementType(*)[tileSizeY+2*yg][tileSizeX+2*xg]) p;

    // This dumps the full tile including ghosts
    for (kk = 0;  kk < sizeZ;  ++kk) {
      fprintf (stdout, "\n");
      for (jj = tileSizeY+2*yg - 1;  jj >= 0;  --jj) {
        for (ii = 0;  ii < tileSizeX+2*xg;  ++ii) {
          fprintf (stdout, " %9.1f", (double)tileBuf[kk][jj][ii]);
        }
        fprintf (stdout, "\n");
      }
      fprintf (stdout, "\n");
    }
  }
  fprintf (stdout, "////////////////////////////////////  \n");
  fflush (stdout);
}


void
dumpAllTileBufs (MPI_Comm appComm, dataElementType *tileBuf,  int sizeZ)
{
  int curRank, myRank, commSize;
  MPI_Comm_rank (appComm, &myRank);
  MPI_Comm_size (appComm, &commSize);

  for (curRank = 0;  curRank < commSize;  ++curRank) {
    // Dump buf one at a time for readablity
    if (curRank == myRank) {
      dumpOneTileBuf (tileBuf, sizeZ);
    }
    MPI_Barrier (appComm);
  }
}


#define FILENAME  "./dataFile"
long int fieldOffsetInFile = 0;

void
doIO(MPI_Comm appComm)
{
    // For testing, we want to use small numbers
    assert ((tileSizeX < 10) && (tileSizeY < 10));
    assert ((numTilesInX < 10) && (numTilesInY < 10));

    int tileID;
    int ii, sizeZ, maxSizeZ;

    // The input file is created by an auxillary program
    // Sizes in the file
    long zLevelSizeInElements = numTilesInX*numTilesInY*tileSizeX*tileSizeY;
    long zLevelSizeInBytes = zLevelSizeInElements * sizeof(dataElementType);

    int  appCommSize, appCommRank;
    MPI_Comm_size(appComm, &appCommSize);
    MPI_Comm_rank(appComm, &appCommRank);

    // If there are "dry" tiles, this mapping will be more complex
    tileID = appCommRank + 1;  // 1-based tile ID's

    // Sizes of the in-core tileBuf.
    maxSizeZ = 10;
    size_t bufElements = (tileSizeX + 2*xGhosts) * (tileSizeY + 2*yGhosts) * maxSizeZ;
    dataElementType *tileBuf = malloc (bufElements*sizeof(dataElementType));


    MPI_Barrier(appComm);
    sizeZ = 3;
    for (ii = 0;  ii < bufElements;  ++ii) tileBuf[ii] = -1.0;
    if (0 == appCommRank) system("/bin/echo -n 'begin read: ' ; date ");
    readField (appComm, FILENAME, zLevelSizeInBytes * 1,
              tileBuf, tileID, sizeZ);
    if (0 == appCommRank) system("/bin/echo -n 'end read: ' ; date ");
    dumpAllTileBufs (appComm, tileBuf, sizeZ);


    MPI_Barrier(appComm);
    sizeZ = 3;  // same size as previous will reuse the types
    for (ii = 0;  ii < bufElements;  ++ii) tileBuf[ii] = -1.0;
    if (0 == appCommRank) system("/bin/echo -n 'begin read: ' ; date ");
    readField (appComm, FILENAME, zLevelSizeInBytes * 0,
              tileBuf, tileID, sizeZ);
    if (0 == appCommRank) system("/bin/echo -n 'end read: ' ; date ");
    dumpAllTileBufs (appComm, tileBuf, sizeZ);


    MPI_Barrier(appComm);
    sizeZ = 1;
    for (ii = 0;  ii < bufElements;  ++ii) tileBuf[ii] = -1.0;
    if (0 == appCommRank) system("/bin/echo -n 'begin read: ' ; date ");
    readField (appComm, FILENAME, zLevelSizeInBytes * 4,
              tileBuf, tileID, sizeZ);
    if (0 == appCommRank) system("/bin/echo -n 'end read: ' ; date ");
    dumpAllTileBufs (appComm, tileBuf, sizeZ);

}



int
main(int argc, char *argv[])
{
    MPI_Comm  appComm = MPI_COMM_NULL;

    MPI_Init(&argc, &argv);
    MPI_Comm_dup(MPI_COMM_WORLD, &appComm);

    int commSize;
    MPI_Comm_size (appComm, &commSize);
    assert (numTilesInX * numTilesInY == commSize);

    initSizesAndTypes();
    doIO(appComm);

    MPI_Finalize();
    return 0;
}
#endif

