/*
    DFT++ is a density functional package developed by the research group
    of Professor Tomas Arias

    Copyright 1996-2003 Sohrab Ismail-Beigi

    This file is part of DFT++.

    DFT++ is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DFT++ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DFT++; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Please see the file CREDITS for a list of authors.

    For academic users, we request that publications using results obtained with
    this software reference

    "New algebraic formulation of density functional calculation," by Sohrab Ismail-Beigi
    and T.A. Arias, Computer Physics Communications 128:1-2, 1-45 (June 2000).

    and, if using the wavelet basis, further reference

    "Multiresolution analysis of electronic structure: semicardinal and wavelet bases,"
    T.A. Arias, Reviews of Modern Physics 71:1, 267-311 (January 1999).

    and 

    "Robust ab initio calculation of condensed matter: transparent convergence through
    semicardinal multiresolution analysis,'' I.P. Daykov, T.A. Arias, and
    Torkel D. Engeness, Physical Review Letters, 90:21, 216402 (May 2003).

    For your convenience, preprints of the above articles may be obtained from
    http://arXiv.org/abs/cond-mat/9909130, 9805262, and 0204411, respectively.
*/

/* 
 * Gabor Csanyi, June 1999
 * 
 * generalized I/O functions to handle serial and MPI binary I/O in a
 * unified way
 * 
 * dft_fopen()
 * dft_fclose()
 * dft_fread() BUG: assumes data is type double
 * dft_fwrite()
 *
 */

#include "header.h"
#include "parallel.h"


/* 
 * dft_fopen() opens a file and returns the file pointer. arguments
 * are as in standard fopen(). If called in a serial context, it
 * simply executes a system fopen(). If called in an MPI context, it
 * executes a fopen() on the I/O node, and returns a NULL on all the
 * other nodes.
 *
 * ---> dft_open does its own error checking, so please do not check
 *      for NULL returns from dft_open as an error (in fact, on MPI
 *      nodes, it returns NULL so this would create trouble!)
 */
FILE *
dft_fopen(char *filename, char *access_string)
{

  FILE *stream;
  char s[DFT_MSG_LEN];

  // Some systems like a "b" at the end of the access string
  // otherwise they will not process the file as binary but
  // as text which causes trouble.  So we'll append b, unless
  // it is already there.
  strcpy(s,access_string);
  if (s[strlen(s)-1] != 'b')
    strcat(s,"b");

#ifdef DFT_MPI
  // we are running on an MPI system
  int ready_flag = 0;
  int my_id = System::Get_procID();
  int io_node = System::Get_IOprocID();

  if (my_id == io_node){
    stream = fopen(filename,s);

    // check if fopen was successful
    if (stream == NULL){
      // it wasnt... tell all processes to quit.
      MPI_Bcast ( &ready_flag, 1, MPI_INT, io_node, MPI_COMM_WORLD );
      die("Can't open %s with mode %s in dft_fopen()\n", filename, s);
    }
    ready_flag = 1; // tell all processes that we are fine
    MPI_Bcast ( &ready_flag, 1, MPI_INT, io_node, MPI_COMM_WORLD );
    return(stream);
  }else{
    // Get signal from the I/O node
    MPI_Bcast ( &ready_flag, 1, MPI_INT, io_node, MPI_COMM_WORLD );

    // Error. Abort.
    if (ready_flag != 1)
      die("Proc %d aborts on dft_fopen of %s with mode %s.\n", 
	  my_id, filename, s);

    return(NULL);
  }



#else //DFT_MPI

  // we are running in a serial context
  stream = fopen(filename,s);
  if (stream == NULL){
    die("Can't open %s in dft_fopen()\n",filename);
  }

  return(stream);



#endif //DFT_MPI

}


/* 
 * dft_fclose() closes the stream pointed to by its argument. if the
 * argument is NULL, it does nothing.
 *
 */
void
dft_fclose(FILE *stream)
{

#ifdef DFT_MPI
  // we are in an MPI context

  int my_id = System::Get_procID();
  int io_node = System::Get_IOprocID();

  if (my_id == io_node){
    fclose(stream);
  }

#else // DFT_MPI

  // we are in a serial context, so just close the stream
  fclose(stream);

#endif //DFT_MPI
}


/*
 * dft_fwrite() called in a scalar context just does an fwrite(). in an
 * MPI context, it does an fwrite if executed in the I/O node, and nothing
 * on any other node. returns the number of bytes written.
 */
size_t 
dft_fwrite(const void *data, size_t size, size_t nmemb, FILE *stream)
{
#ifdef DFT_MPI
  int my_id = System::Get_procID();
  int io_node = System::Get_IOprocID();

  if (my_id == io_node){
    return(fwrite(data, size, nmemb, stream));
  }else{
    return(0);
  }
#else // DFT_MPI
  // we are in a serial context

  return(fwrite(data, size, nmemb, stream));

#endif //DFT_MPI
}


/*
 * dft_fread() called in a scalar context just does an fread(). in an
 * MPI context, it does an fread if executed in the I/O node, and nothing
 * on any other node. then it broadcases the results, and returns the return vallue
 * of the system fread.
 */

size_t
dft_fread(void *data, size_t size, size_t nmemb, FILE *stream)
{
#ifdef DFT_MPI
  // we are in an MPI context

  // the problem is that dft_fread() is not aware of the type of the data
  // but the MPI broadcast needs that information. so at a minimum we assume
  // that its a double, and we trap if its not.
  if(size != sizeof(double)*SCALAR_SIZE)
    die("dft_fread() can only handle doubles at this point\n");
  
  int my_id = System::Get_procID();
  int io_node = System::Get_IOprocID();
  size_t items;

  if (my_id == io_node)
    items = fread(data, size, nmemb, stream);
  else
    items=0;

  // this assumes that the data is type double. TO BE FIXED
  MPI_Bcast ( data, nmemb*SCALAR_SIZE, MPI_DOUBLE, io_node, MPI_COMM_WORLD );

  return(items);
#else  // DFT_MPI
  // we are in a serial context
  return(fread(data, size, nmemb, stream));

#endif // DFT_MPI
}


// Flush the file if we're the IO node
int
dft_fflush(FILE *stream)
{
#ifdef DFT_MPI
  // we are in an MPI context
  if (System::Get_procID() == System::Get_IOprocID())
    return fflush(stream);
  else
    return 0;
#else // DFT_MPI
  // we are in a serial context, so just flush the stream
  return fflush(stream);
#endif //DFT_MPI
}


/* Does vfprintf */
int
dft_vfprintf(FILE *stream, char *format, va_list ap)
{
#ifdef DFT_MPI
  // we are in an MPI context
  if (System::Get_procID() == System::Get_IOprocID())
    return vfprintf(stream,format,ap);
  else
    return 0;
#else // DFT_MPI
  // we are in a serial context, so just flush the stream
  return vfprintf(stream,format,ap);
#endif //DFT_MPI
}


syntax highlighted by Code2HTML, v. 0.9.1