Skip to content

Commit f833803

Browse files
FoadsfFoadsf
authored andcommitted
many fortran C mixed programing examples added
1 parent b3764b8 commit f833803

File tree

133 files changed

+6314
-79
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+6314
-79
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,15 @@ lapacke_mangling_with_flags.h
155155
lapacke_mangling.h
156156
lapack_utils.h
157157
lapacke.h
158+
159+
160+
libblas.a
161+
librefblas.a
162+
libcblas.a
163+
libf2c.a
164+
libgfortran.dylib
165+
liblapack.a
166+
liblapacke.a
167+
liblapacke.dylib
168+
librefblas.a
169+
libtmglib.a
Binary file not shown.

A_fortran/other/f90ppr/f90ppr.f90.gz

35.5 KB
Binary file not shown.

A_fortran/other/floppy7/floppy7.tgz

172 KB
Binary file not shown.
Binary file not shown.

A_fortran/readme.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,21 @@ sources to be looked at:
2020
8. https://docs.oracle.com/cd/E19059-01/stud.9/817-6694/11_cfort.html
2121
9. https://www.math.utah.edu/software/c-with-fortran.html
2222
10. https://software.intel.com/en-us/forums/intel-visual-fortran-compiler-for-windows/topic/290051
23+
11. http://luthaf.github.io/calling-c++-from-fortran.html
24+
12. http://alignment.hep.brandeis.edu/Software/Mixing/Mixing_Manual.html
25+
13. http://www.fortran.bcs.org/2002/interop.htm
26+
14. http://www.pgroup.com/userforum/viewtopic.php?p=7398&sid=2a05571a922045c6c4ae26d65bbf9fba
27+
15. http://www.unidata.ucar.edu/software/netcdf/examples/programs/
28+
16. http://people.sc.fsu.edu/~jburkardt/c_src/mixed/mixed.html
29+
17. https://docs.oracle.com/cd/E19422-01/819-3685/11_cfort.html
30+
2331

2432
points:
2533

2634
1. name of the routines/subroutines/functions and structs/commons in some versions or dialects of fortran must be in small letters and and an underscore _ must be added to the C function
35+
36+
37+
issues:
38+
1. test22 result not correct
39+
2. test19 does not compile
40+
3. test24 not compiling

A_fortran/test1/readme.txt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
this file shows how we can call a C function from fortran
1+
fortran code written if FORTRAN 77?
2+
3+
this file shows how we can call a C function from fortran and how to pass a string properly from fortran to C
24
source: http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html
35

46

57
points:
6-
1. when you pass a string from fortran to C you actually pass two variables including an array of single characters and also the length of the array.
8+
1. when you pass a string from fortran to C you actually pass two variables including a pointer to an array of single characters (string) and also the length of the array.
79
2. when you pass a string to C you need to first be sure that you allocate at least one letter more than the characters you need and in C first of se "\0" or NULL as the end of the string
810
3. common is the equivalent of struct in C
911
in C:
@@ -18,7 +20,9 @@ points:
1820
...
1921
common/mystruct/ i, c, ...
2022

21-
4. when fortran passes a variable to C function, the C function receives a pointer
23+
4. when fortran passes a variable to C function, the C function receives a pointer?
24+
5. when you want to call a C function in fortran you don't need to declare a dummy in the fortran code, unlike C ,fortran doesn't care if the function is not declared and then on run time it will look for the function (how about variables and structs?)
25+
6. when passing variables, functions or srtucts from/to C/fortran it is Case-insensitive. but you should be careful that inside each world it is case sensitive.
2226

2327

2428

@@ -28,3 +32,11 @@ questions:
2832
I think we needed a
2933
external doubleijk
3034
here, but we don't!
35+
36+
37+
38+
39+
fortran points:
40+
1. in FORTRAN 77 (?) one can print to terminal with something like:
41+
write(6,<line number for format e.g. 10>) var_1,var_2,...
42+
10 format('some text', typeof_var_N,'some other text',...)

A_fortran/test1/test1F.f

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
program test
22

33
integer ii, jj, kk
4-
common/ijk/ ii, jj, kk
4+
common/IjK/ ii, jj, kk
5+
56
real*8 ff
67
character*25 cc
78
character*7 dd

A_fortran/test10/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
all:
2+
gfortran -c ranvec.f90
3+
gcc -c main.c
4+
gfortran -o result.out ranvec.o main.o
5+
rm -rf *.o
6+
7+
clean :
8+
rm -rf *.out *~ *.bak *.o

A_fortran/test10/main.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdio.h>
2+
3+
void ranvec_(double *, int *);
4+
5+
int main() {
6+
int n = 5;
7+
double a[5];
8+
9+
ranvec_(a, &n);
10+
11+
printf("a = %f, %f, %f, %f, %f\n", a[0], a[1], a[2], a[3], a[4]);
12+
13+
return 0;
14+
}

