stop.F90 11.2 KB
Newer Older
1 2 3 4 5 6
!--------------------------------------------------------------------------------
! 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.
!--------------------------------------------------------------------------------

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
MODULE m_juDFT_stop
  !-----------------------------------------------
  !    module to terminate Calculation, should be used instead
  !    of a simple STOP
  !
  !    error(message,calledby,hint,no,warning)
  !         message  : message string
  !         calledby : subroutine in which error occurs(optional)
  !         hint     : string with more information (optional)
  !         no       : error number (optional)
  !         warning  : logical indicating a warning message (optional)
  !
  !    warn(message,calledby,hint,no)
  !         shortcut for calling error with warning=.true.
  !
  !    juDFT_end(message)
  !         call this to terminate without error
  !
  !   IF the file "JUDFT_WARN_ONLY" is not present, warnings will lead to errors.
  !
  !   If the file "JUDFT_TRACE" is present, a stacktrace will be generated
  !   on some compilers
  !
  !
  !                 Daniel Wortmann (2010)
  !-----------------------------------------------
  USE m_judft_time
  USE m_judft_sysinfo
35
  USE m_judft_args
36 37 38 39 40 41 42 43 44 45 46 47
  IMPLICIT NONE
  PRIVATE
  PUBLIC juDFT_error,juDFT_warn,juDFT_end,judft_file_readable
CONTAINS

  SUBROUTINE judfT_file_readable(filename,warning)
    IMPLICIT NONE
    CHARACTER(len=*),INTENT(IN):: filename
    LOGICAL,INTENT(IN),OPTIONAL:: warning
    LOGICAL  :: l_exist

    INQUIRE(file=filename,exist=l_exist)
