Yet Another eXchange Tool  DO_NOT_EDIT_HERE
test_redist_collection_static_parallel_f.f90
1 
13 
14 !
15 ! Keywords:
16 ! Maintainer: Jörg Behrens <behrens@dkrz.de>
17 ! Moritz Hanke <hanke@dkrz.de>
18 ! Thomas Jahns <jahns@dkrz.de>
19 ! URL: https://doc.redmine.dkrz.de/yaxt/html/
20 !
21 ! Redistribution and use in source and binary forms, with or without
22 ! modification, are permitted provided that the following conditions are
23 ! met:
24 !
25 ! Redistributions of source code must retain the above copyright notice,
26 ! this list of conditions and the following disclaimer.
27 !
28 ! Redistributions in binary form must reproduce the above copyright
29 ! notice, this list of conditions and the following disclaimer in the
30 ! documentation and/or other materials provided with the distribution.
31 !
32 ! Neither the name of the DKRZ GmbH nor the names of its contributors
33 ! may be used to endorse or promote products derived from this software
34 ! without specific prior written permission.
35 !
36 ! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
37 ! IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
38 ! TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
39 ! PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
40 ! OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
41 ! EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
42 ! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
43 ! PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
44 ! LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
45 ! NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46 ! SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 !
48 PROGRAM test_redist_collection_static_parallel
49  USE mpi
50  USE ftest_common, ONLY: init_mpi, finish_mpi, test_abort
51  USE test_idxlist_utils, ONLY: test_err_count
52  USE yaxt, ONLY: xt_initialize, xt_finalize, xt_int_kind, xi => xt_int_kind, &
58  xt_idxlist_get_indices, xt_int_mpidt
59  USE test_redist_common, ONLY: check_redist_xi
60  USE iso_c_binding, ONLY: c_loc, c_ptr
61  ! older PGI compilers do not handle generic interface correctly
62 #if defined __PGI && (__PGIC__ < 12 || (__PGIC__ == 12 && __PGIC_MINOR__ <= 10))
64 #endif
65  IMPLICIT NONE
66  INTEGER :: rank, comm_size, ierror
67  CALL init_mpi
68  CALL xt_initialize(mpi_comm_world)
69 
70  CALL mpi_comm_rank(mpi_comm_world, rank, ierror)
71  IF (ierror /= mpi_success) &
72  CALL test_abort('mpi_comm_rank failed', &
73  __file__, &
74  __line__)
75  CALL mpi_comm_size(mpi_comm_world, comm_size, ierror)
76  IF (ierror /= mpi_success) &
77  CALL test_abort('mpi_comm_size failed', &
78  __file__, &
79  __line__)
80 
81  IF (comm_size > 1) THEN
82  CALL test_4redist
83  CALL test_rr_exchange
84  END IF
85 
86  IF (test_err_count() /= 0) &
87  CALL test_abort("non-zero error count!", &
88  __file__, &
89  __line__)
90  CALL xt_finalize
91  CALL finish_mpi
92 CONTAINS
93  SUBROUTINE build_idxlists(indices_a, indices_b, indices_all)
94  ! redist test with four different redists
95  TYPE(xt_idxlist), INTENT(out) :: indices_a, indices_b, indices_all
96 
97  TYPE(xt_idxlist) :: indices_a_(2)
98  INTEGER :: i
99  INTEGER(xt_int_kind), PARAMETER :: start = 0
100  INTEGER(xt_int_kind) :: global_size(2), local_start(2, 2)
101  INTEGER :: local_size(2)
102 
103  TYPE(xt_stripe) :: stripe
104 
105  global_size(1) = int(2 * comm_size, xi)
106  global_size(2) = int(comm_size**2, xi)
107  local_size = comm_size
108  local_start = reshape((/ 0_xi, int(rank*comm_size, xi), &
109  int(comm_size, xi), int(comm_size**2-(rank+1)*comm_size, xi) /), &
110  (/ 2, 2 /))
111 
112  DO i = 1, 2
113  indices_a_(i) = xt_idxsection_new(start, global_size, local_size, &
114  local_start(:, i))
115  END DO
116  indices_a = xt_idxlist_collection_new(indices_a_)
117 
118  CALL xt_idxlist_delete(indices_a_(1))
119  CALL xt_idxlist_delete(indices_a_(2))
120 
121  stripe = xt_stripe(int(rank * 2 * comm_size**2, xi), 1_xi, 2*comm_size**2)
122  indices_b = xt_idxstripes_new(stripe)
123 
124  stripe = xt_stripe(0_xi, 1_xi, 2*comm_size**3)
125  indices_all = xt_idxstripes_new(stripe)
126  END SUBROUTINE build_idxlists
127 
128  SUBROUTINE test_4redist
129  INTEGER, PARAMETER :: num_tx = 4
130  TYPE(xt_idxlist) :: indices_a, indices_b, indices_all
131  INTEGER(xt_int_kind), ALLOCATABLE, TARGET :: src(:), dst(:)
132  INTEGER(xt_int_kind), POINTER :: index_vector_a(:), &
133  index_vector_b(:), index_vector_all(:)
134  TYPE(xt_xmap) :: xmaps(num_tx)
135  TYPE(xt_redist) :: redists(num_tx), redist, redist_copy
136  INTEGER(mpi_address_kind) :: src_displacements(num_tx), &
137  dst_displacements(num_tx)
138  INTEGER :: i, ierror, size_a, size_b, size_all
139  INTEGER(xt_int_kind), POINTER :: results_1(:), &
140  results_2(:), results_3(:), results_4(:)
141 
142  size_a = 2 * comm_size**2
143  size_b = 2 * comm_size**2
144  size_all = 2 * comm_size**3
145 
146  ALLOCATE(src(size_a + size_b + size_all), dst(size_b + size_a + 2*size_all))
147 
148  index_vector_a => src(1:size_a)
149  index_vector_b => src(size_a+1:size_a+size_b)
150  index_vector_all => src(size_a+size_b+1:)
151 
152  results_1 => dst(1:size_b)
153  results_2 => dst(size_b+1:size_b+size_a)
154  results_3 => dst(size_b+size_a+1:size_b+size_a+size_all)
155  results_4 => dst(size_b+size_a+size_all+1:size_b+size_a+2*size_all)
156 
157  CALL build_idxlists(indices_a, indices_b, indices_all)
158 
159  CALL xt_idxlist_get_indices(indices_a, index_vector_a)
160  CALL xt_idxlist_get_indices(indices_b, index_vector_b)
161  CALL xt_idxlist_get_indices(indices_all, index_vector_all)
162 
163  xmaps(1) = xt_xmap_all2all_new(indices_a, indices_b, mpi_comm_world)
164  xmaps(2) = xt_xmap_all2all_new(indices_b, indices_a, mpi_comm_world)
165  xmaps(3) = xt_xmap_all2all_new(indices_a, indices_all, mpi_comm_world)
166  xmaps(4) = xt_xmap_all2all_new(indices_b, indices_all, mpi_comm_world)
167 
168  CALL xt_idxlist_delete(indices_a)
169  CALL xt_idxlist_delete(indices_b)
170  CALL xt_idxlist_delete(indices_all)
171 
172  DO i = 1, num_tx
173  redists(i) = xt_redist_p2p_new(xmaps(i), xt_int_mpidt)
174  CALL xt_xmap_delete(xmaps(i))
175  END DO
176 
177  CALL mpi_get_address(index_vector_a, src_displacements(1), ierror)
178  CALL mpi_get_address(index_vector_b, src_displacements(2), ierror)
179  CALL mpi_get_address(index_vector_a, src_displacements(3), ierror)
180  CALL mpi_get_address(index_vector_b, src_displacements(4), ierror)
181 
182  src_displacements = src_displacements - src_displacements(1)
183 
184  CALL mpi_get_address(results_1, dst_displacements(1), ierror)
185  CALL mpi_get_address(results_2, dst_displacements(2), ierror)
186  CALL mpi_get_address(results_3, dst_displacements(3), ierror)
187  CALL mpi_get_address(results_4, dst_displacements(4), ierror)
188 
189  dst_displacements = dst_displacements - dst_displacements(1)
190 
191  redist = xt_redist_collection_static_new(redists, num_tx, &
192  src_displacements, dst_displacements, mpi_comm_world)
193 
194  ! test communicator of redist
195  ! if (!test_communicator(xt_redist_get_MPI_Comm(redist), MPI_COMM_WORLD))
196  ! PUT_ERR("error in xt_redist_get_MPI_Comm\n");
197 
198  CALL xt_redist_delete(redists)
199 
200  CALL test_transpose_gather(redist, dst, size_a, size_b, size_all, &
201  index_vector_a, index_vector_b, index_vector_all)
202  redist_copy = xt_redist_copy(redist)
203  CALL xt_redist_delete(redist)
204  CALL test_transpose_gather(redist_copy, dst, size_a, size_b, size_all, &
205  index_vector_a, index_vector_b, index_vector_all)
206 
207  ! clean up
208  CALL xt_redist_delete(redist_copy)
209  END SUBROUTINE test_4redist
210 
211  SUBROUTINE test_transpose_gather(redist, dst, size_a, size_b, &
212  size_all, index_vector_a, index_vector_b, index_vector_all)
213  TYPE(xt_redist), INTENT(in) :: redist
214  INTEGER, INTENT(in) :: size_a, size_b, size_all
215  INTEGER(xt_int_kind), TARGET, INTENT(inout) :: dst(size_b+size_a+2*size_all)
216  INTEGER(xt_int_kind), TARGET, INTENT(in) :: index_vector_a(size_a)
217  INTEGER(xt_int_kind), INTENT(in) :: index_vector_b(size_b), &
218  index_vector_all(size_all)
219 
220  INTEGER(xt_int_kind), POINTER :: results_1(:), &
221  results_2(:), results_3(:), results_4(:)
222  TYPE(c_ptr) :: results(1), input(1)
223 
224  results_1 => dst(1:size_b)
225  results_2 => dst(size_b+1:size_b+size_a)
226  results_3 => dst(size_b+size_a+1:size_b+size_a+size_all)
227  results_4 => dst(size_b+size_a+size_all+1:size_b+size_a+2*size_all)
228 
229  dst = 0
230  input(1) = c_loc(index_vector_a(1))
231  results(1) = c_loc(results_1(1))
232 
233  CALL xt_redist_s_exchange(redist, 1, input, results)
234 
235  ! check results
236  IF (any(results_1(:) /= index_vector_b)) &
237  CALL test_abort("error on xt_redist_s_exchange", &
238  __file__, &
239  __line__)
240 
241  IF (any(results_2(:) /= index_vector_a)) &
242  CALL test_abort("error on xt_redist_s_exchange", &
243  __file__, &
244  __line__)
245 
246  IF (any(results_3(:) /= index_vector_all)) &
247  CALL test_abort("error on xt_redist_s_exchange", &
248  __file__, &
249  __line__)
250 
251  IF (any(results_4(:) /= index_vector_all)) &
252  CALL test_abort("error on xt_redist_s_exchange", &
253  __file__, &
254  __line__)
255 
256  END SUBROUTINE test_transpose_gather
257 
258  ! redist test with two redists that do a round robin exchange in
259  ! different directions
260  SUBROUTINE test_rr_exchange
261  TYPE(xt_idxlist) :: src_idxlist, dst_idxlist
262  INTEGER, PARAMETER :: num_local_indices = 5
263  INTEGER(xt_int_kind) :: src_indices(num_local_indices)
264  INTEGER(xt_int_kind) :: i_xi, temp, dst_indices(num_local_indices, 2)
265  INTEGER(xt_int_kind) :: num_indices_global
266  INTEGER :: i
267  TYPE(xt_xmap) :: xmaps(2)
268  TYPE(xt_redist) :: redists(2), redist
269  INTEGER(xt_int_kind) :: results(num_local_indices, 2)
270  INTEGER(mpi_address_kind) :: src_displacements(2), dst_displacements(2), &
271  addr_temp
272  INTEGER :: ierror
273 
274  num_indices_global = int(comm_size, xi) * int(num_local_indices, xi)
275  DO i = 1, num_local_indices
276  i_xi = int(i, xi)
277  src_indices(i) &
278  = int(rank, xi) * int(num_local_indices, xi) + (i_xi - 1_xi)
279  dst_indices(i, 1) = mod(src_indices(i) + 1_xi, num_indices_global)
280  temp = src_indices(i) - 1_xi
281  dst_indices(i, 2) = merge(num_indices_global - 1_xi, temp, temp < 0_xi)
282  END DO
283 
284  src_idxlist = xt_idxvec_new(src_indices, num_local_indices)
285  DO i = 1, 2
286  dst_idxlist = xt_idxvec_new(dst_indices(:, i))
287  xmaps(i) = xt_xmap_all2all_new(src_idxlist, dst_idxlist, mpi_comm_world)
288  CALL xt_idxlist_delete(dst_idxlist)
289  redists(i) = xt_redist_p2p_new(xmaps(i), xt_int_mpidt)
290  CALL xt_xmap_delete(xmaps(i))
291  END DO
292 
293  CALL xt_idxlist_delete(src_idxlist)
294 
295  src_displacements = 0_mpi_address_kind
296  dst_displacements(1) = 0_mpi_address_kind
297  CALL mpi_get_address(results(:, 2), dst_displacements(2), ierror)
298  IF (ierror /= mpi_success) &
299  CALL test_abort("error in mpi_get_address", &
300  __file__, &
301  __line__)
302  CALL mpi_get_address(results(:, 1), addr_temp, ierror)
303  IF (ierror /= mpi_success) &
304  CALL test_abort("error in mpi_get_address", &
305  __file__, &
306  __line__)
307  dst_displacements(2) = dst_displacements(2) - addr_temp
308 
309  redist = xt_redist_collection_static_new(redists, 2, src_displacements, &
310  dst_displacements, mpi_comm_world)
311 
312  ! test communicator of redist
313  ! IF (!test_communicator(xt_redist_get_MPI_Comm(redist), MPI_COMM_WORLD))
314  ! PUT_ERR("error in xt_redist_get_MPI_Comm\n");
315 
316  CALL xt_redist_delete(redists)
317 
318  CALL check_redist_xi(redist, src_indices, SIZE(dst_indices), &
319  results, dst_indices)
320 
321  ! clean up
322  CALL xt_redist_delete(redist)
323  END SUBROUTINE test_rr_exchange
324 
325 END PROGRAM test_redist_collection_static_parallel
326 !
327 ! Local Variables:
328 ! f90-continuation-indent: 5
329 ! coding: utf-8
330 ! indent-tabs-mode: nil
331 ! show-trailing-whitespace: t
332 ! require-trailing-newline: t
333 ! End:
334 !