Octopus
cube.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2011 M. Marques, A. Castro, A. Rubio,
2!! G. Bertsch, M. Oliveira, J. Alberdi-Rodriguez
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 cube_oct_m
23 use accel_oct_m
26 use debug_oct_m
27 use fft_oct_m
28 use global_oct_m
29 use io_oct_m
30 use, intrinsic :: iso_fortran_env
33 use mesh_oct_m
35 use mpi_oct_m
37 use parser_oct_m
38 use pfft_oct_m
40 use space_oct_m
41
42 implicit none
43 private
44 public :: &
45 cube_t, &
47 cube_init, &
54
55 type cube_t
56 ! Components are public by default
57 logical :: parallel_in_domains
58 type(mpi_grp_t) :: mpi_grp
59
60 integer :: rs_n_global(1:3)
61 integer :: fs_n_global(1:3)
62 integer :: rs_n(1:3)
63 integer :: fs_n(1:3)
64 integer :: rs_istart(1:3)
65 integer :: fs_istart(1:3)
66 integer :: center(1:3)
67
68 integer, allocatable :: fs_ifx(:)
69 integer, allocatable :: fs_ify(:)
70 integer, allocatable :: fs_ifz(:)
71
72 real(real64), allocatable :: Lrs(:,:)
73 real(real64), allocatable :: Lfs(:,:)
74
75 integer, allocatable :: np_local(:)
76 integer, allocatable :: xlocal(:)
77 integer, allocatable :: local(:,:)
78 integer, allocatable :: np_local_fs(:)
79 integer, allocatable :: xlocal_fs(:)
80 integer, allocatable :: local_fs(:,:)
81
82
83 type(fft_t), allocatable :: fft
84 logical, private :: has_cube_mapping = .false.
86
87 real(real64) :: spacing(3)
88 ! latt is declared as allocatable as a work-around for a bug in gfortran when invoking the finalizer of latt.
89 type(lattice_vectors_t), allocatable :: latt
90
91 type(mesh_cube_map_t) :: cube_map
92 logical :: cube_map_present = .false.
93 integer :: batch_capacity
94 contains
95 procedure :: length => cube_length
96 end type cube_t
97
103 type dimensions_t
104 integer :: start_xyz(1:3)
105 integer :: end_xyz(1:3)
106 end type dimensions_t
107
108contains
109
110 ! ---------------------------------------------------------
111 subroutine cube_init(cube, nn, namespace, space, spacing, coord_system, fft_type, fft_library, dont_optimize, nn_out, &
112 mpi_grp, need_partition, tp_enlarge, blocksize, batch_size, nthreads)
113 type(cube_t), intent(out) :: cube
114 integer, intent(in) :: nn(:)
115 type(namespace_t), intent(in) :: namespace
116 class(space_t), intent(in) :: space
117 real(real64), intent(in) :: spacing(:)
118 class(coordinate_system_t), intent(in) :: coord_system
119 integer, optional, intent(in) :: fft_type
120 integer, optional, intent(in) :: fft_library
121 logical, optional, intent(in) :: dont_optimize
122 integer, optional, intent(out) :: nn_out(3)
124 type(mpi_grp_t), optional, intent(in) :: mpi_grp
125 logical, optional, intent(in) :: need_partition
126 real(real64), optional, intent(in) :: tp_enlarge(3)
129 integer, optional, intent(in) :: blocksize
131 integer, optional, intent(in) :: batch_size
132 ! the batch size for the FFTW and cuFFT solvers
133 integer, optional, intent(in) :: nthreads
134
135 type(MPI_Comm) :: comm
136 integer :: tmp_n(3), fft_type_, optimize_parity(3), fft_library_, nn3d(3)
137 integer :: effdim_fft, my_n(3), idir, idir2
138 logical :: optimize(3)
139 type(mpi_grp_t) :: mpi_grp_
140 real(real64) :: tp_enlarge_(3), lattice_vectors(3, 3)
141 type(space_t) :: cube_space
142
143 push_sub(cube_init)
144
145 assert(all(nn > 0))
146 assert(space%dim <= 3)
147
148 nn3d(1:space%dim) = nn(1:space%dim)
149 nn3d(space%dim+1:3) = 1
151 cube%spacing(1:space%dim) = spacing(1:space%dim)
152 cube%spacing(space%dim+1:3) = -m_one
153 cube%batch_capacity = optional_default(batch_size, 1)
154
155 fft_type_ = optional_default(fft_type, fft_none)
156 tp_enlarge_(:) = (/m_one, m_one, m_one/)
157 if (present(tp_enlarge)) tp_enlarge_(:)=tp_enlarge(:)
159 effdim_fft = min(3, space%dim)
161 mpi_grp_ = mpi_world
162 if (present(mpi_grp)) mpi_grp_ = mpi_grp
164 if (fft_type_ /= fft_none) then
166 if (present(fft_library)) then
167 fft_library_ = fft_library
168 else
169 fft_library_ = fft_default_lib
170 end if
172#ifndef HAVE_PFFT
173 if (fft_library_ == fftlib_pfft) then
174 write(message(1),'(a)')'You have selected the PFFT for FFT, but it was not linked.'
175 call messages_fatal(1, namespace=namespace)
176 end if
177#endif
179 else
180 fft_library_ = fftlib_none
181 end if
183 ! Only FFTW and cuFFT can batch the FFT (howmany > 1); PFFT/NFFT/PNFFT run one transform at a
184 ! time, so a batched cube on those backends is not possible. Callers that requested a batch
185 ! (batch_size > 1) fall back to a per-function loop (see X(poisson_solve_batch)).
186 if (fft_library_ /= fftlib_fftw .and. fft_library_ /= fftlib_accel) then
187 cube%batch_capacity = 1
188 end if
189
190 ! Note: later we set parallel_in_domains if blocksize is given, too
191 cube%parallel_in_domains = (fft_library_ == fftlib_pfft .or. fft_library_ == fftlib_pnfft)
192 if (present(blocksize)) then
193 assert(present(need_partition).and.need_partition)
194 assert(fft_library_ == fftlib_none)
195 ! For all the different FFT libraries there are strange (?)
196 ! rules about how the decomposition is chosen. What we want
197 ! (for libvdwxc) is a cube parallelized according to the simple
198 ! but contrary rule "just do what I say". Hence the blocksize
199 ! parameter. (Later to be expanded to allow 2D distributions.)
200 cube%rs_n_global = nn3d
201 cube%fs_n_global = nn3d ! not to be used
202 cube%fs_n = cube%fs_n_global ! not to be used
203 cube%fs_istart = 1 ! not to be used
204
205 comm = mpi_grp_%comm
206 cube%parallel_in_domains = (mpi_grp_%size > 1) ! XXX whether comm size > 1
207 call cube_set_blocksize(cube%rs_n_global, blocksize, mpi_grp_%rank, cube%rs_n, cube%rs_istart)
208 else if (fft_library_ == fftlib_none) then
209 cube%rs_n_global = nn3d
210 cube%fs_n_global = nn3d
211 cube%rs_n = cube%rs_n_global
212 cube%fs_n = cube%fs_n_global
213 cube%rs_istart = 1
214 cube%fs_istart = 1
215 comm = mpi_comm_undefined
216 if (present(nn_out)) nn_out(1:3) = nn3d(1:3)
217 else
218 safe_allocate(cube%fft)
219 tmp_n = nn3d
220
221 optimize(1:3) = .false.
222 optimize_parity(1:3) = 0
223 optimize(space%periodic_dim + 1:effdim_fft) = .true.
224 optimize_parity(space%periodic_dim + 1:effdim_fft) = 1
225
226 if (present(dont_optimize)) then
227 if (dont_optimize) optimize = .false.
228 end if
229
230 if (present(tp_enlarge)) call cube_tp_fft_defaults(cube, fft_library_)
231
232 call fft_init(cube%fft, tmp_n, space%dim, fft_type_, fft_library_, optimize, optimize_parity, &
233 comm=comm, mpi_grp = mpi_grp_, use_aligned=.true., howmany=cube%batch_capacity, nthreads=nthreads)
234 if (present(nn_out)) nn_out(1:3) = tmp_n(1:3)
235
236 call fft_get_dims(cube%fft, cube%rs_n_global, cube%fs_n_global, cube%rs_n, cube%fs_n, &
237 cube%rs_istart, cube%fs_istart)
238
239 if (present(tp_enlarge)) then
240 call cube_init_coords(cube, tp_enlarge_, cube%spacing, fft_library_)
241 end if
242
243 if (fft_library_ == fftlib_nfft .or. fft_library_ == fftlib_pnfft) then
244 call fft_init_stage1(cube%fft, namespace, cube%Lrs, cube%rs_n_global)
245 !set local dimensions after stage1 - needed for PNFFT
246 call fft_get_dims(cube%fft, cube%rs_n_global, cube%fs_n_global, cube%rs_n, cube%fs_n, &
247 cube%rs_istart, cube%fs_istart)
248 end if
249
250 end if
251
252 if (.not. allocated(cube%Lrs)) then
253 call cube_init_coords(cube, tp_enlarge_, cube%spacing, fft_library_)
254 end if
255
256 cube%center(1:3) = cube%rs_n_global(1:3)/2 + 1
257
259
260 call mpi_grp_init(cube%mpi_grp, comm)
261
262 ! Initialize mapping only if needed
263 if (present(need_partition) .and. cube%parallel_in_domains) then
264 cube%has_cube_mapping = need_partition
265 else
266 cube%has_cube_mapping = .false.
267 end if
268 if (cube%has_cube_mapping) then
269 call cube_do_mapping(cube, fs = fft_library_ == fftlib_pnfft)
270 end if
271
272 if (cube%parallel_in_domains) call cube_partition_messages_debug(cube, namespace)
273
274 select type (coord_system)
275 class is (affine_coordinates_t)
276 ! We are constructing a lattice vector for the cube
277 ! This differs from the actual lattice vectors if it is not an integer multiple of the spacing
278 ! mesh%idx%ll is "general" in aperiodic directions,
279 ! but "periodic" in periodic directions.
280 my_n(1:space%periodic_dim) = cube%rs_n_global(1:space%periodic_dim) + 1
281 my_n(space%periodic_dim + 1:space%dim) = cube%rs_n_global(space%periodic_dim + 1:space%dim)
282
283 lattice_vectors = m_zero
284 do idir = 1, space%dim
285 do idir2 = 1, space%dim
286 lattice_vectors(idir2, idir) = cube%spacing(idir) * (my_n(idir) - 1) * coord_system%basis%vectors(idir2, idir)
287 end do
288 end do
289 do idir = space%dim + 1, 3
290 lattice_vectors(idir, idir) = m_one
291 end do
292
293 cube_space%dim = 3
294 cube_space%periodic_dim = space%periodic_dim
295 safe_allocate(cube%latt)
296 cube%latt = lattice_vectors_t(namespace, cube_space, lattice_vectors)
297 class default
298 message(1) = "The cube only support affine coordinate systems."
299 call messages_fatal(1, namespace=namespace)
300 end select
301
302 pop_sub(cube_init)
303 end subroutine cube_init
304
305 ! ---------------------------------------------------------
306 subroutine cube_end(cube)
307 type(cube_t), intent(inout) :: cube
308
309 push_sub(cube_end)
310
311 if (allocated(cube%fft)) then
312 call fft_end(cube%fft)
313 safe_deallocate_a(cube%fft)
314 end if
315
316 if (cube%has_cube_mapping) then
317 safe_deallocate_a(cube%np_local)
318 safe_deallocate_a(cube%xlocal)
319 safe_deallocate_a(cube%local)
320
321 safe_deallocate_a(cube%np_local_fs)
322 safe_deallocate_a(cube%xlocal_fs)
323 safe_deallocate_a(cube%local_fs)
324 end if
325
326 if (cube%cube_map_present) then
327 call mesh_cube_map_end(cube%cube_map)
328 end if
329
330 safe_deallocate_a(cube%Lrs)
331 safe_deallocate_a(cube%Lfs)
332
333 safe_deallocate_a(cube%latt)
334 safe_deallocate_a(cube%fs_ifx)
335 safe_deallocate_a(cube%fs_ify)
336 safe_deallocate_a(cube%fs_ifz)
337
338 pop_sub(cube_end)
339 end subroutine cube_end
340
342 pure function cube_length(cube) result(l)
343 class(cube_t), intent(in) :: cube
344 real(real64) :: l(3)
345 l = cube%rs_n_global(1:3) * cube%spacing(1:3)
346 end function cube_length
347
361 class(cube_t), intent(inout) :: cube
362
363 integer :: lx, ly, lz
364
366
367 safe_allocate(cube%fs_ifx(1:max(1, cube%fs_n(1))))
368 safe_allocate(cube%fs_ify(1:max(1, cube%fs_n(2))))
369 safe_allocate(cube%fs_ifz(1:max(1, cube%fs_n(3))))
370
371 do lx = 1, cube%fs_n(1)
372 cube%fs_ifx(lx) = pad_feq(cube%fs_istart(1) + lx - 1, cube%rs_n_global(1), .true.)
373 end do
374 do ly = 1, cube%fs_n(2)
375 cube%fs_ify(ly) = pad_feq(cube%fs_istart(2) + ly - 1, cube%rs_n_global(2), .true.)
376 end do
377 do lz = 1, cube%fs_n(3)
378 cube%fs_ifz(lz) = pad_feq(cube%fs_istart(3) + lz - 1, cube%rs_n_global(3), .true.)
379 end do
380
382
384
385
386 ! ---------------------------------------------------------
387 subroutine cube_tp_fft_defaults(cube, fft_library)
388 type(cube_t), intent(inout) :: cube
389 integer, intent(in) :: fft_library
390
391 push_sub(cube_tp_fft_defaults)
392 select case (fft_library)
393 case (fftlib_pnfft)
394 cube%fft%pnfft%set_defaults = .true.
395 cube%fft%pnfft%m = 2
396 cube%fft%pnfft%sigma = 1.1_real64
397
398 case default
399 !do nothing
400 end select
402 pop_sub(cube_tp_fft_defaults)
403 end subroutine cube_tp_fft_defaults
404
405
406 ! ---------------------------------------------------------
407 subroutine cube_init_coords(cube, tp_enlarge, spacing, fft_library)
408 type(cube_t), intent(inout) :: cube
409 real(real64), intent(in) :: tp_enlarge(3)
410 real(real64), intent(in) :: spacing(3)
411 integer, intent(in) :: fft_library
412
413 real(real64) :: temp
414 integer :: ii, nn(3), maxn, idim
415
416 push_sub(cube_init_coords)
417
418
419 nn(1:3) = cube%fs_n_global(1:3)
420
421 maxn = maxval(nn)
422 safe_allocate(cube%Lrs(1:maxn, 1:3))
423 cube%Lrs(:,:) = m_zero
424
425 !! Real space coordinates
426 do idim = 1,3
427 if (tp_enlarge(idim) > m_one) then
428 do ii = 2, nn(idim) - 1
429 cube%Lrs(ii, idim) = (ii - int(nn(idim)/2) -1) * spacing(idim)
430 end do
431 cube%Lrs(1, idim) = (-int(nn(idim)/2)) * spacing(idim) * tp_enlarge(idim)
432 cube%Lrs(nn(idim), idim) = (int(nn(idim)/2)) * spacing(idim) * tp_enlarge(idim)
433 else
434 do ii = 1, nn(idim)
435 cube%Lrs(ii, idim) = (ii - int(nn(idim)/2) -1) * spacing(idim)
436 end do
437 end if
438 end do
439
440
441 !! Fourier space coordinates
442 if (fft_library /= fftlib_none) then
443
444 safe_allocate(cube%Lfs(1:maxn, 1:3))
445 cube%Lfs(:,:) = m_zero
446
447 do idim = 1,3
448 temp = m_two * m_pi / (nn(idim) * spacing(idim))
449!temp = M_PI / (nn * spacing(1))
450 do ii = 1, nn(idim)
451 if (fft_library == fftlib_nfft .or. fft_library == fftlib_pnfft) then
452 !The Fourier space is shrunk by the tp_enlarge factor
453 !cube%Lfs(ii, 1:3) = (ii - nn/2 - 1)*temp/tp_enlarge
454!HH NOTE:
455!not sure this is the right general factor
456 cube%Lfs(ii, idim) = (ii - nn(idim)/2 - 1)*temp/tp_enlarge(idim)
457 else
458 cube%Lfs(ii, idim) = pad_feq(ii,nn(idim), .true.) * temp
459 end if
460 end do
461 end do
462 end if
463
464 pop_sub(cube_init_coords)
465 end subroutine cube_init_coords
466
467
468 ! ---------------------------------------------------------
471 logical function cube_global2local(cube, ixyz, lxyz) result(is_here)
472 type(cube_t), intent(in) :: cube
473 integer, intent(in) :: ixyz(3)
474 integer, intent(out) :: lxyz(3)
475
476 lxyz(1) = ixyz(1) - cube%rs_istart(1) + 1
477 lxyz(2) = ixyz(2) - cube%rs_istart(2) + 1
478 lxyz(3) = ixyz(3) - cube%rs_istart(3) + 1
479 is_here = lxyz(1) >= 1 .and. lxyz(1) <= cube%rs_n(1) .and. &
480 lxyz(2) >= 1 .and. lxyz(2) <= cube%rs_n(2) .and. &
481 lxyz(3) >= 1 .and. lxyz(3) <= cube%rs_n(3)
483 end function cube_global2local
484
485
486 ! ---------------------------------------------------------
491 integer function cube_getfftlibrary(cube) result(fft_library)
492 type(cube_t), intent(in) :: cube
493
494 if (allocated(cube%fft)) then
495 fft_library = cube%fft%library
496 else
497 fft_library = fftlib_none
498 end if
499 end function cube_getfftlibrary
500
501 ! ---------------------------------------------------------
503 subroutine cube_do_mapping(cube, fs)
504 type(cube_t), intent(inout) :: cube
505 logical, intent(in) :: fs
506
507 integer :: tmp_local(6), position, process, ix, iy, iz, index
508 integer, allocatable :: local_sizes(:)
509 integer(int64) :: number_points
510
511 push_sub(cube_do_mapping)
512
513 !!BEGIN:gather the local information into a unique vector.
514 !!do a gather in 3d of all the box, into a loop
515 tmp_local(1) = cube%rs_istart(1)
516 tmp_local(2) = cube%rs_istart(2)
517 tmp_local(3) = cube%rs_istart(3)
518 tmp_local(4) = cube%rs_n(1)
519 tmp_local(5) = cube%rs_n(2)
520 tmp_local(6) = cube%rs_n(3)
521
522 if (cube%parallel_in_domains) then
523 safe_allocate(local_sizes(1:6*cube%mpi_grp%size))
524 call profiling_in("CUBE_GAT")
525 call cube%mpi_grp%allgather(tmp_local, 6, mpi_integer, local_sizes, 6, mpi_integer)
526 call profiling_out("CUBE_GAT")
527 else
528 safe_allocate(local_sizes(1:6))
529 local_sizes = tmp_local
530 end if
531
532 call profiling_in("CUBE_MAP")
533
534 safe_allocate(cube%xlocal(1:cube%mpi_grp%size))
535 safe_allocate(cube%np_local(1:cube%mpi_grp%size))
536 ! make sure we do not run into integer overflow here
537 number_points = cube%rs_n_global(1) * cube%rs_n_global(2)
538 number_points = number_points * cube%rs_n_global(3)
539 if (number_points >= huge(0)) then
540 message(1) = "Error: too many points for the normal cube. Please try to use a distributed FFT."
541 call messages_fatal(1)
542 end if
543 safe_allocate(cube%local(1:cube%rs_n_global(1)*cube%rs_n_global(2)*cube%rs_n_global(3), 1:3))
544
545 index = 1
546 do process = 1, cube%mpi_grp%size
547 position = ((process-1)*6)+1
548 if (position == 1) then
549 cube%xlocal(1) = 1
550 cube%np_local(1) = local_sizes(4)*local_sizes(5)*local_sizes(6)
551 else
552 ! calculate the begin index and size of each process
553 cube%xlocal(process) = cube%xlocal(process-1) + cube%np_local(process-1)
554 cube%np_local(process) = local_sizes(position+3)*local_sizes(position+4)*local_sizes(position+5)
555 end if
556
557 ! save the mapping between the global x,y,z and the global index
558 ! and determine which partition the point belongs to
559 do iz = local_sizes(position+2), local_sizes(position+2)+local_sizes(position+5)-1
560 do iy = local_sizes(position+1), local_sizes(position+1)+local_sizes(position+4)-1
561 do ix = local_sizes(position), local_sizes(position)+local_sizes(position+3)-1
562 cube%local(index, 1) = ix
563 cube%local(index, 2) = iy
564 cube%local(index, 3) = iz
565 index = index + 1
566 end do
567 end do
568 end do
569 end do
570
571 call profiling_out("CUBE_MAP")
572
573 if (optional_default(fs,.false.)) then
574
575 tmp_local(1) = cube%fs_istart(1)
576 tmp_local(2) = cube%fs_istart(2)
577 tmp_local(3) = cube%fs_istart(3)
578 tmp_local(4) = cube%fs_n(1)
579 tmp_local(5) = cube%fs_n(2)
580 tmp_local(6) = cube%fs_n(3)
581
582 local_sizes = 0
583 if (cube%parallel_in_domains) then
584 call profiling_in("CUBE_GAT_FS")
585 call cube%mpi_grp%allgather(tmp_local, 6, mpi_integer, local_sizes, 6, mpi_integer)
586 call profiling_out("CUBE_GAT_FS")
587 else
588 local_sizes = tmp_local
589 end if
590
591 call profiling_in("CUBE_MAP_FS")
592
593 safe_allocate(cube%xlocal_fs(1:cube%mpi_grp%size))
594 safe_allocate(cube%np_local_fs(1:cube%mpi_grp%size))
595 ! make sure we do not run into integer overflow here
596 number_points = cube%fs_n_global(1) * cube%fs_n_global(2)
597 number_points = number_points * cube%fs_n_global(3)
598 if (number_points >= huge(0)) then
599 message(1) = "Error: too many points for the normal cube. Please try to use a distributed FFT."
600 call messages_fatal(1)
601 end if
602 safe_allocate(cube%local_fs(1:cube%fs_n_global(1)*cube%fs_n_global(2)*cube%fs_n_global(3), 1:3))
603
604 index = 1
605 do process = 1, cube%mpi_grp%size
606 position = ((process-1)*6)+1
607 if (position == 1) then
608 cube%xlocal_fs(1) = 1
609 cube%np_local_fs(1) = local_sizes(4)*local_sizes(5)*local_sizes(6)
610 else
611 ! calculate the begin index and size of each process
612 cube%xlocal_fs(process) = cube%xlocal_fs(process-1) + cube%np_local_fs(process-1)
613 cube%np_local_fs(process) = local_sizes(position+3)*local_sizes(position+4)*local_sizes(position+5)
614 end if
615
616 ! save the mapping between the global x,y,z and the global index
617 ! and determine which partition the point belongs to
618 do iz = local_sizes(position+2), local_sizes(position+2)+local_sizes(position+5)-1
619 do iy = local_sizes(position+1), local_sizes(position+1)+local_sizes(position+4)-1
620 do ix = local_sizes(position), local_sizes(position)+local_sizes(position+3)-1
621 cube%local_fs(index, 1) = ix
622 cube%local_fs(index, 2) = iy
623 cube%local_fs(index, 3) = iz
624 index = index + 1
625 end do
626 end do
627 end do
628 end do
629
630 call profiling_out("CUBE_MAP_FS")
631
632 end if
633
634
635
636 safe_deallocate_a(local_sizes)
637
638 pop_sub(cube_do_mapping)
639 end subroutine cube_do_mapping
640
641 !!> Given a x, y, z point of the cube, it returns the corresponding process
642 !!
643 !! last_found is used to speed-up the search
644 integer pure function cube_point_to_process(mpi_grp, xyz, part) result(process)
645 type(mpi_grp_t), intent(in) :: mpi_grp
646 integer, intent(in) :: xyz(1:3)
647 type(dimensions_t), intent(in) :: part(:)
648
649 integer :: proc
650 logical :: found
651
652 ! No PUSH/POP because it is a PURE function
653
654 found = .false.
655 do proc = 1, mpi_grp%size
656 !Compare XYZ index
657 if (all(xyz >= part(proc)%start_xyz) .and. all(xyz <= part(proc)%end_xyz)) then
658 process = proc
659 found = .true.
660 exit
661 end if
662 end do
663
664 ! An error message should be raised, if this point is reached
665 if (.not. found) then
666 process = -1
667 end if
668
669 end function cube_point_to_process
670
671 ! Sets a 1D decomposition with fixed-size blocks over the last (least-contiguous) axis.
672 ! Each core will have <blocksize> slices except the last one which will typically have
673 ! less. (In some cases, there can be multiple trailing cores without any slices.)
674 subroutine cube_set_blocksize(rs_n_global, blocksize, rank, rs_n, rs_istart)
675 integer, intent(in) :: rs_n_global(1:3)
676 integer, intent(in) :: blocksize
677 integer, intent(in) :: rank
678 integer, intent(out) :: rs_n(1:3)
679 integer, intent(out) :: rs_istart(1:3)
680
681 integer :: imin, imax
682
683 rs_n = rs_n_global
684 rs_istart = 1
685
686 imin = min(blocksize * rank, rs_n_global(3))
687 imax = min(imin + blocksize, rs_n_global(3))
688 rs_istart(3) = 1 + imin
689 rs_n(3) = imax - imin
690 end subroutine cube_set_blocksize
691
692 ! ---------------------------------------------------------
693 subroutine cube_partition(cube, part)
694 type(cube_t), intent(in) :: cube
695 type(dimensions_t), intent(out) :: part(:)
696
697 integer :: tmp_local(6), position, process
698 integer, allocatable :: local_sizes(:)
699
700 push_sub(cube_partition)
701
702 !!gather the local information into a unique vector.
703 tmp_local(1) = cube%rs_istart(1)
704 tmp_local(2) = cube%rs_istart(2)
705 tmp_local(3) = cube%rs_istart(3)
706 tmp_local(4) = cube%rs_n(1)
707 tmp_local(5) = cube%rs_n(2)
708 tmp_local(6) = cube%rs_n(3)
709
710 if (cube%parallel_in_domains) then
711 safe_allocate(local_sizes(1:6*cube%mpi_grp%size))
712 call cube%mpi_grp%allgather(tmp_local, 6, mpi_integer, local_sizes, 6, mpi_integer)
713 else
714 safe_allocate(local_sizes(1:6))
715 local_sizes(:) = tmp_local(:)
716 end if
717
718 do process = 1, cube%mpi_grp%size
719 position = ((process-1)*6)+1
720
721 part(process)%start_xyz(1) = local_sizes(position)
722 part(process)%start_xyz(2) = local_sizes(position+1)
723 part(process)%start_xyz(3) = local_sizes(position+2)
724 part(process)%end_xyz(1) = local_sizes(position)+local_sizes(position+3)-1
725 part(process)%end_xyz(2) = local_sizes(position+1)+local_sizes(position+4)-1
726 part(process)%end_xyz(3) = local_sizes(position+2)+local_sizes(position+5)-1
727
728 end do
729
730 pop_sub(cube_partition)
731 end subroutine cube_partition
732
733 ! ---------------------------------------------------------
734 subroutine cube_partition_messages_debug(cube, namespace)
735 type(cube_t), intent(in) :: cube
736 type(namespace_t), intent(in) :: namespace
737
738 integer :: nn, ii, jj, kk ! Counters.
739 integer :: ixyz(3) ! Current value of xyz
740 integer :: npart
741 integer :: iunit ! For debug output to files.
742 character(len=3) :: filenum
743 type(dimensions_t), allocatable :: part(:)
744
746
747 if (debug%info) then
748 safe_allocate(part(1:cube%mpi_grp%size))
749 call cube_partition(cube, part)
750
751 if (mpi_world%is_root()) then
752 call io_mkdir('debug/cube_partition', namespace)
753 npart = cube%mpi_grp%size
754
755 ! Debug output. Write points of each partition in a different file.
756 do nn = 1, npart
757
758 write(filenum, '(i3.3)') nn
759
760 iunit = io_open('debug/cube_partition/cube_partition.'//filenum, &
761 namespace, action='write')
762 do kk = 1, cube%rs_n_global(3)
763 do jj = 1, cube%rs_n_global(2)
764 do ii = 1, cube%rs_n_global(1)
765 ixyz(1) = ii
766 ixyz(2) = jj
767 ixyz(3) = kk
768 if (cube_point_to_process(cube%mpi_grp, ixyz, part) == nn) then
769 write(iunit, '(3i8)') ii, jj, kk
770 end if
771 end do
772 end do
773 end do
774 call io_close(iunit)
775 end do
776
777
778 end if
779
780 safe_deallocate_a(part)
781 end if
782
783 call cube%mpi_grp%barrier()
784
786 end subroutine cube_partition_messages_debug
787
788 ! ---------------------------------------------------------
789 subroutine cube_init_cube_map(cube, mesh)
790 type(cube_t), intent(inout) :: cube
791 class(mesh_t), intent(in) :: mesh
792
793 push_sub(cube_init_cube_map)
794
795 call mesh_cube_map_init(cube%cube_map, mesh, mesh%np)
796 cube%cube_map_present = .true.
797
798 pop_sub(cube_init_cube_map)
799 end subroutine cube_init_cube_map
800end module cube_oct_m
801
802
803!! Local Variables:
804!! mode: f90
805!! coding: utf-8
806!! End:
subroutine cube_set_blocksize(rs_n_global, blocksize, rank, rs_n, rs_istart)
Definition: cube.F90:770
subroutine cube_init_fourier_mode_numbers_mapping(cube)
Initialises the mapping between local cube index in Fourier space and global FFT integer frequencies....
Definition: cube.F90:456
subroutine cube_do_mapping(cube, fs)
do the mapping between global and local points of the cube
Definition: cube.F90:599
subroutine, public cube_end(cube)
Definition: cube.F90:402
pure real(real64) function, dimension(3) cube_length(cube)
Total length of the cube in each dimension.
Definition: cube.F90:438
logical function, public cube_global2local(cube, ixyz, lxyz)
True if global coordinates belong to this process. On output lxyz contains the local coordinates.
Definition: cube.F90:567
subroutine cube_tp_fft_defaults(cube, fft_library)
Definition: cube.F90:483
integer pure function, public cube_point_to_process(mpi_grp, xyz, part)
Definition: cube.F90:740
integer function, public cube_getfftlibrary(cube)
Returns the FFT library of the cube. Possible values are FFTLIB_NONE, FFTLIB_FFTW,...
Definition: cube.F90:587
subroutine, public cube_init(cube, nn, namespace, space, spacing, coord_system, fft_type, fft_library, dont_optimize, nn_out, mpi_grp, need_partition, tp_enlarge, blocksize, batch_size, nthreads)
Definition: cube.F90:208
subroutine cube_init_coords(cube, tp_enlarge, spacing, fft_library)
Definition: cube.F90:503
subroutine, public cube_partition(cube, part)
Definition: cube.F90:789
subroutine, public cube_init_cube_map(cube, mesh)
Definition: cube.F90:885
subroutine cube_partition_messages_debug(cube, namespace)
Definition: cube.F90:830
Fast Fourier Transform module. This module provides a single interface that works with different FFT ...
Definition: fft.F90:120
integer, parameter, public fft_none
global constants
Definition: fft.F90:174
subroutine, public fft_end(this)
Definition: fft.F90:800
integer, public fft_default_lib
Definition: fft.F90:257
pure integer function, public pad_feq(ii, nn, mode)
convert between array index and G-vector
Definition: fft.F90:914
integer, parameter, public fftlib_accel
Definition: fft.F90:179
subroutine, public fft_init(this, nn, dim, type, library, optimize, optimize_parity, comm, mpi_grp, use_aligned, howmany, nthreads)
Definition: fft.F90:412
subroutine, public fft_get_dims(fft, rs_n_global, fs_n_global, rs_n, fs_n, rs_istart, fs_istart)
Definition: fft.F90:888
integer, parameter, public fftlib_nfft
Definition: fft.F90:179
integer, parameter, public fftlib_none
Definition: fft.F90:179
integer, parameter, public fftlib_pnfft
Definition: fft.F90:179
integer, parameter, public fftlib_pfft
Definition: fft.F90:179
integer, parameter, public fftlib_fftw
Definition: fft.F90:179
subroutine, public fft_init_stage1(this, namespace, XX, nn)
Some fft-libraries (only NFFT for the moment) need an additional precomputation stage that depends on...
Definition: fft.F90:758
real(real64), parameter, public m_two
Definition: global.F90:202
real(real64), parameter, public m_zero
Definition: global.F90:200
real(real64), parameter, public m_pi
some mathematical constants
Definition: global.F90:198
real(real64), parameter, public m_one
Definition: global.F90:201
Definition: io.F90:116
subroutine, public mesh_cube_map_end(this)
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:120
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:162
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:410
type(mpi_comm), parameter, public mpi_comm_undefined
used to indicate a communicator has not been initialized
Definition: mpi.F90:138
type(mpi_grp_t), public mpi_world
Definition: mpi.F90:276
subroutine mpi_grp_init(grp, comm)
Initialize MPI group instance.
Definition: mpi.F90:345
The low level module to work with the PFFT library. http:
Definition: pfft.F90:128
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:631
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:554
It is intended to be used within a vector.
Definition: cube.F90:198
This is defined even when running serial.
Definition: mpi.F90:144
int true(void)