Changes between Initial Version and Version 1 of cypress/Programming/FortranCpp


Ignore:
Timestamp:
05/15/15 11:55:47 (9 years ago)
Author:
cmaggio
Comment:

migrated content from ccs wiki

Legend:

Unmodified
Added
Removed
Modified
  • cypress/Programming/FortranCpp

    v1 v1  
     1= Fortran and C/C++ =
     2[[cypress/Programming/BlasLapack|BLAS / LAPACK]] routines are written in Fortran 77.
     3
     4Difference between C and Fortran:
     5* The way matrices are stored in memory
     6  * we have to transpose our matrices before we can pass them to a Fortran routine.
     7* The way arguments are passed to a subroutine.
     8  * In Fortran the argument is passed to the routine and the routine can read and change this value. (call by reference)
     9  * 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)
     10  * To use call by reference in C, we have to pass pointers to variables instead of the variables themselves to the routine.
     11
     12== Arrays in Fortran / C ==
     132D array (matrix) in C is defined as
     14{{{#!c
     15double matrix[10][10];
     16}}}
     17
     18In Fortran,
     19{{{#!fortran
     20Real*8 matrix(10,10)
     21}}}
     22
     23These 2D-array(matrix) are allocated in the 1D-memory (RAM) address space, however, the way of this is deferent in C and Fortran.
     24
     25In C,
     26
     272D Array C.png
     28
     29In Fortran
     30
     312D Array Fortran.png
     32
     33== How arguments passed in Fortran / C ==
     34Subroutine Arg.png
     35
     36In Fortran, if make a value change in subroutine, the value in the main routine changes.
     37
     38To do 'call by reference' in C, send pointers.
     39
     40{{{#!c
     41mysub(&a,&b,&c);
     42
     43:
     44
     45}
     46
     47void mybud(double *x,double *y,double *z){
     48
     49:
     50
     51}
     52}}}
     53
     54== Fortran subroutine names from C ==
     55Lower case, put “_” in end.
     56
     57For example, fortran subroutine
     58
     59{{{DNRM2(n,x,m,nm)}}}
     60
     61is :
     62
     63{{{dnrm2_(&n,x,&m,&nm);}}}
     64
     65when calling from C program.