Actual source code: pounders.c
1: #include <../src/tao/leastsquares/impls/pounders/pounders.h>
3: static PetscErrorCode pounders_h(Tao subtao, Vec v, Mat H, Mat Hpre, void *ctx)
4: {
6: return(0);
7: }
9: static PetscErrorCode pounders_fg(Tao subtao, Vec x, PetscReal *f, Vec g, void *ctx)
10: {
11: TAO_POUNDERS *mfqP = (TAO_POUNDERS*)ctx;
12: PetscReal d1,d2;
16: /* g = A*x (add b later)*/
17: MatMult(mfqP->subH,x,g);
19: /* f = 1/2 * x'*(Ax) + b'*x */
20: VecDot(x,g,&d1);
21: VecDot(mfqP->subb,x,&d2);
22: *f = 0.5 *d1 + d2;
24: /* now g = g + b */
25: VecAXPY(g, 1.0, mfqP->subb);
26: return(0);
27: }
29: static PetscErrorCode pounders_feval(Tao tao, Vec x, Vec F, PetscReal *fsum)
30: {
32: TAO_POUNDERS *mfqP = (TAO_POUNDERS*)tao->data;
33: PetscInt i,row,col;
34: PetscReal fr,fc;
37: TaoComputeResidual(tao,x,F);
38: if (tao->res_weights_v) {
39: VecPointwiseMult(mfqP->workfvec,tao->res_weights_v,F);
40: VecDot(mfqP->workfvec,mfqP->workfvec,fsum);
41: } else if (tao->res_weights_w) {
42: *fsum=0;
43: for (i=0;i<tao->res_weights_n;i++) {
44: row=tao->res_weights_rows[i];
45: col=tao->res_weights_cols[i];
46: VecGetValues(F,1,&row,&fr);
47: VecGetValues(F,1,&col,&fc);
48: *fsum += tao->res_weights_w[i]*fc*fr;
49: }
50: } else {
51: VecDot(F,F,fsum);
52: }
53: PetscInfo1(tao,"Least-squares residual norm: %20.19e\n",(double)*fsum);
54: if (PetscIsInfOrNanReal(*fsum)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER, "User provided compute function generated Inf or NaN");
55: return(0);
56: }
58: static PetscErrorCode gqtwrap(Tao tao,PetscReal *gnorm, PetscReal *qmin)
59: {
61: #if defined(PETSC_USE_REAL_SINGLE)
62: PetscReal atol=1.0e-5;
63: #else
64: PetscReal atol=1.0e-10;
65: #endif
66: PetscInt info,its;
67: TAO_POUNDERS *mfqP = (TAO_POUNDERS*)tao->data;
70: if (!mfqP->usegqt) {
71: PetscReal maxval;
72: PetscInt i,j;
74: VecSetValues(mfqP->subb,mfqP->n,mfqP->indices,mfqP->Gres,INSERT_VALUES);
75: VecAssemblyBegin(mfqP->subb);
76: VecAssemblyEnd(mfqP->subb);
78: VecSet(mfqP->subx,0.0);
80: VecSet(mfqP->subndel,-1.0);
81: VecSet(mfqP->subpdel,+1.0);
83: /* Complete the lower triangle of the Hessian matrix */
84: for (i=0;i<mfqP->n;i++) {
85: for (j=i+1;j<mfqP->n;j++) {
86: mfqP->Hres[j+mfqP->n*i] = mfqP->Hres[mfqP->n*j+i];
87: }
88: }
89: MatSetValues(mfqP->subH,mfqP->n,mfqP->indices,mfqP->n,mfqP->indices,mfqP->Hres,INSERT_VALUES);
90: MatAssemblyBegin(mfqP->subH,MAT_FINAL_ASSEMBLY);
91: MatAssemblyEnd(mfqP->subH,MAT_FINAL_ASSEMBLY);
93: TaoResetStatistics(mfqP->subtao);
94: /* TaoSetTolerances(mfqP->subtao,*gnorm,*gnorm,PETSC_DEFAULT); */
95: /* enforce bound constraints -- experimental */
96: if (tao->XU && tao->XL) {
97: VecCopy(tao->XU,mfqP->subxu);
98: VecAXPY(mfqP->subxu,-1.0,tao->solution);
99: VecScale(mfqP->subxu,1.0/mfqP->delta);
100: VecCopy(tao->XL,mfqP->subxl);
101: VecAXPY(mfqP->subxl,-1.0,tao->solution);
102: VecScale(mfqP->subxl,1.0/mfqP->delta);
104: VecPointwiseMin(mfqP->subxu,mfqP->subxu,mfqP->subpdel);
105: VecPointwiseMax(mfqP->subxl,mfqP->subxl,mfqP->subndel);
106: } else {
107: VecCopy(mfqP->subpdel,mfqP->subxu);
108: VecCopy(mfqP->subndel,mfqP->subxl);
109: }
110: /* Make sure xu > xl */
111: VecCopy(mfqP->subxl,mfqP->subpdel);
112: VecAXPY(mfqP->subpdel,-1.0,mfqP->subxu);
113: VecMax(mfqP->subpdel,NULL,&maxval);
114: if (maxval > 1e-10) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_OUTOFRANGE,"upper bound < lower bound in subproblem");
115: /* Make sure xu > tao->solution > xl */
116: VecCopy(mfqP->subxl,mfqP->subpdel);
117: VecAXPY(mfqP->subpdel,-1.0,mfqP->subx);
118: VecMax(mfqP->subpdel,NULL,&maxval);
119: if (maxval > 1e-10) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_OUTOFRANGE,"initial guess < lower bound in subproblem");
121: VecCopy(mfqP->subx,mfqP->subpdel);
122: VecAXPY(mfqP->subpdel,-1.0,mfqP->subxu);
123: VecMax(mfqP->subpdel,NULL,&maxval);
124: if (maxval > 1e-10) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_OUTOFRANGE,"initial guess > upper bound in subproblem");
126: TaoSolve(mfqP->subtao);
127: TaoGetSolutionStatus(mfqP->subtao,NULL,qmin,NULL,NULL,NULL,NULL);
129: /* test bounds post-solution*/
130: VecCopy(mfqP->subxl,mfqP->subpdel);
131: VecAXPY(mfqP->subpdel,-1.0,mfqP->subx);
132: VecMax(mfqP->subpdel,NULL,&maxval);
133: if (maxval > 1e-5) {
134: PetscInfo(tao,"subproblem solution < lower bound\n");
135: tao->reason = TAO_DIVERGED_TR_REDUCTION;
136: }
138: VecCopy(mfqP->subx,mfqP->subpdel);
139: VecAXPY(mfqP->subpdel,-1.0,mfqP->subxu);
140: VecMax(mfqP->subpdel,NULL,&maxval);
141: if (maxval > 1e-5) {
142: PetscInfo(tao,"subproblem solution > upper bound\n");
143: tao->reason = TAO_DIVERGED_TR_REDUCTION;
144: }
145: } else {
146: 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);
147: }
148: *qmin *= -1;
149: return(0);
150: }
152: static PetscErrorCode pounders_update_res(Tao tao)
153: {
154: TAO_POUNDERS *mfqP = (TAO_POUNDERS*)tao->data;
155: PetscInt i,row,col;
156: PetscBLASInt blasn=mfqP->n,blasn2=blasn*blasn,blasm=mfqP->m,ione=1;
157: PetscReal zero=0.0,one=1.0,wii,factor;
161: for (i=0;i<mfqP->n;i++) {
162: mfqP->Gres[i]=0;
163: }
164: for (i=0;i<mfqP->n*mfqP->n;i++) {
165: mfqP->Hres[i]=0;
166: }
168: /* Compute Gres= sum_ij[wij * (cjgi + cigj)] */
169: if (tao->res_weights_v) {
170: /* Vector(diagonal) weights: gres = sum_i(wii*ci*gi) */
171: for (i=0;i<mfqP->m;i++) {
172: VecGetValues(tao->res_weights_v,1,&i,&factor);
173: factor=factor*mfqP->C[i];
174: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&blasn,&factor,&mfqP->Fdiff[blasn*i],&ione,mfqP->Gres,&ione));
175: }
177: /* compute Hres = sum_ij [wij * (*ci*Hj + cj*Hi + gi gj' + gj gi') ] */
178: /* vector(diagonal weights) Hres = sum_i(wii*(ci*Hi + gi * gi')*/
179: for (i=0;i<mfqP->m;i++) {
180: VecGetValues(tao->res_weights_v,1,&i,&wii);
181: if (tao->niter>1) {
182: factor=wii*mfqP->C[i];
183: /* add wii * ci * Hi */
184: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&blasn2,&factor,&mfqP->H[i],&blasm,mfqP->Hres,&ione));
185: }
186: /* add wii * gi * gi' */
187: PetscStackCallBLAS("BLASgemm",BLASgemm_("N","T",&blasn,&blasn,&ione,&wii,&mfqP->Fdiff[blasn*i],&blasn,&mfqP->Fdiff[blasn*i],&blasn,&one,mfqP->Hres,&blasn));
188: }
189: } else if (tao->res_weights_w) {
190: /* General case: .5 * Gres= sum_ij[wij * (cjgi + cigj)] */
191: for (i=0;i<tao->res_weights_n;i++) {
192: row=tao->res_weights_rows[i];
193: col=tao->res_weights_cols[i];
195: factor = tao->res_weights_w[i]*mfqP->C[col]/2.0;
196: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&blasn,&factor,&mfqP->Fdiff[blasn*row],&ione,mfqP->Gres,&ione));
197: factor = tao->res_weights_w[i]*mfqP->C[row]/2.0;
198: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&blasn,&factor,&mfqP->Fdiff[blasn*col],&ione,mfqP->Gres,&ione));
199: }
201: /* compute Hres = sum_ij [wij * (*ci*Hj + cj*Hi + gi gj' + gj gi') ] */
202: /* .5 * sum_ij [wij * (*ci*Hj + cj*Hi + gi gj' + gj gi') ] */
203: for (i=0;i<tao->res_weights_n;i++) {
204: row=tao->res_weights_rows[i];
205: col=tao->res_weights_cols[i];
206: factor=tao->res_weights_w[i]/2.0;
207: /* add wij * gi gj' + wij * gj gi' */
208: PetscStackCallBLAS("BLASgemm",BLASgemm_("N","T",&blasn,&blasn,&ione,&factor,&mfqP->Fdiff[blasn*row],&blasn,&mfqP->Fdiff[blasn*col],&blasn,&one,mfqP->Hres,&blasn));
209: PetscStackCallBLAS("BLASgemm",BLASgemm_("N","T",&blasn,&blasn,&ione,&factor,&mfqP->Fdiff[blasn*col],&blasn,&mfqP->Fdiff[blasn*row],&blasn,&one,mfqP->Hres,&blasn));
210: }
211: if (tao->niter > 1) {
212: for (i=0;i<tao->res_weights_n;i++) {
213: row=tao->res_weights_rows[i];
214: col=tao->res_weights_cols[i];
216: /* add wij*cj*Hi */
217: factor = tao->res_weights_w[i]*mfqP->C[col]/2.0;
218: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&blasn2,&factor,&mfqP->H[row],&blasm,mfqP->Hres,&ione));
220: /* add wij*ci*Hj */
221: factor = tao->res_weights_w[i]*mfqP->C[row]/2.0;
222: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&blasn2,&factor,&mfqP->H[col],&blasm,mfqP->Hres,&ione));
223: }
224: }
225: } else {
226: /* Default: Gres= sum_i[cigi] = G*c' */
227: PetscInfo(tao,"Identity weights\n");
228: PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&blasn,&blasm,&one,mfqP->Fdiff,&blasn,mfqP->C,&ione,&zero,mfqP->Gres,&ione));
230: /* compute Hres = sum_ij [wij * (*ci*Hj + cj*Hi + gi gj' + gj gi') ] */
231: /* Hres = G*G' + 0.5 sum {F(xkin,i)*H(:,:,i)} */
232: PetscStackCallBLAS("BLASgemm",BLASgemm_("N","T",&blasn,&blasn,&blasm,&one,mfqP->Fdiff, &blasn,mfqP->Fdiff, &blasn,&zero,mfqP->Hres,&blasn));
234: /* sum(F(xkin,i)*H(:,:,i)) */
235: if (tao->niter>1) {
236: for (i=0;i<mfqP->m;i++) {
237: factor = mfqP->C[i];
238: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&blasn2,&factor,&mfqP->H[i],&blasm,mfqP->Hres,&ione));
239: }
240: }
241: }
242: return(0);
243: }
245: static PetscErrorCode phi2eval(PetscReal *x, PetscInt n, PetscReal *phi)
246: {
247: /* 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] */
248: PetscInt i,j,k;
249: PetscReal sqrt2 = PetscSqrtReal(2.0);
252: j=0;
253: for (i=0;i<n;i++) {
254: phi[j] = 0.5 * x[i]*x[i];
255: j++;
256: for (k=i+1;k<n;k++) {
257: phi[j] = x[i]*x[k]/sqrt2;
258: j++;
259: }
260: }
261: return(0);
262: }
264: static PetscErrorCode getquadpounders(TAO_POUNDERS *mfqP)
265: {
266: /* Computes the parameters of the quadratic Q(x) = c + g'*x + 0.5*x*G*x'
267: that satisfies the interpolation conditions Q(X[:,j]) = f(j)
268: for j=1,...,m and with a Hessian matrix of least Frobenius norm */
270: /* NB --we are ignoring c */
271: PetscInt i,j,k,num,np = mfqP->nmodelpoints;
272: PetscReal one = 1.0,zero=0.0,negone=-1.0;
273: PetscBLASInt blasnpmax = mfqP->npmax;
274: PetscBLASInt blasnplus1 = mfqP->n+1;
275: PetscBLASInt blasnp = np;
276: PetscBLASInt blasint = mfqP->n*(mfqP->n+1) / 2;
277: PetscBLASInt blasint2 = np - mfqP->n-1;
278: PetscBLASInt info,ione=1;
279: PetscReal sqrt2 = PetscSqrtReal(2.0);
282: for (i=0;i<mfqP->n*mfqP->m;i++) {
283: mfqP->Gdel[i] = 0;
284: }
285: for (i=0;i<mfqP->n*mfqP->n*mfqP->m;i++) {
286: mfqP->Hdel[i] = 0;
287: }
289: /* factor M */
290: PetscStackCallBLAS("LAPACKgetrf",LAPACKgetrf_(&blasnplus1,&blasnp,mfqP->M,&blasnplus1,mfqP->npmaxiwork,&info));
291: if (info != 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"LAPACK routine getrf returned with value %d\n",info);
293: if (np == mfqP->n+1) {
294: for (i=0;i<mfqP->npmax-mfqP->n-1;i++) {
295: mfqP->omega[i]=0.0;
296: }
297: for (i=0;i<mfqP->n*(mfqP->n+1)/2;i++) {
298: mfqP->beta[i]=0.0;
299: }
300: } else {
301: /* Let Ltmp = (L'*L) */
302: 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));
304: /* factor Ltmp */
305: PetscStackCallBLAS("LAPACKpotrf",LAPACKpotrf_("L",&blasint2,mfqP->L_tmp,&blasint,&info));
306: if (info != 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"LAPACK routine potrf returned with value %d\n",info);
307: }
309: for (k=0;k<mfqP->m;k++) {
310: if (np != mfqP->n+1) {
311: /* Solve L'*L*Omega = Z' * RESk*/
312: PetscStackCallBLAS("BLASgemv",BLASgemv_("T",&blasnp,&blasint2,&one,mfqP->Z,&blasnpmax,&mfqP->RES[mfqP->npmax*k],&ione,&zero,mfqP->omega,&ione));
313: PetscStackCallBLAS("LAPACKpotrs",LAPACKpotrs_("L",&blasint2,&ione,mfqP->L_tmp,&blasint,mfqP->omega,&blasint2,&info));
314: if (info != 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"LAPACK routine potrs returned with value %d\n",info);
316: /* Beta = L*Omega */
317: PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&blasint,&blasint2,&one,&mfqP->L[(mfqP->n+1)*blasint],&blasint,mfqP->omega,&ione,&zero,mfqP->beta,&ione));
318: }
320: /* solve M'*Alpha = RESk - N'*Beta */
321: PetscStackCallBLAS("BLASgemv",BLASgemv_("T",&blasint,&blasnp,&negone,mfqP->N,&blasint,mfqP->beta,&ione,&one,&mfqP->RES[mfqP->npmax*k],&ione));
322: PetscStackCallBLAS("LAPACKgetrs",LAPACKgetrs_("T",&blasnplus1,&ione,mfqP->M,&blasnplus1,mfqP->npmaxiwork,&mfqP->RES[mfqP->npmax*k],&blasnplus1,&info));
323: if (info != 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"LAPACK routine getrs returned with value %d\n",info);
325: /* Gdel(:,k) = Alpha(2:n+1) */
326: for (i=0;i<mfqP->n;i++) {
327: mfqP->Gdel[i + mfqP->n*k] = mfqP->RES[mfqP->npmax*k + i+1];
328: }
330: /* Set Hdels */
331: num=0;
332: for (i=0;i<mfqP->n;i++) {
333: /* H[i,i,k] = Beta(num) */
334: mfqP->Hdel[(i*mfqP->n + i)*mfqP->m + k] = mfqP->beta[num];
335: num++;
336: for (j=i+1;j<mfqP->n;j++) {
337: /* H[i,j,k] = H[j,i,k] = Beta(num)/sqrt(2) */
338: mfqP->Hdel[(j*mfqP->n + i)*mfqP->m + k] = mfqP->beta[num]/sqrt2;
339: mfqP->Hdel[(i*mfqP->n + j)*mfqP->m + k] = mfqP->beta[num]/sqrt2;
340: num++;
341: }
342: }
343: }
344: return(0);
345: }
347: static PetscErrorCode morepoints(TAO_POUNDERS *mfqP)
348: {
349: /* Assumes mfqP->model_indices[0] is minimum index
350: Finishes adding points to mfqP->model_indices (up to npmax)
351: Computes L,Z,M,N
352: np is actual number of points in model (should equal npmax?) */
353: PetscInt point,i,j,offset;
354: PetscInt reject;
355: PetscBLASInt blasn=mfqP->n,blasnpmax=mfqP->npmax,blasnplus1=mfqP->n+1,info,blasnmax=mfqP->nmax,blasint,blasint2,blasnp,blasmaxmn;
356: const PetscReal *x;
357: PetscReal normd;
358: PetscErrorCode ierr;
361: /* Initialize M,N */
362: for (i=0;i<mfqP->n+1;i++) {
363: VecGetArrayRead(mfqP->Xhist[mfqP->model_indices[i]],&x);
364: mfqP->M[(mfqP->n+1)*i] = 1.0;
365: for (j=0;j<mfqP->n;j++) {
366: mfqP->M[j+1+((mfqP->n+1)*i)] = (x[j] - mfqP->xmin[j]) / mfqP->delta;
367: }
368: VecRestoreArrayRead(mfqP->Xhist[mfqP->model_indices[i]],&x);
369: phi2eval(&mfqP->M[1+((mfqP->n+1)*i)],mfqP->n,&mfqP->N[mfqP->n*(mfqP->n+1)/2 * i]);
370: }
372: /* Now we add points until we have npmax starting with the most recent ones */
373: point = mfqP->nHist-1;
374: mfqP->nmodelpoints = mfqP->n+1;
375: while (mfqP->nmodelpoints < mfqP->npmax && point>=0) {
376: /* Reject any points already in the model */
377: reject = 0;
378: for (j=0;j<mfqP->n+1;j++) {
379: if (point == mfqP->model_indices[j]) {
380: reject = 1;
381: break;
382: }
383: }
385: /* Reject if norm(d) >c2 */
386: if (!reject) {
387: VecCopy(mfqP->Xhist[point],mfqP->workxvec);
388: VecAXPY(mfqP->workxvec,-1.0,mfqP->Xhist[mfqP->minindex]);
389: VecNorm(mfqP->workxvec,NORM_2,&normd);
390: normd /= mfqP->delta;
391: if (normd > mfqP->c2) {
392: reject =1;
393: }
394: }
395: if (reject) {
396: point--;
397: continue;
398: }
400: VecGetArrayRead(mfqP->Xhist[point],&x);
401: mfqP->M[(mfqP->n+1)*mfqP->nmodelpoints] = 1.0;
402: for (j=0;j<mfqP->n;j++) {
403: mfqP->M[j+1+((mfqP->n+1)*mfqP->nmodelpoints)] = (x[j] - mfqP->xmin[j]) / mfqP->delta;
404: }
405: VecRestoreArrayRead(mfqP->Xhist[point],&x);
406: phi2eval(&mfqP->M[1+(mfqP->n+1)*mfqP->nmodelpoints],mfqP->n,&mfqP->N[mfqP->n*(mfqP->n+1)/2 * (mfqP->nmodelpoints)]);
408: /* Update QR factorization */
409: /* Copy M' to Q_tmp */
410: for (i=0;i<mfqP->n+1;i++) {
411: for (j=0;j<mfqP->npmax;j++) {
412: mfqP->Q_tmp[j+mfqP->npmax*i] = mfqP->M[i+(mfqP->n+1)*j];
413: }
414: }
415: blasnp = mfqP->nmodelpoints+1;
416: /* Q_tmp,R = qr(M') */
417: blasmaxmn=PetscMax(mfqP->m,mfqP->n+1);
418: PetscStackCallBLAS("LAPACKgeqrf",LAPACKgeqrf_(&blasnp,&blasnplus1,mfqP->Q_tmp,&blasnpmax,mfqP->tau_tmp,mfqP->mwork,&blasmaxmn,&info));
419: if (info != 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"LAPACK routine geqrf returned with value %d\n",info);
421: /* Reject if min(svd(N*Q(:,n+2:np+1)) <= theta2 */
422: /* L = N*Qtmp */
423: blasint2 = mfqP->n * (mfqP->n+1) / 2;
424: /* Copy N to L_tmp */
425: for (i=0;i<mfqP->n*(mfqP->n+1)/2 * mfqP->npmax;i++) {
426: mfqP->L_tmp[i]= mfqP->N[i];
427: }
428: /* Copy L_save to L_tmp */
430: /* L_tmp = N*Qtmp' */
431: PetscStackCallBLAS("LAPACKormqr",LAPACKormqr_("R","N",&blasint2,&blasnp,&blasnplus1,mfqP->Q_tmp,&blasnpmax,mfqP->tau_tmp,mfqP->L_tmp,&blasint2,mfqP->npmaxwork,&blasnmax,&info));
432: if (info != 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"LAPACK routine ormqr returned with value %d\n",info);
434: /* Copy L_tmp to L_save */
435: for (i=0;i<mfqP->npmax * mfqP->n*(mfqP->n+1)/2;i++) {
436: mfqP->L_save[i] = mfqP->L_tmp[i];
437: }
439: /* Get svd for L_tmp(:,n+2:np+1) (L_tmp is modified in process) */
440: blasint = mfqP->nmodelpoints - mfqP->n;
441: PetscFPTrapPush(PETSC_FP_TRAP_OFF);
442: 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));
443: PetscFPTrapPop();
444: if (info != 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"LAPACK routine gesvd returned with value %d\n",info);
446: if (mfqP->beta[PetscMin(blasint,blasint2)-1] > mfqP->theta2) {
447: /* accept point */
448: mfqP->model_indices[mfqP->nmodelpoints] = point;
449: /* Copy Q_tmp to Q */
450: for (i=0;i<mfqP->npmax* mfqP->npmax;i++) {
451: mfqP->Q[i] = mfqP->Q_tmp[i];
452: }
453: for (i=0;i<mfqP->npmax;i++) {
454: mfqP->tau[i] = mfqP->tau_tmp[i];
455: }
456: mfqP->nmodelpoints++;
457: blasnp = mfqP->nmodelpoints;
459: /* Copy L_save to L */
460: for (i=0;i<mfqP->npmax * mfqP->n*(mfqP->n+1)/2;i++) {
461: mfqP->L[i] = mfqP->L_save[i];
462: }
463: }
464: point--;
465: }
467: blasnp = mfqP->nmodelpoints;
468: /* Copy Q(:,n+2:np) to Z */
469: /* First set Q_tmp to I */
470: for (i=0;i<mfqP->npmax*mfqP->npmax;i++) {
471: mfqP->Q_tmp[i] = 0.0;
472: }
473: for (i=0;i<mfqP->npmax;i++) {
474: mfqP->Q_tmp[i + mfqP->npmax*i] = 1.0;
475: }
477: /* Q_tmp = I * Q */
478: PetscStackCallBLAS("LAPACKormqr",LAPACKormqr_("R","N",&blasnp,&blasnp,&blasnplus1,mfqP->Q,&blasnpmax,mfqP->tau,mfqP->Q_tmp,&blasnpmax,mfqP->npmaxwork,&blasnmax,&info));
479: if (info != 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"LAPACK routine ormqr returned with value %d\n",info);
481: /* Copy Q_tmp(:,n+2:np) to Z) */
482: offset = mfqP->npmax * (mfqP->n+1);
483: for (i=offset;i<mfqP->npmax*mfqP->npmax;i++) {
484: mfqP->Z[i-offset] = mfqP->Q_tmp[i];
485: }
487: if (mfqP->nmodelpoints == mfqP->n + 1) {
488: /* Set L to I_{n+1} */
489: for (i=0;i<mfqP->npmax * mfqP->n*(mfqP->n+1)/2;i++) {
490: mfqP->L[i] = 0.0;
491: }
492: for (i=0;i<mfqP->n;i++) {
493: mfqP->L[(mfqP->n*(mfqP->n+1)/2)*i + i] = 1.0;
494: }
495: }
496: return(0);
497: }
499: /* Only call from modelimprove, addpoint() needs ->Q_tmp and ->work to be set */
500: static PetscErrorCode addpoint(Tao tao, TAO_POUNDERS *mfqP, PetscInt index)
501: {
505: /* Create new vector in history: X[newidx] = X[mfqP->index] + delta*X[index]*/
506: VecDuplicate(mfqP->Xhist[0],&mfqP->Xhist[mfqP->nHist]);
507: VecSetValues(mfqP->Xhist[mfqP->nHist],mfqP->n,mfqP->indices,&mfqP->Q_tmp[index*mfqP->npmax],INSERT_VALUES);
508: VecAssemblyBegin(mfqP->Xhist[mfqP->nHist]);
509: VecAssemblyEnd(mfqP->Xhist[mfqP->nHist]);
510: VecAYPX(mfqP->Xhist[mfqP->nHist],mfqP->delta,mfqP->Xhist[mfqP->minindex]);
512: /* Project into feasible region */
513: if (tao->XU && tao->XL) {
514: VecMedian(mfqP->Xhist[mfqP->nHist], tao->XL, tao->XU, mfqP->Xhist[mfqP->nHist]);
515: }
517: /* Compute value of new vector */
518: VecDuplicate(mfqP->Fhist[0],&mfqP->Fhist[mfqP->nHist]);
519: CHKMEMQ;
520: pounders_feval(tao,mfqP->Xhist[mfqP->nHist],mfqP->Fhist[mfqP->nHist],&mfqP->Fres[mfqP->nHist]);
522: /* Add new vector to model */
523: mfqP->model_indices[mfqP->nmodelpoints] = mfqP->nHist;
524: mfqP->nmodelpoints++;
525: mfqP->nHist++;
526: return(0);
527: }
529: static PetscErrorCode modelimprove(Tao tao, TAO_POUNDERS *mfqP, PetscInt addallpoints)
530: {
531: /* modeld = Q(:,np+1:n)' */
533: PetscInt i,j,minindex=0;
534: PetscReal dp,half=0.5,one=1.0,minvalue=PETSC_INFINITY;
535: PetscBLASInt blasn=mfqP->n, blasnpmax = mfqP->npmax, blask,info;
536: PetscBLASInt blas1=1,blasnmax = mfqP->nmax;
539: blask = mfqP->nmodelpoints;
540: /* Qtmp = I(n x n) */
541: for (i=0;i<mfqP->n;i++) {
542: for (j=0;j<mfqP->n;j++) {
543: mfqP->Q_tmp[i + mfqP->npmax*j] = 0.0;
544: }
545: }
546: for (j=0;j<mfqP->n;j++) {
547: mfqP->Q_tmp[j + mfqP->npmax*j] = 1.0;
548: }
550: /* Qtmp = Q * I */
551: PetscStackCallBLAS("LAPACKormqr",LAPACKormqr_("R","N",&blasn,&blasn,&blask,mfqP->Q,&blasnpmax,mfqP->tau, mfqP->Q_tmp, &blasnpmax, mfqP->npmaxwork,&blasnmax, &info));
553: for (i=mfqP->nmodelpoints;i<mfqP->n;i++) {
554: PetscStackCallBLAS("BLASdot",dp = BLASdot_(&blasn,&mfqP->Q_tmp[i*mfqP->npmax],&blas1,mfqP->Gres,&blas1));
555: if (dp>0.0) { /* Model says use the other direction! */
556: for (j=0;j<mfqP->n;j++) {
557: mfqP->Q_tmp[i*mfqP->npmax+j] *= -1;
558: }
559: }
560: /* mfqP->work[i] = Cres+Modeld(i,:)*(Gres+.5*Hres*Modeld(i,:)') */
561: for (j=0;j<mfqP->n;j++) {
562: mfqP->work2[j] = mfqP->Gres[j];
563: }
564: PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&blasn,&blasn,&half,mfqP->Hres,&blasn,&mfqP->Q_tmp[i*mfqP->npmax], &blas1, &one, mfqP->work2,&blas1));
565: PetscStackCallBLAS("BLASdot",mfqP->work[i] = BLASdot_(&blasn,&mfqP->Q_tmp[i*mfqP->npmax],&blas1,mfqP->work2,&blas1));
566: if (i==mfqP->nmodelpoints || mfqP->work[i] < minvalue) {
567: minindex=i;
568: minvalue = mfqP->work[i];
569: }
570: if (addallpoints != 0) {
571: addpoint(tao,mfqP,i);
572: }
573: }
574: if (!addallpoints) {
575: addpoint(tao,mfqP,minindex);
576: }
577: return(0);
578: }
580: static PetscErrorCode affpoints(TAO_POUNDERS *mfqP, PetscReal *xmin,PetscReal c)
581: {
582: PetscInt i,j;
583: PetscBLASInt blasm=mfqP->m,blasj,blask,blasn=mfqP->n,ione=1,info;
584: PetscBLASInt blasnpmax = mfqP->npmax,blasmaxmn;
585: PetscReal proj,normd;
586: const PetscReal *x;
587: PetscErrorCode ierr;
590: for (i=mfqP->nHist-1;i>=0;i--) {
591: VecGetArrayRead(mfqP->Xhist[i],&x);
592: for (j=0;j<mfqP->n;j++) {
593: mfqP->work[j] = (x[j] - xmin[j])/mfqP->delta;
594: }
595: VecRestoreArrayRead(mfqP->Xhist[i],&x);
596: PetscStackCallBLAS("BLAScopy",BLAScopy_(&blasn,mfqP->work,&ione,mfqP->work2,&ione));
597: PetscStackCallBLAS("BLASnrm2",normd = BLASnrm2_(&blasn,mfqP->work,&ione));
598: if (normd <= c) {
599: blasj=PetscMax((mfqP->n - mfqP->nmodelpoints),0);
600: if (!mfqP->q_is_I) {
601: /* project D onto null */
602: blask=(mfqP->nmodelpoints);
603: PetscStackCallBLAS("LAPACKormqr",LAPACKormqr_("R","N",&ione,&blasn,&blask,mfqP->Q,&blasnpmax,mfqP->tau,mfqP->work2,&ione,mfqP->mwork,&blasm,&info));
604: if (info < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"ormqr returned value %d\n",info);
605: }
606: PetscStackCallBLAS("BLASnrm2",proj = BLASnrm2_(&blasj,&mfqP->work2[mfqP->nmodelpoints],&ione));
608: if (proj >= mfqP->theta1) { /* add this index to model */
609: mfqP->model_indices[mfqP->nmodelpoints]=i;
610: mfqP->nmodelpoints++;
611: PetscStackCallBLAS("BLAScopy",BLAScopy_(&blasn,mfqP->work,&ione,&mfqP->Q_tmp[mfqP->npmax*(mfqP->nmodelpoints-1)],&ione));
612: blask=mfqP->npmax*(mfqP->nmodelpoints);
613: PetscStackCallBLAS("BLAScopy",BLAScopy_(&blask,mfqP->Q_tmp,&ione,mfqP->Q,&ione));
614: blask = mfqP->nmodelpoints;
615: blasmaxmn = PetscMax(mfqP->m,mfqP->n);
616: PetscStackCallBLAS("LAPACKgeqrf",LAPACKgeqrf_(&blasn,&blask,mfqP->Q,&blasnpmax,mfqP->tau,mfqP->mwork,&blasmaxmn,&info));
617: if (info < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"geqrf returned value %d\n",info);
618: mfqP->q_is_I = 0;
619: }
620: if (mfqP->nmodelpoints == mfqP->n) {
621: break;
622: }
623: }
624: }
626: return(0);
627: }
629: static PetscErrorCode TaoSolve_POUNDERS(Tao tao)
630: {
631: TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;
632: PetscInt i,ii,j,k,l;
633: PetscReal step=1.0;
634: PetscInt low,high;
635: PetscReal minnorm;
636: PetscReal *x,*f;
637: const PetscReal *xmint,*fmin;
638: PetscReal deltaold;
639: PetscReal gnorm;
640: PetscBLASInt info,ione=1,iblas;
641: PetscBool valid,same;
642: PetscReal mdec, rho, normxsp;
643: PetscReal one=1.0,zero=0.0,ratio;
644: PetscBLASInt blasm,blasn,blasncopy,blasnpmax;
645: PetscErrorCode ierr;
646: static PetscBool set = PETSC_FALSE;
648: /* n = # of parameters
649: m = dimension (components) of function */
651: PetscCitationsRegister("@article{UNEDF0,\n"
652: "title = {Nuclear energy density optimization},\n"
653: "author = {Kortelainen, M. and Lesinski, T. and Mor\'e, J. and Nazarewicz, W.\n"
654: " and Sarich, J. and Schunck, N. and Stoitsov, M. V. and Wild, S. },\n"
655: "journal = {Phys. Rev. C},\n"
656: "volume = {82},\n"
657: "number = {2},\n"
658: "pages = {024313},\n"
659: "numpages = {18},\n"
660: "year = {2010},\n"
661: "month = {Aug},\n"
662: "doi = {10.1103/PhysRevC.82.024313}\n}\n",&set);
663: tao->niter=0;
664: if (tao->XL && tao->XU) {
665: /* Check x0 <= XU */
666: PetscReal val;
668: VecCopy(tao->solution,mfqP->Xhist[0]);
669: VecAXPY(mfqP->Xhist[0],-1.0,tao->XU);
670: VecMax(mfqP->Xhist[0],NULL,&val);
671: if (val > 1e-10) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_OUTOFRANGE,"X0 > upper bound");
673: /* Check x0 >= xl */
674: VecCopy(tao->XL,mfqP->Xhist[0]);
675: VecAXPY(mfqP->Xhist[0],-1.0,tao->solution);
676: VecMax(mfqP->Xhist[0],NULL,&val);
677: if (val > 1e-10) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_OUTOFRANGE,"X0 < lower bound");
679: /* Check x0 + delta < XU -- should be able to get around this eventually */
681: VecSet(mfqP->Xhist[0],mfqP->delta);
682: VecAXPY(mfqP->Xhist[0],1.0,tao->solution);
683: VecAXPY(mfqP->Xhist[0],-1.0,tao->XU);
684: VecMax(mfqP->Xhist[0],NULL,&val);
685: if (val > 1e-10) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_OUTOFRANGE,"X0 + delta > upper bound");
686: }
688: blasm = mfqP->m; blasn=mfqP->n; blasnpmax = mfqP->npmax;
689: for (i=0;i<mfqP->n*mfqP->n*mfqP->m;++i) mfqP->H[i]=0;
691: VecCopy(tao->solution,mfqP->Xhist[0]);
693: /* This provides enough information to approximate the gradient of the objective */
694: /* using a forward difference scheme. */
696: PetscInfo1(tao,"Initialize simplex; delta = %10.9e\n",(double)mfqP->delta);
697: pounders_feval(tao,mfqP->Xhist[0],mfqP->Fhist[0],&mfqP->Fres[0]);
698: mfqP->minindex = 0;
699: minnorm = mfqP->Fres[0];
701: VecGetOwnershipRange(mfqP->Xhist[0],&low,&high);
702: for (i=1;i<mfqP->n+1;++i) {
703: VecCopy(mfqP->Xhist[0],mfqP->Xhist[i]);
705: if (i-1 >= low && i-1 < high) {
706: VecGetArray(mfqP->Xhist[i],&x);
707: x[i-1-low] += mfqP->delta;
708: VecRestoreArray(mfqP->Xhist[i],&x);
709: }
710: CHKMEMQ;
711: pounders_feval(tao,mfqP->Xhist[i],mfqP->Fhist[i],&mfqP->Fres[i]);
712: if (mfqP->Fres[i] < minnorm) {
713: mfqP->minindex = i;
714: minnorm = mfqP->Fres[i];
715: }
716: }
717: VecCopy(mfqP->Xhist[mfqP->minindex],tao->solution);
718: VecCopy(mfqP->Fhist[mfqP->minindex],tao->ls_res);
719: PetscInfo1(tao,"Finalize simplex; minnorm = %10.9e\n",(double)minnorm);
721: /* Gather mpi vecs to one big local vec */
723: /* Begin serial code */
725: /* Disp[i] = Xi-xmin, i=1,..,mfqP->minindex-1,mfqP->minindex+1,..,n */
726: /* Fdiff[i] = (Fi-Fmin)', i=1,..,mfqP->minindex-1,mfqP->minindex+1,..,n */
727: /* (Column oriented for blas calls) */
728: ii=0;
730: PetscInfo1(tao,"Build matrix: %D\n",(PetscInt)mfqP->size);
731: if (1 == mfqP->size) {
732: VecGetArrayRead(mfqP->Xhist[mfqP->minindex],&xmint);
733: for (i=0;i<mfqP->n;i++) mfqP->xmin[i] = xmint[i];
734: VecRestoreArrayRead(mfqP->Xhist[mfqP->minindex],&xmint);
735: VecGetArrayRead(mfqP->Fhist[mfqP->minindex],&fmin);
736: for (i=0;i<mfqP->n+1;i++) {
737: if (i == mfqP->minindex) continue;
739: VecGetArray(mfqP->Xhist[i],&x);
740: for (j=0;j<mfqP->n;j++) {
741: mfqP->Disp[ii+mfqP->npmax*j] = (x[j] - mfqP->xmin[j])/mfqP->delta;
742: }
743: VecRestoreArray(mfqP->Xhist[i],&x);
745: VecGetArray(mfqP->Fhist[i],&f);
746: for (j=0;j<mfqP->m;j++) {
747: mfqP->Fdiff[ii+mfqP->n*j] = f[j] - fmin[j];
748: }
749: VecRestoreArray(mfqP->Fhist[i],&f);
751: mfqP->model_indices[ii++] = i;
752: }
753: for (j=0;j<mfqP->m;j++) {
754: mfqP->C[j] = fmin[j];
755: }
756: VecRestoreArrayRead(mfqP->Fhist[mfqP->minindex],&fmin);
757: } else {
758: VecSet(mfqP->localxmin,0);
759: VecScatterBegin(mfqP->scatterx,mfqP->Xhist[mfqP->minindex],mfqP->localxmin,INSERT_VALUES,SCATTER_FORWARD);
760: VecScatterEnd(mfqP->scatterx,mfqP->Xhist[mfqP->minindex],mfqP->localxmin,INSERT_VALUES,SCATTER_FORWARD);
762: VecGetArrayRead(mfqP->localxmin,&xmint);
763: for (i=0;i<mfqP->n;i++) mfqP->xmin[i] = xmint[i];
764: VecRestoreArrayRead(mfqP->localxmin,&xmint);
766: VecScatterBegin(mfqP->scatterf,mfqP->Fhist[mfqP->minindex],mfqP->localfmin,INSERT_VALUES,SCATTER_FORWARD);
767: VecScatterEnd(mfqP->scatterf,mfqP->Fhist[mfqP->minindex],mfqP->localfmin,INSERT_VALUES,SCATTER_FORWARD);
768: VecGetArrayRead(mfqP->localfmin,&fmin);
769: for (i=0;i<mfqP->n+1;i++) {
770: if (i == mfqP->minindex) continue;
772: VecScatterBegin(mfqP->scatterx,mfqP->Xhist[ii],mfqP->localx,INSERT_VALUES, SCATTER_FORWARD);
773: VecScatterEnd(mfqP->scatterx,mfqP->Xhist[ii],mfqP->localx,INSERT_VALUES, SCATTER_FORWARD);
774: VecGetArray(mfqP->localx,&x);
775: for (j=0;j<mfqP->n;j++) {
776: mfqP->Disp[ii+mfqP->npmax*j] = (x[j] - mfqP->xmin[j])/mfqP->delta;
777: }
778: VecRestoreArray(mfqP->localx,&x);
780: VecScatterBegin(mfqP->scatterf,mfqP->Fhist[ii],mfqP->localf,INSERT_VALUES, SCATTER_FORWARD);
781: VecScatterEnd(mfqP->scatterf,mfqP->Fhist[ii],mfqP->localf,INSERT_VALUES, SCATTER_FORWARD);
782: VecGetArray(mfqP->localf,&f);
783: for (j=0;j<mfqP->m;j++) {
784: mfqP->Fdiff[ii+mfqP->n*j] = f[j] - fmin[j];
785: }
786: VecRestoreArray(mfqP->localf,&f);
788: mfqP->model_indices[ii++] = i;
789: }
790: for (j=0;j<mfqP->m;j++) {
791: mfqP->C[j] = fmin[j];
792: }
793: VecRestoreArrayRead(mfqP->localfmin,&fmin);
794: }
796: /* Determine the initial quadratic models */
797: /* G = D(ModelIn,:) \ (F(ModelIn,1:m)-repmat(F(xkin,1:m),n,1)); */
798: /* D (nxn) Fdiff (nxm) => G (nxm) */
799: blasncopy = blasn;
800: PetscStackCallBLAS("LAPACKgesv",LAPACKgesv_(&blasn,&blasm,mfqP->Disp,&blasnpmax,mfqP->iwork,mfqP->Fdiff,&blasncopy,&info));
801: PetscInfo1(tao,"Linear solve return: %D\n",(PetscInt)info);
803: pounders_update_res(tao);
805: valid = PETSC_TRUE;
807: VecSetValues(tao->gradient,mfqP->n,mfqP->indices,mfqP->Gres,INSERT_VALUES);
808: VecAssemblyBegin(tao->gradient);
809: VecAssemblyEnd(tao->gradient);
810: VecNorm(tao->gradient,NORM_2,&gnorm);
811: gnorm *= mfqP->delta;
812: VecCopy(mfqP->Xhist[mfqP->minindex],tao->solution);
814: tao->reason = TAO_CONTINUE_ITERATING;
815: TaoLogConvergenceHistory(tao,minnorm,gnorm,0.0,tao->ksp_its);
816: TaoMonitor(tao,tao->niter,minnorm,gnorm,0.0,step);
817: (*tao->ops->convergencetest)(tao,tao->cnvP);
819: mfqP->nHist = mfqP->n+1;
820: mfqP->nmodelpoints = mfqP->n+1;
821: PetscInfo1(tao,"Initial gradient: %20.19e\n",(double)gnorm);
823: while (tao->reason == TAO_CONTINUE_ITERATING) {
824: PetscReal gnm = 1e-4;
825: /* Call general purpose update function */
826: if (tao->ops->update) {
827: (*tao->ops->update)(tao, tao->niter, tao->user_update);
828: }
829: tao->niter++;
830: /* Solve the subproblem min{Q(s): ||s|| <= 1.0} */
831: gqtwrap(tao,&gnm,&mdec);
832: /* Evaluate the function at the new point */
834: for (i=0;i<mfqP->n;i++) {
835: mfqP->work[i] = mfqP->Xsubproblem[i]*mfqP->delta + mfqP->xmin[i];
836: }
837: VecDuplicate(tao->solution,&mfqP->Xhist[mfqP->nHist]);
838: VecDuplicate(tao->ls_res,&mfqP->Fhist[mfqP->nHist]);
839: VecSetValues(mfqP->Xhist[mfqP->nHist],mfqP->n,mfqP->indices,mfqP->work,INSERT_VALUES);
840: VecAssemblyBegin(mfqP->Xhist[mfqP->nHist]);
841: VecAssemblyEnd(mfqP->Xhist[mfqP->nHist]);
843: pounders_feval(tao,mfqP->Xhist[mfqP->nHist],mfqP->Fhist[mfqP->nHist],&mfqP->Fres[mfqP->nHist]);
845: rho = (mfqP->Fres[mfqP->minindex] - mfqP->Fres[mfqP->nHist]) / mdec;
846: mfqP->nHist++;
848: /* Update the center */
849: if ((rho >= mfqP->eta1) || (rho > mfqP->eta0 && valid==PETSC_TRUE)) {
850: /* Update model to reflect new base point */
851: for (i=0;i<mfqP->n;i++) {
852: mfqP->work[i] = (mfqP->work[i] - mfqP->xmin[i])/mfqP->delta;
853: }
854: for (j=0;j<mfqP->m;j++) {
855: /* C(j) = C(j) + work*G(:,j) + .5*work*H(:,:,j)*work';
856: G(:,j) = G(:,j) + H(:,:,j)*work' */
857: for (k=0;k<mfqP->n;k++) {
858: mfqP->work2[k]=0.0;
859: for (l=0;l<mfqP->n;l++) {
860: mfqP->work2[k]+=mfqP->H[j + mfqP->m*(k + l*mfqP->n)]*mfqP->work[l];
861: }
862: }
863: for (i=0;i<mfqP->n;i++) {
864: mfqP->C[j]+=mfqP->work[i]*(mfqP->Fdiff[i + mfqP->n* j] + 0.5*mfqP->work2[i]);
865: mfqP->Fdiff[i+mfqP->n*j] +=mfqP-> work2[i];
866: }
867: }
868: /* Cres += work*Gres + .5*work*Hres*work';
869: Gres += Hres*work'; */
871: PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&blasn,&blasn,&one,mfqP->Hres,&blasn,mfqP->work,&ione,&zero,mfqP->work2,&ione));
872: for (i=0;i<mfqP->n;i++) {
873: mfqP->Gres[i] += mfqP->work2[i];
874: }
875: mfqP->minindex = mfqP->nHist-1;
876: minnorm = mfqP->Fres[mfqP->minindex];
877: VecCopy(mfqP->Fhist[mfqP->minindex],tao->ls_res);
878: /* Change current center */
879: VecGetArrayRead(mfqP->Xhist[mfqP->minindex],&xmint);
880: for (i=0;i<mfqP->n;i++) {
881: mfqP->xmin[i] = xmint[i];
882: }
883: VecRestoreArrayRead(mfqP->Xhist[mfqP->minindex],&xmint);
884: }
886: /* Evaluate at a model-improving point if necessary */
887: if (valid == PETSC_FALSE) {
888: mfqP->q_is_I = 1;
889: mfqP->nmodelpoints = 0;
890: affpoints(mfqP,mfqP->xmin,mfqP->c1);
891: if (mfqP->nmodelpoints < mfqP->n) {
892: PetscInfo(tao,"Model not valid -- model-improving\n");
893: modelimprove(tao,mfqP,1);
894: }
895: }
897: /* Update the trust region radius */
898: deltaold = mfqP->delta;
899: normxsp = 0;
900: for (i=0;i<mfqP->n;i++) {
901: normxsp += mfqP->Xsubproblem[i]*mfqP->Xsubproblem[i];
902: }
903: normxsp = PetscSqrtReal(normxsp);
904: if (rho >= mfqP->eta1 && normxsp > 0.5*mfqP->delta) {
905: mfqP->delta = PetscMin(mfqP->delta*mfqP->gamma1,mfqP->deltamax);
906: } else if (valid == PETSC_TRUE) {
907: mfqP->delta = PetscMax(mfqP->delta*mfqP->gamma0,mfqP->deltamin);
908: }
910: /* Compute the next interpolation set */
911: mfqP->q_is_I = 1;
912: mfqP->nmodelpoints=0;
913: PetscInfo2(tao,"Affine Points: xmin = %20.19e, c1 = %20.19e\n",(double)*mfqP->xmin,(double)mfqP->c1);
914: affpoints(mfqP,mfqP->xmin,mfqP->c1);
915: if (mfqP->nmodelpoints == mfqP->n) {
916: valid = PETSC_TRUE;
917: } else {
918: valid = PETSC_FALSE;
919: PetscInfo2(tao,"Affine Points: xmin = %20.19e, c2 = %20.19e\n",(double)*mfqP->xmin,(double)mfqP->c2);
920: affpoints(mfqP,mfqP->xmin,mfqP->c2);
921: if (mfqP->n > mfqP->nmodelpoints) {
922: PetscInfo(tao,"Model not valid -- adding geometry points\n");
923: modelimprove(tao,mfqP,mfqP->n - mfqP->nmodelpoints);
924: }
925: }
926: for (i=mfqP->nmodelpoints;i>0;i--) {
927: mfqP->model_indices[i] = mfqP->model_indices[i-1];
928: }
929: mfqP->nmodelpoints++;
930: mfqP->model_indices[0] = mfqP->minindex;
931: morepoints(mfqP);
932: for (i=0;i<mfqP->nmodelpoints;i++) {
933: VecGetArray(mfqP->Xhist[mfqP->model_indices[i]],&x);
934: for (j=0;j<mfqP->n;j++) {
935: mfqP->Disp[i + mfqP->npmax*j] = (x[j] - mfqP->xmin[j]) / deltaold;
936: }
937: VecRestoreArray(mfqP->Xhist[mfqP->model_indices[i]],&x);
938: VecGetArray(mfqP->Fhist[mfqP->model_indices[i]],&f);
939: for (j=0;j<mfqP->m;j++) {
940: for (k=0;k<mfqP->n;k++) {
941: mfqP->work[k]=0.0;
942: for (l=0;l<mfqP->n;l++) {
943: mfqP->work[k] += mfqP->H[j + mfqP->m*(k + mfqP->n*l)] * mfqP->Disp[i + mfqP->npmax*l];
944: }
945: }
946: PetscStackCallBLAS("BLASdot",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]);
947: }
948: VecRestoreArray(mfqP->Fhist[mfqP->model_indices[i]],&f);
949: }
951: /* Update the quadratic model */
952: PetscInfo2(tao,"Get Quad, size: %D, points: %D\n",mfqP->n,mfqP->nmodelpoints);
953: getquadpounders(mfqP);
954: VecGetArrayRead(mfqP->Fhist[mfqP->minindex],&fmin);
955: PetscStackCallBLAS("BLAScopy",BLAScopy_(&blasm,fmin,&ione,mfqP->C,&ione));
956: /* G = G*(delta/deltaold) + Gdel */
957: ratio = mfqP->delta/deltaold;
958: iblas = blasm*blasn;
959: PetscStackCallBLAS("BLASscal",BLASscal_(&iblas,&ratio,mfqP->Fdiff,&ione));
960: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&iblas,&one,mfqP->Gdel,&ione,mfqP->Fdiff,&ione));
961: /* H = H*(delta/deltaold)^2 + Hdel */
962: iblas = blasm*blasn*blasn;
963: ratio *= ratio;
964: PetscStackCallBLAS("BLASscal",BLASscal_(&iblas,&ratio,mfqP->H,&ione));
965: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&iblas,&one,mfqP->Hdel,&ione,mfqP->H,&ione));
967: /* Get residuals */
968: pounders_update_res(tao);
970: /* Export solution and gradient residual to TAO */
971: VecCopy(mfqP->Xhist[mfqP->minindex],tao->solution);
972: VecSetValues(tao->gradient,mfqP->n,mfqP->indices,mfqP->Gres,INSERT_VALUES);
973: VecAssemblyBegin(tao->gradient);
974: VecAssemblyEnd(tao->gradient);
975: VecNorm(tao->gradient,NORM_2,&gnorm);
976: gnorm *= mfqP->delta;
977: /* final criticality test */
978: TaoLogConvergenceHistory(tao,minnorm,gnorm,0.0,tao->ksp_its);
979: TaoMonitor(tao,tao->niter,minnorm,gnorm,0.0,step);
980: (*tao->ops->convergencetest)(tao,tao->cnvP);
981: /* test for repeated model */
982: if (mfqP->nmodelpoints==mfqP->last_nmodelpoints) {
983: same = PETSC_TRUE;
984: } else {
985: same = PETSC_FALSE;
986: }
987: for (i=0;i<mfqP->nmodelpoints;i++) {
988: if (same) {
989: if (mfqP->model_indices[i] == mfqP->last_model_indices[i]) {
990: same = PETSC_TRUE;
991: } else {
992: same = PETSC_FALSE;
993: }
994: }
995: mfqP->last_model_indices[i] = mfqP->model_indices[i];
996: }
997: mfqP->last_nmodelpoints = mfqP->nmodelpoints;
998: if (same && mfqP->delta == deltaold) {
999: PetscInfo(tao,"Identical model used in successive iterations\n");
1000: tao->reason = TAO_CONVERGED_STEPTOL;
1001: }
1002: }
1003: return(0);
1004: }
1006: static PetscErrorCode TaoSetUp_POUNDERS(Tao tao)
1007: {
1008: TAO_POUNDERS *mfqP = (TAO_POUNDERS*)tao->data;
1009: PetscInt i,j;
1010: IS isfloc,isfglob,isxloc,isxglob;
1014: if (!tao->gradient) {VecDuplicate(tao->solution,&tao->gradient); }
1015: if (!tao->stepdirection) {VecDuplicate(tao->solution,&tao->stepdirection); }
1016: VecGetSize(tao->solution,&mfqP->n);
1017: VecGetSize(tao->ls_res,&mfqP->m);
1018: mfqP->c1 = PetscSqrtReal((PetscReal)mfqP->n);
1019: if (mfqP->npmax == PETSC_DEFAULT) {
1020: mfqP->npmax = 2*mfqP->n + 1;
1021: }
1022: mfqP->npmax = PetscMin((mfqP->n+1)*(mfqP->n+2)/2,mfqP->npmax);
1023: mfqP->npmax = PetscMax(mfqP->npmax, mfqP->n+2);
1025: PetscMalloc1(tao->max_funcs+100,&mfqP->Xhist);
1026: PetscMalloc1(tao->max_funcs+100,&mfqP->Fhist);
1027: for (i=0;i<mfqP->n+1;i++) {
1028: VecDuplicate(tao->solution,&mfqP->Xhist[i]);
1029: VecDuplicate(tao->ls_res,&mfqP->Fhist[i]);
1030: }
1031: VecDuplicate(tao->solution,&mfqP->workxvec);
1032: VecDuplicate(tao->ls_res,&mfqP->workfvec);
1033: mfqP->nHist = 0;
1035: PetscMalloc1(tao->max_funcs+100,&mfqP->Fres);
1036: PetscMalloc1(mfqP->npmax*mfqP->m,&mfqP->RES);
1037: PetscMalloc1(mfqP->n,&mfqP->work);
1038: PetscMalloc1(mfqP->n,&mfqP->work2);
1039: PetscMalloc1(mfqP->n,&mfqP->work3);
1040: PetscMalloc1(PetscMax(mfqP->m,mfqP->n+1),&mfqP->mwork);
1041: PetscMalloc1(mfqP->npmax - mfqP->n - 1,&mfqP->omega);
1042: PetscMalloc1(mfqP->n * (mfqP->n+1) / 2,&mfqP->beta);
1043: PetscMalloc1(mfqP->n + 1 ,&mfqP->alpha);
1045: PetscMalloc1(mfqP->n*mfqP->n*mfqP->m,&mfqP->H);
1046: PetscMalloc1(mfqP->npmax*mfqP->npmax,&mfqP->Q);
1047: PetscMalloc1(mfqP->npmax*mfqP->npmax,&mfqP->Q_tmp);
1048: PetscMalloc1(mfqP->n*(mfqP->n+1)/2*(mfqP->npmax),&mfqP->L);
1049: PetscMalloc1(mfqP->n*(mfqP->n+1)/2*(mfqP->npmax),&mfqP->L_tmp);
1050: PetscMalloc1(mfqP->n*(mfqP->n+1)/2*(mfqP->npmax),&mfqP->L_save);
1051: PetscMalloc1(mfqP->n*(mfqP->n+1)/2*(mfqP->npmax),&mfqP->N);
1052: PetscMalloc1(mfqP->npmax * (mfqP->n+1) ,&mfqP->M);
1053: PetscMalloc1(mfqP->npmax * (mfqP->npmax - mfqP->n - 1) , &mfqP->Z);
1054: PetscMalloc1(mfqP->npmax,&mfqP->tau);
1055: PetscMalloc1(mfqP->npmax,&mfqP->tau_tmp);
1056: mfqP->nmax = PetscMax(5*mfqP->npmax,mfqP->n*(mfqP->n+1)/2);
1057: PetscMalloc1(mfqP->nmax,&mfqP->npmaxwork);
1058: PetscMalloc1(mfqP->nmax,&mfqP->npmaxiwork);
1059: PetscMalloc1(mfqP->n,&mfqP->xmin);
1060: PetscMalloc1(mfqP->m,&mfqP->C);
1061: PetscMalloc1(mfqP->m*mfqP->n,&mfqP->Fdiff);
1062: PetscMalloc1(mfqP->npmax*mfqP->n,&mfqP->Disp);
1063: PetscMalloc1(mfqP->n,&mfqP->Gres);
1064: PetscMalloc1(mfqP->n*mfqP->n,&mfqP->Hres);
1065: PetscMalloc1(mfqP->n*mfqP->n,&mfqP->Gpoints);
1066: PetscMalloc1(mfqP->npmax,&mfqP->model_indices);
1067: PetscMalloc1(mfqP->npmax,&mfqP->last_model_indices);
1068: PetscMalloc1(mfqP->n,&mfqP->Xsubproblem);
1069: PetscMalloc1(mfqP->m*mfqP->n,&mfqP->Gdel);
1070: PetscMalloc1(mfqP->n*mfqP->n*mfqP->m, &mfqP->Hdel);
1071: PetscMalloc1(PetscMax(mfqP->m,mfqP->n),&mfqP->indices);
1072: PetscMalloc1(mfqP->n,&mfqP->iwork);
1073: PetscMalloc1(mfqP->m*mfqP->m,&mfqP->w);
1074: for (i=0;i<mfqP->m;i++) {
1075: for (j=0;j<mfqP->m;j++) {
1076: if (i==j) {
1077: mfqP->w[i+mfqP->m*j]=1.0;
1078: } else {
1079: mfqP->w[i+mfqP->m*j]=0.0;
1080: }
1081: }
1082: }
1083: for (i=0;i<PetscMax(mfqP->m,mfqP->n);i++) {
1084: mfqP->indices[i] = i;
1085: }
1086: MPI_Comm_size(((PetscObject)tao)->comm,&mfqP->size);
1087: if (mfqP->size > 1) {
1088: VecCreateSeq(PETSC_COMM_SELF,mfqP->n,&mfqP->localx);
1089: VecCreateSeq(PETSC_COMM_SELF,mfqP->n,&mfqP->localxmin);
1090: VecCreateSeq(PETSC_COMM_SELF,mfqP->m,&mfqP->localf);
1091: VecCreateSeq(PETSC_COMM_SELF,mfqP->m,&mfqP->localfmin);
1092: ISCreateStride(MPI_COMM_SELF,mfqP->n,0,1,&isxloc);
1093: ISCreateStride(MPI_COMM_SELF,mfqP->n,0,1,&isxglob);
1094: ISCreateStride(MPI_COMM_SELF,mfqP->m,0,1,&isfloc);
1095: ISCreateStride(MPI_COMM_SELF,mfqP->m,0,1,&isfglob);
1097: VecScatterCreate(tao->solution,isxglob,mfqP->localx,isxloc,&mfqP->scatterx);
1098: VecScatterCreate(tao->ls_res,isfglob,mfqP->localf,isfloc,&mfqP->scatterf);
1100: ISDestroy(&isxloc);
1101: ISDestroy(&isxglob);
1102: ISDestroy(&isfloc);
1103: ISDestroy(&isfglob);
1104: }
1106: if (!mfqP->usegqt) {
1107: KSP ksp;
1108: PC pc;
1109: VecCreateSeqWithArray(PETSC_COMM_SELF,mfqP->n,mfqP->n,mfqP->Xsubproblem,&mfqP->subx);
1110: VecCreateSeq(PETSC_COMM_SELF,mfqP->n,&mfqP->subxl);
1111: VecDuplicate(mfqP->subxl,&mfqP->subb);
1112: VecDuplicate(mfqP->subxl,&mfqP->subxu);
1113: VecDuplicate(mfqP->subxl,&mfqP->subpdel);
1114: VecDuplicate(mfqP->subxl,&mfqP->subndel);
1115: TaoCreate(PETSC_COMM_SELF,&mfqP->subtao);
1116: PetscObjectIncrementTabLevel((PetscObject)mfqP->subtao, (PetscObject)tao, 1);
1117: TaoSetType(mfqP->subtao,TAOBNTR);
1118: TaoSetOptionsPrefix(mfqP->subtao,"pounders_subsolver_");
1119: TaoSetInitialVector(mfqP->subtao,mfqP->subx);
1120: TaoSetObjectiveAndGradientRoutine(mfqP->subtao,pounders_fg,(void*)mfqP);
1121: TaoSetMaximumIterations(mfqP->subtao,mfqP->gqt_maxits);
1122: TaoSetFromOptions(mfqP->subtao);
1123: TaoGetKSP(mfqP->subtao,&ksp);
1124: if (ksp) {
1125: KSPGetPC(ksp,&pc);
1126: PCSetType(pc,PCNONE);
1127: }
1128: TaoSetVariableBounds(mfqP->subtao,mfqP->subxl,mfqP->subxu);
1129: MatCreateSeqDense(PETSC_COMM_SELF,mfqP->n,mfqP->n,mfqP->Hres,&mfqP->subH);
1130: TaoSetHessianRoutine(mfqP->subtao,mfqP->subH,mfqP->subH,pounders_h,(void*)mfqP);
1131: }
1132: return(0);
1133: }
1135: static PetscErrorCode TaoDestroy_POUNDERS(Tao tao)
1136: {
1137: TAO_POUNDERS *mfqP = (TAO_POUNDERS*)tao->data;
1138: PetscInt i;
1142: if (!mfqP->usegqt) {
1143: TaoDestroy(&mfqP->subtao);
1144: VecDestroy(&mfqP->subx);
1145: VecDestroy(&mfqP->subxl);
1146: VecDestroy(&mfqP->subxu);
1147: VecDestroy(&mfqP->subb);
1148: VecDestroy(&mfqP->subpdel);
1149: VecDestroy(&mfqP->subndel);
1150: MatDestroy(&mfqP->subH);
1151: }
1152: PetscFree(mfqP->Fres);
1153: PetscFree(mfqP->RES);
1154: PetscFree(mfqP->work);
1155: PetscFree(mfqP->work2);
1156: PetscFree(mfqP->work3);
1157: PetscFree(mfqP->mwork);
1158: PetscFree(mfqP->omega);
1159: PetscFree(mfqP->beta);
1160: PetscFree(mfqP->alpha);
1161: PetscFree(mfqP->H);
1162: PetscFree(mfqP->Q);
1163: PetscFree(mfqP->Q_tmp);
1164: PetscFree(mfqP->L);
1165: PetscFree(mfqP->L_tmp);
1166: PetscFree(mfqP->L_save);
1167: PetscFree(mfqP->N);
1168: PetscFree(mfqP->M);
1169: PetscFree(mfqP->Z);
1170: PetscFree(mfqP->tau);
1171: PetscFree(mfqP->tau_tmp);
1172: PetscFree(mfqP->npmaxwork);
1173: PetscFree(mfqP->npmaxiwork);
1174: PetscFree(mfqP->xmin);
1175: PetscFree(mfqP->C);
1176: PetscFree(mfqP->Fdiff);
1177: PetscFree(mfqP->Disp);
1178: PetscFree(mfqP->Gres);
1179: PetscFree(mfqP->Hres);
1180: PetscFree(mfqP->Gpoints);
1181: PetscFree(mfqP->model_indices);
1182: PetscFree(mfqP->last_model_indices);
1183: PetscFree(mfqP->Xsubproblem);
1184: PetscFree(mfqP->Gdel);
1185: PetscFree(mfqP->Hdel);
1186: PetscFree(mfqP->indices);
1187: PetscFree(mfqP->iwork);
1188: PetscFree(mfqP->w);
1189: for (i=0;i<mfqP->nHist;i++) {
1190: VecDestroy(&mfqP->Xhist[i]);
1191: VecDestroy(&mfqP->Fhist[i]);
1192: }
1193: VecDestroy(&mfqP->workxvec);
1194: VecDestroy(&mfqP->workfvec);
1195: PetscFree(mfqP->Xhist);
1196: PetscFree(mfqP->Fhist);
1198: if (mfqP->size > 1) {
1199: VecDestroy(&mfqP->localx);
1200: VecDestroy(&mfqP->localxmin);
1201: VecDestroy(&mfqP->localf);
1202: VecDestroy(&mfqP->localfmin);
1203: }
1204: PetscFree(tao->data);
1205: return(0);
1206: }
1208: static PetscErrorCode TaoSetFromOptions_POUNDERS(PetscOptionItems *PetscOptionsObject,Tao tao)
1209: {
1210: TAO_POUNDERS *mfqP = (TAO_POUNDERS*)tao->data;
1214: PetscOptionsHead(PetscOptionsObject,"POUNDERS method for least-squares optimization");
1215: PetscOptionsReal("-tao_pounders_delta","initial delta","",mfqP->delta,&mfqP->delta0,NULL);
1216: mfqP->delta = mfqP->delta0;
1217: PetscOptionsInt("-tao_pounders_npmax","max number of points in model","",mfqP->npmax,&mfqP->npmax,NULL);
1218: PetscOptionsBool("-tao_pounders_gqt","use gqt algorithm for subproblem","",mfqP->usegqt,&mfqP->usegqt,NULL);
1219: PetscOptionsTail();
1220: return(0);
1221: }
1223: static PetscErrorCode TaoView_POUNDERS(Tao tao, PetscViewer viewer)
1224: {
1225: TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;
1226: PetscBool isascii;
1230: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
1231: if (isascii) {
1232: PetscViewerASCIIPrintf(viewer, "initial delta: %g\n",(double)mfqP->delta0);
1233: PetscViewerASCIIPrintf(viewer, "final delta: %g\n",(double)mfqP->delta);
1234: PetscViewerASCIIPrintf(viewer, "model points: %D\n",mfqP->nmodelpoints);
1235: if (mfqP->usegqt) {
1236: PetscViewerASCIIPrintf(viewer, "subproblem solver: gqt\n");
1237: } else {
1238: TaoView(mfqP->subtao, viewer);
1239: }
1240: }
1241: return(0);
1242: }
1243: /*MC
1244: TAOPOUNDERS - POUNDERS derivate-free model-based algorithm for nonlinear least squares
1246: Options Database Keys:
1247: + -tao_pounders_delta - initial step length
1248: . -tao_pounders_npmax - maximum number of points in model
1249: - -tao_pounders_gqt - use gqt algorithm for subproblem instead of TRON
1251: Level: beginner
1253: M*/
1255: PETSC_EXTERN PetscErrorCode TaoCreate_POUNDERS(Tao tao)
1256: {
1257: TAO_POUNDERS *mfqP = (TAO_POUNDERS*)tao->data;
1261: tao->ops->setup = TaoSetUp_POUNDERS;
1262: tao->ops->solve = TaoSolve_POUNDERS;
1263: tao->ops->view = TaoView_POUNDERS;
1264: tao->ops->setfromoptions = TaoSetFromOptions_POUNDERS;
1265: tao->ops->destroy = TaoDestroy_POUNDERS;
1267: PetscNewLog(tao,&mfqP);
1268: tao->data = (void*)mfqP;
1269: /* Override default settings (unless already changed) */
1270: if (!tao->max_it_changed) tao->max_it = 2000;
1271: if (!tao->max_funcs_changed) tao->max_funcs = 4000;
1272: mfqP->npmax = PETSC_DEFAULT;
1273: mfqP->delta0 = 0.1;
1274: mfqP->delta = 0.1;
1275: mfqP->deltamax=1e3;
1276: mfqP->deltamin=1e-6;
1277: mfqP->c2 = 10.0;
1278: mfqP->theta1=1e-5;
1279: mfqP->theta2=1e-4;
1280: mfqP->gamma0=0.5;
1281: mfqP->gamma1=2.0;
1282: mfqP->eta0 = 0.0;
1283: mfqP->eta1 = 0.1;
1284: mfqP->usegqt = PETSC_FALSE;
1285: mfqP->gqt_rtol = 0.001;
1286: mfqP->gqt_maxits = 50;
1287: mfqP->workxvec = NULL;
1288: return(0);
1289: }