/****************************************************************************
**  SIONLIB     http://www.fz-juelich.de/jsc/sionlib                       **
*****************************************************************************
**  Copyright (c) 2008-2019                                                **
**  Forschungszentrum Juelich, Juelich Supercomputing Centre               **
**                                                                         **
**  See the file COPYRIGHT in the package base directory for details       **
****************************************************************************/

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

#include "sion.h"

#define BUFFER_SIZE (1024 * 1024)

int main(int argc, char** argv)
{
  MPI_Init(&argc, &argv);
  int size, rank;
  MPI_Comm_size(MPI_COMM_WORLD, &size);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);

  char localbuffer[BUFFER_SIZE];
  memset(localbuffer, 'A', BUFFER_SIZE);

  int sid = sion_paropen_mpi("parfile.sion", SION_OPEN_WRITE, MPI_COMM_WORLD, NULL);

  size_t bwrote = sion_write(localbuffer, 1, BUFFER_SIZE, sid);
  printf("Task %02d: wrote bytes: %zu\n", rank, bwrote);
  sion_parclose_mpi(sid);

  sid = sion_paropen_mpi("parfile.sion", SION_OPEN_READ, MPI_COMM_WORLD, NULL);
  size_t bread = sion_read(localbuffer, 1, BUFFER_SIZE, sid);
  printf("Task %02d: read bytes: %zu\n", rank, bread);
  printf("Task %02d: at end of file: %s\n", rank, (sion_eof(sid)) ? "true" : "false");
  sion_parclose_mpi(sid);

  MPI_Finalize();

  return 0;
}
