Saturday, September 11, 2010

Install gfortran: Windows Binary Installation

I have just installed gfortran on Windows XP using a binary installation. Some binary installation options for Windows can be found at http://gcc.gnu.org/wiki/GFortranBinaries. Below is an outline of the binary options that are available.

  • MinGW (Minimalist GNU for Windows) - The "native Windows" build. Only release versions of gcc/gfortran are distributed. Latest version 4.5.1. http://www.mingw.org/
  • Unofficial build of gfortran 4.6 - Comes equipped with an installer that the gfortran wiki claims is simple to use. Claims binary installation works on Windows XP. Latest version 4.6.x. http://users.humboldt.edu/finneyb/gfortran-windows.exe
  • Equation.com - Binary builds that are, "are said to work well - but is not affiliated with any MinGW or GCC developer. Use at your own risk." They offer release builds in addition to the latest snapshot. Latest release version 5.4.1. Latest snapshot 4.6.2. http://www.equation.com/servlet/equation.cmd?call=fortran
  • Cygwin - A Unix like environment for Windows. Binary builds are provided, but Cygwin must be installed, which is something that takes time. For more information, see the Cygwin homepage http://www.cygwin.com/ and then check the gfortran wiki for links to the binaries and instructions if you decide to go that route. Latest version 4.6.x.

Of the above list, I have installed the binaries from MinGW, Equation.com, and Cygwin with success in the past. The Equation.com builds were the easiest to install, as I remember. In addition to the above installation options, I found another binary installation which I will be using for the purposes of this tutorial.

  • TDM-GCC - Provides an easy to use installer that has the added benefit of integrating nicely with an open source IDE. Only release versions of gcc/gfortran are available. Latest version 4.5.1.http://tdm-gcc.tdragon.net

I chose this option because of the advertised ability to easily integrate with the open source IDE (Integrated Development Environment) Code::Blocks. Here is a little more information about the TDM-GCC binary installation, from the TDM-GCC README file:

This edition of TDM-GCC is an unofficial replacement for the official GCC binaries distributed by the MinGW project; please note the following caveats: TDM-GCC is not formally affiliated with or endorsed by the MinGW project. No level of support for TDM-GCC is in any way guaranteed, although a best effort is made to fix bugs as they are found or forward them to GCC Bugzilla.

Using the TDM/MinGW installer is highly recommended; it can automatically install TDM-GCC (or the official MinGW GCC) as well as all supplementary base system packages. The installer uses a standard wizard interface with reasonable defaults.

TDM-GCC Installation

The installation process for this binary installation could not have been easier. From the download page http://tdm-gcc.tdragon.net/download, I selected the "Bundle Installer" option. Downloading the 20MB file "tdm-gcc-4.5.1.exe" goes quickly and then installation is ready to begin. Luckily, TDM-GCC provides a very nice installer that does pretty much everything. Run "tdm-gcc-4.5.1.exe" to launch the wizard installer. We are interested in installing gfortran, however, we will also be installing gcc with their installer. When the wizard launches, following these steps:

  • Select "Create" to, "create a new TDM-GCC installation."
  • On the next screen, select "MinGW/TDM 32-bit."
  • Enter an installation directory. I suggest keeping the default location "C:\MinGW32"
  • Select the mirror to download any updates from. Again, just keep the default (SourceForge).
  • A list of components is shown. Expand the first item under "Components" which is named "gcc (TDM-GCC Current: 4.5.1-tdm-1)." From the options that drop down, check the box next to "fortran" to ensure that gfortran is installed by the installer. See the screen shot below:

Once you've completed the previous step, click "Install" and wait for the installer to download any updates and complete the installation of gcc/gfortran.

The default wizard options add the necessary directories to the path. To test if the installation was successful, open a command window (Start Menu->Run), enter cmd (see below), and press OK to launch the command line window (equivalent of Terminal on Mac/Linux).

The Windows command line operates much in the same way the Terminal does on Mac/Linux. For example, to test if our gfortran installation was successful, enter the following on the command line:

> gfortran
gfortran: no input files

The gfortran commands for compiling and building are the same as discussed in "Hello!", the previous post below. The only difference is that when a built executable is to be run from the Windows command line, it is invoked with just the file name and nothing else. For example, to build and run our HelloProgram.f95 on Windows, use the following:

> gfortran HelloProgram.f95 -o hello
> hello
Hello!

IDE and Debugger

As mentioned previously, Code::Blocks is an IDE for Windows that was created for use with C/C++. However, there appears to be a Fortran plugin that supports Fortran syntax highlighting, building Fortran projects, and debugging features. It works well with the TDM-GCC binary distribution that was just installed. The Fortran plug-in can be found at http://darmar.vgtu.lt. From the download page, I chose the installation file CodeBlocks_Fortran_v0.4.1_Win32.zip.

Installation instructions are provided at the above website. I have copied the instructions that will allow Code::Blocks to be interfaced properly with the TDM-GCC installation we just installed.

Windows users may need to help Code::Blocks to find MinGW and Fortran compiler on their system. Short instruction:

In Code::Blocks open "Settings -> Compiler and Debugger..." dialog. Find "GNU Fortran Compiler". Open "Toolchain executables" tab. Fill "Compiler's installation directory" text-box (in our case it is C:\MinGW32).

For example, see the "Toolchain executables" tab filled out correctly below. Would also be valid if the "mingw32-" was removed from each of the fields where it currently resides (check the folder C:\MinGW\bin to see which executables are present).

There seems to be a minor bug (the debug toolbar does not appear) when running the codeblocks.exe file provided in the .zip file from http://darmar.vgtu.lt. This is remedied by launching Code::Blocks with the codeblocks_portable.bat file which is located in the same directory.

Besides that minor glitch, everything seems to work great. Debugging works quite well. There is an option which allows variables to be viewed during debugging, found in “Debug->Debugging Windows->Watches.” There are also other debugging options, such as the ability to view the memory stack.

The IDE discussion might warrant a separate post.

Thursday, September 9, 2010

Building gfortran from Source Code on Mac OS X 10.6

The previous post outlined how to install gfortran on Mac OS X 10.6 using a binary installation. The fact that gfortran is a part of the open source gcc project means that building your own binary files directly from the source code is an option. This option is nice to have, especially since the gfortran developer community is continuously working to fix bug reports and add new features to the compiler.

