separating functionality

signal handling and CLI handling are not really related
This commit is contained in:
Martin Diehl 2022-04-23 14:41:10 +02:00
parent 5d12ef5b9f
commit 0e65d44bdc
4 changed files with 138 additions and 110 deletions

View File

@ -5,6 +5,7 @@
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
module CPFEM2 module CPFEM2
use parallelization use parallelization
use signals
use DAMASK_interface use DAMASK_interface
use prec use prec
use IO use IO
@ -21,7 +22,6 @@ module CPFEM2
use material use material
use phase use phase
use homogenization use homogenization
use discretization use discretization
#if defined(MESH) #if defined(MESH)
use FEM_quadrature use FEM_quadrature
@ -44,6 +44,7 @@ subroutine CPFEM_initAll
call parallelization_init call parallelization_init
call DAMASK_interface_init ! Spectral and FEM interface to commandline call DAMASK_interface_init ! Spectral and FEM interface to commandline
call signals_init
call prec_init call prec_init
call IO_init call IO_init
#if defined(MESH) #if defined(MESH)

View File

@ -24,10 +24,6 @@ module DAMASK_interface
implicit none implicit none
private private
logical, volatile, public, protected :: &
interface_SIGTERM, & !< termination signal
interface_SIGUSR1, & !< 1. user-defined signal
interface_SIGUSR2 !< 2. user-defined signal
integer, public, protected :: & integer, public, protected :: &
interface_restartInc = 0 !< Increment at which calculation starts interface_restartInc = 0 !< Increment at which calculation starts
character(len=:), allocatable, public, protected :: & character(len=:), allocatable, public, protected :: &
@ -36,10 +32,7 @@ module DAMASK_interface
public :: & public :: &
getSolverJobName, & getSolverJobName, &
DAMASK_interface_init, & DAMASK_interface_init
interface_setSIGTERM, &
interface_setSIGUSR1, &
interface_setSIGUSR2
contains contains
@ -197,13 +190,6 @@ subroutine DAMASK_interface_init
if (interface_restartInc > 0) & if (interface_restartInc > 0) &
print'(a,i6.6)', ' Restart from increment: ', interface_restartInc print'(a,i6.6)', ' Restart from increment: ', interface_restartInc
call signalterm_c(c_funloc(catchSIGTERM))
call signalusr1_c(c_funloc(catchSIGUSR1))
call signalusr2_c(c_funloc(catchSIGUSR2))
call interface_setSIGTERM(.false.)
call interface_setSIGUSR1(.false.)
call interface_setSIGUSR2(.false.)
end subroutine DAMASK_interface_init end subroutine DAMASK_interface_init
@ -376,92 +362,4 @@ function makeRelativePath(a,b)
end function makeRelativePath end function makeRelativePath
end module DAMASK_interface
!--------------------------------------------------------------------------------------------------
!> @brief Set global variable interface_SIGTERM to .true.
!> @details This function can be registered to catch signals send to the executable.
!--------------------------------------------------------------------------------------------------
subroutine catchSIGTERM(signal) bind(C)
integer(C_INT), value :: signal
print'(a,i0)', ' received signal ',signal
call interface_setSIGTERM(.true.)
end subroutine catchSIGTERM
!--------------------------------------------------------------------------------------------------
!> @brief Set global variable interface_SIGUSR1 to .true.
!> @details This function can be registered to catch signals send to the executable.
!--------------------------------------------------------------------------------------------------
subroutine catchSIGUSR1(signal) bind(C)
integer(C_INT), value :: signal
print'(a,i0)', ' received signal ',signal
call interface_setSIGUSR1(.true.)
end subroutine catchSIGUSR1
!--------------------------------------------------------------------------------------------------
!> @brief Set global variable interface_SIGUSR2 to .true.
!> @details This function can be registered to catch signals send to the executable.
!--------------------------------------------------------------------------------------------------
subroutine catchSIGUSR2(signal) bind(C)
integer(C_INT), value :: signal
print'(a,i0,a)', ' received signal ',signal
call interface_setSIGUSR2(.true.)
end subroutine catchSIGUSR2
!--------------------------------------------------------------------------------------------------
!> @brief Set global variable interface_SIGTERM.
!--------------------------------------------------------------------------------------------------
subroutine interface_setSIGTERM(state)
logical, intent(in) :: state
interface_SIGTERM = state
print*, 'set SIGTERM to',state
end subroutine interface_setSIGTERM
!--------------------------------------------------------------------------------------------------
!> @brief Set global variable interface_SIGUSR.
!--------------------------------------------------------------------------------------------------
subroutine interface_setSIGUSR1(state)
logical, intent(in) :: state
interface_SIGUSR1 = state
print*, 'set SIGUSR1 to',state
end subroutine interface_setSIGUSR1
!--------------------------------------------------------------------------------------------------
!> @brief Set global variable interface_SIGUSR2.
!--------------------------------------------------------------------------------------------------
subroutine interface_setSIGUSR2(state)
logical, intent(in) :: state
interface_SIGUSR2 = state
print*, 'set SIGUSR2 to',state
end subroutine interface_setSIGUSR2
end module

