time.F90 23.4 KB
Newer Older
1 2 3 4 5
!--------------------------------------------------------------------------------
! Copyright (c) 2016 Peter Grünberg Institut, Forschungszentrum Jülich, Germany
! This file is part of FLEUR and available as free software under the conditions
! of the MIT license as expressed in the LICENSE file in more detail.
!--------------------------------------------------------------------------------
Daniel Wortmann's avatar
Daniel Wortmann committed
6
MODULE m_juDFT_time
7
!DEC$ NOOPTIMIZE
Matthias Redies's avatar
Matthias Redies committed
8 9 10 11 12 13 14
   !*****************************************************************
   !     DESC:Timer module for measuring the execution times of different
   !     parts of the code
   !     timestart and timestop should be
   !     called with suitable names for timers
   !     Daniel Wortmann, Fri Sep  6 11:53:08 2002
   !*****************************************************************
15
   USE m_judft_xmlOutput
16 17 18
#ifdef CPP_MPI 
   use mpi 
#endif
Matthias Redies's avatar
Matthias Redies committed
19 20 21
   IMPLICIT NONE
   !     List of different timers
   PRIVATE
22
   INTEGER, PARAMETER        :: max_subtimer = 5 ! blocks of subtimers are allocated in this size
Matthias Redies's avatar
Matthias Redies committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
   REAL                      :: min_time = 0.02 ! minimal time to display in output (part of total)
   LOGICAL                   :: l_debug  !write out each start& stop of timer
   REAL                      :: debugtimestart = -1.0
   TYPE t_p
      TYPE(t_timer), POINTER:: p
   END TYPE t_p

   TYPE t_timer
      REAL                  :: starttime
      REAL                  :: time, mintime, maxtime
      INTEGER               :: no_calls
      CHARACTER(LEN=60)     :: name
      INTEGER               :: n_subtimers
      TYPE(t_p), ALLOCATABLE :: subtimer(:)
      TYPE(t_timer), POINTER :: parenttimer
   END TYPE t_timer

   TYPE(t_timer), POINTER, SAVE:: globaltimer => NULL()
   TYPE(t_timer), POINTER, SAVE:: current_timer => NULL()
   CHARACTER(LEN=256), SAVE   :: lastfile = ""
   INTEGER, SAVE   :: lastline = 0

45
   PUBLIC timestart, timestop, writetimes, writeTimesXML, cputime, addtime
Matthias Redies's avatar
Matthias Redies committed
46 47
   PUBLIC resetIterationDependentTimers, check_time_for_next_iteration
   PUBLIC juDFT_time_lastlocation !should be used for error handling only
Daniel Wortmann's avatar
Daniel Wortmann committed
48 49 50

CONTAINS

Matthias Redies's avatar
Matthias Redies committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
   SUBROUTINE priv_new_timer(name)
      IMPLICIT NONE
      CHARACTER(LEN=*), INTENT(IN)          ::name
      TYPE(t_timer), POINTER      ::t
      TYPE(t_p), ALLOCATABLE      :: tmp(:)

      ALLOCATE (t)
      t%starttime = cputime()
      t%time = 0.0
      t%mintime = 1E99
      t%maxtime = 0.0
      t%no_calls = 0
      t%name = name
      t%n_subtimers = 0
      t%parenttimer => null()
      ALLOCATE (t%subtimer(max_subtimer))

      IF (ASSOCIATED(current_timer)) THEN
         ALLOCATE (t%parenttimer)
         t%parenttimer => current_timer
         !now add timer to list of subtimers of parenttimer
         IF (current_timer%n_subtimers + 1 > SIZE(current_timer%subtimer)) THEN
            ALLOCATE (tmp(SIZE(current_timer%subtimer)))
            tmp = current_timer%subtimer
            DEALLOCATE (current_timer%subtimer)
            ALLOCATE (current_timer%subtimer(SIZE(tmp) + max_subtimer))
            current_timer%subtimer(:SIZE(tmp)) = tmp
            DEALLOCATE (tmp)
         ENDIF

         current_timer%n_subtimers = current_timer%n_subtimers + 1

         current_timer%subtimer(current_timer%n_subtimers)%p => t
      ELSE
         globaltimer => t
      ENDIF
      current_timer => t

   END SUBROUTINE priv_new_timer

   !<-- S: timestart(timer)

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
   subroutine addtime(ttimer, time, n_calls)
      implicit none 
      CHARACTER(LEN=*), INTENT(IN) :: ttimer
      real, intent(in)             :: time
      integer, intent(in)          :: n_calls
      TYPE(t_timer), POINTER       :: ptr_timer
      logical  :: found
      integer  :: n

      found = .False.
      do n = 1,current_timer%n_subtimers 
         if(trim(ttimer) == TRIM(current_timer%subtimer(n)%p%name)) then 
            ptr_timer => current_timer%subtimer(n)%p
            found = .True.
            exit 
         endif
      enddo

      if(.not. found) then
         call priv_new_timer(ttimer)
Matthias Redies's avatar
bugfix  
Matthias Redies committed
113
         ptr_timer => current_timer
114 115 116 117 118 119 120 121 122 123 124
         current_timer => current_timer%parenttimer
      endif
      call priv_debug_output(" finished ", ttimer)

      ptr_timer%time = ptr_timer%time + time
      ptr_timer%no_calls = ptr_timer%no_calls + 1
      ptr_timer%mintime = -1
      ptr_timer%maxtime = -1
      ptr_timer%starttime = -1
   end subroutine addtime

Matthias Redies's avatar
Matthias Redies committed
125 126 127 128 129 130 131 132
   SUBROUTINE timestart(ttimer, file, line)
      USE m_judft_args
      IMPLICIT NONE
      CHARACTER(LEN=*), INTENT(IN)          :: ttimer
      CHARACTER(LEN=*), INTENT(IN), OPTIONAL  :: file
      INTEGER, INTENT(IN), OPTIONAL           :: line

      INTEGER::n
133 134 135 136 137 138
         IF (PRESENT(file)) lastfile = file
         IF (PRESENT(line)) lastline = line
         IF (.NOT. ASSOCIATED(current_timer)) THEN
            CALL priv_new_timer("Total Run")
            l_debug = judft_was_Argument("-debugtime")
         ENDIF
Matthias Redies's avatar
Matthias Redies committed
139

140 141 142 143 144 145 146 147 148 149
         DO n = 1, current_timer%n_subtimers
            IF (TRIM(ttimer) == TRIM(current_timer%subtimer(n)%p%name)) THEN
               current_timer => current_timer%subtimer(n)%p
               IF (current_timer%starttime > 0) THEN
                  WRITE (*, *) "Timer already running:", ttimer
                  STOP "BUG:starttime"
               ENDIF
               current_timer%starttime = cputime()
               CALL priv_debug_output(" started ", current_timer%name)
               RETURN
Matthias Redies's avatar
Matthias Redies committed
150
            ENDIF
151 152 153 154
         ENDDO
         !new subtimer
         CALL priv_new_timer(ttimer)
         CALL priv_debug_output(" started ", current_timer%name)
Matthias Redies's avatar
Matthias Redies committed
155 156 157 158 159 160 161 162 163 164
   END SUBROUTINE timestart

   !>
   !<-- S:timestop(timer)

   SUBROUTINE timestop(ttimer)
      CHARACTER(LEN=*), INTENT(IN) :: ttimer

      REAL::time

165
         IF (.NOT. TRIM(ttimer) == TRIM(current_timer%name)) THEN
166
            WRITE (*, *) "Current timer:", trim(current_timer%name), " could not stop:"
167 168 169 170 171 172 173 174 175 176 177 178
            STOP "BUG:timestop"
         ENDIF
         IF (current_timer%starttime < 0) THEN
            WRITE (*, *) "Timer not initialized:"//ttimer
            STOP "BUG:timestop"
         ENDIF
         time = cputime() - current_timer%starttime !This runtime
         current_timer%time = current_timer%time + time
         current_timer%no_calls = current_timer%no_calls + 1
         current_timer%mintime = MIN(current_timer%mintime, time)
         current_timer%maxtime = MAX(current_timer%maxtime, time)
         current_timer%starttime = -1
Matthias Redies's avatar
Matthias Redies committed
179

180
         CALL priv_debug_output(" stopped ", current_timer%name)
Matthias Redies's avatar
Matthias Redies committed
181

