Actual source code: petsckernel.c

  1: #include "tao_general.h"  /*I "tao_general.h"  I*/
  2: #ifdef TAO_USE_PETSC
  3: #include "src/tao_impl.h"

  5: TaoTruth TaoBeganPetsc = TAO_FALSE; 

  8: #include "petscsys.h"


 11: static PetscFList TaoList = 0;

 15: /*
 16:    TaoPrintVersion - Prints TAO version info.

 18:    Collective on MPI_Comm
 19: */
 20: int TaoPrintVersion(MPI_Comm comm)
 21: {
 22:   int info=0;
 23:   

 26:   info = PetscHelpPrintf(comm,"--------------------------------------------\
 27: ------------------------------\n"); CHKERRQ(info);
 28:   info = PetscHelpPrintf(comm,"\t   %s\n",TAO_VERSION_NUMBER); CHKERRQ(info);
 29:   info = PetscHelpPrintf(comm,"%s",TAO_AUTHOR_INFO); CHKERRQ(info);
 30:   info = PetscHelpPrintf(comm,"See docs/manualpages/index.html for help. \n"); CHKERRQ(info);
 31: #if !defined(PARCH_win32)
 32:   info = PetscHelpPrintf(comm,"TAO libraries linked from %s\n",TAO_LIB_DIR); CHKERRQ(info);
 33: #endif
 34:   info = PetscHelpPrintf(comm,"--------------------------------------------\
 35: ------------------------------\n"); CHKERRQ(info);

 37:   PetscFunctionReturn(info);
 38: }

 42: /*
 43:    TaoPrintHelpIntro - Prints introductory TAO help info.

 45:    Collective on MPI_Comm
 46: */
 47: int TaoPrintHelpIntro(MPI_Comm comm)
 48: {
 49:   int info=0;
 50:   

 53:   info = PetscHelpPrintf(comm,"--------------------------------------------\
 54: ------------------------------\n"); CHKERRQ(info);
 55:   info = PetscHelpPrintf(comm,"TAO help information includes that for the PETSc libraries, which provide\n"); CHKERRQ(info);
 56:   info = PetscHelpPrintf(comm,"low-level system infrastructure and linear algebra tools.\n"); CHKERRQ(info);
 57:   info = PetscHelpPrintf(comm,"--------------------------------------------\
 58: ------------------------------\n"); CHKERRQ(info);

 60:   PetscFunctionReturn(info);
 61: }

 65: /*@
 66:   TaoPrintStatement - prints a character string to stdout.

 68:   Not Collective
 69:   
 70:   Input Parameters:
 71: + tao - the TAO_SOLVER solver context
 72: - statement - the string to print

 74:   Level: beginner
 75: @*/
 76: int TaoPrintStatement(TAO_SOLVER tao, const char *statement){
 79:   PetscPrintf(((PetscObject)tao)->comm,statement);
 80:   return(0);
 81: }

 85: int TaoPrintInt(TAO_SOLVER tao, const char *statement, TaoInt n){
 88:   PetscPrintf(((PetscObject)tao)->comm,statement,(PetscInt)n);
 89:   return(0);
 90: }

 94: int TaoPrintDouble(TAO_SOLVER tao, const char *statement,double dd){
 97:   PetscPrintf(((PetscObject)tao)->comm,statement,dd);
 98:   return(0);
 99: }

103: int TaoPrintString(TAO_SOLVER tao, const char *statement,const char *str){
106:   PetscPrintf(((PetscObject)tao)->comm,statement,str);
107:   return(0);
108: }

112: int TaoOptionsHead(const char *heading){
113:   int info;
115:   info = PetscOptionsHead(heading);CHKERRQ(info);
116:   return(0);
117: }

121: int TaoOptionsTail(){
122:   int info;
124:   info = PetscOptionsTail();CHKERRQ(info);
125:   return(0);
126: }

130: int TaoOptionInt(const char *opt,const char *text,const char *man,TaoInt defaultv,TaoInt *value,TaoTruth *set){
131:   int info;
132:   PetscTruth flg=PETSC_FALSE;
134:   info = PetscOptionsInt(opt,text,man,defaultv,value,&flg);CHKERRQ(info);
135:   if (set){
136:     if (flg==PETSC_TRUE) *set=TAO_TRUE; else *set=TAO_FALSE;
137:   }
138:   return(0);
139: }

143: int TaoOptionDouble(const char *opt,const char *text,const char *man,double defaultv,double *value,TaoTruth *set){
144:   int info;
145:   PetscReal pdv=(PetscReal)defaultv, pv;
146:   PetscTruth flg=PETSC_FALSE;
148:   info = PetscOptionsReal(opt,text,man,pdv,&pv,&flg);CHKERRQ(info);
149:   if (set){
150:     if (flg==PETSC_TRUE) *set=TAO_TRUE; else *set=TAO_FALSE;
151:   }
152:   if (value&&flg==PETSC_TRUE){
153:     *value=(double)pv;
154:   }

156:   return(0);
157: }

