wiki:cypress/Programming/FortranCpp

Version 1 (modified by cmaggio, 10 years ago) ( diff )

migrated content from ccs wiki

Fortran and C/C++

BLAS / LAPACK routines are written in Fortran 77.

Difference between C and Fortran:

  • The way matrices are stored in memory
    • we have to transpose our matrices before we can pass them to a Fortran routine.
  • The way arguments are passed to a subroutine.
    • In Fortran the argument is passed to the routine and the routine can read and change this value. (call by reference)
    • In C a copy of the value of the argument is passed to the routine and not the argument itself, the subroutine can't change the value of the argument. (call by value)
    • To use call by reference in C, we have to pass pointers to variables instead of the variables themselves to the routine.

Arrays in Fortran / C

2D array (matrix) in C is defined as

double matrix[10][10];

In Fortran,

Real*8 matrix(10,10)

These 2D-array(matrix) are allocated in the 1D-memory (RAM) address space, however, the way of this is deferent in C and Fortran.

In C,

2D Array C.png

In Fortran

2D Array Fortran.png

How arguments passed in Fortran / C

Subroutine Arg.png

In Fortran, if make a value change in subroutine, the value in the main routine changes.

To do 'call by reference' in C, send pointers.

mysub(&a,&b,&c);

:

}

void mybud(double *x,double *y,double *z){

:

}

Fortran subroutine names from C

Lower case, put “_” in end.

For example, fortran subroutine

DNRM2(n,x,m,nm)

is :

dnrm2_(&n,x,&m,&nm);

when calling from C program.

Attachments (3)

Download all attachments as: .zip

Note: See TracWiki for help on using the wiki.