Actual source code: taopetsc.c

  1: #include "zpetsc.h"
  2: #include "petscksp.h"
  3: #include "tao.h"


  6: #ifdef PETSC_HAVE_FORTRAN_CAPS

  8: #define taopetscapplicationcreate_     TAOPETSCAPPLICATIONCREATE
  9: #define taoapplicationcreate_          TAOAPPLICATIONCREATE
 10: #define taoapplicationdestroy_          TAOAPPLICATIONDESTROY
 11: #define taosetapplication_          TAOSETAPPLICATION

 13: #define taoappsetobjectiveandgradientroutine_    TAOAPPSETOBJECTIVEANDGRADIENTROUTINE
 14: #define taoappsetobjectiveandgradientro_         TAOAPPSETOBJECTIVEANDGRADIENTRO
 15: #define taoappsetobjectiveroutine_               TAOAPPSETOBJECTIVEROUTINE
 16: #define taoappsetgradientroutine_                TAOAPPSETGRADIENTROUTINE
 17: #define taoappsethessianroutine_                 TAOAPPSETHESSIANROUTINE
 18: #define taoappsetvariableboundsroutine_          TAOAPPSETVARIABLEBOUNDSROUTINE
 19: #define taoappsetjacobianroutine_                TAOAPPSETJACOBIANROUTINE
 20: #define taoappsetconstraintroutine_              TAOAPPSETCONSTRAINTROUTINE
 21: #define taoappsetmonitor_                        TAOAPPSETMONITOR
 22: #define taoappgetksp_                            TAOAPPGETKSP

 24: /* Grid application */
 25: #define daappsethessianroutine_                         DAAPPSETHESSIANROUTINE
 26: #define daappsetobjectiveandgradientroutine_            DAAPPSETOBJECTIVEANDGRADIENTROUTINE
 27: #define daappsetobjectiveandgradientrou_                DAAPPSETOBJECTIVEANDGRADIENTROU
 28: #define daappsetgradientroutine_                        DAAPPSETGRADIENTROUTINE
 29: #define daappsetobjectiveroutine_                       DAAPPSETOBJECTIVEROUTINE
 30: #define daappsetvariableboundsroutine_                  DAAPPSETVARIABLEBOUNDSROUTINE
 31: #define daappsetconstraintroutine_                      DAAPPSETCONSTRAINTROUTINE
 32: #define daappsetjacobianroutine_                        DAAPPSETJACOBIANROUTINE
 33: #define daappsetbeforemonitor_                          DAAPPSETBEFOREMONITOR
 34: #define daappsetaftermonitor_                           DAAPPSETAFTERMONITOR
 35: #define daappsetelementobjectiveandgradientroutine_     DAAPPSETELEMENTOBJECTIVEANDGRADIENTROUTINE
 36: #define daappsetelementobjectiveandgrad_                DAAPPSETELEMENTOBJECTIVEANDGRAD
 37: #define daappsetelementhessianroutine_                  DAAPPSETELEMENTHESSIANROUTINE


 40: #define taodefaultcomputehessian_   TAODEFAULTCOMPUTEHESSIAN
 41: #define taodefaultcomputehessiancolor_ TAODEFAULTCOMPUTEHESSIANCOLOR

 43: #define taogetksp_                      TAOGETKSP

 45: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)

 47: #define taopetscapplicationcreate_     taopetscapplicationcreate 
 48: #define taoapplicationcreate_                taoapplicationcreate 
 49: #define taoapplicationdestroy_                taoapplicationdestroy 
 50: #define taosetapplication_                taosetapplication 


 53: #define taoappsetobjectiveandgradientroutine_    taoappsetobjectiveandgradientroutine
 54: #define taoappsetobjectiveandgradientro_         taoappsetobjectiveandgradientro
 55: #define taoappsetobjectiveroutine_               taoappsetobjectiveroutine
 56: #define taoappsetgradientroutine_                taoappsetgradientroutine
 57: #define taoappsethessianroutine_                 taoappsethessianroutine
 58: #define taoappsetvariableboundsroutine_          taoappsetvariableboundsroutine
 59: #define taoappsetjacobianroutine_                taoappsetjacobianroutine
 60: #define taoappsetconstraintroutine_              taoappsetconstraintroutine
 61: #define taoappsetmonitor_                        taoappsetmonitor
 62: #define taoappgetksp_                            taoappgetksp


 65: /* Grid application */
 66: #define daappsethessianroutine_                         daappsethessianroutine
 67: #define daappsetobjectiveandgradientroutine_            daappsetobjectiveandgradientroutine
 68: #define daappsetobjectiveandgradientrou_                daappsetobjectiveandgradientrou
 69: #define daappsetgradientroutine_                        daappsetgradientroutine
 70: #define daappsetobjectiveroutine_                       daappsetobjectiveroutine
 71: #define daappsetvariableboundsroutine_                  daappsetvariableboundsroutine
 72: #define daappsetconstraintroutine_                      daappsetconstraintroutine
 73: #define daappsetjacobianroutine_                        daappsetjacobianroutine
 74: #define daappsetbeforemonitor_                          daappsetbeforemonitor
 75: #define daappsetaftermonitor_                           daappsetaftermonitor
 76: #define daappsetelementobjectiveandgradientroutine_     daappsetelementobjectiveandgradientroutine
 77: #define daappsetelementobjectiveandgrad_                daappsetelementobjectiveandgrad
 78: #define daappsetelementhessianroutine_                  daappsetelementhessianroutine

 80: #define taodefaultcomputehessian_   taodefaultcomputehessian
 81: #define taodefaultcomputehessiancolor_ taodefaultcomputehessiancolor

 83: #define taogetksp_                      taogetksp

 85: #endif


 89: void PETSC_STDCALL taopetscapplicationcreate_(MPI_Comm *comm,TAO_APPLICATION  *outtao,int *ierr){

 91:   *TaoPetscApplicationCreate((MPI_Comm)PetscToPointerComm(*comm),outtao);

 93: }

 95: void PETSC_STDCALL taoapplicationcreate_(MPI_Comm *comm,TAO_APPLICATION  *outtao,int *ierr){

 97:   *TaoApplicationCreate((MPI_Comm)PetscToPointerComm(*comm),outtao);

 99: }

