Neko  0.8.99
A portable framework for high-order spectral element flow simulations
jobctrl.f90
Go to the documentation of this file.
1 ! Copyright (c) 2021, The Neko Authors
2 ! All rights reserved.
3 !
4 ! Redistribution and use in source and binary forms, with or without
5 ! modification, are permitted provided that the following conditions
6 ! are met:
7 !
8 ! * Redistributions of source code must retain the above copyright
9 ! notice, this list of conditions and the following disclaimer.
10 !
11 ! * Redistributions in binary form must reproduce the above
12 ! copyright notice, this list of conditions and the following
13 ! disclaimer in the documentation and/or other materials provided
14 ! with the distribution.
15 !
16 ! * Neither the name of the authors nor the names of its
17 ! contributors may be used to endorse or promote products derived
18 ! from this software without specific prior written permission.
19 !
20 ! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 ! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 ! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 ! COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 ! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 ! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 ! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 ! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 ! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 ! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 ! POSSIBILITY OF SUCH DAMAGE.
32 !
34 module jobctrl
35  use num_types, only : rp, dp
38  use utils, onlY : neko_error
39  use comm
40  use logger
41  implicit none
42  private
43 
46  end interface jobctrl_set_time_limit
47 
50 
51 contains
52 
54  subroutine jobctrl_init()
55  real(kind=dp) :: jobtime
56 
57  ! Start the job clock
58  jobtime = jobctrl_jobtime()
59 
60  ! Catch SIGXCPU
62 
63  ! Catch SIGUSR1 and SIGUSR2
64  call signal_trap_usr()
65 
66  end subroutine jobctrl_init
67 
69  subroutine jobctrl_set_time_limit_str(limit_str)
70  character(len=*) limit_str
71  integer :: str_len, sep_h, h, m, s
72 
73  str_len = len_trim(limit_str)
74 
75  if (str_len .lt. 8) then
76  call neko_error('Invalid job limit')
77  end if
78 
79  ! hour
80  sep_h = scan(trim(limit_str), ':')
81  read(limit_str(1:sep_h-1), *) h
82 
83  !min
84  read(limit_str(sep_h+1:sep_h+2), *) m
85 
86  !sec
87  read(limit_str(sep_h+4:str_len), *) s
88 
89  call jobctrl_set_time_limit_sec(h*3600 + m * 60 + s)
90 
91  end subroutine jobctrl_set_time_limit_str
92 
95  integer :: sec
96  integer :: jstop_sec
97 
98  if (sec .gt. 0) then
99  jstop_sec = sec - jobctrl_jobtime()
100  call signal_set_timeout(jstop_sec)
101  end if
102 
103  end subroutine jobctrl_set_time_limit_sec
104 
106  function jobctrl_time_limit() result(jstop)
107  logical :: jstop
108  integer :: ierr
109  character(len=LOG_SIZE) :: log_buf
110 
111  jstop = (signal_timeout() .or. signal_usr(1))
112 
113  if (jstop) then
114  ! Todo: This might be a warning instead of a message?
115  write(log_buf, '(A)') '! stop at job limit >>>'
116  call neko_log%message(log_buf, neko_log_quiet)
117  end if
118 
119  ! Let rank zero decide if we should stop
120  call mpi_bcast(jstop, 1, mpi_logical, 0, neko_comm, ierr)
121 
122  end function jobctrl_time_limit
123 
125  function jobctrl_jobtime() result(jobtime)
126  real(kind=rp), save :: stime
127  real(kind=rp) :: jobtime
128  logical, save :: init = .false.
129 
130  if (.not. init) then
131  stime = mpi_wtime()
132  init = .true.
133  end if
134 
135  jobtime = mpi_wtime() - stime
136  end function jobctrl_jobtime
137 
138 end module jobctrl
Definition: comm.F90:1
type(mpi_comm) neko_comm
MPI communicator.
Definition: comm.F90:16
Job control.
Definition: jobctrl.f90:34
subroutine jobctrl_set_time_limit_str(limit_str)
Set a job's time limit (in walltime 'HH:MM:SS')
Definition: jobctrl.f90:70
logical function, public jobctrl_time_limit()
Check if the job's time limit has been reached.
Definition: jobctrl.f90:107
real(kind=rp) function, public jobctrl_jobtime()
Returns a job's time in seconds relative to the first call.
Definition: jobctrl.f90:126
subroutine jobctrl_set_time_limit_sec(sec)
Set a job's time limit (in seconds)
Definition: jobctrl.f90:95
subroutine, public jobctrl_init()
Initialize jobctrl.
Definition: jobctrl.f90:55
Logging routines.
Definition: log.f90:34
integer, parameter, public neko_log_quiet
Always logged.
Definition: log.f90:63
type(log_t), public neko_log
Global log stream.
Definition: log.f90:61
integer, parameter, public dp
Definition: num_types.f90:9
integer, parameter, public rp
Global precision used in computations.
Definition: num_types.f90:12
Interface to signal handler.
Definition: signal.f90:34
subroutine, public signal_set_timeout(sec)
Set a timeout after seconds.
Definition: signal.f90:116
logical function, public signal_usr(usr)
Check if a user signal has been raised.
Definition: signal.f90:96
subroutine, public signal_trap_usr()
Initialize signal handler to trap SIGUSR1 and SIGUSR2.
Definition: signal.f90:139
logical function, public signal_timeout()
Check if any timeout has occurred (either SIGXCPU or SIGALRM)
Definition: signal.f90:84
subroutine, public signal_trap_cpulimit()
Initialize signal handler to trap SIGXCPU.
Definition: signal.f90:126
Utilities.
Definition: utils.f90:35