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