Actual source code: chwirut2.c
1: /*
2: Include "petsctao.h" so that we can use TAO solvers. Note that this
3: file automatically includes libraries such as:
4: petsc.h - base PETSc routines petscvec.h - vectors
5: petscsys.h - system routines petscmat.h - matrices
6: petscis.h - index sets petscksp.h - Krylov subspace methods
7: petscviewer.h - viewers petscpc.h - preconditioners
9: */
11: #include <petsctao.h>
13: /*
14: Description: These data are the result of a NIST study involving
15: ultrasonic calibration. The response variable is
16: ultrasonic response, and the predictor variable is
17: metal distance.
19: Reference: Chwirut, D., NIST (197?).
20: Ultrasonic Reference Block Study.
21: */
23: static char help[] = "Finds the nonlinear least-squares solution to the model \n\
24: y = exp[-b1*x]/(b2+b3*x) + e \n";
26: #define NOBSERVATIONS 214
27: #define NPARAMETERS 3
29: #define DIE_TAG 2000
30: #define IDLE_TAG 1000
32: /* User-defined application context */
33: typedef struct {
34: /* Working space */
35: PetscReal t[NOBSERVATIONS]; /* array of independent variables of observation */
36: PetscReal y[NOBSERVATIONS]; /* array of dependent variables */
37: PetscMPIInt size, rank;
38: } AppCtx;
40: /* User provided Routines */
41: PetscErrorCode InitializeData(AppCtx *user);
42: PetscErrorCode FormStartingPoint(Vec);
43: PetscErrorCode EvaluateFunction(Tao, Vec, Vec, void *);
44: PetscErrorCode TaskWorker(AppCtx *user);
45: PetscErrorCode StopWorkers(AppCtx *user);
46: PetscErrorCode RunSimulation(PetscReal *x, PetscInt i, PetscReal *f, AppCtx *user);
48: /*--------------------------------------------------------------------*/
49: int main(int argc, char **argv)
50: {
51: Vec x, f; /* solution, function */
52: Tao tao; /* Tao solver context */
53: AppCtx user; /* user-defined work context */
55: /* Initialize TAO and PETSc */
57: PetscInitialize(&argc, &argv, (char *)0, help);
58: MPI_Comm_size(MPI_COMM_WORLD, &user.size);
59: MPI_Comm_rank(MPI_COMM_WORLD, &user.rank);
60: InitializeData(&user);
62: /* Run optimization on rank 0 */
63: if (user.rank == 0) {
64: /* Allocate vectors */
65: VecCreateSeq(PETSC_COMM_SELF, NPARAMETERS, &x);
66: VecCreateSeq(PETSC_COMM_SELF, NOBSERVATIONS, &f);
68: /* TAO code begins here */
70: /* Create TAO solver and set desired solution method */
71: TaoCreate(PETSC_COMM_SELF, &tao);
72: TaoSetType(tao, TAOPOUNDERS);
74: /* Set the function and Jacobian routines. */
75: FormStartingPoint(x);
76: TaoSetSolution(tao, x);
77: TaoSetResidualRoutine(tao, f, EvaluateFunction, (void *)&user);
79: /* Check for any TAO command line arguments */
80: TaoSetFromOptions(tao);
82: /* Perform the Solve */
83: TaoSolve(tao);
85: /* Free TAO data structures */
86: TaoDestroy(&tao);
88: /* Free PETSc data structures */
89: VecDestroy(&x);
90: VecDestroy(&f);
91: StopWorkers(&user);
92: } else {
93: TaskWorker(&user);
94: }
95: PetscFinalize();
96: return 0;
97: }
99: /*--------------------------------------------------------------------*/
100: PetscErrorCode EvaluateFunction(Tao tao, Vec X, Vec F, void *ptr)
101: {
102: AppCtx *user = (AppCtx *)ptr;
103: PetscInt i;
104: PetscReal *x, *f;
106: VecGetArray(X, &x);
107: VecGetArray(F, &f);
108: if (user->size == 1) {
109: /* Single processor */
110: for (i = 0; i < NOBSERVATIONS; i++) RunSimulation(x, i, &f[i], user);
111: } else {
112: /* Multiprocessor main */
113: PetscMPIInt tag;
114: PetscInt finishedtasks, next_task, checkedin;
115: PetscReal f_i = 0.0;
116: MPI_Status status;
118: next_task = 0;
119: finishedtasks = 0;
120: checkedin = 0;
122: while (finishedtasks < NOBSERVATIONS || checkedin < user->size - 1) {
123: MPI_Recv(&f_i, 1, MPIU_REAL, MPI_ANY_SOURCE, MPI_ANY_TAG, PETSC_COMM_WORLD, &status);
124: if (status.MPI_TAG == IDLE_TAG) {
125: checkedin++;
126: } else {
127: tag = status.MPI_TAG;
128: f[tag] = (PetscReal)f_i;
129: finishedtasks++;
130: }
132: if (next_task < NOBSERVATIONS) {
133: MPI_Send(x, NPARAMETERS, MPIU_REAL, status.MPI_SOURCE, next_task, PETSC_COMM_WORLD);
134: next_task++;
136: } else {
137: /* Send idle message */
138: MPI_Send(x, NPARAMETERS, MPIU_REAL, status.MPI_SOURCE, IDLE_TAG, PETSC_COMM_WORLD);
139: }
140: }
141: }
142: VecRestoreArray(X, &x);
143: VecRestoreArray(F, &f);
144: PetscLogFlops(6 * NOBSERVATIONS);
145: return 0;
146: }
148: /* ------------------------------------------------------------ */
149: PetscErrorCode FormStartingPoint(Vec X)
150: {
151: PetscReal *x;
153: VecGetArray(X, &x);
154: x[0] = 0.15;
155: x[1] = 0.008;
156: x[2] = 0.010;
157: VecRestoreArray(X, &x);
158: return 0;
159: }
161: /* ---------------------------------------------------------------------- */
162: PetscErrorCode InitializeData(AppCtx *user)
163: {
164: PetscReal *t = user->t, *y = user->y;
165: PetscInt i = 0;
167: y[i] = 92.9000;
168: t[i++] = 0.5000;
169: y[i] = 78.7000;
170: t[i++] = 0.6250;
171: y[i] = 64.2000;
172: t[i++] = 0.7500;
173: y[i] = 64.9000;
174: t[i++] = 0.8750;
175: y[i] = 57.1000;
176: t[i++] = 1.0000;
177: y[i] = 43.3000;
178: t[i++] = 1.2500;
179: y[i] = 31.1000;
180: t[i++] = 1.7500;
181: y[i] = 23.6000;
182: t[i++] = 2.2500;
183: y[i] = 31.0500;
184: t[i++] = 1.7500;
185: y[i] = 23.7750;
186: t[i++] = 2.2500;
187: y[i] = 17.7375;
188: t[i++] = 2.7500;
189: y[i] = 13.8000;
190: t[i++] = 3.2500;
191: y[i] = 11.5875;
192: t[i++] = 3.7500;
193: y[i] = 9.4125;
194: t[i++] = 4.2500;
195: y[i] = 7.7250;
196: t[i++] = 4.7500;
197: y[i] = 7.3500;
198: t[i++] = 5.2500;
199: y[i] = 8.0250;
200: t[i++] = 5.7500;
201: y[i] = 90.6000;
202: t[i++] = 0.5000;
203: y[i] = 76.9000;
204: t[i++] = 0.6250;
205: y[i] = 71.6000;
206: t[i++] = 0.7500;
207: y[i] = 63.6000;
208: t[i++] = 0.8750;
209: y[i] = 54.0000;
210: t[i++] = 1.0000;
211: y[i] = 39.2000;
212: t[i++] = 1.2500;
213: y[i] = 29.3000;
214: t[i++] = 1.7500;
215: y[i] = 21.4000;
216: t[i++] = 2.2500;
217: y[i] = 29.1750;
218: t[i++] = 1.7500;
219: y[i] = 22.1250;
220: t[i++] = 2.2500;
221: y[i] = 17.5125;
222: t[i++] = 2.7500;
223: y[i] = 14.2500;
224: t[i++] = 3.2500;
225: y[i] = 9.4500;
226: t[i++] = 3.7500;
227: y[i] = 9.1500;
228: t[i++] = 4.2500;
229: y[i] = 7.9125;
230: t[i++] = 4.7500;
231: y[i] = 8.4750;
232: t[i++] = 5.2500;
233: y[i] = 6.1125;
234: t[i++] = 5.7500;
235: y[i] = 80.0000;
236: t[i++] = 0.5000;
237: y[i] = 79.0000;
238: t[i++] = 0.6250;
239: y[i] = 63.8000;
240: t[i++] = 0.7500;
241: y[i] = 57.2000;
242: t[i++] = 0.8750;
243: y[i] = 53.2000;
244: t[i++] = 1.0000;
245: y[i] = 42.5000;
246: t[i++] = 1.2500;
247: y[i] = 26.8000;
248: t[i++] = 1.7500;
249: y[i] = 20.4000;
250: t[i++] = 2.2500;
251: y[i] = 26.8500;
252: t[i++] = 1.7500;
253: y[i] = 21.0000;
254: t[i++] = 2.2500;
255: y[i] = 16.4625;
256: t[i++] = 2.7500;
257: y[i] = 12.5250;
258: t[i++] = 3.2500;
259: y[i] = 10.5375;
260: t[i++] = 3.7500;
261: y[i] = 8.5875;
262: t[i++] = 4.2500;
263: y[i] = 7.1250;
264: t[i++] = 4.7500;
265: y[i] = 6.1125;
266: t[i++] = 5.2500;
267: y[i] = 5.9625;
268: t[i++] = 5.7500;
269: y[i] = 74.1000;
270: t[i++] = 0.5000;
271: y[i] = 67.3000;
272: t[i++] = 0.6250;
273: y[i] = 60.8000;
274: t[i++] = 0.7500;
275: y[i] = 55.5000;
276: t[i++] = 0.8750;
277: y[i] = 50.3000;
278: t[i++] = 1.0000;
279: y[i] = 41.0000;
280: t[i++] = 1.2500;
281: y[i] = 29.4000;
282: t[i++] = 1.7500;
283: y[i] = 20.4000;
284: t[i++] = 2.2500;
285: y[i] = 29.3625;
286: t[i++] = 1.7500;
287: y[i] = 21.1500;
288: t[i++] = 2.2500;
289: y[i] = 16.7625;
290: t[i++] = 2.7500;
291: y[i] = 13.2000;
292: t[i++] = 3.2500;
293: y[i] = 10.8750;
294: t[i++] = 3.7500;
295: y[i] = 8.1750;
296: t[i++] = 4.2500;
297: y[i] = 7.3500;
298: t[i++] = 4.7500;
299: y[i] = 5.9625;
300: t[i++] = 5.2500;
301: y[i] = 5.6250;
302: t[i++] = 5.7500;
303: y[i] = 81.5000;
304: t[i++] = .5000;
305: y[i] = 62.4000;
306: t[i++] = .7500;
307: y[i] = 32.5000;
308: t[i++] = 1.5000;
309: y[i] = 12.4100;
310: t[i++] = 3.0000;
311: y[i] = 13.1200;
312: t[i++] = 3.0000;
313: y[i] = 15.5600;
314: t[i++] = 3.0000;
315: y[i] = 5.6300;
316: t[i++] = 6.0000;
317: y[i] = 78.0000;
318: t[i++] = .5000;
319: y[i] = 59.9000;
320: t[i++] = .7500;
321: y[i] = 33.2000;
322: t[i++] = 1.5000;
323: y[i] = 13.8400;
324: t[i++] = 3.0000;
325: y[i] = 12.7500;
326: t[i++] = 3.0000;
327: y[i] = 14.6200;
328: t[i++] = 3.0000;
329: y[i] = 3.9400;
330: t[i++] = 6.0000;
331: y[i] = 76.8000;
332: t[i++] = .5000;
333: y[i] = 61.0000;
334: t[i++] = .7500;
335: y[i] = 32.9000;
336: t[i++] = 1.5000;
337: y[i] = 13.8700;
338: t[i++] = 3.0000;
339: y[i] = 11.8100;
340: t[i++] = 3.0000;
341: y[i] = 13.3100;
342: t[i++] = 3.0000;
343: y[i] = 5.4400;
344: t[i++] = 6.0000;
345: y[i] = 78.0000;
346: t[i++] = .5000;
347: y[i] = 63.5000;
348: t[i++] = .7500;
349: y[i] = 33.8000;
350: t[i++] = 1.5000;
351: y[i] = 12.5600;
352: t[i++] = 3.0000;
353: y[i] = 5.6300;
354: t[i++] = 6.0000;
355: y[i] = 12.7500;
356: t[i++] = 3.0000;
357: y[i] = 13.1200;
358: t[i++] = 3.0000;
359: y[i] = 5.4400;
360: t[i++] = 6.0000;
361: y[i] = 76.8000;
362: t[i++] = .5000;
363: y[i] = 60.0000;
364: t[i++] = .7500;
365: y[i] = 47.8000;
366: t[i++] = 1.0000;
367: y[i] = 32.0000;
368: t[i++] = 1.5000;
369: y[i] = 22.2000;
370: t[i++] = 2.0000;
371: y[i] = 22.5700;
372: t[i++] = 2.0000;
373: y[i] = 18.8200;
374: t[i++] = 2.5000;
375: y[i] = 13.9500;
376: t[i++] = 3.0000;
377: y[i] = 11.2500;
378: t[i++] = 4.0000;
379: y[i] = 9.0000;
380: t[i++] = 5.0000;
381: y[i] = 6.6700;
382: t[i++] = 6.0000;
383: y[i] = 75.8000;
384: t[i++] = .5000;
385: y[i] = 62.0000;
386: t[i++] = .7500;
387: y[i] = 48.8000;
388: t[i++] = 1.0000;
389: y[i] = 35.2000;
390: t[i++] = 1.5000;
391: y[i] = 20.0000;
392: t[i++] = 2.0000;
393: y[i] = 20.3200;
394: t[i++] = 2.0000;
395: y[i] = 19.3100;
396: t[i++] = 2.5000;
397: y[i] = 12.7500;
398: t[i++] = 3.0000;
399: y[i] = 10.4200;
400: t[i++] = 4.0000;
401: y[i] = 7.3100;
402: t[i++] = 5.0000;
403: y[i] = 7.4200;
404: t[i++] = 6.0000;
405: y[i] = 70.5000;
406: t[i++] = .5000;
407: y[i] = 59.5000;
408: t[i++] = .7500;
409: y[i] = 48.5000;
410: t[i++] = 1.0000;
411: y[i] = 35.8000;
412: t[i++] = 1.5000;
413: y[i] = 21.0000;
414: t[i++] = 2.0000;
415: y[i] = 21.6700;
416: t[i++] = 2.0000;
417: y[i] = 21.0000;
418: t[i++] = 2.5000;
419: y[i] = 15.6400;
420: t[i++] = 3.0000;
421: y[i] = 8.1700;
422: t[i++] = 4.0000;
423: y[i] = 8.5500;
424: t[i++] = 5.0000;
425: y[i] = 10.1200;
426: t[i++] = 6.0000;
427: y[i] = 78.0000;
428: t[i++] = .5000;
429: y[i] = 66.0000;
430: t[i++] = .6250;
431: y[i] = 62.0000;
432: t[i++] = .7500;
433: y[i] = 58.0000;
434: t[i++] = .8750;
435: y[i] = 47.7000;
436: t[i++] = 1.0000;
437: y[i] = 37.8000;
438: t[i++] = 1.2500;
439: y[i] = 20.2000;
440: t[i++] = 2.2500;
441: y[i] = 21.0700;
442: t[i++] = 2.2500;
443: y[i] = 13.8700;
444: t[i++] = 2.7500;
445: y[i] = 9.6700;
446: t[i++] = 3.2500;
447: y[i] = 7.7600;
448: t[i++] = 3.7500;
449: y[i] = 5.4400;
450: t[i++] = 4.2500;
451: y[i] = 4.8700;
452: t[i++] = 4.7500;
453: y[i] = 4.0100;
454: t[i++] = 5.2500;
455: y[i] = 3.7500;
456: t[i++] = 5.7500;
457: y[i] = 24.1900;
458: t[i++] = 3.0000;
459: y[i] = 25.7600;
460: t[i++] = 3.0000;
461: y[i] = 18.0700;
462: t[i++] = 3.0000;
463: y[i] = 11.8100;
464: t[i++] = 3.0000;
465: y[i] = 12.0700;
466: t[i++] = 3.0000;
467: y[i] = 16.1200;
468: t[i++] = 3.0000;
469: y[i] = 70.8000;
470: t[i++] = .5000;
471: y[i] = 54.7000;
472: t[i++] = .7500;
473: y[i] = 48.0000;
474: t[i++] = 1.0000;
475: y[i] = 39.8000;
476: t[i++] = 1.5000;
477: y[i] = 29.8000;
478: t[i++] = 2.0000;
479: y[i] = 23.7000;
480: t[i++] = 2.5000;
481: y[i] = 29.6200;
482: t[i++] = 2.0000;
483: y[i] = 23.8100;
484: t[i++] = 2.5000;
485: y[i] = 17.7000;
486: t[i++] = 3.0000;
487: y[i] = 11.5500;
488: t[i++] = 4.0000;
489: y[i] = 12.0700;
490: t[i++] = 5.0000;
491: y[i] = 8.7400;
492: t[i++] = 6.0000;
493: y[i] = 80.7000;
494: t[i++] = .5000;
495: y[i] = 61.3000;
496: t[i++] = .7500;
497: y[i] = 47.5000;
498: t[i++] = 1.0000;
499: y[i] = 29.0000;
500: t[i++] = 1.5000;
501: y[i] = 24.0000;
502: t[i++] = 2.0000;
503: y[i] = 17.7000;
504: t[i++] = 2.5000;
505: y[i] = 24.5600;
506: t[i++] = 2.0000;
507: y[i] = 18.6700;
508: t[i++] = 2.5000;
509: y[i] = 16.2400;
510: t[i++] = 3.0000;
511: y[i] = 8.7400;
512: t[i++] = 4.0000;
513: y[i] = 7.8700;
514: t[i++] = 5.0000;
515: y[i] = 8.5100;
516: t[i++] = 6.0000;
517: y[i] = 66.7000;
518: t[i++] = .5000;
519: y[i] = 59.2000;
520: t[i++] = .7500;
521: y[i] = 40.8000;
522: t[i++] = 1.0000;
523: y[i] = 30.7000;
524: t[i++] = 1.5000;
525: y[i] = 25.7000;
526: t[i++] = 2.0000;
527: y[i] = 16.3000;
528: t[i++] = 2.5000;
529: y[i] = 25.9900;
530: t[i++] = 2.0000;
531: y[i] = 16.9500;
532: t[i++] = 2.5000;
533: y[i] = 13.3500;
534: t[i++] = 3.0000;
535: y[i] = 8.6200;
536: t[i++] = 4.0000;
537: y[i] = 7.2000;
538: t[i++] = 5.0000;
539: y[i] = 6.6400;
540: t[i++] = 6.0000;
541: y[i] = 13.6900;
542: t[i++] = 3.0000;
543: y[i] = 81.0000;
544: t[i++] = .5000;
545: y[i] = 64.5000;
546: t[i++] = .7500;
547: y[i] = 35.5000;
548: t[i++] = 1.5000;
549: y[i] = 13.3100;
550: t[i++] = 3.0000;
551: y[i] = 4.8700;
552: t[i++] = 6.0000;
553: y[i] = 12.9400;
554: t[i++] = 3.0000;
555: y[i] = 5.0600;
556: t[i++] = 6.0000;
557: y[i] = 15.1900;
558: t[i++] = 3.0000;
559: y[i] = 14.6200;
560: t[i++] = 3.0000;
561: y[i] = 15.6400;
562: t[i++] = 3.0000;
563: y[i] = 25.5000;
564: t[i++] = 1.7500;
565: y[i] = 25.9500;
566: t[i++] = 1.7500;
567: y[i] = 81.7000;
568: t[i++] = .5000;
569: y[i] = 61.6000;
570: t[i++] = .7500;
571: y[i] = 29.8000;
572: t[i++] = 1.7500;
573: y[i] = 29.8100;
574: t[i++] = 1.7500;
575: y[i] = 17.1700;
576: t[i++] = 2.7500;
577: y[i] = 10.3900;
578: t[i++] = 3.7500;
579: y[i] = 28.4000;
580: t[i++] = 1.7500;
581: y[i] = 28.6900;
582: t[i++] = 1.7500;
583: y[i] = 81.3000;
584: t[i++] = .5000;
585: y[i] = 60.9000;
586: t[i++] = .7500;
587: y[i] = 16.6500;
588: t[i++] = 2.7500;
589: y[i] = 10.0500;
590: t[i++] = 3.7500;
591: y[i] = 28.9000;
592: t[i++] = 1.7500;
593: y[i] = 28.9500;
594: t[i++] = 1.7500;
595: return 0;
596: }
598: PetscErrorCode TaskWorker(AppCtx *user)
599: {
600: PetscReal x[NPARAMETERS], f = 0.0;
601: PetscMPIInt tag = IDLE_TAG;
602: PetscInt index;
603: MPI_Status status;
605: /* Send check-in message to rank-0 */
607: MPI_Send(&f, 1, MPIU_REAL, 0, IDLE_TAG, PETSC_COMM_WORLD);
608: while (tag != DIE_TAG) {
609: MPI_Recv(x, NPARAMETERS, MPIU_REAL, 0, MPI_ANY_TAG, PETSC_COMM_WORLD, &status);
610: tag = status.MPI_TAG;
611: if (tag == IDLE_TAG) {
612: MPI_Send(&f, 1, MPIU_REAL, 0, IDLE_TAG, PETSC_COMM_WORLD);
613: } else if (tag != DIE_TAG) {
614: index = (PetscInt)tag;
615: RunSimulation(x, index, &f, user);
616: MPI_Send(&f, 1, MPIU_REAL, 0, tag, PETSC_COMM_WORLD);
617: }
618: }
619: return 0;
620: }
622: PetscErrorCode RunSimulation(PetscReal *x, PetscInt i, PetscReal *f, AppCtx *user)
623: {
624: PetscReal *t = user->t;
625: PetscReal *y = user->y;
626: #if defined(PETSC_USE_REAL_SINGLE)
627: *f = y[i] - exp(-x[0] * t[i]) / (x[1] + x[2] * t[i]); /* expf() for single-precision breaks this example on Freebsd, Valgrind errors on Linux */
628: #else
629: *f = y[i] - PetscExpScalar(-x[0] * t[i]) / (x[1] + x[2] * t[i]);
630: #endif
631: return (0);
632: }
634: PetscErrorCode StopWorkers(AppCtx *user)
635: {
636: PetscInt checkedin;
637: MPI_Status status;
638: PetscReal f, x[NPARAMETERS];
640: checkedin = 0;
641: while (checkedin < user->size - 1) {
642: MPI_Recv(&f, 1, MPIU_REAL, MPI_ANY_SOURCE, MPI_ANY_TAG, PETSC_COMM_WORLD, &status);
643: checkedin++;
644: PetscArrayzero(x, NPARAMETERS);
645: MPI_Send(x, NPARAMETERS, MPIU_REAL, status.MPI_SOURCE, DIE_TAG, PETSC_COMM_WORLD);
646: }
647: return 0;
648: }
650: /*TEST
652: build:
653: requires: !complex
655: test:
656: nsize: 3
657: requires: !single
658: args: -tao_smonitor -tao_max_it 100 -tao_type pounders -tao_gatol 1.e-5
660: TEST*/