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 branches/2011/DEV_r2739_STFC_dCSE/NEMOGCM/NEMO/OPA_SRC – NEMO

source: branches/2011/DEV_r2739_STFC_dCSE/NEMOGCM/NEMO/OPA_SRC/timing.F90 @ 3432

Last change on this file since 3432 was 3432, checked in by trackstand2, 12 years ago

Merge branch 'ksection_partition'

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