Skip to content

Commit 69bdf3a

Browse files
committed
Add unit test based on #172
But configure it so that it passes and doesn't exhibit regression
1 parent 3d06a26 commit 69bdf3a

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ if(opencoarrays_aware_compiler)
412412
add_mpi_test(syncimages2 32 ${tests_root}/unit/sync/syncimages2)
413413
add_mpi_test(duplicate_syncimages 8 ${tests_root}/unit/sync/duplicate_syncimages)
414414
add_mpi_test(co_reduce 4 ${tests_root}/unit/collectives/co_reduce_test)
415+
add_mpi_test(co_reduce_res_im 4 ${tests_root}/unit/collectives/co_reduce_res_im)
415416
add_mpi_test(syncimages_status 32 ${tests_root}/unit/sync/syncimages_status)
416417
add_mpi_test(sync_ring_abort_np3 3 ${tests_root}/unit/sync/sync_image_ring_abort_on_stopped_image)
417418
add_mpi_test(sync_ring_abort_np7 7 ${tests_root}/unit/sync/sync_image_ring_abort_on_stopped_image)

src/tests/unit/collectives/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ target_link_libraries(co_max_test OpenCoarrays)
1212

1313
add_executable(co_reduce_test co_reduce.F90)
1414
target_link_libraries(co_reduce_test OpenCoarrays)
15+
16+
add_executable(co_reduce_res_im co_reduce_res_im.f90)
17+
target_link_libraries(co_reduce_res_im OpenCoarrays)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)