182
         current_timer => current_timer%parenttimer
Matthias Redies's avatar
Matthias Redies committed
183 184 185 186 187 188 189
   END SUBROUTINE timestop

   !>
   SUBROUTINE priv_debug_output(startstop, name)
      USE m_judft_sysinfo
      IMPLICIT NONE
      CHARACTER(LEN=*), INTENT(IN):: startstop, name
190
#ifdef CPP_MPI
Matthias Redies's avatar
Matthias Redies committed
191
      INTEGER::irank, ierr
192
      LOGICAL:: l_mpi
Matthias Redies's avatar
Matthias Redies committed
193
      INCLUDE 'mpif.h'
194
#endif
Matthias Redies's avatar
Matthias Redies committed
195 196
      IF (.NOT. l_debug) RETURN
      if (debugtimestart < 0) debugtimestart = cputime()
197
#ifdef CPP_MPI
198
      CALL MPI_INITIALIZED(l_mpi,ierr)
199 200 201 202 203 204
      IF (l_mpi) THEN
         CALL MPI_COMM_RANK(MPI_COMM_WORLD, irank, ierr)
         WRITE (*, "(i3,3a,f20.2,5x,a)") irank, startstop, name, " at:", cputime() - debugtimestart, memory_usage_string()
      ELSE
         WRITE (*, "(3a,f20.2,5x,a)") startstop, name, " at:", cputime() - debugtimestart, memory_usage_string()
      ENDIF
Daniel Wortmann's avatar
Daniel Wortmann committed
205
#else
Matthias Redies's avatar
Matthias Redies committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
      WRITE (*, "(3a,f20.2,5x,a)") startstop, name, " at:", cputime() - debugtimestart, memory_usage_string()
#endif
   END SUBROUTINE priv_debug_output

   RECURSIVE SUBROUTINE priv_writetimes_longest(timer, fid, timernames, timertimes)
      IMPLICIT NONE
      TYPE(t_timer), INTENT(IN)                 :: timer
      INTEGER, INTENT(IN), OPTIONAL              :: fid
      CHARACTER(LEN=60), INTENT(INOUT), OPTIONAL ::timernames(10)
      REAL, INTENT(INOUT), OPTIONAL              ::timertimes(10)

      REAL, ALLOCATABLE              ::times(:)
      CHARACTER(LEN=60), ALLOCATABLE ::names(:)
      REAL                          ::sum_time

      INTEGER :: n, i
      IF (.NOT. PRESENT(timernames)) THEN
         ALLOCATE (times(10), names(10))
         times = 0.0
         names = ""
         CALL priv_writetimes_longest(timer, timernames=names, timertimes=times)
         WRITE (fid, *)
         WRITE (fid, *) "-------------------------------------------------"
         WRITE (fid, *)
         WRITE (fid, *) "most relevant subroutines:"
         sum_time = 0.0
         DO n = 1, 10
            IF (MAXVAL(times) < 1E-4) EXIT
            i = MAXLOC(times, dim=1)
            WRITE (fid, "(a,T7,a,T46,a)") " ", TRIM(names(i)), timestring(times(i), timer%time, 2)
            sum_time = sum_time + times(i)
            times(i) = 0.0
         ENDDO
239
         WRITE (fid, "(t77,'Sum: ',f5.1,'%')") sum_time/max(1E-10,timer%time)*100.
