0

I am looking for a pure way to have access to time information. I thought about intrinsic functions and subroutines of standards compiler (date_and_time,cpu_time, system_clock, ltime, ctime, ...), the format do not really matter to me. I also thought about MPI function, but it is the same as intrinsic functions, there are all impure functions.

Here is a minimal example:

elemental subroutine add(message, text)
  ! function add
  IMPLICIT NONE
  character(len=:),allocatable,intent(inout)   :: message
  character(len=*), intent(in)                 :: text
  character(len=1), parameter                  :: nl=char(10)
  character(10) :: time
  ! character(8)  :: date

  ! time= 'hhmmss.sss'
  call DATE_AND_TIME(time) 
  Message= Message//time////text//nl

end subroutine add

and I get a logical error :

Error: Subroutine call to intrinsic ‘date_and_time’ at (1) is not PURE  

Thus, I am wandering if a pure way to have a time information exists, or if it is impossible to have it purely (maybe because it has to use cpu information which, for a reason unknown to me, could be thread-unsafe).

Maybe, a subquestion, could be is there a solution to force the compiler to consider date_and_time pure (or any other function of that kind)?

R. N
  • 707
  • 11
  • 31
  • Are you happy with `impure elemental` for those that do need access to non-pure intrinsics? – francescalus Apr 25 '17 at 09:49
  • @francescalus you are right `ltime` and `ctime` are not Fortran standard intrinsics neither are MPI functions. But if it does the job I would accept it as a solution. And I want to keep an `pure elemental` subroutine for this example. Otherwise `impure elemental` is an option to compile the example but not to answer the question. – R. N Apr 25 '17 at 10:04
  • 3
    Wikipedia (https://en.wikipedia.org/wiki/Pure_function) states that a function which returns the current time is impure. I agree with Wikipedia. But you don't have to. – High Performance Mark Apr 25 '17 at 10:10
  • 1
    In answer to forcing the compiler to consider a non-pure/impure procedure as pure, consider [a hack](https://stackoverflow.com/a/29683745) I mentioned in a different context. Just don't consider it too much... – francescalus Apr 25 '17 at 10:17
  • @HighPerformanceMark Ok, thanks to you, I understand that the answer to the main question is no because at different times it will yield different results. Anyway, maybe my subquestion makes more sound now. – R. N Apr 25 '17 at 10:18
  • Worth noting too, perhaps, that the Fortran standard doesn't state that `date_and_time` is pure which is probably to be understood as meaning that it is impure. – High Performance Mark Apr 25 '17 at 10:24

1 Answers1

3

The answer about a pure manner to get time is no. A function that returns the current time or date, is impure because at different times it will yield different results—it refers to some global state.

There are certainly tricks to persuade the compiler that a subroutine is pure. One is to flat out lie in an interface block.

But there are consequences from lying to the compiler. It can do optimizations which are unsafe to do and the results will be undefined (most often correct anyway, but...).

module m
contains

  elemental subroutine add(text)
    IMPLICIT NONE
    character(len=*), intent(in)                 :: text
    character(len=1), parameter                  :: nl=char(10)
    character(10) :: time
          intrinsic date_and_time

    interface
      pure subroutine my_date_and_time(time)
        character(10), intent(out) :: time
      end subroutine
    end interface

    call MY_DATE_AND_TIME(time) 
  end subroutine add

end module

program test
  use m

  call add("test")
end program

subroutine my_date_and_time(time)
  character(10), intent(out) :: time

  call date_and_time(time)

end subroutine

Notice I had to delete your message because that was absolutely incompatible with elemental.

R. N
  • 707
  • 11
  • 31
  • I have some troubles when I try to compile your solution, I get the error: undefined reference to « my_date_and_time_ » when I try to produce the executable file. And this even when I change the subroutine name "my_date_time" to "my_date_and_time". What am I missing ? – R. N Apr 25 '17 at 14:02
  • To precise my previous comment : I put your solution in the contains part of module, when I create a test program using the previous module add only call the add subroutine. Doing so, I get the error. Perhaps I should edit my question to let this try appear ? – R. N Apr 25 '17 at 14:16
  • Well, you can't put `my_date_and_time` in a module. You can't lie about a function in a module. It is an external function. – Vladimir F Героям слава Apr 25 '17 at 14:18
  • I feel completely dumb. I put the subroutine `my_date_and_time` outside of the module and outside of the program and I still the same error. Could you give a working example using a `module` and a `program` in your answer ? – R. N Apr 25 '17 at 14:40