48
    IF (.not.l_exist) CALL judft_error("File not readable:"//filename,hint="You tried to read a file that is not present",warning=warning)
49 50 51
  END SUBROUTINE judfT_file_readable

  SUBROUTINE juDFT_error(message,calledby,hint,no,warning,file,line)
52
    USE m_judft_usage
53
    USE m_judft_xmloutput
54 55 56 57 58 59 60 61
    IMPLICIT NONE
    CHARACTER*(*),INTENT(IN)          :: message
    CHARACTER*(*),OPTIONAL,INTENT(IN) :: calledby,hint
    INTEGER,OPTIONAL,INTENT(IN)       :: no
    LOGICAL,OPTIONAL,INTENT(IN)       :: warning
    CHARACTER*(*),OPTIONAL,INTENT(IN) :: file
    INTEGER,INTENT(IN),OPTIONAL       :: line

62
    LOGICAL                       :: callstop,warn,first_pe
63
    LOGICAL                       :: l_mpi=.FALSE.
64 65
    INTEGER                       :: isize,irank,e,i
    CHARACTER(len=100),ALLOCATABLE::message_list(:)
66
#ifdef CPP_MPI
67
    include 'mpif.h'
68
    LOGICAL :: first_parallel
69
    CALL MPI_INITIALIZED(l_mpi,e)
70 71 72 73 74 75 76 77
    IF (l_mpi) THEN
       CALL MPI_COMM_RANK(MPI_COMM_WORLD,irank,e)
       CALL MPI_COMM_SIZE(MPI_COMM_WORLD,isize,e)
    ELSE
       first_pe=.TRUE.
       isize=1
       irank=0
    ENDIF
78
#else
79 80 81
    first_pe=.TRUE.
    isize=1
    irank=0
82
#endif
83 84 85 86
    warn = .FALSE.
    IF (PRESENT(warning)) warn = warning
    IF (warn) THEN
       !check if we stop nevertheless
87 88 89 90 91 92
       IF (judft_was_argument("-warn_only")) THEN
          callstop=.false.
       ELSE
          INQUIRE(FILE ="JUDFT_WARN_ONLY",EXIST= callstop)
          callstop  = .NOT.callstop
       ENDIF
93 94 95
    ELSE
       callstop = .TRUE.
    ENDIF
96
    
97
#ifdef CPP_MPI
98
    if (l_mpi) CALL collect_messages(message,message_list,first_pe)
99 100 101 102 103
#endif
    
    IF (first_pe) THEN
       IF (.NOT.warn) THEN
          WRITE(0,'(a)') "**************juDFT-Error*****************"
104
       ELSE
105 106 107 108 109 110 111 112 113 114 115
          WRITE(0,'(a)') "************juDFT-Warning*****************"
       ENDIF
       WRITE(0,"(3a)") "Error message:",message
       IF (PRESENT(calledby)) THEN
          WRITE(0,"(3a)") "Error occurred in subroutine:",calledby
       ENDIF
       IF (PRESENT(hint)) THEN
          WRITE(0,"(3a)") "Hint:",hint
       ENDIF
       IF (PRESENT(no)) THEN
          WRITE(0,"(1a,i0)") "Error number:",no
116
       ENDIF
117 118 119 120 121 122 123 124
       IF (PRESENT(file)) THEN
          IF (PRESENT(line)) THEN
             WRITE(0,"(3a,i0)") "Source:",file,":",line
          ELSE
             WRITE(0,"(3a)") "Source:",file
          ENDIF
       ENDIF
#ifdef CPP_MPI
125 126 127 128 129 130 131 132 133 134 135
       IF (l_mpi) THEN
          WRITE(0,'(a,i0,a,i0)') "Error from PE:",irank,"/",isize
          first_parallel=.TRUE.
          DO i=0,isize-1
             IF (i==irank) CYCLE
             IF (LEN_TRIM(message_list(i))>1)THEN
                IF (first_parallel) THEN
                   WRITE(0,'(2a)') "Other PEs with error messages:"
                   first_parallel=.FALSE.
                END IF
                WRITE(0,'(a,i4,2a)') "  ",i,"-",message_list(i)
136
             END IF
137 138
          END DO
       END IF
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
#endif
       WRITE(0,'(2a)') "*****************************************"
       
       IF (.NOT.warn) CALL juDFT_time_lastlocation()
       IF (callstop.and.warn) WRITE(0,'(a)')"Warnings not ignored. Touch 'JUDFT_WARN_ONLY' to make the warning nonfatal"
       IF (callstop) THEN
          CALL writetimes()
          CALL print_memory_info(0,.TRUE.)
          IF (irank==0) THEN
             !Error on PE0 write info to out and out.xml
             WRITE(6,*) "***************ERROR***************"
             WRITE(6,*) message
             WRITE(6,*) "***************ERROR***************"
             CALL writeXMLElement('ERROR',(/"Message"/),(/message/))
             !try closing the out file
             CLOSE(6,iostat=e)
             !Try closing the xml-out
             CALL endXMLOutput()
          ENDIF
       END IF
    ELSE
       CALL priv_wait(2.0)
161 162 163
    ENDIF

    IF (callstop) THEN
164
       CALL add_usage_data("Error",message)
165
       !$OMP MASTER
166
       CALL send_usage_data()
167
       !$OMP END MASTER
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
       CALL juDFT_STOP()
    ENDIF
  END SUBROUTINE juDFT_error

  SUBROUTINE juDFT_warn(message,calledby,hint,no,file,line)
    IMPLICIT NONE
    CHARACTER*(*),INTENT(IN)          :: message
    CHARACTER*(*),OPTIONAL,INTENT(IN) :: calledby,hint
    INTEGER,OPTIONAL,INTENT(IN)       :: no
    CHARACTER*(*),OPTIONAL,INTENT(IN) :: file
    INTEGER,INTENT(IN),OPTIONAL       :: line

    CALL juDFT_error(message,calledby,hint,no,warning = .TRUE.,file=file,line=line)

  END SUBROUTINE juDFT_warn

  SUBROUTINE juDFT_END(message, irank, l_endXML)
    ! If irank is present every mpi process has to call this routine.
    ! Otherwise only a single mpi process is allowed to call the routine.
187
    USE m_judft_xmlOutput
188
    USE m_judft_usage
189 190 191 192 193
    IMPLICIT NONE
    CHARACTER*(*), INTENT(IN)      :: message
    INTEGER, OPTIONAL, INTENT(IN)  :: irank
    LOGICAL, OPTIONAL, INTENT(IN)  :: l_endXML

Matthias Redies's avatar
Matthias Redies committed
194
    LOGICAL l_endXML_local, is_root
195 196 197 198
    LOGICAL :: l_mpi=.false.
#ifdef CPP_MPI
    INCLUDE 'mpif.h'
    INTEGER :: ierr
199
    CALL MPI_INITIALIZED(l_mpi,ierr)
200
#endif
201 202 203 204 205 206 207 208 209 210 211 212 213 214

    l_endXML_local = .TRUE.
    IF(PRESENT(l_endXML)) THEN
       l_endXML_local = l_endXML
    END IF

    IF(l_endXML_local) THEN
       IF(PRESENT(irank)) THEN
          IF (irank.EQ.0) CALL endXMLOutput()
       ELSE
          ! It is assumed that this is the only mpi process calling this routine.
          CALL endXMLOutput()
       END IF
    END IF
215
    IF (TRIM(message)=="") STOP ! simple stop if no end message is given
Matthias Redies's avatar
Matthias Redies committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    
    if(present(irank)) then
       is_root = (irank == 0)
    else
       is_root = .True.
    endif

    IF(is_root) THEN
       WRITE(0,*)
       WRITE(0,*) "*****************************************"
       WRITE(0,*) "Run finished successfully"
       WRITE(0,*) "Stop message:"
       WRITE(0,*) "  ",message
       WRITE(0,*) "*****************************************"
    ENDIF
231
    CALL writetimes()
232
    CALL print_memory_info(0,.true.)
233
    CALL send_usage_data()
234
#ifdef CPP_MPI
235 236 237
    IF (l_mpi) THEN
       IF(PRESENT(irank)) THEN
          CALL MPI_BARRIER(MPI_COMM_WORLD,ierr)
238
          CALL MPI_ERRHANDLER_SET(MPI_COMM_WORLD,MPI_ERRORS_RETURN,ierr)
239 240 241 242 243
          CALL MPI_FINALIZE(ierr)
       ELSE
          CALL juDFT_STOP(0)
       END IF
    ENDIF
244
#endif
Matthias Redies's avatar
Matthias Redies committed
245 246 247 248 249
    if(is_root) then
       STOP 'OK'
    else
       STOP
    endif
250
  END SUBROUTINE juDFT_END
251

252 253 254
  !this is a private subroutine that stops the calculations
  !different compilers might have to be added here
  SUBROUTINE juDFT_stop(errorCode)
255
#ifdef __INTEL_COMPILER
256
    USE ifcore
257
#endif
258 259 260
    INTEGER, OPTIONAL, INTENT(IN)  :: errorCode
    INTEGER :: error
    LOGICAL :: calltrace
261
    LOGICAL,ALLOCATABLE::a(:)
262
    LOGICAL :: l_mpi=.FALSE.
263
#ifdef CPP_MPI
264
    INTEGER :: ierr
265 266
    INCLUDE 'mpif.h'
    CALL mpi_initialized(l_mpi,ierr)
267
#endif
268 269 270 271 272
    error = 0

    IF(PRESENT(errorCode)) THEN
       error = errorCode
    END IF
273
    !Now try to generate a stack-trace if requested
274
    INQUIRE(FILE="JUDFT_TRACE",EXIST=calltrace)
275
    IF (judft_was_argument("-trace")) calltrace=.TRUE.
276 277
    IF (error.EQ.1) calltrace = .TRUE.
    IF (calltrace) THEN
278
#ifdef __INTEL_COMPILER
279
       CALL tracebackqq(USER_EXIT_CODE=-1) !return after traceback
280
#elif (defined(CPP_AIX)&&!defined(__PGI))
281
       CALL xl__trbk()
282
#endif
Matthias Redies's avatar
Matthias Redies committed
283 284
       ! cause an error, so that the compiler generates a stacktrace
       DEALLOCATE(a)
285
    ENDIF
286 287

#if defined(CPP_MPI)
288 289 290 291 292 293 294 295
    IF (l_mpi) THEN
       IF(error.EQ.0) THEN
          WRITE(0,*) ""
          WRITE(0,*) "Terminating all MPI processes."
          WRITE(0,*) "Note: This is a normal procedure."
          WRITE(0,*) "      Error messages in the following lines can be ignored."
          WRITE(0,*) ""
       END IF
296
       CALL MPI_ERRHANDLER_SET(MPI_COMM_WORLD,MPI_ERRORS_RETURN,ierr)
297 298
       CALL MPI_ABORT(MPI_COMM_WORLD,error,ierr)
    ENDIF
299
#endif
300 301
    STOP 'juDFT-STOPPED'
  END SUBROUTINE juDFT_stop
302 303


304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
#ifdef CPP_MPI
  SUBROUTINE collect_messages(mymessage,message_list,first_pe)
    !This routine collects all error messages from all PE into an array
    !As not all PE might call this routine we use non-blocking communication
    !The integer first_pe is true if this pe is the one with the lowest rank among
    !all having error messages to report
    IMPLICIT NONE
    CHARACTER(len=*),INTENT(IN)                              :: mymessage
    CHARACTER(len=100),ASYNCHRONOUS,ALLOCATABLE,INTENT(OUT)  :: message_list(:)
    LOGICAL,INTENT(OUT)                                      :: first_pe
    INCLUDE 'mpif.h'
    INTEGER:: irank,isize,ierr,i
    LOGICAL:: completed
    INTEGER,ALLOCATABLE::ihandle(:)
    CHARACTER(len=100):: message
    REAL :: t1,t2
    
    message=mymessage
    
    CALL MPI_COMM_RANK(MPI_COMM_WORLD,irank,ierr)
    CALL MPI_COMM_SIZE(MPI_COMM_WORLD,isize,ierr)
    ALLOCATE(message_list(0:isize-1))
    ALLOCATE(ihandle(0:isize-1))
    message_list=""
    !Announce that I have a message to all PE
    DO i=0,isize-1
330
       CALL MPI_isend(message,100,MPI_CHARACTER,i,999,MPI_COMM_WORLD,ihandle(0),ierr)
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    ENDDO
    !Collect all message
    DO i=0,isize-1
       CALL MPI_irecv(message_list(i),100,MPI_CHARACTER,i,999,MPI_COMM_WORLD,ihandle(i),ierr)
    ENDDO
    !Wait for 2 seconds
    CALL priv_wait(2.0)
    !Check if any PE with a lower rank also reports an error
    first_pe=.TRUE.
    DO i=0,isize-1
       CALL MPI_TEST(ihandle(i),completed,MPI_STATUS_IGNORE,ierr)
       IF (i<irank) first_pe=first_pe.AND..NOT.completed
    ENDDO
  END SUBROUTINE collect_messages
#endif

  SUBROUTINE priv_wait(sec)
    !Simple routine to wait for sec-seconds
    IMPLICIT NONE
    REAL::sec,t1,t2
    CALL cpu_TIME(t1)
    t2=t1
    DO WHILE(t2-t1<sec)
       CALL cpu_TIME(t2)
    ENDDO
  END SUBROUTINE priv_wait
357
END MODULE m_juDFT_stop
358 359