Matthias Redies's avatar
Matthias Redies committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
         WRITE (fid, *)
         WRITE (fid, *) "-------------------------------------------------"
         WRITE (fid, *)
         RETURN
      ENDIF

      sum_time = timer%time
      DO n = 1, timer%n_subtimers
         CALL priv_writetimes_longest(timer%subtimer(n)%p, timernames=timernames, timertimes=timertimes)
         sum_time = sum_time - timer%subtimer(n)%p%time
      ENDDO
      IF (sum_time > MINVAL(timertimes)) THEN
         i = MINLOC(timertimes, dim=1)
         IF (ASSOCIATED(timer%parenttimer)) THEN
            WRITE (timernames(i), "(a18,'->',a40)") timer%parenttimer%name, timer%name
         ELSE
            timernames(i) = timer%name
         ENDIF
         timertimes(i) = sum_time
      ENDIF
   END SUBROUTINE priv_writetimes_longest

   RECURSIVE SUBROUTINE priv_writetimes(timer, level, fid, debug)
      IMPLICIT NONE
      TYPE(t_timer), INTENT(IN)    :: timer
      INTEGER, INTENT(IN)          :: level, fid
      LOGICAL, INTENT(IN), OPTIONAL ::debug

      INTEGER          :: n
      REAL             :: time
      CHARACTER(LEN=30):: timername, indentstring
      WRITE (timername, "(i0)") level - 1
      WRITE (indentstring, "(100a)") (" ", n=2, (MIN(level, 5))), TRIM(timername)
      IF (timer%starttime > 0) THEN
         time = timer%time + cputime() - timer%starttime
         timername = timer%name//" not term."
      ELSE
         time = timer%time
         timername = timer%name
      ENDIF
      IF (time < min_time*globaltimer%time) RETURN !do not print parts that take less than min_time of the totaltime
      IF (timer%no_calls > 1) THEN
         WRITE (fid, "(a,T7,a,T46,a,T85,a,i0,a,f9.3,a,f9.3,a)") &
            TRIM(indentstring), TRIM(timername), TRIM(timestring(time, globaltimer%time, level)), &
            "%  (", timer%no_calls, "calls:", timer%mintime, "sec -", timer%maxtime, "sec)"
      ELSE
         WRITE (fid, "(a,T7,a,T46,a)") TRIM(indentstring), TRIM(timername), timestring(time, globaltimer%time, level)
      END IF
      FLUSH(fid)
      IF (PRESENT(debug) .OR. timer%n_subtimers == 0) RETURN

      time = 0
      DO n = 1, timer%n_subtimers
         time = time + timer%subtimer(n)%p%time
      ENDDO
Matthias Redies's avatar
Matthias Redies committed
295
      WRITE (fid, "(a,a,T46,3a)") TRIM(indentstring), " measured in submodules:", TRIM(timestring(time, -.1, level))
Matthias Redies's avatar
Matthias Redies committed
296 297 298 299 300 301 302
      FLUSH(fid)
      DO n = 1, timer%n_subtimers
         CALL priv_writetimes(timer%subtimer(n)%p, level + 1, fid)
      ENDDO
   END SUBROUTINE priv_writetimes

   !<-- S:writetimes()
303

Matthias Redies's avatar
Matthias Redies committed
304
   RECURSIVE SUBROUTINE priv_genjson(timer, level, outstr, opt_idstr)
Matthias Redies's avatar
Matthias Redies committed
305 306 307 308 309
      use m_judft_string
      IMPLICIT NONE
      TYPE(t_timer), INTENT(IN)                    :: timer
      INTEGER, INTENT(IN)                          :: level
      CHARACTER(len=:), allocatable, INTENT(INOUT) :: outstr
Matthias Redies's avatar
Matthias Redies committed
310
      CHARACTER(len=:), allocatable, optional      :: opt_idstr
Matthias Redies's avatar
Matthias Redies committed
311 312

      CHARACTER(len=1), PARAMETER   :: nl=NEW_LINE("A")
Matthias Redies's avatar
Matthias Redies committed
313
      INTEGER, PARAMETER            :: indent_spaces=3
Matthias Redies's avatar
Matthias Redies committed
314 315 316

      INTEGER          :: n
      REAL             :: time
317
      CHARACTER(LEN=30):: timername
Matthias Redies's avatar
Matthias Redies committed
318
      CHARACTER(LEN=:), allocatable :: idstr
319

Matthias Redies's avatar
Matthias Redies committed
320 321 322 323 324
      if(present(opt_idstr)) then
         idstr = opt_idstr
      else
         idstr = ""
      endif
325

Matthias Redies's avatar
Matthias Redies committed
326 327 328 329 330 331 332
      IF (timer%starttime > 0) THEN
         time = timer%time + cputime() - timer%starttime
         timername = timer%name//" not term."
      ELSE
         time = timer%time
         timername = timer%name
      ENDIF
333 334

      if(level > 1 ) outstr = outstr // nl
335 336 337 338
      outstr = outstr // idstr // "{"
      idstr = idstr // repeat(" ", indent_spaces)

      outstr = outstr // nl // idstr // '"timername" : "' // trim(timername)         // '",'
