Compile your source files with g95 as follows:
g95 -c -fPIC mylib.f
and make the objects into your own shared library:
gcc -shared -o mylib.so mylib.o
That's it! Use the library in R as follows:
> dyn.load("/path-to/libg95.so",local=FALSE)
> dyn.load("/path-to/mylib.so")
Example: ( square a vector )
myib.f:
subroutine fsquv(n, x)
integer n
double precision x(n)
integer i
do 100 i = 1, n
x(i) = x(i) ** 2
100 continue
End
After the steps above in R:
> dyn.load("/path-to/libg95.so",local=FALSE)
> dyn.load("/path-to/mylib.so")
then define a "wrapper" function in R to make it easier
squarevector <- function(x) {
if (!is.numeric(x)) stop("argument x must be numeric")
out <- .Fortran("fsquv",n=as.integer(length(x)),x=as.double(x))
return(out$x)
}
Then use your wrapper function:
> squarevector(1:5)
[1] 1 4 9 16 25
How to use mkmf with g95
- Last update: 7 March 2007
- This Howto includes contributions from Hani Andreas Ibrahim and
Mohammad Rahmani
- Further suggestions are welcome!
mkmf is a tool written in perl5 that will construct a
Makefile. It is available from <http://www.gfdl.noaa.gov/fms/pubrel/k/am2/bin/mkmf.html>.
mkmf understands dependencies in f90 (modules and use), the fortran include
statement, and the cpp #include statement in any type of source. Detailed
information on using mkmf is provided at the link provided above.
Perl 5 must be installed in order to use mkmf. If using Windows, Perl is
included in the msysDTK package. Download and install both "msys" and
"msysDTK" from www.mingw.org. Include the
mingw\bin and Msys\1.0\bin directories in your PATH.
1. Download
the mkmf script and save it somewhere in your system (e.g. c:\mingw\bin).
2. mkmf currently only distinguishes .f, .F, .f90, and .F90 source
filename extensions. If you wish to add support other Fortran extensions,
change lines 59 to 82 in the mkmf perl script as follows, otherwise skip to
step 3.
#some constants my $endline = $/; my @src_suffixes = ( q/\.F/, q/\.FOR/, q/\.F90/, q/\.F95/, q/\.F03/, q/\.c/, q/\.f/, q/\.for/,q/\.f90/, q/\.f95/, q/\.f03/ ); my @inc_suffixes = ( q/\.H/, q/\.fh/, q/\.h/, q/\.inc/ ); # push @inc_suffixes, @src_suffixes; # sourcefiles can be includefiles too: DISALLOW, 6 May 2004 my %compile_cmd = ( # command to create .o file from a given source file suffix q/.F/ => q/$(FC) $(CPPDEFS) $(CPPFLAGS) $(FFLAGS) -c/, q/.FOR/ => q/$(FC) $(CPPDEFS) $(CPPFLAGS) $(FFLAGS) -c/, q/.F90/ => q/$(FC) $(CPPDEFS) $(CPPFLAGS) $(FFLAGS) -c/, q/.F95/ => q/$(FC) $(CPPDEFS) $(CPPFLAGS) $(FFLAGS) -c/, q/.F03/ => q/$(FC) $(CPPDEFS) $(CPPFLAGS) $(FFLAGS) -c/, q/.c/ => q/$(CC) $(CPPDEFS) $(CPPFLAGS) $(CFLAGS) -c/, q/.f/ => q/$(FC) $(FFLAGS) -c/, q/.for/ => q/$(FC) $(FFLAGS) -c/, q/.f90/ => q/$(FC) $(FFLAGS) -c/, q/.f95/ => q/$(FC) $(FFLAGS) -c/, q/.f03/ => q/$(FC) $(FFLAGS) -c/ ); my %delim_match = ( q/'/ => q/'/, # hash to find include file delimiter pair q/"/ => q/"/, q/</ => q/>/ ); #formatting command for MAKEFILE, keeps very long lines to 256 characters
|
3. Create a template file for your compiler to adapt to user
environment.
Below is an example template file for g95:
FC = g95 LD = g95 FFLAGS = -I c:/mingw/include LDFLAGS = $(LIBS) # add your libraries after LIBS= , e.g. -lblas LIBS = c:/mingw/lib
|
Give a name to the above template file (e.g. g95_args) and save
it as c:\mingw\bin\g95_args
4. The syntax for using mkmf is:
mkmf [-a abspath] [-c cppdefs] [-d] [-f] [-m makefile] [-p program] [-t template] [-v] [-x] [args]
- -a abspath attaches the abspath at the front of all
relative paths to source files;
- cppdefs is a list of cpp #defines to be passed to
the source files: affected object files will be selectively removed if
there has been a change in this state;
- -d is a debug flag to mkmf (much more verbose than
-v, but probably of use only if you are modifying mkmf itself);
- -f is a formatting flag to restrict lines in the
makefile to 256 characters. This allows editing makefiles using vi. Lines
longer than that will use continuation lines as needed;
- makefile is the name of the makefile written
(default Makefile);
- template is a file containing a list of make macros
or commands written to the beginning of the makefile;
- program is the name of the final target (default
a.out, change to a.exe for Windows systems);
- -v is a verbosity flag to mkmf;
- -x executes the makefile immediately;
- args are a list of directories and files to be
searched for targets and dependencies.
5. Usage
5.1. To use mkmf with the g95 compiler to create a Makefile, go to the
directory containing your source files and issue the following command:
Perl c:\mingw\bin\mkmf -t c:\mingw\bin\g95_args
5.2. To create a makefile and execute it immediately with an optional
target name, do as follows:
mkmf -t c:\mingw\bin\g95_args -p myproj.exe -x
Here "myproj.exe" is the name of the final target (output)
6. To make using mkmf much easier, create a script file (e.g. mkmf-g95.bat
on Windows) and put it in c:\mingw\bin. (Make sure you have this in your
PATH.) The script file should contain the following (modify to fit your
setup):
Echo off REM This file can be called from anywhere to create a Makefile REM based on the Fortran source files in that location. REM a.exe is the name of final output. Change it to fit your needs. REM If ignored, the mkmf script will use a.out by default. REM g95_args is the name of a template file used to set user REM environment variables. Change it as you wish. REM REM Perl c:\mingw\bin\mkmf -t c:\mingw\bin\g95_args -p a.exe If (%1)==() ( Perl c:\mingw\bin\mkmf -t c:\mingw\bin\g95_args -p a.exe ) Else ( Perl c:\mingw\bin\mkmf -t c:\mingw\bin\g95_args -p %1 ) REM The above allows the target name to be passed as an argument
|
In the above code, if you pass a target name as an argument to
mkmf-g95.bat, it will generate a Makefile with the target name as its output.
Otherwise the script will produce a Makefile with a.exe as the output target.
7. Change a.out to a.exe [Only on Windows machines]. The mkmf script
creates Makefiles with a.out as the target, which is not suitable for Windows
so it is necessary to modify the mkmf to produce "a.exe" as the default output
instead of "a.out". To do this, open the mkmf script file in a text editor
and replace all a.out with a.exe.
How to use Photran with G95
- Last update: 7 March 2007
- This install guide has been tested successfully with Photran 3.1 Beta
2 for Windows
- This Howto includes contributions from Hani Andreas Ibrahim
(25/01/2007) and Mohammad Rahmani (9/02/2007)
- The g95 compiler, and Perl should be in your PATH
- Further suggestions for improvements and/or corrections are welcome!
Now you can develop your Fortran programs with g95 using Photran, a full featured Fortran
IDE based on Eclipse. It is available for both Windows and Linux systems.
This is a summary of the steps required to setup Photran and g95 on a Windows
XP system.
1. If you use MinGW it is useful to download and install "msys" and
"msysDTK" from www.mingw.org. The Msys bin
directory has to be in your path.
2. Generating a g95 Makefile automatically
(Skip this section if you don’t want to use mkmf for auto creation
of Makefile)
Set up mkmf by carefully following all the directions provided in
How to use mkmf with with g95.
If your paths are different from those used in the
install instructions make appropriate adjustments to the paths used in the
following steps.
In order to use mkmf in Photran, Perl 5 must be installed. If using
Windows, Perl is included in the msysDTK package from www.mingw.org.
2.1. Open Photran and click the menu "Run -> External tools ->
External tools..."
2.1.1. On the Main Tab fill out the form as
follows:
Name: g95 make Makefile
Location: c:/mingw/bin/mkmf-g95.bat
Working directory: ${container_loc}
Arguments: ${project_name}
Note: The file c:\mingw\bin\mkmf-g95.bat is
the batch file for running the mkmf Perl script described in Section 6 of
How to use mkmf with with g95
2.1.2 Refresh Tab
For refresh tab fill the form as follows:
[x] Refresh resources upon completion
(x) The folder containing the selected
resources
[x] Recursively include sub-folders
2.1.3. Common Tab
(x) Local file
Display in favorite menu
[x] External Tools
[x] Launch in background
2.1.4. Click "Apply"
2.2 On the Photran toolbar click the menu "Run -> External tools
-> External tools..."
2.2.1 Start a new configuration.
2.2.2 On the Main Tab fill out the form as
follows:
Name: g95 build
Location: c:/msys/1.0/bin/make.exe
Working directory: ${container_loc}
Arguments: all
2.2.3 Fill out the Refresh Tab as in 2.1.2
and the Common Tab as in section 2.1.3
2.2.4 Click "Apply"
2.3 On the Photran toolbar click the menu "Run -> External tools
-> External tools..."
2.3.1 Start a new configuration.
2.3.2 On the Main Tab fill out the form as
follows:
Name: Run
Location: ${container_loc}/${resource_name}
Working directory: ${container_loc}
Arguments:
2.3.3 Fill out the Refresh Tab as in 2.1.2
and the Common Tab as in section 2.1.3
2.3.4 Click "Apply"
3. Click on menu Windows->Preferences->C/C++->Make->Makefile
Editor->New Make Projects.
Choose Tab 'Binary Parser'. Enable 'PE Windows
Parser' and disable all others. Click on the OK button.
4. Setup is done.
To edit, compile and run Fortran programs do the following:
1. File->New->Standard Make Fortran Project. Give a name to your
project in the 'Project Name' field. Click 'Finish'.
2. Make sure that your project name is highlighted in the 'Fortran
Projects' or 'Navigator' frame on the left. Click File->New->Source
File. Fill out the 'Source File' field (e.g. myprogram.f90). Click
'Finish'.
3. Type your code in the new editor tab and save it (File->Save)
4. Add further source files if necessary.
5. Click on one of your source files in the Navigator or Fortran projects
frame.
6. Create Makefile
6.1. If you have set up mkmf (see section 2) do as follows:
6.1.1. Click menu "Run->External tools->External tools ..."
6.1.2. Choose (e.g.) "g95 make Makefile" and click the "Run" button.
In the Navigator/Fortran Projects frame you should see the "Makefile"
file in your project section. (After the first run of "g95 make Makefile"
it will appear in the root section of the External tools submenu or
toolbar icon for easy invocation.)
6.2. If you have not set up mkmf on your system you will need to code
the Makefile yourself
7. Select one of your source files in the Navigator/Fortran Projects frame
again.
8. Click menu "Run->External tools->External tools ..." and select
"g95 build".
9. After compilation you should see "myprogram.exe" in your
Navigator/Fortran Projects frame.
10. Select "myprogram.exe" in the Navigator/Fortran Projects frame,
then click on the menu "Run->External tools->External tools ..." and
select "Run". The program output will display in the Console.
For information on setting up Photran with Cygwin, see: ftp://ftp.swcp.com/pub/walt/F/photran.pdf
How to configure LAPACK for g95
The only change that needs to be made to correctly build LAPACK for
x86 machines is to set the NOOPT macro in make.inc:
NOOPT=-ffloat-store
This prevents optimizations from interfering with the subroutines that
are trying to query various characteristics of the floating point
unit.
How to compile ARPACK with g95 (Guglielmo Pasa)
Use ARmakeg95.inc and Makefileg95.make and OBjectg.make in this
tarball to build a static and a dynamic library
for arpack.
How to configure MPICH2-1.0.1 with g95 (Guglielmo Pasa)
For bash/sh, you can use:
alias f77=g95 -ffixed-form -fno-underscoring
alias f90=g95 -ffree-form -fno-underscoring
or for csh/tcsh:
Setenv F90
Setenv F77
Setenv CC
Verify that it detects the right compilers and then run
$ ./configure --prefix=/usr/local --enable-f77 --enable-f90 \
--with-pm=smpd --with-pmi=smpd
$ make
$ make install
Try some tests. You can use the MPICH2 WinXP service binary distro
found on the MPICH2 site with it.
An alternate procedure (Patti Michelle) is to configure with:
$ F77=/path/to/g95 F90=/path/to/g95 ./configure
$ make
$ make install
How to configure BLACS with g95 (Guglielmo Pasa)
If BLACS source tree is in $(HOME)/BLACS Edit Bmake.inc and change
BTOPdir=$(HOME)/BLACS
COMMLIB = MPI
PLAT = g95xp # for example
MPIdir = /usr/local # root installation directory of MPICH
MPILIBdir = $(MPIdir)/lib
MPIINCdir = $(MPIdir)/include
MPILIB = -L$(MPILIBdir) -lmpich -lpmpich -lmpich -lcrypto
....
INTFACE = -DNoChange # leaves symbols of C objects the same form as g95 ones
# i.e. no underscores and lower case
...
TRANSCOMM = -DCSameF77
...
F77 = g95
F77NO_OPTFLAGS = -ffixed-form -fno-underscoring
...
RANLIB = ranlib
Once this is done go to SRC/MPI and modify Makefile
In
Clong = ...
If you are on windows, change all the .C extensions with .Z as windows
isn't case sensitive to copy/write files, so even if cygwin is case
sensitive sometimes it fails to distinguish .C and .c
...
Cintobj = $(comm.:.o=.Z) $(supp:.o=.Z) $(Clong)
...
clean:
rm -f ... $(long:.o=.Z)
...
Cblacs_gridinit_.C : etc change all extension .C to .Z
...
.SUFFIXES: .o .Z
.c.Z:
$(CC) -o C$*.o -c $(CCFLAGS) $(BLACSDEFS) -DCallFromC $<
mv C$*.o $*.Z
Then go to the root of BLACS and do
make mpi
and install libraries by hand.
How to configure SCALAPACK with g95 (Guglielmo Pasa)
Edit and modify SLmake.inc
home = $(HOME)/SCALAPACK # where is the SCALAPACK source tree
PLAT = g95xp # just for coherence in choices ;o)
BLACSdir = /usr/local/lib # where BLACS has been put
USEMPI = -DUsingMpiBlacs
SMPLIB = -L/usr/local/lib -lmpich -lpmpich -lmpich -lcrypto
...
F77 = g95 -ffixed-form -fno-underscoring
...
CDEFS = -DNoChange $(USEMPI)
...
RANLIB = ranlib
# I use LAPACK3 and its BLAS distro as ATLAS has bad performance (see below)
# _sf77 stands for s=static (I have _f77.dll too) and f77 for
...f77(aka 95)
BLASLIB = -L/usr/local/lib -lblas_sf77
# it's only necessary for tests apps
Modify the Makefile:
PLAT = g95xp
BLASLIB = -L/usr/local/lib -lblas_sf77
RANLIB = ranllib
FORTRAN = g95 -ffixed-form -fno-underscoring
CDEFS = -DNoChange
Edit and modify UMFPACKv4.4/AMD/Make/Make.include
F77 = g95
F77FLAGS = -ffixed-form -fno-underscoring
...
CONFIG = -DBLAS_NO_UNDERSCORE
...
include ../Make/Make.cygwin
Next, still in AMD/Make copy Make.linux to Make.cygwin and modify
CC = gcc
CFLAGS = -O2
...
LIB = -L/usr/local/lib -lblas_sf77 -lf95 # you have to copy libf95.a to /lib
# or /usr/local/lib first !! It is needed by gcc on linking with C progs
Next go to AMD/ and run make (if you want to use matlab also, I don't
know how to include it correctly yet ! so disable it or you'll get an
error after the library libamd.a has been built. If you just want the
library that's fine! the error just appears as make doesn't find mex)
Next go to UMFPACK and run
make hb
that builds libumfpack.a and the hb example. Next you can copy the
libraries to /usr/local/lib and the include files to /usr/local/include.
Edit Makefile.in
RANLIB = ranlib
Run make and copy libmetis.a to /usr/local/lib/libmetis-4.0.a.
Copy Makefile.inc.generic and Makefile.inc.generic.SEQ from Make.inc/
to the root of MUMPS source tree as Makefile.inc and Makefile.inc.SEQ
respectively and edit Makefile.inc (parallel version, edit SEQ
accordingly with MPI indications cited in the file)
LMETISDIR = /usr/local/lib
LMETIS = -$(LMETISDIR) -lmetis-4.0
ORDERINGS = -Dmetis -Dpord
ORDERINGSF = -Dpord -Dmetis
...
FC = g95
RANLIB = ranlib
SCALAP = -lscalapack \
/usr/local/lib/blacs_MPI-g95xp-0.a \
/usr/local/lib/blacsF77init_MPI-g95xp-0.a \
/usr/local/lib/blacs_MPI-g95xp-0.a
# INCLUDE DIRECTORY FOR MPI
INCPAR = -I/usr/local/include
# LIBRARIES USED BY THE PARALLEL VERSION OF MUMPS: $(SCALAP) and MPI
LIBPAR = $(SCALAP) -L/usr/local/lib -lmpich -lpmpich -lmpich -lcrypto
# DEFINE HERE YOUR BLAS LIBRARY
LIBBLAS = -lblas_sf77
CDEFS = # empty !!
#COMPILER OPTIONS
OPTF = -O -fno-underscoring
Download the required files (look in get_packages/netlib_mail to see
which one are required, pay attention that dblas1.f etc. are not real
sources so you have to look inside those files to see which one are
really needed) and put them in get_packages. Make rip.exe and run the
script process_netlib.
Edit lib/Makefile.template
COMM = SERIAL
MACHINE = GENERIC
MPI_INCLUDE_DIR = -I/usr/local/include
MPI_LIB = -L/usr/local/lib -lmpich -lpmpich -lmpich -lcrypto -lm
...
CC_TFLOP = gcc
FC_TFLOP = g95
AR_TFLOP = ar
RANLIB_TFLOP = ranlib
TIME_TFLOP = md_timer_sun.c
CFORT_TFLOP = -Dmatched
CFLAGS_TFLOP = -O2
FFLAGS_TFLOP = -O2 -ffixed-form -fno-underscoring
The end of Makefile.template should look like:
aztec: $(OBJ)
@echo "Building library $(TARGET)"
@rm -f libaztec$(COMM).a
$(AR) ruv libaztec$(COMM).a $(OBJ)
@$(RNLIB) libaztec$(COMM).a
The first few lines of Makefile.template should be:
1) for an MPI library
COMM = MPI
MACHINE = TFLOP
MPI_INCLUDE_DIR = -I/usr/local/include
MPI_LIB = -L/usr/local/lib -lmpich -lpmpich -lmpich -lcrypto -lm
2) for a SERIAL library
COMM = SERIAL
MACHINE = TFLOP
MPI_INCLUDE_DIR =
MPI_LIB =
Don't change anything else. Do the same in app/
./set_makefiles
cd lib
make
cp libaztec.a /usr/local/lib
cp *.h /usr/local/include
cd ..
cd app
make
Run the demos.
MATLAB provides an easy way of incorporating C or Fortran code. The
programmer must write a small subroutine called a gateway which passes
variables back and forth between C/Fortran and MATLAB. The code is
then compiled, usually using the MATLAB command 'mex', which calls an
external compiler. The compiled code can then be used exactly like a
MATLAB function. Details on creating mex files can be found in the
help files.
Although G95 is not an officially supported compiler, it appears to
work fine. The Fortran compiler is specified in the mexopts.sh file,
which is stored in [MATLAB root]/bin, or in a local copy kept
somewhere in ~/.matlab. To use G95, open mexopts.sh and go the section
relevant for your OS. Change the variable 'FC' to specify the full
path to G95. Also, it is necessary to link to libf95.a or MATLAB will
crash when attempting to use certain intrinsics, so add that to the
'FLIBS' variable. The other variables should be set to whatever
additional flags and optimization options you want. As an example, on
Mac OS X and MATLAB 7, the relevant section of mexopts.sh looks like:
FC='/usr/local/g95-install/bin/powerpc-apple-darwin6.8-g95'
FFLAGS='-ffree-form'
FLIBS='-L/usr/local/g95-install/lib/gcc-lib/powerpc-apple-darwin6.8/4.0.0/ -lf95'
FOPTIMFLAGS='-O'
FDEBUGFLAGS='-g'
Notes
- The simplest way to pass data to and from MATLAB is through the
%VAL() construct or the F2003 VALUE attribute.
- I have only tested G95 with MATLAB 7 and Mac OS X, so there are
probably things I've missed. Information on using MATLAB and other
Fortran90 compilers is on the web and may be helpful.