161: int TaoOptionString(const char *opt,const char *text,const char *man,const char* defaultv,char *value, TaoInt len, TaoTruth *set){
162:   int info;
163:   PetscTruth flg=PETSC_FALSE;
165:   info = PetscOptionsString(opt,text,man,defaultv,value,len,&flg);CHKERRQ(info);
166:   if (set){
167:     if (flg==PETSC_TRUE) *set=TAO_TRUE; else *set=TAO_FALSE;
168:   }
169:   return(0);
170: }

174: int TaoOptionName(const char *opt,const char *text,const char *man,TaoTruth *set){
175:   int info;
176:   PetscTruth flg=PETSC_FALSE;
178:   info = PetscOptionsName(opt,text,man,&flg);CHKERRQ(info);
179:   if (set){
180:     if (flg==PETSC_TRUE) *set=TAO_TRUE; else *set=TAO_FALSE;
181:   }
182:   return(0);
183: }

187: int TaoOptionList(const char *opt, const char *ltext, const char *man, 
188:                   const char **list, TaoInt nlist, const char *defaultv, 
189:                   TaoInt *value, TaoTruth *set)
190: {
191:   int info;
192:   PetscTruth flg=PETSC_FALSE;
194:   info = PetscOptionsEList(opt, ltext, man, list, nlist, defaultv, value, &flg); CHKERRQ(info);
195:   
196:   if (set) {
197:     if (PETSC_TRUE == flg) {
198:       *set=TAO_TRUE; 
199:     }
200:     else {
201:       *set=TAO_FALSE;
202:     }
203:   }
204:   return(0);
205: }

209: int TaoMethodsList(const char *opt,const char *ltext,const char *man,const char *defaultv,char *value,TaoInt len,TaoTruth *set){
210:   int info;
211:   PetscTruth flg=PETSC_FALSE;
213:   info = PetscOptionsList(opt,ltext,man,TaoList,defaultv,value,len,&flg); CHKERRQ(info);
214:   if (set){
215:     if (flg==PETSC_TRUE) *set=TAO_TRUE; else *set=TAO_FALSE;
216:   }
217:   return(0);
218: }


223: int TaoFindSolver(TAO_SOLVER tao, TaoMethod type,  int (**r)(TAO_SOLVER) ){
224:   int info;
226:   info = PetscFListFind(TaoList,((PetscObject)tao)->comm,type,(void (**)(void))r);CHKERRQ(info);
227:   if (*r){
228:     info = PetscObjectChangeTypeName((PetscObject)tao,type);CHKERRQ(info);
229:   }
230:   return(0);
231: }

235: /*@C
236:    TaoRegisterDestroy - Frees the list of minimization solvers that were
237:    registered by TaoRegisterDynamic().

239:    Not Collective

241:    Level: advanced

243: .keywords: TAO_SOLVER, register, destroy

245: .seealso: TaoRegisterAll()
246: @*/
247: int TaoRegisterDestroy(){
248:   int info;
250:   if (TaoList) {
251:     info=PetscFListDestroy(&TaoList);CHKERRQ(info);
252:     TaoList=0;
253:   }
254:   if (TaoBeganPetsc) {
255:     info = PetscFinalize();CHKERRQ(info);
256:   }

258:   return(0);
259: }

263: /* --------------------------------------------------------------------- */
264: /*MC
265:    TaoRegisterDynamic - Adds a method to the TAO_SOLVER package for unconstrained minimization.

267:    Synopsis:
268:    TaoRegisterDynamic(char *name_solver,char *path,char *name_Create,int (*routine_Create)(TAO_SOLVER))

270:    Not collective

272:    Input Parameters:
273: +  name_solver - name of a new user-defined solver
274: .  path - path (either absolute or relative) the library containing this solver
275: .  name_Create - name of routine to Create method context
276: -  routine_Create - routine to Create method context

278:    Notes:
279:    TaoRegisterDynamic() may be called multiple times to add several user-defined solvers.

281:    If dynamic libraries are used, then the fourth input argument (routine_Create)
282:    is ignored.

284:    Environmental variables such as ${TAO_DIR}, ${PETSC_ARCH}, ${PETSC_DIR}, 
285:    and others of the form ${any_environmental_variable} occuring in pathname will be 
286:    replaced with the appropriate values used when PETSc and TAO were compiled.

288:    Sample usage:
289: .vb
290:    TaoRegisterDynamic("my_solver","/home/username/my_lib/lib/libg_c++/solaris/mylib.a",
291:                 "MySolverCreate",MySolverCreate);
292: .ve

294:    Then, your solver can be chosen with the procedural interface via
295: $     TaoSetMethod(solver,"my_solver")
296:    or at runtime via the option
297: $     -tao_method my_solver

299:    Level: advanced

301: .keywords: TAO_SOLVER, register

303: .seealso: TaoRegisterAll(), TaoRegisterDestroy()
304: M*/
308: int TaoRegisterDynamic(const char *sname,const char *path,const char *name,int (*function)(TAO_SOLVER))
309: {
310:   char fullname[256];
311:   int  info;

314:   info = PetscFListConcat(path,name,fullname); CHKERRQ(info);
315:   //#if defined(PETSC_USE_DYNAMIC_LIBRARIES)
316:   //info = PetscFListAddDynamic(&TaoList,sname,fullname, 0);CHKERRQ(info);
317:   //#else
318:   info = PetscFListAddDynamic(&TaoList,sname,fullname, (void (*)())function);CHKERRQ(info);
319:   //#endif
320:   return(0);
321: }

326: /*@C
327:    TaoCompareMethod - Determines whether the TAO_SOLVER structure is of a
328:    specified type.

330:    Not Collective

332:    Input Parameter:
333: .  tao - the TAO_SOLVER solver context
334: .  type - a TAO_SOLVER solver method

336:    Output Parameter:
337: .  same - TAO_TRUE if 'tao' is of method 'type'

339:    Level: developer

341: .keywords: method
342: @*/
343: int TaoCompareMethod(TAO_SOLVER tao, TaoMethod type,TaoTruth *issame){
344:   int info;
345:   PetscTruth flag;
346:   TaoMethod currenttype;
349:   info = TaoGetMethod(tao,&currenttype);CHKERRQ(info);
350:   info = PetscStrcmp(type,currenttype,&flag);CHKERRQ(info);
351:   if (issame){
352:     if (flag==PETSC_TRUE) *issame=TAO_TRUE; else *issame=TAO_FALSE;
353:   }
354:   return(0);
355: }

359: int TaoStrcmp(const char *str1,const char *str2,TaoTruth *flag){
360:   int info;
361:   PetscTruth flg;
363:   info = PetscStrcmp(str1,str2,&flg);CHKERRQ(info);
364:   if (flg==PETSC_TRUE) *flag=TAO_TRUE; else *flag=TAO_FALSE;
365:   return(0);
366: }

370: int TaoStrcpy(char* str1,const char*str2){
371:   int info;
373:   info = PetscStrcpy(str1,str2);CHKERRQ(info);
374:   return(0);
375: }


380: static int TaoPublish_Petsc(PetscObject obj)
381: {
382: #if defined(PETSC_HAVE_AMS)
383:   TAO_SOLVER       v = (TAO_SOLVER) obj;
384:   int          info;
385: #endif

387:   TaoFunctionBegin;

389: #if defined(PETSC_HAVE_AMS)
390:   /* if it is already published then return */
391:   if (v->amem >=0 ) TaoFunctionReturn(0);

393:   info = PetscObjectPublishBaseBegin(obj);CHKERRQ(info);
394:   info = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(info);
395:   info = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->fc,1,AMS_DOUBLE,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(info);
396:   info = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(info);
397:   info = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",(int*)&v->reason,1,AMS_INT,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(info);
398:   info = PetscObjectPublishBaseEnd(obj);CHKERRQ(info);
399: #endif
400:   TaoFunctionReturn(0);
401: }


406: int TaoObjectCreate( TAO_SOLVER *newsolver, MPI_Comm comm){
407:   TAO_SOLVER solver;
408:   int info;

411:   info = PetscHeaderCreate(solver,_p_TAO_SOLVER,int,TAO_COOKIE,-1,"TAO Solver",comm,TaoDestroy,0); CHKERRQ(info);

413:   ((PetscObject)solver)->bops->publish      = TaoPublish_Petsc;
414:   info=PetscPublishAll(solver);CHKERRQ(info);
415:   *newsolver = solver;
416:   return(0);
417: }

421: int TaoObjectDestroy( TAO_SOLVER solver){
422:   int info;
424:   /* if memory was published with AMS then destroy it */

426:   info = PetscObjectDepublish(solver);CHKERRQ(info);
427:   
428:   PetscHeaderDestroy(solver); 

430:   return(0);
431: }
432: static int one = 1;
433: static char *executable = (char *)"tao";
434: static char **executablePtr = &executable;

438: int TaoLogClassRegister(int*cookie,const char*objectname){
439:   int info;
440:   int argc;
441:   char **args;

444:   info = TaoGetArgs(&argc,&args); CHKERRQ(info);

446: #if !defined(PARCH_t3d)
447:   info = PetscSetHelpVersionFunctions(TaoPrintHelpIntro,TaoPrintVersion); CHKERRQ(info);
448: #endif

450:   if (!PetscInitializeCalled) {
451:     if (argc&&args){
452:       info = PetscInitialize(&argc,&args,0,0); CHKERRQ(info);
453:     } else {
454:       info = PetscInitialize(&one,&executablePtr,0,0); CHKERRQ(info);
455:     }
456:     TaoBeganPetsc = TAO_TRUE;
457:   }
458:   info=PetscCookieRegister(objectname,cookie);CHKERRQ(info);
459:   return(0);
460: }

462: #endif

464: /* The PetscObject is reduced and microkernal capabilities are absent
465: #define PetscObjectComposeFunctionDynamic(a,b,c,d) 0
466: #define PetscObjectQueryFunction(a,b,c) 0
467:   PetscLogInfo((void *,const char*,...){ return 0);}

469: */