Yet Another eXchange Tool  DO_NOT_EDIT_HERE
test_redist_repeat_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_repeat_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, &
53  xt_idxlist, xt_idxlist_delete, xt_stripe, &
54  xt_idxfsection_new, xt_idxlist_collection_new, xt_idxstripes_new, &
58  xt_idxlist_get_indices, xt_int_mpidt
59  ! older PGI compilers do not handle generic interface correctly
60 #if defined __PGI
64 #endif
65  USE iso_c_binding, ONLY: c_int
66  IMPLICIT NONE
67  INTEGER :: comm_rank, comm_size, ierror
68  CALL init_mpi
69  CALL xt_initialize(mpi_comm_world)
70 
71  CALL mpi_comm_rank(mpi_comm_world, comm_rank, ierror)
72  IF (ierror /= mpi_success) &
73  CALL test_abort('mpi_comm_rank failed', &
74  __file__, &
75  __line__)
76  CALL mpi_comm_size(mpi_comm_world, comm_size, ierror)
77  IF (ierror /= mpi_success) &
78  CALL test_abort('mpi_comm_size failed', &
79  __file__, &
80  __line__)
81 
82  IF (comm_size > 1) THEN
83  CALL test_4redist
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  ! create index lists to exchange data sections from a global 4D
94  ! array.
95  !
96  ! For the source side, the global array is of size W x X x Y x Z,
97  ! where W=X=Y=comm_size and Z=2. The source data is decomposed
98  ! into two shards of size comm_size*comm_size per process, where one
99  ! shard is positioned at (1, comm_rank, 1, 1), the other at (1,
100  ! comm_size-comm_rank, 1, 2), i.e. the decomposition of the last
101  ! dimension is decomposed anti-symmetrically.
102  !
103  ! The destination decomposition is a contiguous subset of the
104  ! interval [0,W*X*Y*Z-1], the stripe [S, S+2*comm_size^2] with S =
105  ! comm_rank * 2 * comm_size**2. This corresponds to a section of a
106  ! 4D array reshaped to [W,X,Z,Y], decomposed along the Y-axis
107  ! according to comm_rank (but enumerated differently from source
108  ! array).
109  SUBROUTINE build_idxlists(indices_a, indices_b)
110  TYPE(xt_idxlist), INTENT(out) :: indices_a, indices_b
111 
112  INTEGER, PARAMETER :: glob_rank = 4
113  TYPE(xt_idxlist) :: indices_a_(2)
114  INTEGER :: i
115  INTEGER(xt_int_kind), PARAMETER :: start = 0
116  INTEGER(xt_int_kind) :: global_size(glob_rank), local_start(glob_rank, 2)
117  INTEGER :: local_size(glob_rank)
118 
119  TYPE(xt_stripe) :: stripe
120 
121  global_size(1) = int(comm_size, xi)
122  global_size(2) = int(comm_size, xi)
123  global_size(3) = int(comm_size, xi)
124  global_size(4) = 2_xi
125  local_size(1) = comm_size
126  local_size(2) = 1
127  local_size(3) = comm_size
128  local_size(4) = 1
129  local_start(1, 1) = 1_xi
130  local_start(2, 1) = int(comm_rank + 1, xi)
131  local_start(3, 1) = 1_xi
132  local_start(4, 1) = 1_xi
133  !
134  local_start(1, 2) = 1_xi
135  local_start(2, 2) = int(comm_size-comm_rank, xi)
136  local_start(3, 2) = 1_xi
137  local_start(4, 2) = 2_xi
138 
139  DO i = 1, 2
140  indices_a_(i) = xt_idxfsection_new(start, global_size, local_size, &
141  local_start(:, i))
142  END DO
143  indices_a = xt_idxlist_collection_new(indices_a_)
144 
145  CALL xt_idxlist_delete(indices_a_(1))
146  CALL xt_idxlist_delete(indices_a_(2))
147 
148  stripe = xt_stripe(start = int(comm_rank * 2 * comm_size**2, xi), &
149  & stride = 1_xi, &
150  & nstrides = int(2*comm_size**2, c_int))
151  indices_b = xt_idxstripes_new(stripe)
152  END SUBROUTINE build_idxlists
153 
154  ! redist test for 4 level repetition of redist (i.e. 3D extension of 2D
155  ! redist)
156  SUBROUTINE test_4redist
157  TYPE(xt_idxlist) :: indices_a, indices_b
158  INTEGER(xt_int_kind) :: index_vector_a(2*comm_size**2), &
159  index_vector_b(2*comm_size**2)
160  TYPE(xt_xmap) :: xmap
161  TYPE(xt_redist) :: redist_repeat, redist_repeat_2, redist_p2p
162  INTEGER(xt_int_kind) :: results_1(2*comm_size**2,4), &
163  results_2(2*comm_size**2,9)
164  INTEGER(xt_int_kind) :: input_data(2*comm_size**2,9)
165  INTEGER(xt_int_kind) :: ref_results_1(2*comm_size**2,4), &
166  ref_results_2(2*comm_size**2,9)
167  INTEGER(mpi_address_kind) :: extent
168  INTEGER(mpi_address_kind) :: base_address, temp_address
169  INTEGER(c_int), PARAMETER :: &
170  displacements(4) = (/ 0_c_int, 1_c_int, 2_c_int, 3_c_int /), &
171  displacements_2(4) = (/ 1_c_int, 2_c_int, 4_c_int, 8_c_int /)
172  INTEGER :: i, ierror
173  INTEGER(xt_int_kind) :: j
174 
175  CALL build_idxlists(indices_a, indices_b)
176 
177  CALL xt_idxlist_get_indices(indices_a, index_vector_a)
178  CALL xt_idxlist_get_indices(indices_b, index_vector_b)
179 
180  xmap = xt_xmap_all2all_new(indices_a, indices_b, mpi_comm_world)
181 
182  CALL xt_idxlist_delete(indices_a)
183  CALL xt_idxlist_delete(indices_b)
184 
185  redist_p2p = xt_redist_p2p_new(xmap, xt_int_mpidt)
186  CALL xt_xmap_delete(xmap)
187 
188  CALL mpi_get_address(input_data(1,1), base_address, ierror)
189  CALL mpi_get_address(input_data(1,2), temp_address, ierror)
190  extent = temp_address - base_address
191 
192  redist_repeat = xt_redist_repeat_new(redist_p2p, extent, extent, &
193  4, displacements)
194  redist_repeat_2 = xt_redist_repeat_new(redist_p2p, extent, extent, &
195  4, displacements_2)
196 
197  CALL xt_redist_delete(redist_p2p)
198 
199  DO j = 1, 9
200  DO i = 1, 2*comm_size**2
201  input_data(i, j) = index_vector_a(i) + (j - 1_xi) * int(4*comm_size**2, xi)
202  END DO
203  END DO
204  results_1 = -1
205  results_2 = -1
206 
207  CALL xt_redist_s_exchange(redist_repeat, input_data, results_1)
208  CALL xt_redist_s_exchange(redist_repeat_2, input_data, results_2)
209 
210  DO j = 1_xi, 4_xi
211  DO i = 1, 2*comm_size**2
212  ref_results_1(i, j) = index_vector_b(i) + (j - 1_xi) * int(4*comm_size**2, xi)
213  END DO
214  END DO
215  DO j = 1, 9
216  DO i = 1, 2*comm_size**2
217  ref_results_2(i, j) = index_vector_b(i) + (j - 1_xi) * int(4*comm_size**2, xi)
218  END DO
219  END DO
220 
221  ref_results_2(:,1:4:3) = -1
222  ref_results_2(:,6:8:1) = -1
223 
224  ! check results
225  IF (any(results_1 /= ref_results_1)) &
226  CALL test_abort("error on xt_redist_s_exchange", &
227  __file__, &
228  __line__)
229  IF (any(results_2 /= ref_results_2)) &
230  CALL test_abort("error on xt_redist_s_exchange", &
231  __file__, &
232  __line__)
233 
234  ! clean up
235 
236  CALL xt_redist_delete(redist_repeat)
237  CALL xt_redist_delete(redist_repeat_2)
238  END SUBROUTINE test_4redist
239 
240 END PROGRAM test_redist_repeat_parallel
241 !
242 ! Local Variables:
243 ! f90-continuation-indent: 5
244 ! coding: utf-8
245 ! indent-tabs-mode: nil
246 ! show-trailing-whitespace: t
247 ! require-trailing-newline: t
248 ! End:
249 !