Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

gfortran on Windows Subsystem for Linux

5,318 views
Skip to first unread message

Beliavsky

unread,
May 3, 2017, 11:39:09 AM5/3/17
to
I just tried Windows Subsystem for Linux (WSL) to run Fortran programs. Here are my impressions, which are positive.

You can install WSL on Windows 10, which gives you a bash terminal. Then you can install gfortran 4.8.4 using sudo apt-get install gfortran . There are more recent versions of gfortran that I will look into installing on WSL. One of my programs compiled with GNU Fortran (GCC) 8.0.0 20170430 from equation.com freezes on Windows for certain inputs. That program, with the same inputs, runs fine with gfortran on WSL. Great! With other inputs, the program on Windows takes 31 s with gfortran and 24 s with Intel Fortran, but it only takes 17 s with gfortran on WSL (all programs compiled with -O2).

I am pleased that on WSL, my program compiled by gfortran run faster and more robustly. On WSL, you can see the Windows file system. What is c:\fortran on Windows is /mnt/c/fortran on WSL. You do not need to make separate copies of source code for Windows and WSL. If you have data files in c:\data, your WSL program needs to translate that into /mnt/c/data . Using the get_command_argument of Fortran 2003, you could determine at run-time whether Windows or WSL is being used (is the executable a.exe or a.out?) and translate Windows directories to WSL when needed. For now I am storing data files in the same directory as the code.

WSL does not run Linux GUI programs by default, and I find editing source code on it inconvenient. But it looks like a good way to run console-based Fortran programs.

For the program below http://www.60bits.net/msu/mycomp/bench.htm the time taken is 3.7 s on gfortran/Windows and 3.8 s on gfortran/WSL . Even if gfortran/WSL is not faster than gfortran/Windows, there may not be an appreciable speed penalty.

PROGRAM RANPI

c Program to compute PI by probability.
c By Mark Riordan 24-DEC-1986;
c Original version apparently by Don Shull.
c To be used as a CPU benchmark.

write(*,*) 'Starting PI...'
ztot = 0.0
low = 1
ixran = 1907
yran = 5813.0
ymult = 1307.0
ymod = 5471.0
itot = 120000000 ! scaled up 100 from original program
DO 100 j=1,itot

c X and Y are two uniform random numbers between 0 and 1.
c They are computed using two linear congruential generators.
c A mix of integer and real arithmetic is used to simulate a
c real program. Magnitudes are kept small to prevent 32-bit
c integer overflow and to allow full precision even with a 23-bit
c mantissa.

iprod = 27611 * ixran
ixran = iprod - 74383*(iprod/74383)
x = float(ixran) / 74383.0
prod = ymult * yran
yran = (prod - ymod*int(prod/ymod))
y = yran / ymod
Z = X*x + Y*y
call myadd(ztot,z)
IF ( Z .LE. 1.0 ) THEN
low = low + 1
ENDIF
100 continue
write(*,9000) x,y,low,j
9000 format(' x=',f8.5,' y=',f8.5,' low=',i7,' j=',i7)
Pi = 4.0 * float(low)/float(Itot)
write(*,*) 'Pi = ',pi,' ztot = ',ztot,' itot = ',itot
STOP
END
subroutine myadd(sum,addend)

c Simple adding subroutine thrown in to allow subroutine
c calls/returns to be factored in as part of the benchmark.

sum = sum + addend
return
end

tim prince

unread,
May 3, 2017, 1:58:54 PM5/3/17
to
You may have noticed that gfortran/gcc/g++ 7.1 can build and run
testsuite on WSL, (only) if testsuite is run in default sequential mode.
gfortran 8.0 will not complete testsuite.
WSL appears to have typical Windows problems if running more than 1
program or process which pipes to a file or screen.
cilkplus should probably be disabled.

--
Tim Prince

Beliavsky

unread,
May 11, 2017, 3:18:02 PM5/11/17
to
On Wednesday, May 3, 2017 at 11:39:09 AM UTC-4, Beliavsky wrote:
> I just tried Windows Subsystem for Linux (WSL) to run Fortran programs. Here are my impressions, which are positive.
>

