source: trunk/LMDZ.COMMON/libf/misc/job_timelimit_mod.F90 @ 3842

Last change on this file since 3842 was 3837, checked in by jbclement, 2 weeks ago

COMMON:
Follow-up of r3836 (addition of the new files).
For a review of the "--version" option, read https://lmdz-forge.lmd.jussieu.fr/mediawiki/Planets/index.php/Tool_Box#Printing_the_code_version_of_a_program or use the command-line option "--help" when running your program.
JBC

File size: 2.9 KB
RevLine 
[3837]1MODULE job_timelimit_mod
2
3!***********************************************************************
4! DESCRIPTION:
5!    Retrieves the time limit (in seconds) for a given job ID via
6!    SLURM (squeue) or PBS/TORQUE (qstat).
7!***********************************************************************
8
9implicit none
10
11real            :: timelimit = 86400. ! Time limit for the job: 86400 s = 24 h by default
12real, parameter :: antetime  = 3600.  ! Anticipation time to prevent reaching the job time limit: 3600 s = 1 h by default
13logical         :: timewall = .false. ! Flag to enable the program self-termination before timeout
14
15!=======================================================================
16contains
17!=======================================================================
18
19SUBROUTINE get_job_timelimit(arg)
20
21implicit none
22
23!---- Arguments
24character(*), intent(in) :: arg
25
26!---- Variables
27logical        :: num_str
28character(256) :: chtimelimit ! Time limit for the job outputted by the SLURM or PBS/TORQUE command
29integer        :: i, cstat, days, hours, minutes, seconds
30character(1)   :: sep
31
32!---- Code
33! Check that the job ID is numeric
34num_str = .true.
35do i = 1,len_trim(arg)
36    if (arg(i:i) < '0' .or. arg(i:i) > '9') then
37        num_str = .false.
38        exit
39    endif
40enddo
41if (.not. num_str) error stop 'Error: job ID must be numeric.'
42
43! Try SLURM (squeue)
44call execute_command_line('squeue -j '//trim(adjustl(arg))//' -h --Format TimeLimit > tmp_cmdout.txt',cmdstat = cstat)
45if (cstat /= 0) then
46    ! On failure, try PBS/TORQUE (qstat)
47    call execute_command_line('qstat -f '//trim(adjustl(arg))//' | grep "Walltime" | awk ''{print $3}'' > tmp_cmdout.txt',cmdstat = cstat)
48    if (cstat > 0) then
49        error stop 'Error: command execution failed!'
50    else if (cstat < 0) then
51        error stop 'Error: command execution not supported (neither SLURM nor PBS/TORQUE is installed)!'
52    endif
53endif
54
55! Read the output
56open(1,file = 'tmp_cmdout.txt',status = 'old')
57read(1,'(a)') chtimelimit
58close(1)
59chtimelimit = trim(adjustl(chtimelimit))
60
61! Remove temporary file
62call execute_command_line('rm tmp_cmdout.txt',cmdstat = cstat)
63if (cstat > 0) then
64    error stop 'Error: command execution failed!'
65else if (cstat < 0) then
66    error stop 'Error: command execution not supported!'
67endif
68
69! Parse D-HH:MM:SS, HH:MM:SS or MM:SS
70if (index(chtimelimit,'-') > 0) then ! Format "D-HH:MM:SS"
71    read(chtimelimit,'(i1,a1,i2,a1,i2,a1,i2)') days, sep, hours, sep, minutes, sep, seconds
72    timelimit = days*86400 + hours*3600 + minutes*60 + seconds
73else if (index(chtimelimit,':') > 0 .and. len_trim(chtimelimit) > 5) then ! Format "HH:MM:SS"
74    read(chtimelimit,'(i2,a1,i2,a1,i2)') hours, sep, minutes, sep, seconds
75    timelimit = hours*3600 + minutes*60 + seconds
76else ! Format "MM:SS"
77    read(chtimelimit,'(i2,a1,i2)') minutes, sep, seconds
78    timelimit = minutes*60 + seconds
79endif
80
81timewall = .true.
82
83END SUBROUTINE get_job_timelimit
84
85END MODULE job_timelimit_mod
Note: See TracBrowser for help on using the repository browser.