stop.F90 10.3 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_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 63 64
    LOGICAL                       :: callstop,warn,first_pe
    INTEGER                       :: isize,irank,e,i
    CHARACTER(len=100),ALLOCATABLE::message_list(:)
65
#ifdef CPP_MPI
66
    include 'mpif.h'
67
    LOGICAL :: first_parallel
68
    CALL MPI_COMM_RANK(MPI_COMM_WORLD,irank,e)
69
    CALL MPI_COMM_SIZE(MPI_COMM_WORLD,isize,e)
70
#else
71 72 73
    first_pe=.TRUE.
    isize=1
    irank=0
74
#endif
75 76 77 78
    warn = .FALSE.
    IF (PRESENT(warning)) warn = warning
    IF (warn) THEN
       !check if we stop nevertheless
79 80 81 82 83 84
       IF (judft_was_argument("-warn_only")) THEN
          callstop=.false.
       ELSE
          INQUIRE(FILE ="JUDFT_WARN_ONLY",EXIST= callstop)
          callstop  = .NOT.callstop
       ENDIF
85 86 87
    ELSE
       callstop = .TRUE.
    ENDIF
88
    
89 90 91 92 93 94 95
#ifdef CPP_MPI
    CALL collect_messages(message,message_list,first_pe)
#endif
    
    IF (first_pe) THEN
       IF (.NOT.warn) THEN
          WRITE(0,'(a)') "**************juDFT-Error*****************"
96
       ELSE
97 98 99 100 101 102 103 104 105 106 107
          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
108
       ENDIF
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
       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
       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)
          END IF
       END DO
#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)
151 152 153
    ENDIF

    IF (callstop) THEN
154
       CALL add_usage_data("Error",message)
155
       !$OMP MASTER
156
       CALL send_usage_data()
157
       !$OMP END MASTER
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
       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.
    USE m_xmlOutput
178
    USE m_judft_usage
179
    IMPLICIT NONE
180
#ifdef CPP_MPI
181 182
    INCLUDE 'mpif.h'
    INTEGER :: ierr
183
#endif
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    CHARACTER*(*), INTENT(IN)      :: message
    INTEGER, OPTIONAL, INTENT(IN)  :: irank
    LOGICAL, OPTIONAL, INTENT(IN)  :: l_endXML

    LOGICAL l_endXML_local

    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
203
    IF (TRIM(message)=="") STOP ! simple stop if no end message is given
204
    WRITE(0,*)
205 206 207 208 209 210
    WRITE(0,*) "*****************************************"
    WRITE(0,*) "Run finished successfully"
    WRITE(0,*) "Stop message:"
    WRITE(0,*) "  ",message
    WRITE(0,*) "*****************************************"
    CALL writetimes()
211
    CALL print_memory_info(0,.true.)
212
    CALL send_usage_data()
213
#ifdef CPP_MPI
214 215 216 217 218 219
    IF(PRESENT(irank)) THEN
       CALL MPI_BARRIER(MPI_COMM_WORLD,ierr)
       CALL MPI_FINALIZE(ierr)
    ELSE
       CALL juDFT_STOP(0)
    END IF
220
#endif
221 222
    STOP 'OK'
  END SUBROUTINE juDFT_END
223

224 225 226
  !this is a private subroutine that stops the calculations
  !different compilers might have to be added here
  SUBROUTINE juDFT_stop(errorCode)
227
#ifdef __INTEL_COMPILER
228
    USE ifcore
229 230
#endif
#ifdef CPP_MPI
231
    INCLUDE 'mpif.h'
232
#endif
233 234 235
    INTEGER, OPTIONAL, INTENT(IN)  :: errorCode
    INTEGER :: error
    LOGICAL :: calltrace
236
    LOGICAL,ALLOCATABLE::a(:)
237
#ifdef CPP_MPI
238
    INTEGER :: ierr
239
#endif
240 241 242 243 244
    error = 0

    IF(PRESENT(errorCode)) THEN
       error = errorCode
    END IF
245
    !Now try to generate a stack-trace if requested
246
    INQUIRE(FILE="JUDFT_TRACE",EXIST=calltrace)
247
    IF (judft_was_argument("-trace")) calltrace=.TRUE.
248 249
    IF (error.EQ.1) calltrace = .TRUE.
    IF (calltrace) THEN
250
#ifdef __INTEL_COMPILER
251
       CALL tracebackqq(USER_EXIT_CODE=-1) !return after traceback
252
#elif (defined(CPP_AIX)&&!defined(__PGI))
253
       CALL xl__trbk()
254
#endif
255 256
       DEALLOCATE(a)!will generate an error that can be found by the compiler
    ENDIF
257 258

#if defined(CPP_MPI)
259 260 261 262 263 264 265 266
    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
    CALL MPI_ABORT(MPI_COMM_WORLD,error,ierr)
267
#endif
268 269
    STOP 'juDFT-STOPPED'
  END SUBROUTINE juDFT_stop
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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
#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
       CALL MPI_ibsend(message,100,MPI_CHARACTER,i,999,MPI_COMM_WORLD,ihandle(0),ierr)
    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
325
END MODULE m_juDFT_stop
326 327