xref: /petsc/src/tao/leastsquares/impls/pounders/gqt.c (revision 73cf7048d21d856451b1b6963b20c5c23c7baee4)
1 #include <petscsys.h>
2 #include <petscblaslapack.h>
3 
4 static PetscErrorCode estsv(PetscInt n, PetscReal *r, PetscInt ldr, PetscReal *svmin, PetscReal *z)
5 {
6   PetscBLASInt blas1=1, blasn=n, blasnmi, blasj, blasldr = ldr;
7   PetscInt     i,j;
8   PetscReal    e,temp,w,wm,ynorm,znorm,s,sm;
9 
10   PetscFunctionBegin;
11   for (i=0;i<n;i++) {
12     z[i]=0.0;
13   }
14   e = PetscAbs(r[0]);
15   if (e == 0.0) {
16     *svmin = 0.0;
17     z[0] = 1.0;
18   } else {
19     /* Solve R'*y = e */
20     for (i=0;i<n;i++) {
21       /* Scale y. The scaling factor (0.01) reduces the number of scalings */
22       if (z[i] >= 0.0) e =-PetscAbs(e);
23       else             e = PetscAbs(e);
24 
25       if (PetscAbs(e - z[i]) > PetscAbs(r[i + ldr*i])) {
26         temp = PetscMin(0.01,PetscAbs(r[i + ldr*i]))/PetscAbs(e-z[i]);
27         PetscStackCallBLAS("BLASscal",BLASscal_(&blasn, &temp, z, &blas1));
28         e = temp*e;
29       }
30 
31       /* Determine the two possible choices of y[i] */
32       if (r[i + ldr*i] == 0.0) {
33         w = wm = 1.0;
34       } else {
35         w = (e - z[i]) / r[i + ldr*i];
36         wm = - (e + z[i]) / r[i + ldr*i];
37       }
38 
39       /*  Chose y[i] based on the predicted value of y[j] for j>i */
40       s = PetscAbs(e - z[i]);
41       sm = PetscAbs(e + z[i]);
42       for (j=i+1;j<n;j++) {
43         sm += PetscAbs(z[j] + wm * r[i + ldr*j]);
44       }
45       if (i < n-1) {
46         blasnmi = n-i-1;
47         PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&blasnmi, &w, &r[i + ldr*(i+1)], &blasldr, &z[i+1], &blas1));
48         PetscStackCallBLAS("BLASasum",s += BLASasum_(&blasnmi, &z[i+1], &blas1));
49       }
50       if (s < sm) {
51         temp = wm - w;
52         w = wm;
53         if (i < n-1) {
54           PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&blasnmi, &temp, &r[i + ldr*(i+1)], &blasldr, &z[i+1], &blas1));
55         }
56       }
57       z[i] = w;
58     }
59 
60     PetscStackCallBLAS("BLASnrm2",ynorm = BLASnrm2_(&blasn, z, &blas1));
61 
62     /* Solve R*z = y */
63     for (j=n-1; j>=0; j--) {
64       /* Scale z */
65       if (PetscAbs(z[j]) > PetscAbs(r[j + ldr*j])) {
66         temp = PetscMin(0.01, PetscAbs(r[j + ldr*j] / z[j]));
67         PetscStackCallBLAS("BLASscal",BLASscal_(&blasn, &temp, z, &blas1));
68         ynorm *=temp;
69       }
70       if (r[j + ldr*j] == 0) {
71         z[j] = 1.0;
72       } else {
73         z[j] = z[j] / r[j + ldr*j];
74       }
75       temp = -z[j];
76       blasj=j;
77       PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&blasj,&temp,&r[0+ldr*j],&blas1,z,&blas1));
78     }
79 
80     /* Compute svmin and normalize z */
81     PetscStackCallBLAS("BLASnrm2",znorm = 1.0 / BLASnrm2_(&blasn, z, &blas1));
82     *svmin = ynorm*znorm;
83     PetscStackCallBLAS("BLASscal",BLASscal_(&blasn, &znorm, z, &blas1));
84   }
85   PetscFunctionReturn(0);
86 }
87 
88 /*
89 c     ***********
90 c
91 c     Subroutine gqt
92 c
93 c     Given an n by n symmetric matrix A, an n-vector b, and a
94 c     positive number delta, this subroutine determines a vector
95 c     x which approximately minimizes the quadratic function
96 c
97 c           f(x) = (1/2)*x'*A*x + b'*x
98 c
99 c     subject to the Euclidean norm constraint
100 c
101 c           norm(x) <= delta.
102 c
103 c     This subroutine computes an approximation x and a Lagrange
104 c     multiplier par such that either par is zero and
105 c
106 c            norm(x) <= (1+rtol)*delta,
107 c
108 c     or par is positive and
109 c
110 c            abs(norm(x) - delta) <= rtol*delta.
111 c
112 c     If xsol is the solution to the problem, the approximation x
113 c     satisfies
114 c
115 c            f(x) <= ((1 - rtol)**2)*f(xsol)
116 c
117 c     The subroutine statement is
118 c
119 c       subroutine gqt(n,a,lda,b,delta,rtol,atol,itmax,
120 c                        par,f,x,info,z,wa1,wa2)
121 c
122 c     where
123 c
124 c       n is an integer variable.
125 c         On entry n is the order of A.
126 c         On exit n is unchanged.
127 c
128 c       a is a double precision array of dimension (lda,n).
129 c         On entry the full upper triangle of a must contain the
130 c            full upper triangle of the symmetric matrix A.
131 c         On exit the array contains the matrix A.
132 c
133 c       lda is an integer variable.
134 c         On entry lda is the leading dimension of the array a.
135 c         On exit lda is unchanged.
136 c
137 c       b is an double precision array of dimension n.
138 c         On entry b specifies the linear term in the quadratic.
139 c         On exit b is unchanged.
140 c
141 c       delta is a double precision variable.
142 c         On entry delta is a bound on the Euclidean norm of x.
143 c         On exit delta is unchanged.
144 c
145 c       rtol is a double precision variable.
146 c         On entry rtol is the relative accuracy desired in the
147 c            solution. Convergence occurs if
148 c
149 c              f(x) <= ((1 - rtol)**2)*f(xsol)
150 c
151 c         On exit rtol is unchanged.
152 c
153 c       atol is a double precision variable.
154 c         On entry atol is the absolute accuracy desired in the
155 c            solution. Convergence occurs when
156 c
157 c              norm(x) <= (1 + rtol)*delta
158 c
159 c              max(-f(x),-f(xsol)) <= atol
160 c
161 c         On exit atol is unchanged.
162 c
163 c       itmax is an integer variable.
164 c         On entry itmax specifies the maximum number of iterations.
165 c         On exit itmax is unchanged.
166 c
167 c       par is a double precision variable.
168 c         On entry par is an initial estimate of the Lagrange
169 c            multiplier for the constraint norm(x) <= delta.
170 c         On exit par contains the final estimate of the multiplier.
171 c
172 c       f is a double precision variable.
173 c         On entry f need not be specified.
174 c         On exit f is set to f(x) at the output x.
175 c
176 c       x is a double precision array of dimension n.
177 c         On entry x need not be specified.
178 c         On exit x is set to the final estimate of the solution.
179 c
180 c       info is an integer variable.
181 c         On entry info need not be specified.
182 c         On exit info is set as follows:
183 c
184 c            info = 1  The function value f(x) has the relative
185 c                      accuracy specified by rtol.
186 c
187 c            info = 2  The function value f(x) has the absolute
188 c                      accuracy specified by atol.
189 c
190 c            info = 3  Rounding errors prevent further progress.
191 c                      On exit x is the best available approximation.
192 c
193 c            info = 4  Failure to converge after itmax iterations.
194 c                      On exit x is the best available approximation.
195 c
196 c       z is a double precision work array of dimension n.
197 c
198 c       wa1 is a double precision work array of dimension n.
199 c
200 c       wa2 is a double precision work array of dimension n.
201 c
202 c     Subprograms called
203 c
204 c       MINPACK-2  ......  destsv
205 c
206 c       LAPACK  .........  dpotrf
207 c
208 c       Level 1 BLAS  ...  daxpy, dcopy, ddot, dnrm2, dscal
209 c
210 c       Level 2 BLAS  ...  dtrmv, dtrsv
211 c
212 c     MINPACK-2 Project. October 1993.
213 c     Argonne National Laboratory and University of Minnesota.
214 c     Brett M. Averick, Richard Carter, and Jorge J. More'
215 c
216 c     ***********
217 */
218 PetscErrorCode gqt(PetscInt n, PetscReal *a, PetscInt lda, PetscReal *b,
219                    PetscReal delta, PetscReal rtol, PetscReal atol,
220                    PetscInt itmax, PetscReal *retpar, PetscReal *retf,
221                    PetscReal *x, PetscInt *retinfo, PetscInt *retits,
222                    PetscReal *z, PetscReal *wa1, PetscReal *wa2)
223 {
224   PetscErrorCode ierr;
225   PetscReal      f=0.0,p001=0.001,p5=0.5,minusone=-1,delta2=delta*delta;
226   PetscInt       iter, j, rednc,info;
227   PetscBLASInt   indef;
228   PetscBLASInt   blas1=1, blasn=n, iblas, blaslda = lda,blasldap1=lda+1,blasinfo;
229   PetscReal      alpha, anorm, bnorm, parc, parf, parl, pars, par=*retpar,paru, prod, rxnorm, rznorm=0.0, temp, xnorm;
230 
231   PetscFunctionBegin;
232   parf = 0.0;
233   xnorm = 0.0;
234   rxnorm = 0.0;
235   rednc = 0;
236   for (j=0; j<n; j++) {
237     x[j] = 0.0;
238     z[j] = 0.0;
239   }
240 
241   /* Copy the diagonal and save A in its lower triangle */
242   PetscStackCallBLAS("BLAScopy",BLAScopy_(&blasn,a,&blasldap1, wa1, &blas1));
243   for (j=0;j<n-1;j++) {
244     iblas = n - j - 1;
245     PetscStackCallBLAS("BLAScopy",BLAScopy_(&iblas,&a[j + lda*(j+1)], &blaslda, &a[j+1 + lda*j], &blas1));
246   }
247 
248   /* Calculate the l1-norm of A, the Gershgorin row sums, and the
249    l2-norm of b */
250   anorm = 0.0;
251   for (j=0;j<n;j++) {
252     PetscStackCallBLAS("BLASasum",wa2[j] = BLASasum_(&blasn, &a[0 + lda*j], &blas1));CHKMEMQ;
253     anorm = PetscMax(anorm,wa2[j]);
254   }
255   for (j=0;j<n;j++) {
256     wa2[j] = wa2[j] - PetscAbs(wa1[j]);
257   }
258   PetscStackCallBLAS("BLASnrm2",bnorm = BLASnrm2_(&blasn,b,&blas1));CHKMEMQ;
259   /* Calculate a lower bound, pars, for the domain of the problem.
260    Also calculate an upper bound, paru, and a lower bound, parl,
261    for the Lagrange multiplier. */
262   pars = parl = paru = -anorm;
263   for (j=0;j<n;j++) {
264     pars = PetscMax(pars, -wa1[j]);
265     parl = PetscMax(parl, wa1[j] + wa2[j]);
266     paru = PetscMax(paru, -wa1[j] + wa2[j]);
267   }
268   parl = PetscMax(bnorm/delta - parl,pars);
269   parl = PetscMax(0.0,parl);
270   paru = PetscMax(0.0, bnorm/delta + paru);
271 
272   /* If the input par lies outside of the interval (parl, paru),
273    set par to the closer endpoint. */
274 
275   par = PetscMax(par,parl);
276   par = PetscMin(par,paru);
277 
278   /* Special case: parl == paru */
279   paru = PetscMax(paru, (1.0 + rtol)*parl);
280 
281   /* Beginning of an iteration */
282 
283   info = 0;
284   for (iter=1;iter<=itmax;iter++) {
285     /* Safeguard par */
286     if (par <= pars && paru > 0) {
287       par = PetscMax(p001, PetscSqrtScalar(parl/paru)) * paru;
288     }
289 
290     /* Copy the lower triangle of A into its upper triangle and
291      compute A + par*I */
292 
293     for (j=0;j<n-1;j++) {
294       iblas = n - j - 1;
295       PetscStackCallBLAS("BLAScopy",BLAScopy_(&iblas,&a[j+1 + j*lda], &blas1,&a[j + (j+1)*lda], &blaslda));
296     }
297     for (j=0;j<n;j++) {
298       a[j + j*lda] = wa1[j] + par;
299     }
300 
301     /* Attempt the Cholesky factorization of A without referencing
302      the lower triangular part. */
303     PetscStackCallBLAS("LAPACKpotrf",LAPACKpotrf_("U",&blasn,a,&blaslda,&indef));
304 
305     /* Case 1: A + par*I is pos. def. */
306     if (indef == 0) {
307 
308       /* Compute an approximate solution x and save the
309        last value of par with A + par*I pos. def. */
310 
311       parf = par;
312       PetscStackCallBLAS("BLAScopy",BLAScopy_(&blasn, b, &blas1, wa2, &blas1));
313       PetscStackCallBLAS("LAPACKtrtrs",LAPACKtrtrs_("U","T","N",&blasn,&blas1,a,&blaslda,wa2,&blasn,&blasinfo));
314       PetscStackCallBLAS("BLASnrm2",rxnorm = BLASnrm2_(&blasn, wa2, &blas1));
315       PetscStackCallBLAS("LAPACKtrtrs",LAPACKtrtrs_("U","N","N",&blasn,&blas1,a,&blaslda,wa2,&blasn,&blasinfo));
316 
317       PetscStackCallBLAS("BLAScopy",BLAScopy_(&blasn, wa2, &blas1, x, &blas1));
318       PetscStackCallBLAS("BLASscal",BLASscal_(&blasn, &minusone, x, &blas1));
319       PetscStackCallBLAS("BLASnrm2",xnorm = BLASnrm2_(&blasn, x, &blas1));CHKMEMQ;
320 
321       /* Test for convergence */
322       if (PetscAbs(xnorm - delta) <= rtol*delta ||
323           (par == 0  && xnorm <= (1.0+rtol)*delta)) {
324         info = 1;
325       }
326 
327       /* Compute a direction of negative curvature and use this
328        information to improve pars. */
329 
330       iblas=blasn*blasn;
331 
332       ierr = estsv(n,a,lda,&rznorm,z);CHKERRQ(ierr);CHKMEMQ;
333       pars = PetscMax(pars, par-rznorm*rznorm);
334 
335       /* Compute a negative curvature solution of the form
336        x + alpha*z,  where norm(x+alpha*z)==delta */
337 
338       rednc = 0;
339       if (xnorm < delta) {
340         /* Compute alpha */
341         PetscStackCallBLAS("BLASdot",prod = BLASdot_(&blasn, z, &blas1, x, &blas1)/delta);
342         temp = (delta - xnorm)*((delta + xnorm)/delta);
343         alpha = temp/(PetscAbs(prod) + PetscSqrtScalar(prod*prod + temp/delta));
344         if (prod >= 0) alpha = PetscAbs(alpha);
345         else alpha =-PetscAbs(alpha);
346 
347                 /* Test to decide if the negative curvature step
348                    produces a larger reduction than with z=0 */
349         rznorm = PetscAbs(alpha) * rznorm;
350         if ((rznorm*rznorm + par*xnorm*xnorm)/(delta2) <= par) {
351           rednc = 1;
352         }
353         /* Test for convergence */
354         if (p5 * rznorm*rznorm / delta2 <= rtol*(1.0-p5*rtol)*(par + rxnorm*rxnorm/delta2)) {
355           info = 1;
356         } else if (info == 0 && (p5*(par + rxnorm*rxnorm/delta2) <= atol/delta2)) {
357           info = 2;
358         }
359       }
360 
361       /* Compute the Newton correction parc to par. */
362       if (xnorm == 0) {
363         parc = -par;
364       } else {
365         PetscStackCallBLAS("BLAScopy",BLAScopy_(&blasn, x, &blas1, wa2, &blas1));
366         temp = 1.0/xnorm;
367         PetscStackCallBLAS("BLASscal",BLASscal_(&blasn, &temp, wa2, &blas1));
368         PetscStackCallBLAS("LAPACKtrtrs",LAPACKtrtrs_("U","T","N",&blasn, &blas1, a, &blaslda, wa2, &blasn, &blasinfo));
369         PetscStackCallBLAS("BLASnrm2",temp = BLASnrm2_(&blasn, wa2, &blas1));
370         parc = (xnorm - delta)/(delta*temp*temp);
371       }
372 
373       /* update parl or paru */
374       if (xnorm > delta) {
375         parl = PetscMax(parl, par);
376       } else if (xnorm < delta) {
377         paru = PetscMin(paru, par);
378       }
379     } else {
380       /* Case 2: A + par*I is not pos. def. */
381 
382       /* Use the rank information from the Cholesky
383        decomposition to update par. */
384 
385       if (indef > 1) {
386         /* Restore column indef to A + par*I. */
387         iblas = indef - 1;
388         PetscStackCallBLAS("BLAScopy",BLAScopy_(&iblas,&a[indef-1 + 0*lda],&blaslda,&a[0 + (indef-1)*lda],&blas1));
389         a[indef-1 + (indef-1)*lda] = wa1[indef-1] + par;
390 
391                 /* compute parc. */
392         PetscStackCallBLAS("BLAScopy",BLAScopy_(&iblas,&a[0 + (indef-1)*lda], &blas1, wa2, &blas1));
393         PetscStackCallBLAS("LAPACKtrtrs",LAPACKtrtrs_("U","T","N",&iblas,&blas1,a,&blaslda,wa2,&blasn,&blasinfo));
394         PetscStackCallBLAS("BLAScopy",BLAScopy_(&iblas,wa2,&blas1,&a[0 + (indef-1)*lda],&blas1));
395         PetscStackCallBLAS("BLASnrm2",temp = BLASnrm2_(&iblas,&a[0 + (indef-1)*lda],&blas1));CHKMEMQ;
396         a[indef-1 + (indef-1)*lda] -= temp*temp;
397         PetscStackCallBLAS("LAPACKtrtrs",LAPACKtrtrs_("U","N","N",&iblas,&blas1,a,&blaslda,wa2,&blasn,&blasinfo));
398       }
399 
400       wa2[indef-1] = -1.0;
401       iblas = indef;
402       PetscStackCallBLAS("BLASnrm2",temp = BLASnrm2_(&iblas,wa2,&blas1));
403       parc = - a[indef-1 + (indef-1)*lda]/(temp*temp);
404       pars = PetscMax(pars,par+parc);
405 
406       /* If necessary, increase paru slightly.
407        This is needed because in some exceptional situations
408        paru is the optimal value of par. */
409 
410       paru = PetscMax(paru, (1.0+rtol)*pars);
411     }
412 
413     /* Use pars to update parl */
414     parl = PetscMax(parl,pars);
415 
416     /* Test for converged. */
417     if (info == 0) {
418       if (iter == itmax) info=4;
419       if (paru <= (1.0+p5*rtol)*pars) info=3;
420       if (paru == 0.0) info = 2;
421     }
422 
423     /* If exiting, store the best approximation and restore
424      the upper triangle of A. */
425 
426     if (info != 0) {
427       /* Compute the best current estimates for x and f. */
428       par = parf;
429       f = -p5 * (rxnorm*rxnorm + par*xnorm*xnorm);
430       if (rednc) {
431         f = -p5 * (rxnorm*rxnorm + par*delta*delta - rznorm*rznorm);
432         PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&blasn, &alpha, z, &blas1, x, &blas1));
433       }
434       /* Restore the upper triangle of A */
435       for (j = 0; j<n; j++) {
436         iblas = n - j - 1;
437         PetscStackCallBLAS("BLAScopy",BLAScopy_(&iblas,&a[j+1 + j*lda],&blas1, &a[j + (j+1)*lda],&blaslda));
438       }
439       iblas = lda+1;
440       PetscStackCallBLAS("BLAScopy",BLAScopy_(&blasn,wa1,&blas1,a,&iblas));
441       break;
442     }
443     par = PetscMax(parl,par+parc);
444   }
445   *retpar = par;
446   *retf = f;
447   *retinfo = info;
448   *retits = iter;
449   CHKMEMQ;
450   PetscFunctionReturn(0);
451 }
452