Octopus
batch.F90
Go to the documentation of this file.
1!! Copyright (C) 2008 X. Andrade, 2020 S. Ohlmann
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17!!
18
19#include "global.h"
20
39!
40module batch_oct_m
41 use accel_oct_m
43 use blas_oct_m
44 use debug_oct_m
45 use global_oct_m
47 use iso_c_binding
48 use math_oct_m
50 use mpi_oct_m
52 use types_oct_m
53
54 implicit none
55
56 private
57 public :: &
58 batch_t, &
59 batch_p_t, &
60 batch_init, &
67
70 type batch_t
71 private
72 integer, public :: nst
73 integer, public :: dim
74 integer :: np
75 integer :: ndims
76 integer, allocatable :: ist_idim_index(:, :)
77
81 integer, allocatable, public :: ist(:)
87
88 logical :: is_allocated
89 logical :: own_memory
90 ! We also need a linear array with the states in order to calculate derivatives, etc.
91 integer, public :: nst_linear
92
93 integer :: status_of
97 integer :: status_host
106 type(type_t) :: type_of
107 integer :: device_buffer_count
108 integer :: host_buffer_count
109 logical :: special_memory
110 logical :: needs_finish_unpack
111
113 ! unpacked variables; linear variables are pointers with different shapes
114 real(real64), pointer, contiguous, public :: dff(:, :, :)
115 ! !! indices are (1:np, 1:dim, 1:nst)
116 complex(real64), pointer, contiguous, public :: zff(:, :, :)
117 ! !! indices are (1:np, 1:dim, 1:nst)
118 real(real64), pointer, contiguous, public :: dff_linear(:, :)
119 ! !! indices are (1:np, 1:nst_linear)
120 complex(real64), pointer, contiguous, public :: zff_linear(:, :)
121 ! !! indices are (1:np, 1:nst_linear)
122
123 ! packed variables; only rank-2 arrays due to padding to powers of 2
124 real(real64), pointer, contiguous, public :: dff_pack(:, :)
125 ! !! indices are (1:nst_linear, 1:np)
126 complex(real64), pointer, contiguous, public :: zff_pack(:, :)
127 ! !! indices are (1:nst_linear, 1:np)
128
129 ! single-precision storage (used by the single-precision exchange path)
130 real(real32), pointer, contiguous, public :: sff(:, :, :)
131 complex(real32), pointer, contiguous, public :: cff(:, :, :)
132 real(real32), pointer, contiguous, public :: sff_linear(:, :)
133 complex(real32), pointer, contiguous, public :: cff_linear(:, :)
134 real(real32), pointer, contiguous, public :: sff_pack(:, :)
135 complex(real32), pointer, contiguous, public :: cff_pack(:, :)
136
137 integer(int64), public :: pack_size(1:2)
139 integer(int64), public :: pack_size_real(1:2)
143 type(accel_mem_t), public :: ff_device
145 contains
146 procedure :: check_compatibility_with => batch_check_compatibility_with
147 procedure :: clone_to => batch_clone_to
148 procedure :: clone_to_array => batch_clone_to_array
149 procedure :: copy_to => batch_copy_to
150 procedure :: copy_data_to => batch_copy_data_to
151 procedure :: do_pack_generic => batch_do_pack_generic
152 procedure :: do_pack_target => batch_do_pack_target
153 generic :: do_pack => do_pack_generic, do_pack_target
154 procedure :: do_unpack => batch_do_unpack
155 procedure :: finish_unpack => batch_finish_unpack
156 procedure :: end => batch_end
157 procedure :: inv_index => batch_inv_index
158 procedure :: is_packed => batch_is_packed
159 procedure :: ist_idim_to_linear => batch_ist_idim_to_linear
160 procedure :: linear_to_idim => batch_linear_to_idim
161 procedure :: linear_to_ist => batch_linear_to_ist
162 procedure :: pack_total_size => batch_pack_total_size
163 procedure :: remote_access_start => batch_remote_access_start
164 procedure :: remote_access_stop => batch_remote_access_stop
165 procedure :: status => batch_status
166 procedure :: type => batch_type
167 procedure :: type_as_int => batch_type_as_integer
168 procedure, private :: dallocate_unpacked_host => dbatch_allocate_unpacked_host
170 procedure, private :: zallocate_unpacked_host => zbatch_allocate_unpacked_host
171
172 procedure, private :: sallocate_unpacked_host => sbatch_allocate_unpacked_host
173
174 procedure, private :: callocate_unpacked_host => cbatch_allocate_unpacked_host
175
176 procedure, private :: allocate_unpacked_host => batch_allocate_unpacked_host
177
178 procedure, private :: dallocate_packed_host => dbatch_allocate_packed_host
180 procedure, private :: zallocate_packed_host => zbatch_allocate_packed_host
182 procedure, private :: sallocate_packed_host => sbatch_allocate_packed_host
183
184 procedure, private :: callocate_packed_host => cbatch_allocate_packed_host
185
186 procedure, private :: allocate_packed_host => batch_allocate_packed_host
187
188 procedure, private :: allocate_packed_device => batch_allocate_packed_device
189
190 procedure, private :: deallocate_unpacked_host => batch_deallocate_unpacked_host
191
192 procedure, private :: deallocate_packed_host => batch_deallocate_packed_host
193
194 procedure, private :: deallocate_packed_device => batch_deallocate_packed_device
195
196 end type batch_t
197
199 class(batch_t), allocatable :: p
200 end type batch_p_t
201
202 !--------------------------------------------------------------
206 interface batch_init
208 module procedure zbatch_init_with_memory_3
210 module procedure cbatch_init_with_memory_3
212 module procedure zbatch_init_with_memory_2
213 module procedure sbatch_init_with_memory_2
214 module procedure cbatch_init_with_memory_2
216 module procedure zbatch_init_with_memory_1
217 module procedure sbatch_init_with_memory_1
219 end interface batch_init
221 integer, public, parameter :: &
222 batch_not_packed = 0, & !< functions are stored in CPU memory, unpacked order
226 integer, parameter :: gpu_pack_max_buffer_size = 4
229contains
231 !--------------------------------------------------------------
239 subroutine batch_end(this, copy)
240 class(batch_t), intent(inout) :: this
241 logical, optional, intent(in) :: copy
243
244 push_sub(batch_end)
245
246 if (this%own_memory .and. this%is_packed()) then
247 !deallocate directly to avoid unnecessary copies
248 if (this%status() == batch_device_packed) then
249 call this%deallocate_packed_device()
250 end if
251 if (this%status() == batch_packed .or. this%status_host == batch_packed) then
252 call this%deallocate_packed_host()
253 end if
254 this%status_of = batch_not_packed
255 this%status_host = batch_not_packed
256 this%host_buffer_count = 0
257 this%device_buffer_count = 0
258 end if
259 if (this%status() == batch_device_packed) call this%do_unpack(copy, force = .true.)
260 if (this%status() == batch_packed) call this%do_unpack(copy, force = .true.)
261
262 if (this%is_allocated) then
263 call this%deallocate_unpacked_host()
264 end if
265
266 safe_deallocate_a(this%ist_idim_index)
267 safe_deallocate_a(this%ist)
268
269 pop_sub(batch_end)
270 end subroutine batch_end
272 !--------------------------------------------------------------
276 !
277 subroutine batch_deallocate_unpacked_host(this)
278 class(batch_t), intent(inout) :: this
279
281
282 this%is_allocated = .false.
283
284 if (this%special_memory) then
285 if (associated(this%dff)) then
286 call deallocate_hardware_aware(c_loc(this%dff(1,1,1)), int(this%np, int64)*this%dim*this%nst*8)
287 end if
288 if (associated(this%zff)) then
289 call deallocate_hardware_aware(c_loc(this%zff(1,1,1)), int(this%np, int64)*this%dim*this%nst*16)
290 end if
291 if (associated(this%sff)) then
292 call deallocate_hardware_aware(c_loc(this%sff(1,1,1)), int(this%np, int64)*this%dim*this%nst*4)
293 end if
294 if (associated(this%cff)) then
295 call deallocate_hardware_aware(c_loc(this%cff(1,1,1)), int(this%np, int64)*this%dim*this%nst*8)
296 end if
297 else
298 safe_deallocate_p(this%dff)
299 safe_deallocate_p(this%zff)
300 safe_deallocate_p(this%sff)
301 safe_deallocate_p(this%cff)
302 end if
303 nullify(this%dff)
304 nullify(this%dff_linear)
305 nullify(this%zff)
306 nullify(this%zff_linear)
307 nullify(this%sff)
308 nullify(this%sff_linear)
309 nullify(this%cff)
310 nullify(this%cff_linear)
313 end subroutine batch_deallocate_unpacked_host
314
315 !--------------------------------------------------------------
319 !
320 subroutine batch_deallocate_packed_host(this)
321 class(batch_t), intent(inout) :: this
322
324
325 if (this%special_memory) then
326 if (associated(this%dff_pack)) then
327 call deallocate_hardware_aware(c_loc(this%dff_pack(1,1)), int(this%pack_size(1), int64)*this%pack_size(2)*8)
328 end if
329 if (associated(this%zff_pack)) then
330 call deallocate_hardware_aware(c_loc(this%zff_pack(1,1)), int(this%pack_size(1), int64)*this%pack_size(2)*16)
331 end if
332 if (associated(this%sff_pack)) then
333 call deallocate_hardware_aware(c_loc(this%sff_pack(1,1)), int(this%pack_size(1), int64)*this%pack_size(2)*4)
334 end if
335 if (associated(this%cff_pack)) then
336 call deallocate_hardware_aware(c_loc(this%cff_pack(1,1)), int(this%pack_size(1), int64)*this%pack_size(2)*8)
337 end if
338 else
339 safe_deallocate_p(this%dff_pack)
340 safe_deallocate_p(this%zff_pack)
341 safe_deallocate_p(this%sff_pack)
342 safe_deallocate_p(this%cff_pack)
343 end if
344 nullify(this%dff_pack)
345 nullify(this%zff_pack)
346 nullify(this%sff_pack)
347 nullify(this%cff_pack)
348
350 end subroutine batch_deallocate_packed_host
351
352 !--------------------------------------------------------------
354 !
355 subroutine batch_deallocate_packed_device(this)
356 class(batch_t), intent(inout) :: this
357
359
360 call accel_free_buffer(this%ff_device)
361
363 end subroutine batch_deallocate_packed_device
364
365 !--------------------------------------------------------------
369 !
370 subroutine batch_allocate_unpacked_host(this)
371 class(batch_t), intent(inout) :: this
372
374
375 if (this%type() == type_float) then
376 call this%dallocate_unpacked_host()
377 else if (this%type() == type_cmplx) then
378 call this%zallocate_unpacked_host()
379 else if (this%type() == type_float_single) then
380 call this%sallocate_unpacked_host()
381 else if (this%type() == type_cmplx_single) then
382 call this%callocate_unpacked_host()
383 end if
384
386 end subroutine batch_allocate_unpacked_host
387
388 !--------------------------------------------------------------
393 subroutine batch_allocate_packed_host(this)
394 class(batch_t), intent(inout) :: this
395
397
398 if (this%type() == type_float) then
399 call this%dallocate_packed_host()
400 else if (this%type() == type_cmplx) then
401 call this%zallocate_packed_host()
402 else if (this%type() == type_float_single) then
403 call this%sallocate_packed_host()
404 else if (this%type() == type_cmplx_single) then
405 call this%callocate_packed_host()
406 end if
407
409 end subroutine batch_allocate_packed_host
410
411 !--------------------------------------------------------------
415 !
416 subroutine batch_allocate_packed_device(this)
417 class(batch_t), intent(inout) :: this
418
420
421 call accel_create_buffer(this%ff_device, accel_mem_read_write, this%type(), &
422 product(this%pack_size))
423
425 end subroutine batch_allocate_packed_device
426
427 !--------------------------------------------------------------
433 !
434 subroutine batch_init_empty (this, dim, nst, np)
435 type(batch_t), intent(out) :: this
436 integer, intent(in) :: dim
437 integer, intent(in) :: nst
438 integer, intent(in) :: np
439
440 push_sub(batch_init_empty)
441
442 this%is_allocated = .false.
443 this%own_memory = .false.
444 this%special_memory = .false.
445 this%needs_finish_unpack = .false.
446 this%nst = nst
447 this%dim = dim
448 this%type_of = type_none
449
450 this%nst_linear = nst*dim
451
452 this%np = np
453 this%device_buffer_count = 0
454 this%host_buffer_count = 0
455 this%status_of = batch_not_packed
456 this%status_host = batch_not_packed
457
458 this%ndims = 2
459 safe_allocate(this%ist_idim_index(1:this%nst_linear, 1:this%ndims))
460 safe_allocate(this%ist(1:this%nst))
461
462 nullify(this%dff, this%zff, this%dff_linear, this%zff_linear)
463 nullify(this%dff_pack, this%zff_pack)
464 nullify(this%sff, this%cff, this%sff_linear, this%cff_linear)
465 nullify(this%sff_pack, this%cff_pack)
466
467 pop_sub(batch_init_empty)
468 end subroutine batch_init_empty
469
470 !--------------------------------------------------------------
475 !
476 subroutine batch_clone_to(this, dest, pack, copy_data, new_np, special, dest_type)
477 class(batch_t), intent(in) :: this
478 class(batch_t), allocatable, intent(out) :: dest
479 logical, optional, intent(in) :: pack
481 logical, optional, intent(in) :: copy_data
483 integer, optional, intent(in) :: new_np
484 logical, optional, intent(in) :: special
486 type(type_t), optional, intent(in) :: dest_type
487
489
490 if (.not. allocated(dest)) then
491 safe_allocate_type(batch_t, dest)
492 else
493 message(1) = "Internal error: destination batch in batch_clone_to has been previously allocated."
494 call messages_fatal(1)
495 end if
496
497 call this%copy_to(dest, pack, copy_data, new_np, special, dest_type)
498
499 pop_sub(batch_clone_to)
500 end subroutine batch_clone_to
501
502 !--------------------------------------------------------------
503
504 subroutine batch_clone_to_array(this, dest, n_batches, pack, copy_data, new_np, special, dest_type)
505 class(batch_t), intent(in) :: this
506 class(batch_t), allocatable, intent(out) :: dest(:)
507 integer, intent(in) :: n_batches
508 logical, optional, intent(in) :: pack
510 logical, optional, intent(in) :: copy_data
512 integer, optional, intent(in) :: new_np
513 logical, optional, intent(in) :: special
515 type(type_t), optional, intent(in) :: dest_type
516
517 integer :: ib
518
519 push_sub(batch_clone_to_array)
520
521 if (.not. allocated(dest)) then
522 safe_allocate_type_array(batch_t, dest, (1:n_batches))
523 else
524 message(1) = "Internal error: destination batch in batch_clone_to_array has been previously allocated."
525 call messages_fatal(1)
526 end if
527
528 do ib = 1, n_batches
529 call this%copy_to(dest(ib), pack, copy_data, new_np, special, dest_type)
530 end do
531
532 pop_sub(batch_clone_to_array)
533 end subroutine batch_clone_to_array
534
535 !--------------------------------------------------------------
540 subroutine batch_copy_to(this, dest, pack, copy_data, new_np, special, dest_type)
541 class(batch_t), intent(in) :: this
542 class(batch_t), intent(out) :: dest
543 logical, optional, intent(in) :: pack
545 logical, optional, intent(in) :: copy_data
547 integer, optional, intent(in) :: new_np
548 logical, optional, intent(in) :: special
550 type(type_t), optional, intent(in) :: dest_type
551
552 logical :: host_packed, special_
553 integer :: np_
554 type(type_t) :: type_
555
556 push_sub(batch_copy_to)
557
558 np_ = optional_default(new_np, this%np)
559
560 host_packed = this%host_buffer_count > 0
561 ! use special memory here only for batches not on the GPU to avoid allocating
562 ! pinned memory for temporary batches because that leads to a severe performance
563 ! decrease for GPU runs (up to 20x)
564 if (present(special)) then
565 special_ = this%special_memory
566 else
567 special_ = this%special_memory .and. .not. this%device_buffer_count > 0
568 end if
569
570 if (present(dest_type)) then
571 type_ = dest_type
572 else
573 type_ = this%type()
574 end if
575
576 if (type_ == type_float) then
577 call dbatch_init(dest, this%dim, 1, this%nst, np_, packed=host_packed, special=special_)
578 else if (type_ == type_cmplx) then
579 call zbatch_init(dest, this%dim, 1, this%nst, np_, packed=host_packed, special=special_)
580 else if (type_ == type_float_single) then
581 call sbatch_init(dest, this%dim, 1, this%nst, np_, packed=host_packed, special=special_)
582 else if (type_ == type_cmplx_single) then
583 call cbatch_init(dest, this%dim, 1, this%nst, np_, packed=host_packed, special=special_)
584 else
585 message(1) = "Internal error: unknown batch type in batch_copy_to."
586 call messages_fatal(1)
587 end if
588
589 if (this%status() /= dest%status() .and. optional_default(pack, this%is_packed())) call dest%do_pack(copy = .false.)
590
591 dest%ist_idim_index(1:this%nst_linear, 1:this%ndims) = this%ist_idim_index(1:this%nst_linear, 1:this%ndims)
592 dest%ist(1:this%nst) = this%ist(1:this%nst)
593
594 if (optional_default(copy_data, .false.)) then
595 assert(np_ == this%np)
596 ! copy_data_to cannot convert precision; use batch_copy_precision instead
597 assert(type_is_single(this%type()) .eqv. type_is_single(dest%type()))
598 call this%copy_data_to(min(this%np, np_), dest)
599 end if
600
601 pop_sub(batch_copy_to)
602 end subroutine batch_copy_to
603
604 ! ----------------------------------------------------
609 type(type_t) pure function batch_type(this) result(btype)
610 class(batch_t), intent(in) :: this
611
612 btype = this%type_of
613
614 end function batch_type
615
616 ! ----------------------------------------------------
618 integer pure function batch_type_as_integer(this) result(itype)
619 class(batch_t), intent(in) :: this
620
621 type(type_t) :: btype
622
623 itype = 0
624 btype = this%type()
625 if (btype == type_float) itype = 1
626 if (btype == type_cmplx) itype = 2
627 if (btype == type_float_single) itype = 3
628 if (btype == type_cmplx_single) itype = 4
629
630 end function batch_type_as_integer
631
632 ! ----------------------------------------------------
637 integer pure function batch_status(this) result(bstatus)
638 class(batch_t), intent(in) :: this
639
640 bstatus = this%status_of
641 end function batch_status
642
643 ! ----------------------------------------------------
644
645 logical pure function batch_is_packed(this) result(in_buffer)
646 class(batch_t), intent(in) :: this
647
648 in_buffer = (this%device_buffer_count > 0) .or. (this%host_buffer_count > 0)
649 end function batch_is_packed
650
651 ! ----------------------------------------------------
652
653 integer(int64) function batch_pack_total_size(this) result(size)
654 class(batch_t), intent(inout) :: this
655
656 size = this%np
657 if (accel_is_enabled()) size = accel_padded_size(size)
658 size = size*pad_pow2(this%nst_linear)*types_get_size(this%type())
659
660 end function batch_pack_total_size
661
662 ! ----------------------------------------------------
663
669 subroutine batch_do_pack_generic(this, copy, async)
670 class(batch_t), intent(inout) :: this
671 logical, optional, intent(in) :: copy
672 logical, optional, intent(in) :: async
674
675 integer :: source, target
676
677 ! no push_sub, called too frequently
678
679 ! get source and target states for this batch
680 source = this%status()
681 select case (source)
683 if (accel_is_enabled()) then
684 target = batch_device_packed
685 else
686 target = batch_packed
687 end if
689 target = batch_device_packed
690 end select
691
692 call this%do_pack(target, copy, async)
693
694 end subroutine batch_do_pack_generic
695
700 subroutine batch_do_pack_target(this, target, copy, async, cpu_only)
701 class(batch_t), intent(inout) :: this
702 integer, intent(in) :: target
703 logical, optional, intent(in) :: copy
704 logical, optional, intent(in) :: async
706 logical, optional, intent(in) :: cpu_only
707
708 logical :: copy_
709 logical :: async_
710 integer :: source
711
712 ! no push_sub, called too frequently
713
714 call profiling_in("BATCH_DO_PACK")
715
716 copy_ = optional_default(copy, .true.)
718 async_ = optional_default(async, .false.)
719
720 ! get source state for this batch
721 source = this%status()
722
723 ! only do something if target is different from source
724 if (source /= target) then
725 select case (target)
727 call this%allocate_packed_device()
728 this%status_of = batch_device_packed ! Note that this%status_host remains untouched.
729
730 if (copy_) then
731 select case (source)
732 case (batch_not_packed)
733 ! copy from unpacked host array to device
735 case (batch_packed)
736 ! copy from packed host array to device
737 call batch_write_packed_to_device(this, async_)
738 end select
739 end if
740 case (batch_packed)
741 call this%allocate_packed_host()
742 this%status_of = batch_packed
743 this%status_host = batch_packed
744
745 if (copy_) then
746 if (this%type() == type_float) then
747 call dbatch_pack_copy(this)
748 else if (this%type() == type_cmplx) then
749 call zbatch_pack_copy(this)
750 else if (this%type() == type_float_single) then
751 call sbatch_pack_copy(this)
752 else if (this%type() == type_cmplx_single) then
753 call cbatch_pack_copy(this)
754 end if
755 end if
756 if (this%own_memory) call this%deallocate_unpacked_host()
757 case (batch_not_packed)
758 call messages_not_implemented("Error: batch_do_pack called with BATCH_NOT_PACKED as target.")
759 end select
760 end if
761
762 select case (target)
764 this%device_buffer_count = this%device_buffer_count + 1
765 case (batch_packed)
766 this%host_buffer_count = this%host_buffer_count + 1
767 end select
768
769 call profiling_out("BATCH_DO_PACK")
770 end subroutine batch_do_pack_target
771
773 ! ----------------------------------------------------
778 subroutine batch_do_unpack(this, copy, force, async)
779 class(batch_t), intent(inout) :: this
780 logical, optional, intent(in) :: copy
781 logical, optional, intent(in) :: force
782 logical, optional, intent(in) :: async
784
785 logical :: copy_, force_, async_
786 integer :: source, target
787
788 push_sub(batch_do_unpack)
789
790 call profiling_in("BATCH_DO_UNPACK")
791
792 copy_ = optional_default(copy, .true.)
793
794 force_ = optional_default(force, .false.)
795
796 async_ = optional_default(async, .false.)
797
798 ! get source and target states for this batch
799 source = this%status()
800 select case (source)
801 case (batch_not_packed)
802 target = source
803 case (batch_packed)
804 target = batch_not_packed
806 target = this%status_host
807 end select
808
809 ! only do something if target is different from source
810 if (source /= target) then
811 select case (source)
812 case (batch_packed)
813 if (this%host_buffer_count == 1 .or. force_) then
814 if (this%own_memory) call this%allocate_unpacked_host()
815 ! unpack from packed_host to unpacked_host
816 if (copy_ .or. this%own_memory) then
817 if (this%type() == type_float) then
818 call dbatch_unpack_copy(this)
819 else if (this%type() == type_cmplx) then
820 call zbatch_unpack_copy(this)
821 else if (this%type() == type_float_single) then
822 call sbatch_unpack_copy(this)
823 else if (this%type() == type_cmplx_single) then
824 call cbatch_unpack_copy(this)
825 end if
826 end if
827 call this%deallocate_packed_host()
828 this%status_host = target
829 this%status_of = target
830 this%host_buffer_count = 1
831 end if
832 this%host_buffer_count = this%host_buffer_count - 1
834 if (this%device_buffer_count == 1 .or. force_) then
835 if (copy_) then
836 select case (target)
837 ! unpack from packed_device to unpacked_host
838 case (batch_not_packed)
840 ! unpack from packed_device to packed_host
841 case (batch_packed)
842 call batch_read_device_to_packed(this, async_)
843 end select
844 end if
845 if (async_) then
846 this%needs_finish_unpack = .true.
847 else
848 call this%deallocate_packed_device()
849 end if
850 this%status_of = target
851 this%device_buffer_count = 1
852 end if
853 this%device_buffer_count = this%device_buffer_count - 1
854 end select
855 end if
856
857 call profiling_out("BATCH_DO_UNPACK")
858
859 pop_sub(batch_do_unpack)
860 end subroutine batch_do_unpack
861
862 ! ----------------------------------------------------
864 subroutine batch_finish_unpack(this)
865 class(batch_t), intent(inout) :: this
866
867 push_sub(batch_finish_unpack)
868 if (this%needs_finish_unpack) then
869 call accel_finish()
870 call this%deallocate_packed_device()
871 this%needs_finish_unpack = .false.
872 end if
873 pop_sub(batch_finish_unpack)
874 end subroutine batch_finish_unpack
875
876 ! ----------------------------------------------------
877
878 subroutine batch_write_unpacked_to_device(this)
879 class(batch_t), intent(inout) :: this
880
881 integer :: ist, ist2
882 integer(int64) :: unroll, bsize, gsize
883 type(accel_mem_t) :: tmp
884 type(accel_kernel_t), pointer :: kernel
885
887
888 call profiling_in("BATCH_WRT_UNPACK_ACCEL")
889 if (this%nst_linear == 1) then
890 ! we can copy directly
891 if (this%type() == type_float) then
892 call accel_write_buffer(this%ff_device, ubound(this%dff_linear, dim=1), this%dff_linear(:, 1))
893 else if (this%type() == type_cmplx) then
894 call accel_write_buffer(this%ff_device, ubound(this%zff_linear, dim=1), this%zff_linear(:, 1))
895 else if (this%type() == type_float_single) then
896 call accel_write_buffer(this%ff_device, ubound(this%sff_linear, dim=1), this%sff_linear(:, 1))
897 else
898 call accel_write_buffer(this%ff_device, ubound(this%cff_linear, dim=1), this%cff_linear(:, 1))
899 end if
900
901 else
902 ! we copy to a temporary array and then we re-arrange data
903
904 if (this%type() == type_float) then
905 kernel => dpack
906 else if (this%type() == type_cmplx) then
907 kernel => zpack
908 else if (this%type() == type_float_single) then
909 kernel => spack
910 else
911 kernel => cpack
912 end if
913
914 unroll = min(int(gpu_pack_max_buffer_size, int64), this%pack_size(1))
915
916 call accel_create_buffer(tmp, accel_mem_read_only, this%type(), unroll*this%pack_size(2))
917
918 do ist = 1, this%nst_linear, int(unroll, int32)
919
920 ! copy a number 'unroll' of states to the buffer
921 do ist2 = ist, min(ist + int(unroll, int32) - 1, this%nst_linear)
922
923 if (this%type() == type_float) then
924 call accel_write_buffer(tmp, ubound(this%dff_linear, dim=1, kind=int64), this%dff_linear(:, ist2), &
925 offset = (ist2 - ist)*this%pack_size(2))
926 else if (this%type() == type_cmplx) then
927 call accel_write_buffer(tmp, ubound(this%zff_linear, dim=1, kind=int64), this%zff_linear(:, ist2), &
928 offset = (ist2 - ist)*this%pack_size(2))
929 else if (this%type() == type_float_single) then
930 call accel_write_buffer(tmp, ubound(this%sff_linear, dim=1, kind=int64), this%sff_linear(:, ist2), &
931 offset = (ist2 - ist)*this%pack_size(2))
932 else
933 call accel_write_buffer(tmp, ubound(this%cff_linear, dim=1, kind=int64), this%cff_linear(:, ist2), &
934 offset = (ist2 - ist)*this%pack_size(2))
935 end if
936 end do
937
938 ! now call a kernel to rearrange the data
939 call accel_set_kernel_arg(kernel, 0, int(this%pack_size(1), int32))
940 call accel_set_kernel_arg(kernel, 1, this%np)
941 call accel_set_kernel_arg(kernel, 2, this%nst_linear)
942 call accel_set_kernel_arg(kernel, 3, ist - 1)
943 call accel_set_kernel_arg(kernel, 4, tmp)
944 call accel_set_kernel_arg(kernel, 5, this%ff_device)
945
946 call profiling_in("GPU_PACK")
947
948 ! Compute the grid size
949 bsize = accel_max_block_size()/unroll
950 call accel_grid_size(this%pack_size(2), bsize, gsize)
951
952 call accel_kernel_run(kernel, (/gsize, 1_int64/), (/bsize, unroll/))
953
954 if (this%type() == type_float) then
955 call profiling_count_transfers(unroll*this%pack_size(2), m_one)
956 else
957 call profiling_count_transfers(unroll*this%pack_size(2), m_zi)
958 end if
959
960 call accel_finish()
961 call profiling_out("GPU_PACK")
962
963 end do
964
965 call accel_free_buffer(tmp)
966
967 end if
968
969 call profiling_out("BATCH_WRT_UNPACK_ACCEL")
971 end subroutine batch_write_unpacked_to_device
972
973 ! ------------------------------------------------------------------
974
975 subroutine batch_read_device_to_unpacked(this)
976 class(batch_t), intent(inout) :: this
977
978 integer :: ist, ist2
979 integer(int64) :: unroll, bsize, gsize
980 type(accel_mem_t) :: tmp
981 type(accel_kernel_t), pointer :: kernel
982
984 call profiling_in("BATCH_READ_UNPACKED_ACCEL")
985
986 if (this%nst_linear == 1) then
987 ! we can copy directly
988 if (this%type() == type_float) then
989 call accel_read_buffer(this%ff_device, ubound(this%dff_linear, dim=1), this%dff_linear(:, 1))
990 else if (this%type() == type_cmplx) then
991 call accel_read_buffer(this%ff_device, ubound(this%zff_linear, dim=1), this%zff_linear(:, 1))
992 else if (this%type() == type_float_single) then
993 call accel_read_buffer(this%ff_device, ubound(this%sff_linear, dim=1), this%sff_linear(:, 1))
994 else
995 call accel_read_buffer(this%ff_device, ubound(this%cff_linear, dim=1), this%cff_linear(:, 1))
996 end if
997 else
998
999 unroll = min(int(gpu_pack_max_buffer_size, int64), this%pack_size(1))
1000
1001 ! we use a kernel to move to a temporary array and then we read
1002 call accel_create_buffer(tmp, accel_mem_write_only, this%type(), unroll*this%pack_size(2))
1003
1004 if (this%type() == type_float) then
1005 kernel => dunpack
1006 else if (this%type() == type_cmplx) then
1007 kernel => zunpack
1008 else if (this%type() == type_float_single) then
1009 kernel => sunpack
1010 else
1011 kernel => cunpack
1012 end if
1013
1014 do ist = 1, this%nst_linear, int(unroll, int32)
1015 call accel_set_kernel_arg(kernel, 0, int(this%pack_size(1), int32))
1016 call accel_set_kernel_arg(kernel, 1, this%np)
1017 call accel_set_kernel_arg(kernel, 2, this%nst_linear)
1018 call accel_set_kernel_arg(kernel, 3, ist - 1)
1019 call accel_set_kernel_arg(kernel, 4, this%ff_device)
1020 call accel_set_kernel_arg(kernel, 5, tmp)
1021
1022 call profiling_in("GPU_UNPACK")
1023
1024 ! Compute the grid size
1025 bsize = accel_max_block_size()/unroll
1026 call accel_grid_size(this%pack_size(2), bsize, gsize)
1027
1028 call accel_kernel_run(kernel, (/1_int64, gsize/), (/unroll, bsize/))
1029
1030 if (this%type() == type_float) then
1031 call profiling_count_transfers(unroll*this%pack_size(2), m_one)
1032 else
1033 call profiling_count_transfers(unroll*this%pack_size(2), m_zi)
1034 end if
1035
1036 call accel_finish()
1037 call profiling_out("GPU_UNPACK")
1038
1039 ! copy a number 'unroll' of states from the buffer
1040 do ist2 = ist, min(ist + int(unroll, int32) - 1, this%nst_linear)
1041
1042 if (this%type() == type_float) then
1043 call accel_read_buffer(tmp, ubound(this%dff_linear, dim=1, kind=int64), this%dff_linear(:, ist2), &
1044 offset = (ist2 - ist)*this%pack_size(2))
1045 else if (this%type() == type_cmplx) then
1046 call accel_read_buffer(tmp, ubound(this%zff_linear, dim=1, kind=int64), this%zff_linear(:, ist2), &
1047 offset = (ist2 - ist)*this%pack_size(2))
1048 else if (this%type() == type_float_single) then
1049 call accel_read_buffer(tmp, ubound(this%sff_linear, dim=1, kind=int64), this%sff_linear(:, ist2), &
1050 offset = (ist2 - ist)*this%pack_size(2))
1051 else
1052 call accel_read_buffer(tmp, ubound(this%cff_linear, dim=1, kind=int64), this%cff_linear(:, ist2), &
1053 offset = (ist2 - ist)*this%pack_size(2))
1054 end if
1055 end do
1056
1057 end do
1058
1059 call accel_free_buffer(tmp)
1060 end if
1061
1062 call profiling_out("BATCH_READ_UNPACKED_ACCEL")
1064 end subroutine batch_read_device_to_unpacked
1065
1066 ! ------------------------------------------------------------------
1067 subroutine batch_write_packed_to_device(this, async)
1068 class(batch_t), intent(inout) :: this
1069 logical, optional, intent(in) :: async
1070
1071
1073
1074 call profiling_in("BATCH_WRITE_PACKED_ACCEL")
1075 if (this%type() == type_float) then
1076 call accel_write_buffer(this%ff_device, this%pack_size(1), this%pack_size(2), this%dff_pack, async=async)
1077 else if (this%type() == type_cmplx) then
1078 call accel_write_buffer(this%ff_device, this%pack_size(1), this%pack_size(2), this%zff_pack, async=async)
1079 else if (this%type() == type_float_single) then
1080 call accel_write_buffer(this%ff_device, this%pack_size(1), this%pack_size(2), this%sff_pack, async=async)
1081 else
1082 call accel_write_buffer(this%ff_device, this%pack_size(1), this%pack_size(2), this%cff_pack, async=async)
1083 end if
1084 call profiling_out("BATCH_WRITE_PACKED_ACCEL")
1085
1087 end subroutine batch_write_packed_to_device
1088
1089 ! ------------------------------------------------------------------
1090 subroutine batch_read_device_to_packed(this, async)
1091 class(batch_t), intent(inout) :: this
1092 logical, optional, intent(in) :: async
1093
1094
1096
1097 call profiling_in("BATCH_READ_PACKED_ACCEL")
1098 if (this%type() == type_float) then
1099 call accel_read_buffer(this%ff_device, this%pack_size(1), this%pack_size(2), this%dff_pack, async=async)
1100 else if (this%type() == type_cmplx) then
1101 call accel_read_buffer(this%ff_device, this%pack_size(1), this%pack_size(2), this%zff_pack, async=async)
1102 else if (this%type() == type_float_single) then
1103 call accel_read_buffer(this%ff_device, this%pack_size(1), this%pack_size(2), this%sff_pack, async=async)
1104 else
1105 call accel_read_buffer(this%ff_device, this%pack_size(1), this%pack_size(2), this%cff_pack, async=async)
1106 end if
1107 call profiling_out("BATCH_READ_PACKED_ACCEL")
1108
1110 end subroutine batch_read_device_to_packed
1111
1112 ! ------------------------------------------------------
1117 integer function batch_inv_index(this, cind) result(index)
1118 class(batch_t), intent(in) :: this
1119 integer, intent(in) :: cind(:)
1120
1121 do index = 1, this%nst_linear
1122 if (all(cind(1:this%ndims) == this%ist_idim_index(index, 1:this%ndims))) exit
1123 end do
1124
1125 assert(index <= this%nst_linear)
1126
1127 end function batch_inv_index
1128
1129 ! ------------------------------------------------------
1133 !
1134 integer pure function batch_ist_idim_to_linear(this, cind) result(index)
1135 class(batch_t), intent(in) :: this
1136 integer, intent(in) :: cind(:)
1137
1138 if (ubound(cind, dim = 1) == 1) then
1139 index = cind(1)
1140 else
1141 index = (cind(1) - 1)*this%dim + cind(2)
1142 end if
1143
1144 end function batch_ist_idim_to_linear
1145
1146 ! ------------------------------------------------------
1151 !
1152 integer pure function batch_linear_to_ist(this, linear_index) result(ist)
1153 class(batch_t), intent(in) :: this
1154 integer, intent(in) :: linear_index
1155
1156 ist = this%ist_idim_index(linear_index, 1)
1157
1158 end function batch_linear_to_ist
1159
1160 ! ------------------------------------------------------
1163 integer pure function batch_linear_to_idim(this, linear_index) result(idim)
1164 class(batch_t), intent(in) :: this
1165 integer, intent(in) :: linear_index
1166
1167 idim = this%ist_idim_index(linear_index, 2)
1168
1169 end function batch_linear_to_idim
1170
1171 ! ------------------------------------------------------
1180 !
1181 subroutine batch_remote_access_start(this, mpi_grp, rma_win)
1182 class(batch_t), intent(inout) :: this
1183 type(mpi_grp_t), intent(in) :: mpi_grp
1184 type(mpi_win), intent(out) :: rma_win
1185
1187
1188 if (mpi_grp%size > 1) then
1190 assert(.not. accel_is_enabled())
1191
1192 call this%do_pack()
1193
1194 assert(int(product(this%pack_size)*types_get_size(this%type())) > 0) ! In case of overflow, this becomes negative
1195 if (this%type() == type_cmplx) then
1196#ifdef HAVE_MPI
1197 call mpi_win_create(this%zff_pack(1, 1), int(product(this%pack_size)*types_get_size(this%type()), mpi_address_kind), &
1198 types_get_size(this%type()), mpi_info_null, mpi_grp%comm, rma_win)
1199#endif
1200 else if (this%type() == type_float) then
1201#ifdef HAVE_MPI
1202 call mpi_win_create(this%dff_pack(1, 1), int(product(this%pack_size)*types_get_size(this%type()), mpi_address_kind), &
1203 types_get_size(this%type()), mpi_info_null, mpi_grp%comm, rma_win)
1204#endif
1205 else if (this%type() == type_cmplx_single) then
1206#ifdef HAVE_MPI
1207 call mpi_win_create(this%cff_pack(1, 1), int(product(this%pack_size)*types_get_size(this%type()), mpi_address_kind), &
1208 types_get_size(this%type()), mpi_info_null, mpi_grp%comm, rma_win)
1209#endif
1210 else if (this%type() == type_float_single) then
1211#ifdef HAVE_MPI
1212 call mpi_win_create(this%sff_pack(1, 1), int(product(this%pack_size)*types_get_size(this%type()), mpi_address_kind), &
1213 types_get_size(this%type()), mpi_info_null, mpi_grp%comm, rma_win)
1214#endif
1215 else
1216 message(1) = "Internal error: unknown batch type in batch_remote_access_start."
1217 call messages_fatal(1)
1218 end if
1219
1220 else
1221 rma_win = mpi_win_null
1222 end if
1223
1225 end subroutine batch_remote_access_start
1226
1227 ! ------------------------------------------------------
1233 !
1234 subroutine batch_remote_access_stop(this, rma_win)
1235 class(batch_t), intent(inout) :: this
1236 type(mpi_win), intent(inout) :: rma_win
1237
1238 push_sub(batch_remote_access_stop)
1239
1240 if (rma_win /= mpi_win_null) then
1241#ifdef HAVE_MPI
1242 call mpi_win_free(rma_win)
1243#endif
1244 call this%do_unpack()
1245 end if
1246
1248 end subroutine batch_remote_access_stop
1249
1250 ! --------------------------------------------------------------
1252 !
1253 subroutine batch_copy_data_to(this, np, dest, async)
1254 class(batch_t), intent(in) :: this
1255 integer, intent(in) :: np
1256 class(batch_t), intent(inout) :: dest
1257 logical, optional, intent(in) :: async
1258
1259 integer(int64), dimension(3) :: gsizes, bsizes
1260 integer :: ist, ip
1261 type(accel_kernel_t), pointer :: kernel
1262
1263 push_sub(batch_copy_data_to)
1264 call profiling_in("BATCH_COPY_DATA_TO")
1265
1266 ! this routine can be used to copy data between batches of different type
1267 call this%check_compatibility_with(dest, type_check=.false.)
1268
1269 if (this%type() == dest%type()) then
1270 select case (this%status())
1271 case (batch_device_packed)
1272 ! pack_size_real counts elements of the real type matching the batch precision,
1273 ! so the kernel has to match it
1274 if (type_is_single(this%type())) then
1275 kernel => kernel_scopy
1276 else
1277 kernel => kernel_copy
1278 end if
1279 call accel_set_kernel_arg(kernel, 0, np)
1280 call accel_set_kernel_arg(kernel, 1, this%ff_device)
1281 call accel_set_kernel_arg(kernel, 2, log2(int(this%pack_size_real(1), int32)))
1282 call accel_set_kernel_arg(kernel, 3, dest%ff_device)
1283 call accel_set_kernel_arg(kernel, 4, log2(int(dest%pack_size_real(1), int32)))
1284
1285 ! Compute the grid (extend to another dimensions if the size of the problem is too big)
1286 call accel_grid_size_extend_dim(int(np, int64), dest%pack_size_real(1), gsizes, bsizes, kernel)
1287
1288 call accel_kernel_run(kernel, gsizes, bsizes)
1289
1290 if(.not. optional_default(async, .false.)) call accel_finish()
1291
1292 case (batch_packed)
1293 if (dest%type() == type_float_single) then
1294 !$omp parallel do private(ist) schedule(static)
1295 do ip = 1, np
1296 do ist = 1, int(this%pack_size(1), int32)
1297 dest%sff_pack(ist, ip) = this%sff_pack(ist, ip)
1298 end do
1299 end do
1300 else if (dest%type() == type_cmplx_single) then
1301 !$omp parallel do private(ist) schedule(static)
1302 do ip = 1, np
1303 do ist = 1, int(this%pack_size(1), int32)
1304 dest%cff_pack(ist, ip) = this%cff_pack(ist, ip)
1305 end do
1306 end do
1307 else if (np*this%pack_size(1) > huge(0_int32)) then
1308 ! BLAS cannot handle 8-byte integers, so we need a special version here
1309 do ip = 1, np
1310 if (dest%type() == type_float) then
1311 call blas_copy(int(this%pack_size(1), int32), this%dff_pack(1, ip), 1, dest%dff_pack(1, ip), 1)
1312 else
1313 call blas_copy(int(this%pack_size(1), int32), this%zff_pack(1, ip), 1, dest%zff_pack(1, ip), 1)
1314 end if
1315 end do
1316 else
1317 if (dest%type() == type_float) then
1318 call blas_copy(int(this%pack_size(1)*np, int32), this%dff_pack(1, 1), 1, dest%dff_pack(1, 1), 1)
1319 else
1320 call blas_copy(int(this%pack_size(1)*np, int32), this%zff_pack(1, 1), 1, dest%zff_pack(1, 1), 1)
1321 end if
1322 end if
1323
1324 case (batch_not_packed)
1325 do ist = 1, dest%nst_linear
1326 if (dest%type() == type_cmplx) then
1327 call blas_copy(np, this%zff_linear(1, ist), 1, dest%zff_linear(1, ist), 1)
1328 else if (dest%type() == type_float_single) then
1329 !$omp parallel do schedule(static)
1330 do ip = 1, np
1331 dest%sff_linear(ip, ist) = this%sff_linear(ip, ist)
1332 end do
1333 else if (dest%type() == type_cmplx_single) then
1334 !$omp parallel do schedule(static)
1335 do ip = 1, np
1336 dest%cff_linear(ip, ist) = this%cff_linear(ip, ist)
1337 end do
1338 else
1339 call blas_copy(np, this%dff_linear(1, ist), 1, dest%dff_linear(1, ist), 1)
1340 end if
1341 end do
1342
1343 end select
1344 else if (this%type() == type_cmplx) then
1345 ! copy complex -> real
1346 select case (this%status())
1347 case (batch_device_packed)
1348 call accel_set_kernel_arg(kernel_copy_complex_to_real, 0, np)
1349 call accel_set_kernel_arg(kernel_copy_complex_to_real, 1, this%ff_device)
1350 call accel_set_kernel_arg(kernel_copy_complex_to_real, 2, log2(int(this%pack_size_real(1), int32)))
1351 call accel_set_kernel_arg(kernel_copy_complex_to_real, 3, dest%ff_device)
1352 call accel_set_kernel_arg(kernel_copy_complex_to_real, 4, log2(int(dest%pack_size_real(1), int32)))
1353
1354 ! Compute the grid (extend to another dimensions if the size of the problem is too big)
1355 call accel_grid_size_extend_dim(int(np, int64), dest%pack_size_real(1), gsizes, bsizes, kernel_copy_complex_to_real)
1356
1357 call accel_kernel_run(kernel_copy_complex_to_real, gsizes, bsizes)
1358
1359 if(.not. optional_default(async, .false.)) call accel_finish()
1360
1361 case (batch_packed)
1362 !$omp parallel do private(ist, ip)
1363 do ip = 1, np
1364 !$omp simd
1365 do ist = 1, dest%nst_linear
1366 dest%dff_pack(ist, ip) = real(this%zff_pack(ist, ip), real64)
1367 end do
1368 end do
1369 !$omp end parallel do
1370 case (batch_not_packed)
1371 !$omp parallel private(ist, ip)
1372 do ist = 1, dest%nst_linear
1373 !$omp do
1374 do ip = 1, np
1375 dest%dff_linear(ip, ist) = real(this%zff_linear(ip, ist), real64)
1376 end do
1377 !$omp end do nowait
1378 end do
1379 !$omp end parallel
1380 end select
1381 else if (this%type() == type_float) then
1382 ! copy real -> complex
1383 select case (this%status())
1384 case (batch_device_packed)
1385 call accel_set_kernel_arg(kernel_copy_real_to_complex, 0, np)
1386 call accel_set_kernel_arg(kernel_copy_real_to_complex, 1, this%ff_device)
1387 call accel_set_kernel_arg(kernel_copy_real_to_complex, 2, log2(int(this%pack_size_real(1), int32)))
1388 call accel_set_kernel_arg(kernel_copy_real_to_complex, 3, dest%ff_device)
1389 call accel_set_kernel_arg(kernel_copy_real_to_complex, 4, log2(int(dest%pack_size_real(1), int32)))
1390
1391 ! Compute the grid (extend to another dimensions if the size of the problem is too big)
1392 call accel_grid_size_extend_dim(int(np, int64), this%pack_size_real(1), gsizes, bsizes, kernel_copy_real_to_complex)
1393
1394 call accel_kernel_run(kernel_copy_real_to_complex, gsizes, bsizes)
1395
1396 if(.not. optional_default(async, .false.)) call accel_finish()
1397
1398 case (batch_packed)
1399 !$omp parallel do private(ist, ip)
1400 do ip = 1, np
1401 !$omp simd
1402 do ist = 1, dest%nst_linear
1403 dest%zff_pack(ist, ip) = cmplx(this%dff_pack(ist, ip), m_zero, real64)
1404 end do
1405 end do
1406 !$omp end parallel do
1407 case (batch_not_packed)
1408 !$omp parallel private(ist, ip)
1409 do ist = 1, dest%nst_linear
1410 !$omp do
1411 do ip = 1, np
1412 dest%zff_linear(ip, ist) = cmplx(this%dff_linear(ip, ist), m_zero, real64)
1413 end do
1414 !$omp end do nowait
1415 end do
1416 !$omp end parallel
1417 end select
1418 else
1419 message(1) = "Error! This should not happen."
1420 call messages_fatal(1)
1421 end if
1422
1423 call profiling_out("BATCH_COPY_DATA_TO")
1424 pop_sub(batch_copy_data_to)
1425 end subroutine batch_copy_data_to
1426
1427 ! --------------------------------------------------------------
1429 !
1430 subroutine batch_check_compatibility_with(this, target, only_check_dim, type_check)
1431 class(batch_t), intent(in) :: this
1432 class(batch_t), intent(in) :: target
1433 logical, optional, intent(in) :: only_check_dim
1434 logical, optional, intent(in) :: type_check
1435
1437
1438 if (optional_default(type_check, .true.)) then
1439 assert(this%type() == target%type())
1440 end if
1441 if (.not. optional_default(only_check_dim, .false.)) then
1442 assert(this%nst_linear == target%nst_linear)
1443 end if
1444 assert(this%status() == target%status())
1445 assert(this%dim == target%dim)
1446
1448
1449 end subroutine batch_check_compatibility_with
1450
1451!--------------------------------------------------------------
1454 subroutine batch_build_indices(this, st_start, st_end)
1455 class(batch_t), intent(inout) :: this
1456 integer, intent(in) :: st_start
1457 integer, intent(in) :: st_end
1458
1459 integer :: idim, ii, ist
1460
1461 push_sub(batch_build_indices)
1462
1463 do ist = st_start, st_end
1464 ! now we also populate the linear array
1465 do idim = 1, this%dim
1466 ii = this%dim*(ist - st_start) + idim
1467 this%ist_idim_index(ii, 1) = ist
1468 this%ist_idim_index(ii, 2) = idim
1469 end do
1470 this%ist(ist - st_start + 1) = ist
1471 end do
1472
1473 ! compute packed sizes
1474 this%pack_size(1) = pad_pow2(this%nst_linear)
1475 this%pack_size(2) = this%np
1476 if (accel_is_enabled()) this%pack_size(2) = accel_padded_size(this%pack_size(2))
1477
1478 this%pack_size_real = this%pack_size
1479 if (type_is_complex(this%type())) this%pack_size_real(1) = 2*this%pack_size_real(1)
1480
1481 pop_sub(batch_build_indices)
1482 end subroutine batch_build_indices
1483
1484
1485#include "real.F90"
1486#include "batch_inc.F90"
1487#include "undef.F90"
1488
1489#include "complex.F90"
1490#include "batch_inc.F90"
1491#include "undef.F90"
1492
1493#include "real32.F90"
1494#include "batch_inc.F90"
1495#include "undef.F90"
1496
1497#include "complex32.F90"
1498#include "batch_inc.F90"
1499#include "undef.F90"
1500
1501end module batch_oct_m
1503!! Local Variables:
1504!! mode: f90
1505!! coding: utf-8
1506!! End:
initialize a batch with existing memory
Definition: batch.F90:278
double log2(double __x) __attribute__((__nothrow__
subroutine, public accel_free_buffer(this, async)
Definition: accel.F90:967
integer, parameter, public accel_mem_read_write
Definition: accel.F90:162
This module contains interfaces for routines in allocate_hardware_aware.c.
subroutine, public deallocate_hardware_aware(array, size)
This module implements batches of mesh functions.
Definition: batch.F90:112
type(type_t) pure function batch_type(this)
return the type of a batch
Definition: batch.F90:682
subroutine zbatch_pack_copy(this)
copy data from the unpacked to the packed arrays
Definition: batch.F90:2094
subroutine cbatch_init_with_memory_3(this, dim, st_start, st_end, psi)
initialize a batch with an rank-3 array of TYPE_CMPLX_SINGLE valued mesh functions psi.
Definition: batch.F90:2547
subroutine, public batch_write_packed_to_device(this, async)
Definition: batch.F90:1140
integer, parameter, public batch_not_packed
functions are stored in CPU memory, unpacked order
Definition: batch.F90:293
integer, parameter, public batch_device_packed
functions are stored in device memory in packed order
Definition: batch.F90:293
subroutine zbatch_init_with_memory_3(this, dim, st_start, st_end, psi)
initialize a batch with an rank-3 array of TYPE_CMPLX valued mesh functions psi.
Definition: batch.F90:1919
subroutine batch_check_compatibility_with(this, target, only_check_dim, type_check)
check whether two batches have compatible dimensions (and type)
Definition: batch.F90:1503
logical pure function batch_is_packed(this)
Definition: batch.F90:718
subroutine dbatch_unpack_copy(this)
copy data from the packed to the unpacked arrays
Definition: batch.F90:1817
subroutine cbatch_pack_copy(this)
copy data from the unpacked to the packed arrays
Definition: batch.F90:2722
subroutine dbatch_init_with_memory_1(this, psi)
initialize a batch with an rank-1 array of TYPE_FLOAT valued mesh functions psi.
Definition: batch.F90:1674
subroutine cbatch_init_with_memory_2(this, dim, st_start, st_end, psi)
initialize a batch with an rank-2 array of TYPE_CMPLX_SINGLE valued mesh functions psi.
Definition: batch.F90:2583
subroutine batch_write_unpacked_to_device(this)
Definition: batch.F90:951
subroutine batch_do_unpack(this, copy, force, async)
unpack a batch
Definition: batch.F90:851
subroutine batch_finish_unpack(this)
finish the unpacking if do_unpack() was called with async=.true.
Definition: batch.F90:937
subroutine zbatch_allocate_unpacked_host(this)
allocate host (CPU) memory for unpacked data of type TYPE_CMPLX
Definition: batch.F90:2005
subroutine batch_deallocate_packed_device(this)
release packed device memory
Definition: batch.F90:428
integer pure function batch_type_as_integer(this)
For debuging purpose only.
Definition: batch.F90:691
subroutine sbatch_unpack_copy(this)
copy data from the packed to the unpacked arrays
Definition: batch.F90:2441
subroutine batch_do_pack_generic(this, copy, async)
pack the data in a batch
Definition: batch.F90:742
integer function batch_inv_index(this, cind)
inverse index lookup
Definition: batch.F90:1190
subroutine dbatch_init_with_memory_2(this, dim, st_start, st_end, psi)
initialize a batch with an rank-2 array of TYPE_FLOAT valued mesh functions psi.
Definition: batch.F90:1645
subroutine batch_allocate_packed_host(this)
allocate host (CPU) memory for packed data
Definition: batch.F90:466
subroutine, public zbatch_init(this, dim, st_start, st_end, np, special, packed)
initialize a TYPE_CMPLX valued batch to given size without providing external memory
Definition: batch.F90:2055
subroutine zbatch_allocate_packed_host(this)
allocate host (CPU) memory for packed data of type TYPE_CMPLX
Definition: batch.F90:2031
subroutine batch_clone_to(this, dest, pack, copy_data, new_np, special, dest_type)
clone a batch to a new batch
Definition: batch.F90:549
subroutine cbatch_allocate_unpacked_host(this)
allocate host (CPU) memory for unpacked data of type TYPE_CMPLX_SINGLE
Definition: batch.F90:2633
subroutine batch_remote_access_stop(this, rma_win)
stop the remote access to the batch
Definition: batch.F90:1307
subroutine sbatch_allocate_unpacked_host(this)
allocate host (CPU) memory for unpacked data of type TYPE_FLOAT_SINGLE
Definition: batch.F90:2319
subroutine batch_read_device_to_unpacked(this)
Definition: batch.F90:1048
subroutine zbatch_init_with_memory_1(this, psi)
initialize a batch with an rank-1 array of TYPE_CMPLX valued mesh functions psi.
Definition: batch.F90:1984
subroutine, public sbatch_init(this, dim, st_start, st_end, np, special, packed)
initialize a TYPE_FLOAT_SINGLE valued batch to given size without providing external memory
Definition: batch.F90:2369
subroutine sbatch_pack_copy(this)
copy data from the unpacked to the packed arrays
Definition: batch.F90:2408
subroutine dbatch_allocate_packed_host(this)
allocate host (CPU) memory for packed data of type TYPE_FLOAT
Definition: batch.F90:1721
subroutine batch_clone_to_array(this, dest, n_batches, pack, copy_data, new_np, special, dest_type)
Definition: batch.F90:577
subroutine batch_allocate_packed_device(this)
allocate device (GPU) memory for packed data
Definition: batch.F90:489
subroutine batch_build_indices(this, st_start, st_end)
build the index ist(:) and ist_idim_index(:,:) and set pack_size
Definition: batch.F90:1527
integer pure function batch_ist_idim_to_linear(this, cind)
direct index lookup
Definition: batch.F90:1207
integer, parameter gpu_pack_max_buffer_size
this value controls the size (in number of wave-functions) of the buffer used to copy states to the G...
Definition: batch.F90:298
subroutine batch_do_pack_target(this, target, copy, async, cpu_only)
pack the data in a batch
Definition: batch.F90:773
subroutine sbatch_init_with_memory_1(this, psi)
initialize a batch with an rank-1 array of TYPE_FLOAT_SINGLE valued mesh functions psi.
Definition: batch.F90:2298
integer pure function batch_linear_to_ist(this, linear_index)
get state index ist from linear (combined dim and nst) index
Definition: batch.F90:1225
subroutine batch_copy_to(this, dest, pack, copy_data, new_np, special, dest_type)
make a copy of a batch
Definition: batch.F90:613
subroutine, public batch_read_device_to_packed(this, async)
Definition: batch.F90:1163
subroutine cbatch_unpack_copy(this)
copy data from the packed to the unpacked arrays
Definition: batch.F90:2755
subroutine dbatch_init_with_memory_3(this, dim, st_start, st_end, psi)
initialize a batch with an rank-3 array of TYPE_FLOAT valued mesh functions psi.
Definition: batch.F90:1609
subroutine cbatch_init_with_memory_1(this, psi)
initialize a batch with an rank-1 array of TYPE_CMPLX_SINGLE valued mesh functions psi.
Definition: batch.F90:2612
subroutine batch_allocate_unpacked_host(this)
allocate host (CPU) memory for unpacked data
Definition: batch.F90:443
subroutine, public cbatch_init(this, dim, st_start, st_end, np, special, packed)
initialize a TYPE_CMPLX_SINGLE valued batch to given size without providing external memory
Definition: batch.F90:2683
subroutine batch_init_empty(this, dim, nst, np)
initialize an empty batch
Definition: batch.F90:507
subroutine, public dbatch_init(this, dim, st_start, st_end, np, special, packed)
initialize a TYPE_FLOAT valued batch to given size without providing external memory
Definition: batch.F90:1745
subroutine zbatch_init_with_memory_2(this, dim, st_start, st_end, psi)
initialize a batch with an rank-2 array of TYPE_CMPLX valued mesh functions psi.
Definition: batch.F90:1955
integer pure function batch_linear_to_idim(this, linear_index)
extract idim from linear index
Definition: batch.F90:1236
subroutine batch_remote_access_start(this, mpi_grp, rma_win)
start remote access to a batch on another node
Definition: batch.F90:1254
subroutine sbatch_allocate_packed_host(this)
allocate host (CPU) memory for packed data of type TYPE_FLOAT_SINGLE
Definition: batch.F90:2345
subroutine batch_copy_data_to(this, np, dest, async)
copy data to another batch.
Definition: batch.F90:1326
subroutine dbatch_allocate_unpacked_host(this)
allocate host (CPU) memory for unpacked data of type TYPE_FLOAT
Definition: batch.F90:1695
subroutine dbatch_pack_copy(this)
copy data from the unpacked to the packed arrays
Definition: batch.F90:1784
subroutine cbatch_allocate_packed_host(this)
allocate host (CPU) memory for packed data of type TYPE_CMPLX_SINGLE
Definition: batch.F90:2659
subroutine sbatch_init_with_memory_2(this, dim, st_start, st_end, psi)
initialize a batch with an rank-2 array of TYPE_FLOAT_SINGLE valued mesh functions psi.
Definition: batch.F90:2269
subroutine sbatch_init_with_memory_3(this, dim, st_start, st_end, psi)
initialize a batch with an rank-3 array of TYPE_FLOAT_SINGLE valued mesh functions psi.
Definition: batch.F90:2233
integer pure function batch_status(this)
return the status of a batch
Definition: batch.F90:710
subroutine batch_deallocate_unpacked_host(this)
release unpacked host memory
Definition: batch.F90:350
integer, parameter, public batch_packed
functions are stored in CPU memory, in transposed (packed) order
Definition: batch.F90:293
subroutine batch_deallocate_packed_host(this)
release packed host memory
Definition: batch.F90:393
integer(int64) function batch_pack_total_size(this)
Definition: batch.F90:726
subroutine batch_end(this, copy)
finalize a batch and release allocated memory, if necessary
Definition: batch.F90:312
subroutine zbatch_unpack_copy(this)
copy data from the packed to the unpacked arrays
Definition: batch.F90:2127
This module contains interfaces for BLAS routines You should not use these routines directly....
Definition: blas.F90:97
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:94
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:138
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:386
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
Definition: profiling.F90:122
type(type_t), parameter, public type_cmplx_single
Definition: types.F90:119
type(type_t), parameter, public type_float_single
Definition: types.F90:118
type(type_t), parameter, public type_cmplx
Definition: types.F90:114
type(type_t), parameter, public type_float
Definition: types.F90:113
logical pure function, public type_is_single(this)
Definition: types.F90:171
type(type_t), parameter, public type_none
Definition: types.F90:112
Class defining batches of mesh functions.
Definition: batch.F90:142
int true(void)