View File

@ -15,6 +15,7 @@ program DAMASK_grid
use prec use prec
use parallelization use parallelization
use signals
use DAMASK_interface use DAMASK_interface
use IO use IO
use config use config
@ -448,15 +449,15 @@ program DAMASK_grid
print'(/,1x,a,i0,a)', 'increment ', totalIncsCounter, ' NOT converged' print'(/,1x,a,i0,a)', 'increment ', totalIncsCounter, ' NOT converged'
endif; flush(IO_STDOUT) endif; flush(IO_STDOUT)
call MPI_Allreduce(interface_SIGUSR1,signal,1_MPI_INTEGER_KIND,MPI_LOGICAL,MPI_LOR,MPI_COMM_WORLD,err_MPI) call MPI_Allreduce(signals_SIGUSR1,signal,1_MPI_INTEGER_KIND,MPI_LOGICAL,MPI_LOR,MPI_COMM_WORLD,err_MPI)
if (err_MPI /= 0_MPI_INTEGER_KIND) error stop 'MPI error' if (err_MPI /= 0_MPI_INTEGER_KIND) error stop 'MPI error'
if (mod(inc,loadCases(l)%f_out) == 0 .or. signal) then if (mod(inc,loadCases(l)%f_out) == 0 .or. signal) then
print'(/,1x,a)', '... writing results to file ...............................................' print'(/,1x,a)', '... writing results to file ...............................................'
flush(IO_STDOUT) flush(IO_STDOUT)
call CPFEM_results(totalIncsCounter,t) call CPFEM_results(totalIncsCounter,t)
endif endif
if (signal) call interface_setSIGUSR1(.false.) if (signal) call signals_setSIGUSR1(.false.)
call MPI_Allreduce(interface_SIGUSR2,signal,1_MPI_INTEGER_KIND,MPI_LOGICAL,MPI_LOR,MPI_COMM_WORLD,err_MPI) call MPI_Allreduce(signals_SIGUSR2,signal,1_MPI_INTEGER_KIND,MPI_LOGICAL,MPI_LOR,MPI_COMM_WORLD,err_MPI)
if (err_MPI /= 0_MPI_INTEGER_KIND) error stop 'MPI error' if (err_MPI /= 0_MPI_INTEGER_KIND) error stop 'MPI error'
if (mod(inc,loadCases(l)%f_restart) == 0 .or. signal) then if (mod(inc,loadCases(l)%f_restart) == 0 .or. signal) then
do field = 1, nActiveFields do field = 1, nActiveFields
@ -469,8 +470,8 @@ program DAMASK_grid
end do end do
call CPFEM_restartWrite call CPFEM_restartWrite
endif endif
if (signal) call interface_setSIGUSR2(.false.) if (signal) call signals_setSIGUSR2(.false.)
call MPI_Allreduce(interface_SIGTERM,signal,1_MPI_INTEGER_KIND,MPI_LOGICAL,MPI_LOR,MPI_COMM_WORLD,err_MPI) call MPI_Allreduce(signals_SIGTERM,signal,1_MPI_INTEGER_KIND,MPI_LOGICAL,MPI_LOR,MPI_COMM_WORLD,err_MPI)
if (err_MPI /= 0_MPI_INTEGER_KIND) error stop 'MPI error' if (err_MPI /= 0_MPI_INTEGER_KIND) error stop 'MPI error'
if (signal) exit loadCaseLooping if (signal) exit loadCaseLooping
endif skipping endif skipping

