Pages

Affichage des articles dont le libellé est fortran. Afficher tous les articles
Affichage des articles dont le libellé est fortran. Afficher tous les articles

jeudi 1 mars 2012

array index starting from 1 for fortran

end of file for a file created by linux command and fortran

If we type:
echo "a b c d " > tt

Then emacs tt, we will find that the end of file is not in the line of "a b c d", instead in the next line.

It is the same when we save the results of grep or cat with the symbol >

If we read such a file with fortran, we found that the number of lines in the file is 1 line more than the actual line number (from wc -l) !!  We need pay attention to this difference if we want to read the file using fortran.

If we read the file by fortran:
allocate(data(realsize))
iEof = 0
i = 1
DO while ( iEof == 0)
     read(iu, *, IOSTAT=iEof)  temp_data
     data(i) = temp_datga
     i = i + 1
Enddo
close(iu)

This would cause memory problem:
* glibc detected free(): invalid next size (normal): 0x000000000103dd90 **. 
Invalid next size" means that glibc has detected corruption in the memory arena.


This is because : data is allocated with realsize, but when we read the file, there are one more line (the last line is blank) is read and the blank line overwrites the good information already stored to the variable "data". This cause the problem !!! The corrected code should be as follows:



allocate(data(realsize))
i = 1
DO while ( i <= realsize)
     read(iu, *, IOSTAT=iEof)  temp_data
     data(i) = temp_datga
     i = i + 1
Enddo
close(iu)

jeudi 23 février 2012

ifort compilation command

Compiling and Linking a Single Source File

The following command compiles x.for, links, and creates an executable file. This command generates a temporary object file, which is deleted after linking:
ifort x.for
To specify a particular name for the executable file, specify the -o (Linux* OS and Mac OS* X) or /exe (Windows* OS) option:
ifort x.for -o myprog.out (Linux OS and Mac OS X) ifort x.for /exe:myprog.exe (Windows OS)

Compiling, but not Linking, a Source File

The following command compiles x.for and generates the object file x.o (Linux OS and Mac OS X) or x.obj (Windows OS). The c option prevents linking (it does not link the object file into an executable file):
ifort -c x.for (Linux OS and Mac OS X) ifort x.for /c (Windows OS)
The following command links x.o or x.obj into an executable file. This command automatically links with the default Intel Fortran libraries:
ifort x.o (Linux OS and Mac OS X) ifort x.obj (Windows OS)

Compiling and Linking Multiple Fortran Source Files

The following command compiles a.forb.for, and c.for. It creates three temporary object files, then links the object files into an executable file named a.out (on Linux OS and Mac OS X) or a.exe(Windows OS):
ifort a.for b.for c.for
When you use modules and compile multiple files, compile the source files that define modules before the files that reference the modules (in USE statements).
When you use a single ifort command, the order in which files are placed on the command line is significant. For example, if the free-form source file moddef.f90 defines the modules referenced by the file projmain.f90, use the following command:
ifort moddef.f90 projmain.f90


Note: this post is from http://software.intel.com/sites/products/documentation/hpc/compilerpro/en-us/fortran/lin/compiler_f/bldaps_for/common/bldaps_exifort.htm.

mercredi 24 août 2011

Segmentation error in fortran

Recently I work a lot with Fortran 90 coding and sometimes will get the segmentation errors. This type of error is often related to the management of arrays, because fortran has strict requires to the array size, intialisations, incorrect index etc. Here I summarize the situations that I have met:

1) array initialisation missing
For example, if one uses an array variable without an intialisation.

2) the allocated size and actual size is different
This is often the problem, and one need to spend a lot of time to locate which variable, the allocated size and the actual size

Sometimes if a part of an array is defined and the other part is not given value (only with allocation), then the system may give some huge values to those undefined part in the array, which cause the problem. The safest way is to set the array to, eg., 0.0 if it is real/integer....just after the allocation.

3) Deallocate the array before allocating it, which is always a safe way.

4) If array A has size a1 and array B has size b1,and a1 > b1; if we want to give values of array A by using the array B, one can not simple A(:)= B(:), because they have different size and the compiler may not know how to designate values for indices of A which do not exist in B.

To be safe, we need specify in A which indices are equal to B. For example :

A(:) = 0
A(1:b1) = B(:)


