Pages

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

jeudi 1 mars 2012

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

mercredi 21 juillet 2010

strace, debug and segmentation error

I have a code in C and it compiles . I used the compiled executive (eg., a.o) as a command and use it in a bash script. But there is an segmentation error when I run the script sometimes. I tried to find out why.

0) This is my bash script. It use the a.o and input binary data, and output the text file. I wrote this code in order to process a lot of binary data files automatically and save the results in different text files.

#!/bin/bash

function usage {
echo 'usage: ./readIAP0_all [OPTIONS] [inputpath] [outputpath]'
echo 'This script read all the binary N0 data into text data'
echo 'options:'
echo '-h display help and exit'
}
while getopts ":h" opt;do
case $opt in
h):
usage
exit 0
;;
esac
done

files=$(ls $1/*DAT)
for i in $files;do
echo $i
infile=${i##*/}
basename=${infile%%.*}
newname=${basename%%.*}.txt
outfile=$2/$newname
echo $outfile
a.o $i >$outfile
echo 'processed the file $i'
done


1) first, in the script code, I tried to add the debug option when I call a.O:
a.o --debug >$outfile

Then run the script again, and it is obvious that the problem is not of bash script, instead of a.O

2) Then I made a simple test. I use the a.o and one binary file directly, and launch it with Stace. Strace can trace system calls and signals. It can runs the command until it exists. I launched the code:
strace a.o inputfile >outputfile

Then on the screen it stops at :
open("energy_tables.dat", O_RDONLY) = -1 ENOENT (No such file or directory)
--- SIGSEGV (Segmentation fault) @ 0 (0) ---

This shows that the code a.o calls and opens a file energy_tables.dat while it runs, but this file is not found/opened properly ! This causes the segmentation error !

3) when I put the right file of energy_tables.dat in the same folder as a.o, voila, it works !

4) It reminds me that an exception should be added in the original C code for openning the energy_table.dat !

jeudi 15 juillet 2010

C code with Python

Notes if we have C source code that we want to execute from Python
we can either::
a) Build a library and create a Python wrapper so it looks like a module, eg., swig
b) Build an executable file and call that using subprocess/popen etc
http://docs.python.org/library/subprocess.html#module-subprocess
c) If its one of the common C libraries or a Windows DLL we can
probably use an existing framework to call it. For example ctypes
will access a lot of stuff.

compile c code in linux (some math functions used in the code)

I had a piece of c code and it worked in windows. Now i want to recompile it in linux.
When I launch :
gcc -o code code.c

It has error messages:
/home/tmp/cczz9Kqc.o: In function `wfptb':
readIAP0.c:(.text+0x197f): undefined reference to `exp'
readIAP0.c:(.text+0x1a27): undefined reference to `exp'
/home/tmp/cczz9Kqc.o: In function `decode':
readIAP0.c:(.text+0x2230): undefined reference to `pow'
readIAP0.c:(.text+0x235e): undefined reference to `pow'
collect2: ld a retourné 1 code d'état d'exécution

This suggests that some math functions can not be used, though "#include " is in the code. when I check:
man exp
in the linux terminal, there it suggests to link with -lm. It looks like that I need tell the linker to link with/usr/lib/libm.so.

So I recompile with:
gcc -o code code.c -lm

and the errors are gone.

samedi 5 juin 2010

modify makefile

I am running a modelling sysytem written in C++, C and fortran. It was compiled in my office, but since I am interested in running it in my home computer, I tried to install it and the denpendent softwares in this computer.

My linux system at home is ubuntu so it is rather easy to install the softwares:
sudo apt-cache search software ----for searching for the right software package
sudo apt-get install software ----for installing it

Then before compiling it, I need modify the makefile !

1) the code uses gfortran, gcc,:
i installed gfortran 4.3 and gcc 4.3,

2) the code use lapack for linear algebra:
I installed liblapack.


3) then modify the makefile
CC = g++-4.3
F77 = gfortran-4.3

add the new path in the INCPATH:
INCPATH = -I/usr/include \
-I/usr/lib \

modify the link:
LINK = g++-4.3

modify the libs:
LIBS = -L/home/xiaoni/software/blitz -llapack -lgfortran

(In the end, I also modified the .bashrc with the correct path: export CPATH, LD_LIBRARY_PATH, CPLUS_INCLUDE_PATH, C_INCLUDE_PATH.)


The code can be compiled correctly !