101: void PETSC_STDCALL taoapplicationdestroy_(TAO_APPLICATION  *outtao,int *ierr){

103:   *TaoApplicationDestroy(*outtao);

105: }

107: /* -------------- Setting call-back routines ---------------- */

109: static void (PETSC_STDCALL *f1)(TAO_APPLICATION*,Vec*,double*,Vec*,void*,int*);


113: static int ourtaominfunctiongradientroutine(TAO_APPLICATION taoapp,Vec x,double *f,Vec r,void *ctx)
114: {
115:   int info = 0;
116:   (*f1)(&taoapp,&x,f,&r,ctx,&info);CHKERRQ(info);
117:   return 0;
118: }



123: void PETSC_STDCALL taoappsetobjectiveandgradientroutine_(TAO_APPLICATION  *taoapp,void (PETSC_STDCALL *func)(TAO_APPLICATION*,Vec*,double*,Vec*,void*,int*),void *ctx,int *info){
124:   f1 = func;
125:   *info = TaoAppSetObjectiveAndGradientRoutine(*taoapp,ourtaominfunctiongradientroutine,ctx);
126: }
127: void PETSC_STDCALL taoappsetobjectiveandgradientro_(TAO_APPLICATION  *taoapp,void (PETSC_STDCALL *func)(TAO_APPLICATION*,Vec*,double*,Vec*,void*,int*),void *ctx,int *info){
128:   f1 = func;
129:   *info = TaoAppSetObjectiveAndGradientRoutine(*taoapp,ourtaominfunctiongradientroutine,ctx);
130: }


133: static void (PETSC_STDCALL *f2)(TAO_APPLICATION*,Vec*,double*,void*,int*);
135: static int ourtaominfunction(TAO_APPLICATION taoapp,Vec x,double* d,void *ctx)
136: {
137:   int info = 0;
138:   (*f2)(&taoapp,&x,d,ctx,&info);CHKERRQ(info);
139:   return 0;
140: }


144: void PETSC_STDCALL taoappsetobjectiveroutine(TAO_APPLICATION  *taoapp,
145:           void (PETSC_STDCALL *func)(TAO_APPLICATION*,Vec*,double*,void*,int*),void *ctx,int *info){
146:   f2 = func;
147:   *info = TaoAppSetObjectiveRoutine(*taoapp,ourtaominfunction,ctx);
148: }



