Actual source code: pounders.c
petsc-3.6.1 2015-08-06
1: #include <../src/tao/leastsquares/impls/pounders/pounders.h>
5: static PetscErrorCode pounders_h(Tao subtao, Vec v, Mat H, Mat Hpre, void *ctx)
6: {
8: return(0);
9: }
12: static PetscErrorCode pounders_fg(Tao subtao, Vec x, PetscReal *f, Vec g, void *ctx)
13: {
14: TAO_POUNDERS *mfqP = (TAO_POUNDERS*)ctx;
15: PetscReal d1,d2;
19: /* g = A*x (add b later)*/
20: MatMult(mfqP->subH,x,g);
22: /* f = 1/2 * x'*(Ax) + b'*x */
23: VecDot(x,g,&d1);
24: VecDot(mfqP->subb,x,&d2);
25: *f = 0.5 *d1 + d2;
27: /* now g = g + b */
28: VecAXPY(g, 1.0, mfqP->subb);
29: return(0);
30: }
34: PetscErrorCode gqtwrap(Tao tao,PetscReal *gnorm, PetscReal *qmin)
35: {
37: #if defined(PETSC_USE_REAL_SINGLE)
38: PetscReal atol=1.0e-5;
39: #else
40: PetscReal atol=1.0e-10;
41: #endif
42: PetscInt info,its;
43: TAO_POUNDERS *mfqP = (TAO_POUNDERS*)tao->data;
46: if (! mfqP->usegqt) {
47: PetscReal maxval;
48: PetscInt i,j;
50: VecSetValues(mfqP->subb,mfqP->n,mfqP->indices,mfqP->Gres,INSERT_VALUES);
51: VecAssemblyBegin(mfqP->subb);
52: VecAssemblyEnd(mfqP->subb);
54: VecSet(mfqP->subx,0.0);
56: VecSet(mfqP->subndel,-mfqP->delta);
57: VecSet(mfqP->subpdel,mfqP->delta);
59: for (i=0;i<mfqP->n;i++) {
60: for (j=i;j<mfqP->n;j++) {
61: mfqP->Hres[j+mfqP->n*i] = mfqP->Hres[mfqP->n*j+i];
62: }
63: }
64: MatSetValues(mfqP->subH,mfqP->n,mfqP->indices,mfqP->n,mfqP->indices,mfqP->Hres,INSERT_VALUES);
65: MatAssemblyBegin(mfqP->subH,MAT_FINAL_ASSEMBLY);
66: MatAssemblyEnd(mfqP->subH,MAT_FINAL_ASSEMBLY);
68: TaoResetStatistics(mfqP->subtao);
69: TaoSetTolerances(mfqP->subtao,PETSC_DEFAULT,PETSC_DEFAULT,*gnorm,*gnorm,PETSC_DEFAULT);
70: /* enforce bound constraints -- experimental */
71: if (tao->XU && tao->XL) {
72: VecCopy(tao->XU,mfqP->subxu);
73: VecAXPY(mfqP->subxu,-1.0,tao->solution);
74: VecScale(mfqP->subxu,1.0/mfqP->delta);
75: VecCopy(tao->XL,mfqP->subxl);
76: VecAXPY(mfqP->subxl,-1.0,tao->solution);
77: VecScale(mfqP->subxl,1.0/mfqP->delta);
79: VecPointwiseMin(mfqP->subxu,mfqP->subxu,mfqP->subpdel);
80: VecPointwiseMax(mfqP->subxl,mfqP->subxl,mfqP->subndel);
81: } else {
82: VecCopy(mfqP->subpdel,mfqP->subxu);
83: VecCopy(mfqP->subndel,mfqP->subxl);
84: }
85: /* Make sure xu > xl */
86: VecCopy(mfqP->subxl,mfqP->subpdel);
87: VecAXPY(mfqP->subpdel,-1.0,mfqP->subxu);
88: VecMax(mfqP->subpdel,NULL,&maxval);
89: if (maxval > 1e-10) SETERRQ(PETSC_COMM_WORLD,1,"upper bound < lower bound in subproblem");
90: /* Make sure xu > tao->solution > xl */
91: VecCopy(mfqP->subxl,mfqP->subpdel);
92: VecAXPY(mfqP->subpdel,-1.0,mfqP->subx);
93: VecMax(mfqP->subpdel,NULL,&maxval);
94: if (maxval > 1e-10) SETERRQ(PETSC_COMM_WORLD,1,"initial guess < lower bound in subproblem");
96: VecCopy(mfqP->subx,mfqP->subpdel);
97: VecAXPY(mfqP->subpdel,-1.0,mfqP->subxu);
98: VecMax(mfqP->subpdel,NULL,&maxval);
99: if (maxval > 1e-10) SETERRQ(PETSC_COMM_WORLD,1,"initial guess > upper bound in subproblem");
101: TaoSolve(mfqP->subtao);
102: TaoGetSolutionStatus(mfqP->subtao,NULL,qmin,NULL,NULL,NULL,NULL);
104: /* test bounds post-solution*/
105: VecCopy(mfqP->subxl,mfqP->subpdel);
106: VecAXPY(mfqP->subpdel,-1.0,mfqP->subx);
107: VecMax(mfqP->subpdel,NULL,&maxval);
108: if (maxval > 1e-5) {
109: PetscInfo(tao,"subproblem solution < lower bound\n");
110: tao->reason = TAO_DIVERGED_TR_REDUCTION;
111: }
113: VecCopy(mfqP->subx,mfqP->subpdel);
114: VecAXPY(mfqP->subpdel,-1.0,mfqP->subxu);
115: VecMax(mfqP->subpdel,NULL,&maxval);
116: if (maxval > 1e-5) {
117: PetscInfo(tao,"subproblem solution > upper bound\n");
118: tao->reason = TAO_DIVERGED_TR_REDUCTION;
119: }
120: } else {
121: gqt(mfqP->n,mfqP->Hres,mfqP->n,mfqP->Gres,1.0,mfqP->gqt_rtol,atol,mfqP->gqt_maxits,gnorm,qmin,mfqP->Xsubproblem,&info,&its,mfqP->work,mfqP->work2, mfqP->work3);
122: }
123: *qmin *= -1;
124: return(0);
125: }
129: PetscErrorCode phi2eval(PetscReal *x, PetscInt n, PetscReal *phi)
130: {
131: /* Phi = .5*[x(1)^2 sqrt(2)*x(1)*x(2) ... sqrt(2)*x(1)*x(n) ... x(2)^2 sqrt(2)*x(2)*x(3) .. x(n)^2] */
132: PetscInt i,j,k;
133: PetscReal sqrt2 = PetscSqrtReal(2.0);
136: j=0;
137: for (i=0;i<n;i++) {
138: phi[j] = 0.5 * x[i]*x[i];
139: j++;
140: for (k=i+1;k<n;k++) {
141: phi[j] = x[i]*x[k]/sqrt2;
142: j++;
143: }
144: }
145: return(0);
146: }
150: PetscErrorCode getquadpounders(TAO_POUNDERS *mfqP)
151: {
152: /* Computes the parameters of the quadratic Q(x) = c + g'*x + 0.5*x*G*x'
153: that satisfies the interpolation conditions Q(X[:,j]) = f(j)
154: for j=1,...,m and with a Hessian matrix of least Frobenius norm */
156: /* NB --we are ignoring c */
157: PetscInt i,j,k,num,np = mfqP->nmodelpoints;
158: PetscReal one = 1.0,zero=0.0,negone=-1.0;
159: PetscBLASInt blasnpmax = mfqP->npmax;
160: PetscBLASInt blasnplus1 = mfqP->n+1;
161: PetscBLASInt blasnp = np;
162: PetscBLASInt blasint = mfqP->n*(mfqP->n+1) / 2;
163: PetscBLASInt blasint2 = np - mfqP->n-1;
164: PetscBLASInt info,ione=1;
165: PetscReal sqrt2 = PetscSqrtReal(2.0);
168: for (i=0;i<mfqP->n*mfqP->m;i++) {
169: mfqP->Gdel[i] = 0;
170: }
171: for (i=0;i<mfqP->n*mfqP->n*mfqP->m;i++) {
172: mfqP->Hdel[i] = 0;
173: }
175: /* factor M */
176: PetscStackCallBLAS("LAPACKgetrf",LAPACKgetrf_(&blasnplus1,&blasnp,mfqP->M,&blasnplus1,mfqP->npmaxiwork,&info));
177: if (info != 0) SETERRQ1(PETSC_COMM_SELF,1,"LAPACK routine getrf returned with value %d\n",info);
179: if (np == mfqP->n+1) {
180: for (i=0;i<mfqP->npmax-mfqP->n-1;i++) {
181: mfqP->omega[i]=0.0;
182: }
183: for (i=0;i<mfqP->n*(mfqP->n+1)/2;i++) {
184: mfqP->beta[i]=0.0;
185: }
186: } else {
187: /* Let Ltmp = (L'*L) */
188: PetscStackCallBLAS("BLASgemm",BLASgemm_("T","N",&blasint2,&blasint2,&blasint,&one,&mfqP->L[(mfqP->n+1)*blasint],&blasint,&mfqP->L[(mfqP->n+1)*blasint],&blasint,&zero,mfqP->L_tmp,&blasint));
190: /* factor Ltmp */
191: PetscStackCallBLAS("LAPACKpotrf",LAPACKpotrf_("L",&blasint2,mfqP->L_tmp,&blasint,&info));
192: if (info != 0) SETERRQ1(PETSC_COMM_SELF,1,"LAPACK routine potrf returned with value %d\n",info);
193: }
195: for (k=0;k<mfqP->m;k++) {
196: if (np != mfqP->n+1) {
197: /* Solve L'*L*Omega = Z' * RESk*/
198: PetscStackCallBLAS("BLASgemv",BLASgemv_("T",&blasnp,&blasint2,&one,mfqP->Z,&blasnpmax,&mfqP->RES[mfqP->npmax*k],&ione,&zero,mfqP->omega,&ione));
199: PetscStackCallBLAS("LAPACKpotrs",LAPACKpotrs_("L",&blasint2,&ione,mfqP->L_tmp,&blasint,mfqP->omega,&blasint2,&info));
200: if (info != 0) SETERRQ1(PETSC_COMM_SELF,1,"LAPACK routine potrs returned with value %d\n",info);
202: /* Beta = L*Omega */
203: PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&blasint,&blasint2,&one,&mfqP->L[(mfqP->n+1)*blasint],&blasint,mfqP->omega,&ione,&zero,mfqP->beta,&ione));
204: }
206: /* solve M'*Alpha = RESk - N'*Beta */
207: PetscStackCallBLAS("BLASgemv",BLASgemv_("T",&blasint,&blasnp,&negone,mfqP->N,&blasint,mfqP->beta,&ione,&one,&mfqP->RES[mfqP->npmax*k],&ione));
208: PetscStackCallBLAS("LAPACKgetrs",LAPACKgetrs_("T",&blasnplus1,&ione,mfqP->M,&blasnplus1,mfqP->npmaxiwork,&mfqP->RES[mfqP->npmax*k],&blasnplus1,&info));
209: if (info != 0) SETERRQ1(PETSC_COMM_SELF,1,"LAPACK routine getrs returned with value %d\n",info);
211: /* Gdel(:,k) = Alpha(2:n+1) */
212: for (i=0;i<mfqP->n;i++) {
213: mfqP->Gdel[i + mfqP->n*k] = mfqP->RES[mfqP->npmax*k + i+1];
214: }
216: /* Set Hdels */
217: num=0;
218: for (i=0;i<mfqP->n;i++) {
219: /* H[i,i,k] = Beta(num) */
220: mfqP->Hdel[(i*mfqP->n + i)*mfqP->m + k] = mfqP->beta[num];
221: num++;
222: for (j=i+1;j<mfqP->n;j++) {
223: /* H[i,j,k] = H[j,i,k] = Beta(num)/sqrt(2) */
224: mfqP->Hdel[(j*mfqP->n + i)*mfqP->m + k] = mfqP->beta[num]/sqrt2;
225: mfqP->Hdel[(i*mfqP->n + j)*mfqP->m + k] = mfqP->beta[num]/sqrt2;
226: num++;
227: }
228: }
229: }
230: return(0);
231: }
235: PetscErrorCode morepoints(TAO_POUNDERS *mfqP)
236: {
237: /* Assumes mfqP->model_indices[0] is minimum index
238: Finishes adding points to mfqP->model_indices (up to npmax)
239: Computes L,Z,M,N
240: np is actual number of points in model (should equal npmax?) */
241: PetscInt point,i,j,offset;
242: PetscInt reject;
243: PetscBLASInt blasn=mfqP->n,blasnpmax=mfqP->npmax,blasnplus1=mfqP->n+1,info,blasnmax=mfqP->nmax,blasint,blasint2,blasnp,blasmaxmn;
244: const PetscReal *x;
245: PetscReal normd;
246: PetscErrorCode ierr;
249: /* Initialize M,N */
250: for (i=0;i<mfqP->n+1;i++) {
251: VecGetArrayRead(mfqP->Xhist[mfqP->model_indices[i]],&x);
252: mfqP->M[(mfqP->n+1)*i] = 1.0;
253: for (j=0;j<mfqP->n;j++) {
254: mfqP->M[j+1+((mfqP->n+1)*i)] = (x[j] - mfqP->xmin[j]) / mfqP->delta;
255: }
256: VecRestoreArrayRead(mfqP->Xhist[mfqP->model_indices[i]],&x);
257: phi2eval(&mfqP->M[1+((mfqP->n+1)*i)],mfqP->n,&mfqP->N[mfqP->n*(mfqP->n+1)/2 * i]);
258: }
260: /* Now we add points until we have npmax starting with the most recent ones */
261: point = mfqP->nHist-1;
262: mfqP->nmodelpoints = mfqP->n+1;
263: while (mfqP->nmodelpoints < mfqP->npmax && point>=0) {
264: /* Reject any points already in the model */
265: reject = 0;
266: for (j=0;j<mfqP->n+1;j++) {
267: if (point == mfqP->model_indices[j]) {
268: reject = 1;
269: break;
270: }
271: }
273: /* Reject if norm(d) >c2 */
274: if (!reject) {
275: VecCopy(mfqP->Xhist[point],mfqP->workxvec);
276: VecAXPY(mfqP->workxvec,-1.0,mfqP->Xhist[mfqP->minindex]);
277: VecNorm(mfqP->workxvec,NORM_2,&normd);
278: normd /= mfqP->delta;
279: if (normd > mfqP->c2) {
280: reject =1;
281: }
282: }
283: if (reject){
284: point--;
285: continue;
286: }
288: VecGetArrayRead(mfqP->Xhist[point],&x);
289: mfqP->M[(mfqP->n+1)*mfqP->nmodelpoints] = 1.0;
290: for (j=0;j<mfqP->n;j++) {
291: mfqP->M[j+1+((mfqP->n+1)*mfqP->nmodelpoints)] = (x[j] - mfqP->xmin[j]) / mfqP->delta;
292: }
293: VecRestoreArrayRead(mfqP->Xhist[point],&x);
294: phi2eval(&mfqP->M[1+(mfqP->n+1)*mfqP->nmodelpoints],mfqP->n,&mfqP->N[mfqP->n*(mfqP->n+1)/2 * (mfqP->nmodelpoints)]);
296: /* Update QR factorization */
297: /* Copy M' to Q_tmp */
298: for (i=0;i<mfqP->n+1;i++) {
299: for (j=0;j<mfqP->npmax;j++) {
300: mfqP->Q_tmp[j+mfqP->npmax*i] = mfqP->M[i+(mfqP->n+1)*j];
301: }
302: }
303: blasnp = mfqP->nmodelpoints+1;
304: /* Q_tmp,R = qr(M') */
305: blasmaxmn=PetscMax(mfqP->m,mfqP->n+1);
306: PetscStackCallBLAS("LAPACKgeqrf",LAPACKgeqrf_(&blasnp,&blasnplus1,mfqP->Q_tmp,&blasnpmax,mfqP->tau_tmp,mfqP->mwork,&blasmaxmn,&info));
307: if (info != 0) SETERRQ1(PETSC_COMM_SELF,1,"LAPACK routine geqrf returned with value %d\n",info);
309: /* Reject if min(svd(N*Q(:,n+2:np+1)) <= theta2 */
310: /* L = N*Qtmp */
311: blasint2 = mfqP->n * (mfqP->n+1) / 2;
312: /* Copy N to L_tmp */
313: for (i=0;i<mfqP->n*(mfqP->n+1)/2 * mfqP->npmax;i++) {
314: mfqP->L_tmp[i]= mfqP->N[i];
315: }
316: /* Copy L_save to L_tmp */
318: /* L_tmp = N*Qtmp' */
319: PetscStackCallBLAS("LAPACKormqr",LAPACKormqr_("R","N",&blasint2,&blasnp,&blasnplus1,mfqP->Q_tmp,&blasnpmax,mfqP->tau_tmp,mfqP->L_tmp,&blasint2,mfqP->npmaxwork,&blasnmax,&info));
320: if (info != 0) SETERRQ1(PETSC_COMM_SELF,1,"LAPACK routine ormqr returned with value %d\n",info);
322: /* Copy L_tmp to L_save */
323: for (i=0;i<mfqP->npmax * mfqP->n*(mfqP->n+1)/2;i++) {
324: mfqP->L_save[i] = mfqP->L_tmp[i];
325: }
327: /* Get svd for L_tmp(:,n+2:np+1) (L_tmp is modified in process) */
328: blasint = mfqP->nmodelpoints - mfqP->n;
329: PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("N","N",&blasint2,&blasint,&mfqP->L_tmp[(mfqP->n+1)*blasint2],&blasint2,mfqP->beta,mfqP->work,&blasn,mfqP->work,&blasn,mfqP->npmaxwork,&blasnmax,&info));
330: if (info != 0) SETERRQ1(PETSC_COMM_SELF,1,"LAPACK routine gesvd returned with value %d\n",info);
332: if (mfqP->beta[PetscMin(blasint,blasint2)-1] > mfqP->theta2) {
333: /* accept point */
334: mfqP->model_indices[mfqP->nmodelpoints] = point;
335: /* Copy Q_tmp to Q */
336: for (i=0;i<mfqP->npmax* mfqP->npmax;i++) {
337: mfqP->Q[i] = mfqP->Q_tmp[i];
338: }
339: for (i=0;i<mfqP->npmax;i++){
340: mfqP->tau[i] = mfqP->tau_tmp[i];
341: }
342: mfqP->nmodelpoints++;
343: blasnp = mfqP->nmodelpoints;
345: /* Copy L_save to L */
346: for (i=0;i<mfqP->npmax * mfqP->n*(mfqP->n+1)/2;i++) {
347: mfqP->L[i] = mfqP->L_save[i];
348: }
349: }
350: point--;
351: }
353: blasnp = mfqP->nmodelpoints;
354: /* Copy Q(:,n+2:np) to Z */
355: /* First set Q_tmp to I */
356: for (i=0;i<mfqP->npmax*mfqP->npmax;i++) {
357: mfqP->Q_tmp[i] = 0.0;
358: }
359: for (i=0;i<mfqP->npmax;i++) {
360: mfqP->Q_tmp[i + mfqP->npmax*i] = 1.0;
361: }
363: /* Q_tmp = I * Q */
364: PetscStackCallBLAS("LAPACKormqr",LAPACKormqr_("R","N",&blasnp,&blasnp,&blasnplus1,mfqP->Q,&blasnpmax,mfqP->tau,mfqP->Q_tmp,&blasnpmax,mfqP->npmaxwork,&blasnmax,&info));
365: if (info != 0) SETERRQ1(PETSC_COMM_SELF,1,"LAPACK routine ormqr returned with value %d\n",info);
367: /* Copy Q_tmp(:,n+2:np) to Z) */
368: offset = mfqP->npmax * (mfqP->n+1);
369: for (i=offset;i<mfqP->npmax*mfqP->npmax;i++) {
370: mfqP->Z[i-offset] = mfqP->Q_tmp[i];
371: }
373: if (mfqP->nmodelpoints == mfqP->n + 1) {
374: /* Set L to I_{n+1} */
375: for (i=0;i<mfqP->npmax * mfqP->n*(mfqP->n+1)/2;i++) {
376: mfqP->L[i] = 0.0;
377: }
378: for (i=0;i<mfqP->n;i++) {
379: mfqP->L[(mfqP->n*(mfqP->n+1)/2)*i + i] = 1.0;
380: }
381: }
382: return(0);
383: }
387: /* Only call from modelimprove, addpoint() needs ->Q_tmp and ->work to be set */
388: PetscErrorCode addpoint(Tao tao, TAO_POUNDERS *mfqP, PetscInt index)
389: {
393: /* Create new vector in history: X[newidx] = X[mfqP->index] + delta*X[index]*/
394: VecDuplicate(mfqP->Xhist[0],&mfqP->Xhist[mfqP->nHist]);
395: VecSetValues(mfqP->Xhist[mfqP->nHist],mfqP->n,mfqP->indices,&mfqP->Q_tmp[index*mfqP->npmax],INSERT_VALUES);
396: VecAssemblyBegin(mfqP->Xhist[mfqP->nHist]);
397: VecAssemblyEnd(mfqP->Xhist[mfqP->nHist]);
398: VecAYPX(mfqP->Xhist[mfqP->nHist],mfqP->delta,mfqP->Xhist[mfqP->minindex]);
400: /* Project into feasible region */
401: if (tao->XU && tao->XL) {
402: VecMedian(mfqP->Xhist[mfqP->nHist], tao->XL, tao->XU, mfqP->Xhist[mfqP->nHist]);
403: }
405: /* Compute value of new vector */
406: VecDuplicate(mfqP->Fhist[0],&mfqP->Fhist[mfqP->nHist]);
407: CHKMEMQ;
408: TaoComputeSeparableObjective(tao,mfqP->Xhist[mfqP->nHist],mfqP->Fhist[mfqP->nHist]);
409: VecNorm(mfqP->Fhist[mfqP->nHist],NORM_2,&mfqP->Fres[mfqP->nHist]);
410: if (PetscIsInfOrNanReal(mfqP->Fres[mfqP->nHist])) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN");
411: mfqP->Fres[mfqP->nHist]*=mfqP->Fres[mfqP->nHist];
413: /* Add new vector to model */
414: mfqP->model_indices[mfqP->nmodelpoints] = mfqP->nHist;
415: mfqP->nmodelpoints++;
416: mfqP->nHist++;
417: return(0);
418: }
422: PetscErrorCode modelimprove(Tao tao, TAO_POUNDERS *mfqP, PetscInt addallpoints)
423: {
424: /* modeld = Q(:,np+1:n)' */
426: PetscInt i,j,minindex=0;
427: PetscReal dp,half=0.5,one=1.0,minvalue=PETSC_INFINITY;
428: PetscBLASInt blasn=mfqP->n, blasnpmax = mfqP->npmax, blask,info;
429: PetscBLASInt blas1=1,blasnmax = mfqP->nmax;
431: blask = mfqP->nmodelpoints;
432: /* Qtmp = I(n x n) */
433: for (i=0;i<mfqP->n;i++) {
434: for (j=0;j<mfqP->n;j++) {
435: mfqP->Q_tmp[i + mfqP->npmax*j] = 0.0;
436: }
437: }
438: for (j=0;j<mfqP->n;j++) {
439: mfqP->Q_tmp[j + mfqP->npmax*j] = 1.0;
440: }
442: /* Qtmp = Q * I */
443: PetscStackCallBLAS("LAPACKormqr",LAPACKormqr_("R","N",&blasn,&blasn,&blask,mfqP->Q,&blasnpmax,mfqP->tau, mfqP->Q_tmp, &blasnpmax, mfqP->npmaxwork,&blasnmax, &info));
445: for (i=mfqP->nmodelpoints;i<mfqP->n;i++) {
446: dp = BLASdot_(&blasn,&mfqP->Q_tmp[i*mfqP->npmax],&blas1,mfqP->Gres,&blas1);
447: if (dp>0.0) { /* Model says use the other direction! */
448: for (j=0;j<mfqP->n;j++) {
449: mfqP->Q_tmp[i*mfqP->npmax+j] *= -1;
450: }
451: }
452: /* mfqP->work[i] = Cres+Modeld(i,:)*(Gres+.5*Hres*Modeld(i,:)') */
453: for (j=0;j<mfqP->n;j++) {
454: mfqP->work2[j] = mfqP->Gres[j];
455: }
456: PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&blasn,&blasn,&half,mfqP->Hres,&blasn,&mfqP->Q_tmp[i*mfqP->npmax], &blas1, &one, mfqP->work2,&blas1));
457: mfqP->work[i] = BLASdot_(&blasn,&mfqP->Q_tmp[i*mfqP->npmax],&blas1,mfqP->work2,&blas1);
458: if (i==mfqP->nmodelpoints || mfqP->work[i] < minvalue) {
459: minindex=i;
460: minvalue = mfqP->work[i];
461: }
462: if (addallpoints != 0) {
463: addpoint(tao,mfqP,i);
464: }
465: }
466: if (!addallpoints) {
467: addpoint(tao,mfqP,minindex);
468: }
469: return(0);
470: }
475: PetscErrorCode affpoints(TAO_POUNDERS *mfqP, PetscReal *xmin,PetscReal c)
476: {
477: PetscInt i,j;
478: PetscBLASInt blasm=mfqP->m,blasj,blask,blasn=mfqP->n,ione=1,info;
479: PetscBLASInt blasnpmax = mfqP->npmax,blasmaxmn;
480: PetscReal proj,normd;
481: const PetscReal *x;
482: PetscErrorCode ierr;
485: for (i=mfqP->nHist-1;i>=0;i--) {
486: VecGetArrayRead(mfqP->Xhist[i],&x);
487: for (j=0;j<mfqP->n;j++) {
488: mfqP->work[j] = (x[j] - xmin[j])/mfqP->delta;
489: }
490: VecRestoreArrayRead(mfqP->Xhist[i],&x);
491: PetscStackCallBLAS("BLAScopy",BLAScopy_(&blasn,mfqP->work,&ione,mfqP->work2,&ione));
492: normd = BLASnrm2_(&blasn,mfqP->work,&ione);
493: if (normd <= c*c) {
494: blasj=PetscMax((mfqP->n - mfqP->nmodelpoints),0);
495: if (!mfqP->q_is_I) {
496: /* project D onto null */
497: blask=(mfqP->nmodelpoints);
498: PetscStackCallBLAS("LAPACKormqr",LAPACKormqr_("R","N",&ione,&blasn,&blask,mfqP->Q,&blasnpmax,mfqP->tau,mfqP->work2,&ione,mfqP->mwork,&blasm,&info));
499: if (info < 0) SETERRQ1(PETSC_COMM_SELF,1,"ormqr returned value %d\n",info);
500: }
501: proj = BLASnrm2_(&blasj,&mfqP->work2[mfqP->nmodelpoints],&ione);
503: if (proj >= mfqP->theta1) { /* add this index to model */
504: mfqP->model_indices[mfqP->nmodelpoints]=i;
505: mfqP->nmodelpoints++;
506: PetscStackCallBLAS("BLAScopy",BLAScopy_(&blasn,mfqP->work,&ione,&mfqP->Q_tmp[mfqP->npmax*(mfqP->nmodelpoints-1)],&ione));
507: blask=mfqP->npmax*(mfqP->nmodelpoints);
508: PetscStackCallBLAS("BLAScopy",BLAScopy_(&blask,mfqP->Q_tmp,&ione,mfqP->Q,&ione));
509: blask = mfqP->nmodelpoints;
510: blasmaxmn = PetscMax(mfqP->m,mfqP->n);
511: PetscStackCallBLAS("LAPACKgeqrf",LAPACKgeqrf_(&blasn,&blask,mfqP->Q,&blasnpmax,mfqP->tau,mfqP->mwork,&blasmaxmn,&info));
512: if (info < 0) SETERRQ1(PETSC_COMM_SELF,1,"geqrf returned value %d\n",info);
513: mfqP->q_is_I = 0;
514: }
515: if (mfqP->nmodelpoints == mfqP->n) {
516: break;
517: }
518: }
519: }
521: return(0);
522: }
526: static PetscErrorCode TaoSolve_POUNDERS(Tao tao)
527: {
528: TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;
529: PetscInt i,ii,j,k,l;
530: PetscReal step=1.0;
531: TaoConvergedReason reason = TAO_CONTINUE_ITERATING;
532: PetscInt low,high;
533: PetscReal minnorm;
534: PetscReal *x,*f;
535: const PetscReal *xmint,*fmin;
536: PetscReal cres,deltaold;
537: PetscReal gnorm;
538: PetscBLASInt info,ione=1,iblas;
539: PetscBool valid,same;
540: PetscReal mdec, rho, normxsp;
541: PetscReal one=1.0,zero=0.0,ratio;
542: PetscBLASInt blasm,blasn,blasnpmax,blasn2;
543: PetscErrorCode ierr;
544: static PetscBool set = PETSC_FALSE;
546: /* n = # of parameters
547: m = dimension (components) of function */
549: PetscCitationsRegister("@article{UNEDF0,\n"
550: "title = {Nuclear energy density optimization},\n"
551: "author = {Kortelainen, M. and Lesinski, T. and Mor\'e, J. and Nazarewicz, W.\n"
552: " and Sarich, J. and Schunck, N. and Stoitsov, M. V. and Wild, S. },\n"
553: "journal = {Phys. Rev. C},\n"
554: "volume = {82},\n"
555: "number = {2},\n"
556: "pages = {024313},\n"
557: "numpages = {18},\n"
558: "year = {2010},\n"
559: "month = {Aug},\n"
560: "doi = {10.1103/PhysRevC.82.024313}\n}\n",&set);
561: tao->niter=0;
562: if (tao->XL && tao->XU) {
563: /* Check x0 <= XU */
564: PetscReal val;
565: VecCopy(tao->solution,mfqP->Xhist[0]);
566: VecAXPY(mfqP->Xhist[0],-1.0,tao->XU);
567: VecMax(mfqP->Xhist[0],NULL,&val);
568: if (val > 1e-10) SETERRQ(PETSC_COMM_WORLD,1,"X0 > upper bound");
570: /* Check x0 >= xl */
571: VecCopy(tao->XL,mfqP->Xhist[0]);
572: VecAXPY(mfqP->Xhist[0],-1.0,tao->solution);
573: VecMax(mfqP->Xhist[0],NULL,&val);
574: if (val > 1e-10) SETERRQ(PETSC_COMM_WORLD,1,"X0 < lower bound");
576: /* Check x0 + delta < XU -- should be able to get around this eventually */
578: VecSet(mfqP->Xhist[0],mfqP->delta);
579: VecAXPY(mfqP->Xhist[0],1.0,tao->solution);
580: VecAXPY(mfqP->Xhist[0],-1.0,tao->XU);
581: VecMax(mfqP->Xhist[0],NULL,&val);
582: if (val > 1e-10) SETERRQ(PETSC_COMM_WORLD,1,"X0 + delta > upper bound");
583: }
585: CHKMEMQ;
586: blasm = mfqP->m; blasn=mfqP->n; blasnpmax = mfqP->npmax;
587: for (i=0;i<mfqP->n*mfqP->n*mfqP->m;i++) mfqP->H[i]=0;
589: VecCopy(tao->solution,mfqP->Xhist[0]);
590: CHKMEMQ;
591: TaoComputeSeparableObjective(tao,tao->solution,mfqP->Fhist[0]);
593: VecNorm(mfqP->Fhist[0],NORM_2,&mfqP->Fres[0]);
594: if (PetscIsInfOrNanReal(mfqP->Fres[0])) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN");
595: mfqP->Fres[0]*=mfqP->Fres[0];
596: mfqP->minindex = 0;
597: minnorm = mfqP->Fres[0];
598: TaoMonitor(tao, tao->niter, minnorm, PETSC_INFINITY, 0.0, step, &reason);
599: tao->niter++;
601: VecGetOwnershipRange(mfqP->Xhist[0],&low,&high);
602: for (i=1;i<mfqP->n+1;i++) {
603: VecCopy(tao->solution,mfqP->Xhist[i]);
604: if (i-1 >= low && i-1 < high) {
605: VecGetArray(mfqP->Xhist[i],&x);
606: x[i-1-low] += mfqP->delta;
607: VecRestoreArray(mfqP->Xhist[i],&x);
608: }
609: CHKMEMQ;
610: TaoComputeSeparableObjective(tao,mfqP->Xhist[i],mfqP->Fhist[i]);
611: VecNorm(mfqP->Fhist[i],NORM_2,&mfqP->Fres[i]);
612: if (PetscIsInfOrNanReal(mfqP->Fres[i])) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN");
613: mfqP->Fres[i]*=mfqP->Fres[i];
614: if (mfqP->Fres[i] < minnorm) {
615: mfqP->minindex = i;
616: minnorm = mfqP->Fres[i];
617: }
618: }
619: VecCopy(mfqP->Xhist[mfqP->minindex],tao->solution);
620: VecCopy(mfqP->Fhist[mfqP->minindex],tao->sep_objective);
621: /* Gather mpi vecs to one big local vec */
623: /* Begin serial code */
625: /* Disp[i] = Xi-xmin, i=1,..,mfqP->minindex-1,mfqP->minindex+1,..,n */
626: /* Fdiff[i] = (Fi-Fmin)', i=1,..,mfqP->minindex-1,mfqP->minindex+1,..,n */
627: /* (Column oriented for blas calls) */
628: ii=0;
630: if (mfqP->size == 1) {
631: VecGetArrayRead(mfqP->Xhist[mfqP->minindex],&xmint);
632: for (i=0;i<mfqP->n;i++) mfqP->xmin[i] = xmint[i];
633: VecRestoreArrayRead(mfqP->Xhist[mfqP->minindex],&xmint);
634: VecGetArrayRead(mfqP->Fhist[mfqP->minindex],&fmin);
635: for (i=0;i<mfqP->n+1;i++) {
636: if (i == mfqP->minindex) continue;
638: VecGetArray(mfqP->Xhist[i],&x);
639: for (j=0;j<mfqP->n;j++) {
640: mfqP->Disp[ii+mfqP->npmax*j] = (x[j] - mfqP->xmin[j])/mfqP->delta;
641: }
642: VecRestoreArray(mfqP->Xhist[i],&x);
644: VecGetArray(mfqP->Fhist[i],&f);
645: for (j=0;j<mfqP->m;j++) {
646: mfqP->Fdiff[ii+mfqP->n*j] = f[j] - fmin[j];
647: }
648: VecRestoreArray(mfqP->Fhist[i],&f);
649: mfqP->model_indices[ii++] = i;
651: }
652: for (j=0;j<mfqP->m;j++) {
653: mfqP->C[j] = fmin[j];
654: }
655: VecRestoreArrayRead(mfqP->Fhist[mfqP->minindex],&fmin);
656: } else {
657: VecScatterBegin(mfqP->scatterx,mfqP->Xhist[mfqP->minindex],mfqP->localxmin,INSERT_VALUES,SCATTER_FORWARD);
658: VecScatterEnd(mfqP->scatterx,mfqP->Xhist[mfqP->minindex],mfqP->localxmin,INSERT_VALUES,SCATTER_FORWARD);
659: VecGetArrayRead(mfqP->localxmin,&xmint);
660: for (i=0;i<mfqP->n;i++) mfqP->xmin[i] = xmint[i];
661: VecRestoreArrayRead(mfqP->localxmin,&xmint);
663: VecScatterBegin(mfqP->scatterf,mfqP->Fhist[mfqP->minindex],mfqP->localfmin,INSERT_VALUES,SCATTER_FORWARD);
664: VecScatterEnd(mfqP->scatterf,mfqP->Fhist[mfqP->minindex],mfqP->localfmin,INSERT_VALUES,SCATTER_FORWARD);
665: VecGetArrayRead(mfqP->localfmin,&fmin);
666: for (i=0;i<mfqP->n+1;i++) {
667: if (i == mfqP->minindex) continue;
669: mfqP->model_indices[ii++] = i;
670: VecScatterBegin(mfqP->scatterx,mfqP->Xhist[ii],mfqP->localx,INSERT_VALUES, SCATTER_FORWARD);
671: VecScatterEnd(mfqP->scatterx,mfqP->Xhist[ii],mfqP->localx,INSERT_VALUES, SCATTER_FORWARD);
672: VecGetArray(mfqP->localx,&x);
673: for (j=0;j<mfqP->n;j++) {
674: mfqP->Disp[i+mfqP->npmax*j] = (x[j] - mfqP->xmin[j])/mfqP->delta;
675: }
676: VecRestoreArray(mfqP->localx,&x);
678: VecScatterBegin(mfqP->scatterf,mfqP->Fhist[ii],mfqP->localf,INSERT_VALUES, SCATTER_FORWARD);
679: VecScatterEnd(mfqP->scatterf,mfqP->Fhist[ii],mfqP->localf,INSERT_VALUES, SCATTER_FORWARD);
680: VecGetArray(mfqP->localf,&f);
681: for (j=0;j<mfqP->m;j++) {
682: mfqP->Fdiff[i*mfqP->n+j] = f[j] - fmin[j];
683: }
684: VecRestoreArray(mfqP->localf,&f);
685: }
686: for (j=0;j<mfqP->m;j++) {
687: mfqP->C[j] = fmin[j];
688: }
689: VecRestoreArrayRead(mfqP->localfmin,&fmin);
690: }
692: /* Determine the initial quadratic models */
693: /* G = D(ModelIn,:) \ (F(ModelIn,1:m)-repmat(F(xkin,1:m),n,1)); */
694: /* D (nxn) Fdiff (nxm) => G (nxm) */
695: blasn2 = blasn;
696: PetscStackCallBLAS("LAPACKgesv",LAPACKgesv_(&blasn,&blasm,mfqP->Disp,&blasnpmax,mfqP->iwork,mfqP->Fdiff,&blasn2,&info));
697: PetscInfo1(tao,"gesv returned %d\n",info);
699: cres = minnorm;
700: /* Gres = G*F(xkin,1:m)' G (nxm) Fk (m) */
701: PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&blasn,&blasm,&one,mfqP->Fdiff,&blasn,mfqP->C,&ione,&zero,mfqP->Gres,&ione));
703: /* Hres = G*G' */
704: PetscStackCallBLAS("BLASgemm",BLASgemm_("N","T",&blasn,&blasn,&blasm,&one,mfqP->Fdiff, &blasn,mfqP->Fdiff,&blasn,&zero,mfqP->Hres,&blasn));
706: valid = PETSC_TRUE;
708: VecSetValues(tao->gradient,mfqP->n,mfqP->indices,mfqP->Gres,INSERT_VALUES);
709: VecAssemblyBegin(tao->gradient);
710: VecAssemblyEnd(tao->gradient);
711: VecNorm(tao->gradient,NORM_2,&gnorm);
712: gnorm *= mfqP->delta;
713: VecCopy(mfqP->Xhist[mfqP->minindex],tao->solution);
714: TaoMonitor(tao, tao->niter, minnorm, gnorm, 0.0, step, &reason);
715: mfqP->nHist = mfqP->n+1;
716: mfqP->nmodelpoints = mfqP->n+1;
718: while (reason == TAO_CONTINUE_ITERATING) {
719: PetscReal gnm = 1e-4;
720: tao->niter++;
721: /* Solve the subproblem min{Q(s): ||s|| <= delta} */
722: gqtwrap(tao,&gnm,&mdec);
723: /* Evaluate the function at the new point */
725: for (i=0;i<mfqP->n;i++) {
726: mfqP->work[i] = mfqP->Xsubproblem[i]*mfqP->delta + mfqP->xmin[i];
727: }
728: VecDuplicate(tao->solution,&mfqP->Xhist[mfqP->nHist]);
729: VecDuplicate(tao->sep_objective,&mfqP->Fhist[mfqP->nHist]);
730: VecSetValues(mfqP->Xhist[mfqP->nHist],mfqP->n,mfqP->indices,mfqP->work,INSERT_VALUES);
731: VecAssemblyBegin(mfqP->Xhist[mfqP->nHist]);
732: VecAssemblyEnd(mfqP->Xhist[mfqP->nHist]);
734: TaoComputeSeparableObjective(tao,mfqP->Xhist[mfqP->nHist],mfqP->Fhist[mfqP->nHist]);
735: VecNorm(mfqP->Fhist[mfqP->nHist],NORM_2,&mfqP->Fres[mfqP->nHist]);
736: if (PetscIsInfOrNanReal(mfqP->Fres[mfqP->nHist])) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN");
737: mfqP->Fres[mfqP->nHist]*=mfqP->Fres[mfqP->nHist];
738: rho = (mfqP->Fres[mfqP->minindex] - mfqP->Fres[mfqP->nHist]) / mdec;
739: mfqP->nHist++;
741: /* Update the center */
742: if ((rho >= mfqP->eta1) || (rho > mfqP->eta0 && valid==PETSC_TRUE)) {
743: /* Update model to reflect new base point */
744: for (i=0;i<mfqP->n;i++) {
745: mfqP->work[i] = (mfqP->work[i] - mfqP->xmin[i])/mfqP->delta;
746: }
747: for (j=0;j<mfqP->m;j++) {
748: /* C(j) = C(j) + work*G(:,j) + .5*work*H(:,:,j)*work';
749: G(:,j) = G(:,j) + H(:,:,j)*work' */
750: for (k=0;k<mfqP->n;k++) {
751: mfqP->work2[k]=0.0;
752: for (l=0;l<mfqP->n;l++) {
753: mfqP->work2[k]+=mfqP->H[j + mfqP->m*(k + l*mfqP->n)]*mfqP->work[l];
754: }
755: }
756: for (i=0;i<mfqP->n;i++) {
757: mfqP->C[j]+=mfqP->work[i]*(mfqP->Fdiff[i + mfqP->n* j] + 0.5*mfqP->work2[i]);
758: mfqP->Fdiff[i+mfqP->n*j] +=mfqP-> work2[i];
759: }
760: }
761: /* Cres += work*Gres + .5*work*Hres*work';
762: Gres += Hres*work'; */
764: PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&blasn,&blasn,&one,mfqP->Hres,&blasn,mfqP->work,&ione,&zero,mfqP->work2,&ione));
765: for (i=0;i<mfqP->n;i++) {
766: cres += mfqP->work[i]*(mfqP->Gres[i] + 0.5*mfqP->work2[i]);
767: mfqP->Gres[i] += mfqP->work2[i];
768: }
769: mfqP->minindex = mfqP->nHist-1;
770: minnorm = mfqP->Fres[mfqP->minindex];
771: VecCopy(mfqP->Fhist[mfqP->minindex],tao->sep_objective);
772: /* Change current center */
773: VecGetArrayRead(mfqP->Xhist[mfqP->minindex],&xmint);
774: for (i=0;i<mfqP->n;i++) {
775: mfqP->xmin[i] = xmint[i];
776: }
777: VecRestoreArrayRead(mfqP->Xhist[mfqP->minindex],&xmint);
778: }
780: /* Evaluate at a model-improving point if necessary */
781: if (valid == PETSC_FALSE) {
782: mfqP->q_is_I = 1;
783: mfqP->nmodelpoints = 0;
784: affpoints(mfqP,mfqP->xmin,mfqP->c1);
785: if (mfqP->nmodelpoints < mfqP->n) {
786: PetscInfo(tao,"Model not valid -- model-improving\n");
787: modelimprove(tao,mfqP,1);
788: }
789: }
791: /* Update the trust region radius */
792: deltaold = mfqP->delta;
793: normxsp = 0;
794: for (i=0;i<mfqP->n;i++) {
795: normxsp += mfqP->Xsubproblem[i]*mfqP->Xsubproblem[i];
796: }
797: normxsp = PetscSqrtReal(normxsp);
798: if (rho >= mfqP->eta1 && normxsp > 0.5*mfqP->delta) {
799: mfqP->delta = PetscMin(mfqP->delta*mfqP->gamma1,mfqP->deltamax);
800: } else if (valid == PETSC_TRUE) {
801: mfqP->delta = PetscMax(mfqP->delta*mfqP->gamma0,mfqP->deltamin);
802: }
804: /* Compute the next interpolation set */
805: mfqP->q_is_I = 1;
806: mfqP->nmodelpoints=0;
807: affpoints(mfqP,mfqP->xmin,mfqP->c1);
808: if (mfqP->nmodelpoints == mfqP->n) {
809: valid = PETSC_TRUE;
810: } else {
811: valid = PETSC_FALSE;
812: affpoints(mfqP,mfqP->xmin,mfqP->c2);
813: if (mfqP->n > mfqP->nmodelpoints) {
814: PetscInfo(tao,"Model not valid -- adding geometry points\n");
815: modelimprove(tao,mfqP,mfqP->n - mfqP->nmodelpoints);
816: }
817: }
818: for (i=mfqP->nmodelpoints;i>0;i--) {
819: mfqP->model_indices[i] = mfqP->model_indices[i-1];
820: }
821: mfqP->nmodelpoints++;
822: mfqP->model_indices[0] = mfqP->minindex;
823: morepoints(mfqP);
824: for (i=0;i<mfqP->nmodelpoints;i++) {
825: VecGetArray(mfqP->Xhist[mfqP->model_indices[i]],&x);
826: for (j=0;j<mfqP->n;j++) {
827: mfqP->Disp[i + mfqP->npmax*j] = (x[j] - mfqP->xmin[j]) / deltaold;
828: }
829: VecRestoreArray(mfqP->Xhist[mfqP->model_indices[i]],&x);
830: VecGetArray(mfqP->Fhist[mfqP->model_indices[i]],&f);
831: for (j=0;j<mfqP->m;j++) {
832: for (k=0;k<mfqP->n;k++) {
833: mfqP->work[k]=0.0;
834: for (l=0;l<mfqP->n;l++) {
835: mfqP->work[k] += mfqP->H[j + mfqP->m*(k + mfqP->n*l)] * mfqP->Disp[i + mfqP->npmax*l];
836: }
837: }
838: mfqP->RES[j*mfqP->npmax + i] = -mfqP->C[j] - BLASdot_(&blasn,&mfqP->Fdiff[j*mfqP->n],&ione,&mfqP->Disp[i],&blasnpmax) - 0.5*BLASdot_(&blasn,mfqP->work,&ione,&mfqP->Disp[i],&blasnpmax) + f[j];
839: }
840: VecRestoreArray(mfqP->Fhist[mfqP->model_indices[i]],&f);
841: }
843: /* Update the quadratic model */
844: getquadpounders(mfqP);
845: VecGetArrayRead(mfqP->Fhist[mfqP->minindex],&fmin);
846: PetscStackCallBLAS("BLAScopy",BLAScopy_(&blasm,fmin,&ione,mfqP->C,&ione));
847: /* G = G*(delta/deltaold) + Gdel */
848: ratio = mfqP->delta/deltaold;
849: iblas = blasm*blasn;
850: PetscStackCallBLAS("BLASscal",BLASscal_(&iblas,&ratio,mfqP->Fdiff,&ione));
851: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&iblas,&one,mfqP->Gdel,&ione,mfqP->Fdiff,&ione));
852: /* H = H*(delta/deltaold) + Hdel */
853: iblas = blasm*blasn*blasn;
854: ratio *= ratio;
855: PetscStackCallBLAS("BLASscal",BLASscal_(&iblas,&ratio,mfqP->H,&ione));
856: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&iblas,&one,mfqP->Hdel,&ione,mfqP->H,&ione));
858: /* Get residuals */
859: cres = mfqP->Fres[mfqP->minindex];
860: /* Gres = G*F(xkin,1:m)' */
861: PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&blasn,&blasm,&one,mfqP->Fdiff,&blasn,mfqP->C,&ione,&zero,mfqP->Gres,&ione));
862: /* Hres = sum i=1..m {F(xkin,i)*H(:,:,i)} + G*G' */
863: PetscStackCallBLAS("BLASgemm",BLASgemm_("N","T",&blasn,&blasn,&blasm,&one,mfqP->Fdiff,&blasn,mfqP->Fdiff,&blasn,&zero,mfqP->Hres,&blasn));
865: iblas = mfqP->n*mfqP->n;
867: for (j=0;j<mfqP->m;j++) {
868: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&iblas,&fmin[j],&mfqP->H[j],&blasm,mfqP->Hres,&ione));
869: }
871: /* Export solution and gradient residual to TAO */
872: VecCopy(mfqP->Xhist[mfqP->minindex],tao->solution);
873: VecSetValues(tao->gradient,mfqP->n,mfqP->indices,mfqP->Gres,INSERT_VALUES);
874: VecAssemblyBegin(tao->gradient);
875: VecAssemblyEnd(tao->gradient);
876: VecNorm(tao->gradient,NORM_2,&gnorm);
877: gnorm *= mfqP->delta;
878: /* final criticality test */
879: TaoMonitor(tao, tao->niter, minnorm, gnorm, 0.0, step, &reason);
880: /* test for repeated model */
881: if (mfqP->nmodelpoints==mfqP->last_nmodelpoints) {
882: same = PETSC_TRUE;
883: } else {
884: same = PETSC_FALSE;
885: }
886: for (i=0;i<mfqP->nmodelpoints;i++) {
887: if (same) {
888: if (mfqP->model_indices[i] == mfqP->last_model_indices[i]) {
889: same = PETSC_TRUE;
890: } else {
891: same = PETSC_FALSE;
892: }
893: }
894: mfqP->last_model_indices[i] = mfqP->model_indices[i];
895: }
896: mfqP->last_nmodelpoints = mfqP->nmodelpoints;
897: if (same && mfqP->delta == deltaold) {
898: PetscInfo(tao,"Identical model used in successive iterations\n");
899: reason = TAO_CONVERGED_STEPTOL;
900: tao->reason = TAO_CONVERGED_STEPTOL;
901: }
902: }
903: return(0);
904: }
908: static PetscErrorCode TaoSetUp_POUNDERS(Tao tao)
909: {
910: TAO_POUNDERS *mfqP = (TAO_POUNDERS*)tao->data;
911: PetscInt i;
912: IS isfloc,isfglob,isxloc,isxglob;
916: if (!tao->gradient) {VecDuplicate(tao->solution,&tao->gradient); }
917: if (!tao->stepdirection) {VecDuplicate(tao->solution,&tao->stepdirection); }
918: VecGetSize(tao->solution,&mfqP->n);
919: VecGetSize(tao->sep_objective,&mfqP->m);
920: mfqP->c1 = PetscSqrtReal((PetscReal)mfqP->n);
921: if (mfqP->npmax == PETSC_DEFAULT) {
922: mfqP->npmax = 2*mfqP->n + 1;
923: }
924: mfqP->npmax = PetscMin((mfqP->n+1)*(mfqP->n+2)/2,mfqP->npmax);
925: mfqP->npmax = PetscMax(mfqP->npmax, mfqP->n+2);
927: PetscMalloc1(tao->max_funcs+10,&mfqP->Xhist);
928: PetscMalloc1(tao->max_funcs+10,&mfqP->Fhist);
929: for (i=0;i<mfqP->n +1;i++) {
930: VecDuplicate(tao->solution,&mfqP->Xhist[i]);
931: VecDuplicate(tao->sep_objective,&mfqP->Fhist[i]);
932: }
933: VecDuplicate(tao->solution,&mfqP->workxvec);
934: mfqP->nHist = 0;
936: PetscMalloc1(tao->max_funcs+10,&mfqP->Fres);
937: PetscMalloc1(mfqP->npmax*mfqP->m,&mfqP->RES);
938: PetscMalloc1(mfqP->n,&mfqP->work);
939: PetscMalloc1(mfqP->n,&mfqP->work2);
940: PetscMalloc1(mfqP->n,&mfqP->work3);
941: PetscMalloc1(PetscMax(mfqP->m,mfqP->n+1),&mfqP->mwork);
942: PetscMalloc1(mfqP->npmax - mfqP->n - 1,&mfqP->omega);
943: PetscMalloc1(mfqP->n * (mfqP->n+1) / 2,&mfqP->beta);
944: PetscMalloc1(mfqP->n + 1 ,&mfqP->alpha);
946: PetscMalloc1(mfqP->n*mfqP->n*mfqP->m,&mfqP->H);
947: PetscMalloc1(mfqP->npmax*mfqP->npmax,&mfqP->Q);
948: PetscMalloc1(mfqP->npmax*mfqP->npmax,&mfqP->Q_tmp);
949: PetscMalloc1(mfqP->n*(mfqP->n+1)/2*(mfqP->npmax),&mfqP->L);
950: PetscMalloc1(mfqP->n*(mfqP->n+1)/2*(mfqP->npmax),&mfqP->L_tmp);
951: PetscMalloc1(mfqP->n*(mfqP->n+1)/2*(mfqP->npmax),&mfqP->L_save);
952: PetscMalloc1(mfqP->n*(mfqP->n+1)/2*(mfqP->npmax),&mfqP->N);
953: PetscMalloc1(mfqP->npmax * (mfqP->n+1) ,&mfqP->M);
954: PetscMalloc1(mfqP->npmax * (mfqP->npmax - mfqP->n - 1) , &mfqP->Z);
955: PetscMalloc1(mfqP->npmax,&mfqP->tau);
956: PetscMalloc1(mfqP->npmax,&mfqP->tau_tmp);
957: mfqP->nmax = PetscMax(5*mfqP->npmax,mfqP->n*(mfqP->n+1)/2);
958: PetscMalloc1(mfqP->nmax,&mfqP->npmaxwork);
959: PetscMalloc1(mfqP->nmax,&mfqP->npmaxiwork);
960: PetscMalloc1(mfqP->n,&mfqP->xmin);
961: PetscMalloc1(mfqP->m,&mfqP->C);
962: PetscMalloc1(mfqP->m*mfqP->n,&mfqP->Fdiff);
963: PetscMalloc1(mfqP->npmax*mfqP->n,&mfqP->Disp);
964: PetscMalloc1(mfqP->n,&mfqP->Gres);
965: PetscMalloc1(mfqP->n*mfqP->n,&mfqP->Hres);
966: PetscMalloc1(mfqP->n*mfqP->n,&mfqP->Gpoints);
967: PetscMalloc1(mfqP->npmax,&mfqP->model_indices);
968: PetscMalloc1(mfqP->npmax,&mfqP->last_model_indices);
969: PetscMalloc1(mfqP->n,&mfqP->Xsubproblem);
970: PetscMalloc1(mfqP->m*mfqP->n,&mfqP->Gdel);
971: PetscMalloc1(mfqP->n*mfqP->n*mfqP->m, &mfqP->Hdel);
972: PetscMalloc1(PetscMax(mfqP->m,mfqP->n),&mfqP->indices);
973: PetscMalloc1(mfqP->n,&mfqP->iwork);
974: for (i=0;i<PetscMax(mfqP->m,mfqP->n);i++) {
975: mfqP->indices[i] = i;
976: }
977: MPI_Comm_size(((PetscObject)tao)->comm,&mfqP->size);
978: if (mfqP->size > 1) {
979: VecCreateSeq(PETSC_COMM_SELF,mfqP->n,&mfqP->localx);
980: VecCreateSeq(PETSC_COMM_SELF,mfqP->n,&mfqP->localxmin);
981: VecCreateSeq(PETSC_COMM_SELF,mfqP->m,&mfqP->localf);
982: VecCreateSeq(PETSC_COMM_SELF,mfqP->m,&mfqP->localfmin);
983: ISCreateStride(MPI_COMM_SELF,mfqP->n,0,1,&isxloc);
984: ISCreateStride(MPI_COMM_SELF,mfqP->n,0,1,&isxglob);
985: ISCreateStride(MPI_COMM_SELF,mfqP->m,0,1,&isfloc);
986: ISCreateStride(MPI_COMM_SELF,mfqP->m,0,1,&isfglob);
989: VecScatterCreate(tao->solution,isxglob,mfqP->localx,isxloc,&mfqP->scatterx);
990: VecScatterCreate(tao->sep_objective,isfglob,mfqP->localf,isfloc,&mfqP->scatterf);
992: ISDestroy(&isxloc);
993: ISDestroy(&isxglob);
994: ISDestroy(&isfloc);
995: ISDestroy(&isfglob);
996: }
998: if (!mfqP->usegqt) {
999: KSP ksp;
1000: PC pc;
1001: VecCreateSeqWithArray(PETSC_COMM_SELF,mfqP->n,mfqP->n,mfqP->Xsubproblem,&mfqP->subx);
1002: VecCreateSeq(PETSC_COMM_SELF,mfqP->n,&mfqP->subxl);
1003: VecDuplicate(mfqP->subxl,&mfqP->subb);
1004: VecDuplicate(mfqP->subxl,&mfqP->subxu);
1005: VecDuplicate(mfqP->subxl,&mfqP->subpdel);
1006: VecDuplicate(mfqP->subxl,&mfqP->subndel);
1007: TaoCreate(PETSC_COMM_SELF,&mfqP->subtao);
1008: TaoSetType(mfqP->subtao,TAOTRON);
1009: TaoSetOptionsPrefix(mfqP->subtao,"pounders_subsolver_");
1010: TaoSetInitialVector(mfqP->subtao,mfqP->subx);
1011: TaoSetObjectiveAndGradientRoutine(mfqP->subtao,pounders_fg,(void*)mfqP);
1012: TaoSetMaximumIterations(mfqP->subtao,mfqP->gqt_maxits);
1013: TaoSetFromOptions(mfqP->subtao);
1014: TaoGetKSP(mfqP->subtao,&ksp);
1015: if (ksp) {
1016: KSPGetPC(ksp,&pc);
1017: PCSetType(pc,PCNONE);
1018: }
1019: TaoSetVariableBounds(mfqP->subtao,mfqP->subxl,mfqP->subxu);
1020: MatCreateSeqDense(PETSC_COMM_SELF,mfqP->n,mfqP->n,mfqP->Hres,&mfqP->subH);
1021: TaoSetHessianRoutine(mfqP->subtao,mfqP->subH,mfqP->subH,pounders_h,(void*)mfqP);
1022: }
1023: return(0);
1024: }
1028: static PetscErrorCode TaoDestroy_POUNDERS(Tao tao)
1029: {
1030: TAO_POUNDERS *mfqP = (TAO_POUNDERS*)tao->data;
1031: PetscInt i;
1035: if (!mfqP->usegqt) {
1036: TaoDestroy(&mfqP->subtao);
1037: VecDestroy(&mfqP->subx);
1038: VecDestroy(&mfqP->subxl);
1039: VecDestroy(&mfqP->subxu);
1040: VecDestroy(&mfqP->subb);
1041: VecDestroy(&mfqP->subpdel);
1042: VecDestroy(&mfqP->subndel);
1043: MatDestroy(&mfqP->subH);
1044: }
1045: PetscFree(mfqP->Fres);
1046: PetscFree(mfqP->RES);
1047: PetscFree(mfqP->work);
1048: PetscFree(mfqP->work2);
1049: PetscFree(mfqP->work3);
1050: PetscFree(mfqP->mwork);
1051: PetscFree(mfqP->omega);
1052: PetscFree(mfqP->beta);
1053: PetscFree(mfqP->alpha);
1054: PetscFree(mfqP->H);
1055: PetscFree(mfqP->Q);
1056: PetscFree(mfqP->Q_tmp);
1057: PetscFree(mfqP->L);
1058: PetscFree(mfqP->L_tmp);
1059: PetscFree(mfqP->L_save);
1060: PetscFree(mfqP->N);
1061: PetscFree(mfqP->M);
1062: PetscFree(mfqP->Z);
1063: PetscFree(mfqP->tau);
1064: PetscFree(mfqP->tau_tmp);
1065: PetscFree(mfqP->npmaxwork);
1066: PetscFree(mfqP->npmaxiwork);
1067: PetscFree(mfqP->xmin);
1068: PetscFree(mfqP->C);
1069: PetscFree(mfqP->Fdiff);
1070: PetscFree(mfqP->Disp);
1071: PetscFree(mfqP->Gres);
1072: PetscFree(mfqP->Hres);
1073: PetscFree(mfqP->Gpoints);
1074: PetscFree(mfqP->model_indices);
1075: PetscFree(mfqP->last_model_indices);
1076: PetscFree(mfqP->Xsubproblem);
1077: PetscFree(mfqP->Gdel);
1078: PetscFree(mfqP->Hdel);
1079: PetscFree(mfqP->indices);
1080: PetscFree(mfqP->iwork);
1082: for (i=0;i<mfqP->nHist;i++) {
1083: VecDestroy(&mfqP->Xhist[i]);
1084: VecDestroy(&mfqP->Fhist[i]);
1085: }
1086: if (mfqP->workxvec) {
1087: VecDestroy(&mfqP->workxvec);
1088: }
1089: PetscFree(mfqP->Xhist);
1090: PetscFree(mfqP->Fhist);
1092: if (mfqP->size > 1) {
1093: VecDestroy(&mfqP->localx);
1094: VecDestroy(&mfqP->localxmin);
1095: VecDestroy(&mfqP->localf);
1096: VecDestroy(&mfqP->localfmin);
1097: }
1098: PetscFree(tao->data);
1099: return(0);
1100: }
1104: static PetscErrorCode TaoSetFromOptions_POUNDERS(PetscOptions *PetscOptionsObject,Tao tao)
1105: {
1106: TAO_POUNDERS *mfqP = (TAO_POUNDERS*)tao->data;
1110: PetscOptionsHead(PetscOptionsObject,"POUNDERS method for least-squares optimization");
1111: PetscOptionsReal("-tao_pounders_delta","initial delta","",mfqP->delta,&mfqP->delta0,NULL);
1112: mfqP->delta = mfqP->delta0;
1113: mfqP->npmax = PETSC_DEFAULT;
1114: PetscOptionsInt("-tao_pounders_npmax","max number of points in model","",mfqP->npmax,&mfqP->npmax,NULL);
1115: mfqP->usegqt = PETSC_FALSE;
1116: PetscOptionsBool("-tao_pounders_gqt","use gqt algorithm for subproblem","",mfqP->usegqt,&mfqP->usegqt,NULL);
1117: PetscOptionsTail();
1118: return(0);
1119: }
1123: static PetscErrorCode TaoView_POUNDERS(Tao tao, PetscViewer viewer)
1124: {
1125: TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;
1126: PetscBool isascii;
1127: PetscInt nits;
1131: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
1132: if (isascii) {
1133: PetscViewerASCIIPushTab(viewer);
1134: PetscViewerASCIIPrintf(viewer, "initial delta: %g\n",(double)mfqP->delta0);
1135: PetscViewerASCIIPrintf(viewer, "final delta: %g\n",(double)mfqP->delta);
1136: PetscViewerASCIIPrintf(viewer, "model points: %D\n",mfqP->nmodelpoints);
1137: if (mfqP->usegqt) {
1138: PetscViewerASCIIPrintf(viewer, "subproblem solver: gqt\n");
1139: } else {
1140: PetscViewerASCIIPrintf(viewer, "subproblem solver: %s\n",((PetscObject)mfqP->subtao)->type_name);
1141: TaoGetTotalIterationNumber(mfqP->subtao,&nits);
1142: PetscViewerASCIIPrintf(viewer, "total subproblem iterations: %D\n",nits);
1143: }
1144: PetscViewerASCIIPopTab(viewer);
1145: }
1146: return(0);
1147: }
1148: /*MC
1149: TAOPOUNDERS - POUNDERS derivate-free model-based algorithm for nonlinear least squares
1151: Options Database Keys:
1152: + -tao_pounders_delta - initial step length
1153: . -tao_pounders_npmax - maximum number of points in model
1154: - -tao_pounders_gqt - use gqt algorithm for subproblem instead of TRON
1156: Level: beginner
1157:
1158: M*/
1162: PETSC_EXTERN PetscErrorCode TaoCreate_POUNDERS(Tao tao)
1163: {
1164: TAO_POUNDERS *mfqP = (TAO_POUNDERS*)tao->data;
1168: tao->ops->setup = TaoSetUp_POUNDERS;
1169: tao->ops->solve = TaoSolve_POUNDERS;
1170: tao->ops->view = TaoView_POUNDERS;
1171: tao->ops->setfromoptions = TaoSetFromOptions_POUNDERS;
1172: tao->ops->destroy = TaoDestroy_POUNDERS;
1174: PetscNewLog(tao,&mfqP);
1175: tao->data = (void*)mfqP;
1176: /* Override default settings (unless already changed) */
1177: if (!tao->max_it_changed) tao->max_it = 2000;
1178: if (!tao->max_funcs_changed) tao->max_funcs = 4000;
1179: #if defined(PETSC_USE_REAL_SINGLE)
1180: if (!tao->fatol_changed) tao->fatol = 1.0e-4;
1181: if (!tao->frtol_changed) tao->frtol = 1.0e-4;
1182: mfqP->deltamin=1e-3;
1183: #else
1184: if (!tao->fatol_changed) tao->fatol = 1.0e-8;
1185: if (!tao->frtol_changed) tao->frtol = 1.0e-8;
1186: mfqP->deltamin=1e-6;
1187: #endif
1188: mfqP->delta0 = 0.1;
1189: mfqP->delta = 0.1;
1190: mfqP->deltamax=1e3;
1191: mfqP->c2 = 100.0;
1192: mfqP->theta1=1e-5;
1193: mfqP->theta2=1e-4;
1194: mfqP->gamma0=0.5;
1195: mfqP->gamma1=2.0;
1196: mfqP->eta0 = 0.0;
1197: mfqP->eta1 = 0.1;
1198: mfqP->gqt_rtol = 0.001;
1199: mfqP->gqt_maxits = 50;
1200: mfqP->workxvec = 0;
1201: return(0);
1202: }