5) Sometimes, even the initial values are set to 0 (for example) for an array A and array B, B has some given values from calculations, there could be errurs if one tries to define the values of A with B, ie., A = B. The undefined values of B (even it is initialized before calculation), supposed to be 0, will become large values in the corresponding position of A. I got this problem when I tried to combine two arrays into one. I have to manually set the value ! Hope to find better way !



In summary, in order to avoid the segmentation errors in fortran 90:
1) Always define the size of array correctly
2) Always initialize the array before getting values from other parts of the program
3) Always give values to the array at the correct corresponding indices

mardi 23 août 2011

install ifort in x86_64 in Linux system

In mandriva 9.0:

1)download Intel fortran composer XE for linux
2) register in the intel website to receive the series number
3) In the installation script, if the computer cpu is x86_64, the architecture will be 32e; if the cpu is ia64, then the architecture will be 64; otherwise the architecture will be 32.

4) check if the depencies exists
"rpm -q glibc" checks that glibc package is installed.
"rpm -q libgcc1" checks that libgcc is installed.
"rpm -q libstdc++6" checks that libstdc++6 is installed.
Those three packages are part of a basic distribution, so they should not be missing. "rpm -q gcc" checks if gcc is installed. If it is not, you should install the gnu c compiler. "rpm -q libstdc++5" checks that libstdc++5 is installed, if it is not, install the package. If you want to double check everything, "/sbin/ldconfig -p | grep libstdc++" should give you files in both /usr/lib and /usr/lib64 with both versions of 5 and 6 of those libraries (4 files total). "/sbin/ldconfig -p | grep libgcc" should give you files (links to files) in /lib, /lib64, /usr/lib and /usr/lib64. If you want to check all the packages are 64-bit then the commands:
rpm -q --queryformat %{ARCH} libgcc1
rpm -q --queryformat %{ARCH} glibc
rpm -q --queryformat %{ARCH} libstdc++6
rpm -q --queryformat %{ARCH} libstdc++5
rpm -q --queryformat %{ARCH} gcc
should all give you the answer "x86_64".

If a lib not found, install it before the ifort installation.

5) go to /etc/mandriva-releases:
cp it to mandriva-release-original
open the file and modify the mandriva version to 2007.0, because it works with ifort.

6) use ./install.sh in the ifort package and install it following the instructions

7) source /opt/intel/composerxe-2011.5.220/bin/compilervars.sh intel64
this will allow the command "ifort" working in the terminal

8) change back the mandriva release to the original form.

9) it should be work


In ubuntu:
1) dpkg -s gcc:
searching if the gcc is installed in the computer, and is found
2) dpkg -s glibc: not found ,
but dpkg -s  libstdc++6: is found, which should work as well....
3) the architecture of computer is 32bit, and this is why in the installation directory:
/opt/intel/composer_xe_2011_sp1.11.339/bin/,
there is a folder ia32 created by the installation process, not intel64

4) source /opt/intel/composer_xe_2011_sp1.11.339/bin/compilervars.sh   ia32


in OpenSuse:
1) rpm -q gcc: gcc-4.7-2.1.1.x86_64, which uses libstdc++.so.6.
libstdc++.so.5 is included in gcc3.3.

rpm -q glibc: glibc-2.15-22.17.1.x86_64
rpm -q libstdc++5----not found

2) downloaded the recent ifort: l_fcompxe_2013.2.146

3) ./install.sh
---- there is a message 'Unsupported OS system', but I continue anyway

3) the installation is finished without other problems.

4) source /opt/intel/composer_xe_2013.2.146/bin/compilervars.sh intel64

for X86-64, it works with both 32 or 64 bit architecture.

mardi 2 août 2011

IFPORT: ifort portable module for linux

To know which version of fortran is used:
which ifort---------->/opt/intel/fce/9.1.037/bin/ifort

TO know the libs included in the ifortran:
go to /opt/intel/fce/9.1.037/include/
There are modules like IFPORT, IFLPORT, IFLPOSIX, iosdef, etc.

The IFLPORT is for the version 7.0 and IFPORT for version 8.0 and up. The IFPORT module is a portable libarary for ifort, which contains the routines not available in intel fortran, ie., the routines useful on linux system. For example, the subroutines of chdir, getcwd etc. The detailed documentation about this module and its routines are in : http://www.slac.stanford.edu/comp/unix/package/intel_tools/ifort/mergedProjects/lref_for/

To use this module, one must add the following line in the fortran code:
USE IFPORT