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

/*!
 * @file sion_api.h
 * @brief SION public API
 *
 * @date Sep 15, 2008
 * @author: gstu0808
 */

#ifndef SION_API_H_
#define SION_API_H_

#ifdef SION_MPI
#include "mpi.h"

#if defined(_SION_LINUX)
#define sion_int32 int
#define sion_int64 long long
#elif defined(_SION_AIX)
#define sion_int32 int
#define sion_int64 long long
#elif defined(_SION_BGL)
#define sion_int32 int
#define sion_int64 long long
#elif defined(_SION_BGP)
#define sion_int32 int
#define sion_int64 long long
#endif

#if defined(_SION_LINUX)
#define SION_MPI_INT32 MPI_INT
#define SION_MPI_INT64 MPI_LONG_LONG
#elif defined(_SION_AIX)
#define SION_MPI_INT32 MPI_INT
#define SION_MPI_INT64 MPI_LONG_LONG
#elif defined(_SION_BGL)
#define SION_MPI_INT32 MPI_INTEGER4
#define SION_MPI_INT64 MPI_INTEGER8
#elif defined(_SION_BGP)
#define SION_MPI_INT32 MPI_INTEGER4
#define SION_MPI_INT64 MPI_INTEGER8
#endif


/* Wrapper function that calls sion_paropen_multi_mpi for 1 file */
  int sion_paropen_mpi(    char    *fname,
                           char*    file_mode,
                           MPI_Comm comm,
                           sion_int64    *chunksize,
                           sion_int32    *fsblksize,
                           int     *globalrank,
                           FILE   **fileptr);

  int sion_paropen_multi_mpi(char *fname,
                           char *file_mode,
                           int  *numFiles,
                           MPI_Comm gComm,
                           MPI_Comm *lComm,
                           sion_int64 *chunksize,
                           sion_int32 *fsblksize,
                           int  *globalrank,
                           FILE **fileptr,
                           char *newfname);

  int sion_parclose_mpi(   int      sid  );

  int sion_paropen_comms_mpi(char *fname,
                           char *file_mode,
                           int *numFiles,
                           MPI_Comm gComm,
                           MPI_Comm lComm,
                           sion_int64 *chunksize,
                           sion_int32 *fsblksize,
                           int  *globalrank,
                           FILE **fileptr,
                           char *newfname);
#endif

 #define SION_CURRENT_RANK        -101  /*!< Alias for the current rank */
 #define SION_CURRENT_BLK         -102  /*!< Alias for the current block */
 #define SION_CURRENT_POS         -103  /*!< Alias for the current position in the current block */

  int sion_open(    char *fname,
                     char* file_mode,
                     int  *ntasks,
                     sion_int64 **chunksizes,
                     sion_int32  *fsblksize,
                     int  **globalranks,
                     FILE **fileptr);

   int sion_open_rank(    char  *fname,
                          char  *file_mode,
                          sion_int64  *chunksize,
                          sion_int32  *fsblksize,
                          int   *rank,
                          FILE **fileptr);

   int sion_feof(int sid);

   int sion_flush(int sid);

   int sion_close(int sid);

   int sion_set_fp_closed(int sid);

   int sion_set_second_fp(int sid, FILE *fp);
   int sion_unset_second_fp(int sid);

   FILE * sion_get_fp(int sid);

   int sion_ensure_free_space(int sid, sion_int64 bytes);

   /* FILE INFO with sion datatypes */
   int sion_get_locations(    int    sid,
                              int   *ntasks,
                              int   *maxblocks,
                              sion_int64 *globalskip,
                              sion_int64 *start_of_varheader,
                              sion_int64 **sion_localsizes,
                              sion_int64 **sion_globalranks,
                              sion_int64 **sion_blockcount,
                              sion_int64 **sion_blocksizes);

   int sion_get_current_location(    int sid,
                                     int *ntasks,
                                     sion_int64 **sion_currentpos,
                                     sion_int64 **sion_currentblocknr );

   int sion_seek(  int  sid,
                   int  rank,
                   int  currentblocknr,
                   sion_int64 posinblk );

   /* get number of bytes not read in current block */
   sion_int64 sion_bytes_avail_in_block( int sid );

#endif /* SION_API_H_ */
