|
| 1 | +! caf -o gather gather.f90 |
| 2 | +! cafrun -np 2 ./gather |
| 3 | +! this example mimics a mpi_gatherv with root=1, and variable size chunks |
| 4 | +! gather into specified locations from all processes in a group |
| 5 | + |
| 6 | +program gather |
| 7 | + implicit none |
| 8 | + |
| 9 | + type gvec ! a global vector |
| 10 | + real, allocatable :: a(:) |
| 11 | + end type |
| 12 | + |
| 13 | + type(gvec) :: gc[*] |
| 14 | + real, allocatable :: gv(:), tmp(:) |
| 15 | + integer :: me, nimg, gsize, i, lo, hi |
| 16 | + logical :: fail = .false. |
| 17 | + |
| 18 | + me = this_image() |
| 19 | + nimg = num_images() |
| 20 | + |
| 21 | + allocate(gc % a(2 * me)) ! variable size data in container |
| 22 | + gc % a = [(me * i, i=1, 2 * me)] ! assignement |
| 23 | + |
| 24 | + ! collect the global vector size by summing local sizes |
| 25 | + gsize = size(gc % a) |
| 26 | + call co_sum(gsize, result_image=1) |
| 27 | + sync all |
| 28 | + |
| 29 | + if (me == 1) then |
| 30 | + if (gsize /= 6) error stop 1 |
| 31 | + allocate(gv(gsize)) ! allocate a global vector of size 6 on img 1 |
| 32 | + lo = 1 |
| 33 | + do i = 1, nimg |
| 34 | + tmp = gc[i] % a ! note: automatic reallocation of tmp |
| 35 | + hi = lo + size(tmp) - 1 |
| 36 | + gv(lo:hi) = tmp |
| 37 | + lo = hi + 1 ! start of next chunk |
| 38 | + end do |
| 39 | + print *, 'gv=', gv, ' sum=', sum(gv) |
| 40 | + |
| 41 | + if (abs(sum(gv) - 23.) > epsilon(0.)) fail = .true. |
| 42 | + end if |
| 43 | + |
| 44 | + sync all |
| 45 | + |
| 46 | + ! CMake test output handler |
| 47 | + call co_broadcast(fail, source_image=1) |
| 48 | + if (fail) then |
| 49 | + write(*, *) 'Test failed!' |
| 50 | + error stop 5 |
| 51 | + else |
| 52 | + write(*, *) 'Test passed.' |
| 53 | + end if |
| 54 | + |
| 55 | +end program |
0 commit comments