71 use num_types,
only: rp, dp, sp
72 use json_module,
only: json_file
73 use json_utils,
only: json_get_or_default
74 use vector,
only: vector_t
75 use matrix,
only: matrix_t
76 use comm,
only: pe_rank, neko_comm, pe_size, mpi_real_precision
77 use utils,
only: neko_error, filename_suffix
78 use neko_config,
only: neko_bcknd_device, neko_bcknd_cuda, neko_bcknd_hip, &
80 use device,
only: device_memcpy, host_to_device, device_to_host
81 use,
intrinsic :: iso_c_binding, only: c_ptr
82 use logger,
only: neko_log
83 use mpi_f08,
only: mpi_sum, mpi_allreduce, mpi_integer
84 use scratch_registry,
only: scratch_registry_t
92 integer :: n, m, n_global, max_iter
93 real(kind=rp) :: a0, asyinit, asyincr, asydecr, epsimin, &
95 real(kind=rp) :: move_limit = 0.2_rp
96 type(vector_t) :: xold1, xold2, low, upp, alpha, beta, a, c, d, xmax, xmin
97 logical :: is_initialized = .false.
98 logical :: is_updated = .false.
99 type(scratch_registry_t) :: scratch
100 character(len=:),
allocatable :: subsolver, bcknd
103 type(vector_t) :: p0j, q0j
104 type(matrix_t) :: pij, qij
108 real(kind=rp) :: z, zeta
109 type(vector_t) :: y, lambda, s, mu
110 type(vector_t) :: xsi, eta
113 generic,
public :: init => init_from_json, init_from_components
114 procedure,
public, pass(this) :: init_from_json => mma_init_from_json
115 procedure,
public, pass(this) :: init_from_components => &
116 mma_init_from_components
117 procedure,
public, pass(this) :: free => mma_free
118 procedure,
public, pass(this) :: get_n => mma_get_n
119 procedure,
public, pass(this) :: get_m => mma_get_m
120 procedure,
public, pass(this) :: get_residumax => mma_get_residumax
121 procedure,
public, pass(this) :: get_residunorm => mma_get_residunorm
122 procedure,
public, pass(this) :: get_max_iter => mma_get_max_iter
123 procedure,
public, pass(this) :: get_backend_and_subsolver => &
124 mma_get_backend_and_subsolver
126 generic,
public :: update => update_vector, update_cpu, update_device
127 procedure, pass(this) :: update_vector => mma_update_vector
128 procedure, pass(this) :: update_cpu => mma_update_cpu
129 procedure, pass(this) :: update_device => mma_update_device
131 generic,
public :: kkt => kkt_vector, kkt_cpu, kkt_device
132 procedure, pass(this) :: kkt_vector => mma_kkt_vector
133 procedure, pass(this) :: kkt_cpu => mma_kkt_cpu
134 procedure, pass(this) :: kkt_device => mma_kkt_device
136 procedure, pass(this) :: save_checkpoint => mma_save_checkpoint
137 procedure, pass(this) :: load_checkpoint => mma_load_checkpoint
140 procedure, pass(this) :: copy_from => mma_copy_from
147 real(kind=rp),
parameter :: a0_default = 1.0_rp
148 real(kind=rp),
parameter :: a_default = 0.0_rp
149 real(kind=rp),
parameter :: c_default = 1000.0_rp
150 real(kind=rp),
parameter :: d_default = 0.0_rp
151 real(kind=rp),
parameter :: xmin_default = 0.0_rp
152 real(kind=rp),
parameter :: xmax_default = 1.0_rp
154 real(kind=rp),
parameter :: asyinit_default = 0.2_rp
155 real(kind=rp),
parameter :: asyincr_default = 1.05_rp
156 real(kind=rp),
parameter :: asydecr_default = 0.65_rp
157 real(kind=rp),
parameter :: move_limit_default = 0.2_rp
159 integer,
parameter :: max_iter_default = 100
160 character(len=*),
parameter :: subsolver_default =
"dip"
161 real(kind=rp),
parameter :: scale_default = 1.0_rp
162 logical,
parameter :: auto_scale_default = .false.
169 module subroutine mma_update_cpu(this, iter, x, df0dx, fval, dfdx)
170 class(mma_t),
intent(inout) :: this
171 integer,
intent(in) :: iter
172 real(kind=rp),
dimension(this%n),
intent(inout) :: x
173 real(kind=rp),
dimension(this%n),
intent(in) :: df0dx
174 real(kind=rp),
dimension(this%m),
intent(in) :: fval
175 real(kind=rp),
dimension(this%m, this%n),
intent(in) :: dfdx
176 end subroutine mma_update_cpu
179 module subroutine mma_kkt_cpu(this, x, df0dx, fval, dfdx)
180 class(mma_t),
intent(inout) :: this
181 real(kind=rp),
dimension(this%n),
intent(in) :: x
182 real(kind=rp),
dimension(this%n),
intent(in) :: df0dx
183 real(kind=rp),
dimension(this%m),
intent(in) :: fval
184 real(kind=rp),
dimension(this%m, this%n),
intent(in) :: dfdx
185 end subroutine mma_kkt_cpu
191 module subroutine mma_update_device(this, iter, x, df0dx, fval, dfdx)
192 class(mma_t),
intent(inout) :: this
193 integer,
intent(in) :: iter
194 type(c_ptr),
intent(inout) :: x
195 type(c_ptr),
intent(in) :: df0dx, fval, dfdx
196 end subroutine mma_update_device
199 module subroutine mma_kkt_device(this, x, df0dx, fval, dfdx)
200 class(mma_t),
intent(inout) :: this
201 type(c_ptr),
intent(in) :: x, df0dx, fval, dfdx
202 end subroutine mma_kkt_device
210 module subroutine mma_save_checkpoint_hdf5(object, filename, overwrite)
211 class(mma_t),
intent(inout) :: object
212 character(len=*),
intent(in) :: filename
213 logical,
intent(in),
optional :: overwrite
214 end subroutine mma_save_checkpoint_hdf5
216 module subroutine mma_load_checkpoint_hdf5(object, filename)
217 class(mma_t),
intent(inout) :: object
218 character(len=*),
intent(in) :: filename
219 end subroutine mma_load_checkpoint_hdf5
228 subroutine mma_init_from_json(this, x, n, m, json, scale, auto_scale)
240 class(mma_t),
intent(inout) :: this
241 integer,
intent(in) :: n, m
242 type(vector_t),
intent(in) :: x
244 type(json_file),
intent(inout) :: json
247 real(kind=rp),
intent(out) :: scale
248 logical,
intent(out) :: auto_scale
256 real(kind=rp),
dimension(n) :: xmax, xmin
257 real(kind=rp),
dimension(m) :: a, c, d
258 character(len=:),
allocatable :: subsolver, bcknd, bcknd_default
261 real(kind=rp) :: a0 , xmax_const, xmin_const, a_const, c_const, d_const
262 real(kind=rp) :: move_limit
264 integer :: max_iter, n_global, ierr
265 real(kind=rp) :: epsimin, asyinit, asyincr, asydecr
267 call mpi_allreduce(n, n_global, 1, mpi_integer, &
268 mpi_sum, neko_comm, ierr)
271 if (neko_bcknd_device .eq. 1)
then
272 bcknd_default =
"device"
274 bcknd_default =
"cpu"
280 call json_get_or_default(json,
'mma.epsimin', epsimin, &
281 1.0e-9_rp * sqrt(real(m + n_global, rp)))
282 call json_get_or_default(json,
'mma.max_iter', max_iter, max_iter_default)
285 call json_get_or_default(json,
'mma.asyinit', asyinit, asyinit_default)
286 call json_get_or_default(json,
'mma.asyincr', asyincr, asyincr_default)
287 call json_get_or_default(json,
'mma.asydecr', asydecr, asydecr_default)
289 call json_get_or_default(json,
'mma.backend', bcknd, bcknd_default)
290 call json_get_or_default(json,
'mma.subsolver', subsolver, subsolver_default)
292 call json_get_or_default(json,
'mma.xmin', xmin_const, xmin_default)
293 call json_get_or_default(json,
'mma.xmax', xmax_const, xmax_default)
294 call json_get_or_default(json,
'mma.a0', a0, a0_default)
295 call json_get_or_default(json,
'mma.a', a_const, a_default)
296 call json_get_or_default(json,
'mma.c', c_const, c_default)
297 call json_get_or_default(json,
'mma.d', d_const, d_default)
298 call json_get_or_default(json,
'mma.move_limit', move_limit, move_limit_default)
300 call json_get_or_default(json,
'mma.scale', scale, scale_default)
301 call json_get_or_default(json,
'mma.auto_scale', auto_scale, auto_scale_default)
313 call this%init(x, n, m, a0, a, c, d, xmin, xmax, &
314 max_iter, epsimin, asyinit, asyincr, asydecr, bcknd, subsolver, &
317 end subroutine mma_init_from_json
320 subroutine mma_free(this)
321 class(mma_t),
intent(inout) :: this
323 call this%xold1%free()
324 call this%xold2%free()
325 call this%alpha%free()
326 call this%beta%free()
332 call this%xmax%free()
333 call this%xmin%free()
338 call this%lambda%free()
347 call this%scratch%free()
349 this%is_initialized = .false.
350 this%is_updated = .false.
351 end subroutine mma_free
354 subroutine mma_init_from_components(this, x, n, m, a0, a, c, d, xmin, xmax, &
355 max_iter, epsimin, asyinit, asyincr, asydecr, bcknd, subsolver, &
368 class(mma_t),
intent(inout) :: this
369 integer,
intent(in) :: n, m
370 type(vector_t),
intent(in) :: x
378 real(kind=rp),
intent(in),
dimension(n) :: xmax, xmin
379 real(kind=rp),
intent(in),
dimension(m) :: a, c, d
380 real(kind=rp),
intent(in) :: a0
381 integer,
intent(in),
optional :: max_iter
382 real(kind=rp),
intent(in),
optional :: epsimin, asyinit, asyincr, asydecr
383 real(kind=rp),
intent(in),
optional :: move_limit
384 character(len=*),
intent(in),
optional :: bcknd, subsolver
385 character(len=256) :: log_msg
389 call this%scratch%init()
394 call this%xold1%init(n)
395 call this%xold2%init(n)
399 call this%alpha%init(n)
400 call this%beta%init(n)
405 call this%low%init(n)
406 call this%upp%init(n)
407 call this%xmax%init(n)
408 call this%xmin%init(n)
411 call this%p0j%init(n)
412 call this%q0j%init(n)
413 call this%pij%init(m, n)
414 call this%qij%init(m, n)
419 call this%lambda%init(m)
422 call this%xsi%init(n)
423 call this%eta%init(n)
436 if (neko_bcknd_device .eq. 1)
then
437 call this%a%copy_from(host_to_device, sync = .false.)
438 call this%c%copy_from(host_to_device, sync = .false.)
439 call this%d%copy_from(host_to_device, sync = .false.)
440 call this%xmax%copy_from(host_to_device, sync = .false.)
441 call this%xmin%copy_from(host_to_device, sync = .true.)
445 this%residumax = huge(0.0_rp)
446 this%residunorm = huge(0.0_rp)
449 call mpi_allreduce(n, this%n_global, 1, mpi_integer, mpi_sum, neko_comm, &
456 if (.not.
present(max_iter)) this%max_iter = max_iter_default
457 if (.not.
present(epsimin))
then
458 this%epsimin = 1.0e-9_rp * sqrt(real(this%m + this%n_global, rp))
462 if (.not.
present(asyinit)) this%asyinit = asyinit_default
463 if (.not.
present(asyincr)) this%asyincr = asyincr_default
464 if (.not.
present(asydecr)) this%asydecr = asydecr_default
465 if (.not.
present(move_limit)) this%move_limit = move_limit_default
468 if (.not.
present(bcknd) .and. neko_bcknd_device .eq. 0)
then
470 else if (.not.
present(bcknd))
then
471 this%bcknd =
"device"
475 if (.not.
present(subsolver)) this%subsolver = subsolver_default
478 if (
present(max_iter)) this%max_iter = max_iter
479 if (
present(epsimin)) this%epsimin = epsimin
480 if (
present(asyinit)) this%asyinit = asyinit
481 if (
present(asyincr)) this%asyincr = asyincr
482 if (
present(asydecr)) this%asydecr = asydecr
483 if (
present(move_limit)) this%move_limit = move_limit
484 if (
present(bcknd)) this%bcknd = bcknd
485 if (
present(subsolver)) this%subsolver = subsolver
488 if (this%bcknd .ne.
"cpu" .and. this%bcknd .ne.
"device")
then
489 call neko_error(
"MMA: unknown backend '" // trim(this%bcknd) // &
490 "', expected 'cpu' or 'device'.")
494 if (.not. (this%epsimin .gt. 0.0_rp))
then
495 call neko_error(
"MMA: epsimin must be positive.")
498 call neko_log%section(
'MMA Parameters')
500 write(log_msg,
'(A10,1X,A)')
'backend ', trim(this%bcknd)
501 call neko_log%message(log_msg)
502 write(log_msg,
'(A10,1X,A)')
'subsolver ', trim(this%subsolver)
503 call neko_log%message(log_msg)
505 write(log_msg,
'(A10,1X,I0)')
'n ', this%n_global
506 call neko_log%message(log_msg)
507 write(log_msg,
'(A10,1X,I0)')
'm ', this%m
508 call neko_log%message(log_msg)
509 write(log_msg,
'(A10,1X,I0)')
'max_iter ', this%max_iter
510 call neko_log%message(log_msg)
512 write(log_msg,
'(A10,1X,E11.5)')
'epsimin ', this%epsimin
513 call neko_log%message(log_msg)
515 write(log_msg,
'(A10,1X,E11.5)')
'asyinit ', this%asyinit
516 call neko_log%message(log_msg)
517 write(log_msg,
'(A10,1X,E11.5)')
'asyincr ', this%asyincr
518 call neko_log%message(log_msg)
519 write(log_msg,
'(A10,1X,E11.5)')
'asydecr ', this%asydecr
520 call neko_log%message(log_msg)
521 write(log_msg,
'(A10,1X,E11.5)')
'a0 ', this%a0
522 call neko_log%message(log_msg)
523 write(log_msg,
'(A10,1X,E11.5)')
'movelimit ', this%move_limit
524 call neko_log%message(log_msg)
526 call neko_log%message(
'Parameters a')
528 write(log_msg,
'(3X,A,I2,A,E11.5)')
'a(', i,
') = ', this%a%x(i)
529 call neko_log%message(log_msg)
531 call neko_log%message(
'Parameters c')
533 write(log_msg,
'(3X,A,I2,A,E11.5)')
'c(', i,
') = ', this%c%x(i)
534 call neko_log%message(log_msg)
536 call neko_log%message(
'Parameters d')
538 write(log_msg,
'(3X,A,I2,A,E11.5)')
'd(', i,
') = ', this%d%x(i)
539 call neko_log%message(log_msg)
542 call neko_log%end_section()
545 this%is_initialized = .true.
546 end subroutine mma_init_from_components
552 subroutine mma_update_vector(this, iter, x, df0dx, fval, dfdx)
553 class(mma_t),
intent(inout) :: this
554 integer,
intent(in) :: iter
555 type(vector_t),
intent(inout) :: x
556 type(vector_t),
intent(inout) :: df0dx, fval
557 type(matrix_t),
intent(inout) :: dfdx
560 select case (this%bcknd)
562 if (neko_bcknd_device .eq. 1)
then
563 call x%copy_from(device_to_host, sync = .false.)
564 call df0dx%copy_from(device_to_host, sync = .false.)
565 call fval%copy_from(device_to_host, sync = .false.)
566 call dfdx%copy_from(device_to_host, sync = .true.)
569 call mma_update_cpu(this, iter, x%x, df0dx%x, fval%x, dfdx%x)
571 if (neko_bcknd_device .eq. 1)
then
572 call x%copy_from(host_to_device, sync = .true.)
576 call mma_update_device(this, iter, x%x_d, df0dx%x_d, fval%x_d, dfdx%x_d)
578 call neko_error(
'mma_update_vector: Unknown backend: ' // &
582 end subroutine mma_update_vector
585 subroutine mma_kkt_vector(this, x, df0dx, fval, dfdx)
586 class(mma_t),
intent(inout) :: this
587 type(vector_t),
intent(inout) :: x, df0dx, fval
588 type(matrix_t),
intent(inout) :: dfdx
591 select case (this%bcknd )
593 if (neko_bcknd_device .eq. 1)
then
594 call device_memcpy(x%x, x%x_d, this%n, device_to_host, &
596 call device_memcpy(df0dx%x, df0dx%x_d, this%n, device_to_host, &
598 call device_memcpy(fval%x, fval%x_d, this%m, device_to_host, &
600 call device_memcpy(dfdx%x, dfdx%x_d, this%m * this%n, device_to_host,&
604 call mma_kkt_cpu(this, x%x, df0dx%x, fval%x, dfdx%x)
606 call mma_kkt_device(this, x%x_d, df0dx%x_d, fval%x_d, dfdx%x_d)
608 call neko_error(
'mma_KKT_vector: Unknown backend: ' // &
611 end subroutine mma_kkt_vector
620 subroutine mma_save_checkpoint(this, filename, overwrite)
621 class(mma_t),
intent(inout) :: this
622 character(len=*),
intent(in) :: filename
623 logical,
intent(in),
optional :: overwrite
624 character(len=12) :: file_ext
627 call filename_suffix(filename, file_ext)
629 select case (trim(file_ext))
630 case (
'h5',
'hdf5',
'hf5')
631 call mma_save_checkpoint_hdf5(this, filename, overwrite)
633 call neko_error(
'mma_save_checkpoint: Unsupported file format: ' // &
637 end subroutine mma_save_checkpoint
642 subroutine mma_load_checkpoint(this, filename)
643 class(mma_t),
intent(inout) :: this
644 character(len=*),
intent(in) :: filename
645 character(len=12) :: file_ext
648 call filename_suffix(filename, file_ext)
650 select case (trim(file_ext))
651 case (
'h5',
'hdf5',
'hf5')
652 call mma_load_checkpoint_hdf5(this, filename)
654 call neko_error(
'mma_load_checkpoint: Unsupported file format: ' // &
657 end subroutine mma_load_checkpoint
663 pure function mma_get_n(this)
result(n)
664 class(mma_t),
intent(in) :: this
667 end function mma_get_n
670 pure function mma_get_m(this)
result(m)
671 class(mma_t),
intent(in) :: this
674 end function mma_get_m
677 pure function mma_get_residumax(this)
result(residumax)
678 class(mma_t),
intent(in) :: this
679 real(kind=rp) :: residumax
680 residumax = this%residumax
681 end function mma_get_residumax
684 pure function mma_get_residunorm(this)
result(residunorm)
685 class(mma_t),
intent(in) :: this
686 real(kind=rp) :: residunorm
687 residunorm = this%residunorm
688 end function mma_get_residunorm
691 pure function mma_get_max_iter(this)
result(max_iter_value)
692 class(mma_t),
intent(in) :: this
693 integer :: max_iter_value
694 max_iter_value = this%max_iter
695 end function mma_get_max_iter
698 pure function mma_get_backend_and_subsolver(this)
result(backend_subsolver)
699 class(mma_t),
intent(in) :: this
700 character(len=:),
allocatable :: backend_subsolver
701 character(len=:),
allocatable :: backend
703 if (neko_bcknd_cuda .eq. 1)
then
705 else if (neko_bcknd_hip .eq. 1)
then
707 else if (neko_bcknd_opencl .eq. 1)
then
713 backend_subsolver =
'backend:' // trim(backend) //
', subsolver:' // &
715 end function mma_get_backend_and_subsolver
721 subroutine mma_copy_from(this, direction, sync)
722 class(mma_t),
intent(inout) :: this
723 integer,
intent(in) :: direction
724 logical,
intent(in) :: sync
726 call this%xold1%copy_from(direction, sync = .false.)
727 call this%xold2%copy_from(direction, sync = .false.)
728 call this%xmax%copy_from(direction, sync = .false.)
729 call this%xmin%copy_from(direction, sync = .false.)
731 call this%low%copy_from(direction, sync = .false.)
732 call this%upp%copy_from(direction, sync = .false.)
734 call this%a%copy_from(direction, sync = .false.)
735 call this%c%copy_from(direction, sync = .false.)
736 call this%d%copy_from(direction, sync = .false.)
737 call this%y%copy_from(direction, sync = .false.)
738 call this%s%copy_from(direction, sync = .false.)
740 call this%p0j%copy_from(direction, sync = .false.)
741 call this%q0j%copy_from(direction, sync = .false.)
742 call this%pij%copy_from(direction, sync = .false.)
743 call this%qij%copy_from(direction, sync = .false.)
744 call this%bi%copy_from(direction, sync = .false.)
746 call this%alpha%copy_from(direction, sync = .false.)
747 call this%beta%copy_from(direction, sync = .false.)
748 call this%lambda%copy_from(direction, sync = .false.)
749 call this%mu%copy_from(direction, sync = .false.)
750 call this%xsi%copy_from(direction, sync = .false.)
751 call this%eta%copy_from(direction, sync = sync)
753 end subroutine mma_copy_from
759 module subroutine mma_save_checkpoint_hdf5(object, filename, overwrite)
760 class(mma_t),
intent(inout) :: object
761 character(len=*),
intent(in) :: filename
762 logical,
intent(in),
optional :: overwrite
763 call neko_error(
'mma: HDF5 support not enabled rebuild with HAVE_HDF5')
764 end subroutine mma_save_checkpoint_hdf5
766 module subroutine mma_load_checkpoint_hdf5(object, filename)
767 class(mma_t),
intent(inout) :: object
768 character(len=*),
intent(in) :: filename
769 call neko_error(
'mma: HDF5 support not enabled rebuild with HAVE_HDF5')
770 end subroutine mma_load_checkpoint_hdf5