339
      outstr = outstr // nl // idstr // '"totaltime" : '  // float2str(time)
340 341 342 343
      if(level > 1) then
         outstr = outstr // ","
         outstr = outstr // nl // idstr // '"mintime"   : '  // float2str(timer%mintime)// ','
         outstr = outstr // nl // idstr // '"maxtime"   : '  // float2str(timer%maxtime)// ','
344
         outstr = outstr // nl // idstr // '"ncalls"    : '  // int2str(timer%no_calls)
345
      endif
Matthias Redies's avatar
Matthias Redies committed
346

347 348 349 350 351 352
      time = 0
      DO n = 1, timer%n_subtimers
         time = time + timer%subtimer(n)%p%time
      ENDDO
      if(timer%n_subtimers > 0) then
         !add comma behind ncalls
353 354
         outstr = outstr // ","
         outstr = outstr // nl // idstr // '"subtimers": '  // "["
355
         idstr = idstr // repeat(" ", indent_spaces)
Matthias Redies's avatar
Matthias Redies committed
356
         DO n = 1, timer%n_subtimers
357 358
            CALL priv_genjson(timer%subtimer(n)%p, level + 1, outstr, idstr)
            if(n /= timer%n_subtimers) outstr = outstr // ","
Matthias Redies's avatar
Matthias Redies committed
359
         ENDDO
360 361
         idstr  = idstr(:len(idstr)-indent_spaces)
         outstr = outstr // nl // idstr // ']'
362
      endif
363
      idstr  = idstr(:len(idstr)-indent_spaces)
Matthias Redies's avatar
Matthias Redies committed
364
      outstr = outstr // nl // idstr // "}"
Matthias Redies's avatar
Matthias Redies committed
365
   END SUBROUTINE priv_genjson
Matthias Redies's avatar
Matthias Redies committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

   RECURSIVE SUBROUTINE writelocation(location)
      !writes the stack of current timers to std-out
      !usefull for debugging and error messages
      IMPLICIT NONE
      TYPE(t_timer), INTENT(IN), OPTIONAL::location

      IF (.NOT. PRESENT(location)) THEN
         IF (ASSOCIATED(current_timer)) CALL writelocation(current_timer)
      ELSE
         WRITE (0, *) "Timer:", location%name
         IF (ASSOCIATED(location%parenttimer)) CALL writelocation(location%parenttimer)
      ENDIF
   END SUBROUTINE writelocation

   ! writes all times to file
   SUBROUTINE writetimes(stdout)
383 384 385
#ifdef CPP_MPI 
     use mpi 
#endif
386
     USE m_juDFT_internalParams
387 388
     USE m_judft_usage
     USE m_judft_args
Matthias Redies's avatar
Matthias Redies committed
389 390
      IMPLICIT NONE
      LOGICAL, INTENT(IN), OPTIONAL::stdout
391
      INTEGER :: irank = 0
Matthias Redies's avatar
Matthias Redies committed
392
      CHARACTER(len=:), allocatable :: json_str
393
      CHARACTER(len=30)::filename
394
#ifdef CPP_MPI
395 396 397 398
      INTEGER::err,isize
      LOGICAL:: l_mpi
      CALL mpi_initialized(l_mpi,err)
      if (l_mpi) CALL MPI_COMM_RANK(MPI_COMM_WORLD, irank, err)
399
#endif
Matthias Redies's avatar
Matthias Redies committed
400
      IF (.NOT. ASSOCIATED(globaltimer)) RETURN !write nothing if no timing recorded
401

402

403 404 405
      IF (irank == 0) THEN
         globaltimer%time = cputime() - globaltimer%starttime
         globaltimer%starttime = cputime()
406
         WRITE (juDFT_outUnit, "(//,'Total execution time: ',i0,'sec')") INT(globaltimer%time)
407
         CALL add_usage_data("Runtime", globaltimer%time)
408
         CALL priv_writetimes_longest(globaltimer, fid=juDFT_outUnit)
Matthias Redies's avatar
Matthias Redies committed
409

410
         WRITE (juDFT_outUnit, "('Total execution time: ',i0,'sec, minimal timing printed:',i0,'sec')") &
