111 real(kind=rp),
intent(in) :: t
112 integer,
intent(in) :: tstep
114 integer :: iter, i, ierr, rank, size, nglobal
115 integer,
allocatable :: recv_counts(:), displs(:)
117 real(kind=rp) :: start_time, end_time
118 real(kind=rp),
dimension(this%mma%get_n()) :: x
119 real(kind=rp) :: f0val
120 type(vector_t) :: df0dx, fval
121 type(matrix_t) :: dfdx
123 real(kind=rp),
dimension(this%mma%get_n(), 4) :: stuff
126 real(kind=rp),
allocatable :: all_stuff(:, :)
127 integer,
allocatable :: nloc_all(:)
129 call df0dx%init(this%mma%get_n())
130 call fval%init(this%mma%get_m())
131 call dfdx%init(this%mma%get_m(), this%mma%get_n())
134 call cpu_time(start_time)
135 x = reshape(this%designx%x, [this%mma%get_n()])
136 call func1(this, this%mma%get_n(), this%mma%get_m(), &
137 f0val, df0dx%x, fval%x, dfdx%x)
139 call device_memcpy(df0dx%x, df0dx%x_d, this%mma%get_n(), &
140 host_to_device, sync = .false.)
141 call device_memcpy(fval%x, fval%x_d, this%mma%get_m(), &
142 host_to_device, sync = .false.)
143 call device_memcpy(dfdx%x, dfdx%x_d, this%mma%get_n()*this%mma%get_m(), &
144 host_to_device, sync = .false.)
146 if (pe_rank .eq. 0)
then
147 print *,
'iter = ', 0, &
148 '-------, f0val = ', f0val,
', fval = ', fval%x
151 stuff(:, 1) = reshape(this%designx%dof%x, [this%mma%get_n()])
152 stuff(:, 2) = reshape(this%designx%dof%y, [this%mma%get_n()])
153 stuff(:, 3) = reshape(this%designx%dof%z, [this%mma%get_n()])
154 stuff(:, 4) = reshape(this%designx%x, [this%mma%get_n()])
156 call mpi_comm_size(neko_comm,
size, ierr)
157 call mpi_comm_rank(neko_comm, rank, ierr)
158 allocate(recv_counts(size))
159 allocate(displs(size))
160 call mpi_allreduce(this%mma%get_n(), nglobal, 1, &
161 mpi_integer, mpi_sum, neko_comm, ierr)
163 allocate(nloc_all(size))
165 call mpi_allgather(this%mma%get_n(), 1, mpi_integer, nloc_all, &
166 1, mpi_integer, mpi_comm_world, ierr)
167 recv_counts = this%mma%get_n()
170 displs(i) = displs(i-1) + nloc_all(i-1)
172 allocate(all_stuff(nglobal, 4))
174 call mpi_gatherv(stuff(:, 1), this%mma%get_n(), mpi_real_precision, &
175 all_stuff(:, 1), recv_counts, displs, mpi_real_precision, &
177 call mpi_gatherv(stuff(:, 2), this%mma%get_n(), mpi_real_precision, &
178 all_stuff(:, 2), recv_counts, displs, mpi_real_precision, &
180 call mpi_gatherv(stuff(:, 3), this%mma%get_n(), mpi_real_precision, &
181 all_stuff(:, 3), recv_counts, displs, mpi_real_precision, &
183 call mpi_gatherv(stuff(:, 4), this%mma%get_n(), mpi_real_precision, &
184 all_stuff(:, 4), recv_counts, displs, mpi_real_precision, &
187 if (pe_rank .eq. 0)
then
193 call this%mma%update(iter, x, df0dx, fval, dfdx)
194 this%designx%x = reshape(x, shape(this%designx%x))
196 call func1(this, this%mma%get_n(), this%mma%get_m(), &
197 f0val, df0dx%x, fval%x, dfdx%x)
199 call device_memcpy(df0dx%x, df0dx%x_d, this%mma%get_n(), &
200 host_to_device, sync = .false.)
201 call device_memcpy(fval%x, fval%x_d, this%mma%get_m(), &
202 host_to_device, sync = .false.)
203 call device_memcpy(dfdx%x, dfdx%x_d, &
204 this%mma%get_n()*this%mma%get_m(), host_to_device, sync = .false.)
206 call this%mma%KKT(this%designx%x, df0dx, fval, dfdx)
208 if (pe_rank .eq. 0)
then
209 print *,
'iter = ', iter, &
210 '-------, f0val = ', f0val,
', fval = ', fval%x(1), &
211 ', KKTmax = ', this%mma%get_residumax(), &
212 ', KKTnorm2 = ', this%mma%get_residunorm()
215 if (this%mma%get_residumax() .lt. 1.0e-3_rp)
exit
218 call cpu_time(end_time)
220 print *,
'Elapsed Time: ', end_time - start_time,
' seconds'
221 print *,
"this%designx%x is updated"
224 stuff(:, 1) = reshape(this%designx%dof%x, [this%mma%get_n()])
225 stuff(:, 2) = reshape(this%designx%dof%y, [this%mma%get_n()])
226 stuff(:, 3) = reshape(this%designx%dof%z, [this%mma%get_n()])
227 stuff(:, 4) = reshape(this%designx%x, [this%mma%get_n()])
229 call mpi_gatherv(stuff(:, 1), this%mma%get_n(), mpi_real_precision, &
230 all_stuff(:, 1), recv_counts, displs, mpi_real_precision, &
232 call mpi_gatherv(stuff(:, 2), this%mma%get_n(), mpi_real_precision, &
233 all_stuff(:, 2), recv_counts, displs, mpi_real_precision, &
235 call mpi_gatherv(stuff(:, 3), this%mma%get_n(), mpi_real_precision, &
236 all_stuff(:, 3), recv_counts, displs, mpi_real_precision, &
238 call mpi_gatherv(stuff(:, 4), this%mma%get_n(), mpi_real_precision, &
239 all_stuff(:, 4), recv_counts, displs, mpi_real_precision, &
242 if (pe_rank .eq. 0)
then
293 subroutine func1(this, n, m, f0val, df0dx, fval, dfdx)
304 integer,
intent(in) :: n, m
305 real(kind=rp),
intent(inout) :: f0val
306 real(kind=rp),
dimension(n),
intent(inout) :: df0dx
307 real(kind=rp),
dimension(m),
intent(inout) :: fval
308 real(kind=rp),
dimension(m, n),
intent(inout) :: dfdx
310 real(kind=rp),
dimension(n) :: x, coordx
311 integer :: ierr, nglobal
312 real(kind=rp) :: globalf0val
314 call mpi_allreduce(n, nglobal, 1, &
315 mpi_integer, mpi_sum, neko_comm, ierr)
317 x = reshape(this%designx%x, [n])
318 coordx = reshape(this%designx%dof%x, [n])
320 f0val = sum(x) / nglobal
321 df0dx = 1.0_rp / nglobal
323 call mpi_allreduce(f0val, globalf0val, 1, &
324 mpi_real_precision, mpi_sum, neko_comm, ierr)
328 fval(1) = sum((x-coordx)**2)
330 call mpi_allreduce(fval(1), globalf0val, 1, &
331 mpi_real_precision, mpi_sum, neko_comm, ierr)
332 fval(1) = globalf0val
334 dfdx(1, :) = 2.0_rp * (x - coordx)
336 dfdx(2, :) = - dfdx(1, :)