!===================================================
! DO NOT EDIT THIS FILE, it was generated using /glade/home/jedwards/fis/pio_trunk/pio/genf90.pl 
! Any changes you make to this file may be lost
!===================================================
#define __PIO_FILE__ "rearrange.F90"
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
!  Module rearrange
!
!  Provide a generic entry to the data rearrangement
!
!
!  20070919  Initial version  - R. Loy
!
module rearrange

  use pio_kinds
  use pio_types
  use pio_support
  use box_rearrange


#ifdef TIMING
  use perf_mod, only : t_startf, t_stopf, t_barrierf     ! _EXTERNAL
#endif

  implicit none
  private
  save

!> 
!! @private
!<
  public :: rearrange_init, &
            rearrange_create, &
            rearrange_comp2io, &
            rearrange_io2comp, &
            rearrange_free

# 36 "rearrange.F90.in"
  interface rearrange_init
    module procedure rearrange_init_
  end interface


# 41 "rearrange.F90.in"
  interface rearrange_create
    module procedure rearrange_create_box_
  end interface

# 45 "rearrange.F90.in"
  interface rearrange_comp2io
    ! TYPE real,double,int
    module procedure rearrange_comp2io_real
    ! TYPE real,double,int
    module procedure rearrange_comp2io_double
    ! TYPE real,double,int
    module procedure rearrange_comp2io_int
  end interface

# 50 "rearrange.F90.in"
  interface rearrange_io2comp
    ! TYPE real,double,int
    module procedure rearrange_io2comp_real
    ! TYPE real,double,int
    module procedure rearrange_io2comp_double
    ! TYPE real,double,int
    module procedure rearrange_io2comp_int
  end interface

# 55 "rearrange.F90.in"
  interface rearrange_free
    module procedure rearrange_free_
  end interface


# 60 "rearrange.F90.in"
contains

! TYPE real,double,int
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! rearrange_comp2io_real
!

# 68 "rearrange.F90.in"
  subroutine rearrange_comp2io_real(Iosystem,iodesc,compbuf,iobuf)
    implicit none

    type (Iosystem_desc_t) :: Iosystem
    type (io_desc_t)   :: iodesc
    real(r4), intent(in) ::  compbuf(:)
    real(r4), intent(out) :: iobuf(:)

#ifdef TIMING
    call t_barrierf("pio_rearrange_comp2io_real",IoSystem%comp_comm)
    call t_startf("pio_rearrange_comp2io_real")
#endif


    call box_rearrange_comp2io(Iosystem,iodesc,size(compbuf), compbuf,size(iobuf), iobuf)

#ifdef TIMING
    call t_stopf("pio_rearrange_comp2io_real")
#endif

  end subroutine rearrange_comp2io_real

! TYPE real,double,int
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! rearrange_comp2io_double
!

# 68 "rearrange.F90.in"
  subroutine rearrange_comp2io_double(Iosystem,iodesc,compbuf,iobuf)
    implicit none

    type (Iosystem_desc_t) :: Iosystem
    type (io_desc_t)   :: iodesc
    real(r8), intent(in) ::  compbuf(:)
    real(r8), intent(out) :: iobuf(:)

#ifdef TIMING
    call t_barrierf("pio_rearrange_comp2io_double",IoSystem%comp_comm)
    call t_startf("pio_rearrange_comp2io_double")
#endif


    call box_rearrange_comp2io(Iosystem,iodesc,size(compbuf), compbuf,size(iobuf), iobuf)

#ifdef TIMING
    call t_stopf("pio_rearrange_comp2io_double")
#endif

  end subroutine rearrange_comp2io_double

! TYPE real,double,int
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! rearrange_comp2io_int
!

# 68 "rearrange.F90.in"
  subroutine rearrange_comp2io_int(Iosystem,iodesc,compbuf,iobuf)
    implicit none

    type (Iosystem_desc_t) :: Iosystem
    type (io_desc_t)   :: iodesc
    integer(i4), intent(in) ::  compbuf(:)
    integer(i4), intent(out) :: iobuf(:)

#ifdef TIMING
    call t_barrierf("pio_rearrange_comp2io_int",IoSystem%comp_comm)
    call t_startf("pio_rearrange_comp2io_int")
#endif


    call box_rearrange_comp2io(Iosystem,iodesc,size(compbuf), compbuf,size(iobuf), iobuf)

#ifdef TIMING
    call t_stopf("pio_rearrange_comp2io_int")