152: static void (PETSC_STDCALL *f3)(TAO_APPLICATION*,Vec*,Vec *,void*,int*);

155: static int ourtaogradientfunction(TAO_APPLICATION taoapp,Vec x,Vec d,void *ctx)
156: {
157:   int info = 0;
158:   (*f3)(&taoapp,&x,&d,ctx,&info);CHKERRQ(info);
159:   return 0;
160: }


164: void PETSC_STDCALL taoappsetgradientroutine_(TAO_APPLICATION *taoapp,void (PETSC_STDCALL *func)(TAO_APPLICATION*,Vec*,Vec*,void*,int*),void *ctx,int *info){
165:   f3 = func;
166:   *info = TaoAppSetGradientRoutine(*taoapp,ourtaogradientfunction,ctx);
167: }


170: /* ---------------------------------------------------------*/
171: /*
172:   taodefaultcomputehessian() and taodefaultcomputehessiancolor().
173:   These routines can be used directly from Fortran, but the following is done
174:   primarily so that a Fortran call to TaoAppSetPetscHessian() will properly handle the
175:   defaults being passed in.

177:   functions, hence no STDCALL
178: */
179: void taodefaultcomputehessian_(TAO_APPLICATION *tao,Vec *x,Mat *m,Mat *p,MatStructure* type,
180:                                void *ctx,int *info)
181: {
182:   *info = TaoAppDefaultComputeHessian(*tao,*x,m,p,type,ctx);
183: }

185: void  taodefaultcomputehessiancolor_(TAO_APPLICATION *tao,Vec *x,Mat *m,Mat *p,
186:                                      MatStructure* type,void *ctx,int *info)
187: {
188:   *info = TaoAppDefaultComputeHessianColor(*tao,*x,m,p,type,*(MatFDColoring*)ctx);
189: }

191: static void (PETSC_STDCALL *f4)(TAO_APPLICATION*,Vec*,Mat*,Mat*,MatStructure*,void*,int*);

194: static int ourtaohessian(TAO_APPLICATION taoapp,Vec x,Mat* m,Mat* p,MatStructure* type,void*ctx)
195: {
196:   int info = 0;
197:   (*f4)(&taoapp,&x,m,p,type,ctx,&info);CHKERRQ(info);
198:   return 0;
199: }



204: void PETSC_STDCALL taoappsethessianroutine_(TAO_APPLICATION  *taoapp,void (PETSC_STDCALL *func)(TAO_APPLICATION*,Vec*,Mat*,Mat*,
205:                                   MatStructure*,void*,int*),void *ctx,int *info)
206: {
207:   if ((void(*)())func == (void(*)()) taodefaultcomputehessian_) {
208:     *info = TaoAppSetHessianRoutine(*taoapp,TaoAppDefaultComputeHessian,ctx);
209:   } else if ( (void(*)()) func == (void(*)()) taodefaultcomputehessiancolor_) {
210:     *info = TaoAppSetHessianRoutine(*taoapp,TaoAppDefaultComputeHessianColor,*(MatFDColoring*)ctx);
211:   } else {
212:     f4 = func;
213:     *info = TaoAppSetHessianRoutine(*taoapp,ourtaohessian,ctx);
214:   }
215: }


218: static void (PETSC_STDCALL *fb)(TAO_APPLICATION*, Vec*,Vec *,void*,int*);


222: static int ourtaovariableboundsroutine(TAO_APPLICATION tao, Vec x,Vec d,void *ctx)
223: {
224:   int info = 0;
225:   (*fb)(&tao,&x,&d,ctx,&info);CHKERRQ(info);
226:   return 0;
227: }



232: void PETSC_STDCALL taoappsetvariableboundsroutine_(TAO_APPLICATION *taoapp,void (PETSC_STDCALL *func)(TAO_APPLICATION*,Vec*,Vec*,void*,int*),void *ctx,int *info){
233:   fb = func;
234:   *info = TaoAppSetVariableBoundsRoutine(*taoapp,ourtaovariableboundsroutine,ctx);
235: }


