Neko  0.9.99
A portable framework for high-order spectral element flow simulations
signal.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 signal
35  use utils, only : neko_error
36  use, intrinsic :: iso_c_binding
37  implicit none
38  private
39 
42 
43  interface
44  integer (c_int8_t) function sighdl_timeout() &
45  bind(c, name='sighdl_timeout')
46  use, intrinsic :: iso_c_binding
47  end function sighdl_timeout
48  end interface
49 
50  interface
51  integer (c_int8_t) function sighdl_usr() &
52  bind(c, name='sighdl_usr')
53  use, intrinsic :: iso_c_binding
54  end function sighdl_usr
55  end interface
56 
57  interface
58  integer (c_int) function sighdl_set_timeout(sec) &
59  bind(c, name='sighdl_set_timeout')
60  use, intrinsic :: iso_c_binding
61  implicit none
62  integer(c_int) :: sec
63  end function sighdl_set_timeout
64  end interface
65 
66  interface
67  integer (c_int) function sighdl_trap_cpulimit() &
68  bind(c, name='sighdl_trap_cpulimit')
69  use, intrinsic :: iso_c_binding
70  end function sighdl_trap_cpulimit
71  end interface
72 
73  interface
74  integer (c_int) function sighdl_trap_usr() &
75  bind(c, name='sighdl_trap_usr')
76  use, intrinsic :: iso_c_binding
77  end function sighdl_trap_usr
78  end interface
79 
80 contains
81 
83  function signal_timeout() result(timeout)
84  logical :: timeout
85 
86  if (sighdl_timeout() .gt. 0) then
87  timeout = .true.
88  else
89  timeout = .false.
90  end if
91 
92  end function signal_timeout
93 
95  function signal_usr(usr) result(raised)
96  integer, intent(in) :: usr
97  logical :: raised
98  integer(kind=c_int8_t) :: usr12
99 
100  if (usr .gt. 2) then
101  call neko_error('Invalid usr signal')
102  end if
103 
104  usr12 = sighdl_usr()
105 
106  if (bge(usr12, usr)) then
107  raised = .true.
108  else
109  raised = .false.
110  end if
111 
112  end function signal_usr
113 
115  subroutine signal_set_timeout(sec)
116  integer(kind=c_int) :: sec
117 
118  if (sighdl_set_timeout(sec) .lt. 0) then
119  call neko_error('sighdl failed to set SIGALRM')
120  end if
121 
122  end subroutine signal_set_timeout
123 
125  subroutine signal_trap_cpulimit()
126  logical, save :: initialized = .false.
127 
128  if (.not. initialized) then
129  if (sighdl_trap_cpulimit() .lt. 0) then
130  call neko_error('sighdl failed to trap SIGXCPU')
131  end if
132  initialized = .true.
133  end if
134 
135  end subroutine signal_trap_cpulimit
136 
138  subroutine signal_trap_usr()
139  logical, save :: initialized = .false.
140 
141  if (.not. initialized) then
142  if (sighdl_trap_usr() .lt. 0) then
143  call neko_error('sighdl failed to trap SIGUSR*')
144  end if
145  initialized = .true.
146  end if
147 
148  end subroutine signal_trap_usr
149 
150 end module signal
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