!= MPI 計算での Message 制御
!
!= Message control for MPI calculation
!
! Authors::   Shin-ichi Takehiro
! Version::   $Id: mpi_messagecntl.f90,v 1.0 2025/09/17 22:00:00 takepiro Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2020. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module mpi_messagecntl
  !
  != MPI 計算での Message 制御
  !
  != Message control for MPI calculation
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! MPI 計算での Message 制御のためのモジュール
  !
  ! This is a module for controlling message outputs in MPI calculation
  !
  !== Procedures List
  !
  ! RadiationFinalize       :: 終了処理 (モジュール内部の変数の割り付け解除)
  ! ------------            :: ------------
  ! RadiationFinalize       :: Termination (deallocate variables in this module)
  !
  !== NAMELIST
  !
  ! NAMELIST#mpi_messagecntl_nml
  !

  ! モジュール引用 ; USE statements
  !
  use mpi_wrapper, only: myrank
  
  ! 宣言文 ; Declaration statements
  !
  implicit none
  private

  ! 公開手続き
  ! Public procedure
  !
  public :: DoesOutputMPIMessage
  public :: MPIMessageCntlInit

  !
  ! メッセージ出力する MPI プロセス番号. 負ならすべて.
  ! MPI process number which outputs messages.
  ! All processes output messages when negative.
  ! 
  integer, save :: Output_Rank_save = -1
  
  character(*), parameter:: module_name = 'mpi_messagecntl'
                              ! モジュールの名称.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: mpi_messagecntl.f90,v 1.00 2025/09/17 22:00:00 takepiro Exp $'
                              ! モジュールのバージョン
                              ! Module version

contains

  !
  ! 初期化
  !
  subroutine MPIMEssageCntlInit

    ! ファイル入出力補助
    ! File I/O support
    !
    use dc_iounit, only: FileOpen

    ! 種別型パラメタ
    ! Kind type parameter
    !
    use dc_types, only: STDOUT    ! 標準出力の装置番号. Unit number of standard output

    ! メッセージ抑制設定
    ! Message suppression configration
    !
    use dc_message, only:  MessageSuppressMPI

     ! NAMELIST ファイル入力に関するユーティリティ
    ! Utilities for NAMELIST file input
    !
    use namelist_util, only: namelist_filename, NmlutilMsg

    ! メッセージ出力する MPI プロセス番号. 負ならすべて. 
    ! MPI process number which outputs messages.
    ! All processes output messages when negative.
    !
    integer :: Output_Rank = -1

    ! NAMELIST 変数群
    ! NAMELIST group name
    !
    namelist /mpi_messagecntl_nml/ &
         & Output_Rank

    integer:: unit_nml        ! NAMELIST ファイルオープン用装置番号.
    ! Unit number for NAMELIST file open
    integer:: iostat_nml      ! NAMELIST 読み込み時の IOSTAT.
    ! IOSTAT of NAMELIST read

    ! NAMELIST の読み込み
    ! NAMELIST is input
    !
    if ( trim(namelist_filename) /= '' ) then
       call FileOpen( unit_nml, &          ! (out)
            & namelist_filename, mode = 'r' ) ! (in)

       rewind( unit_nml )
       read( unit_nml, &                ! (in)
            & nml = mpi_messagecntl_nml, & ! (out)
            & iostat = iostat_nml )        ! (out)
       close( unit_nml )

       Output_Rank_save = Output_Rank

       ! Message suppression configration      
       call MessageSuppressMPI( Output_Rank_save )

       call NmlutilMsg( iostat_nml, module_name ) ! (in)
       if ( iostat_nml == 0 .AND. DoesOutputMPIMessage() ) &
            write( STDOUT, nml = mpi_messagecntl_nml )
    end if

  end subroutine MPIMessageCntlInit

  function DoesOutputMPIMessage() result(Flag)

    logical :: Flag
    
    if ( Output_Rank_save < 0 ) then
       Flag = .true.
    else if ( Output_Rank_save == myrank ) then
       Flag = .true.
    else
       Flag = .false.
    end if

  end function DoesOutputMPIMessage

end module mpi_messagecntl