238: static void (PETSC_STDCALL *f6)(TAO_APPLICATION*,Vec*,Vec *,void*,int*);
239: static void (PETSC_STDCALL *fm)(TAO_APPLICATION*,void*,int*);
241: static int ourtaoconstraintsfunction(TAO_APPLICATION taoapp,Vec x,Vec d,void *ctx)
242: {
243:   int info = 0;
244:   (*f6)(&taoapp,&x,&d,ctx,&info);CHKERRQ(info);
245:   return 0;
246: }

248: static int ourtaoappsetmonitor(TAO_APPLICATION taoapp, void *ctx) {
249:   int info = 0;
250:   (*fm)(&taoapp,ctx,&info); CHKERRQ(info);
251:   return 0;
252: }


256: void PETSC_STDCALL taoappsetconstraintroutine_(TAO_APPLICATION  *taoapp,void (PETSC_STDCALL *func)(TAO_APPLICATION*,Vec*,Vec*,void*,int*),void *ctx,int *info){
257:   f6 = func;
258:   *info = TaoAppSetConstraintRoutine(*taoapp,ourtaoconstraintsfunction,ctx);
259: }

261: void PETSC_STDCALL taoappsetmonitor_(TAO_APPLICATION *taoapp, void (PETSC_STDCALL *func)(TAO_APPLICATION*,void *, int*), void *ctx, int *info){
262:   fm = func;
263:   *info = TaoAppSetMonitor(*taoapp,ourtaoappsetmonitor,ctx);
264: }


267: static void (PETSC_STDCALL *f7)(TAO_APPLICATION*,Vec*,Mat *,Mat*, MatStructure*,void*,int*);

270: static int ourtaojacobianfunction(TAO_APPLICATION tao,Vec x,Mat *J,Mat *JP, MatStructure *flag,void *ctx)
271: {
272:   int info = 0;
273:   (*f7)(&tao,&x,J,JP,flag,ctx,&info);CHKERRQ(info);
274:   return 0;
275: }


279: void PETSC_STDCALL taoappsetjacobianroutine_(TAO_APPLICATION  *taoapp,void (PETSC_STDCALL *func)(TAO_APPLICATION*,Vec*,Mat*,Mat*,MatStructure*,void*,int*),void *ctx,int *info){
280:   f7 = func;
281:   *info = TaoAppSetJacobianRoutine(*taoapp,ourtaojacobianfunction,ctx);
282: }
283: void PETSC_STDCALL taoappgetksp_(TAO_APPLICATION *taoapp,KSP *tksp, int *info ){
284:   *info = TaoAppGetKSP(*taoapp,tksp);
285: }


288: void PETSC_STDCALL taogetksp_(TAO_SOLVER  *tao,KSP *tksp, int *info ){
289:   *info = TaoGetKSP(*tao,tksp);
290: }



295: #include "taodaapplication.h"