411
            INT(globaltimer%time), INT(min_time*globaltimer%time)
412

413
         CALL priv_writetimes(globaltimer, 1, juDFT_outUnit)
414
#ifdef CPP_MPI
415 416
         IF (l_mpi) THEN
            CALL MPI_COMM_SIZE(MPI_COMM_WORLD, isize, err)
417
            WRITE (juDFT_outUnit, *) "Program used ", isize, " PE"
418
         ENDIF
419
#endif
420
      END IF
421
      IF (irank==0.OR.judft_was_argument("-all_times")) THEN
422
         json_str = ""
423
         CALL priv_genjson(globaltimer, 1, json_str)
424
         IF (.not. judft_was_argument("-all_times")) THEN
425 426
            OPEN(32, file="juDFT_times.json")
         ELSE
427
            WRITE(filename,"(a,i0.4,a)") "juDFT_times_pe=",irank,".json"
428 429
            OPEN(32, file=trim(filename))
         END IF
430 431 432
         write (32,"(A)") json_str
         close(32)
      ENDIF
Matthias Redies's avatar
Matthias Redies committed
433
   END SUBROUTINE writetimes
434

Matthias Redies's avatar
Matthias Redies committed
435 436
   ! writes all times to out.xml file
   SUBROUTINE writeTimesXML()
437 438 439
#ifdef CPP_MPI 
      use mpi 
#endif
Matthias Redies's avatar
Matthias Redies committed
440
      IMPLICIT NONE
441

442
      INTEGER                ::  irank = 0
Matthias Redies's avatar
Matthias Redies committed
443 444
      LOGICAL                :: l_out
      TYPE(t_timer), POINTER :: timer
445
#ifdef CPP_MPI
Matthias Redies's avatar
Matthias Redies committed
446
      INTEGER::err, isize
447 448 449
      LOGICAL:: l_mpi
      CALL mpi_initialized(l_mpi,err)
      IF (l_mpi) CALL MPI_COMM_RANK(MPI_COMM_WORLD, irank, err)
450
#endif
451

Matthias Redies's avatar
Matthias Redies committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
      IF (irank .NE. 0) RETURN
      IF (.NOT. ASSOCIATED(globaltimer)) RETURN !write nothing if no timing recorded

      timer => NULL()
      CALL findIterationTimer(globaltimer, timer)
      IF (.NOT. ASSOCIATED(timer)) RETURN

      CALL openXMLElement('timing', (/'units'/), (/'sec'/))
      CALL privWriteTimesXML(timer, 1)
      CALL closeXMLElement('timing')

   END SUBROUTINE writeTimesXML

   RECURSIVE SUBROUTINE privWriteTimesXML(timer, level)

      IMPLICIT NONE

      TYPE(t_timer), INTENT(IN)    :: timer
      INTEGER, INTENT(IN)          :: level

      INTEGER            :: n, timerNameLength
      REAL               :: time
      CHARACTER(LEN=50)  :: timername
      CHARACTER(LEN=50)  :: attributes(2)

      IF (timer%starttime > 0) THEN
         time = timer%time + cputime() - timer%starttime
      ELSE
         time = timer%time
      END IF
      timername = ''
      DO n = 1, level - 2
         timername = timername(1:3*(n - 1))//'|  '
      END DO
      IF (level .GE. 2) THEN
         timername = timername(1:3*(level - 2))//'+--'
      END IF
      timername = TRIM(ADJUSTL(timername))//TRIM(ADJUSTL(timer%name))
      timerNameLength = LEN(TRIM(ADJUSTL(timername)))

      DO n = 1, timerNameLength
         IF (timername(n:n) .EQ. '&') THEN
            timername(n:n) = '+'
         END IF
      END DO

      WRITE (attributes(1), '(a)') TRIM(ADJUSTL(timername))
      WRITE (attributes(2), '(f12.3)') time
      IF (timer%n_subtimers .EQ. 0) THEN
         CALL writeXMLElementForm('timer', (/'name ', 'value'/), attributes, &
                                  RESHAPE((/14 + 20 - 3*level, 55 - timerNameLength, timerNameLength, 12/), (/2, 2/)))
      ELSE
         CALL openXMLElementForm('compositeTimer', (/'name ', 'value'/), attributes, &
                                 RESHAPE((/5 + 20 - 3*level, 55 - timerNameLength, timerNameLength, 12/), (/2, 2/)))
         DO n = 1, timer%n_subtimers
            CALL privWriteTimesXML(timer%subtimer(n)%p, level + 1)
         END DO
         CALL closeXMLElement('compositeTimer')
      END IF
   END SUBROUTINE privWriteTimesXML

   SUBROUTINE check_time_for_next_iteration(it, l_cont)
