1      integer function util_time_remaining(rtdb)
2* $Id$
3      implicit none
4#include "rtdb.fh"
5#include "mafdecls.fh"
6#include "global.fh"
7      integer rtdb
8c
9c     Return the minimum of the time remaining w.r.t. limits
10c     .   set in the input (if any), and
11c     .   set in the the batch job (if any)
12c
13c     If no limit information is available then return -1
14c
15c     NB: THIS IS A COLLECTIVE FUNCTION in order to ensure that
16c     all processes get the same information and also to avoid
17c     creation of many subprocesses on machines for which it is
18c     necessary to analyse the output of shell commands using perl.
19c
20      integer user_left, batch_left, left
21      integer util_batch_job_time_remaining
22      double precision util_wallsec
23      external util_batch_job_time_remaining, util_wallsec
24      logical rtdb_mode
25c
26      if (ga_nodeid() .eq. 0) then
27c
28         rtdb_mode = rtdb_parallel(.false.)
29         if (rtdb_get(rtdb,'timelimit', mt_int, 1, user_left)) then
30*            write(6,*) ' USER LIMIT ', user_left
31            user_left = user_left - util_wallsec()
32            if (user_left .lt. 0) user_left = 0
33         else
34            user_left = -1
35         endif
36*         write(6,*) '  USER LEFT ', user_left
37         rtdb_mode = rtdb_parallel(rtdb_mode)
38         batch_left = util_batch_job_time_remaining()
39*         write(6,*) ' BATCH LEFT ', batch_left
40c
41         if (user_left .eq. -1) then ! User limit not defined
42            left = batch_left
43*            write(6,*) ' LEFT1 ', left
44         else if (batch_left .eq. -1) then ! Batch limit not defined
45            left = user_left
46*            write(6,*) ' LEFT2 ', left
47         else                   ! Both defined
48*            left = min(batch_left,user_left)
49            left = user_left    ! Seems better to allow override from input
50*            write(6,*) ' LEFT3 ', left
51         endif
52      endif
53c
54      call ga_brdcst(161,left,MA_sizeof(MT_INT,1,MT_BYTE),0)
55c
56      util_time_remaining = left
57c
58      end
59      logical function util_test_time_remaining(rtdb, required)
60      implicit none
61#include "rtdb.fh"
62#include "mafdecls.fh"
63#include "global.fh"
64#include "tcgmsg.fh"
65      integer rtdb ! [input]
66      integer required ! [input] required time in seconds
67c
68c     Return TRUE if the required no. of seconds remain in the
69c     job (minimum of user or batch job limits) or if no limits
70c     have been set.  If insufficient time remains, return FALSE.
71c
72c     NB: THIS IS A COLLECTIVE FUNCTION
73c
74      integer left, req
75      integer util_time_remaining
76      external util_time_remaining
77c
78      left = util_time_remaining(rtdb)
79c
80      util_test_time_remaining = .true.
81c
82      if (left .eq. -1) return
83c
84      req = required
85      call ga_igop(353, req, 1, 'max')
86c
87      if (left.eq.0 .or. req.gt.left) then
88         util_test_time_remaining = .false.
89         if (ga_nodeid().eq.0) then
90            write(6,1) left, required
91 1          format(/' !! Terminating execution since time remaining =',
92     $           i7,'s',/,' !!             is less than the ',
93     $           'required time =',i7,'s'/)
94            call util_flush(6)
95         endif
96      endif
97c
98      end
99