Octopus
partition.F90
Go to the documentation of this file.
1!! Copyright (C) 2013 M. Oliveira
2!! Copyright (C) 2021 S. Ohlmann
3!!
4!! This program is free software; you can redistribute it and/or modify
5!! it under the terms of the GNU General Public License as published by
6!! the Free Software Foundation; either version 2, or (at your option)
7!! any later version.
8!!
9!! This program is distributed in the hope that it will be useful,
10!! but WITHOUT ANY WARRANTY; without even the implied warranty of
11!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12!! GNU General Public License for more details.
13!!
14!! You should have received a copy of the GNU General Public License
15!! along with this program; if not, write to the Free Software
16!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17!! 02110-1301, USA.
18!!
19
20#include "global.h"
21
22module partition_oct_m
23 use debug_oct_m
24 use global_oct_m
25 use io_oct_m
28 use mpi_oct_m
31
32 implicit none
33
34 private
35
36 public :: &
50
51
71 !
72 type partition_t
73 private
74
75 ! The following components are the same for all processes:
76 type(mpi_grp_t) :: mpi_grp
77 integer(int64) :: np_global
78 integer :: npart = 1
79 integer, allocatable :: np_local_vec(:)
80 integer(int64), allocatable :: istart_vec(:)
81
82 ! The following components are process-dependent:
83 integer :: partno
84 integer :: np_local
85 integer(int64) :: istart
86 integer, allocatable :: part(:)
87 end type partition_t
88
92
93
94contains
95
96 !---------------------------------------------------------
98 !
99 subroutine partition_init(partition, np_global, mpi_grp)
100 type(partition_t), intent(out) :: partition
101 integer(int64), intent(in) :: np_global
102 type(mpi_grp_t), intent(in) :: mpi_grp
103
104 integer :: ipart
105 integer(int64) :: iend
106
107 push_sub(partition_init)
108
109 !Global variables
110 partition%mpi_grp = mpi_grp
111 partition%np_global = np_global
112 partition%npart = mpi_grp%size
113 partition%partno = mpi_grp%rank + 1
114
115 safe_allocate(partition%np_local_vec(1:partition%npart))
116 safe_allocate(partition%istart_vec(1:partition%npart))
117
118 ! use block data decomposition
119 do ipart = 1, partition%npart
120 ! according to Fortran 2008 standard, Sect. 7.1.9.3, point 4, the kind parameter of an
121 ! operation is the one with a larger decimal range, i.e., everything is promoted to i8
122 partition%istart_vec(ipart) = (ipart-1) * np_global/partition%npart + 1
123 iend = ipart * np_global/partition%npart
124 partition%np_local_vec(ipart) = i8_to_i4(iend - partition%istart_vec(ipart) + 1)
125 end do
126 partition%istart = partition%istart_vec(partition%partno)
127 partition%np_local = partition%np_local_vec(partition%partno)
128
129 !Allocate memory for the partition
130 safe_allocate(partition%part(1:partition%np_local))
131
132 pop_sub(partition_init)
133 end subroutine partition_init
134
135 ! ---------------------------------------------------------
136 subroutine partition_end(partition)
137 type(partition_t), intent(inout) :: partition
138
139 push_sub(partition_end)
140
141 safe_deallocate_a(partition%part)
142 safe_deallocate_a(partition%np_local_vec)
143 safe_deallocate_a(partition%istart_vec)
144
145 pop_sub(partition_end)
146 end subroutine partition_end
147
148 ! ---------------------------------------------------------
149 subroutine partition_set(partition, part)
150 type(partition_t), intent(inout) :: partition
151 integer, intent(in) :: part(:)
152
153 push_sub(partition_set)
154
155 partition%part(1:partition%np_local) = part(1:partition%np_local)
156
157 pop_sub(partition_set)
158 end subroutine partition_set
159
160 ! ---------------------------------------------------------
166 subroutine partition_dump(partition, dir, filename, ierr)
167 type(partition_t), intent(in) :: partition
168 character(len=*), intent(in) :: dir
169 character(len=*), intent(in) :: filename
170 integer, intent(out) :: ierr
172 integer :: err
173 character(len=MAX_PATH_LEN) :: full_filename
174
175 push_sub(partition_dump)
177 ierr = 0
178 full_filename = trim(dir)//'/'//trim(filename)
180 ! Write the header (root only) and wait
181 if (mpi_grp_is_root(partition%mpi_grp)) then
182 call iwrite_header(full_filename, partition%np_global, err)
183 if (err /= 0) ierr = ierr + 1
184 end if
185 call partition%mpi_grp%bcast(ierr, 1, mpi_integer, 0)
186
187 assert(all(partition%part(:) > 0))
188
189 ! Each process writes a portion of the partition
190 if (ierr == 0) then
191 call mpi_debug_in(partition%mpi_grp%comm, c_mpi_file_write)
192 ! Only one rank per partition group should write the partition restart information
193 ! Otherwise, more than once is trying to be written data
194 if (mod(mpi_world%rank, mpi_world%size/partition%mpi_grp%size) == 0) then
195 call io_binary_write_parallel(full_filename, partition%mpi_grp%comm, partition%istart, &
196 partition%np_local, partition%part, err)
197 call mpi_debug_out(partition%mpi_grp%comm, c_mpi_file_write)
198 if (err /= 0) ierr = ierr + 2
199 end if
200 end if
201
202 pop_sub(partition_dump)
203 end subroutine partition_dump
204
205 ! ---------------------------------------------------------
210 !
211 subroutine partition_load(partition, dir, filename, ierr)
212 use iso_c_binding, only: c_sizeof
213 type(partition_t), intent(inout) :: partition
214 character(len=*), intent(in) :: dir
215 character(len=*), intent(in) :: filename
216 integer, intent(out) :: ierr
217
218 integer :: err
219 integer(int64) :: np, file_size
220 integer, allocatable :: scounts(:)
221 integer(int64), allocatable :: sdispls(:)
222 character(len=MAX_PATH_LEN) :: full_filename
223
224 push_sub(partition_load)
225
226 ierr = 0
227 full_filename = trim(dir)//'/'//trim(filename)
228
229 ! This is a writing to avoid an optimization of gfortran with -O3
230 write(message(1),'(a,i8)') "Info: number of points in the partition (in root process) =", size(partition%part)
231 call messages_info(1)
232
233 ! Check if the file exists and has the proper size (only world root)
234 if (mpi_world%rank == 0) then
235 call io_binary_get_info(full_filename, np, file_size, err)
236 assert(np == partition%np_global)
237 end if
238
239 ! All nodes need to know the result
240 call mpi_world%bcast(err, 1, mpi_integer, 0)
241 call mpi_world%bcast(file_size, 1, mpi_integer8, 0)
243 if (err /= 0) then
244 ierr = ierr + 1
245 pop_sub(partition_load)
246 return
247 end if
248 ! The size of the file is not as big as np_global
249 if (file_size - 64 /= partition%np_global * c_sizeof(int(0))) then
250 ierr = ierr + 2
251 pop_sub(partition_load)
252 return
253 end if
254
255 ! Calculate displacements for reading
256 safe_allocate(scounts(1:partition%npart))
257 safe_allocate(sdispls(1:partition%npart))
258
259 scounts(:) = partition%np_local_vec(:)
260 sdispls(:) = partition%istart_vec(:) - 1
261
262 assert(sum(scounts(:)) == partition%np_global)
263
264#ifdef HAVE_MPI
265 call mpi_debug_in(partition%mpi_grp%comm, c_mpi_file_read)
266 call io_binary_read_parallel(full_filename, partition%mpi_grp%comm, partition%istart, &
267 partition%np_local, partition%part, err)
268 call mpi_debug_out(partition%mpi_grp%comm, c_mpi_file_read)
269 if (err /= 0) ierr = ierr + 4
270#endif
271
272 if (any(partition%part(:) <= 0)) then
273 write(message(1),'(a)') 'Internal error: some elements of partition are <= 0.'
274 write(message(2),*) 'filename = ', full_filename
275 write(message(3),*) 'scounts = ', scounts(:)
276 write(message(4),*) 'sdispls = ', sdispls(:)
277 call messages_fatal(6)
278 end if
279
280 safe_deallocate_a(scounts)
281 safe_deallocate_a(sdispls)
282
283 pop_sub(partition_load)
284 end subroutine partition_load
285
286 ! ---------------------------------------------------------
287 subroutine partition_get_local_size(partition, istart, np_local)
288 type(partition_t), intent(in) :: partition
289 integer(int64), intent(out) :: istart
290 integer, intent(out) :: np_local
291
293
294 istart = partition%istart
295 np_local = partition%np_local
296
298 end subroutine partition_get_local_size
299
300 ! ---------------------------------------------------------
303 subroutine partition_get_global(partition, part_global, root)
304 type(partition_t), intent(in) :: partition
305 integer, intent(out) :: part_global(:)
306 integer, optional, intent(in) :: root
307
308 integer(int64), allocatable :: rdispls(:)
309 integer, allocatable :: rcounts(:)
310
311 push_sub(partition_get_global)
312
313 safe_allocate(rdispls(1:partition%npart))
314 safe_allocate(rcounts(1:partition%npart))
315
316 rcounts(:) = partition%np_local_vec(:)
317 rdispls(:) = partition%istart_vec(:) - 1
318
319 assert(all(partition%part(1:partition%np_local) > 0))
320
321 if (present(root)) then
322 call partition%mpi_grp%gatherv(partition%part, partition%np_local, mpi_integer, &
323 part_global, rcounts, rdispls, mpi_integer, root)
324 else
325 call partition%mpi_grp%allgatherv(partition%part, partition%np_local, mpi_integer, &
326 part_global, rcounts, rdispls, mpi_integer)
327 end if
328
329 if (.not. present(root) .or. partition%mpi_grp%rank == 0) then
330 assert(all(part_global(:) > 0))
331 end if
332
333#ifndef HAVE_MPI
334 part_global = partition%part
335#endif
336
337 safe_deallocate_a(rdispls)
338 safe_deallocate_a(rcounts)
339
340 pop_sub(partition_get_global)
341 end subroutine partition_get_global
342
343 ! ---------------------------------------------------------
348 subroutine partition_get_partition_number_4(partition, np, points, partno)
349 type(partition_t), intent(in) :: partition
350 integer, intent(in) :: np
351 integer(int64), intent(in) :: points(:)
352 integer, intent(out) :: partno(:)
353
354 integer :: ip, nproc, rnp
355 integer(int64), allocatable :: sbuffer(:), rbuffer(:)
356 integer, allocatable :: scounts(:), rcounts(:)
357 integer, allocatable :: sdispls(:), rdispls(:)
358 integer, allocatable :: ipos(:), order(:)
359
361
362 safe_allocate(scounts(1:partition%npart))
363 safe_allocate(rcounts(1:partition%npart))
364 safe_allocate(sdispls(1:partition%npart))
365 safe_allocate(rdispls(1:partition%npart))
366
367 ! How many points will we have to send/receive from each process?
368 scounts = 0
369 do ip = 1, np
370 ! Who knows where points(ip) is stored?
371 nproc = partition_get_number(partition, points(ip))
372 ! We increase the respective counter
373 scounts(nproc) = scounts(nproc) + 1
374 end do
375
376
377 !Tell each process how many points we will need from it
378 call partition%mpi_grp%alltoall(scounts, 1, mpi_integer, &
379 rcounts, 1, mpi_integer)
381 !Build displacement arrays
382 sdispls(1) = 0
383 rdispls(1) = 0
384 do ip = 2, partition%npart
385 sdispls(ip) = sdispls(ip-1) + scounts(ip-1)
386 rdispls(ip) = rdispls(ip-1) + rcounts(ip-1)
387 end do
388
389
390 rnp = sum(rcounts)
391 safe_allocate(sbuffer(1:np))
392 safe_allocate(rbuffer(1:rnp))
393
394 !Put points in correct order for sending
395 safe_allocate(ipos(1:partition%npart))
396 safe_allocate(order(1:np))
397 ipos = 0
398 do ip = 1, np
399 ! Who knows where points(ip) is stored?
400 nproc = partition_get_number(partition, points(ip))
401
402 !We increase the respective counter
403 ipos(nproc) = ipos(nproc) + 1
404
405 !Put the point in the correct place
406 order(ip) = sdispls(nproc) + ipos(nproc)
407 sbuffer(order(ip)) = points(ip) ! global index of the point
408 end do
409 safe_deallocate_a(ipos)
410
411 !Send the global indices of the points to the process that knows what is the corresponding partition
412 call partition%mpi_grp%alltoallv(sbuffer, scounts, sdispls, mpi_integer8, &
413 rbuffer, rcounts, rdispls, mpi_integer8)
414
415 !We get the partition number from the global index. This will be send back.
416 do ip = 1, rnp
417 if (rbuffer(ip) == 0) cycle
418 rbuffer(ip) = partition%part(rbuffer(ip) - partition%istart + 1)
419 end do
420
421 !Now we send the information backwards
422 call partition%mpi_grp%alltoallv(rbuffer, rcounts, rdispls, mpi_integer8, &
423 sbuffer, scounts, sdispls, mpi_integer8)
424
425 !Reorder the points
426 do ip = 1, np
427 partno(ip) = i8_to_i4(sbuffer(order(ip)))
428 end do
429
430 !Deallocate memory
431 safe_deallocate_a(order)
432 safe_deallocate_a(sbuffer)
433 safe_deallocate_a(scounts)
434 safe_deallocate_a(sdispls)
435 safe_deallocate_a(rbuffer)
436 safe_deallocate_a(rcounts)
437 safe_deallocate_a(rdispls)
438
442 !---------------------------------------------------------
443 subroutine partition_get_partition_number_8(partition, np, points, partno)
444 type(partition_t), intent(in) :: partition
445 integer(int64), intent(in) :: np
446 integer(int64), intent(in) :: points(:)
447 integer, intent(out) :: partno(:)
448
449 integer(int64) :: rounds, offset, iround
450
452
453 rounds = np/huge(0_int32)
454 do iround = 1, rounds
455 offset = (iround - 1)*huge(0_int32) + 1
456 call partition_get_partition_number(partition, huge(0_int32), points(offset:offset+huge(0_int32)), &
457 partno(offset:offset+huge(0_int32)))
458 end do
459 offset = rounds*huge(0_int32) + 1
460 call partition_get_partition_number(partition, i8_to_i4(np-offset+1), points(offset:np), &
461 partno(offset:np))
462
465
466 !---------------------------------------------------------
469 subroutine partition_get_np_local(partition, np_local_vec)
470 type(partition_t), intent(in) :: partition
471 integer, contiguous, intent(inout) :: np_local_vec(:)
472
473 integer, allocatable :: np_local_vec_tmp(:)
474 integer :: ip
475
476 push_sub(partition_get_np_local)
477
478 assert(ubound(np_local_vec, 1) >= partition%npart)
479 assert(partition%npart > 0)
480 assert(all(partition%part(:) > 0))
481 safe_allocate(np_local_vec_tmp(1:partition%npart))
482 np_local_vec_tmp = 0
483
484 ! Calculate locally the local points of each partition
485 do ip = 1, partition%np_local
486 np_local_vec_tmp(partition%part(ip)) = np_local_vec_tmp(partition%part(ip)) + 1
487 end do
488
489 ! Collect all the local points
490 call partition%mpi_grp%allreduce(np_local_vec_tmp, np_local_vec, partition%npart, mpi_integer, mpi_sum)
491 safe_deallocate_a(np_local_vec_tmp)
492
494 end subroutine partition_get_np_local
495
496 !---------------------------------------------------------
498 pure integer function partition_get_npart(partition) result(npart)
499 type(partition_t), intent(in) :: partition
500 npart = partition%npart
501 end function partition_get_npart
502
503 !---------------------------------------------------------
505 pure integer function partition_get_part(partition, local_point) result(part)
506 type(partition_t), intent(in) :: partition
507 integer, intent(in) :: local_point
508 part = partition%part(local_point)
509 end function partition_get_part
510
511 !---------------------------------------------------------
514 pure integer function partition_get_number(partition, global_point) result(part)
515 type(partition_t), intent(in) :: partition
516 integer(int64), intent(in) :: global_point
517
518 if (global_point == 0) then
519 part = partition%partno
520 else
521 part = i8_to_i4((partition%npart*global_point - 1)/partition%np_global + 1)
522 end if
523 end function partition_get_number
524
525 !---------------------------------------------------------
529 subroutine partition_get_local(partition, rbuffer, np_local)
530 type(partition_t), intent(in) :: partition
531 integer(int64), contiguous, intent(inout) :: rbuffer(:)
532 integer, intent(out) :: np_local
533
534 integer :: ip, ipart
535 integer(int64) :: istart
536 integer(int64), allocatable :: sdispls(:), rdispls(:), sbuffer(:)
537 integer, allocatable :: scounts(:), rcounts(:)
538
539 push_sub(partition_get_local)
540
541 safe_allocate(sdispls(1:partition%npart))
542 safe_allocate(scounts(1:partition%npart))
543 safe_allocate(rcounts(1:partition%npart))
544
545 ! Calculate the starting point of the running process
546 istart = partition%istart - 1
547
548 scounts = 0
549 ! Count and store the local points for each partition
550 do ip = 1, partition%np_local
551 ipart = partition%part(ip)
552 scounts(ipart) = scounts(ipart) + 1
553 end do
554
555 ! Create displacements
556 sdispls(1) = 0
557 do ipart = 2, partition%npart
558 sdispls(ipart) = sdispls(ipart-1) + scounts(ipart-1)
559 end do
560
561 ! Allocate and fill the send buffer
562 np_local = sum(scounts)
563 scounts = 0
564 safe_allocate(sbuffer(1:np_local))
565 do ip = 1, np_local
566 ipart = partition%part(ip)
567 scounts(ipart) = scounts(ipart) + 1
568 sbuffer(sdispls(ipart) + scounts(ipart)) = ip + istart
569 end do
570
571 ! Tell each process how many points we will need from it
572 call partition%mpi_grp%alltoall(scounts, 1, mpi_integer, &
573 rcounts, 1, mpi_integer)
574
575 ! Create the displacement vector from the counts vector
576 np_local = sum(rcounts)
577 safe_allocate(rdispls(1:partition%npart))
578 assert(ubound(rbuffer, 1) >= np_local)
579
580 rdispls(1) = 0
581 do ipart = 2, partition%npart
582 rdispls(ipart) = rcounts(ipart-1) + rdispls(ipart-1)
583 end do
584
585 ! Collect the corresponding points to each process
586 call partition%mpi_grp%alltoallv(sbuffer, scounts, sdispls, mpi_integer8, &
587 rbuffer, rcounts, rdispls, mpi_integer8)
588
589 safe_deallocate_a(sdispls)
590 safe_deallocate_a(scounts)
591 safe_deallocate_a(sbuffer)
592 safe_deallocate_a(rcounts)
593 safe_deallocate_a(rdispls)
594
595 pop_sub(partition_get_local)
596 end subroutine partition_get_local
597end module partition_oct_m
599!! Local Variables:
600!! mode: f90
601!! coding: utf-8
602!! End:
subroutine, public io_binary_get_info(fname, np, file_size, ierr)
subroutine, public iwrite_header(fname, np_global, ierr)
Definition: io.F90:114
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:161
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:415
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:617
integer, parameter, public c_mpi_file_read
Definition: mpi_debug.F90:137
subroutine, public mpi_debug_in(comm, index)
Definition: mpi_debug.F90:241
subroutine, public mpi_debug_out(comm, index)
Definition: mpi_debug.F90:265
integer, parameter, public c_mpi_file_write
Definition: mpi_debug.F90:137
logical function mpi_grp_is_root(grp)
Is the current MPI process of grpcomm, root.
Definition: mpi.F90:430
type(mpi_grp_t), public mpi_world
Definition: mpi.F90:266
subroutine, public partition_init(partition, np_global, mpi_grp)
initialize the partition table
Definition: partition.F90:193
pure integer function partition_get_number(partition, global_point)
Returns the partition number for a given global index If the index is zero, return local partition.
Definition: partition.F90:608
pure integer function, public partition_get_npart(partition)
Returns the total number of partitions.
Definition: partition.F90:592
subroutine, public partition_set(partition, part)
Definition: partition.F90:243
subroutine partition_get_partition_number_4(partition, np, points, partno)
Given a list of global indices, return the partition number where those points are stored....
Definition: partition.F90:442
subroutine, public partition_get_np_local(partition, np_local_vec)
Given the partition, returns the corresponding number of local points that each partition has.
Definition: partition.F90:563
pure integer function, public partition_get_part(partition, local_point)
Returns the partition of the local point.
Definition: partition.F90:599
subroutine partition_get_partition_number_8(partition, np, points, partno)
Definition: partition.F90:537
subroutine, public partition_dump(partition, dir, filename, ierr)
write the partition data
Definition: partition.F90:260
subroutine, public partition_get_local(partition, rbuffer, np_local)
Calculates the local vector of all partitions in parallel. Local vector stores the global point indic...
Definition: partition.F90:623
subroutine, public partition_end(partition)
Definition: partition.F90:230
subroutine, public partition_load(partition, dir, filename, ierr)
read the partition data
Definition: partition.F90:305
subroutine, public partition_get_local_size(partition, istart, np_local)
Definition: partition.F90:381
subroutine, public partition_get_global(partition, part_global, root)
Returns the global partition. If root is present, the partition is gathered only in that node....
Definition: partition.F90:397
The partition is an array that contains the mapping between some global index and a process,...
Definition: partition.F90:165