514

Matthias Redies's avatar
Matthias Redies committed
515 516 517 518
      USE m_judft_args
      IMPLICIT NONE
      INTEGER, INTENT(IN)     :: it
      LOGICAL, INTENT(INOUT)  :: l_cont
Matthias Redies's avatar
Matthias Redies committed
519 520 521 522
      CHARACTER(len=1000) :: wtime_string
      INTEGER             :: time_used, time_per_iter
      INTEGER             :: irank = 0
      real                :: wtime
523
#ifdef CPP_MPI
Matthias Redies's avatar
Matthias Redies committed
524
      INTEGER::err, isize
525 526 527
      LOGICAL:: l_mpi
      CALL mpi_initialized(l_mpi,err)
      if (l_mpi) CALL MPI_COMM_RANK(MPI_COMM_WORLD, irank, err)
528 529
#endif

Matthias Redies's avatar
Matthias Redies committed
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
      IF (.NOT. l_cont) RETURN !stop anyway
      IF (.NOT. ASSOCIATED(globaltimer)) RETURN !do nothing if no timing recorded
      IF (judft_was_argument("-wtime")) THEN
         wtime_string = judft_string_for_argument("-wtime")
         READ (wtime_string, *) wtime
         time_used = FLOOR((cputime() - globaltimer%starttime)/60.0) + 1
         time_per_iter = FLOOR((cputime() - globaltimer%starttime)/60.0/it) + 1
         IF (irank == 0) THEN
            WRITE (*, *) "Test for time of next iteration:"
            WRITE (*, *) "Time provided (min):", wtime
            WRITE (*, *) "Time used     (min):", time_used
            WRITE (*, *) "Time per iter (min):", time_per_iter
         ENDIF
         IF (time_used + time_per_iter > wtime) THEN
            l_cont = .FALSE.
            IF (irank == 0) WRITE (*, *) "No further iterations"
         ENDIF
      END IF
   END SUBROUTINE check_time_for_next_iteration

   SUBROUTINE resetIterationDependentTimers()
551 552 553
#ifdef CPP_MPI 
      use mpi 
#endif
Matthias Redies's avatar
Matthias Redies committed
554 555
      IMPLICIT NONE

556
      INTEGER                ::  irank = 0
Matthias Redies's avatar
Matthias Redies committed
557 558
      LOGICAL                :: l_out
      TYPE(t_timer), POINTER :: timer, parenttimer
559
#ifdef CPP_MPI
Matthias Redies's avatar
Matthias Redies committed
560
      INTEGER::err, isize
561 562 563
      LOGICAL:: l_mpi
      CALL mpi_initialized(l_mpi,err)
      if (l_mpi)  CALL MPI_COMM_RANK(MPI_COMM_WORLD, irank, err)
