Octopus
phase.F90
Go to the documentation of this file.
1!! Copyright (C) 2009 X. Andrade
2!! Copyright (C) 2024 N. Tancogne-Dejean
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 phase_oct_m
23 use accel_oct_m
24 use batch_oct_m
27 use debug_oct_m
31 use global_oct_m
32 use grid_oct_m
35 use math_oct_m
36 use mesh_oct_m
38 use mpi_oct_m
40 use space_oct_m
44 use types_oct_m
46
47 implicit none
48
49 private
50
51 public :: &
52 phase_t, &
55
86 type phase_t
87 private
88 complex(real64), allocatable :: phase(:, :)
91 complex(real64), public, allocatable :: phase_corr(:,:)
94 complex(real64), allocatable :: phase_k(:, :)
96 complex(real64), allocatable :: phase_corr_k(:,:)
97 complex(real64), allocatable :: vp_phase(:)
98 complex(real64), allocatable :: vp_phase_corr(:)
99 complex(real64), allocatable :: phase_spiral(:,:)
102 type(accel_mem_t) :: buff_phase
103 type(accel_mem_t) :: buff_phase_spiral
104 type(accel_mem_t), public :: buff_phase_corr
105 integer :: buff_phase_qn_start
106 real(real64), public, pointer :: spin(:,:,:) => null()
107 contains
108 procedure :: init => phase_init_phases
109
110 procedure :: update => phase_update_phases
111
112 procedure :: end => phase_end
113
114 procedure :: set_phase_corr => phase_set_phase_corr
115
116 procedure :: unset_phase_corr => phase_unset_phase_corr
117
118 procedure :: apply_to => phase_apply_batch
119
120 procedure :: apply_to_single => phase_apply_mf
121
122 procedure :: apply_phase_spiral => phase_phase_spiral
123
124 procedure :: is_allocated => phase_is_allocated
125
126 procedure :: copy_and_set_phase => phase_copy_and_set_phase
127
128 procedure :: set_boundaries => phase_set_boundaries
129 end type phase_t
130
131contains
132
133 ! ---------------------------------------------------------
135 subroutine phase_init_phases(phase, gr, kpt, kpoints, d, space)
136 class(phase_t), intent(inout) :: phase
137 class(mesh_t), intent(in) :: gr
138 type(distributed_t), intent(in) :: kpt
139 type(kpoints_t), intent(in) :: kpoints
140 type(states_elec_dim_t), intent(in) :: d
141 type(space_t), intent(in) :: space
142
143 integer :: ip, ik, sp
144 integer(int64) :: ip_inner_global
145 real(real64) :: kpoint(space%dim), x_global(space%dim)
146
147 push_sub(phase_init_phases)
148
149 ! no e^ik phase needed for Gamma-point-only periodic calculations
150 ! unless for velocity-gauge for lasers
151 if (accel_is_enabled()) then
152 phase%buff_phase_qn_start = kpt%start
153 end if
154 if(kpoints%gamma_only()) then
155 pop_sub(phase_init_phases)
156 return
157 end if
159 safe_allocate(phase%phase(1:gr%np_part, kpt%start:kpt%end))
160 safe_allocate(phase%phase_corr(gr%np+1:gr%np_part, kpt%start:kpt%end))
161 !$omp parallel private(ip)
162 do ik = kpt%start, kpt%end
163 !$omp do
164 do ip = gr%np + 1, gr%np_part
165 phase%phase_corr(ip, ik) = m_one
166 end do
167 !$omp end do nowait
168 end do
169 !$omp end parallel
171 ! Only when gr is a grid_t type, we can access gr%der
172 select type(gr)
173 class is(grid_t)
174 if (gr%der%boundaries%spiralBC) then
175 sp = gr%np
176 if (gr%parallel_in_domains) sp = gr%np + gr%pv%np_ghost
178 ! We decided to allocate the array from 1:np_part-sp as this is less error prone when passing
179 ! the array to other routines, or in particular creating a C-style pointer from phase_spiral(1,1).
180 ! We will also update phase_corr and possible other similar arrays.
181
182 safe_allocate(phase%phase_spiral(1:gr%np_part-sp, 1:2))
183
184 ! loop over boundary points
185 do ip = sp + 1, gr%np_part
186 ! get corresponding inner point
187 ip_inner_global = mesh_periodic_point(gr, space, ip)
188 x_global = mesh_x_global(gr, ip_inner_global)
189 phase%phase_spiral(ip-sp, 1) = &
190 exp(m_zi * sum((gr%x(1:space%dim, ip)-x_global(1:space%dim)) * gr%der%boundaries%spiral_q(1:space%dim)))
191 phase%phase_spiral(ip-sp, 2) = &
192 exp(-m_zi * sum((gr%x(1:space%dim, ip)-x_global(1:space%dim)) * gr%der%boundaries%spiral_q(1:space%dim)))
193 end do
195 if (accel_is_enabled()) then
196 call accel_create_buffer(phase%buff_phase_spiral, accel_mem_read_only, type_cmplx, (gr%np_part-sp)*2)
197 call accel_write_buffer(phase%buff_phase_spiral, gr%np_part-sp, 2, phase%phase_spiral)
198 end if
199 end if
200 class default
201 ! Do nothing
202 end select
203
204
205 kpoint(1:space%dim) = m_zero
206
207 sp = gr%np
208 if (gr%parallel_in_domains) sp = gr%np + gr%pv%np_ghost
209
210 !$omp parallel private(ip, ip_inner_global, x_global, kpoint)
211 do ik = kpt%start, kpt%end
212 kpoint(1:space%dim) = kpoints%get_point(d%get_kpoint_index(ik))
213 !$omp do
214 do ip = 1, gr%np_part
215 phase%phase(ip, ik) = exp(-m_zi * sum(gr%x(1:space%dim, ip) * kpoint(1:space%dim)))
216 end do
217 !$omp end do
218
219 ! loop over boundary points
220 !$omp do
221 do ip = sp + 1, gr%np_part
222 ! get corresponding inner point
223 ip_inner_global = mesh_periodic_point(gr, space, ip)
224
225 ! compute phase correction from global coordinate (opposite sign!)
226 x_global = mesh_x_global(gr, ip_inner_global)
227 phase%phase_corr(ip, ik) = phase%phase(ip, ik)* &
228 exp(m_zi * sum(x_global(1:space%dim) * kpoint(1:space%dim)))
229 end do
230 !$omp end do nowait
231 end do
232 !$omp end parallel
233
234 if (accel_is_enabled()) then
235 call accel_create_buffer(phase%buff_phase, accel_mem_read_write, type_cmplx, gr%np_part*kpt%nlocal())
236 call accel_write_buffer(phase%buff_phase, gr%np_part, kpt%nlocal(), phase%phase)
237 call accel_create_buffer(phase%buff_phase_corr, accel_mem_read_write, type_cmplx, (gr%np_part - gr%np)*kpt%nlocal())
238 call accel_write_buffer(phase%buff_phase_corr, gr%np_part - gr%np, kpt%nlocal(), phase%phase_corr)
239 end if
240
241 pop_sub(phase_init_phases)
242 end subroutine phase_init_phases
243
244 ! ----------------------------------------------------------------------------------
246 subroutine phase_update_phases(phase, mesh, kpt, kpoints, d, space, uniform_vector_potential)
247 class(phase_t), intent(inout) :: phase
248 class(mesh_t), intent(in) :: mesh
249 type(distributed_t), intent(in) :: kpt
250 type(kpoints_t), intent(in) :: kpoints
251 type(states_elec_dim_t), intent(in) :: d
252 type(space_t), intent(in) :: space
253 real(real64), allocatable, intent(in) :: uniform_vector_potential(:)
254
255 integer :: ik, ip, sp
256 integer(int64), dimension(2) :: np, gsize, bsize
257 integer(int64) :: ip_inner_global
258 real(real64) :: kpoint(space%dim)
259 real(real64), allocatable :: x_global(:,:), kpt_vec_pot(:,:)
260 type(accel_mem_t) :: buff_vec_pot, buff_x_global, buff_x
261 type(accel_kernel_t), save, target :: kernels(3)
262 type(accel_kernel_t), pointer :: kernel
263 character(len=128) :: kernel_name
264 real(real64) :: tmp_sum
265
266 if (.not. allocated(uniform_vector_potential)) return
267
268 push_sub_with_profile(phase_update_phases)
269
270
271 if (.not. allocated(phase%phase)) then
272 safe_allocate(phase%phase(1:mesh%np_part, kpt%start:kpt%end))
273 if (accel_is_enabled()) then
274 call accel_create_buffer(phase%buff_phase, accel_mem_read_write, type_cmplx, &
275 mesh%np_part*kpt%nlocal())
276 end if
277 end if
278
279 if (.not. allocated(phase%phase_corr)) then
280 safe_allocate(phase%phase_corr(mesh%np+1:mesh%np_part, kpt%start:kpt%end))
281 if (accel_is_enabled()) then
282 call accel_create_buffer(phase%buff_phase_corr, accel_mem_read_write, type_cmplx, &
283 (mesh%np_part - mesh%np)*kpt%nlocal())
284 end if
285 end if
286
287 ! TODO: We should not recompute this every time-step. We should store it.
288
289 ! loop over boundary points
290 sp = mesh%np
291 ! skip ghost points
292 if (mesh%parallel_in_domains) sp = mesh%np + mesh%pv%np_ghost
293
294 safe_allocate(x_global(1:space%dim,(sp + 1):mesh%np_part))
295
296 !$omp parallel do schedule(static) private(ip_inner_global)
297 do ip = sp + 1, mesh%np_part
298 ! get corresponding inner point
299 ip_inner_global = mesh_periodic_point(mesh, space, ip)
300 ! compute the difference between the global coordinate and the local coordinate
301 x_global(:,ip) = mesh_x_global(mesh, ip_inner_global) - mesh%x(1:space%dim, ip)
302 end do
303
304
305 if (.not. accel_is_enabled()) then
306 ! Factorize exp(-i x.(k+A)) = exp(-i x.k) * exp(-i x.A(t)). The k-only factor is static and
307 ! computed once; the A(t) factor is the same for all k-points, so only ~np_part sincos are
308 ! needed per step instead of np_part*n_kpoints.
309 if (.not. allocated(phase%phase_k)) then
310 safe_allocate(phase%phase_k(1:mesh%np_part, kpt%start:kpt%end))
311 safe_allocate(phase%phase_corr_k(sp + 1:mesh%np_part, kpt%start:kpt%end))
312 safe_allocate(phase%vp_phase(1:mesh%np_part))
313 safe_allocate(phase%vp_phase_corr(sp + 1:mesh%np_part))
314 !$omp parallel private(ik, ip, kpoint, tmp_sum)
315 do ik = kpt%start, kpt%end
316 kpoint(1:space%dim) = kpoints%get_point(d%get_kpoint_index(ik))
317 !$omp do schedule(static)
318 do ip = 1, mesh%np_part
319 tmp_sum = sum(mesh%x(1:space%dim, ip)*kpoint(1:space%dim))
320 phase%phase_k(ip, ik) = cmplx(cos(tmp_sum), -sin(tmp_sum), real64)
321 end do
322 !$omp end do nowait
323 !$omp do schedule(static)
324 do ip = sp + 1, mesh%np_part
325 tmp_sum = sum(x_global(1:space%dim, ip)*kpoint(1:space%dim))
326 phase%phase_corr_k(ip, ik) = cmplx(cos(tmp_sum), sin(tmp_sum), real64)
327 end do
328 !$omp end do nowait
329 end do
330 !$omp end parallel
331 end if
332
333 !$omp parallel private(ik, ip, tmp_sum)
334 ! k-independent time-dependent factor exp(-i x.A(t)) (recomputed each step)
335 !$omp do schedule(static)
336 do ip = 1, mesh%np_part
337 tmp_sum = sum(mesh%x(1:space%dim, ip)*uniform_vector_potential(1:space%dim))
338 phase%vp_phase(ip) = cmplx(cos(tmp_sum), -sin(tmp_sum), real64)
339 end do
340 !$omp end do
341 !$omp do schedule(static)
342 do ip = sp + 1, mesh%np_part
343 tmp_sum = sum(x_global(1:space%dim, ip)*uniform_vector_potential(1:space%dim))
344 phase%vp_phase_corr(ip) = cmplx(cos(tmp_sum), sin(tmp_sum), real64)
345 end do
346 !$omp end do
347
348 ! combine static and time-dependent factors (contiguous in ip)
349 do ik = kpt%start, kpt%end
350 !$omp do schedule(static)
351 do ip = 1, mesh%np_part
352 phase%phase(ip, ik) = phase%phase_k(ip, ik) * phase%vp_phase(ip)
353 end do
354 !$omp end do nowait
355 !$omp do schedule(static)
356 do ip = sp + 1, mesh%np_part
357 phase%phase_corr(ip, ik) = phase%phase_corr_k(ip, ik) * phase%vp_phase_corr(ip)
358 end do
359 !$omp end do nowait
360 end do
361 !$omp end parallel
362
363 else !accel_is enabled
364
365 call accel_create_buffer(buff_vec_pot, accel_mem_read_only, type_float, space%dim*kpt%nlocal())
366 safe_allocate(kpt_vec_pot(1:space%dim,kpt%start:kpt%end))
367 do ik = kpt%start, kpt%end
368 kpoint(1:space%dim) = kpoints%get_point(d%get_kpoint_index(ik))
369 kpt_vec_pot(1:space%dim, ik) = kpoint(1:space%dim) + uniform_vector_potential(1:space%dim)
370 end do
371 call accel_write_buffer(buff_vec_pot, space%dim, kpt%nlocal(), kpt_vec_pot, async=.true.)
372
373 ! Note: this should be globally stored
374 call accel_create_buffer(buff_x, accel_mem_read_only, type_float, space%dim*(mesh%np_part))
375 call accel_write_buffer(buff_x, space%dim, mesh%np_part, mesh%x, async=.true.)
376
377 call accel_create_buffer(buff_x_global, accel_mem_read_only, type_float, space%dim*(mesh%np_part-sp))
378 call accel_write_buffer(buff_x_global, space%dim, mesh%np_part-sp, x_global(1:space%dim,(sp + 1):mesh%np_part), async=.true.)
379
380 write(kernel_name, '(a,i1,a)') 'update_phases<double, Dim::D', space%dim, '>'
381 kernel => kernels(space%dim)
382 call accel_kernel_start_call(kernel, 'phase.cu', trim(kernel_name))
383
384 call accel_set_kernel_arg(kernel, 0, mesh%np)
385 call accel_set_kernel_arg(kernel, 1, mesh%np_part)
386 call accel_set_kernel_arg(kernel, 2, kpt%start)
387 call accel_set_kernel_arg(kernel, 3, kpt%end)
388 call accel_set_kernel_arg(kernel, 4, sp)
389 call accel_set_kernel_arg(kernel, 5, buff_vec_pot)
390 call accel_set_kernel_arg(kernel, 6, buff_x)
391 call accel_set_kernel_arg(kernel, 7, buff_x_global)
392 call accel_set_kernel_arg(kernel, 8, phase%buff_phase)
393 call accel_set_kernel_arg(kernel, 9, phase%buff_phase_corr)
394
395 ! Compute the grid size
396 np = (/mesh%np_part, kpt%nlocal()/)
397 bsize = (/accel_kernel_block_size(kernel), 1/)
398 call accel_grid_size(np, bsize, gsize)
399
400 call accel_kernel_run(kernel, gsize, bsize)
401
402 call accel_read_buffer(phase%buff_phase, mesh%np_part, kpt%nlocal(), phase%phase, async=.true.)
403 call accel_read_buffer(phase%buff_phase_corr, mesh%np_part - mesh%np, kpt%nlocal(), phase%phase_corr)
404
405 call accel_free_buffer(buff_vec_pot)
406 call accel_free_buffer(buff_x)
407 call accel_free_buffer(buff_x_global)
408 safe_deallocate_a(kpt_vec_pot)
409 end if
410
411 safe_deallocate_a(x_global)
412
413 pop_sub_with_profile(phase_update_phases)
414 end subroutine phase_update_phases
415
416 ! ----------------------------------------------------------------------------------
418 subroutine phase_end(phase)
419 class(phase_t), intent(inout) :: phase
420
421 push_sub(phase_end)
422
423 if (phase%is_allocated() .and. accel_is_enabled()) then
424 call accel_free_buffer(phase%buff_phase)
425 call accel_free_buffer(phase%buff_phase_corr)
426 end if
427
428 if (allocated(phase%phase_spiral) .and. accel_is_enabled()) then
429 call accel_free_buffer(phase%buff_phase_spiral)
430 end if
431
432 safe_deallocate_a(phase%phase)
433 safe_deallocate_a(phase%phase_corr)
434 safe_deallocate_a(phase%phase_k)
435 safe_deallocate_a(phase%phase_corr_k)
436 safe_deallocate_a(phase%vp_phase)
437 safe_deallocate_a(phase%vp_phase_corr)
438 safe_deallocate_a(phase%phase_spiral)
439
440 pop_sub(phase_end)
441 end subroutine phase_end
442
443 ! ----------------------------------------------------------------------------------
445 subroutine phase_accel_rebuild(phase, mesh, kpt)
446 class(phase_t), intent(inout) :: phase
447 class(mesh_t), intent(in) :: mesh
448 type(distributed_t), intent(in) :: kpt
449
450 integer :: nlocal
451
452 push_sub(phase_accel_rebuild)
453
454 if (.not. accel_is_enabled()) then
455 pop_sub(phase_accel_rebuild)
456 return
457 end if
458
459 phase%buff_phase_qn_start = kpt%start
460
461 call accel_detach_buffer(phase%buff_phase)
462 call accel_detach_buffer(phase%buff_phase_corr)
463 call accel_detach_buffer(phase%buff_phase_spiral)
464
465 if (allocated(phase%phase)) then
466 assert(size(phase%phase, 1) == mesh%np_part)
467 nlocal = ubound(phase%phase, dim=2) - lbound(phase%phase, dim=2) + 1
468 call accel_create_buffer(phase%buff_phase, accel_mem_read_write, type_cmplx, size(phase%phase, 1)*nlocal)
469 call accel_write_buffer(phase%buff_phase, size(phase%phase, 1), nlocal, phase%phase)
470 end if
471
472 if (allocated(phase%phase_corr)) then
473 assert(size(phase%phase_corr, 1) == mesh%np_part - mesh%np)
474 nlocal = ubound(phase%phase_corr, dim=2) - lbound(phase%phase_corr, dim=2) + 1
475 call accel_create_buffer(phase%buff_phase_corr, accel_mem_read_write, type_cmplx, &
476 size(phase%phase_corr, 1)*nlocal)
477 call accel_write_buffer(phase%buff_phase_corr, size(phase%phase_corr, 1), nlocal, phase%phase_corr)
478 end if
479
480 if (allocated(phase%phase_spiral)) then
481 call accel_create_buffer(phase%buff_phase_spiral, accel_mem_read_only, type_cmplx, &
482 size(phase%phase_spiral, 1)*size(phase%phase_spiral, 2))
483 call accel_write_buffer(phase%buff_phase_spiral, size(phase%phase_spiral, 1), &
484 size(phase%phase_spiral, 2), phase%phase_spiral)
485 end if
486
487 pop_sub(phase_accel_rebuild)
488 end subroutine phase_accel_rebuild
489
490 ! ----------------------------------------------------------------------------------
492 !
493 subroutine phase_set_phase_corr(phase, mesh, psib, async)
494 class(phase_t), intent(in) :: phase
495 class(mesh_t), intent(in) :: mesh
496 type(wfs_elec_t), intent(inout) :: psib
497 logical, optional, intent(in) :: async
498
499
500 logical :: phase_correction
501
502 push_sub(phase_set_phase_corr)
503
504 ! check if we only want a phase correction for the boundary points
505 phase_correction = phase%is_allocated()
506
507 !We apply the phase only to np points, and the phase for the np+1 to np_part points
508 !will be treated as a phase correction in the Hamiltonian
509 if (phase_correction) then
510 call phase%apply_to(mesh, mesh%np, .false., psib, async=async)
511 end if
512
513 pop_sub(phase_set_phase_corr)
514 end subroutine phase_set_phase_corr
515
516 ! ----------------------------------------------------------------------------------
518 !
519 subroutine phase_unset_phase_corr(phase, mesh, psib, async)
520 class(phase_t), intent(in) :: phase
521 class(mesh_t), intent(in) :: mesh
522 type(wfs_elec_t), intent(inout) :: psib
523 logical, optional, intent(in) :: async
524
525 logical :: phase_correction
526
527 push_sub(phase_unset_phase_corr)
528
529 ! check if we only want a phase correction for the boundary points
530 phase_correction = phase%is_allocated()
531
532 !We apply the phase only to np points, and the phase for the np+1 to np_part points
533 !will be treated as a phase correction in the Hamiltonian
534 if (phase_correction) then
535 call phase%apply_to(mesh, mesh%np, .true., psib, async=async)
536 end if
537
539 end subroutine phase_unset_phase_corr
540
541 ! ---------------------------------------------------------------------------------------
543 !
544 subroutine phase_apply_batch(this, mesh, np, conjugate, psib, src, async)
545 class(phase_t), intent(in) :: this
546 class(mesh_t), intent(in) :: mesh
547 integer, intent(in) :: np
548 logical, intent(in) :: conjugate
549 type(wfs_elec_t), target, intent(inout) :: psib
550 type(wfs_elec_t), optional, target, intent(in) :: src
551 logical, optional, intent(in) :: async
552
553 integer :: ip, ii, sp
554 type(wfs_elec_t), pointer :: src_
555 complex(real64) :: phase
556 integer(int64), dimension(3) :: gsizes, bsizes
557 type(accel_kernel_t), save :: ker_phase
558
559 push_sub(phase_apply_batch)
560 call profiling_in("PHASE_APPLY_BATCH")
561
562 call profiling_count_operations(6*np*psib%nst_linear)
563
564 assert(np <= mesh%np_part)
565 assert(psib%type() == type_cmplx)
566 assert(psib%ik >= lbound(this%phase, dim=2))
567 assert(psib%ik <= ubound(this%phase, dim=2))
568
569 src_ => psib
570 if (present(src)) src_ => src
571
572 assert(src_%has_phase .eqv. conjugate)
573 assert(src_%ik == psib%ik)
574 assert(src_%type() == type_cmplx)
575
576 ! We want to skip the ghost points for setting the phase
577 sp = min(np, mesh%np)
578 if (np > mesh%np .and. mesh%parallel_in_domains) sp = mesh%np + mesh%pv%np_ghost
579
580 select case (psib%status())
581 case (batch_packed)
582
583 if (conjugate) then
584
585 !$omp parallel private(ii, phase)
586 !$omp do
587 do ip = 1, min(mesh%np, np)
588 phase = conjg(this%phase(ip, psib%ik))
589 !$omp simd
590 do ii = 1, psib%nst_linear
591 psib%zff_pack(ii, ip) = phase*src_%zff_pack(ii, ip)
592 end do
593 end do
594 !$omp end do nowait
595
596 ! Boundary points, if requested
597 !$omp do
598 do ip = sp+1, np
599 phase = conjg(this%phase(ip, psib%ik))
600 !$omp simd
601 do ii = 1, psib%nst_linear
602 psib%zff_pack(ii, ip) = phase*src_%zff_pack(ii, ip)
603 end do
604 end do
605 !$omp end parallel
606
607 else
608
609 !$omp parallel private(ii, phase)
610 !$omp do
611 do ip = 1, min(mesh%np, np)
612 phase = this%phase(ip, psib%ik)
613 !$omp simd
614 do ii = 1, psib%nst_linear
615 psib%zff_pack(ii, ip) = phase*src_%zff_pack(ii, ip)
616 end do
617 end do
618 !$omp end do nowait
619
620 ! Boundary points, if requested
621 !$omp do
622 do ip = sp+1, np
623 phase = this%phase(ip, psib%ik)
624 !$omp simd
625 do ii = 1, psib%nst_linear
626 psib%zff_pack(ii, ip) = phase*src_%zff_pack(ii, ip)
627 end do
628 end do
629 !$omp end parallel
630
631 end if
632
633 case (batch_not_packed)
634
635 if (conjugate) then
636
637 !$omp parallel private(ii, ip)
638 do ii = 1, psib%nst_linear
639 !$omp do simd
640 do ip = 1, min(mesh%np, np)
641 psib%zff_linear(ip, ii) = conjg(this%phase(ip, psib%ik))*src_%zff_linear(ip, ii)
642 end do
643 !$omp end do simd nowait
644
645 ! Boundary points, if requested
646 !$omp do simd
647 do ip = sp+1, np
648 psib%zff_linear(ip, ii) = conjg(this%phase(ip, psib%ik))*src_%zff_linear(ip, ii)
649 end do
650 !$omp end do simd nowait
651 end do
652 !$omp end parallel
653
654 else
655 !$omp parallel private(ii, ip)
656 do ii = 1, psib%nst_linear
657 !$omp do simd
658 do ip = 1, min(mesh%np, np)
659 psib%zff_linear(ip, ii) = this%phase(ip, psib%ik)*src_%zff_linear(ip, ii)
660 end do
661 !$omp end do simd nowait
662
663 ! Boundary points, if requested
664 !$omp do simd
665 do ip = sp+1, np
666 psib%zff_linear(ip, ii) = this%phase(ip, psib%ik)*src_%zff_linear(ip, ii)
667 end do
668 !$omp end do simd nowait
669
670 end do
671 !$omp end parallel
672
673 end if
674
676 call accel_kernel_start_call(ker_phase, 'phase.cu', 'phase_hamiltonian<double>')
677
678 if (conjugate) then
679 call accel_set_kernel_arg(ker_phase, 0, 1_4)
680 else
681 call accel_set_kernel_arg(ker_phase, 0, 0_4)
682 end if
683
684 call accel_set_kernel_arg(ker_phase, 1, (psib%ik - this%buff_phase_qn_start)*mesh%np_part)
685 call accel_set_kernel_arg(ker_phase, 2, np)
686 call accel_set_kernel_arg(ker_phase, 3, this%buff_phase)
687 call accel_set_kernel_arg(ker_phase, 4, src_%ff_device)
688 call accel_set_kernel_arg(ker_phase, 5, log2(int(src_%pack_size(1), int32)))
689 call accel_set_kernel_arg(ker_phase, 6, psib%ff_device)
690 call accel_set_kernel_arg(ker_phase, 7, log2(int(psib%pack_size(1), int32)))
691
692 ! Compute the grid (extend to another dimensions if the size of the problem is too big)
693 call accel_grid_size_extend_dim(int(np, int64), psib%pack_size(1), gsizes, bsizes, ker_phase)
694
695 call accel_kernel_run(ker_phase, gsizes, bsizes)
696
697 if(.not. optional_default(async, .false.)) call accel_finish()
698 end select
699
700 psib%has_phase = .not. conjugate
701
702 call profiling_out("PHASE_APPLY_BATCH")
703 pop_sub(phase_apply_batch)
704 end subroutine phase_apply_batch
705
711 !
712 subroutine phase_apply_mf(this, psi, np, dim, ik, conjugate)
713 class(phase_t), intent(in) :: this
714 complex(real64), intent(inout) :: psi(:, :)
715 integer, intent(in) :: np
716 integer, intent(in) :: dim
717 integer, intent(in) :: ik
718 logical, intent(in) :: conjugate
719
720 integer :: idim, ip
721
722 push_sub(phase_apply_mf)
723
724 assert(ik >= lbound(this%phase, dim=2))
725 assert(ik <= ubound(this%phase, dim=2))
726
727 call profiling_in("PHASE_APPLY_SINGLE")
728
729 if (conjugate) then
730 ! Apply the phase that contains both the k-point and vector-potential terms.
731 do idim = 1, dim
732 !$omp parallel do
733 do ip = 1, np
734 psi(ip, idim) = conjg(this%phase(ip, ik))*psi(ip, idim)
735 end do
736 !$omp end parallel do
737 end do
738 else
739 ! Apply the conjugate of (i.e. remove) the phase that contains both the k-point and vector-potential terms.
740 do idim = 1, dim
741 !$omp parallel do
742 do ip = 1, np
743 psi(ip, idim) = this%phase(ip, ik)*psi(ip, idim)
744 end do
745 !$omp end parallel do
746 end do
747 end if
748
749 call profiling_out("PHASE_APPLY_SINGLE")
750
751 pop_sub(phase_apply_mf)
752 end subroutine phase_apply_mf
753
754
755 ! ---------------------------------------------------------------------------------------
762 !
763 subroutine phase_build_vector(mesh, np, kvector, conjugate, phase)
764 class(mesh_t), intent(in) :: mesh
765 integer, intent(in) :: np
766 real(real64), contiguous, intent(in) :: kvector(:)
767 logical, intent(in) :: conjugate
768 complex(real64), contiguous, intent(out) :: phase(:)
769
770 integer :: ip
771 complex(real64) :: factor
772
773 push_sub_with_profile(phase_build_vector)
774
775 assert(size(phase) >= np)
776
777 factor = merge(m_zi, -m_zi, conjugate)
778
779 !$omp parallel do
780 do ip = 1, np
781 phase(ip) = exp(factor*dot_product(mesh%x(1:size(kvector), ip), kvector))
782 end do
783 !$omp end parallel do
785 pop_sub_with_profile(phase_build_vector)
786 end subroutine phase_build_vector
787
788
789 ! ---------------------------------------------------------------------------------------
791 !
792 subroutine phase_phase_spiral(this, der, psib)
793 class(phase_t), intent(in) :: this
794 type(derivatives_t), intent(in) :: der
795 class(wfs_elec_t), intent(inout) :: psib
796
797 integer :: ip, ii, sp
798 integer, allocatable :: spin_label(:)
799 type(accel_mem_t) :: spin_label_buffer
800 integer(int64) :: bsize
801 integer(int64), dimension(2) :: np, gsizes, bsizes
802
803 push_sub(phase_phase_spiral)
804 call profiling_in("PBC_PHASE_SPIRAL")
805
806 call profiling_count_operations(6*(der%mesh%np_part-der%mesh%np)*psib%nst_linear)
807
808 assert(der%boundaries%spiral)
809 assert(psib%type() == type_cmplx)
810
811 sp = der%mesh%np
812 if (der%mesh%parallel_in_domains) sp = der%mesh%np + der%mesh%pv%np_ghost
813
814
815 select case (psib%status())
816 case (batch_packed)
817
818 !$omp parallel do private(ip, ii)
819 do ip = sp + 1, der%mesh%np_part
820 do ii = 1, psib%nst_linear, 2
821 if (this%spin(3,psib%linear_to_ist(ii), psib%ik)>0) then
822 psib%zff_pack(ii+1, ip) = psib%zff_pack(ii+1, ip)*this%phase_spiral(ip-sp, 1)
823 else
824 psib%zff_pack(ii, ip) = psib%zff_pack(ii, ip)*this%phase_spiral(ip-sp, 2)
825 end if
826 end do
827 end do
828 !$omp end parallel do
829
830 case (batch_not_packed)
831
832 !$omp parallel private(ii, ip)
833 do ii = 1, psib%nst_linear, 2
834 if (this%spin(3,psib%linear_to_ist(ii), psib%ik)>0) then
835 !$omp do
836 do ip = sp + 1, der%mesh%np_part
837 psib%zff_linear(ip, ii+1) = psib%zff_linear(ip, ii+1)*this%phase_spiral(ip-sp, 1)
838 end do
839 !$omp end do nowait
840 else
841 !$omp do
842 do ip = sp + 1, der%mesh%np_part
843 psib%zff_linear(ip, ii) = psib%zff_linear(ip, ii)*this%phase_spiral(ip-sp, 2)
844 end do
845 !$omp end do nowait
846 end if
847 end do
848 !$omp end parallel
849
851
852 assert(accel_is_enabled())
853
854 ! generate array of offsets for access of psib and phase_spiral:
855 ! TODO: Move this to the routine where spin(:,:,:) is generated
856 ! and also move the buffer to the GPU at this point to
857 ! avoid unecessary latency here!
858
859 safe_allocate(spin_label(1:psib%nst_linear))
860 spin_label = 0
861 do ii = 1, psib%nst_linear, 2
862 if (this%spin(3, psib%linear_to_ist(ii), psib%ik) > 0) spin_label(ii)=1
863 end do
865 call accel_create_buffer(spin_label_buffer, accel_mem_read_only, type_integer, psib%nst_linear)
866 call accel_write_buffer(spin_label_buffer, psib%nst_linear, spin_label)
867
868 call accel_kernel_start_call(kernel_phase_spiral, 'phase_spiral.cu', 'phase_spiral_apply<double>')
869
872 call accel_set_kernel_arg(kernel_phase_spiral, 2, der%mesh%np_part)
873 call accel_set_kernel_arg(kernel_phase_spiral, 3, psib%ff_device)
874 call accel_set_kernel_arg(kernel_phase_spiral, 4, log2(psib%pack_size(1)))
875 call accel_set_kernel_arg(kernel_phase_spiral, 5, this%buff_phase_spiral)
876 call accel_set_kernel_arg(kernel_phase_spiral, 6, spin_label_buffer)
877
878 ! Compute the grid size
879 bsize = accel_kernel_block_size(kernel_phase_spiral)/psib%pack_size(1)
880 np = (/psib%pack_size(1)/2_int64, int(der%mesh%np_part - sp, int64)/)
881 bsizes = (/psib%pack_size(1)/2, 2*bsize/)
882 call accel_grid_size(np, bsizes, gsizes)
883
884 call accel_kernel_run(kernel_phase_spiral, bsizes, gsizes)
885
886 call accel_finish()
887
888 call accel_free_buffer(spin_label_buffer)
889
890 safe_deallocate_a(spin_label)
891
892 end select
893
894 call profiling_out("PBC_PHASE_SPIRAL")
895 pop_sub(phase_phase_spiral)
896 end subroutine phase_phase_spiral
897
898
899 ! ---------------------------------------------------------------------------------------
900 logical pure function phase_is_allocated(this)
901 class(phase_t), intent(in) :: this
902
903 phase_is_allocated = allocated(this%phase)
904 end function phase_is_allocated
905
906 !----------------------------------------------------------
913 subroutine phase_copy_and_set_phase(phase, gr, kpt, psib, psib_with_phase)
914 class(phase_t), intent(in) :: phase
915 type(grid_t), intent(in) :: gr
916 type(distributed_t), intent(in) :: kpt
917 type(wfs_elec_t), intent(in) :: psib
918 type(wfs_elec_t), intent(out) :: psib_with_phase
919
920 integer :: k_offset, n_boundary_points
921
923
924 call psib%copy_to(psib_with_phase)
925 if (phase%is_allocated()) then
926 call phase%apply_to(gr, gr%np, conjugate = .false., psib = psib_with_phase, src = psib, async=.true.)
927 ! apply phase correction while setting boundary -> memory needs to be
928 ! accessed only once
929 k_offset = psib%ik - kpt%start
930 n_boundary_points = int(gr%np_part - gr%np)
931 call boundaries_set(gr%der%boundaries, gr, psib_with_phase, phase_correction = phase%phase_corr(:, psib%ik), &
932 buff_phase_corr = phase%buff_phase_corr, offset=k_offset*n_boundary_points, async=.true.)
933 else
934 call psib%copy_data_to(gr%np, psib_with_phase)
935 call boundaries_set(gr%der%boundaries, gr, psib_with_phase)
936 end if
937
938 call psib_with_phase%do_pack(copy = .true.)
939
941 end subroutine phase_copy_and_set_phase
942
943 ! ----------------------------------------------------------------------------------
950 !
951 subroutine phase_set_boundaries(this, mesh, boundaries, ffb)
952 class(phase_t), intent(in) :: this
953 class(mesh_t), intent(in) :: mesh
954 type(boundaries_t), intent(in) :: boundaries
955 type(wfs_elec_t), intent(inout) :: ffb
956
957 integer :: k_offset, n_boundary_points
958
959 push_sub(phase_set_boundaries)
960
961 if (this%is_allocated()) then
962 assert(ffb%has_phase)
963 k_offset = ffb%ik - lbound(this%phase_corr, dim=2)
964 n_boundary_points = mesh%np_part - mesh%np
965 call boundaries_set(boundaries, mesh, ffb, phase_correction = this%phase_corr(:, ffb%ik), &
966 buff_phase_corr = this%buff_phase_corr, offset = k_offset*n_boundary_points)
967 else
968 assert(.not. ffb%has_phase)
969 call boundaries_set(boundaries, mesh, ffb)
970 end if
971
973 end subroutine phase_set_boundaries
974
975
976end module phase_oct_m
977
978!! Local Variables:
979!! mode: f90
980!! coding: utf-8
981!! End:
double exp(double __x) __attribute__((__nothrow__
double sin(double __x) __attribute__((__nothrow__
double cos(double __x) __attribute__((__nothrow__
integer function, public accel_kernel_block_size(kernel)
Definition: accel.F90:1194
subroutine, public accel_free_buffer(this, async)
Definition: accel.F90:967
subroutine, public accel_kernel_start_call(this, file_name, kernel_name, flags)
Definition: accel.F90:1842
subroutine, public accel_finish()
Definition: accel.F90:1104
subroutine, public accel_detach_buffer(this)
Clear a buffer handle without freeing device memory.
Definition: accel.F90:1036
integer, parameter, public accel_mem_read_write
Definition: accel.F90:162
type(accel_kernel_t), target, save, public kernel_phase_spiral
Definition: accel.F90:253
pure logical function, public accel_is_enabled()
Definition: accel.F90:391
integer, parameter, public accel_mem_read_only
Definition: accel.F90:162
This module implements batches of mesh functions.
Definition: batch.F90:112
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
integer, parameter, public batch_packed
functions are stored in CPU memory, in transposed (packed) order
Definition: batch.F90:293
This module implements common operations on batches of mesh functions.
Definition: batch_ops.F90:95
Module implementing boundary conditions in Octopus.
Definition: boundaries.F90:101
This module calculates the derivatives (gradients, Laplacians, etc.) of a function.
real(real64), parameter, public m_zero
Definition: global.F90:173
complex(real64), parameter, public m_zi
Definition: global.F90:187
real(real64), parameter, public m_one
Definition: global.F90:174
This module implements the underlying real-space grid.
Definition: grid.F90:96
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:94
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:97
integer(int64) function, public mesh_periodic_point(mesh, space, ip)
This function returns the point inside the grid corresponding to a boundary point when PBCs are used....
Definition: mesh.F90:710
real(real64) function, dimension(1:mesh%box%dim), public mesh_x_global(mesh, ipg)
Given a global point index, this function returns the coordinates of the point.
Definition: mesh.F90:803
subroutine phase_phase_spiral(this, der, psib)
apply spiral phase
Definition: phase.F90:865
subroutine phase_unset_phase_corr(phase, mesh, psib, async)
unset the phase correction (if necessary)
Definition: phase.F90:592
subroutine, public phase_accel_rebuild(phase, mesh, kpt)
Rebuild phase accelerator buffers after an intrinsic copy.
Definition: phase.F90:518
subroutine phase_copy_and_set_phase(phase, gr, kpt, psib, psib_with_phase)
Copy a batch to another batch and apply the Bloch phase to it.
Definition: phase.F90:986
subroutine phase_init_phases(phase, gr, kpt, kpoints, d, space)
Initiliaze the phase arrays and copy to GPU the data.
Definition: phase.F90:208
subroutine phase_end(phase)
Releases the memory of the phase object.
Definition: phase.F90:491
subroutine phase_update_phases(phase, mesh, kpt, kpoints, d, space, uniform_vector_potential)
Update the phases.
Definition: phase.F90:319
subroutine, public phase_build_vector(mesh, np, kvector, conjugate, phase)
tabulate an explicitly supplied reciprocal-vector phase.
Definition: phase.F90:836
logical pure function phase_is_allocated(this)
Definition: phase.F90:973
subroutine phase_apply_batch(this, mesh, np, conjugate, psib, src, async)
apply (remove) the phase to the wave functions before (after) applying the Hamiltonian
Definition: phase.F90:617
subroutine phase_set_phase_corr(phase, mesh, psib, async)
set the phase correction (if necessary)
Definition: phase.F90:566
subroutine phase_apply_mf(this, psi, np, dim, ik, conjugate)
apply (or remove) the phase to a wave function psi
Definition: phase.F90:785
subroutine phase_set_boundaries(this, mesh, boundaries, ffb)
Set the boundary points of a (possibly Bloch-phased) batch.
Definition: phase.F90:1024
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
Definition: profiling.F90:122
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:672
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:604
This module handles spin dimensions of the states and the k-point distribution.
type(type_t), parameter, public type_cmplx
Definition: types.F90:114
type(type_t), parameter, public type_integer
Definition: types.F90:115
type(type_t), parameter, public type_float
Definition: types.F90:113
class representing derivatives
Distribution of N instances over mpi_grpsize processes, for the local rank mpi_grprank....
Description of the grid, containing information on derivatives, stencil, and symmetries.
Definition: grid.F90:148
Describes mesh distribution to nodes.
Definition: mesh.F90:165
A container for the phase.
Definition: phase.F90:158
class for organizing spins and k-points
batches of electronic states
Definition: wfs_elec.F90:120
int true(void)