297: /* Grid Application routines */
299: static void (PETSC_STDCALL *grid1)(TAO_APPLICATION*, DA*, Vec*, Mat*, void*, int*);
300: static void (PETSC_STDCALL *grid2)(TAO_APPLICATION*, DA*, Vec*, double*, Vec*, void*, int*);
301: static void (PETSC_STDCALL *grid3)(TAO_APPLICATION*, DA*, Vec*, Vec*, void*, int*);
302: static void (PETSC_STDCALL *grid4)(TAO_APPLICATION*, DA*, Vec*, double*, void*, int*);
303: static void (PETSC_STDCALL *grid5)(TAO_APPLICATION*, DA*, Vec*, Vec*, void*, int*);
304: static void (PETSC_STDCALL *grid6)(TAO_APPLICATION*, DA*, Vec*, Vec*, void*, int*);
305: static void (PETSC_STDCALL *grid7)(TAO_APPLICATION*, DA*, Vec*, Mat*, void*, int*);
306: static void (PETSC_STDCALL *grid8)(TAO_APPLICATION*, DA*, int*, void*, int*);
307: static void (PETSC_STDCALL *grid9)(TAO_APPLICATION*, DA*, int*, void*, int*);
308: /* Grid Application Element routines */
309: static void (PETSC_STDCALL *grid10)(int[2], double[4], double*, double[4], void*, int*);
310: static void (PETSC_STDCALL *grid11)(int[2], double[4], double[4][4], void*, int*);
312: static int ourdaappsethessianroutine(TAO_APPLICATION daapp, DA da, Vec x, Mat H, void *ctx) {
313:   int info = 0;
314:   (*grid1)(&daapp, &da, &x, &H, ctx, &info);
315:   return info;
316: }
317: static int ourdaappsetobjectiveandgradientroutine(TAO_APPLICATION daapp, DA da, Vec x, double *f, Vec G, void *ctx) {
318:   int info = 0;
319:   (*grid2)(&daapp, &da, &x, f, &G, ctx, &info);
320:   return info;
321: }
322: static int ourdaappsetgradientroutine(TAO_APPLICATION daapp, DA da, Vec x, Vec g, void *ctx) {
323:   int info = 0;
324:   (*grid3)(&daapp, &da, &x, &g, ctx, &info);
325:   return info;
326: }
327: static int ourdaappsetobjectiveroutine(TAO_APPLICATION daapp, DA da, Vec x, double *f, void *ctx) {
328:   int info = 0;
329:   (*grid4)(&daapp, &da, &x, f, ctx, &info);
330:   return info;
331: }
332: static int ourdaappsetvariableboundsroutine(TAO_APPLICATION daapp, DA da, Vec L, Vec U, void *ctx) {
333:   int info = 0;
334:   (*grid5)(&daapp, &da, &L, &U, ctx, &info);
335:   return info;
336: }
337: static int ourdaappsetconstraintroutine(TAO_APPLICATION daapp, DA da, Vec X, Vec R, void *ctx) {
338:   int info = 0;
339:   (*grid6)(&daapp, &da, &X, &R, ctx, &info);
340:   return info;
341: }
342: static int ourdaappsetjacobianroutine(TAO_APPLICATION daapp, DA da, Vec X, Mat J, void *ctx) {
343:   int info = 0;
344:   (*grid7)(&daapp, &da, &X, &J, ctx, &info);
345:   return info;
346: }
347: static int ourdaappsetbeforemonitor(TAO_APPLICATION daapp, DA da, int levels, void *ctx) {
348:   int info = 0;
349:   (*grid8)(&daapp, &da, &levels, ctx, &info);
350:   return info;
351: }
352: static int ourdaappsetaftermonitor(TAO_APPLICATION daapp, DA da, int levels, void *ctx) {
353:   int info = 0;
354:   (*grid9)(&daapp, &da, &levels, ctx, &info);
355:   return info;
356: }
357: static int ourdaappsetelementobjectiveandgradientroutine(int coord[2], double x[4], double *f, double g[4], void *ctx) {
358:   int info = 0;
359:   (*grid10)(coord, x, f, g, ctx, &info);
360:   return info;
361: }
362: static int ourdaappsetelementhessianroutine(int coord[2], double x[4], double H[4][4], void *ctx) {
363:   int info = 0;
364:   (*grid11)(coord, x, H, ctx, &info);
365:   return info;
366: }
368: void PETSC_STDCALL daappsethessianroutine_(TAO_APPLICATION *daapp, void (PETSC_STDCALL *func)(TAO_APPLICATION*,DA*,Vec*, Mat*, void*, int*),void *ctx, int *info) {
369:   grid1 = func;
370:   *info = DAAppSetHessianRoutine(*daapp, ourdaappsethessianroutine, ctx);
371: }
372: void PETSC_STDCALL daappsetobjectiveandgradientroutine_(TAO_APPLICATION *daapp, void (PETSC_STDCALL *func)(TAO_APPLICATION*, DA*, Vec*, double*, Vec*, void*, int*), void *ctx, int *info) {
373:   grid2 = func;
374:   *info = DAAppSetObjectiveAndGradientRoutine(*daapp, ourdaappsetobjectiveandgradientroutine, ctx);
375: }
376: void PETSC_STDCALL daappsetobjectiveandgradientrou_(TAO_APPLICATION *daapp, void (PETSC_STDCALL *func)(TAO_APPLICATION*, DA*, Vec*, double*, Vec*, void*, int*), void *ctx, int *info) {
377:   grid2 = func;
378:   *info = DAAppSetObjectiveAndGradientRoutine(*daapp, ourdaappsetobjectiveandgradientroutine, ctx);
379: }