A_fortran/test10/ranvec.f90

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SUBROUTINE ranvec(v, n)
2+
INTEGER :: n
3+
DOUBLE PRECISION, DIMENSION(n) :: v
4+
5+
CALL RANDOM_SEED()
6+
CALL RANDOM_NUMBER(v)
7+
8+
END SUBROUTINE ranvec

A_fortran/test10/readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
source: http://www.math.chalmers.se/Math/Grundutb/CTH/tma881/0809/Assignments/Mixing_C_Fortran.html

A_fortran/test11/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
all:
2+
gfortran -c add.f
3+
gcc -c test11.c
4+
gfortran -o result.out add.o test11.o
5+
rm -rf *.o
6+
7+
clean :
8+
rm -rf *.out *~ *.bak *.o

A_fortran/test11/add.f

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
REAL*8 FUNCTION ADD (A, B, C, D)
2+
REAL*8 B,D
3+
INTEGER*4 A,C
4+
DIMENSION B(4), D(4)
5+
ADD = B(A) + D(C)
6+
RETURN
7+
END

A_fortran/test11/redme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
source: http://www.ibm.com/support/knowledgecenter/SSGH2K_13.1.2/com.ibm.xlc131.aix.doc/proguide/tucsampl.html

A_fortran/test11/test11.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <stdio.h>
2+
3+
double add_(int *, double[], int *, double[]);
4+
5+
double ar1[4] = {1.0, 2.0, 3.0, 4.0};
6+
double ar2[4] = {5.0, 6.0, 7.0, 8.0};
7+
8+
int main() {
9+
int x, y;
10+
double z;
11+
12+
x = 3;
13+
y = 3;
14+
15+
z = add_(&x, ar1, &y, ar2); /* Call Fortran add routine */
16+
/* Note: Fortran indexes arrays 1..n */
17+
/* C indexes arrays 0..(n-1) */
18+
19+
printf("The sum of %1.0f and %1.0f is %2.0f \n", ar1[x - 1], ar2[y - 1], z);
20+
return 0;
21+
}

A_fortran/test12/Makefile

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# CC = gcc
2+
# FC = gfortran
3+
# # Make sure the CC and FC compilers were built with the same glibc library
4+
# # g77 will work fine on most systems
5+
# CFLAGS = -Wall
6+
#
7+
# OBJS = testdot.o testret.o testmat.o
8+
#
9+
# all: driver lib
10+
#
11+
# driver : driver.o $(OBJS)
12+
# $(FC) -o driver driver.o $(OBJS) -lstdc++
13+
#
14+
# testret.o : testret.cpp
15+
# $(CC) $(CFLAGS) -c testret.cpp
16+
#
17+
# testdot.o : testdot.cpp
18+
# $(CC) $(CFLAGS) -c testdot.cpp
19+
#
20+
# testmat.o : testmat.cpp
21+
# $(CC) $(CFLAGS) -c testmat.cpp
22+
#
23+
# driver.o : driver.f
24+
# $(FC) -c driver.f
25+
#
26+
# # Default Targets for Cleaning up the Environment
27+
# clean :
28+
# rm *.o
29+
# rm *.a
30+
#
31+
# pristine :
32+
# rm *.o
33+
# rm *.a
34+
# touch *.cpp
35+
#
36+
# ctags :
37+
# ctags *.cpp
38+
#
39+
# # Target for making the library
40+
#
41+
# lib: $(OBJS)
42+
# ar -rc libbblas.a $(OBJS)
43+
# ranlib libbblas.a
44+
45+
46+
all:
47+
gfortran -c driver.f
48+
g++ -c testdot.cpp
49+
g++ -c testmat.cpp
50+
g++ -c testret.cpp
51+
g++ -o result.out testret.o testmat.o testdot.o driver.o -L /usr/local/Cellar/gcc/6.2.0/lib/gcc/6/ -lgfortran
52+
rm -rf *.o
53+
54+
55+
clean :
56+
rm -rf *.out *~ *.bak *.o

