SharedFilePtr.h 1.57 KB
Newer Older
Francois Gygi committed
1 2
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
3 4 5 6
// Copyright (c) 2008 The Regents of the University of California
//
// This file is part of Qbox
//
Francois Gygi committed
7 8
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
Francois Gygi committed
9 10 11 12 13 14
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
15 16 17 18 19 20 21
// SharedFilePtr.h
//
////////////////////////////////////////////////////////////////////////////////

#ifndef SHAREDFILEPTR_H
#define SHAREDFILEPTR_H

22 23 24
#include<fstream>

#if USE_MPI
Francois Gygi committed
25
#include "mpi.h"
26 27 28 29 30
#else
  typedef int MPI_Comm;
  typedef std::ofstream MPI_File;
  typedef long long int  MPI_Offset;
#endif
Francois Gygi committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

class SharedFilePtr
{
  private:

  MPI_Comm comm_;
  MPI_File& fh_;
  long long int offset_;

  public:

  MPI_File& file(void) { return fh_; }
  long long int offset(void) const { return offset_; }
  MPI_Offset mpi_offset(void) const { return (MPI_Offset) offset_; }
  void sync(void)
  {
    // set all offsets to the largest offset
48
#if USE_MPI
Francois Gygi committed
49 50
    long long int s_off = offset_;
    MPI_Allreduce(&s_off,&offset_,1,MPI_LONG_LONG,MPI_MAX,comm_);
51
#endif
Francois Gygi committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  }
  void set_offset(long long int off)
  {
    offset_ = off;
  }
  void advance(long long int dist)
  {
    offset_ += dist;
  }

  SharedFilePtr(MPI_Comm comm, MPI_File& fh) : comm_(comm),
                fh_(fh), offset_(0) {}
  ~SharedFilePtr() {}
};
#endif