Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Routines to reshape / redistribute tensors
10 : !> \author Patrick Seewald
11 : ! **************************************************************************************************
12 : MODULE dbt_reshape_ops
13 : #:include "dbt_macros.fypp"
14 : #:set maxdim = maxrank
15 : #:set ndims = range(2,maxdim+1)
16 :
17 : USE OMP_LIB, ONLY: omp_get_num_threads, omp_get_thread_num
18 : USE dbt_block, ONLY: &
19 : dbt_iterator_type, dbt_iterator_next_block, dbt_iterator_blocks_left, dbt_iterator_start, &
20 : dbt_iterator_stop, dbt_get_block_data, dbt_put_block_data, dbt_reserve_blocks, dbt_copy_block
21 : USE dbt_tas_mm, ONLY: dbt_tas_batched_mm_complete
22 : USE dbt_types, ONLY: dbt_blk_sizes, &
23 : dbt_type, &
24 : dbt_get_info, &
25 : ndims_tensor, &
26 : dbt_get_stored_coordinates, &
27 : dbt_clear
28 : USE kinds, ONLY: dp
29 : USE matrix_communication, ONLY: exchange_matrix_buffers, matrix_buffer_type
30 : USE message_passing, ONLY: mp_comm_type
31 : USE util, ONLY: get_limit
32 :
33 : #include "../base/base_uses.f90"
34 :
35 : IMPLICIT NONE
36 : PRIVATE
37 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'dbt_reshape_ops'
38 :
39 : PUBLIC :: dbt_reshape, dbt_copy_indexed, dbt_reshape_release
40 :
41 : ! Non-owning references allow scalar copies and image gathers to share one transport.
42 : TYPE tensor_source_type
43 : TYPE(dbt_type), POINTER :: tensor => NULL()
44 : INTEGER :: stack_offset = 0
45 : END TYPE tensor_source_type
46 :
47 : INTERFACE dbt_copy_indexed
48 : MODULE PROCEDURE dbt_reshape, dbt_gather
49 : END INTERFACE dbt_copy_indexed
50 :
51 : ! Communication buffers persist across calls such that pages are faulted in once rather than per
52 : ! call; per block, the index holds the data offset (relative to the peer's data) and nd-index.
53 : TYPE(matrix_buffer_type), SAVE :: buffer_send, buffer_recv
54 :
55 : CONTAINS
56 :
57 : ! **************************************************************************************************
58 : !> \brief copy data (involves reshape)
59 : !> tensor_out = tensor_out + tensor_in move_data memory optimization:
60 : !> transfer data from tensor_in to tensor_out s.t. tensor_in is empty on return
61 : !> \param bounds crop tensor data while copying (as dbt_crop): start and end index per dimension
62 : !> \param tensor_in source tensor on the parent communicator
63 : !> \param tensor_out destination tensor; smaller communicators denote equal, contiguous subgroups
64 : !> \param summation add to destination instead of replacing it
65 : !> \param move_data clear the source after copying
66 : !> \param index_map optional source-to-destination block indices; zero skips a block
67 : !> \param group_mask optional subgroup selection by source block index
68 : !> \param group_dim source dimension indexing group_mask
69 : !> \author Ole Schuett
70 : ! **************************************************************************************************
71 373911 : SUBROUTINE dbt_reshape(tensor_in, tensor_out, summation, move_data, bounds, index_map, group_mask, group_dim)
72 :
73 : TYPE(dbt_type), TARGET, INTENT(INOUT) :: tensor_in
74 : TYPE(dbt_type), INTENT(INOUT) :: tensor_out
75 : LOGICAL, INTENT(IN), OPTIONAL :: summation, move_data
76 : INTEGER, INTENT(IN), OPTIONAL :: bounds(:, :), index_map(:, :), group_dim
77 : LOGICAL, INTENT(IN), OPTIONAL :: group_mask(:, :)
78 :
79 747822 : TYPE(tensor_source_type) :: sources(1)
80 :
81 373911 : sources(1)%tensor => tensor_in
82 : CALL dbt_reshape_sources(sources, tensor_out, tensor_in%pgrid%mp_comm_2d, summation, move_data, bounds, &
83 1478844 : index_map=index_map, group_mask=group_mask, group_dim=group_dim)
84 373911 : END SUBROUTINE dbt_reshape
85 :
86 : ! **************************************************************************************************
87 : !> \brief Gather whole blocks from selected tensors, optionally relabelling each block index.
88 : !> Zero images or mapped indices are skipped. Blocks retain their sizes. Each image occupies
89 : !> one source tensor extent along stack_dim, including skipped images. Retained index maps
90 : !> must be injective; the stack-dimension map stays within one source extent.
91 : !> \param tensor_in source tensors on one communicator, with identical shapes
92 : !> \param tensor_out destination tensor
93 : !> \param images source tensor indices; zero skips an image
94 : !> \param index_map source-to-destination block indices, padded to the largest source dimension
95 : !> \param stack_dim dimension along which images are stacked
96 : ! **************************************************************************************************
97 22364 : SUBROUTINE dbt_gather(tensor_in, tensor_out, images, index_map, stack_dim)
98 : TYPE(dbt_type), TARGET, INTENT(INOUT) :: tensor_in(:)
99 : TYPE(dbt_type), INTENT(INOUT) :: tensor_out
100 : INTEGER, INTENT(IN) :: images(:), index_map(:, :), stack_dim
101 :
102 44728 : INTEGER :: i, nblks(SIZE(index_map, 2))
103 308380 : TYPE(tensor_source_type) :: sources(SIZE(images))
104 :
105 22364 : CPASSERT(SIZE(tensor_in) > 0)
106 22364 : CPASSERT(SIZE(index_map, 2) == ndims_tensor(tensor_out))
107 22364 : CPASSERT(stack_dim >= 1 .AND. stack_dim <= SIZE(nblks))
108 22364 : CALL dbt_get_info(tensor_in(1), nblks_total=nblks)
109 89456 : CPASSERT(SIZE(index_map, 1) >= MAXVAL(nblks))
110 73416 : CPASSERT(ALL(index_map(:nblks(stack_dim), stack_dim) <= nblks(stack_dim)))
111 308380 : DO i = 1, SIZE(images)
112 286016 : CPASSERT(images(i) >= 0 .AND. images(i) <= SIZE(tensor_in))
113 286016 : IF (images(i) == 0) CYCLE
114 169104 : CALL dbt_tas_batched_mm_complete(tensor_in(images(i))%matrix_rep, warn=.TRUE.)
115 169104 : sources(i)%tensor => tensor_in(images(i))
116 308380 : sources(i)%stack_offset = (i - 1)*nblks(stack_dim)
117 : END DO
118 : ! As in dbt_copy, invalidate any input replicas kept by the previous contraction.
119 22364 : CALL dbt_tas_batched_mm_complete(tensor_out%matrix_rep, warn=.TRUE.)
120 : CALL dbt_reshape_sources(sources, tensor_out, tensor_in(1)%pgrid%mp_comm_2d, &
121 22364 : index_map=index_map, stack_dim=stack_dim)
122 22364 : END SUBROUTINE dbt_gather
123 :
124 : ! **************************************************************************************************
125 : !> \brief Shared block transport for copies, indexed gathers and subgroup replication.
126 : !> Subgroups have identical layouts and contiguous rank ranges in comm. group_mask selects
127 : !> replicas by a source block index; with no mask, every subgroup receives the block.
128 : ! **************************************************************************************************
129 792550 : SUBROUTINE dbt_reshape_sources(sources, tensor_out, comm, summation, move_data, bounds, &
130 396275 : index_map, stack_dim, group_mask, group_dim)
131 :
132 : TYPE(tensor_source_type), INTENT(IN) :: sources(:)
133 : TYPE(dbt_type), INTENT(INOUT) :: tensor_out
134 : CLASS(mp_comm_type), INTENT(IN) :: comm
135 : LOGICAL, INTENT(IN), OPTIONAL :: summation
136 : LOGICAL, INTENT(IN), OPTIONAL :: move_data
137 : INTEGER, INTENT(IN), OPTIONAL :: bounds(:, :), index_map(:, :), stack_dim, group_dim
138 : LOGICAL, INTENT(IN), OPTIONAL :: group_mask(:, :)
139 :
140 : CHARACTER(LEN=*), PARAMETER :: routineN = 'dbt_reshape'
141 :
142 : INTEGER :: iproc, numnodes, nindex, &
143 : handle, handle2, iblk, offset, ndata, &
144 : isource, d, igroup, ngroups, group_size, peer, first_offset
145 : INTEGER, DIMENSION(2) :: block_range
146 : TYPE(dbt_iterator_type) :: iter
147 : CHARACTER(LEN=32) :: name_pack, name_unpack
148 396275 : INTEGER, DIMENSION(ndims_tensor(tensor_out)) :: blk_size, blk_offset, ind_nd, dest, dest_size
149 792550 : INTEGER, DIMENSION(2, ndims_tensor(tensor_out)) :: bounds_prv
150 : LOGICAL :: found, summation_prv, move_prv, do_bounds, overlap, inside
151 : TYPE(dbt_type), POINTER :: tensor_in
152 792550 : INTEGER :: source_shape(ndims_tensor(tensor_out))
153 :
154 396275 : INTEGER, ALLOCATABLE :: counts_send(:, :), counts_recv(:, :)
155 396275 : INTEGER, ALLOCATABLE :: nblks_send_mythread(:), ndata_send_mythread(:)
156 :
157 396275 : CALL timeset(routineN, handle)
158 :
159 396275 : IF (PRESENT(summation)) THEN
160 145887 : summation_prv = summation
161 : ELSE
162 250388 : summation_prv = .FALSE.
163 : END IF
164 :
165 396275 : IF (PRESENT(move_data)) THEN
166 349343 : move_prv = move_data
167 : ELSE
168 : move_prv = .FALSE.
169 : END IF
170 :
171 396275 : do_bounds = PRESENT(bounds)
172 396275 : IF (do_bounds) THEN
173 9380 : bounds_prv(:, :) = bounds(:, :)
174 : ELSE
175 3578052 : bounds_prv(:, :) = 0
176 : END IF
177 :
178 396275 : CPASSERT(tensor_out%valid)
179 396275 : group_size = tensor_out%pgrid%mp_comm_2d%num_pe
180 396275 : CPASSERT(MOD(comm%num_pe, group_size) == 0)
181 396275 : CPASSERT(MOD(comm%mepos, group_size) == tensor_out%pgrid%mp_comm_2d%mepos)
182 396275 : ngroups = comm%num_pe/group_size
183 396275 : CPASSERT(PRESENT(group_mask) .EQV. PRESENT(group_dim))
184 396275 : IF (PRESENT(group_mask)) THEN
185 15862 : CPASSERT(SIZE(group_mask, 1) == ngroups)
186 15862 : CPASSERT(group_dim >= 1 .AND. group_dim <= SIZE(ind_nd))
187 : END IF
188 396275 : IF (PRESENT(index_map)) THEN
189 22364 : CPASSERT(SIZE(index_map, 2) == SIZE(ind_nd))
190 16964996 : CPASSERT(ALL(index_map >= 0))
191 : END IF
192 :
193 396275 : IF (.NOT. summation_prv) THEN
194 250448 : CALL timeset(routineN//"_clear", handle2)
195 250448 : CALL dbt_clear(tensor_out)
196 250448 : CALL timestop(handle2)
197 : END IF
198 :
199 396275 : numnodes = comm%num_pe
200 396275 : nindex = ndims_tensor(tensor_out) + 1
201 : ! separate timers for blocks that need a reshape (index order differs from matrix layout)
202 396275 : name_pack = routineN//"_pack"
203 1056202 : DO isource = 1, SIZE(sources)
204 659927 : IF (.NOT. ASSOCIATED(sources(isource)%tensor)) CYCLE
205 543015 : CALL dbt_get_info(sources(isource)%tensor, nblks_total=source_shape)
206 543015 : IF (PRESENT(index_map)) THEN
207 676416 : CPASSERT(SIZE(index_map, 1) >= MAXVAL(source_shape))
208 : END IF
209 543015 : IF (PRESENT(group_mask)) THEN
210 15862 : CPASSERT(SIZE(group_mask, 2) == source_shape(group_dim))
211 : END IF
212 939290 : IF (permuted(sources(isource)%tensor)) name_pack = routineN//"_pack_perm"
213 : END DO
214 396275 : name_unpack = routineN//"_unpack"
215 396275 : IF (permuted(tensor_out)) name_unpack = routineN//"_unpack_perm"
216 396275 : IF (numnodes == 1) THEN
217 334480 : CALL dbt_reshape_local(sources, tensor_out, summation_prv, bounds, index_map, stack_dim, group_mask, group_dim)
218 : ELSE
219 1232072 : ALLOCATE (counts_send(2, 0:numnodes - 1), counts_recv(2, 0:numnodes - 1), source=0)
220 :
221 : !$OMP PARALLEL DEFAULT(NONE) &
222 : !$OMP SHARED(sources,tensor_out,summation,comm,numnodes,nindex,name_pack,name_unpack) &
223 : !$OMP SHARED(index_map,stack_dim,group_mask,group_dim,group_size,ngroups) &
224 : !$OMP SHARED(do_bounds,bounds_prv,buffer_send,buffer_recv) &
225 : !$OMP SHARED(counts_send,counts_recv) &
226 : !$OMP PRIVATE(nblks_send_mythread,ndata_send_mythread) &
227 : !$OMP PRIVATE(iter,ind_nd,blk_size,blk_offset,found,overlap,inside,iproc,handle2) &
228 308018 : !$OMP PRIVATE(block_range,offset,ndata,iblk,tensor_in,isource,d,dest,dest_size,igroup,peer,first_offset)
229 : CALL timeset(routineN//"_count", handle2)
230 : ALLOCATE (nblks_send_mythread(0:numnodes - 1), ndata_send_mythread(0:numnodes - 1), source=0)
231 :
232 : DO isource = 1, SIZE(sources)
233 : tensor_in => sources(isource)%tensor
234 : IF (.NOT. ASSOCIATED(tensor_in)) CYCLE
235 : CALL dbt_iterator_start(iter, tensor_in)
236 : DO WHILE (dbt_iterator_blocks_left(iter))
237 : CALL dbt_iterator_next_block(iter, ind_nd, blk_size=blk_size, blk_offset=blk_offset)
238 : IF (do_bounds) THEN
239 : CALL block_bounds(bounds_prv, blk_offset, blk_size, overlap, inside)
240 : IF (.NOT. overlap) CYCLE
241 : END IF
242 : dest = ind_nd
243 : IF (PRESENT(index_map)) THEN
244 : DO d = 1, SIZE(dest)
245 : dest(d) = index_map(ind_nd(d), d)
246 : END DO
247 : IF (ANY(dest == 0)) CYCLE
248 : END IF
249 : IF (PRESENT(stack_dim)) dest(stack_dim) = dest(stack_dim) + sources(isource)%stack_offset
250 : IF (PRESENT(index_map)) THEN
251 : CALL dbt_blk_sizes(tensor_out, dest, dest_size)
252 : CPASSERT(ALL(dest_size == blk_size))
253 : END IF
254 : CALL dbt_get_stored_coordinates(tensor_out, dest, iproc)
255 : DO igroup = 1, ngroups
256 : IF (PRESENT(group_mask)) THEN
257 : IF (.NOT. group_mask(igroup, ind_nd(group_dim))) CYCLE
258 : END IF
259 : peer = (igroup - 1)*group_size + iproc
260 : nblks_send_mythread(peer) = nblks_send_mythread(peer) + 1
261 : ndata_send_mythread(peer) = ndata_send_mythread(peer) + PRODUCT(blk_size)
262 : END DO
263 : END DO
264 : CALL dbt_iterator_stop(iter)
265 : END DO
266 : !$OMP CRITICAL(omp_dbt_reshape)
267 : counts_send(1, :) = counts_send(1, :) + ndata_send_mythread(:)
268 : counts_send(2, :) = counts_send(2, :) + nblks_send_mythread(:)
269 : ndata_send_mythread(:) = counts_send(1, :) ! current totals indicate slot for this thread
270 : nblks_send_mythread(:) = counts_send(2, :)
271 : !$OMP END CRITICAL(omp_dbt_reshape)
272 : !$OMP BARRIER
273 :
274 : !$OMP MASTER
275 : CALL comm%alltoall(counts_send, counts_recv, 2)
276 : !$OMP END MASTER
277 : !$OMP BARRIER
278 : CALL timestop(handle2)
279 :
280 : CALL timeset(TRIM(name_pack), handle2)
281 : !$OMP MASTER
282 : CALL grow_buffer(buffer_send, counts_send(1, :), counts_send(2, :), nindex)
283 : CALL grow_buffer(buffer_recv, counts_recv(1, :), counts_recv(2, :), nindex)
284 : !$OMP END MASTER
285 : !$OMP BARRIER
286 :
287 : DO isource = 1, SIZE(sources)
288 : tensor_in => sources(isource)%tensor
289 : IF (.NOT. ASSOCIATED(tensor_in)) CYCLE
290 : CALL dbt_iterator_start(iter, tensor_in)
291 : DO WHILE (dbt_iterator_blocks_left(iter))
292 : CALL dbt_iterator_next_block(iter, ind_nd, blk_size=blk_size, blk_offset=blk_offset)
293 : inside = .TRUE.
294 : IF (do_bounds) THEN
295 : CALL block_bounds(bounds_prv, blk_offset, blk_size, overlap, inside)
296 : IF (.NOT. overlap) CYCLE
297 : END IF
298 : dest = ind_nd
299 : IF (PRESENT(index_map)) THEN
300 : DO d = 1, SIZE(dest)
301 : dest(d) = index_map(ind_nd(d), d)
302 : END DO
303 : IF (ANY(dest == 0)) CYCLE
304 : END IF
305 : IF (PRESENT(stack_dim)) dest(stack_dim) = dest(stack_dim) + sources(isource)%stack_offset
306 : CALL dbt_get_stored_coordinates(tensor_out, dest, iproc)
307 : first_offset = -1
308 : DO igroup = 1, ngroups
309 : IF (PRESENT(group_mask)) THEN
310 : IF (.NOT. group_mask(igroup, ind_nd(group_dim))) CYCLE
311 : END IF
312 : peer = (igroup - 1)*group_size + iproc
313 : ! Insert block data, reading the source only once for all replicas.
314 : ndata = PRODUCT(blk_size)
315 : ndata_send_mythread(peer) = ndata_send_mythread(peer) - ndata
316 : offset = buffer_send%msg_offset(peer) + ndata_send_mythread(peer)
317 : IF (first_offset < 0) THEN
318 : CALL dbt_get_block_data(tensor_in, ind_nd, blk_size, buffer_send%msg(offset + 1:offset + ndata), found)
319 : CPASSERT(found)
320 : IF (.NOT. inside) THEN
321 : CALL block_crop(bounds_prv, blk_offset, blk_size, buffer_send%msg(offset + 1:offset + ndata))
322 : END IF
323 : first_offset = offset
324 : ELSE
325 : buffer_send%msg(offset + 1:offset + ndata) = buffer_send%msg(first_offset + 1:first_offset + ndata)
326 : END IF
327 : ! Payload offsets in the metadata are relative to the peer's segment.
328 : nblks_send_mythread(peer) = nblks_send_mythread(peer) - 1
329 : iblk = buffer_send%indx_offset(peer) + nblks_send_mythread(peer) + 1
330 : buffer_send%indx(2:, iblk) = dest(:)
331 : buffer_send%indx(1, iblk) = ndata_send_mythread(peer)
332 : END DO
333 : END DO
334 : CALL dbt_iterator_stop(iter)
335 : END DO
336 : DEALLOCATE (nblks_send_mythread, ndata_send_mythread)
337 : !$OMP BARRIER
338 : CALL timestop(handle2)
339 :
340 : !$OMP MASTER
341 : CALL exchange_matrix_buffers(comm, buffer_recv, buffer_send)
342 : !$OMP END MASTER
343 : !$OMP BARRIER
344 :
345 : CALL timeset(routineN//"_reserve", handle2)
346 : block_range = get_limit(buffer_recv%indx_offset(numnodes), omp_get_num_threads(), omp_get_thread_num())
347 : CALL dbt_reserve_blocks(tensor_out, TRANSPOSE(buffer_recv%indx(2:, block_range(1):block_range(2))))
348 : !$OMP BARRIER
349 : CALL timestop(handle2)
350 :
351 : CALL timeset(TRIM(name_unpack), handle2)
352 : DO iproc = 0, numnodes - 1
353 : !$OMP DO
354 : DO iblk = buffer_recv%indx_offset(iproc) + 1, buffer_recv%indx_offset(iproc + 1)
355 : ind_nd(:) = buffer_recv%indx(2:, iblk)
356 : CALL dbt_blk_sizes(tensor_out, ind_nd, blk_size)
357 : offset = buffer_recv%msg_offset(iproc) + buffer_recv%indx(1, iblk)
358 : ndata = PRODUCT(blk_size)
359 : CALL dbt_put_block_data(tensor_out, ind_nd, blk_size, buffer_recv%msg(offset + 1:offset + ndata), &
360 : summation=summation)
361 : END DO
362 : !$OMP END DO
363 : END DO
364 : CALL timestop(handle2)
365 : !$OMP END PARALLEL
366 :
367 308018 : DEALLOCATE (counts_send, counts_recv)
368 : END IF
369 :
370 396275 : IF (move_prv) THEN
371 250035 : CALL timeset(routineN//"_clear", handle2)
372 500070 : DO isource = 1, SIZE(sources)
373 500070 : IF (ASSOCIATED(sources(isource)%tensor)) CALL dbt_clear(sources(isource)%tensor)
374 : END DO
375 250035 : CALL timestop(handle2)
376 : END IF
377 :
378 396275 : CALL timestop(handle)
379 792550 : END SUBROUTINE dbt_reshape_sources
380 :
381 : ! **************************************************************************************************
382 : !> \brief Copy single-rank tensors without communication buffers or a separate reserve pass.
383 : !> Matching block layouts are copied directly; other layouts and partial crops use one
384 : !> reusable buffer per thread.
385 : ! **************************************************************************************************
386 88257 : SUBROUTINE dbt_reshape_local(sources, tensor_out, summation, bounds, index_map, stack_dim, group_mask, group_dim)
387 : TYPE(tensor_source_type), INTENT(IN) :: sources(:)
388 : TYPE(dbt_type), INTENT(INOUT) :: tensor_out
389 : LOGICAL, INTENT(IN) :: summation
390 : INTEGER, INTENT(IN), OPTIONAL :: bounds(:, :), index_map(:, :), stack_dim, group_dim
391 : LOGICAL, INTENT(IN), OPTIONAL :: group_mask(:, :)
392 :
393 : INTEGER :: isource, d, ndata
394 88257 : INTEGER :: ind(ndims_tensor(tensor_out)), dest(ndims_tensor(tensor_out)), &
395 88257 : sizes(ndims_tensor(tensor_out)), offsets(ndims_tensor(tensor_out)), &
396 88257 : dest_sizes(ndims_tensor(tensor_out))
397 176514 : LOGICAL :: direct_copy(SIZE(sources)), found, overlap, inside
398 88257 : REAL(dp), ALLOCATABLE :: data(:)
399 : TYPE(dbt_type), POINTER :: tensor_in
400 : TYPE(dbt_iterator_type) :: iter
401 :
402 417270 : DO isource = 1, SIZE(sources)
403 329013 : tensor_in => sources(isource)%tensor
404 329013 : IF (.NOT. ASSOCIATED(tensor_in)) CYCLE
405 : direct_copy(isource) = &
406 : SIZE(tensor_in%nd_index_blk%map1_2d) == SIZE(tensor_out%nd_index_blk%map1_2d) .AND. &
407 1025163 : ALL(tensor_in%nd_index_blk%map_nd == tensor_out%nd_index_blk%map_nd)
408 : END DO
409 :
410 : !$OMP PARALLEL DEFAULT(NONE) &
411 : !$OMP SHARED(sources,tensor_out,summation,bounds,index_map,stack_dim,group_mask,group_dim,direct_copy) &
412 88257 : !$OMP PRIVATE(iter,tensor_in,isource,ind,dest,sizes,offsets,dest_sizes,d,ndata,data,found,overlap,inside)
413 : DO isource = 1, SIZE(sources)
414 : tensor_in => sources(isource)%tensor
415 : IF (.NOT. ASSOCIATED(tensor_in)) CYCLE
416 : CALL dbt_iterator_start(iter, tensor_in)
417 : DO WHILE (dbt_iterator_blocks_left(iter))
418 : CALL dbt_iterator_next_block(iter, ind, blk_size=sizes, blk_offset=offsets)
419 : IF (PRESENT(group_mask)) THEN
420 : IF (.NOT. group_mask(1, ind(group_dim))) CYCLE
421 : END IF
422 : inside = .TRUE.
423 : IF (PRESENT(bounds)) THEN
424 : CALL block_bounds(bounds, offsets, sizes, overlap, inside)
425 : IF (.NOT. overlap) CYCLE
426 : END IF
427 : dest(:) = ind(:)
428 : IF (PRESENT(index_map)) THEN
429 : DO d = 1, SIZE(dest)
430 : dest(d) = index_map(ind(d), d)
431 : END DO
432 : IF (ANY(dest == 0)) CYCLE
433 : END IF
434 : IF (PRESENT(stack_dim)) dest(stack_dim) = dest(stack_dim) + sources(isource)%stack_offset
435 : IF (PRESENT(index_map)) THEN
436 : CALL dbt_blk_sizes(tensor_out, dest, dest_sizes)
437 : CPASSERT(ALL(dest_sizes == sizes))
438 : END IF
439 : IF (direct_copy(isource) .AND. inside) THEN
440 : CALL dbt_copy_block(tensor_in, tensor_out, ind, summation=summation, ind_out=dest)
441 : ELSE
442 : ndata = PRODUCT(sizes)
443 : IF (.NOT. ALLOCATED(data)) ALLOCATE (data(ndata))
444 : IF (SIZE(data) < ndata) THEN
445 : DEALLOCATE (data)
446 : ALLOCATE (data(ndata))
447 : END IF
448 : CALL dbt_get_block_data(tensor_in, ind, sizes, data(:ndata), found)
449 : CPASSERT(found)
450 : IF (.NOT. inside) CALL block_crop(bounds, offsets, sizes, data(:ndata))
451 : CALL dbt_put_block_data(tensor_out, dest, sizes, data(:ndata), summation=summation)
452 : END IF
453 : END DO
454 : CALL dbt_iterator_stop(iter)
455 : END DO
456 : IF (ALLOCATED(data)) DEALLOCATE (data)
457 : !$OMP END PARALLEL
458 176514 : END SUBROUTINE dbt_reshape_local
459 :
460 : ! **************************************************************************************************
461 : !> \brief Release the communication buffers that persist across calls of dbt_reshape.
462 : !> \author Hans Pabst
463 : ! **************************************************************************************************
464 2934 : SUBROUTINE dbt_reshape_release()
465 2934 : CALL release_buffer(buffer_send)
466 2934 : CALL release_buffer(buffer_recv)
467 2934 : END SUBROUTINE dbt_reshape_release
468 :
469 : ! **************************************************************************************************
470 : !> \brief Whether the tensor's index order differs from its matrix layout (blocks need a reshape).
471 : !> \author Hans Pabst
472 : ! **************************************************************************************************
473 939290 : FUNCTION permuted(tensor)
474 : TYPE(dbt_type), INTENT(IN) :: tensor
475 : LOGICAL :: permuted
476 :
477 : INTEGER :: i
478 :
479 : ASSOCIATE (map1_2d => tensor%nd_index_blk%map1_2d, map2_2d => tensor%nd_index_blk%map2_2d)
480 10916645 : permuted = .NOT. ALL([map1_2d, map2_2d] == [(i, i=1, SIZE(map1_2d) + SIZE(map2_2d))])
481 : END ASSOCIATE
482 939290 : END FUNCTION permuted
483 :
484 : ! **************************************************************************************************
485 : !> \brief Whether a block (element offset and size per dimension) overlaps the bounds, and whether
486 : !> it lies entirely inside them.
487 : !> \author Hans Pabst
488 : ! **************************************************************************************************
489 173988 : PURE SUBROUTINE block_bounds(bounds, blk_offset, blk_size, overlap, inside)
490 : INTEGER, DIMENSION(:, :), INTENT(IN) :: bounds
491 : INTEGER, DIMENSION(:), INTENT(IN) :: blk_offset, blk_size
492 : LOGICAL, INTENT(OUT) :: overlap, inside
493 :
494 430562 : overlap = ALL(blk_offset <= bounds(2, :) .AND. bounds(1, :) <= blk_offset + blk_size - 1)
495 430542 : inside = ALL(bounds(1, :) <= blk_offset .AND. blk_offset + blk_size - 1 <= bounds(2, :))
496 173988 : END SUBROUTINE block_bounds
497 :
498 : ! **************************************************************************************************
499 : !> \brief Zero the elements of a block (column-major data) outside the bounds (as dbt_crop).
500 : !> \author Hans Pabst
501 : ! **************************************************************************************************
502 5 : PURE SUBROUTINE block_crop(bounds, blk_offset, blk_size, data)
503 : INTEGER, DIMENSION(:, :), INTENT(IN) :: bounds
504 : INTEGER, DIMENSION(:), INTENT(IN) :: blk_offset, blk_size
505 : REAL(dp), DIMENSION(:), INTENT(INOUT) :: data
506 :
507 : INTEGER :: d, i
508 10 : INTEGER, DIMENSION(SIZE(blk_size)) :: hi, idx, lo
509 :
510 20 : lo(:) = MAX(bounds(1, :) - blk_offset + 1, 1)
511 20 : hi(:) = MIN(bounds(2, :) - blk_offset + 1, blk_size)
512 20 : idx(:) = 1
513 5801 : DO i = 1, SIZE(data)
514 13608 : IF (ANY(idx < lo .OR. hi < idx)) data(i) = 0.0_dp
515 6574 : DO d = 1, SIZE(idx) ! next column-major index
516 6564 : idx(d) = idx(d) + 1
517 6564 : IF (idx(d) <= blk_size(d)) EXIT
518 6569 : idx(d) = 1
519 : END DO
520 : END DO
521 5 : END SUBROUTINE block_crop
522 :
523 : ! **************************************************************************************************
524 : !> \brief Lay out a persistent buffer for the given per-peer counts (as allocate_matrix_buffers),
525 : !> growing its arrays only when they are too small.
526 : !> \author Hans Pabst
527 : ! **************************************************************************************************
528 616036 : SUBROUTINE grow_buffer(buffer, ndata, nblks, nindex)
529 : TYPE(matrix_buffer_type), INTENT(INOUT) :: buffer
530 : INTEGER, DIMENSION(0:), INTENT(IN) :: ndata, nblks
531 : INTEGER, INTENT(IN) :: nindex
532 :
533 : INTEGER :: iproc, nprocs
534 :
535 616036 : nprocs = SIZE(ndata)
536 616036 : IF (ALLOCATED(buffer%msg_offset)) THEN
537 614924 : IF (SIZE(buffer%msg_offset) /= nprocs + 1) DEALLOCATE (buffer%msg_offset, buffer%indx_offset)
538 : END IF
539 619372 : IF (.NOT. ALLOCATED(buffer%msg_offset)) ALLOCATE (buffer%msg_offset(0:nprocs), buffer%indx_offset(0:nprocs))
540 616036 : buffer%msg_offset(0) = 0
541 616036 : buffer%indx_offset(0) = 0
542 1848108 : DO iproc = 0, nprocs - 1
543 1232072 : buffer%msg_offset(iproc + 1) = buffer%msg_offset(iproc) + ndata(iproc)
544 1848108 : buffer%indx_offset(iproc + 1) = buffer%indx_offset(iproc) + nblks(iproc)
545 : END DO
546 :
547 616036 : IF (ALLOCATED(buffer%msg)) THEN
548 614924 : IF (SIZE(buffer%msg) < buffer%msg_offset(nprocs)) DEALLOCATE (buffer%msg)
549 : END IF
550 616036 : IF (.NOT. ALLOCATED(buffer%msg)) THEN
551 6456 : ALLOCATE (buffer%msg(MAX(buffer%msg_offset(nprocs) + buffer%msg_offset(nprocs)/4, 1)))
552 : END IF
553 : ! records are exchanged as whole columns: the leading dimension must match
554 616036 : IF (ALLOCATED(buffer%indx)) THEN
555 614924 : IF (SIZE(buffer%indx, 1) /= nindex .OR. SIZE(buffer%indx, 2) < buffer%indx_offset(nprocs)) THEN
556 59926 : DEALLOCATE (buffer%indx)
557 : END IF
558 : END IF
559 616036 : IF (.NOT. ALLOCATED(buffer%indx)) THEN
560 244152 : ALLOCATE (buffer%indx(nindex, MAX(buffer%indx_offset(nprocs) + buffer%indx_offset(nprocs)/4, 1)))
561 : END IF
562 616036 : END SUBROUTINE grow_buffer
563 :
564 : ! **************************************************************************************************
565 : !> \brief Deallocate the arrays of a buffer.
566 : !> \author Hans Pabst
567 : ! **************************************************************************************************
568 5868 : SUBROUTINE release_buffer(buffer)
569 : TYPE(matrix_buffer_type), INTENT(INOUT) :: buffer
570 :
571 5868 : IF (ALLOCATED(buffer%msg)) DEALLOCATE (buffer%msg)
572 5868 : IF (ALLOCATED(buffer%indx)) DEALLOCATE (buffer%indx)
573 5868 : IF (ALLOCATED(buffer%msg_offset)) DEALLOCATE (buffer%msg_offset)
574 5868 : IF (ALLOCATED(buffer%indx_offset)) DEALLOCATE (buffer%indx_offset)
575 5868 : END SUBROUTINE release_buffer
576 :
577 0 : END MODULE dbt_reshape_ops
|