When I say gcc, I am refering to the GNU Compiler Collection (http://gcc.gnu.org/), which is a collection of various types of compilers. The project currently, "includes front ends for C, C++, Objective-C, Fortran, Java, and Ada." The Fortran compiler of the gcc project is gfortran, which we will be installing, and which previous posts have refered to. Keep in mind that gfortran is just a part of gcc. We will selectively only install gfortran at this time.

Downloading the Source Code

Source code for the gcc project is provided in two ways:

  • Subversion (SVN) - A command line tool that automatically downloads the current source code
  • Tar-ball (.tar.gz) - a compressed file that contains a snapshot of the source code from a particular time in development

The gcc project provides snapshots on a weekly basis. These snapshots are working versions of the compilers, thus, they are considered experimental. Stable releases are also provided. These releases come every couple of months, so while they might be considered more stable, they might not have the current additions or bug-fixes that the development releases might have.

The tar-ball for either a stable release or a snapshot release can be downloaded from: http://gcc.gnu.org/mirrors.html Select any mirror site from the list provided. Some of the mirrors are more up to date than others, so keep that in mind if a particular version of gcc/gfortran is not available on the site you are browsing.

I am currently using Firefox to browse the ftp site of the mirror I chose from the list. I am not sure if other browsers can view ftp sites. You should see a list of folders on the mirror site:

  • infrastructure
  • libstdc++
  • old-releases
  • releases
  • snapshots
  • summit

We are interested in the releases and snapshots folders. The releases folder contains complete releases of gcc, major or minor versions. The snapshots folder contains snapshots of the source code in current development. For more information on the weekly snapshots, see: http://gcc.gnu.org/snapshots.html

For this example, we will go through the exercise of building a release of gfortran 4.5.1. Navigate to the releases folder, navigate down to the gcc-4.5.1 folder. Within this folder, we are going to download the tar-ball file: gcc-4.5.1.tar.gz, which is roughly 84MB.

Install Apple Developer Tools

If not already done, install the Apple Developer Tools either from the installation disc that came with your computer or download a .dmg file from Apple's website: http://developer.apple.com/technologies/xcode.html

Building the Source Code

There are limited instructions provided on the gcc wiki: http://gcc.gnu.org/wiki/GFortranSource. I will go through the installation instructions step-by-step below for building gfortran on Mac OS X 10.6.

Let's say that we downloaded gcc-4.5.1.tar.gz to the Desktop. On the Mac, double click on the downloaded file to unpack its contents. Unpacking should produce a folder named gcc-4.5.1. Before doing anything else, create a new, empty folder on the Desktop named gfortran-4.5.1-build, for example. It is important that this folder should be at the same "level" as the unpacked folder that contains the gcc source code. Our newly created gfortran-4.5.1-build folder will be our working directory. Launch Terminal to get started and enter the following on the command line:

$ cd desktop/gfortran-4.5.1-build
$ ../gcc-4.5.1/configure --prefix=$HOME/gfortran-4.5.1 --enable-languages=fortran --enable-checking=release --disable-bootstrap

A program will now run within Terminal that will configure a makefile. In all honesty, I am not entirely sure what the above options do, but here is a description of each:

--prefix=$HOME/gfortran-4.5.1

This option ensures that gfortran will be installed in the directory gfortran-4.5.1 at the location /users/userName on your hard drive (your home folder). You could specify any directory or location for the install.

--enable-languages=fortran

Fortran is the only language from gcc that we wish to install. If this option was omitted, all the compilers from the gcc project would be installed.

--enable-checking=release

From the gfortran wiki, “esp. useful for compile-time tests as it disables some consistency checks in the compile.” Not required.

--disable-bootstrap

From the gfortran wiki, “builds faster, but disables some consistency checks.” Not required.

Before moving on to the next step, enter the following command in Terminal, your result should be the same as mine, assuming the Apple Developer Tools were installed properly:

$ which gcc
/usr/bin/gcc

With the configure process completed, it is time to start building the source code. Please keep in mind that this process might take upwards of an hour and will take a lot of CPU resources. To begin, enter the following in Terminal:

$ make CC=/usr/bin/gcc

When the process finally finishes, hopefully without error, issue the following command in terminal:

$ make install

This final step completes the installation. The install directory was specified as “$HOME/gfortran-4.5.1”, thus, that is where the gfortran installation will be located. The folder gfortran-4.5.1 should contain the following folders:

  • bin
  • include
  • lib
  • libexec
  • share

Updating the PATH

The folder gfortran-4.5.1 can now be moved to someplace else on the hard drive (for example, /usr/local). Since /usr/local is a hidden folder, it can be opened in Finder with the following Terminal command:

$ open -a finder /usr/local

If the folder was copied to /usr/local, the PATH would have to be updated with the following:

sudo pico /etc/paths

Update the file with the following entry:

/usr/local/gfortran-4.5.1/bin

Close and reopen the terminal for the PATH changes to take effect. The above folder is the location of the gfortran executable that we have just built. It might also be a good idea to rename the gfortran executable with a version number in the name (example: gfortran-4.5.1) just in case there are multiple versions of gfortran in the PATH. Test that the newly built gfortran is working properly:

$ gfortran
gfortran: no input files
$ gfortran --version
GNU Fortran (GCC) 4.5.1 (stable)
Copyright (C) 2009 Free Software Foundation, Inc.

GNU Fortran comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of GNU Fortran
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING

Test your newly built gfortran by building some Fortran code of your own. See the post entitled "Hello!" for a quick, simple example.

Sunday, September 5, 2010

How to Install gfortran: Mac OS X 10.6/10.5 Binaries

The easiest way to install gfortran is by using a binary installation. The binary installation can be found at the following site: http://gcc.gnu.org/wiki/GFortranBinaries

The above website currently provides four different binary installation options.

  • Fink - A package manager for Mac OS X that aims to port a lot of GNU software to the Mac operating system for easy installation.
  • HPC - High Performance Computing. Offers relatively up to date binary installations for both Mac OS X 10.5 and 10.6.
  • r.research.att.com - Offers a binary installation, but only for an older version of gfortran/gcc.
  • gcc.gnu.org - Provides an Apple-style installer for certain versions of gfortran.

I have tried installing gfortran/gcc using Fink, however, I had mixed results. Fink made my computer run quite hot and took much longer than other binary sources. Unfortunately, the gfortran/gcc version provided by r.research.att.net is an older release, so it probably won't have access to the newer features added to gfortra. I have not tried the "Apple like installer" option provided by gnu.gcc.org, as they have just recently begun to provide binaries for Mac OS X 10.6. I have found that using the binary installation provided by HPC is the most straightforward and up to date way to install gfortran on Mac OS X. Currently a development edition of gfortran 4.6 is provided. The direct link to their website is here: http://hpc.sourceforge.net/.

Install Apple Developer Tools

Before installing gfortran, it is required that the Apple Developer Tools (which includes Xcode) be installed. The developer tools can be installed from the operating system installation disc or they can be installed by downloading a .dmg from Apple’s website which requires a free registration and login to access: http://developer.apple.com/technologies/xcode.html

gfortran Binary Installation

I have chosen to provide instructions using the HPC binary installation. There are install instructions on the HPC website (http://hpc.sourceforge.net/), but I will also include the installation instructions here. To begin, select the .dmg from the HPC website that corresponds to the Mac operating system you are using (either 10.5 or 10.6). For example, I downloaded the file gfortran-snwleo-intel.bin.tar.gz since I am running Mac OS X 10.6, aka Snow Leopard. Once the .dmg has been downloaded, open a Terminal window and "cd" to the folder where the file was saved. For example, if I saved the file in my Downloads folder, I would use the following Terminal commands:

$ cd /users/user/downloads

Once at the location of the .dmg file, type the following into the Terminal command prompt:

$ gunzip gfortran-snwleo-intel-bin.tar.gz
$ sudo tar -xvf gfortran-snwleo-intel-bin.tar.gz -C /

Note that the "sudo" command requires you to enter the administrator password. That's it. The above process performs a binary installation of gfortran in the hidden directory /usr/local. It should be noted that this folder will only exist after the Apple Developer Tools have been installed. One method used to view this directory in Finder is to issue the following command in Terminal:

$ open -a finder /usr/local

If the folder does not exist, install the Developer Tools! I am not sure, but I believe the gfortran install process would have failed if the Developer Tools were not installed properly at this time.

Modify PATH

It is possible that gfortran is ready to use at this time. However, there is also the possibility that the /usr/local/bin folder (where the gfortran executable is stored) still needs to be added to the PATH. This allows the executable to be invoked while in any folder in Terminal. I believe by default the folder is in the path on 10.6, but it is possible that something has been changed. To manually add the folder to the PATH, we will open a text editor in Terminal and edit the file /etc/paths.

$ sudo pico /etc/paths

Opening this file requires entering the admin password. If the file /etc/paths contains a line /usr/local/bin, the file can be closed with CTRL+X. If not present, arrow down to the last available line and type the folder location:

/usr/local/bin

After the folder has been added, CTRL+X, press Y to save the file, and then press enter to exit the file. Terminal needs to be closed and re-opened before the changes take effect.

Test Installation

To test if gfortran is installed and the PATH is defined correctly, type the following in Terminal:

$ gfortran
gfortran: no input files

Seeing the above result indicates that the gfortran executable was found via the PATH, was executed, and correctly issued the error message indicating there was no Fortran source file specified on the command line. It is entirely possible that while the gfortran executable will work with no input files, when it comes time to compile a file, the executable might fail if not installed properly. To test gfortran with a Fortran source input file, see the previous post titled "Hello!" which contains the source code and steps required to compile, link, and run the created executable.

The disadvantage of installing a binary gfortran executable is that you are at the mercy of the binary creator/maintainer to keep the binary up to date. I recently ran into a problem where a bug in gfortran was fixed, but the binary file was not up to date with the fix. Therefore, the next best option is to install gfortran from source code. This takes a little longer and is a little more complicated, but it will allow you to test the working development of gfortran or a version that the binary supplier does not support. That is next on the list to be added here.

I will also try to add posts that outline how to install gfortran on Ubuntu and Windows sometime.

Friday, September 3, 2010

Hello!

A Brief Introduction

First, please note that I am using Mac OS X 10.6 for these examples. The command line syntax is not very different from Linux operating systems, but does vary slightly from Windows systems. If I achieve my goal with this blog, the differences will be explained at a later time.

Also, when sections appear with a "$" character as the left-most character, it means that those commands are supposed to be entered by you on the command line (Terminal on Mac/Ubuntu), even though the command line for Mac by default prompts with something like:

user-computer:folder user$

After running an executable, any command line output that is successful will be denoted in green.

To the Fortran...

This is a simple Fortran program that will print out "Hello!" to stdout. The below code must be written in a file with a Fortran extension (use .f95). Using an editor such as Text/Edit, create a new file named:

HelloProgram.f95

and enter the following source code:

program hello
  implicit none
  print *, "Hello!"
end program hello

Close the editor to save the file to disk. In order to get the program to run, the source code must be compiled and linked using a Fortran compiler. Luckily there are a few free Fortran compilers available. Currently I am using gfortran (GNU Fortran): http://gcc.gnu.org/fortran/

More on how to install these compilers later. From the command line, compile, link, and run the program using the following commands:

$ gfortran HelloProgram.f95 -c
$ gfortran HelloProgram.o -o hello
$ ./hello
Hello!

The above can be shortened to one line (the "-o" tells gfortran to compile and link the source code):

$ gfortran HelloProgram.f95 -o hello
$ ./hello
Hello!

Test Post

Never having used Blogger or HTML before, I am still testing it out to see how easy or difficult it might be to post with source code examples. I have updated this post to reflect what I consider the easiest way to post source code with HTML. At first I was using <code> tags, but using <pre> tags is actually much easier. The latter doesn't require line breaks or leading spaces, the formatting is how you type it within the <pre> tags. One difference is that by default, <pre> tags do not wrap text, so you must define the following style to get wrapping to work properly (did this in the XML Template file):

<style type="text/css">
pre
{
white-space: pre-wrap;                 /* css-3 */
white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
white-space: -pre-wrap;                /* Opera 4-6 */
white-space: -o-pre-wrap;              /* Opera 7 */
word-wrap: break-word;                 /* Internet Explorer 5.5+ */
background: #fbfcfe;
border: 1px solid #DADCE0;
padding: 0.75em 1.5em;
}
</style>

Here is an example of what source code would look like using the <pre> tags:

program main
  implicit none
end program main

The source code for the code block above looks like the following in html:

<pre>
program main
  implicit none
end program main
</pre>

The formatting to the smaller text is automatically handled by the browser when it sees the <pre> tags (pre = pre-formatted). For completeness sake, the html source code was displayed above by replacing the "<" character with &lt; and replacing the ">" character with &gt; when typing out the HTML formatting code.

I did not anticipate learning any HTML formatting, but Blogger's "Compose" leaves something to be desired when typing out source code, which I must do if I wish to provide any Fortran examples. It will probably be easier to post using Blogger's "Edit HTML" option, in order to get the formatting to look correct.

The following links are where I found information about html:
http://www.w3schools.com
http://www.google.com/support/blogger/bin/answer.py?hl=en&answer=41954