New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
timing.F90 in NEMO/branches/2020/r12377_ticket2386/src/OCE – NEMO

source: NEMO/branches/2020/r12377_ticket2386/src/OCE/timing.F90 @ 13540

Last change on this file since 13540 was 13540, checked in by andmirek, 3 years ago

Ticket #2386: update to latest trunk

  • Property svn:keywords set to Id
File size: 35.8 KB
Line 
1MODULE timing
2   !!========================================================================
3   !!                     ***  MODULE  timing  ***
4   !!========================================================================
5   !! History : 4.0  ! 2001-05  (R. Benshila)   
6   !!------------------------------------------------------------------------
7
8   !!------------------------------------------------------------------------
9   !!   timming_init    : initialize timing process
10   !!   timing_start    : start Timer
11   !!   timing_stop     : stop  Timer
12   !!   timing_reset    : end timing variable creation
13   !!   timing_finalize : compute stats and write output in calling w*_info
14   !!   timing_ini_var  : create timing variables
15   !!   timing_listing  : print instumented subroutines in ocean.output
16   !!   wcurrent_info   : compute and print detailed stats on the current CPU
17   !!   wave_info       : compute and print averaged statson all processors
18   !!   wmpi_info       : compute and write global stats 
19   !!   supress         : suppress an element of the timing linked list 
20   !!   insert          : insert an element of the timing linked list 
21   !!------------------------------------------------------------------------
22   USE in_out_manager  ! I/O manager
23   USE dom_oce         ! ocean domain
24   USE lib_mpp         
25   
26   IMPLICIT NONE
27   PRIVATE
28
29   PUBLIC   timing_init, timing_finalize   ! called in nemogcm module
30   PUBLIC   timing_reset                   ! called in step module
31   PUBLIC   timing_start, timing_stop      ! called in each routine to time
32   
33#if defined key_mpp_mpi
34   INCLUDE 'mpif.h'
35#endif
36
37   ! Variables for fine grain timing
38   TYPE timer
39      CHARACTER(LEN=20)  :: cname
40      CHARACTER(LEN=20)  :: surname
41      INTEGER :: rank
42      REAL(wp)  :: t_cpu, t_clock, tsum_cpu, tsum_clock, tmax_cpu, tmax_clock, tmin_cpu, tmin_clock, tsub_cpu, tsub_clock
43      INTEGER :: ncount, ncount_max, ncount_rate 
44      INTEGER :: niter
45      LOGICAL :: l_tdone
46      TYPE(timer), POINTER :: next => NULL()
47      TYPE(timer), POINTER :: prev => NULL()
48      TYPE(timer), POINTER :: parent_section => NULL()
49   END TYPE timer
50   
51   TYPE alltimer
52      CHARACTER(LEN=20), DIMENSION(:), POINTER :: cname => NULL()
53      REAL(wp), DIMENSION(:), POINTER :: tsum_cpu   => NULL()
54      REAL(wp), DIMENSION(:), POINTER :: tsum_clock => NULL()
55      INTEGER, DIMENSION(:), POINTER :: niter => NULL()
56      TYPE(alltimer), POINTER :: next => NULL()
57      TYPE(alltimer), POINTER :: prev => NULL()
58   END TYPE alltimer 
59 
60   TYPE(timer), POINTER :: s_timer_root => NULL()
61   TYPE(timer), POINTER :: s_timer      => NULL()
62   TYPE(timer), POINTER :: s_timer_old      => NULL()
63
64   TYPE(timer), POINTER :: s_wrk        => NULL()
65   REAL(wp) :: t_overclock, t_overcpu
66   LOGICAL :: l_initdone = .FALSE.
67   INTEGER :: nsize
68   
69   ! Variables for coarse grain timing
70   REAL(wp) :: tot_etime, tot_ctime
71   REAL(kind=wp), DIMENSION(2)     :: t_elaps, t_cpu
72   REAL(wp), ALLOCATABLE, DIMENSION(:) :: all_etime, all_ctime
73   INTEGER :: nfinal_count, ncount, ncount_rate, ncount_max
74   INTEGER, DIMENSION(8)           :: nvalues
75   CHARACTER(LEN=8), DIMENSION(2)  :: cdate
76   CHARACTER(LEN=10), DIMENSION(2) :: ctime
77   CHARACTER(LEN=5)                :: czone
78   
79   ! From of ouput file (1/proc or one global)   !RB to put in nammpp or namctl
80   LOGICAL :: ln_onefile = .TRUE. 
81   LOGICAL :: lwriter
82   !!----------------------------------------------------------------------
83   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
84   !! $Id$
85   !! Software governed by the CeCILL license (see ./LICENSE)
86   !!----------------------------------------------------------------------
87CONTAINS
88
89   SUBROUTINE timing_start(cdinfo)
90      !!----------------------------------------------------------------------
91      !!               ***  ROUTINE timing_start  ***
92      !! ** Purpose :   collect execution time
93      !!----------------------------------------------------------------------
94      CHARACTER(len=*), INTENT(in) :: cdinfo
95      !
96       IF(ASSOCIATED(s_timer) ) s_timer_old => s_timer
97       !
98      ! Create timing structure at first call of the routine
99       CALL timing_ini_var(cdinfo)
100   !   write(*,*) 'after inivar ', s_timer%cname
101
102      ! ici timing_ini_var a soit retrouve s_timer et fait return soit ajoute un maillon
103      ! maintenant on regarde si le call d'avant corrsspond a un parent ou si il est ferme
104      IF( .NOT. s_timer_old%l_tdone ) THEN     
105         s_timer%parent_section => s_timer_old
106      ELSE
107         s_timer%parent_section => NULL()
108      ENDIF   
109
110      s_timer%l_tdone = .FALSE.
111      s_timer%niter = s_timer%niter + 1
112      s_timer%t_cpu = 0.
113      s_timer%t_clock = 0.
114                 
115      ! CPU time collection
116      CALL CPU_TIME( s_timer%t_cpu  )
117      ! clock time collection
118#if defined key_mpp_mpi
119      s_timer%t_clock= MPI_Wtime()
120#else
121      CALL SYSTEM_CLOCK(COUNT_RATE=s_timer%ncount_rate, COUNT_MAX=s_timer%ncount_max)
122      CALL SYSTEM_CLOCK(COUNT = s_timer%ncount)
123#endif
124!      write(*,*) 'end of start ', s_timer%cname
125
126      !
127   END SUBROUTINE timing_start
128
129
130   SUBROUTINE timing_stop(cdinfo, csection)
131      !!----------------------------------------------------------------------
132      !!               ***  ROUTINE timing_stop  ***
133      !! ** Purpose :   finalize timing and output
134      !!----------------------------------------------------------------------
135      CHARACTER(len=*), INTENT(in) :: cdinfo
136      CHARACTER(len=*), INTENT(in), OPTIONAL :: csection
137      !
138      INTEGER  :: ifinal_count, iperiods   
139      REAL(wp) :: zcpu_end, zmpitime,zcpu_raw,zclock_raw
140      !
141      s_wrk => NULL()
142
143      ! clock time collection
144#if defined key_mpp_mpi
145      zmpitime = MPI_Wtime()
146#else
147      CALL SYSTEM_CLOCK(COUNT = ifinal_count)
148#endif
149      ! CPU time collection
150      CALL CPU_TIME( zcpu_end )
151
152!!$      IF(associated(s_timer%parent_section))then
153!!$        write(*,*) s_timer%cname,' <-- ', s_timer%parent_section%cname
154!!$      ENDIF 
155
156 !     No need to search ... : s_timer has the last value defined in start
157 !     s_timer => s_timer_root
158 !     DO WHILE( TRIM(s_timer%cname) /= TRIM(cdinfo) )
159 !        IF( ASSOCIATED(s_timer%next) ) s_timer => s_timer%next
160 !     END DO
161 
162      ! CPU time correction
163      zcpu_raw = zcpu_end - s_timer%t_cpu - t_overcpu ! total time including child
164      s_timer%t_cpu  = zcpu_raw - s_timer%tsub_cpu
165  !    IF(s_timer%cname==trim('lbc_lnk_2d'))  write(*,*) s_timer%tsub_cpu,zcpu_end
166
167      ! clock time correction
168#if defined key_mpp_mpi
169      zclock_raw = zmpitime - s_timer%t_clock - t_overclock ! total time including child
170      s_timer%t_clock = zclock_raw - t_overclock - s_timer%tsub_clock
171#else
172      iperiods = ifinal_count - s_timer%ncount
173      IF( ifinal_count < s_timer%ncount )  &
174         iperiods = iperiods + s_timer%ncount_max 
175         zclock_raw = REAL(iperiods) / s_timer%ncount_rate !- t_overclock   
176         s_timer%t_clock  = zclock_raw - s_timer%tsub_clock
177#endif
178 !     IF(s_timer%cname==trim('lbc_lnk_2d')) write(*,*) zclock_raw , s_timer%tsub_clock
179     
180      ! Correction of parent section
181      IF( .NOT. PRESENT(csection) ) THEN
182         IF ( ASSOCIATED(s_timer%parent_section ) ) THEN
183            s_timer%parent_section%tsub_cpu   = zcpu_raw   + s_timer%parent_section%tsub_cpu 
184            s_timer%parent_section%tsub_clock = zclock_raw + s_timer%parent_section%tsub_clock             
185         ENDIF
186      ENDIF
187           
188      ! time diagnostics
189      s_timer%tsum_clock = s_timer%tsum_clock + s_timer%t_clock 
190      s_timer%tsum_cpu   = s_timer%tsum_cpu   + s_timer%t_cpu
191!RB to use to get min/max during a time integration
192!      IF( .NOT. l_initdone ) THEN
193!         s_timer%tmin_clock = s_timer%t_clock
194!         s_timer%tmin_cpu   = s_timer%t_cpu
195!      ELSE
196!         s_timer%tmin_clock = MIN( s_timer%tmin_clock, s_timer%t_clock )
197!         s_timer%tmin_cpu   = MIN( s_timer%tmin_cpu  , s_timer%t_cpu   )
198!      ENDIF   
199!      s_timer%tmax_clock = MAX( s_timer%tmax_clock, s_timer%t_clock )
200!      s_timer%tmax_cpu   = MAX( s_timer%tmax_cpu  , s_timer%t_cpu   ) 
201      !
202      s_timer%tsub_clock = 0.
203      s_timer%tsub_cpu = 0.
204      s_timer%l_tdone = .TRUE.
205      !
206      !
207      ! we come back
208      IF ( ASSOCIATED(s_timer%parent_section ) ) s_timer => s_timer%parent_section
209     
210!      write(*,*) 'end of stop ', s_timer%cname
211
212   END SUBROUTINE timing_stop
213 
214 
215   SUBROUTINE timing_init( clname )
216      !!----------------------------------------------------------------------
217      !!               ***  ROUTINE timing_init  ***
218      !! ** Purpose :   open timing output file
219      !!----------------------------------------------------------------------
220      INTEGER :: iperiods, istart_count, ifinal_count
221      REAL(wp) :: zdum
222      LOGICAL :: ll_f
223      CHARACTER(len=*), INTENT(in), OPTIONAL :: clname
224      CHARACTER(len=20)                      :: cln
225
226      IF( PRESENT(clname) ) THEN   ;   cln = clname
227      ELSE                         ;   cln = 'timing.output'
228      ENDIF
229
230      IF( ln_onefile ) THEN
231         IF( lwp) CALL ctl_opn( numtime, cln, 'REPLACE', 'FORMATTED', 'SEQUENTIAL', -1, numout,.TRUE., narea )
232         lwriter = lwp
233      ELSE
234         CALL ctl_opn( numtime, cln, 'REPLACE', 'FORMATTED', 'SEQUENTIAL', -1, numout,.FALSE., narea )
235         lwriter = .TRUE.
236      ENDIF
237     
238      IF( lwriter) THEN     
239         WRITE(numtime,*)
240         WRITE(numtime,*) '      CNRS - NERC - Met OFFICE - MERCATOR-ocean - CMCC - INGV'
241         WRITE(numtime,*) '                             NEMO team'
242         WRITE(numtime,*) '                  Ocean General Circulation Model'
243         WRITE(numtime,*) '                        version 4.0  (2019) '
244         WRITE(numtime,*)
245         WRITE(numtime,*) '                        Timing Informations '
246         WRITE(numtime,*)
247         WRITE(numtime,*)
248      ENDIF   
249     
250      ! Compute clock function overhead
251#if defined key_mpp_mpi       
252      t_overclock = MPI_WTIME()
253      t_overclock = MPI_WTIME() - t_overclock
254#else       
255      CALL SYSTEM_CLOCK(COUNT_RATE=ncount_rate, COUNT_MAX=ncount_max)
256      CALL SYSTEM_CLOCK(COUNT = istart_count)
257      CALL SYSTEM_CLOCK(COUNT = ifinal_count)
258      iperiods = ifinal_count - istart_count
259      IF( ifinal_count < istart_count )  &
260          iperiods = iperiods + ncount_max 
261      t_overclock = REAL(iperiods) / ncount_rate
262#endif
263
264      ! Compute cpu_time function overhead
265      CALL CPU_TIME(zdum)
266      CALL CPU_TIME(t_overcpu)
267     
268      ! End overhead omputation 
269      t_overcpu = t_overcpu - zdum       
270      t_overclock = t_overcpu + t_overclock       
271
272      ! Timing on date and time
273      CALL DATE_AND_TIME(cdate(1),ctime(1),czone,nvalues)
274   
275      CALL CPU_TIME(t_cpu(1))     
276#if defined key_mpp_mpi       
277      ! Start elapsed and CPU time counters
278      t_elaps(1) = MPI_WTIME()
279#else
280      CALL SYSTEM_CLOCK(COUNT_RATE=ncount_rate, COUNT_MAX=ncount_max)
281      CALL SYSTEM_CLOCK(COUNT = ncount)
282#endif                 
283      !
284   END SUBROUTINE timing_init
285
286
287   SUBROUTINE timing_finalize
288      !!----------------------------------------------------------------------
289      !!               ***  ROUTINE timing_finalize ***
290      !! ** Purpose :  compute average time
291      !!               write timing output file
292      !!----------------------------------------------------------------------
293      TYPE(timer), POINTER :: s_temp
294      INTEGER :: idum, iperiods, icode
295      INTEGER :: ji
296      LOGICAL :: ll_ord, ll_averep
297      CHARACTER(len=120) :: clfmt           
298      REAL(wp), DIMENSION(:), ALLOCATABLE ::   timing_glob
299      REAL(wp) ::   zsypd   ! simulated years per day (Balaji 2017)
300      REAL(wp) ::   zperc, ztot
301
302      ll_averep = .TRUE.
303   
304      ! total CPU and elapse
305      CALL CPU_TIME(t_cpu(2))
306      t_cpu(2)   = t_cpu(2)    - t_cpu(1)   - t_overcpu
307#if defined key_mpp_mpi
308      t_elaps(2) = MPI_WTIME() - t_elaps(1) - t_overclock
309#else
310      CALL SYSTEM_CLOCK(COUNT = nfinal_count)
311      iperiods = nfinal_count - ncount
312      IF( nfinal_count < ncount )  &
313          iperiods = iperiods + ncount_max 
314      t_elaps(2) = REAL(iperiods) / ncount_rate - t_overclock
315#endif     
316
317      ! End of timings on date & time
318      CALL DATE_AND_TIME(cdate(2),ctime(2),czone,nvalues)
319       
320      ! Compute the numer of routines
321      nsize = 0 
322      s_timer => s_timer_root
323      DO WHILE( ASSOCIATED(s_timer) )
324         nsize = nsize + 1
325         s_timer => s_timer%next
326      END DO
327      idum = nsize
328      CALL mpp_sum('timing', idum)
329      IF( idum/jpnij /= nsize ) THEN
330         IF( lwriter ) WRITE(numtime,*) '        ===> W A R N I N G: '
331         IF( lwriter ) WRITE(numtime,*) ' Some CPU have different number of routines instrumented for timing'
332         IF( lwriter ) WRITE(numtime,*) ' No detailed report on averaged timing can be provided'
333         IF( lwriter ) WRITE(numtime,*) ' The following detailed report only deals with the current processor'
334         IF( lwriter ) WRITE(numtime,*)
335         ll_averep = .FALSE.
336      ENDIF   
337
338#if defined key_mpp_mpi     
339      ! in MPI gather some info
340      ALLOCATE( all_etime(jpnij), all_ctime(jpnij) )
341      CALL MPI_ALLGATHER(t_elaps(2), 1, MPI_DOUBLE_PRECISION,   &
342                         all_etime , 1, MPI_DOUBLE_PRECISION,   &
343                         MPI_COMM_OCE, icode)
344      CALL MPI_ALLGATHER(t_cpu(2) , 1, MPI_DOUBLE_PRECISION,   &
345                         all_ctime, 1, MPI_DOUBLE_PRECISION,   &
346                         MPI_COMM_OCE, icode)
347      tot_etime = SUM(all_etime(:))
348      tot_ctime = SUM(all_ctime(:))
349#else
350      tot_etime = t_elaps(2)
351      tot_ctime = t_cpu  (2)           
352#endif
353
354      ! write output file
355      IF( lwriter ) WRITE(numtime,*) 
356      IF( lwriter ) WRITE(numtime,*) 
357      IF( lwriter ) WRITE(numtime,*) 'Total timing (sum) :'
358      IF( lwriter ) WRITE(numtime,*) '--------------------'
359      IF( lwriter ) WRITE(numtime,"('Elapsed Time (s)  CPU Time (s)')")
360      IF( lwriter ) WRITE(numtime,'(5x,f12.3,1x,f12.3)')  tot_etime, tot_ctime
361      IF( lwriter ) WRITE(numtime,*) 
362#if defined key_mpp_mpi
363      IF( ll_averep ) CALL waver_info
364      CALL wmpi_info
365#endif     
366      IF( lwriter ) CALL wcurrent_info
367     
368      clfmt='(1X,"Timing started on ",2(A2,"/"),A4," at ",2(A2,":"),A2," MET ",A3,":",A2," from GMT")'
369      IF( lwriter ) WRITE(numtime, TRIM(clfmt)) &           
370      &       cdate(1)(7:8), cdate(1)(5:6), cdate(1)(1:4),   &
371      &       ctime(1)(1:2), ctime(1)(3:4), ctime(1)(5:6),   &
372      &       czone(1:3),    czone(4:5)                     
373      clfmt='(1X,  "Timing   ended on ",2(A2,"/"),A4," at ",2(A2,":"),A2," MET ",A3,":",A2," from GMT")'
374      IF( lwriter ) WRITE(numtime, TRIM(clfmt)) &           
375      &       cdate(2)(7:8), cdate(2)(5:6), cdate(2)(1:4),   &
376      &       ctime(2)(1:2), ctime(2)(3:4), ctime(2)(5:6),   &
377      &       czone(1:3),    czone(4:5)
378
379#if defined key_mpp_mpi
380      ALLOCATE(timing_glob(4*jpnij), stat=icode)
381      CALL MPI_GATHER( (/compute_time, waiting_time(1), waiting_time(2), elapsed_time/),   &
382         &             4, MPI_DOUBLE_PRECISION, timing_glob, 4, MPI_DOUBLE_PRECISION, 0, MPI_COMM_OCE, icode)
383      IF( narea == 1 ) THEN
384         WRITE(numtime,*) ' '
385         WRITE(numtime,*) ' Report on time spent on waiting MPI messages '
386         WRITE(numtime,*) '    total timing measured between nit000+1 and nitend-1 '
387         WRITE(numtime,*) '    warning: includes restarts writing time if output before nitend... '
388         WRITE(numtime,*) ' '
389         DO ji = 1, jpnij
390            ztot = SUM( timing_glob(4*ji-3:4*ji-1) )
391            WRITE(numtime,'(A28,F11.6,            A34,I8)') 'Computing       time : ',timing_glob(4*ji-3), ' on MPI rank : ', ji
392            IF ( ztot /= 0. ) zperc = timing_glob(4*ji-2) / ztot * 100.
393            WRITE(numtime,'(A28,F11.6,A2, F4.1,A3,A25,I8)') 'Waiting lbc_lnk time : ',timing_glob(4*ji-2)   &
394               &                                                         , ' (',      zperc,' %)',   ' on MPI rank : ', ji
395            IF ( ztot /= 0. ) zperc = timing_glob(4*ji-1) / ztot * 100.
396            WRITE(numtime,'(A28,F11.6,A2, F4.1,A3,A25,I8)') 'Waiting  global time : ',timing_glob(4*ji-1)   &
397               &                                                         , ' (',      zperc,' %)',   ' on MPI rank : ', ji
398            zsypd = rn_Dt * REAL(nitend-nit000-1, wp) / (timing_glob(4*ji) * 365.)
399            WRITE(numtime,'(A28,F11.6,A7,F10.3,A2,A15,I8)') 'Total           time : ',timing_glob(4*ji  )   &
400               &                                                         , ' (SYPD: ', zsypd, ')',   ' on MPI rank : ', ji
401         END DO
402      ENDIF
403      DEALLOCATE(timing_glob)
404#endif     
405
406      IF( lwriter ) CLOSE(numtime) 
407      !
408   END SUBROUTINE timing_finalize
409   
410
411   SUBROUTINE wcurrent_info
412      !!----------------------------------------------------------------------
413      !!               ***  ROUTINE wcurrent_info ***
414      !! ** Purpose :  compute and write timing output file
415      !!----------------------------------------------------------------------
416      LOGICAL :: ll_ord
417      CHARACTER(len=2048) :: clfmt           
418   
419      ! reorder the current list by elapse time     
420      s_wrk => NULL()
421      s_timer => s_timer_root
422      DO
423         ll_ord = .TRUE.
424         s_timer => s_timer_root
425         DO WHILE ( ASSOCIATED( s_timer%next ) )
426         IF (.NOT. ASSOCIATED(s_timer%next)) EXIT
427            IF ( s_timer%tsum_clock < s_timer%next%tsum_clock ) THEN
428               ALLOCATE(s_wrk)
429               s_wrk = s_timer%next
430               CALL insert  (s_timer, s_timer_root, s_wrk)
431               CALL suppress(s_timer%next)           
432               ll_ord = .FALSE.
433               CYCLE           
434            ENDIF           
435         IF( ASSOCIATED(s_timer%next) ) s_timer => s_timer%next
436         END DO         
437         IF( ll_ord ) EXIT
438      END DO
439           
440      ! write current info
441      WRITE(numtime,*) 'Detailed timing for proc :', narea-1
442      WRITE(numtime,*) '--------------------------'
443      WRITE(numtime,*) 'Section             ',            &
444      &   'Elapsed Time (s)  ','Elapsed Time (%)  ',   &
445      &   'CPU Time(s)  ','CPU Time (%)  ','CPU/Elapsed  ','Frequency' 
446      s_timer => s_timer_root 
447      clfmt = '(1x,a,4x,f12.3,6x,f12.3,x,f12.3,2x,f12.3,6x,f7.3,2x,i9)'
448      DO WHILE ( ASSOCIATED(s_timer) )
449         WRITE(numtime,TRIM(clfmt))   s_timer%cname,   &
450         &   s_timer%tsum_clock,s_timer%tsum_clock*100./t_elaps(2),            &
451         &   s_timer%tsum_cpu  ,s_timer%tsum_cpu*100./t_cpu(2)    ,            &
452         &   s_timer%tsum_cpu/s_timer%tsum_clock, s_timer%niter
453         s_timer => s_timer%next
454      END DO
455      WRITE(numtime,*)
456      !                 
457   END SUBROUTINE wcurrent_info
458
459#if defined key_mpp_mpi     
460   SUBROUTINE waver_info
461      !!----------------------------------------------------------------------
462      !!               ***  ROUTINE wcurrent_info ***
463      !! ** Purpose :  compute and write averaged timing informations
464      !!----------------------------------------------------------------------
465      TYPE(alltimer), POINTER :: sl_timer_glob_root => NULL()
466      TYPE(alltimer), POINTER :: sl_timer_glob      => NULL()
467      TYPE(timer), POINTER :: sl_timer_ave_root => NULL()
468      TYPE(timer), POINTER :: sl_timer_ave      => NULL()
469      INTEGER :: icode
470      INTEGER :: ierr
471      LOGICAL :: ll_ord           
472      CHARACTER(len=200) :: clfmt             
473                 
474      ! Initialised the global strucutre   
475      ALLOCATE(sl_timer_glob_root, Stat=ierr)
476      IF(ierr /= 0)THEN
477         WRITE(numtime,*) 'Failed to allocate global timing structure in waver_info'
478         RETURN
479      END IF
480
481      ALLOCATE(sl_timer_glob_root%cname     (jpnij), &
482               sl_timer_glob_root%tsum_cpu  (jpnij), &
483               sl_timer_glob_root%tsum_clock(jpnij), &
484               sl_timer_glob_root%niter     (jpnij), Stat=ierr)
485      IF(ierr /= 0)THEN
486         WRITE(numtime,*) 'Failed to allocate global timing structure in waver_info'
487         RETURN
488      END IF
489      sl_timer_glob_root%cname(:)       = ''
490      sl_timer_glob_root%tsum_cpu(:)   = 0._wp
491      sl_timer_glob_root%tsum_clock(:) = 0._wp
492      sl_timer_glob_root%niter(:)      = 0
493      sl_timer_glob_root%next => NULL()
494      sl_timer_glob_root%prev => NULL()
495      !ARPDBG - don't need to allocate a pointer that's immediately then
496      !         set to point to some other object.
497      !ALLOCATE(sl_timer_glob)
498      !ALLOCATE(sl_timer_glob%cname     (jpnij))
499      !ALLOCATE(sl_timer_glob%tsum_cpu  (jpnij))
500      !ALLOCATE(sl_timer_glob%tsum_clock(jpnij))
501      !ALLOCATE(sl_timer_glob%niter     (jpnij))
502      sl_timer_glob => sl_timer_glob_root
503      !
504      IF( narea .EQ. 1 ) THEN
505         ALLOCATE(sl_timer_ave_root)
506         sl_timer_ave_root%cname       = ''
507         sl_timer_ave_root%t_cpu      = 0._wp
508         sl_timer_ave_root%t_clock    = 0._wp
509         sl_timer_ave_root%tsum_cpu   = 0._wp
510         sl_timer_ave_root%tsum_clock = 0._wp
511         sl_timer_ave_root%tmax_cpu   = 0._wp
512         sl_timer_ave_root%tmax_clock = 0._wp
513         sl_timer_ave_root%tmin_cpu   = 0._wp
514         sl_timer_ave_root%tmin_clock = 0._wp
515         sl_timer_ave_root%tsub_cpu   = 0._wp
516         sl_timer_ave_root%tsub_clock = 0._wp
517         sl_timer_ave_root%ncount      = 0
518         sl_timer_ave_root%ncount_rate = 0
519         sl_timer_ave_root%ncount_max  = 0
520         sl_timer_ave_root%niter       = 0
521         sl_timer_ave_root%l_tdone  = .FALSE.
522         sl_timer_ave_root%next => NULL()
523         sl_timer_ave_root%prev => NULL()
524         ALLOCATE(sl_timer_ave)
525         sl_timer_ave => sl_timer_ave_root           
526      ENDIF 
527
528      ! Gather info from all processors
529      s_timer => s_timer_root
530      DO WHILE ( ASSOCIATED(s_timer) )
531         CALL MPI_GATHER(s_timer%cname     , 20, MPI_CHARACTER,   &
532                         sl_timer_glob%cname, 20, MPI_CHARACTER,   &
533                         0, MPI_COMM_OCE, icode)
534         CALL MPI_GATHER(s_timer%tsum_clock     , 1, MPI_DOUBLE_PRECISION,   &
535                         sl_timer_glob%tsum_clock, 1, MPI_DOUBLE_PRECISION,   &
536                         0, MPI_COMM_OCE, icode)
537         CALL MPI_GATHER(s_timer%tsum_cpu     , 1, MPI_DOUBLE_PRECISION,   &
538                         sl_timer_glob%tsum_cpu, 1, MPI_DOUBLE_PRECISION,   &
539                         0, MPI_COMM_OCE, icode)
540         CALL MPI_GATHER(s_timer%niter     , 1, MPI_INTEGER,   &
541                         sl_timer_glob%niter, 1, MPI_INTEGER,   &
542                         0, MPI_COMM_OCE, icode)
543
544         IF( narea == 1 .AND. ASSOCIATED(s_timer%next) ) THEN
545            ALLOCATE(sl_timer_glob%next)
546            ALLOCATE(sl_timer_glob%next%cname     (jpnij))
547            ALLOCATE(sl_timer_glob%next%tsum_cpu  (jpnij))
548            ALLOCATE(sl_timer_glob%next%tsum_clock(jpnij))
549            ALLOCATE(sl_timer_glob%next%niter     (jpnij))
550            sl_timer_glob%next%prev => sl_timer_glob
551            sl_timer_glob%next%next => NULL()
552            sl_timer_glob           => sl_timer_glob%next
553         ENDIF             
554         s_timer => s_timer%next
555      END DO     
556     
557      IF( narea == 1 ) THEN   
558         ! Compute some stats
559         sl_timer_glob => sl_timer_glob_root
560         DO WHILE( ASSOCIATED(sl_timer_glob) )
561            sl_timer_ave%cname  = sl_timer_glob%cname(1)
562            sl_timer_ave%tsum_cpu   = SUM   (sl_timer_glob%tsum_cpu  (:)) / jpnij
563            sl_timer_ave%tsum_clock = SUM   (sl_timer_glob%tsum_clock(:)) / jpnij
564            sl_timer_ave%tmax_cpu   = MAXVAL(sl_timer_glob%tsum_cpu  (:))
565            sl_timer_ave%tmax_clock = MAXVAL(sl_timer_glob%tsum_clock(:))
566            sl_timer_ave%tmin_cpu   = MINVAL(sl_timer_glob%tsum_cpu  (:))
567            sl_timer_ave%tmin_clock = MINVAL(sl_timer_glob%tsum_clock(:))
568            sl_timer_ave%niter      = SUM   (sl_timer_glob%niter     (:))
569            !
570            IF( ASSOCIATED(sl_timer_glob%next) ) THEN
571               ALLOCATE(sl_timer_ave%next)         
572               sl_timer_ave%next%prev => sl_timer_ave
573               sl_timer_ave%next%next => NULL()           
574               sl_timer_ave           => sl_timer_ave%next
575            ENDIF
576            sl_timer_glob => sl_timer_glob%next                               
577         END DO
578     
579         ! reorder the averaged list by CPU time     
580         s_wrk => NULL()
581         sl_timer_ave => sl_timer_ave_root
582         DO
583            ll_ord = .TRUE.
584            sl_timer_ave => sl_timer_ave_root
585            DO WHILE( ASSOCIATED( sl_timer_ave%next ) )
586
587               IF( .NOT. ASSOCIATED(sl_timer_ave%next) ) EXIT
588
589               IF ( sl_timer_ave%tsum_clock < sl_timer_ave%next%tsum_clock ) THEN
590                  ALLOCATE(s_wrk)
591                  ! Copy data into the new object pointed to by s_wrk
592                  s_wrk = sl_timer_ave%next
593                  ! Insert this new timer object before our current position
594                  CALL insert  (sl_timer_ave, sl_timer_ave_root, s_wrk)
595                  ! Remove the old object from the list
596                  CALL suppress(sl_timer_ave%next)           
597                  ll_ord = .FALSE.
598                  CYCLE           
599               ENDIF           
600               IF( ASSOCIATED(sl_timer_ave%next) ) sl_timer_ave => sl_timer_ave%next
601            END DO         
602            IF( ll_ord ) EXIT
603         END DO
604
605         ! write averaged info
606         WRITE(numtime,"('Averaged timing on all processors :')")
607         WRITE(numtime,"('-----------------------------------')")
608         WRITE(numtime,"('Section',13x,'Elap. Time(s)',2x,'Elap. Time(%)',2x, &
609         &   'CPU Time(s)',2x,'CPU Time(%)',2x,'CPU/Elap',1x,   &
610         &   'Max elap(%)',2x,'Min elap(%)',2x,            &           
611         &   'Freq')")
612         sl_timer_ave => sl_timer_ave_root 
613         clfmt = '((A),E15.7,2x,f6.2,5x,f12.2,5x,f6.2,5x,f7.2,2x,f12.2,4x,f6.2,2x,f9.2)'
614         DO WHILE ( ASSOCIATED(sl_timer_ave) )
615            WRITE(numtime,TRIM(clfmt))   sl_timer_ave%cname(1:18),                            &
616            &   sl_timer_ave%tsum_clock,sl_timer_ave%tsum_clock*100.*jpnij/tot_etime,   &
617            &   sl_timer_ave%tsum_cpu  ,sl_timer_ave%tsum_cpu*100.*jpnij/tot_ctime  ,   &
618            &   sl_timer_ave%tsum_cpu/sl_timer_ave%tsum_clock,                          &
619            &   sl_timer_ave%tmax_clock*100.*jpnij/tot_etime,                           &
620            &   sl_timer_ave%tmin_clock*100.*jpnij/tot_etime,                           &                                               
621            &   sl_timer_ave%niter/REAL(jpnij)
622            sl_timer_ave => sl_timer_ave%next
623         END DO
624         WRITE(numtime,*)
625         !
626         DEALLOCATE(sl_timer_ave_root)
627      ENDIF
628      !
629      DEALLOCATE(sl_timer_glob_root)
630      !                 
631   END SUBROUTINE waver_info
632 
633 
634   SUBROUTINE wmpi_info
635      !!----------------------------------------------------------------------
636      !!               ***  ROUTINE wmpi_time  ***
637      !! ** Purpose :   compute and write a summary of MPI infos
638      !!----------------------------------------------------------------------   
639      !   
640      INTEGER                            :: idum, icode
641      INTEGER, ALLOCATABLE, DIMENSION(:) :: iall_rank
642      REAL(wp) :: ztot_ratio
643      REAL(wp) :: zmax_etime, zmax_ctime, zmax_ratio, zmin_etime, zmin_ctime, zmin_ratio
644      REAL(wp) :: zavg_etime, zavg_ctime, zavg_ratio
645      REAL(wp), ALLOCATABLE, DIMENSION(:) :: zall_ratio
646      CHARACTER(LEN=128), dimension(8) :: cllignes
647      CHARACTER(LEN=128)               :: clhline, clstart_date, clfinal_date
648      CHARACTER(LEN=2048)              :: clfmt   
649   
650      ! Gather all times
651      ALLOCATE( zall_ratio(jpnij), iall_rank(jpnij) )
652      IF( narea == 1 ) THEN
653         iall_rank(:) = (/ (idum,idum=0,jpnij-1) /)
654   
655         ! Compute elapse user time
656         zavg_etime = tot_etime/REAL(jpnij,wp)
657         zmax_etime = MAXVAL(all_etime(:))
658         zmin_etime = MINVAL(all_etime(:))
659
660         ! Compute CPU user time
661         zavg_ctime = tot_ctime/REAL(jpnij,wp)
662         zmax_ctime = MAXVAL(all_ctime(:))
663         zmin_ctime = MINVAL(all_ctime(:))
664   
665         ! Compute cpu/elapsed ratio
666         zall_ratio(:) = all_ctime(:) / all_etime(:)
667         ztot_ratio    = SUM(all_ctime(:))/SUM(all_etime(:))
668         zavg_ratio    = SUM(zall_ratio(:))/REAL(jpnij,wp)
669         zmax_ratio    = MAXVAL(zall_ratio(:))
670         zmin_ratio    = MINVAL(zall_ratio(:))   
671   
672         ! Output Format
673         clhline    ='1x,13("-"),"|",18("-"),"|",14("-"),"|",18("-"),/,'
674         cllignes(1)='(1x,"MPI summary report :",/,'
675         cllignes(2)='1x,"--------------------",//,'
676         cllignes(3)='1x,"Process Rank |"," Elapsed Time (s) |"," CPU Time (s) |"," Ratio CPU/Elapsed",/,'
677         cllignes(4)='      (4x,i6,4x,"|",f12.3,6x,"|",f12.3,2x,"|",4x,f7.3,/),'
678         WRITE(cllignes(4)(1:6),'(I6)') jpnij
679         cllignes(5)='1x,"Total        |",f12.3,6x,"|",F12.3,2x,"|",4x,f7.3,/,'
680         cllignes(6)='1x,"Minimum      |",f12.3,6x,"|",F12.3,2x,"|",4x,f7.3,/,'
681         cllignes(7)='1x,"Maximum      |",f12.3,6x,"|",F12.3,2x,"|",4x,f7.3,/,'
682         cllignes(8)='1x,"Average      |",f12.3,6x,"|",F12.3,2x,"|",4x,f7.3)'
683         clfmt=TRIM(cllignes(1))// TRIM(cllignes(2))//TRIM(cllignes(3))//          &
684           & TRIM(clhline)//TRIM(cllignes(4))//TRIM(clhline)//TRIM(cllignes(5))//  &
685           & TRIM(clhline)//TRIM(cllignes(6))//TRIM(clhline)//TRIM(cllignes(7))//  &
686           & TRIM(clhline)//TRIM(cllignes(8))
687         WRITE(numtime, TRIM(clfmt)) &
688             (iall_rank(idum),all_etime(idum),all_ctime(idum),zall_ratio(idum),idum=1, jpnij), &
689             tot_etime,     tot_ctime,     ztot_ratio,   &
690             zmin_etime,    zmin_ctime,    zmin_ratio,   &
691             zmax_etime,    zmax_ctime,    zmax_ratio,   &
692             zavg_etime,    zavg_ctime,    zavg_ratio
693         WRITE(numtime,*)   
694      END IF
695      !
696      DEALLOCATE(zall_ratio, iall_rank)
697      !
698   END SUBROUTINE wmpi_info
699#endif   
700
701
702   SUBROUTINE timing_ini_var(cdinfo)
703      !!----------------------------------------------------------------------
704      !!               ***  ROUTINE timing_ini_var  ***
705      !! ** Purpose :   create timing structure
706      !!----------------------------------------------------------------------
707      CHARACTER(len=*), INTENT(in) :: cdinfo
708      LOGICAL :: ll_section
709       
710      !
711      IF( .NOT. ASSOCIATED(s_timer_root) ) THEN
712         ALLOCATE(s_timer_root)
713         s_timer_root%cname       = cdinfo
714         s_timer_root%t_cpu      = 0._wp
715         s_timer_root%t_clock    = 0._wp
716         s_timer_root%tsum_cpu   = 0._wp
717         s_timer_root%tsum_clock = 0._wp
718         s_timer_root%tmax_cpu   = 0._wp
719         s_timer_root%tmax_clock = 0._wp
720         s_timer_root%tmin_cpu   = 0._wp
721         s_timer_root%tmin_clock = 0._wp
722         s_timer_root%tsub_cpu   = 0._wp
723         s_timer_root%tsub_clock = 0._wp
724         s_timer_root%ncount      = 0
725         s_timer_root%ncount_rate = 0
726         s_timer_root%ncount_max  = 0
727         s_timer_root%niter       = 0
728         s_timer_root%l_tdone  = .FALSE.
729         s_timer_root%next => NULL()
730         s_timer_root%prev => NULL()
731         s_timer => s_timer_root
732         !
733         ALLOCATE(s_wrk)
734         s_wrk => NULL()
735         !
736         ALLOCATE(s_timer_old)
737         s_timer_old%cname       = cdinfo
738         s_timer_old%t_cpu      = 0._wp
739         s_timer_old%t_clock    = 0._wp
740         s_timer_old%tsum_cpu   = 0._wp
741         s_timer_old%tsum_clock = 0._wp
742         s_timer_old%tmax_cpu   = 0._wp
743         s_timer_old%tmax_clock = 0._wp
744         s_timer_old%tmin_cpu   = 0._wp
745         s_timer_old%tmin_clock = 0._wp
746         s_timer_old%tsub_cpu   = 0._wp
747         s_timer_old%tsub_clock = 0._wp
748         s_timer_old%ncount      = 0
749         s_timer_old%ncount_rate = 0
750         s_timer_old%ncount_max  = 0
751         s_timer_old%niter       = 0
752         s_timer_old%l_tdone  = .TRUE.
753         s_timer_old%next => NULL()
754         s_timer_old%prev => NULL()
755
756      ELSE
757         s_timer => s_timer_root
758         ! case of already existing area (typically inside a loop)
759   !         write(*,*) 'in ini_var for routine : ', cdinfo
760         DO WHILE( ASSOCIATED(s_timer) ) 
761            IF( TRIM(s_timer%cname) .EQ. TRIM(cdinfo) ) THEN
762 !             write(*,*) 'in ini_var for routine : ', cdinfo,' we return'           
763               RETURN ! cdinfo is already in the chain
764            ENDIF
765            s_timer => s_timer%next
766         END DO
767
768         ! end of the chain
769         s_timer => s_timer_root
770         DO WHILE( ASSOCIATED(s_timer%next) )
771            s_timer => s_timer%next
772         END DO
773
774    !     write(*,*) 'after search', s_timer%cname
775         ! cdinfo is not part of the chain so we add it with initialisation         
776          ALLOCATE(s_timer%next)
777    !     write(*,*) 'after allocation of next'
778 
779         s_timer%next%cname       = cdinfo
780         s_timer%next%t_cpu      = 0._wp
781         s_timer%next%t_clock    = 0._wp
782         s_timer%next%tsum_cpu   = 0._wp
783         s_timer%next%tsum_clock = 0._wp 
784         s_timer%next%tmax_cpu   = 0._wp
785         s_timer%next%tmax_clock = 0._wp
786         s_timer%next%tmin_cpu   = 0._wp
787         s_timer%next%tmin_clock = 0._wp
788         s_timer%next%tsub_cpu   = 0._wp
789         s_timer%next%tsub_clock = 0._wp
790         s_timer%next%ncount      = 0
791         s_timer%next%ncount_rate = 0
792         s_timer%next%ncount_max  = 0
793         s_timer%next%niter       = 0
794         s_timer%next%l_tdone  = .FALSE.
795         s_timer%next%parent_section => NULL()
796         s_timer%next%prev => s_timer
797         s_timer%next%next => NULL()
798         s_timer => s_timer%next
799      ENDIF 
800      !    write(*,*) 'after allocation'
801     !
802   END SUBROUTINE timing_ini_var
803
804
805   SUBROUTINE timing_reset
806      !!----------------------------------------------------------------------
807      !!               ***  ROUTINE timing_reset  ***
808      !! ** Purpose :   go to root of timing tree
809      !!----------------------------------------------------------------------
810      l_initdone = .TRUE. 
811!      IF(lwp) WRITE(numout,*)
812!      IF(lwp) WRITE(numout,*) 'timing_reset : instrumented routines for timing'
813!      IF(lwp) WRITE(numout,*) '~~~~~~~~~~~~'
814      CALL timing_list(s_timer_root)
815!      WRITE(numout,*)
816      !
817   END SUBROUTINE timing_reset
818
819
820   RECURSIVE SUBROUTINE timing_list(ptr)
821   
822      TYPE(timer), POINTER, INTENT(inout) :: ptr
823      !
824      IF( ASSOCIATED(ptr%next) ) CALL timing_list(ptr%next)
825      IF(lwp) WRITE(numout,*)'   ', ptr%cname   
826      !
827   END SUBROUTINE timing_list
828
829
830   SUBROUTINE insert(sd_current, sd_root ,sd_ptr)
831      !!----------------------------------------------------------------------
832      !!               ***  ROUTINE insert  ***
833      !! ** Purpose :   insert an element in timer structure
834      !!----------------------------------------------------------------------
835      TYPE(timer), POINTER, INTENT(inout) :: sd_current, sd_root, sd_ptr
836      !
837     
838      IF( ASSOCIATED( sd_current, sd_root ) ) THEN
839         ! If our current element is the root element then
840         ! replace it with the one being inserted
841         sd_root => sd_ptr
842      ELSE
843         sd_current%prev%next => sd_ptr
844      END IF
845      sd_ptr%next     => sd_current
846      sd_ptr%prev     => sd_current%prev
847      sd_current%prev => sd_ptr
848      ! Nullify the pointer to the new element now that it is held
849      ! within the list. If we don't do this then a subsequent call
850      ! to ALLOCATE memory to this pointer will fail.
851      sd_ptr => NULL()
852      !   
853   END SUBROUTINE insert
854 
855 
856   SUBROUTINE suppress(sd_ptr)
857      !!----------------------------------------------------------------------
858      !!               ***  ROUTINE suppress  ***
859      !! ** Purpose :   supress an element in timer structure
860      !!----------------------------------------------------------------------
861      TYPE(timer), POINTER, INTENT(inout) :: sd_ptr
862      !
863      TYPE(timer), POINTER :: sl_temp
864   
865      sl_temp => sd_ptr
866      sd_ptr => sd_ptr%next   
867      IF ( ASSOCIATED(sl_temp%next) ) sl_temp%next%prev => sl_temp%prev
868      DEALLOCATE(sl_temp)
869      sl_temp => NULL()
870      !
871    END SUBROUTINE suppress
872
873   !!=====================================================================
874END MODULE timing
Note: See TracBrowser for help on using the repository browser.