#endif

  end subroutine rearrange_comp2io_int




! TYPE real,double,int
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! rearrange_io2comp_real
!
# 98 "rearrange.F90.in"
  subroutine rearrange_io2comp_real (Iosystem,iodesc,iobuf,compbuf)
    implicit none

    type (Iosystem_desc_t) :: Iosystem
    type(io_desc_t) :: iodesc
    real(r4) :: iobuf(:)
    real(r4) ::  compbuf(:)

#ifdef TIMING
    call t_startf("pio_rearrange_io2comp_real")
#endif

    call box_rearrange_io2comp(Iosystem,iodesc,size(iobuf),iobuf,size(compbuf),compbuf)

#ifdef TIMING
    call t_stopf("pio_rearrange_io2comp_real")
#endif

  end subroutine rearrange_io2comp_real




! TYPE real,double,int
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! rearrange_io2comp_double
!
# 98 "rearrange.F90.in"
  subroutine rearrange_io2comp_double (Iosystem,iodesc,iobuf,compbuf)
    implicit none

    type (Iosystem_desc_t) :: Iosystem
    type(io_desc_t) :: iodesc
    real(r8) :: iobuf(:)
    real(r8) ::  compbuf(:)

#ifdef TIMING
    call t_startf("pio_rearrange_io2comp_double")
#endif

    call box_rearrange_io2comp(Iosystem,iodesc,size(iobuf),iobuf,size(compbuf),compbuf)

#ifdef TIMING
    call t_stopf("pio_rearrange_io2comp_double")
#endif

  end subroutine rearrange_io2comp_double




! TYPE real,double,int
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! rearrange_io2comp_int
!
# 98 "rearrange.F90.in"
  subroutine rearrange_io2comp_int (Iosystem,iodesc,iobuf,compbuf)
    implicit none

    type (Iosystem_desc_t) :: Iosystem
    type(io_desc_t) :: iodesc
    integer(i4) :: iobuf(:)
    integer(i4) ::  compbuf(:)

#ifdef TIMING
    call t_startf("pio_rearrange_io2comp_int")
#endif

    call box_rearrange_io2comp(Iosystem,iodesc,size(iobuf),iobuf,size(compbuf),compbuf)

#ifdef TIMING
    call t_stopf("pio_rearrange_io2comp_int")
#endif

  end subroutine rearrange_io2comp_int


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! rearrange_init
! 
! called from initFile_
!


# 127 "rearrange.F90.in"
  subroutine rearrange_init_(Iosystem)
    implicit none

    type (Iosystem_desc_t), intent(inout) :: Iosystem



        ! no general init required for box rearranger

  end subroutine rearrange_init_

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
!  rearrange_create_box_
!
!  called from initDecomp
!


# 146 "rearrange.F90.in"
  subroutine rearrange_create_box_(Iosystem,compDOF, &
                               dims,ndims,ioDesc)
    implicit none

    type (Iosystem_desc_t), intent(in) :: Iosystem
    integer (kind=pio_offset), intent(in) :: compDOF(:)
    integer, intent(in) :: dims(:)
    integer, intent(in) :: ndims
    type (IO_desc_t) :: ioDesc
    
#ifdef TIMING
     call t_startf("pio_rearrange_create_box")
#endif

    if (Iosystem%rearr /= PIO_rearr_box) then
      call piodie( __PIO_FILE__,__LINE__, &
           'rearrange_create called with args for box but rearranger type is not box, Iosystem%rearr=',&
           Iosystem%rearr)
    endif


    call box_rearrange_create( Iosystem,compDOF,dims,ndims,Iosystem%num_iotasks,ioDesc)


#ifdef TIMING
     call t_stopf("pio_rearrange_create_box")
#endif

  end subroutine rearrange_create_box_




!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! rearrange_free_
!
! called from freeDecomp


# 186 "rearrange.F90.in"
  subroutine rearrange_free_(Iosystem,ioDesc)
    implicit none

    type (Iosystem_desc_t), intent(in) :: Iosystem
    type (IO_desc_t)                   :: ioDesc


    select case (Iosystem%rearr)
    case (PIO_rearr_box)
       call box_rearrange_free(Iosystem,ioDesc)
    case (PIO_rearr_none)
        ! do nothing 

    case default
       call piodie(__PIO_FILE__,__LINE__,'Unrecognized rearranger:',Iosystem%rearr)

    end select


  end subroutine rearrange_free_


end module rearrange
