LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-13-2015, 04:45 PM   #1
el_duderino
LQ Newbie
 
Registered: Mar 2015
Posts: 3

Rep: Reputation: Disabled
Fortran: problem with NEWUNIT on different machines


Hello all,

I'm writing a program in Fortran 90 which uses random number distributions. To generate the distributions, I'm using the following program (just as a test..):

Code:
program test_rand
 implicit none

 integer :: seed, i, j, k, kk

 real :: r

 CALL init_random_seed()         ! see example of RANDOM_SEED
 
 do j = 1, 5
   kk = 0
    do k = 1, 100
       do i = 1, 1000
          kk = kk + 1
          CALL RANDOM_NUMBER(r)
          write(1000+j,*) kk, r
       enddo
    enddo
 enddo
 
 stop
end program test_rand

subroutine init_random_seed()
  implicit none
  integer, allocatable :: seed(:)
  integer :: i, n, un, istat, dt(8), pid, t(2), s
  integer(8) :: count, tms

  call random_seed(size = n)
  allocate(seed(n))
  ! First try if the OS provides a random number generator
  open(newunit=un, file="/dev/urandom", access="stream", &
       form="unformatted", action="read", status="old", iostat=istat)
  if (istat == 0) then
     read(un) seed
     close(un)
  else
     ! Fallback to XOR:ing the current time and pid. The PID is
     ! useful in case one launches multiple instances of the same
     ! program in parallel.
     call system_clock(count)
     if (count /= 0) then
        t = transfer(count, t)
     else
        call date_and_time(values=dt)
        tms = (dt(1) - 1970) * 365_8 * 24 * 60 * 60 * 1000 &
             + dt(2) * 31_8 * 24 * 60 * 60 * 1000 &
             + dt(3) * 24 * 60 * 60 * 60 * 1000 &
             + dt(5) * 60 * 60 * 1000 &
             + dt(6) * 60 * 1000 + dt(7) * 1000 &
             + dt(8)
        t = transfer(tms, t)
     end if
     s = ieor(t(1), t(2))
     pid = getpid() + 1099279 ! Add a prime
     s = ieor(s, pid)
     if (n >= 3) then
        seed(1) = t(1) + 36269
        seed(2) = t(2) + 72551
        seed(3) = pid
        if (n > 3) then
           seed(4:) = s + 37 * (/ (i, i = 0, n - 4) /)
        end if
     else
        seed = s + 37 * (/ (i, i = 0, n - 1 ) /)
     end if
  end if
  call random_seed(put=seed)
end subroutine init_random_seed
Now, if I try to compile this small program on my laptop with gfortran and Ubuntu 14.10, no problem. Same as for another laptop, Ubuntu 10.10, and for a Red Hat Cluster. However, if I try to complile it on a Mac, I have the following error message:
Code:
open(newunit=un,file="/dev/urandom", access="stream", &
             1
Error: Syntax error in OPEN statement at (1)
I'm really struggling trying to understand why the Mac machine does not like the 'newunit' statement. In fact, it is also not even fonted in vim.

Based on the code posted, if I substitute 'newunit' with 'unit', I have different results if I ask to print the variables 'unit' and 'seed' in my laptop and in the Mac machine, and I do not understand why.

Could anyone please help me?

Thanks
 
Old 03-13-2015, 05:12 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
newunit is not a valid argument to open in fortran 90:
ftp://ftp.nag.co.uk/sc22wg5/N001-N1100/N692.pdf

It was introduced in fortran 2008:
http://www.j3-fortran.org/doc/year/10/10-007.pdf

A good reference for the Fortran standards is here (that's where those previous two links are from):
https://gcc.gnu.org/wiki/GFortranStandards

As for the different values, of course, unit is an input to open, newunit is an output. When you change newunit to unit, it means you're passing in an undefined variable for the unit number, which may have unintended side effects when you go to use it.

Just change newunit=un to unit=10 (or [almost] any other number, or set un=10 on a previous line and use unit=un).

Last edited by suicidaleggroll; 03-13-2015 at 05:15 PM.
 
1 members found this post helpful.
Old 03-14-2015, 09:41 AM   #3
el_duderino
LQ Newbie
 
Registered: Mar 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank! Now I have similar results in all the machines.
Probably, in the Mac machine the fortran compiler is too old, while newer compiler on other machines do recognize newunit. Am I right?

However, I still don't understand why the results are so different specifying un = 10 from the case in which I do not specify it in the Mac machine. Do you have any idea?

Last edited by el_duderino; 03-14-2015 at 09:52 AM.
 
Old 03-14-2015, 10:01 AM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Yes gfortran on the Linux machine is probably newer and has been updated with the 2008 standards.

As for the different results - you were passing an uninitialized variable to the unit number input. Unit numbers can not be anything you want, if the wrong number was in that memory location (eg: stdin, stdout, stderr) it could cause open to behave erratically.
 
1 members found this post helpful.
Old 03-14-2015, 11:20 AM   #5
el_duderino
LQ Newbie
 
Registered: Mar 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Very well, thanks for your help and the explainations!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
g77 in gcc 4.1.0 not found only gfortran fortran 95 compiler! I need fortran 77. TheBrick Linux - Software 3 07-04-2007 06:39 AM
Fortran print problem. ArthurHuang Programming 1 09-29-2006 12:40 PM
Gnu Fortran versus Intel Fortran tomatoefish Linux - Software 3 02-20-2006 01:31 PM
C-Fortran linking problem hhegab Fedora 0 09-24-2005 03:03 AM
does linux fortran compiler in fedora 4 support VAX FORTRAN? terrence Programming 17 08-31-2005 08:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration