|
| 1 | +module object_interface |
| 2 | + implicit none |
| 3 | + private |
| 4 | + public :: object |
| 5 | + |
| 6 | + type object |
| 7 | + private |
| 8 | + integer :: foo=0 |
| 9 | + logical :: bar=.false. |
| 10 | + contains |
| 11 | + procedure :: initialize |
| 12 | + procedure :: co_broadcast_me |
| 13 | + procedure :: not_equal |
| 14 | + procedure :: copy |
| 15 | + generic :: operator(/=)=>not_equal |
| 16 | + generic :: assignment(=)=>copy |
| 17 | + end type |
| 18 | + |
| 19 | + interface |
| 20 | + elemental impure module subroutine initialize(this,foo_,bar_) |
| 21 | + implicit none |
| 22 | + class(object), intent(out) :: this |
| 23 | + integer, intent(in) :: foo_ |
| 24 | + logical, intent(in) :: bar_ |
| 25 | + end subroutine |
| 26 | + |
| 27 | + elemental impure module subroutine co_broadcast_me(this,source_image) |
| 28 | + implicit none |
| 29 | + class(object), intent(inout) :: this |
| 30 | + integer, intent(in) :: source_image |
| 31 | + end subroutine |
| 32 | + |
| 33 | + elemental module function not_equal(lhs,rhs) result(lhs_ne_rhs) |
| 34 | + implicit none |
| 35 | + class(object), intent(in) :: lhs,rhs |
| 36 | + logical lhs_ne_rhs |
| 37 | + end function |
| 38 | + |
| 39 | + elemental impure module subroutine copy(lhs,rhs) |
| 40 | + implicit none |
| 41 | + class(object), intent(inout) :: lhs |
| 42 | + class(object), intent(in) :: rhs |
| 43 | + end subroutine |
| 44 | + end interface |
| 45 | + |
| 46 | +end module |
| 47 | + |
| 48 | +submodule(object_interface) object_implementation |
| 49 | + implicit none |
| 50 | +contains |
| 51 | + module procedure co_broadcast_me |
| 52 | + call co_broadcast(this%foo,source_image) |
| 53 | + call co_broadcast(this%bar,source_image) |
| 54 | + end procedure |
| 55 | + |
| 56 | + module procedure initialize |
| 57 | + this%foo = foo_ |
| 58 | + this%bar = bar_ |
| 59 | + end procedure |
| 60 | + |
| 61 | + module procedure not_equal |
| 62 | + lhs_ne_rhs = (lhs%foo /= rhs%foo) .or. (lhs%bar .neqv. rhs%bar) |
| 63 | + end procedure |
| 64 | + |
| 65 | + module procedure copy |
| 66 | + lhs%foo = rhs%foo |
| 67 | + lhs%bar = rhs%bar |
| 68 | + end procedure |
| 69 | +end submodule |
| 70 | + |
| 71 | +program main |
| 72 | + use object_interface, only : object |
| 73 | + implicit none |
| 74 | + type(object) message |
| 75 | + |
| 76 | + call message%initialize(foo_=1,bar_=.true.) |
| 77 | + |
| 78 | + emulate_co_broadcast: block |
| 79 | + type(object) foobar |
| 80 | + if (this_image()==1) foobar = message |
| 81 | + call foobar%co_broadcast_me(source_image=1) |
| 82 | + if ( foobar /= message ) error stop "Test failed." |
| 83 | + end block emulate_co_broadcast |
| 84 | + |
| 85 | + desired_co_broadcast: block |
| 86 | + type(object) barfoo |
| 87 | + if (this_image()==1) barfoo = message |
| 88 | + call co_broadcast(barfoo,source_image=1) ! OpenCoarrays terminates here with the message "Unsupported data type" |
| 89 | + if ( barfoo /= message ) error stop "Test failed." |
| 90 | + end block desired_co_broadcast |
| 91 | + |
| 92 | + sync all ! Wait for each image to pass the test |
| 93 | + if (this_image()==1) print *,"Test passed." |
| 94 | + |
| 95 | +end program main |
0 commit comments