Skip to content

Commit e357c97

Browse files
author
rasmussn
committed
Initial version.
1 parent 93967d4 commit e357c97

54 files changed

Lines changed: 1331 additions & 0 deletions

Some content is hidden

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

tests/annex_c/c_10_2_1.f90

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module ftn_c_1
2+
use, intrinsic :: iso_c_binding
3+
end module ftn_c_1
4+
5+
module ftn_c_2
6+
interface
7+
! isn't this an error, since c_int won't be defined in time because it's
8+
! not defined until the use ftn_c_1 below??
9+
integer(c_int) function c_library_function &
10+
(sendbuf, sendcount, recvcounts) &
11+
bind(c, name='C_Library_function')
12+
use ftn_c_1
13+
implicit none
14+
type(c_ptr), value :: sendbuf
15+
integer(c_int), value :: sendcount
16+
type(c_ptr), value :: recvcounts
17+
end function c_library_function
18+
end interface
19+
end module ftn_c_2
20+
21+
use, intrinsic :: iso_c_binding, only: c_int, c_float, c_loc
22+
use ftn_c_2
23+
24+
real(c_float), target :: send(100)
25+
integer(c_int) :: sendcount
26+
integer(c_int), allocatable, target :: recvcounts(100)
27+
28+
allocate( recvcounts(100) )
29+
30+
call c_library_function(c_loc(send), sendcoutn, &
31+
c_loc(recvcounts))
32+
33+
end

tests/annex_c/c_10_2_2.f90

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
subroutine simulation(alpha, beta, gamma, delta, arrays) bind(c)
2+
use, intrinsic :: iso_c_binding
3+
implicit none
4+
integer(c_long), value :: alpha
5+
real (c_double), intent(inout) :: beta
6+
integer(c_long), intent(out) :: gamma
7+
real(c_double),dimension(*),intent(in) :: delta
8+
type, bind(c) :: pass
9+
integer(c_int) :: lenc, lenf
10+
type(c_ptr) :: c, f
11+
end type pass
12+
type(pass), intent(inout) :: arrays
13+
real (c_float), allocatable, target, save :: eta(:)
14+
real(c_float), pointer :: c_array(:)
15+
16+
! Associate c_array with an array allocated in C
17+
call c_f_pointer (arrays%c, c_array, (/arrays%lenc/) )
18+
19+
! Allocate an array and make it available in C
20+
arrays%lenf = 100
21+
allocate (eta(arrays%lenf))
22+
arrays%f = c_loc(eta)
23+
24+
end subroutine simulation
25+

tests/annex_c/c_10_2_3.f90

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use, intrinsic :: iso_c_binding
2+
3+
interface
4+
subroutine process_buffer(buffer, n_bytes) bind(c,name="ProcessBuffer")
5+
import :: c_ptr, c_int
6+
type(c_ptr), value :: buffer ! The "C address" of the array buffer
7+
integer(c_int), value :: n_bytes ! Number of bytes in buffer
8+
end subroutine process_buffer
9+
end interface
10+
11+
real(r_quad), dimension(:), allocatable, target :: quad_array
12+
13+
call process_buffer(c_loc(quad_array), int(16*size(quad_array),c_int))
14+
! One quead real takes 16 bytes on this processor
15+
16+
end

tests/annex_c/c_10_2_4.f90

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module assumed_module ! i added this
2+
use, intrinsic :: iso_c_binding
3+
! Assume this code is inside a module
4+
5+
type random_stream
6+
! A (uniform) random number generator (URNG)
7+
contains
8+
procedure(random_uniform), deferred, pass(stream) :: next
9+
! Generates the next number from the stream
10+
end type random_stream
11+
12+
abstract interface
13+
! Abstract interface of Fortran URNG
14+
subroutine random_uniform(stream, number)
15+
import :: random_stream, c_double
16+
class(random_stream), intent(inout) :: stream
17+
real(c_duoble), intent(out) :: number
18+
end subroutine random_uniform
19+
end interface
20+
21+
type :: urng_state ! No BIND(C), as this type is not interoperable
22+
class(random_stream), allocatable :: stream
23+
end type urng_state
24+
25+
contains ! i added this
26+
subroutine initialize_urng(state_handle, method) &
27+
bind(c, name="InitializeURNG")
28+
type(c_ptr), intent(out) :: state_handle
29+
! An opaque handle for the URNG
30+
character(c_char), dimension(*), intent(in) :: method
31+
! The algorithm to be used
32+
type(urng_state), pointer :: state
33+
! An actual URNG object
34+
allocate(state)
35+
! There needs to be a corresponding finalizatin
36+
! procedure to avoid memory leaks, not shown in this example
37+
! Allocate state%stream with a dynamic type depending on method
38+
39+
state_handle=c_loc(state)
40+
! Obtain an opaque handle to return to C
41+
end subroutine initialize_urng
42+
43+
! Generate a random number:
44+
subroutine generate_uniform(state_handle, number) &
45+
bind(c, name="GenerateUniform")
46+
type(c_ptr), intent(in), value :: state_handle
47+
! An opaque handle: Obained via a call to initialize_urng
48+
real(c_double), intent(out) :: number
49+
50+
type(urng_state), pointer :: state
51+
! A pointer to the actual urng
52+
53+
call c_f_pointer(c_ptr=state_handle, fptr=state)
54+
! Convert the opaque handle into a usable pointer
55+
call state%stream%next(number)
56+
! Use the type-bound procedure next to generate number
57+
end subroutine generate_uniform
58+
end module assumed_module
59+
60+

tests/annex_c/c_11_1_1.f90

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
program a
2+
integer i, j
3+
4+
contains
5+
subroutine b
6+
integer i ! Declaration of i hides
7+
! program a's declaration of i
8+
9+
i = j ! Use of variable j from program a
10+
! through host association
11+
end subroutine b
12+
end program a
13+

tests/annex_c/c_11_1_2.f90

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
program a
2+
type t
3+
integer :: field ! add a field to it (not in draft)
4+
end type t
5+
6+
contains
7+
subroutine b
8+
implicit type(t) (c) ! Refers to type T declared below
9+
! in subroutine B, not type T
10+
! declared above in program a
11+
type t
12+
integer :: this_field ! add a field (not in draft)
13+
end type t
14+
end subroutine b
15+
end program a
16+

tests/annex_c/c_11_1_3.f90

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
program q
2+
real(kind=1) :: c
3+
4+
contains
5+
subroutine r
6+
real(kind = kind(c)) :: D ! Invalid declaration
7+
! See below
8+
real(kind=2) :: c
9+
end subroutine r
10+
end program q

tests/annex_c/c_11_2_1.f90

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
interface good1
2+
function f1a(x)
3+
real :: f1a, x
4+
end function f1a
5+
function f1b(x)
6+
integer :: f1b, x
7+
end function f1b
8+
end interface good1
9+
10+
end
11+
12+

tests/annex_c/c_11_2_2.f90

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
interface good2
2+
function f2a(x)
3+
real :: f2a, x
4+
end function f2a
5+
function f2b(x,y)
6+
complex :: f2b
7+
real :: x,y
8+
end function f2b
9+
end interface good2
10+
11+
end

tests/annex_c/c_11_2_3.f90

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
interface good3
2+
subroutine s3a(w,x,y,z)
3+
real :: w,y
4+
integer :: x,z
5+
end subroutine s3a
6+
subroutine s3b(x,w,z,y)
7+
real :: w,z
8+
integer :: x,y
9+
end subroutine s3b
10+
end interface good3
11+
12+
end
13+

0 commit comments

Comments
 (0)