;; add g95 to compilation mode (eval-after-load "compile" '(setq compilation-error-regexp-alist (cons '("^In file \\(.+\\):\\([0-9]+\\)" 1 2) compilation-error-regexp-alist)))This will allow emacs to find the next error message in compile mode.
After installing g95 find the file "libf95.a". Copy this to an empty subdirectory and extract all of the objects: "ar -x libg95.a". Then create a shared library: "gcc -shared -o libg95.so *.o". It is useful to put the file "libg95.so" somewhere where users can make use of it (i.e. /usr/local/lib)
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 EndAfter 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
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 |
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/libGive 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_args5.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 -xHere "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 |
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.
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.
(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.
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
NOOPT=-ffloat-storeThis prevents optimizations from interfering with the subroutines that are trying to query various characteristics of the floating point unit.
alias f77=g95 -ffixed-form -fno-underscoring alias f90=g95 -ffree-form -fno-underscoringor for csh/tcsh:
Setenv F90Verify that it detects the right compilers and then runSetenv F77 Setenv CC
$ ./configure --prefix=/usr/local --enable-f77 --enable-f90 \ --with-pm=smpd --with-pmi=smpd $ make $ make installTry 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
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 = ranlibOnce 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 $*.ZThen go to the root of BLACS and do
make mpiand install libraries by hand.
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
PLAT = g95xp BLASLIB = -L/usr/local/lib -lblas_sf77 RANLIB = ranllib FORTRAN = g95 -ffixed-form -fno-underscoring CDEFS = -DNoChange
F77 = g95 F77FLAGS = -ffixed-form -fno-underscoring ... CONFIG = -DBLAS_NO_UNDERSCORE ... include ../Make/Make.cygwinNext, 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 progsNext 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 hbthat 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.
RANLIB = ranlibRun make and copy libmetis.a to /usr/local/lib/libmetis-4.0.a.
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
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-underscoringThe 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).aThe 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 -lm2) 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 makeRun the demos.
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
patch -l < gnumex.patchIn Matlab add the path C:\Gnumex
>>path(path,'C:\Gnumex')Call "gnumex" in Matlab. Select Mingw for linking and G95 in the "language for compilation?" drop box. If C:\MinGW is not your MinGW root directory adjust the "Mingw root path". See for more information gnumex.sourceforge.net Comments on Gnumex and MinGW: The versions of gcc-3.4.4 and gcc-3.3.3 and lower works fine, but I get for the versions 3.4.0, 3.4.1 and 3.4.2 and the compiler setting pentium4 bad results. The cause is the compiler optimization -mfpmath=sse. I added the setting "pentium4 -f" which has this optimization removed.
touch /opt/modulefiles/g95 chmod a+r /opt/modulefiles/g95 vi /opt/modulefiles/g95
#%Module################################################# # http://ftp.g95.org/g95-mips-irix.tgz default modulefile # IRIX version Fri Jul 21 21:14:30 EDT 2006 # proc ModulesHelp { } { puts stderr "The g95 modulefile defines the default path needed to use #g95" puts stderr "Type \"module load g95\" to load g95 and then" puts stderr "\"module list\" to verify that the g95 modulefile is #loaded." } set _module_name [module-info name] set is_module_rm [module-info mode remove] set G95_CURPATH /opt/g95/bin prepend-path PATH $G95_CURPATH
cd /usr/tmp mkdir -p /opt/g95/bin mkdir -p /opt/g95/lib/gcc-lib/mips-sgi-irix6.5/4.0.1 gunzip -c g95-mips-irix.tgz | tar -xvf - cd g95-install/bin mv mips-sgi-irix6.5-g95 /opt/g95/bin cd ../lib/gcc-lib/mips-sgi-irix6.5/4.0.1 mv * /opt/g95/lib/gcc-lib/mips-sgi-irix6.5/4.0.1 cd /opt/g95 chown -R nobody.nobody . * chmod a+rx . bin cd bin ln -s mips-sgi-irix6.5-g95 g95 cd ../lib/gcc-lib chmod a+rx . .. cd mips-sgi-irix6.5/4.0.1 chmod a+rx . .. * chmod a-x *.a *.o *.1
source /opt/modules/modules/init/csh module load modulesat a prompt or in their .cshrc or .tcshrc; see
grelnotes modules &and click on "Next Chapter" for more info. Once you have modules working on the machine, the users should be able to go straight to
module load g95once they log in and then g95 will be ready:
g95 -O3 -s program.f -o program ./program
fc77=g95 fc90=g95
command.compile.*.f=$(fc77) -I.\ -L.\ $(1) $(2) -c $(FileNameExt) $(3) $(4) -info command.compile.*.for=$(fc77) -I.\ -L.\ $(1) $(2) -c $(FileNameExt) $(3) $(4) -info command.compile.*.f90=$(fc90) -I.\ -L.\ $(1) $(2) -c $(FileNameExt) $(3) $(4) -info command.compile.*.f95=$(fc90) -I.\ -L.\ $(1) $(2) -c $(FileNameExt) $(3) $(4) -info
subroutine foo(n, a) integer, intent(in) :: n real, intent(out) :: a(n,n) end subroutine fooFirst method: using ctypes:
g95 -shared -fPIC foo.f95 -o foo.soand check the name of the routine's symbol
nm foo.so | grep fooThis name will be foo_ if foo is an external procedure, but will be bar_MP_foo_ is foo in contained in a module bar. You can use the BIND(C) feature to control the symbol's actual name.
from numpy import * from ctypes import c_int, POINTER, byref foolib = ctypeslib.load_library('foo', '.') _foo = foolib.foo_ # or _foo = foolib.bar_MP_foo_ _foo.restype = None def foo(n): a = zeros((n,n,), dtype=float32, order='FORTRAN') arg = ctypeslib.ndpointer(dtype=float32, shape=(n,n,), flags='FORTRAN') _foo.argtypes = [POINTER(c_int), arg] cn = c_int(n) _foo(byref(cn), a) return a a = foo(4)
f2py -c --help-fcompilerto check that f2py recognizes g95.
f2py foo.f95 -m foo -h foo.pyfIn more complicated examples, you might have to manually adjust foo.pyf.
f2py -c --fcompiler=g95 foo.pyf foo.f95Here, if you omit the --fcompiler flag then f2py uses the default compiler as determined by the _default_compilers variable in the file numpy/distutils/fcompiler/__init__.py (currently g77).
from numpy import * import foo print foo.foo.__doc__ # shows python interface generated by f2py a = foo.foo(4)
# Example build script for Scons in Msys/MinGW |
FC = f95
$(FC) -c -fpp -D$(PREC) $<
FC = g95
$(FC) -c -cpp -D$(PREC) $<