|
| 1 | +program co_reduce_res_im |
| 2 | + !! author: Daniel Topa & Izaak Beekman |
| 3 | + !! category: unit test |
| 4 | + !! |
| 5 | + !! This test is derived from |
| 6 | + !! [issue #172](https://github.com/sourceryinstitute/opencoarrays/issues/172) |
| 7 | + !! but tweaks the binary operator's (pure function) arguments have |
| 8 | + !! `intent(in)` which results in a working/passing test |
| 9 | + |
| 10 | + implicit none |
| 11 | + integer :: value[ * ] !! Each image stores their image number here |
| 12 | + integer :: k |
| 13 | + value = this_image ( ) |
| 14 | + call co_reduce ( value, result_image = 1, operator = myProd ) |
| 15 | + !! value[k /= 1] undefined, value[ k == 1 ] should equal $n!$ where $n$ is `num_images()` |
| 16 | + if ( this_image ( ) == 1 ) then |
| 17 | + write ( * , '( "Number of images = ", g0 )' ) num_images ( ) |
| 18 | + do k = 1, num_images ( ) |
| 19 | + write ( * , '( 2( a, i0 ) )' ) 'value [ ', k, ' ] is ', value [ k ] |
| 20 | + write ( * , '(a)' ) 'since RESULT_IMAGE is present, value on other images is undefined by the standard' |
| 21 | + end do |
| 22 | + write ( * , '( "Product value = ", g0 )' ) value !! should print num_images() factorial |
| 23 | + if ( value == factorial( num_images() ) ) then |
| 24 | + write ( * , '(a)' ) 'Test passed.' |
| 25 | + else |
| 26 | + write ( * , '(a, I0)') 'Answer should have been num_images()! = ', factorial( num_images() ) |
| 27 | + error stop 'Wrong answer for n! using co_reduce' |
| 28 | + end if |
| 29 | + end if |
| 30 | + |
| 31 | + |
| 32 | +contains |
| 33 | + |
| 34 | + pure function myProd ( a, b ) result ( rslt ) |
| 35 | + !! Product function to be used in `co_reduce` reduction for |
| 36 | + !! computing factorials. When `intent(in)` attribute is changed |
| 37 | + !! to `value` tests fail |
| 38 | + integer, intent(in) :: a, b |
| 39 | + !! multiply two inputs together. If we change `intent(in)` to |
| 40 | + !! `value` the test fails despite being correct according to C1276 |
| 41 | + !! of F2008: |
| 42 | + !! |
| 43 | + !! > C1276 The specification-part of a pure function subprogram |
| 44 | + !! > shall specify that all its nonpointer dummy data objects have |
| 45 | + !! > the INTENT (IN) or the VALUE attribute. |
| 46 | + !! |
| 47 | + !! Thanks to @LadaF for pointing this out. |
| 48 | + integer :: rslt !! product of a*b |
| 49 | + rslt = a * b |
| 50 | + end function |
| 51 | + |
| 52 | + pure function factorial ( n ) result ( rslt ) |
| 53 | + !! Compute $n!$ |
| 54 | + integer, intent(in) :: n |
| 55 | + integer :: rslt |
| 56 | + integer :: i |
| 57 | + rslt = 1 |
| 58 | + do i = 1, n |
| 59 | + rslt = rslt*i |
| 60 | + end do |
| 61 | + end function |
| 62 | +end program |
0 commit comments