A_fortran/test12/driver.f

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
program driver
2+
3+
integer veclen matdim
4+
real mat, dot
5+
real vec1(2), vec2(2)
6+
real matrix1(3,3), matrix2(3,3), matrix3(3,3)
7+
8+
veclen = 2
9+
matdim = 3
10+
11+
vec1(1) = 1.0
12+
vec1(2) = 2.0
13+
vec2(1) = 3.0
14+
vec2(2) = 4.0
15+
16+
matrix1(1,1) = 1.0
17+
matrix1(2,1) = 0.0
18+
matrix1(3,1) = 0.0
19+
matrix1(1,2) = 0.0
20+
matrix1(2,2) = 1.0
21+
matrix1(3,2) = 0.0
22+
matrix1(1,3) = 0.0
23+
matrix1(2,3) = 0.0
24+
matrix1(3,3) = 1.0
25+
26+
matrix2(1,1) = 1.0
27+
matrix2(2,1) = 0.0
28+
matrix2(3,1) = 0.0
29+
matrix2(1,2) = 0.0
30+
matrix2(2,2) = 2.0
31+
matrix2(3,2) = 0.0
32+
matrix2(1,3) = 0.0
33+
matrix2(2,3) = 0.0
34+
matrix2(3,3) = 3.0
35+
36+
mat = 0.0;
37+
38+
* mat should get incremented by 1 in testret
39+
do 10 i = 1, 10
40+
call testret(mat)
41+
10 continue
42+
43+
* testdot should return the dot product of vec1 and vec2 as a real
44+
dot = testdot(veclen, vec1, vec2)
45+
46+
* testmat uses matrix1 and matrix2 to calculate matrix3
47+
call testmat(matdim, matrix1, matrix2, matrix3)
48+
49+
print *, mat, ' ', dot
50+
print *,
51+
do 20 i = 1, matdim
52+
write (6,*) (matrix3(j,i), j=1,matdim)
53+
20 continue
54+
55+
56+
end

A_fortran/test12/readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
source: http://theochem.mercer.edu/interlanguage/

A_fortran/test12/testdot.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
extern "C" {
3+
float testdot_( int *N, float *vec1, float *vec2);
4+
}
5+
6+
7+
float testdot_( int *N, float *vec1, float *vec2 )
8+
{
9+
int i;
10+
float dot;
11+
12+
dot = 0.0;
13+
14+
for (i=0; i<*N; i++) {
15+
dot = dot + *(vec1+i) * *(vec2+i);
16+
}
17+
return dot;
18+
}
19+

A_fortran/test12/testmat.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
extern "C" {
3+
void testmat_( int *N, float *mat1, float *mat2, float *mat3);
4+
}
5+
6+
7+
void testmat_( int *N, float *mat1, float *mat2, float *mat3 )
8+
{
9+
int i,j,k;
10+
float dot;
11+
12+
// Assign stuff to mat3 from mat1 and mat2
13+
for (i=0; i<*N; i++) {
14+
for (j=0; j<*N; j++) {
15+
dot = 0.0;
16+
for (k=0; k<*N; k++ ) {
17+
dot = dot + *(mat1+(*N*j+k)) * *(mat2+(*N*i+j));
18+
}
19+
*(mat3+(*N*j+i)) = dot;
20+
}
21+
}
22+
23+
}
24+

A_fortran/test12/testret.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
extern "C" {
3+
void testret_( float *mat);
4+
}
5+
6+
7+
void testret_( float *mat )
8+
{
9+
*mat = *mat + 1;
10+
}
11+

A_fortran/test13/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
all:
2+
gfortran -c test13F.f
3+
gcc -c test13C.c
4+
gfortran -o result.out test13F.o test13C.o
5+
rm -rf *.o
6+
7+
clean :
8+
rm -rf *.out *~ *.bak *.o

A_fortran/test13/readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
source: http://striky.ece.jhu.edu/~sasha/SOFTWARE/FORTRAN/007-3696-005/sgi_html/ch07.html

A_fortran/test13/test13C.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
typedef char fstr_16[16];
5+
6+
void prt_(fstr_16, float *, fstr_16, int, int);
7+
8+
int main() {
9+
float val = 2.1828e0;
10+
fstr_16 bef, aft;
11+
strncpy(bef, "Before..........", sizeof(bef));
12+
strncpy(aft, "...........After", sizeof(aft));
13+
prt_(bef, &val, aft, sizeof(bef), sizeof(aft));
14+
return 0;
15+
}

A_fortran/test13/test13F.f

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SUBROUTINE PRT(BEF, VAL, AFT)
2+
CHARACTER*(*) :: BEF, AFT
3+
REAL :: VAL
4+
PRINT *, BEF, VAL, AFT
5+
RETURN
6+
END SUBROUTINE PRT

A_fortran/test14/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
all:
2+
gfortran -c test13F.f
3+
gcc -c test13C.c
4+
gfortran -o result.out test13F.o test13C.o
5+
rm -rf *.o
6+
7+
clean :
8+
rm -rf *.out *~ *.bak *.o

A_fortran/test14/readme.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
source: http://striky.ece.jhu.edu/~sasha/SOFTWARE/FORTRAN/007-3696-005/sgi_html/ch07.html
2+
3+
4+
on the fortran side if you do not specify a the type of the variable it assumes it's real!
5+
6+
when transferring a string or an array from C to fortran we must use strlen or size of function to pass the size as well
7+
8+
void func_(int *, int, char *, int);
9+
10+
char * mychar = "some text";
11+
int * myint = {1,2,3,4};
12+
13+
func_(myint, sizeof(myint), mychar , strlen(mychar));
14+
15+
16+
what surprises me is that somehow miraculously even if we change the order of the variables fortran understands to assign the output of strlen and sizeof functions to the right array/string

0 commit comments

Comments
 (0)