381: void PETSC_STDCALL daappsetgradientroutine_(TAO_APPLICATION *daapp, void (PETSC_STDCALL *func)(TAO_APPLICATION*, DA*, Vec*, Vec*, void*, int*), void *ctx, int *info) {
382:   grid3 = func;
383:   *info = DAAppSetGradientRoutine(*daapp, ourdaappsetgradientroutine, ctx);
384: }
385: void PETSC_STDCALL daappsetobjectiveroutine_(TAO_APPLICATION *daapp, void (PETSC_STDCALL *func)(TAO_APPLICATION*, DA*, Vec*, double*, void*, int*), void *ctx, int *info) {
386:   grid4 = func;
387:   *info = DAAppSetObjectiveRoutine(*daapp, ourdaappsetobjectiveroutine, ctx);
388: }
389: void PETSC_STDCALL daappsetvariableboundsroutine_(TAO_APPLICATION *daapp, void (PETSC_STDCALL *func)(TAO_APPLICATION*, DA*, Vec*, Vec*, void*, int*), void *ctx, int *info) {
390:   grid5 = func;
391:   *info = DAAppSetVariableBoundsRoutine(*daapp, ourdaappsetvariableboundsroutine, ctx);
392: }
393: void PETSC_STDCALL daappsetconstraintroutine_(TAO_APPLICATION *daapp, void (PETSC_STDCALL *func)(TAO_APPLICATION*, DA*, Vec*, Vec*, void*, int*), void *ctx, int *info) {
394:   grid6 = func;
395:   *info = DAAppSetConstraintRoutine(*daapp, ourdaappsetconstraintroutine, ctx);
396: }
397: void PETSC_STDCALL daappsetjacobianroutine_(TAO_APPLICATION *daapp, void (PETSC_STDCALL *func)(TAO_APPLICATION*, DA*, Vec*, Mat*, void*, int*), void *ctx, int *info) {
398:   grid7 = func;
399:   *info = DAAppSetJacobianRoutine(*daapp, ourdaappsetjacobianroutine, ctx);
400: }
401: void PETSC_STDCALL daappsetbeforemonitor_(TAO_APPLICATION *daapp, void (PETSC_STDCALL *func)(TAO_APPLICATION*, DA*, int*, void*, int*), void *ctx, int *info) {
402:   grid8 = func;
403:   *info = DAAppSetBeforeMonitor(*daapp, ourdaappsetbeforemonitor, ctx);
404: }
405: void PETSC_STDCALL daappsetaftermonitor_(TAO_APPLICATION *daapp, void (PETSC_STDCALL *func)(TAO_APPLICATION*, DA*, int*, void*, int*), void *ctx, int *info) {
406:   grid9 = func;
407:   *info = DAAppSetAfterMonitor(*daapp, ourdaappsetaftermonitor, ctx);
408: }
409: void PETSC_STDCALL daappsetelementobjectiveandgradientroutine_(TAO_APPLICATION *daapp, void (PETSC_STDCALL *func)(int[2], double[4], double*, double[4], void*, int*), int *flops, void *ctx, int *info) {
410:   grid10 = func;
411:   *info = DAAppSetElementObjectiveAndGradientRoutine(*daapp, ourdaappsetelementobjectiveandgradientroutine, *flops, ctx);
412: }
413: void PETSC_STDCALL daappsetelementobjectiveandgrad_(TAO_APPLICATION *daapp, void (PETSC_STDCALL *func)(int[2], double[4], double*, double[4], void*, int*), int *flops, void *ctx, int *info) {
414:   grid10 = func;
415:   *info = DAAppSetElementObjectiveAndGradientRoutine(*daapp, ourdaappsetelementobjectiveandgradientroutine, *flops, ctx);
416: }
417: void PETSC_STDCALL daappsetelementhessianroutine_(TAO_APPLICATION *daapp, void (PETSC_STDCALL *func)(int[2], double[4], double[4][4], void*, int*), int *flops, void *ctx, int *info) {
418:   grid11 = func;
419:   *info = DAAppSetElementHessianRoutine(*daapp, ourdaappsetelementhessianroutine, *flops, ctx);
420: }