|
| 1 | +! async_comp_alloc.f90 |
| 2 | + |
| 3 | +! Unit test for register procedure and remote allocated test: |
| 4 | +! Test that scalar allocatable component in a derived typed coarray is |
| 5 | +! registered correctly, its allocation is deferred until the program |
| 6 | +! allocates it specifically, and it is deregisterable. The component |
| 7 | +! allocation checks are done on remote images. |
| 8 | +! |
| 9 | +! Copyright (c) 2012-2016, Sourcery, Inc. |
| 10 | +! All rights reserved. |
| 11 | +! |
| 12 | +! Redistribution and use in source and binary forms, with or without |
| 13 | +! modification, are permitted provided that the following conditions are met: |
| 14 | +! * Redistributions of source code must retain the above copyright |
| 15 | +! notice, this list of conditions and the following disclaimer. |
| 16 | +! * Redistributions in binary form must reproduce the above copyright |
| 17 | +! notice, this list of conditions and the following disclaimer in the |
| 18 | +! documentation and/or other materials provided with the distribution. |
| 19 | +! * Neither the name of the Sourcery, Inc., nor the |
| 20 | +! names of its contributors may be used to endorse or promote products |
| 21 | +! derived from this software without specific prior written permission. |
| 22 | +! |
| 23 | +! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 24 | +! ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 25 | +! WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 26 | +! DISCLAIMED. IN NO EVENT SHALL SOURCERY, INC., BE LIABLE FOR ANY |
| 27 | +! DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 28 | +! (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 29 | +! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 30 | +! ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | +! (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 32 | +! SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | + |
| 34 | +program async_comp_alloc |
| 35 | + implicit none |
| 36 | + |
| 37 | + type dt |
| 38 | + integer, allocatable :: i |
| 39 | + end type dt |
| 40 | + type(dt), allocatable :: obj[:] |
| 41 | + !! Container object for testing component allocation status |
| 42 | + |
| 43 | + logical, parameter :: verbose=.true. |
| 44 | + !! Toggle verbose output for debugging purposes |
| 45 | + |
| 46 | + integer :: allocation_status |
| 47 | + integer, parameter :: success=0 |
| 48 | + !! Successful allocation status value |
| 49 | + |
| 50 | + |
| 51 | + associate(me=>this_image(),np=>num_images()) |
| 52 | + !! Make me & np locally immutable by associating them with function results |
| 53 | + |
| 54 | + call assert( np > 1, "num_images()>1") |
| 55 | + !! Ensure at least two images to simplify the test |
| 56 | + |
| 57 | + allocate(obj[*],stat=allocation_status) |
| 58 | + !! Allocate only the container. obj%i must not be allocated hereafter. |
| 59 | + |
| 60 | + call assert(allocation_status == success, "allocated(obj)") |
| 61 | + call assert(.not. allocated(obj%i) , ".not. allocated(obj%i)") |
| 62 | + |
| 63 | + if (me==1) print *,"known failure -- ctest will timeout (see https://github.com/sourceryinstitute/opencoarrays/issues/260)." |
| 64 | + |
| 65 | + block |
| 66 | + integer :: allocating_image, test_image |
| 67 | + character(len=8) :: image_number |
| 68 | + |
| 69 | + loop_over_all_image_numbers: do allocating_image = 1, np |
| 70 | + !! Check that all allocations have been performed up to allocating_image and |
| 71 | + !! that no other allocations have been performed |
| 72 | + |
| 73 | + if (verbose) print *, me, "/", np, ": allocating_image=", allocating_image |
| 74 | + |
| 75 | + if (allocating_image /= me) then |
| 76 | + sync all |
| 77 | + !! Order allocations sequentially by image number: arrival of image 'allocating_image' |
| 78 | + !! at the sync all in the alternate branch launches the following test loop. |
| 79 | + test_allocation_status: do test_image = 1, np |
| 80 | + if (verbose) print *, me, "/", np, ": Checking", test_image, " for allocation status." |
| 81 | + write(image_number,*) test_image |
| 82 | + if (test_image <= allocating_image) then |
| 83 | + call assert(allocated(obj[test_image]%i), " allocated(obj["//image_number//"]%i)" ) |
| 84 | + !! Enforce that image numbers up to mine have allocated their components |
| 85 | + else |
| 86 | + call assert(.not. allocated(obj[test_image]%i), ".not. allocated(obj["//image_number//"]%i) on image ") |
| 87 | + !! Enforce that image numbers higher than 'image' should not have allocated their components yet |
| 88 | + end if |
| 89 | + end do test_allocation_status |
| 90 | + else |
| 91 | + if (verbose) print *, me, "/", np, ": allocating..." |
| 92 | + allocate(obj%i, source = me) |
| 93 | + !! TODO: should we also ensure that implicit (re)allocation upon assignment works here? |
| 94 | + if (verbose) print *, me, "/", np, ": allocated" |
| 95 | + ! Enforce that object has been allocated and obj%i is this_image(): |
| 96 | + call assert( allocated(obj) , "allocated(obj)") |
| 97 | + call assert( allocated(obj%i), "allocated(obj%i)") |
| 98 | + call assert( obj%i == me , "obj%i == this_image()") |
| 99 | + sync all |
| 100 | + end if |
| 101 | + end do loop_over_all_image_numbers |
| 102 | + end block |
| 103 | + end associate |
| 104 | +contains |
| 105 | + subroutine assert(assertion,description,stat) |
| 106 | + logical, intent(in) :: assertion |
| 107 | + character(len=*), intent(in) :: description |
| 108 | + integer, intent(out), optional:: stat |
| 109 | + integer, parameter :: failure=1 |
| 110 | + if (assertion) then |
| 111 | + if (present(stat)) stat=success |
| 112 | + else |
| 113 | + if (present(stat)) then |
| 114 | + stat=failure |
| 115 | + else |
| 116 | + error stop "Assertion "// description //" failed." |
| 117 | + end if |
| 118 | + end if |
| 119 | + end subroutine |
| 120 | +end program |
| 121 | + |
0 commit comments