564
#endif
Matthias Redies's avatar
Matthias Redies committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
      !Check if not enough time for another iteration is left

      IF (irank .NE. 0) RETURN
      IF (.NOT. ASSOCIATED(globaltimer)) RETURN !write nothing if no timing recorded

      timer => NULL()
      CALL findIterationTimer(globaltimer, timer)
      IF (.NOT. ASSOCIATED(timer)) RETURN

      DO WHILE (TRIM(ADJUSTL(timer%name)) .NE. 'Iteration')
         IF (timer%n_subtimers .EQ. 0) RETURN
         timer => timer%subtimer(1)%p
      END DO

      parenttimer => timer%parenttimer
      CALL resetSubtimers(timer%parenttimer)
      ALLOCATE (parenttimer%subtimer(5))

   END SUBROUTINE resetIterationDependentTimers

   RECURSIVE SUBROUTINE findIterationTimer(parent, result)

      IMPLICIT NONE

      TYPE(t_timer), POINTER, INTENT(IN)    :: parent
      TYPE(t_timer), POINTER, INTENT(OUT)   :: result

      INTEGER numSubtimers, i

      IF (TRIM(ADJUSTL(parent%name)) .EQ. 'Iteration') THEN
         result => parent
      ELSE
         numSubtimers = parent%n_subtimers
         DO i = 1, numSubtimers
            CALL findIterationTimer(parent%subtimer(i)%p, result)
            IF (ASSOCIATED(result)) RETURN
         END DO
      END IF

   END SUBROUTINE findIterationTimer

   RECURSIVE SUBROUTINE resetSubtimers(timer)

      IMPLICIT NONE

      TYPE(t_timer), INTENT(INOUT)    :: timer

      INTEGER :: n

      DO n = 1, timer%n_subtimers
         CALL resetSubtimers(timer%subtimer(n)%p)
         DEALLOCATE (timer%subtimer(n)%p)
      END DO
      DEALLOCATE (timer%subtimer)
      timer%n_subtimers = 0
   END SUBROUTINE resetSubtimers

   !>
   !<-- private function timestring

   FUNCTION timestring(time, ttime, level)
      REAL, INTENT(IN)    :: time, ttime
      CHARACTER*(90)     :: timestring
      INTEGER, INTENT(IN) ::level
      !     .. Local Scalars ..
      REAL :: rest, seconds
      INTEGER :: ihours, iminutes

      rest = time
      ihours = INT(rest/3600.0)
      rest = rest - REAL(ihours)*3600
      iminutes = INT(rest/60.0)
      seconds = rest - REAL(iminutes)*60
      IF (ttime < 0) THEN
         WRITE (timestring, "(f9.2,'sec= ',i3,'h ',i2,'min ',i2,'sec')") time, ihours, iminutes, INT(seconds)
      ELSE
         WRITE (timestring, "(f9.2,'sec= ',i3,'h ',i2,'min ',i2,'sec ->',1x,f5.1,'%')") &
642
            time, ihours, iminutes, INT(seconds), time/max(1E-10,ttime)*100.0
Matthias Redies's avatar
Matthias Redies committed
643 644 645 646 647 648 649 650
      ENDIF
   END FUNCTION timestring

   !>
   !<-- F: cputime()
   !Private function to return the cpu_time in sec
   FUNCTION cputime()
!$    use omp_lib
651
#ifdef __INTEL_COMPILER
Matthias Redies's avatar
Matthias Redies committed
652
      USE ifport
653
#endif
Matthias Redies's avatar
Matthias Redies committed
654 655
      IMPLICIT NONE
      REAL::cputime
656

Matthias Redies's avatar
Matthias Redies committed
657
      !TRY TO USE mpi OR openmp wall-clock functions
658
#ifdef _OPENMP
659
      cputime = real(omp_get_wtime(),kind=kind(cputime))
660
#elif __INTEL_COMPILER
Matthias Redies's avatar
Matthias Redies committed
661
      cputime = rtc()
662
#elif CPP_MPI
663
      cputime = real(MPI_WTIME(),kind=kind(cputime))
664
#else
Matthias Redies's avatar
Matthias Redies committed
665 666
      !use f95 intrinsic function
      CALL CPU_TIME(cputime)
667
#endif
Matthias Redies's avatar
Matthias Redies committed
668 669 670 671
   END FUNCTION cputime
   !>
   SUBROUTINE juDFT_time_lastlocation()
      IF (ASSOCIATED(current_timer)) THEN
672
         WRITE (0, *) "Last known location:"
Matthias Redies's avatar
Matthias Redies committed
673 674 675 676 677 678 679 680 681 682 683
         WRITE (0, *) "Last timer:", current_timer%name
         IF (lastline > 0) THEN
            WRITE (0, *) "File:", TRIM(lastfile), ":", lastline
         ENDIF
         IF (ASSOCIATED(current_timer%parenttimer)) THEN
            WRITE (0, *) "Timerstack:"
            CALL writelocation(current_timer%parenttimer)
         ENDIF
         WRITE (0, *) "*****************************************"
      END IF
   END SUBROUTINE juDFT_time_lastlocation
Daniel Wortmann's avatar
Daniel Wortmann committed
684
END MODULE m_juDFT_time