subroutine trmpi_end(ierr )
  ! finalize MPI ..
  call trmpi_end2( .false., ierr )
  end subroutine trmpi_end

subroutine trmpi_abort(ierr )
  ! abort MPI ..
  call trmpi_end2( .true., ierr )
  end subroutine trmpi_abort

    subroutine trmpi_end2( labort, ierr )
  !.. trmpi_end                        Terminate MPI run
  !                                    codesys/source/trmpi/trmpi_end2
  !
  ! if labort
  !    call MPI_ABORT for any idd
  !
  ! else Terminate MPI  -
  !    myid = 0    sends EXIT to listeners
  !           all  call MPI_FINALISE
  !                and  >> return to caller <<
  !--------------------------------------------------------------------------------
  !
  !  Author                            jim.conboy@jet.uk
  !  Version                           1.00,  12Jun2009
  !  Modifications
  !
  !  1.00  12Jun2009                   jim.conboy@jet.uk
  !                                    New at release Fri May  8 04:13:43 EDT 2009
  !----_^---------------------------------=========================================|
  !
  use trmpi
  implicit  none
  !
  !..Arguments
  !
      logical, intent(in)            :: labort  ! .t to abort
      integer, intent(inout)         :: ierr    ! just for the record
  !
  !..local
  !
      integer                        :: ilu     ! IO unit
      character*3                    :: ciw     ! msg flag
      character(len=*),parameter     :: cSr='trmpi_end2'
  !----------------------------------::================!.........................|
  !

  ierr = 0
  ilu  = trmpi_lunmsg

  !-print *, 'trmpi_end2 - ',trmpi_active, trmpi_numprocs, trmpi_myid

#ifndef __MPI
  write(ilu,*) 'trmpi_end2 -I- __MPI undefined '
  return
#else

  if(            trmpi_numprocs == 1      &
      .or. .not. trmpi_active             ) return  ! Not required

  !--------------------


  if( labort )                                    then
    write(ilu,*) 'trmpi_end2 -F- ',trmpi_myid, &
          ' calling  MPI_ABORT for ',trmpi_codeID
    call MPI_ABORT( trmpi_comm, ierr, ierr )      !  NOT TESTED 10Jun2009
    call errmsg_exit('trmpi/trmpi_end2:abort')
  endif

  if( trmpi_myid == 0 )  call trmpi_requestor( 'EXIT', ' ' )

  call MPI_BARRIER(trmpi_comm,ierr)
  call MPI_FINALIZE(ierr)

  trmpi_active = .FALSE.

  ciw  = '-I-' ;   if( ierr /= 0 )  ciw = '-W-'
  write(ilu,*) 'trmpi_end2 ',ciw,trmpi_myid,'Ended MPI for ',trmpi_codeID

  if ( ierr /= 0 )  &
    write(ilu,'(22x,a,i4)') ' Error ',ierr

#endif

end subroutine trmpi_end2