128
src/signals.f90 Normal file
View File

@ -0,0 +1,128 @@
!--------------------------------------------------------------------------------------------------
!> @author Martin Diehl, Max-Planck-Institut für Eisenforschung GmbH
!> @brief Handling of UNIX signals.
!--------------------------------------------------------------------------------------------------
module signals
use prec
use system_routines
implicit none
private
logical, volatile, public, protected :: &
signals_SIGTERM, & !< termination signal
signals_SIGUSR1, & !< 1. user-defined signal
signals_SIGUSR2 !< 2. user-defined signal
public :: &
signals_init, &
signals_setSIGTERM, &
signals_setSIGUSR1, &
signals_setSIGUSR2
contains
!--------------------------------------------------------------------------------------------------
!> @brief Register signal handlers.
!--------------------------------------------------------------------------------------------------
subroutine signals_init()
call signalterm_c(c_funloc(catchSIGTERM))
call signalusr1_c(c_funloc(catchSIGUSR1))
call signalusr2_c(c_funloc(catchSIGUSR2))
call signals_setSIGTERM(.false.)
call signals_setSIGUSR1(.false.)
call signals_setSIGUSR2(.false.)
end subroutine signals_init
!--------------------------------------------------------------------------------------------------
!> @brief Set global variable signals_SIGTERM to .true.
!> @details This function can be registered to catch signals send to the executable.
!--------------------------------------------------------------------------------------------------
subroutine catchSIGTERM(signal) bind(C)
integer(C_INT), value :: signal
print'(a,i0)', ' received signal ',signal
call signals_setSIGTERM(.true.)
end subroutine catchSIGTERM
!--------------------------------------------------------------------------------------------------
!> @brief Set global variable signals_SIGUSR1 to .true.
!> @details This function can be registered to catch signals send to the executable.
!--------------------------------------------------------------------------------------------------
subroutine catchSIGUSR1(signal) bind(C)
integer(C_INT), value :: signal
print'(a,i0)', ' received signal ',signal
call signals_setSIGUSR1(.true.)
end subroutine catchSIGUSR1
!--------------------------------------------------------------------------------------------------
!> @brief Set global variable signals_SIGUSR2 to .true.
!> @details This function can be registered to catch signals send to the executable.
!--------------------------------------------------------------------------------------------------
subroutine catchSIGUSR2(signal) bind(C)
integer(C_INT), value :: signal
print'(a,i0,a)', ' received signal ',signal
call signals_setSIGUSR2(.true.)
end subroutine catchSIGUSR2
!--------------------------------------------------------------------------------------------------
!> @brief Set global variable signals_SIGTERM.
!--------------------------------------------------------------------------------------------------
subroutine signals_setSIGTERM(state)
logical, intent(in) :: state
signals_SIGTERM = state
print*, 'set SIGTERM to',state
end subroutine signals_setSIGTERM
!--------------------------------------------------------------------------------------------------
!> @brief Set global variable signals_SIGUSR.
!--------------------------------------------------------------------------------------------------
subroutine signals_setSIGUSR1(state)
logical, intent(in) :: state
signals_SIGUSR1 = state
print*, 'set SIGUSR1 to',state
end subroutine signals_setSIGUSR1
!--------------------------------------------------------------------------------------------------
!> @brief Set global variable signals_SIGUSR2.
!--------------------------------------------------------------------------------------------------
subroutine signals_setSIGUSR2(state)
logical, intent(in) :: state
signals_SIGUSR2 = state
print*, 'set SIGUSR2 to',state
end subroutine signals_setSIGUSR2
end module signals