Microsoft announced that WSL will be supporting more Linux distributions. Being able to install a Linux distribution on Windows that has easy access to an up-to-date gfortran will be nice.

https://blogs.msdn.microsoft.com/commandline/2017/05/11/new-distros-coming-to-bashwsl-via-windows-store/
We’re REALLY excited about this announcement and think many of you will be too.

On May 11th 2017, during Microsoft’s Build conference keynote, Terry Myerson (EVP for Windows & Devices Group) made several announcements about the Windows Subsystem for Linux:

We continue our partnership with our friends at Canonical to bring Ubuntu to the Windows Store app
We are also working with the great teams at SUSE and Fedora to bring their Linux distro’s to the Windows Store & Windows Subsystem for Linux (WSL)
You will be able to download these distro’s from the store and install them side-by-side on your PC(s)
You’ll be able to run one or more distro’s simultaneously if you wish

Ian D Chivers

unread,
May 11, 2017, 4:20:34 PM5/11/17
to
Thanks Beliavsky

that is good news. I'll ad a news entry in the August edition of Fortran
Forum.

Another option for Windows Fortran users.

Beliavsky

unread,
May 16, 2017, 5:25:32 PM5/16/17
to
On Wednesday, May 3, 2017 at 11:39:09 AM UTC-4, Beliavsky wrote:
> I just tried Windows Subsystem for Linux (WSL) to run Fortran programs. Here are my impressions, which are positive.
>
> You can install WSL on Windows 10, which gives you a bash terminal. Then you can install gfortran 4.8.4 using sudo apt-get install gfortran . There are more recent versions of gfortran that I will look into installing on WSL. One of my programs compiled with GNU Fortran (GCC) 8.0.0 20170430 from equation.com freezes on Windows for certain inputs. That program, with the same inputs, runs fine with gfortran on WSL. Great! With other inputs, the program on Windows takes 31 s with gfortran and 24 s with Intel Fortran, but it only takes 17 s with gfortran on WSL (all programs compiled with -O2).
>
> I am pleased that on WSL, my program compiled by gfortran run faster and more robustly. On WSL, you can see the Windows file system. What is c:\fortran on Windows is /mnt/c/fortran on WSL. You do not need to make separate copies of source code for Windows and WSL. If you have data files in c:\data, your WSL program needs to translate that into /mnt/c/data . Using the get_command_argument of Fortran 2003, you could determine at run-time whether Windows or WSL is being used (is the executable a.exe or a.out?) and translate Windows directories to WSL when needed. For now I am storing data files in the same directory as the code.
>
> WSL does not run Linux GUI programs by default, and I find editing source code on it inconvenient. But it looks like a good way to run console-based Fortran programs.
>

With other inputs, the program compiled by gfortran terminated on WSL, but it did so gracefully without freezing my computer as on Windows. I found a bug in my program -- trying to use an ALLOCATABLE component of a derived type that had not been ALLOCATED. It looks like running gfortran on WSL can be a safer way to debug your program.

Beliavsky

unread,
Jun 4, 2017, 10:23:15 PM6/4/17
to
On Wednesday, May 3, 2017 at 11:39:09 AM UTC-4, Beliavsky wrote:

>
> WSL does not run Linux GUI programs by default, and I find editing source code on it inconvenient. But it looks like a good way to run console-based Fortran programs.
>

Installing an X server as described at http://www.pcworld.com/article/3055403/windows/windows-10s-bash-shell-can-run-graphical-linux-applications-with-this-trick.html makes it possible to run Linux programs with GUIs. Some text editors that work are XEmacs (but I cannot figure out how to change the small font size), gedit, and geany. Geany runs well and has a Fortran mode.

NoPowerTXT Advice

unread,
Dec 9, 2020, 8:41:31 PM12/9/20
to
I wanted to compile a fortran program in Google Cloud Shell, and couldn't find any instructions, and I found this post and it worked verbatim.

sudo apt-get install gfortran
gfortran hello_world.f90 (my program)

Then execute "a.out".

Nice! :D
0 new messages