Tuesday, April 29, 2008

Fortran plus MPI in OSX Tiger

Finally I've got the fortran/mpi stuff running in my macstation. The installation process is very complicated, and so I will be describing it here. First of all, the physics software that we must execute is OpenPIC2, and it must be installed once the running environment is set up.

We need a couple of things in order to run OpenPIC2: we need the fortran compilers up and running, and also an implementation of MPI (the official documents about MPI are available here). To install all that stuff, we'd better use some package installation manager. I recommend two packaging tools: MacPort and Fink. I had to install both, as I need fink to get g77, and I need port to get g95. You can download fink here, and Mac Ports here. Installing these programs in an apple computer is very simple.

Ok, now that we have the package managers up and running, it is time to install the compilers. Let's start with g95:

sudo port -v selfupdate
port -d install g95

This should install g95 in your machine. Actually, there is some other stuff that is pretty useful, and I guess we should install it right now. The first software is rpm, to compile the sources of openmpi, and the second is stow, which helps to organize the software installed.

sudo port -d install rpm
sudo port -d install stow

That should do with the port part of the installation routine. Now, let's get g77, and to do this, we use fink:

fink install g77

It is as easy as this. Now, you probably have two different compilers running in your machine. Fink puts stuff in /sw/bin. You'd better add it to your path. To add it permanently to your path, you can edit /etc/profile.
Good, good. Now comes the turing test for the faint of heart: installing openmpi from source. Actually, you can install it using port, but them it will not enable mpif90, which we need to compile stuff. You can get the source rpm from the openmpi site. I chose openmpi-1.2.6-1.src.rpm. Anyway, download it to your machine, and then do

rpm -i openmpi-1.2.6-1.src.rpm
cd /opt/local/src/macports/SOURCES
sudo tar -jxf openmpi-1.2.6.tar.bz2
cd openmpi-1.2.6
export CXX=g++
export CC=gcc
export F77=g77
export FC=g95
./configure
sudo make
sudo make install prefix=/usr/local/stow/openmpi-1.2.6
cd /usr/local/stow/
sudo stow openmpi-1.2.6

These commands should get openmpi running in your system. To test it, type mpif90 in your command prompt. If this does not report any error message, you can just throw some fireworks; otherwise, I recommending sitting and crying.

To compile the physics software, download this tar ball, and open it somewhere in your machine. You might have to edit your LAM.make file. The one that I have uses the following options:

MPIFC = mpif90
FC90 = g95
FC77 = g77
OPTS90 = -O3 -fno-second-underscore
OPTS77 = -O3 -fno-second-underscore
MOPTS =
LOPTS =
LEGACY =

In order to compile the first example, try:

make -f LAM.make lesopenpic2f77.out

You will probably get a lot of warnings, but do not let them scary you away. To run the program, try:

mpirun -n 2 esopenpic2

You will get a bunch or numbers. Do not ask me what are they about, but it looks right from my 600m distance.

Saturday, April 19, 2008

A little of Erlang

I have started doing some toy programs with Erlang. That is a functional, dynamically typed language that provides support for implementing parallel and distributed systems. It is very simple to write a small distributed application running on the same machine; however, I had problems to find information about this on the web. Thus, I decided to write this little tutorial which uses examples from the book Concurrent Programming in Erlang by Joe Armstrong.

This is the code of a server application, that allows users to make deposits, withdraws and enquires. The client application is given here. I tested these programs in a CentOS linux kernel 2.6.18-53.1.14.el5 running on a 64 bit Intel(R) Xeon(R) CPU. My interpreter is Erlang (BEAM) emulator version 5.5.2 [source] [64-bit] [async-threads:0].

To run these examples, open two command shells, that I shall call server shell and client shell. In the server shell, change to the directory that contains bank_server and start the erlang prompt:

erl -sname bank

Once in the erlang prompt, type the following commands:

c(bank_server.erl).
bank_server:start().

Likewise, start the command prompt in the client shell:

erl

In order to test if the server is reachable, in the client shell you can ping the server erlang node:

net:ping(bank@Tuvalu).

If you get pong as answer, then everything is fine. But, if there is any error, you will get the atom pang instead. I really think this is a bad interface for reporting errors, but, Alas, it is not my decision... Anyway, here, Tuvalu is the short name of the machine where I was running this example, and bank is the node name, determined in the command prompt when starting erlang in the server shell. Once in the erlang prompt, one can access the bank server using the interface provided by the client application. A simple example section is:

bank_client:deposit(fernando, 100).
bank_client:ask(fernando).
bank_client:withdraw(fernando, 50).
bank_client:ask(fernando).