Actual source code: trajmemory.c

  1: #include <petsc/private/tsimpl.h>
  2: #include <petscsys.h>
  3: #if defined(PETSC_HAVE_REVOLVE)
  4: #include <revolve_c.h>

  6: /* Limit Revolve to 32-bits */
  7: #define PETSC_REVOLVE_INT_MAX  2147483647

  9: typedef int PetscRevolveInt;

 11: static inline PetscErrorCode PetscRevolveIntCast(PetscInt a,PetscRevolveInt *b)
 12: {
 13: #if defined(PETSC_USE_64BIT_INDICES)
 14:   *b = 0;
 16: #endif
 17:   *b = (PetscRevolveInt)(a);
 18:   return 0;
 19: }
 20: #endif
 21: #if defined(PETSC_HAVE_CAMS)
 22: #include <offline_schedule.h>
 23: #endif

 25: PetscLogEvent TSTrajectory_DiskWrite, TSTrajectory_DiskRead;
 26: static PetscErrorCode TSTrajectorySet_Memory(TSTrajectory,TS,PetscInt,PetscReal,Vec);

 28: typedef enum {NONE,TWO_LEVEL_NOREVOLVE,TWO_LEVEL_REVOLVE,TWO_LEVEL_TWO_REVOLVE,REVOLVE_OFFLINE,REVOLVE_ONLINE,REVOLVE_MULTISTAGE,CAMS_OFFLINE} SchedulerType;

 30: typedef enum {UNSET=-1,SOLUTIONONLY=0,STAGESONLY=1,SOLUTION_STAGES=2} CheckpointType;

 32: typedef enum {TJ_REVOLVE, TJ_CAMS, TJ_PETSC} TSTrajectoryMemoryType;
 33: static const char *const TSTrajectoryMemoryTypes[] = {"REVOLVE","CAMS","PETSC","TSTrajectoryMemoryType","TJ_",NULL};

 35: #define HaveSolution(m) ((m) == SOLUTIONONLY || (m) == SOLUTION_STAGES)
 36: #define HaveStages(m)   ((m) == STAGESONLY || (m) == SOLUTION_STAGES)

 38: typedef struct _StackElement {
 39:   PetscInt       stepnum;
 40:   Vec            X;
 41:   Vec            *Y;
 42:   PetscReal      time;
 43:   PetscReal      timeprev; /* for no solution_only mode */
 44:   PetscReal      timenext; /* for solution_only mode */
 45:   CheckpointType cptype;
 46: } *StackElement;

 48: #if defined(PETSC_HAVE_REVOLVE)
 49: typedef struct _RevolveCTX {
 50:   PetscBool       reverseonestep;
 51:   PetscRevolveInt where;
 52:   PetscRevolveInt snaps_in;
 53:   PetscRevolveInt stepsleft;
 54:   PetscRevolveInt check;
 55:   PetscRevolveInt oldcapo;
 56:   PetscRevolveInt capo;
 57:   PetscRevolveInt fine;
 58:   PetscRevolveInt info;
 59: } RevolveCTX;
 60: #endif

 62: #if defined(PETSC_HAVE_CAMS)
 63: typedef struct _CAMSCTX {
 64:   PetscInt lastcheckpointstep;
 65:   PetscInt lastcheckpointtype;
 66:   PetscInt num_units_avail;
 67:   PetscInt endstep;
 68:   PetscInt num_stages;
 69:   PetscInt nextcheckpointstep;
 70:   PetscInt nextcheckpointtype; /* (0) solution only (1) stages (2) solution+stages */
 71:   PetscInt info;
 72: } CAMSCTX;
 73: #endif

 75: typedef struct _Stack {
 76:   PetscInt      stacksize;
 77:   PetscInt      top;
 78:   StackElement  *container;
 79:   PetscInt      nallocated;
 80:   PetscInt      numY;
 81:   PetscBool     solution_only;
 82:   PetscBool     use_dram;
 83: } Stack;

 85: typedef struct _DiskStack {
 86:   PetscInt  stacksize;
 87:   PetscInt  top;
 88:   PetscInt  *container;
 89: } DiskStack;

 91: typedef struct _TJScheduler {
 92:   SchedulerType stype;
 93:   TSTrajectoryMemoryType tj_memory_type;
 94: #if defined(PETSC_HAVE_REVOLVE)
 95:   RevolveCTX    *rctx,*rctx2;
 96:   PetscBool     use_online;
 97:   PetscInt      store_stride;
 98: #endif
 99: #if defined(PETSC_HAVE_CAMS)
100:   CAMSCTX       *actx;
101: #endif
102:   PetscBool     recompute;
103:   PetscBool     skip_trajectory;
104:   PetscBool     save_stack;
105:   PetscInt      max_units_ram;  /* maximum checkpointing units in RAM */
106:   PetscInt      max_units_disk; /* maximum checkpointing units on disk */
107:   PetscInt      max_cps_ram;    /* maximum checkpoints in RAM */
108:   PetscInt      max_cps_disk;   /* maximum checkpoints on disk */
109:   PetscInt      stride;
110:   PetscInt      total_steps;    /* total number of steps */
111:   Stack         stack;
112:   DiskStack     diskstack;
113:   PetscViewer   viewer;
114: } TJScheduler;

116: static PetscErrorCode TurnForwardWithStepsize(TS ts,PetscReal nextstepsize)
117: {
118:   /* reverse the direction */
119:   TSSetTimeStep(ts,nextstepsize);
120:   return 0;
121: }

123: static PetscErrorCode TurnForward(TS ts)
124: {
125:   PetscReal      stepsize;

127:   /* reverse the direction */
128:   TSGetTimeStep(ts,&stepsize);
129:   TSSetTimeStep(ts,-stepsize);
130:   return 0;
131: }

133: static PetscErrorCode TurnBackward(TS ts)
134: {
135:   PetscReal      stepsize;

137:   if (!ts->trajectory->adjoint_solve_mode) return 0;
138:   /* reverse the direction */
139:   stepsize = ts->ptime_prev-ts->ptime;
140:   TSSetTimeStep(ts,stepsize);
141:   return 0;
142: }

144: static PetscErrorCode ElementCreate(TS ts,CheckpointType cptype,Stack *stack,StackElement *e)
145: {
146:   Vec            X;
147:   Vec            *Y;

149:   if (stack->top < stack->stacksize-1 && stack->container[stack->top+1]) {
150:     *e = stack->container[stack->top+1];
151:     if (HaveSolution(cptype) && !(*e)->X) {
152:       TSGetSolution(ts,&X);
153:       VecDuplicate(X,&(*e)->X);
154:     }
155:     if (cptype==1 && (*e)->X) {
156:       VecDestroy(&(*e)->X);
157:     }
158:     if (HaveStages(cptype) && !(*e)->Y) {
159:       TSGetStages(ts,&stack->numY,&Y);
160:       if (stack->numY) {
161:         VecDuplicateVecs(Y[0],stack->numY,&(*e)->Y);
162:       }
163:     }
164:     if (cptype==0 && (*e)->Y) {
165:       VecDestroyVecs(stack->numY,&(*e)->Y);
166:     }
167:     (*e)->cptype = cptype;
168:     return 0;
169:   }
170:   if (stack->use_dram) {
171:     PetscMallocSetDRAM();
172:   }
173:   PetscNew(e);
174:   if (HaveSolution(cptype)) {
175:     TSGetSolution(ts,&X);
176:     VecDuplicate(X,&(*e)->X);
177:   }
178:   if (HaveStages(cptype)) {
179:     TSGetStages(ts,&stack->numY,&Y);
180:     if (stack->numY) {
181:       VecDuplicateVecs(Y[0],stack->numY,&(*e)->Y);
182:     }
183:   }
184:   if (stack->use_dram) {
185:     PetscMallocResetDRAM();
186:   }
187:   stack->nallocated++;
188:   (*e)->cptype = cptype;
189:   return 0;
190: }

192: static PetscErrorCode ElementSet(TS ts, Stack *stack, StackElement *e, PetscInt stepnum, PetscReal time, Vec X)
193: {
194:   Vec            *Y;
195:   PetscInt       i;
196:   PetscReal      timeprev;

198:   if (HaveSolution((*e)->cptype)) {
199:     VecCopy(X,(*e)->X);
200:   }
201:   if (HaveStages((*e)->cptype)) {
202:     TSGetStages(ts,&stack->numY,&Y);
203:     for (i=0;i<stack->numY;i++) {
204:       VecCopy(Y[i],(*e)->Y[i]);
205:     }
206:   }
207:   (*e)->stepnum = stepnum;
208:   (*e)->time    = time;
209:   /* for consistency */
210:   if (stepnum == 0) {
211:     (*e)->timeprev = (*e)->time - ts->time_step;
212:   } else {
213:     TSGetPrevTime(ts,&timeprev);
214:     (*e)->timeprev = timeprev;
215:   }
216:   return 0;
217: }

219: static PetscErrorCode ElementDestroy(Stack *stack,StackElement e)
220: {
221:   if (stack->use_dram) {
222:     PetscMallocSetDRAM();
223:   }
224:   VecDestroy(&e->X);
225:   if (e->Y) {
226:     VecDestroyVecs(stack->numY,&e->Y);
227:   }
228:   PetscFree(e);
229:   if (stack->use_dram) {
230:     PetscMallocResetDRAM();
231:   }
232:   stack->nallocated--;
233:   return 0;
234: }

236: static PetscErrorCode StackResize(Stack *stack,PetscInt newsize)
237: {
238:   StackElement   *newcontainer;
239:   PetscInt       i;

241:   PetscCalloc1(newsize*sizeof(StackElement),&newcontainer);
242:   for (i=0;i<stack->stacksize;i++) {
243:     newcontainer[i] = stack->container[i];
244:   }
245:   PetscFree(stack->container);
246:   stack->container = newcontainer;
247:   stack->stacksize = newsize;
248:   return 0;
249: }

251: static PetscErrorCode StackPush(Stack *stack,StackElement e)
252: {
254:   stack->container[++stack->top] = e;
255:   return 0;
256: }

258: static PetscErrorCode StackPop(Stack *stack,StackElement *e)
259: {
260:   *e = NULL;
262:   *e = stack->container[stack->top--];
263:   return 0;
264: }

266: static PetscErrorCode StackTop(Stack *stack,StackElement *e)
267: {
268:   *e = stack->container[stack->top];
269:   return 0;
270: }

272: static PetscErrorCode StackInit(Stack *stack,PetscInt size,PetscInt ny)
273: {
274:   stack->top  = -1;
275:   stack->numY = ny;

277:   if (!stack->container) {
278:     PetscCalloc1(size,&stack->container);
279:   }
280:   return 0;
281: }

283: static PetscErrorCode StackDestroy(Stack *stack)
284: {
285:   const PetscInt n = stack->nallocated;

287:   if (!stack->container) return 0;
289:   for (PetscInt i=0; i<n; i++) ElementDestroy(stack,stack->container[i]);
290:   PetscFree(stack->container);
291:   return 0;
292: }

294: static PetscErrorCode StackFind(Stack *stack,StackElement *e,PetscInt index)
295: {
296:   *e = NULL;
298:   *e = stack->container[index];
299:   return 0;
300: }

302: static PetscErrorCode WriteToDisk(PetscBool stifflyaccurate,PetscInt stepnum,PetscReal time,PetscReal timeprev,Vec X,Vec *Y,PetscInt numY,CheckpointType cptype,PetscViewer viewer)
303: {
304:   PetscViewerBinaryWrite(viewer,&stepnum,1,PETSC_INT);
305:   if (HaveSolution(cptype)) VecView(X,viewer);
306:   if (HaveStages(cptype)) {
307:     for (PetscInt i=0; i<numY; i++) {
308:       /* For stiffly accurate TS methods, the last stage Y[ns-1] is the same as the solution X, thus does not need to be saved again. */
309:       if (stifflyaccurate && i == numY-1 && HaveSolution(cptype)) continue;
310:       VecView(Y[i],viewer);
311:     }
312:   }
313:   PetscViewerBinaryWrite(viewer,&time,1,PETSC_REAL);
314:   PetscViewerBinaryWrite(viewer,&timeprev,1,PETSC_REAL);
315:   return 0;
316: }

318: static PetscErrorCode ReadFromDisk(PetscBool stifflyaccurate,PetscInt *stepnum,PetscReal *time,PetscReal *timeprev,Vec X,Vec *Y,PetscInt numY,CheckpointType cptype,PetscViewer viewer)
319: {
320:   PetscViewerBinaryRead(viewer,stepnum,1,NULL,PETSC_INT);
321:   if (HaveSolution(cptype)) VecLoad(X,viewer);
322:   if (HaveStages(cptype)) {
323:     for (PetscInt i=0; i<numY; i++) {
324:       /* For stiffly accurate TS methods, the last stage Y[ns-1] is the same as the solution X, thus does not need to be loaded again. */
325:       if (stifflyaccurate && i == numY-1 && HaveSolution(cptype)) continue;
326:       VecLoad(Y[i],viewer);
327:     }
328:   }
329:   PetscViewerBinaryRead(viewer,time,1,NULL,PETSC_REAL);
330:   PetscViewerBinaryRead(viewer,timeprev,1,NULL,PETSC_REAL);
331:   return 0;
332: }

334: static PetscErrorCode StackDumpAll(TSTrajectory tj,TS ts,Stack *stack,PetscInt id)
335: {
336:   Vec          *Y;
337:   PetscInt      ndumped,cptype_int;
338:   StackElement  e     = NULL;
339:   TJScheduler  *tjsch = (TJScheduler*)tj->data;
340:   char          filename[PETSC_MAX_PATH_LEN];
341:   MPI_Comm      comm;

343:   PetscObjectGetComm((PetscObject)ts,&comm);
344:   if (tj->monitor) {
345:     PetscViewerASCIIPushTab(tj->monitor);
346:     PetscViewerASCIIPrintf(tj->monitor,"Dump stack id %D to file\n",id);
347:     PetscViewerASCIIPopTab(tj->monitor);
348:   }
349:   PetscSNPrintf(filename,sizeof(filename),"%s/TS-STACK%06d.bin",tj->dirname,id);
350:   PetscViewerFileSetName(tjsch->viewer,filename);
351:   PetscViewerSetUp(tjsch->viewer);
352:   ndumped = stack->top+1;
353:   PetscViewerBinaryWrite(tjsch->viewer,&ndumped,1,PETSC_INT);
354:   for (PetscInt i=0;i<ndumped;i++) {
355:     e = stack->container[i];
356:     cptype_int = (PetscInt)e->cptype;
357:     PetscViewerBinaryWrite(tjsch->viewer,&cptype_int,1,PETSC_INT);
358:     PetscLogEventBegin(TSTrajectory_DiskWrite,tj,ts,0,0);
359:     WriteToDisk(ts->stifflyaccurate,e->stepnum,e->time,e->timeprev,e->X,e->Y,stack->numY,e->cptype,tjsch->viewer);
360:     PetscLogEventEnd(TSTrajectory_DiskWrite,tj,ts,0,0);
361:     ts->trajectory->diskwrites++;
362:     StackPop(stack,&e);
363:   }
364:   /* save the last step for restart, the last step is in memory when using single level schemes, but not necessarily the case for multi level schemes */
365:   TSGetStages(ts,&stack->numY,&Y);
366:   PetscLogEventBegin(TSTrajectory_DiskWrite,tj,ts,0,0);
367:   WriteToDisk(ts->stifflyaccurate,ts->steps,ts->ptime,ts->ptime_prev,ts->vec_sol,Y,stack->numY,SOLUTION_STAGES,tjsch->viewer);
368:   PetscLogEventEnd(TSTrajectory_DiskWrite,tj,ts,0,0);
369:   ts->trajectory->diskwrites++;
370:   return 0;
371: }

373: static PetscErrorCode StackLoadAll(TSTrajectory tj,TS ts,Stack *stack,PetscInt id)
374: {
375:   Vec          *Y;
376:   PetscInt      i,nloaded,cptype_int;
377:   StackElement  e;
378:   PetscViewer   viewer;
379:   char          filename[PETSC_MAX_PATH_LEN];

381:   if (tj->monitor) {
382:     PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
383:     PetscViewerASCIIPrintf(tj->monitor,"Load stack from file\n");
384:     PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
385:   }
386:   PetscSNPrintf(filename,sizeof filename,"%s/TS-STACK%06d.bin",tj->dirname,id);
387:   PetscViewerBinaryOpen(PetscObjectComm((PetscObject)tj),filename,FILE_MODE_READ,&viewer);
388:   PetscViewerBinarySetSkipInfo(viewer,PETSC_TRUE);
389:   PetscViewerPushFormat(viewer,PETSC_VIEWER_NATIVE);
390:   PetscViewerBinaryRead(viewer,&nloaded,1,NULL,PETSC_INT);
391:   for (i=0;i<nloaded;i++) {
392:     PetscViewerBinaryRead(viewer,&cptype_int,1,NULL,PETSC_INT);
393:     ElementCreate(ts,(CheckpointType)cptype_int,stack,&e);
394:     StackPush(stack,e);
395:     PetscLogEventBegin(TSTrajectory_DiskRead,tj,ts,0,0);
396:     ReadFromDisk(ts->stifflyaccurate,&e->stepnum,&e->time,&e->timeprev,e->X,e->Y,stack->numY,e->cptype,viewer);
397:     PetscLogEventEnd(TSTrajectory_DiskRead,tj,ts,0,0);
398:     ts->trajectory->diskreads++;
399:   }
400:   /* load the last step into TS */
401:   TSGetStages(ts,&stack->numY,&Y);
402:   PetscLogEventBegin(TSTrajectory_DiskRead,tj,ts,0,0);
403:   ReadFromDisk(ts->stifflyaccurate,&ts->steps,&ts->ptime,&ts->ptime_prev,ts->vec_sol,Y,stack->numY,SOLUTION_STAGES,viewer);
404:   PetscLogEventEnd(TSTrajectory_DiskRead,tj,ts,0,0);
405:   ts->trajectory->diskreads++;
406:   TurnBackward(ts);
407:   PetscViewerDestroy(&viewer);
408:   return 0;
409: }

411: #if defined(PETSC_HAVE_REVOLVE)
412: static PetscErrorCode StackLoadLast(TSTrajectory tj,TS ts,Stack *stack,PetscInt id)
413: {
414:   Vec         *Y;
415:   PetscInt     size;
416:   PetscViewer  viewer;
417:   char         filename[PETSC_MAX_PATH_LEN];
418: #if defined(PETSC_HAVE_MPIIO)
419:   PetscBool    usempiio;
420: #endif
421:   int          fd;
422:   off_t        off,offset;

424:   if (tj->monitor) {
425:     PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
426:     PetscViewerASCIIPrintf(tj->monitor,"Load last stack element from file\n");
427:     PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
428:   }
429:   TSGetStages(ts,&stack->numY,&Y);
430:   VecGetSize(Y[0],&size);
431:   /* VecView writes to file two extra int's for class id and number of rows */
432:   off  = -((stack->solution_only?0:stack->numY)+1)*(size*PETSC_BINARY_SCALAR_SIZE+2*PETSC_BINARY_INT_SIZE)-PETSC_BINARY_INT_SIZE-2*PETSC_BINARY_SCALAR_SIZE;

434:   PetscSNPrintf(filename,sizeof filename,"%s/TS-STACK%06d.bin",tj->dirname,id);
435:   PetscViewerBinaryOpen(PetscObjectComm((PetscObject)tj),filename,FILE_MODE_READ,&viewer);
436:   PetscViewerBinarySetSkipInfo(viewer,PETSC_TRUE);
437:   PetscViewerPushFormat(viewer,PETSC_VIEWER_NATIVE);
438: #if defined(PETSC_HAVE_MPIIO)
439:   PetscViewerBinaryGetUseMPIIO(viewer,&usempiio);
440:   if (usempiio) {
441:     PetscViewerBinaryGetMPIIODescriptor(viewer,(MPI_File*)&fd);
442:     PetscBinarySynchronizedSeek(PetscObjectComm((PetscObject)tj),fd,off,PETSC_BINARY_SEEK_END,&offset);
443:   } else {
444: #endif
445:     PetscViewerBinaryGetDescriptor(viewer,&fd);
446:     PetscBinarySeek(fd,off,PETSC_BINARY_SEEK_END,&offset);
447: #if defined(PETSC_HAVE_MPIIO)
448:   }
449: #endif
450:   /* load the last step into TS */
451:   PetscLogEventBegin(TSTrajectory_DiskRead,tj,ts,0,0);
452:   ReadFromDisk(ts->stifflyaccurate,&ts->steps,&ts->ptime,&ts->ptime_prev,ts->vec_sol,Y,stack->numY,SOLUTION_STAGES,viewer);
453:   PetscLogEventEnd(TSTrajectory_DiskRead,tj,ts,0,0);
454:   ts->trajectory->diskreads++;
455:   PetscViewerDestroy(&viewer);
456:   TurnBackward(ts);
457:   return 0;
458: }
459: #endif

461: static PetscErrorCode DumpSingle(TSTrajectory tj,TS ts,Stack *stack,PetscInt id)
462: {
463:   Vec            *Y;
464:   PetscInt       stepnum;
465:   TJScheduler    *tjsch = (TJScheduler*)tj->data;
466:   char           filename[PETSC_MAX_PATH_LEN];
467:   MPI_Comm       comm;

469:   PetscObjectGetComm((PetscObject)ts,&comm);
470:   if (tj->monitor) {
471:     PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
472:     PetscViewerASCIIPrintf(tj->monitor,"Dump a single point from file\n");
473:     PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
474:   }
475:   TSGetStepNumber(ts,&stepnum);
476:   PetscSNPrintf(filename,sizeof(filename),"%s/TS-CPS%06d.bin",tj->dirname,id);
477:   PetscViewerFileSetName(tjsch->viewer,filename);
478:   PetscViewerSetUp(tjsch->viewer);

480:   TSGetStages(ts,&stack->numY,&Y);
481:   PetscLogEventBegin(TSTrajectory_DiskWrite,tj,ts,0,0);
482:   WriteToDisk(ts->stifflyaccurate,stepnum,ts->ptime,ts->ptime_prev,ts->vec_sol,Y,stack->numY,SOLUTION_STAGES,tjsch->viewer);
483:   PetscLogEventEnd(TSTrajectory_DiskWrite,tj,ts,0,0);
484:   ts->trajectory->diskwrites++;
485:   return 0;
486: }

488: static PetscErrorCode LoadSingle(TSTrajectory tj,TS ts,Stack *stack,PetscInt id)
489: {
490:   Vec            *Y;
491:   PetscViewer    viewer;
492:   char           filename[PETSC_MAX_PATH_LEN];

494:   if (tj->monitor) {
495:     PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
496:     PetscViewerASCIIPrintf(tj->monitor,"Load a single point from file\n");
497:     PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
498:   }
499:   PetscSNPrintf(filename,sizeof filename,"%s/TS-CPS%06d.bin",tj->dirname,id);
500:   PetscViewerBinaryOpen(PetscObjectComm((PetscObject)tj),filename,FILE_MODE_READ,&viewer);
501:   PetscViewerBinarySetSkipInfo(viewer,PETSC_TRUE);
502:   PetscViewerPushFormat(viewer,PETSC_VIEWER_NATIVE);
503:   TSGetStages(ts,&stack->numY,&Y);
504:   PetscLogEventBegin(TSTrajectory_DiskRead,tj,ts,0,0);
505:   ReadFromDisk(ts->stifflyaccurate,&ts->steps,&ts->ptime,&ts->ptime_prev,ts->vec_sol,Y,stack->numY,SOLUTION_STAGES,viewer);
506:   PetscLogEventEnd(TSTrajectory_DiskRead,tj,ts,0,0);
507:   ts->trajectory->diskreads++;
508:   PetscViewerDestroy(&viewer);
509:   return 0;
510: }

512: static PetscErrorCode UpdateTS(TS ts,Stack *stack,StackElement e,PetscInt stepnum,PetscBool adjoint_mode)
513: {
514:   Vec            *Y;
515:   PetscInt       i;

517:   /* In adjoint mode we do not need to copy solution if the stepnum is the same */
518:   if (!adjoint_mode || (HaveSolution(e->cptype) && e->stepnum!=stepnum)) {
519:     VecCopy(e->X,ts->vec_sol);
520:   }
521:   if (HaveStages(e->cptype)) {
522:     TSGetStages(ts,&stack->numY,&Y);
523:     if (e->stepnum && e->stepnum==stepnum) {
524:       for (i=0;i<stack->numY;i++) {
525:         VecCopy(e->Y[i],Y[i]);
526:       }
527:     } else if (ts->stifflyaccurate) {
528:       VecCopy(e->Y[stack->numY-1],ts->vec_sol);
529:     }
530:   }
531:   if (adjoint_mode) {
532:     TSSetTimeStep(ts,e->timeprev-e->time); /* stepsize will be negative */
533:   } else {
534:     TSSetTimeStep(ts,e->time-e->timeprev); /* stepsize will be positive */
535:   }
536:   ts->ptime      = e->time;
537:   ts->ptime_prev = e->timeprev;
538:   return 0;
539: }

541: static PetscErrorCode ReCompute(TS ts,TJScheduler *tjsch,PetscInt stepnumbegin,PetscInt stepnumend)
542: {
543:   Stack          *stack = &tjsch->stack;
544:   PetscInt       i;

546:   tjsch->recompute = PETSC_TRUE; /* hints TSTrajectorySet() that it is in recompute mode */
547:   TSSetStepNumber(ts,stepnumbegin);/* global step number */
548:   for (i=stepnumbegin;i<stepnumend;i++) { /* assume fixed step size */
549:     if (stack->solution_only && !tjsch->skip_trajectory) { /* revolve online need this */
550:       /* don't use the public interface as it will update the TSHistory: this need a better fix */
551:       TSTrajectorySet_Memory(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);
552:     }
553:     TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);
554:     TSStep(ts);
555:     if (!stack->solution_only && !tjsch->skip_trajectory) {
556:       /* don't use the public interface as it will update the TSHistory: this need a better fix */
557:       TSTrajectorySet_Memory(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);
558:     }
559:     TSEventHandler(ts);
560:     if (!ts->steprollback) {
561:       TSPostStep(ts);
562:     }
563:   }
564:   TurnBackward(ts);
565:   ts->trajectory->recomps += stepnumend-stepnumbegin; /* recomputation counter */
566:   TSSetStepNumber(ts,stepnumend);
567:   tjsch->recompute = PETSC_FALSE; /* reset the flag for recompute mode */
568:   return 0;
569: }

571: static PetscErrorCode TopLevelStore(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscInt localstepnum,PetscInt laststridesize,PetscBool *done)
572: {
573:   Stack          *stack = &tjsch->stack;
574:   DiskStack      *diskstack = &tjsch->diskstack;
575:   PetscInt       stridenum;

577:   *done = PETSC_FALSE;
578:   stridenum    = stepnum/tjsch->stride;
579:   /* make sure saved checkpoint id starts from 1
580:      skip last stride when using stridenum+1
581:      skip first stride when using stridenum */
582:   if (stack->solution_only) {
583:     if (tjsch->save_stack) {
584:       if (localstepnum == tjsch->stride-1 && stepnum < tjsch->total_steps-laststridesize) { /* current step will be saved without going through stack */
585:         StackDumpAll(tj,ts,stack,stridenum+1);
586:         if (tjsch->stype == TWO_LEVEL_TWO_REVOLVE) diskstack->container[++diskstack->top] = stridenum+1;
587:         *done = PETSC_TRUE;
588:       }
589:     } else {
590:       if (localstepnum == 0 && stepnum < tjsch->total_steps-laststridesize) {
591:         DumpSingle(tj,ts,stack,stridenum+1);
592:         if (tjsch->stype == TWO_LEVEL_TWO_REVOLVE) diskstack->container[++diskstack->top] = stridenum+1;
593:         *done = PETSC_TRUE;
594:       }
595:     }
596:   } else {
597:     if (tjsch->save_stack) {
598:       if (localstepnum == 0 && stepnum < tjsch->total_steps && stepnum != 0) { /* skip the first stride */
599:         StackDumpAll(tj,ts,stack,stridenum);
600:         if (tjsch->stype == TWO_LEVEL_TWO_REVOLVE) diskstack->container[++diskstack->top] = stridenum;
601:         *done = PETSC_TRUE;
602:       }
603:     } else {
604:       if (localstepnum == 1 && stepnum < tjsch->total_steps-laststridesize) {
605:         DumpSingle(tj,ts,stack,stridenum+1);
606:         if (tjsch->stype == TWO_LEVEL_TWO_REVOLVE) diskstack->container[++diskstack->top] = stridenum+1;
607:         *done = PETSC_TRUE;
608:       }
609:     }
610:   }
611:   return 0;
612: }

614: static PetscErrorCode TSTrajectoryMemorySet_N(TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscReal time,Vec X)
615: {
616:   Stack          *stack = &tjsch->stack;
617:   StackElement   e;
618:   CheckpointType cptype;

620:   /* skip the last step */
621:   if (ts->reason) { /* only affect the forward run */
622:     /* update total_steps in the end of forward run */
623:     if (stepnum != tjsch->total_steps) tjsch->total_steps = stepnum;
624:     if (stack->solution_only) {
625:       /* get rid of the solution at second last step */
626:       StackPop(stack,&e);
627:     }
628:     return 0;
629:   }
630:   /*  do not save trajectory at the recompute stage for solution_only mode */
631:   if (stack->solution_only && tjsch->recompute) return 0;
632:   /* skip the first step for no_solution_only mode */
633:   if (!stack->solution_only && stepnum == 0) return 0;

635:   /* resize the stack */
636:   if (stack->top+1 == stack->stacksize) {
637:     StackResize(stack,2*stack->stacksize);
638:   }
639:   /* update timenext for the previous step; necessary for step adaptivity */
640:   if (stack->top > -1) {
641:     StackTop(stack,&e);
642:     e->timenext = ts->ptime;
643:   }
644:   if (stepnum < stack->top) {
645:     SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_MEMC,"Illegal modification of a non-top stack element");
646:   }
647:   cptype = stack->solution_only ? SOLUTIONONLY : STAGESONLY;
648:   ElementCreate(ts,cptype,stack,&e);
649:   ElementSet(ts,stack,&e,stepnum,time,X);
650:   StackPush(stack,e);
651:   return 0;
652: }

654: static PetscErrorCode TSTrajectoryMemorySet_N_2(TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscReal time,Vec X)
655: {
656:   Stack          *stack = &tjsch->stack;
657:   StackElement   e;
658:   CheckpointType cptype;

660:   if (stack->top+1 == stack->stacksize) {
661:     StackResize(stack,2*stack->stacksize);
662:   }
663:   /* update timenext for the previous step; necessary for step adaptivity */
664:   if (stack->top > -1) {
665:     StackTop(stack,&e);
666:     e->timenext = ts->ptime;
667:   }
669:   cptype = stack->solution_only ? SOLUTIONONLY : SOLUTION_STAGES; /* Always include solution in a checkpoint in non-adjoint mode */
670:   ElementCreate(ts,cptype,stack,&e);
671:   ElementSet(ts,stack,&e,stepnum,time,X);
672:   StackPush(stack,e);
673:   return 0;
674: }

676: static PetscErrorCode TSTrajectoryMemoryGet_N(TS ts,TJScheduler *tjsch,PetscInt stepnum)
677: {
678:   Stack          *stack = &tjsch->stack;
679:   StackElement   e;
680:   PetscInt       ns;

682:   /* If TSTrajectoryGet() is called after TSAdjointSolve() converges (e.g. outside the while loop in TSAdjointSolve()), skip getting the checkpoint. */
683:   if (ts->reason) return 0;
684:   if (stepnum == tjsch->total_steps) {
685:     TurnBackward(ts);
686:     return 0;
687:   }
688:   /* restore a checkpoint */
689:   StackTop(stack,&e);
690:   UpdateTS(ts,stack,e,stepnum,PETSC_TRUE);
691:   TSGetStages(ts,&ns,PETSC_IGNORE);
692:   if (stack->solution_only && ns) { /* recompute one step */
693:     TurnForwardWithStepsize(ts,e->timenext-e->time);
694:     ReCompute(ts,tjsch,e->stepnum,stepnum);
695:   }
696:   StackPop(stack,&e);
697:   return 0;
698: }

700: static PetscErrorCode TSTrajectoryMemoryGet_N_2(TS ts,TJScheduler *tjsch,PetscInt stepnum)
701: {
702:   Stack        *stack = &tjsch->stack;
703:   StackElement  e     = NULL;

705:   StackFind(stack,&e,stepnum);
707:   UpdateTS(ts,stack,e,stepnum,PETSC_FALSE);
708:   return 0;
709: }

711: static PetscErrorCode TSTrajectoryMemorySet_TLNR(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscReal time,Vec X)
712: {
713:   Stack          *stack = &tjsch->stack;
714:   PetscInt       localstepnum,laststridesize;
715:   StackElement   e;
716:   PetscBool      done;
717:   CheckpointType cptype;

719:   if (!stack->solution_only && stepnum == 0) return 0;
720:   if (stack->solution_only && stepnum == tjsch->total_steps) return 0;
721:   if (tjsch->save_stack && tjsch->recompute) return 0;

723:   localstepnum = stepnum%tjsch->stride;
724:   /* (stridesize-1) checkpoints are saved in each stride; an extra point is added by StackDumpAll() */
725:   laststridesize = tjsch->total_steps%tjsch->stride;
726:   if (!laststridesize) laststridesize = tjsch->stride;

728:   if (!tjsch->recompute) {
729:     TopLevelStore(tj,ts,tjsch,stepnum,localstepnum,laststridesize,&done);
730:     if (!tjsch->save_stack && stepnum < tjsch->total_steps-laststridesize) return 0;
731:   }
732:   if (!stack->solution_only && localstepnum == 0) return 0; /* skip last point in each stride at recompute stage or last stride */
733:   if (stack->solution_only && localstepnum == tjsch->stride-1) return 0; /* skip last step in each stride at recompute stage or last stride */

735:   cptype = stack->solution_only ? SOLUTIONONLY : STAGESONLY;
736:   ElementCreate(ts,cptype,stack,&e);
737:   ElementSet(ts,stack,&e,stepnum,time,X);
738:   StackPush(stack,e);
739:   return 0;
740: }

742: static PetscErrorCode TSTrajectoryMemoryGet_TLNR(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum)
743: {
744:   Stack          *stack = &tjsch->stack;
745:   PetscInt       id,localstepnum,laststridesize;
746:   StackElement   e;

748:   if (stepnum == tjsch->total_steps) {
749:     TurnBackward(ts);
750:     return 0;
751:   }

753:   localstepnum = stepnum%tjsch->stride;
754:   laststridesize = tjsch->total_steps%tjsch->stride;
755:   if (!laststridesize) laststridesize = tjsch->stride;
756:   if (stack->solution_only) {
757:     /* fill stack with info */
758:     if (localstepnum == 0 && tjsch->total_steps-stepnum >= laststridesize) {
759:       id = stepnum/tjsch->stride;
760:       if (tjsch->save_stack) {
761:         StackLoadAll(tj,ts,stack,id);
762:         tjsch->skip_trajectory = PETSC_TRUE;
763:         TurnForward(ts);
764:         ReCompute(ts,tjsch,id*tjsch->stride-1,id*tjsch->stride);
765:         tjsch->skip_trajectory = PETSC_FALSE;
766:       } else {
767:         LoadSingle(tj,ts,stack,id);
768:         TurnForward(ts);
769:         ReCompute(ts,tjsch,(id-1)*tjsch->stride,id*tjsch->stride);
770:       }
771:       return 0;
772:     }
773:     /* restore a checkpoint */
774:     StackPop(stack,&e);
775:     UpdateTS(ts,stack,e,stepnum,PETSC_TRUE);
776:     tjsch->skip_trajectory = PETSC_TRUE;
777:     TurnForward(ts);
778:     ReCompute(ts,tjsch,e->stepnum,stepnum);
779:     tjsch->skip_trajectory = PETSC_FALSE;
780:   } else {
781:     CheckpointType cptype = STAGESONLY;
782:     /* fill stack with info */
783:     if (localstepnum == 0 && tjsch->total_steps-stepnum >= laststridesize) {
784:       id = stepnum/tjsch->stride;
785:       if (tjsch->save_stack) {
786:         StackLoadAll(tj,ts,stack,id);
787:       } else {
788:         LoadSingle(tj,ts,stack,id);
789:         ElementCreate(ts,cptype,stack,&e);
790:         ElementSet(ts,stack,&e,(id-1)*tjsch->stride+1,ts->ptime,ts->vec_sol);
791:         StackPush(stack,e);
792:         TurnForward(ts);
793:         ReCompute(ts,tjsch,e->stepnum,id*tjsch->stride);
794:       }
795:       return 0;
796:     }
797:     /* restore a checkpoint */
798:     StackPop(stack,&e);
799:     UpdateTS(ts,stack,e,stepnum,PETSC_TRUE);
800:   }
801:   return 0;
802: }

804: #if defined(PETSC_HAVE_REVOLVE)
805: static PetscErrorCode printwhattodo(PetscViewer viewer,PetscRevolveInt whattodo,RevolveCTX *rctx,PetscRevolveInt shift)
806: {
807:   if (!viewer) return 0;

809:   switch(whattodo) {
810:     case 1:
811:       PetscViewerASCIIPrintf(viewer,"Advance from %D to %D\n",rctx->oldcapo+shift,rctx->capo+shift);
812:       break;
813:     case 2:
814:       PetscViewerASCIIPrintf(viewer,"Store in checkpoint number %D (located in RAM)\n",rctx->check);
815:       break;
816:     case 3:
817:       PetscViewerASCIIPrintf(viewer,"First turn: Initialize adjoints and reverse first step\n");
818:       break;
819:     case 4:
820:       PetscViewerASCIIPrintf(viewer,"Forward and reverse one step\n");
821:       break;
822:     case 5:
823:       PetscViewerASCIIPrintf(viewer,"Restore in checkpoint number %D (located in RAM)\n",rctx->check);
824:       break;
825:     case 7:
826:       PetscViewerASCIIPrintf(viewer,"Store in checkpoint number %D (located on disk)\n",rctx->check);
827:       break;
828:     case 8:
829:       PetscViewerASCIIPrintf(viewer,"Restore in checkpoint number %D (located on disk)\n",rctx->check);
830:       break;
831:     case -1:
832:       PetscViewerASCIIPrintf(viewer,"Error!");
833:       break;
834:   }
835:   return 0;
836: }

838: static PetscErrorCode printwhattodo2(PetscViewer viewer,PetscRevolveInt whattodo,RevolveCTX *rctx,PetscRevolveInt shift)
839: {
840:   if (!viewer) return 0;

842:   switch(whattodo) {
843:     case 1:
844:       PetscViewerASCIIPrintf(viewer,"[Top Level] Advance from stride %D to stride %D\n",rctx->oldcapo+shift,rctx->capo+shift);
845:       break;
846:     case 2:
847:       PetscViewerASCIIPrintf(viewer,"[Top Level] Store in checkpoint number %D\n",rctx->check);
848:       break;
849:     case 3:
850:       PetscViewerASCIIPrintf(viewer,"[Top Level] First turn: Initialize adjoints and reverse first stride\n");
851:       break;
852:     case 4:
853:       PetscViewerASCIIPrintf(viewer,"[Top Level] Forward and reverse one stride\n");
854:       break;
855:     case 5:
856:       PetscViewerASCIIPrintf(viewer,"[Top Level] Restore in checkpoint number %D\n",rctx->check);
857:       break;
858:     case 7:
859:       PetscViewerASCIIPrintf(viewer,"[Top Level] Store in top-level checkpoint number %D\n",rctx->check);
860:       break;
861:     case 8:
862:       PetscViewerASCIIPrintf(viewer,"[Top Level] Restore in top-level checkpoint number %D\n",rctx->check);
863:       break;
864:     case -1:
865:       PetscViewerASCIIPrintf(viewer,"[Top Level] Error!");
866:       break;
867:   }
868:   return 0;
869: }

871: static PetscErrorCode InitRevolve(PetscInt fine,PetscInt snaps,RevolveCTX *rctx)
872: {
873:   PetscRevolveInt rsnaps,rfine;

875:   PetscRevolveIntCast(snaps,&rsnaps);
876:   PetscRevolveIntCast(fine,&rfine);
877:   revolve_reset();
878:   revolve_create_offline(rfine,rsnaps);
879:   rctx->snaps_in       = rsnaps;
880:   rctx->fine           = rfine;
881:   rctx->check          = 0;
882:   rctx->capo           = 0;
883:   rctx->reverseonestep = PETSC_FALSE;
884:   /* check stepsleft? */
885:   return 0;
886: }

888: static PetscErrorCode FastForwardRevolve(RevolveCTX *rctx)
889: {
890:   PetscRevolveInt whattodo;

892:   whattodo = 0;
893:   while (whattodo!=3) { /* we have to fast forward revolve to the beginning of the backward sweep due to unfriendly revolve interface */
894:     whattodo = revolve_action(&rctx->check,&rctx->capo,&rctx->fine,rctx->snaps_in,&rctx->info,&rctx->where);
895:   }
896:   return 0;
897: }

899: static PetscErrorCode ApplyRevolve(PetscViewer viewer,SchedulerType stype,RevolveCTX *rctx,PetscRevolveInt total_steps,PetscRevolveInt stepnum,PetscRevolveInt localstepnum,PetscBool toplevel,PetscInt *store)
900: {
901:   PetscRevolveInt shift,whattodo;

903:   *store = 0;
904:   if (rctx->stepsleft > 0) { /* advance the solution without checkpointing anything as Revolve requires */
905:     rctx->stepsleft--;
906:     return 0;
907:   }
908:   /* let Revolve determine what to do next */
909:   shift         = stepnum-localstepnum;
910:   rctx->oldcapo = rctx->capo;
911:   rctx->capo    = localstepnum;

913:   if (!toplevel) whattodo = revolve_action(&rctx->check,&rctx->capo,&rctx->fine,rctx->snaps_in,&rctx->info,&rctx->where);
914:   else whattodo = revolve2_action(&rctx->check,&rctx->capo,&rctx->fine,rctx->snaps_in,&rctx->info,&rctx->where);
915:   if (stype == REVOLVE_ONLINE && whattodo == 8) whattodo = 5;
916:   if (stype == REVOLVE_ONLINE && whattodo == 7) whattodo = 2;
917:   if (!toplevel) printwhattodo(viewer,whattodo,rctx,shift);
918:   else printwhattodo2(viewer,whattodo,rctx,shift);
920:   if (whattodo == 1) { /* advance some time steps */
921:     if (stype == REVOLVE_ONLINE && rctx->capo >= total_steps-1) {
922:       revolve_turn(total_steps,&rctx->capo,&rctx->fine);
923:       if (!toplevel) printwhattodo(viewer,whattodo,rctx,shift);
924:       else printwhattodo2(viewer,whattodo,rctx,shift);
925:     }
926:     rctx->stepsleft = rctx->capo-rctx->oldcapo-1;
927:   }
928:   if (whattodo == 3 || whattodo == 4) { /* ready for a reverse step */
929:     rctx->reverseonestep = PETSC_TRUE;
930:   }
931:   if (whattodo == 5) { /* restore a checkpoint and ask Revolve what to do next */
932:     rctx->oldcapo = rctx->capo;
933:     if (!toplevel) whattodo = revolve_action(&rctx->check,&rctx->capo,&rctx->fine,rctx->snaps_in,&rctx->info,&rctx->where); /* must return 1 or 3 or 4*/
934:     else whattodo = revolve2_action(&rctx->check,&rctx->capo,&rctx->fine,rctx->snaps_in,&rctx->info,&rctx->where);
935:     if (!toplevel) printwhattodo(viewer,whattodo,rctx,shift);
936:     else printwhattodo2(viewer,whattodo,rctx,shift);
937:     if (whattodo == 3 || whattodo == 4) rctx->reverseonestep = PETSC_TRUE;
938:     if (whattodo == 1) rctx->stepsleft = rctx->capo-rctx->oldcapo;
939:   }
940:   if (whattodo == 7) { /* save the checkpoint to disk */
941:     *store = 2;
942:     rctx->oldcapo = rctx->capo;
943:     whattodo = revolve_action(&rctx->check,&rctx->capo,&rctx->fine,rctx->snaps_in,&rctx->info,&rctx->where); /* must return 1 */
944:     printwhattodo(viewer,whattodo,rctx,shift);
945:     rctx->stepsleft = rctx->capo-rctx->oldcapo-1;
946:   }
947:   if (whattodo == 2) { /* store a checkpoint to RAM and ask Revolve how many time steps to advance next */
948:     *store = 1;
949:     rctx->oldcapo = rctx->capo;
950:     if (!toplevel) whattodo = revolve_action(&rctx->check,&rctx->capo,&rctx->fine,rctx->snaps_in,&rctx->info,&rctx->where); /* must return 1 */
951:     else whattodo = revolve2_action(&rctx->check,&rctx->capo,&rctx->fine,rctx->snaps_in,&rctx->info,&rctx->where);
952:     if (!toplevel) printwhattodo(viewer,whattodo,rctx,shift);
953:     else printwhattodo2(viewer,whattodo,rctx,shift);
954:     if (stype == REVOLVE_ONLINE && rctx->capo >= total_steps-1) {
955:       revolve_turn(total_steps,&rctx->capo,&rctx->fine);
956:       printwhattodo(viewer,whattodo,rctx,shift);
957:     }
958:     rctx->stepsleft = rctx->capo-rctx->oldcapo-1;
959:   }
960:   return 0;
961: }

963: static PetscErrorCode TSTrajectoryMemorySet_ROF(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscReal time,Vec X)
964: {
965:   Stack           *stack = &tjsch->stack;
966:   PetscInt        store;
967:   StackElement    e;
968:   PetscRevolveInt rtotal_steps,rstepnum;
969:   CheckpointType  cptype;

971:   if (!stack->solution_only && stepnum == 0) return 0;
972:   if (stack->solution_only && stepnum == tjsch->total_steps) return 0;
973:   PetscRevolveIntCast(tjsch->total_steps,&rtotal_steps);
974:   PetscRevolveIntCast(stepnum,&rstepnum);
975:   ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,rtotal_steps,rstepnum,rstepnum,PETSC_FALSE,&store);
976:   if (store == 1) {
978:     cptype = stack->solution_only ? SOLUTIONONLY : SOLUTION_STAGES;
979:     ElementCreate(ts,cptype,stack,&e);
980:     ElementSet(ts,stack,&e,stepnum,time,X);
981:     StackPush(stack,e);
982:   }
983:   return 0;
984: }

986: static PetscErrorCode TSTrajectoryMemoryGet_ROF(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum)
987: {
988:   Stack           *stack = &tjsch->stack;
989:   PetscInt        store;
990:   PetscRevolveInt whattodo,shift,rtotal_steps,rstepnum;
991:   StackElement    e;

993:   if (stepnum == 0 || stepnum == tjsch->total_steps) {
994:     TurnBackward(ts);
995:     tjsch->rctx->reverseonestep = PETSC_FALSE;
996:     return 0;
997:   }
998:   /* restore a checkpoint */
999:   StackTop(stack,&e);
1000:   UpdateTS(ts,stack,e,stepnum,PETSC_TRUE);
1001:   PetscRevolveIntCast(tjsch->total_steps,&rtotal_steps);
1002:   PetscRevolveIntCast(stepnum,&rstepnum);
1003:   if (stack->solution_only) { /* start with restoring a checkpoint */
1004:     tjsch->rctx->capo = rstepnum;
1005:     tjsch->rctx->oldcapo = tjsch->rctx->capo;
1006:     shift = 0;
1007:     whattodo = revolve_action(&tjsch->rctx->check,&tjsch->rctx->capo,&tjsch->rctx->fine,tjsch->rctx->snaps_in,&tjsch->rctx->info,&tjsch->rctx->where);
1008:     printwhattodo(tj->monitor,whattodo,tjsch->rctx,shift);
1009:   } else { /* 2 revolve actions: restore a checkpoint and then advance */
1010:     ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,rtotal_steps,rstepnum,rstepnum,PETSC_FALSE,&store);
1011:     if (tj->monitor) {
1012:       PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
1013:       PetscViewerASCIIPrintf(tj->monitor,"Skip the step from %D to %D (stage values already checkpointed)\n",tjsch->rctx->oldcapo,tjsch->rctx->oldcapo+1);
1014:       PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
1015:     }
1016:     if (!tjsch->rctx->reverseonestep && tjsch->rctx->stepsleft > 0) tjsch->rctx->stepsleft--;
1017:   }
1018:   if (stack->solution_only || (!stack->solution_only && e->stepnum < stepnum)) {
1019:     TurnForward(ts);
1020:     ReCompute(ts,tjsch,e->stepnum,stepnum);
1021:   }
1022:   if ((stack->solution_only && e->stepnum+1 == stepnum) || (!stack->solution_only && e->stepnum == stepnum)) {
1023:     StackPop(stack,&e);
1024:   }
1025:   tjsch->rctx->reverseonestep = PETSC_FALSE;
1026:   return 0;
1027: }

1029: static PetscErrorCode TSTrajectoryMemorySet_RON(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscReal time,Vec X)
1030: {
1031:   Stack           *stack = &tjsch->stack;
1032:   Vec             *Y;
1033:   PetscInt        i,store;
1034:   PetscReal       timeprev;
1035:   StackElement    e;
1036:   RevolveCTX      *rctx = tjsch->rctx;
1037:   PetscRevolveInt rtotal_steps,rstepnum;
1038:   CheckpointType  cptype;

1040:   if (!stack->solution_only && stepnum == 0) return 0;
1041:   if (stack->solution_only && stepnum == tjsch->total_steps) return 0;
1042:   PetscRevolveIntCast(tjsch->total_steps,&rtotal_steps);
1043:   PetscRevolveIntCast(stepnum,&rstepnum);
1044:   ApplyRevolve(tj->monitor,tjsch->stype,rctx,rtotal_steps,rstepnum,rstepnum,PETSC_FALSE,&store);
1045:   if (store == 1) {
1046:     if (rctx->check != stack->top+1) { /* overwrite some non-top checkpoint in the stack */
1047:       StackFind(stack,&e,rctx->check);
1048:       if (HaveSolution(e->cptype)) {
1049:         VecCopy(X,e->X);
1050:       }
1051:       if (HaveStages(e->cptype)) {
1052:         TSGetStages(ts,&stack->numY,&Y);
1053:         for (i=0;i<stack->numY;i++) {
1054:           VecCopy(Y[i],e->Y[i]);
1055:         }
1056:       }
1057:       e->stepnum  = stepnum;
1058:       e->time     = time;
1059:       TSGetPrevTime(ts,&timeprev);
1060:       e->timeprev = timeprev;
1061:     } else {
1063:       cptype = stack->solution_only ? SOLUTIONONLY : SOLUTION_STAGES;
1064:       ElementCreate(ts,cptype,stack,&e);
1065:       ElementSet(ts,stack,&e,stepnum,time,X);
1066:       StackPush(stack,e);
1067:     }
1068:   }
1069:   return 0;
1070: }

1072: static PetscErrorCode TSTrajectoryMemoryGet_RON(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum)
1073: {
1074:   Stack           *stack = &tjsch->stack;
1075:   PetscRevolveInt whattodo,shift,rstepnum;
1076:   StackElement    e;

1078:   if (stepnum == 0 || stepnum == tjsch->total_steps) {
1079:     TurnBackward(ts);
1080:     tjsch->rctx->reverseonestep = PETSC_FALSE;
1081:     return 0;
1082:   }
1083:   PetscRevolveIntCast(stepnum,&rstepnum);
1084:   tjsch->rctx->capo = rstepnum;
1085:   tjsch->rctx->oldcapo = tjsch->rctx->capo;
1086:   shift = 0;
1087:   whattodo = revolve_action(&tjsch->rctx->check,&tjsch->rctx->capo,&tjsch->rctx->fine,tjsch->rctx->snaps_in,&tjsch->rctx->info,&tjsch->rctx->where); /* whattodo=restore */
1088:   if (whattodo == 8) whattodo = 5;
1089:   printwhattodo(tj->monitor,whattodo,tjsch->rctx,shift);
1090:   /* restore a checkpoint */
1091:   StackFind(stack,&e,tjsch->rctx->check);
1092:   UpdateTS(ts,stack,e,stepnum,PETSC_TRUE);
1093:   if (!stack->solution_only) { /* whattodo must be 5 */
1094:     /* ask Revolve what to do next */
1095:     tjsch->rctx->oldcapo = tjsch->rctx->capo;
1096:     whattodo = revolve_action(&tjsch->rctx->check,&tjsch->rctx->capo,&tjsch->rctx->fine,tjsch->rctx->snaps_in,&tjsch->rctx->info,&tjsch->rctx->where); /* must return 1 or 3 or 4*/
1097:     printwhattodo(tj->monitor,whattodo,tjsch->rctx,shift);
1098:     if (whattodo == 3 || whattodo == 4) tjsch->rctx->reverseonestep = PETSC_TRUE;
1099:     if (whattodo == 1) tjsch->rctx->stepsleft = tjsch->rctx->capo-tjsch->rctx->oldcapo;
1100:     if (tj->monitor) {
1101:       PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
1102:       PetscViewerASCIIPrintf(tj->monitor,"Skip the step from %D to %D (stage values already checkpointed)\n",tjsch->rctx->oldcapo,tjsch->rctx->oldcapo+1);
1103:       PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
1104:     }
1105:     if (!tjsch->rctx->reverseonestep && tjsch->rctx->stepsleft > 0) tjsch->rctx->stepsleft--;
1106:   }
1107:   if (stack->solution_only || (!stack->solution_only && e->stepnum < stepnum)) {
1108:     TurnForward(ts);
1109:     ReCompute(ts,tjsch,e->stepnum,stepnum);
1110:   }
1111:   tjsch->rctx->reverseonestep = PETSC_FALSE;
1112:   return 0;
1113: }

1115: static PetscErrorCode TSTrajectoryMemorySet_TLR(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscReal time,Vec X)
1116: {
1117:   Stack           *stack = &tjsch->stack;
1118:   PetscInt        store,localstepnum,laststridesize;
1119:   StackElement    e;
1120:   PetscBool       done = PETSC_FALSE;
1121:   PetscRevolveInt rtotal_steps,rstepnum,rlocalstepnum;
1122:   CheckpointType  cptype;

1124:   if (!stack->solution_only && stepnum == 0) return 0;
1125:   if (stack->solution_only && stepnum == tjsch->total_steps) return 0;

1127:   localstepnum = stepnum%tjsch->stride;
1128:   laststridesize = tjsch->total_steps%tjsch->stride;
1129:   if (!laststridesize) laststridesize = tjsch->stride;

1131:   if (!tjsch->recompute) {
1132:     TopLevelStore(tj,ts,tjsch,stepnum,localstepnum,laststridesize,&done);
1133:     /* revolve is needed for the last stride; different starting points for last stride between solutin_only and !solutin_only */
1134:     if (!stack->solution_only && !tjsch->save_stack && stepnum <= tjsch->total_steps-laststridesize) return 0;
1135:     if (stack->solution_only && !tjsch->save_stack && stepnum < tjsch->total_steps-laststridesize) return 0;
1136:   }
1137:   if (tjsch->save_stack && done) {
1138:     InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1139:     return 0;
1140:   }
1141:   if (laststridesize < tjsch->stride) {
1142:     if (stack->solution_only && stepnum == tjsch->total_steps-laststridesize && !tjsch->recompute) { /* step tjsch->total_steps-laststridesize-1 is skipped, but the next step is not */
1143:       InitRevolve(laststridesize,tjsch->max_cps_ram,tjsch->rctx);
1144:     }
1145:     if (!stack->solution_only && stepnum == tjsch->total_steps-laststridesize+1 && !tjsch->recompute) { /* step tjsch->total_steps-laststridesize is skipped, but the next step is not */
1146:       InitRevolve(laststridesize,tjsch->max_cps_ram,tjsch->rctx);
1147:     }
1148:   }
1149:   PetscRevolveIntCast(tjsch->total_steps,&rtotal_steps);
1150:   PetscRevolveIntCast(stepnum,&rstepnum);
1151:   PetscRevolveIntCast(localstepnum,&rlocalstepnum);
1152:   ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,rtotal_steps,rstepnum,rlocalstepnum,PETSC_FALSE,&store);
1153:   if (store == 1) {
1155:     cptype = stack->solution_only ? SOLUTIONONLY : SOLUTION_STAGES;
1156:     ElementCreate(ts,cptype,stack,&e);
1157:     ElementSet(ts,stack,&e,stepnum,time,X);
1158:     StackPush(stack,e);
1159:   }
1160:   return 0;
1161: }

1163: static PetscErrorCode TSTrajectoryMemoryGet_TLR(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum)
1164: {
1165:   Stack           *stack = &tjsch->stack;
1166:   PetscRevolveInt whattodo,shift,rstepnum,rlocalstepnum,rtotal_steps;
1167:   PetscInt        localstepnum,stridenum,laststridesize,store;
1168:   StackElement    e;
1169:   CheckpointType  cptype;

1171:   localstepnum = stepnum%tjsch->stride;
1172:   stridenum    = stepnum/tjsch->stride;
1173:   if (stepnum == tjsch->total_steps) {
1174:     TurnBackward(ts);
1175:     tjsch->rctx->reverseonestep = PETSC_FALSE;
1176:     return 0;
1177:   }
1178:   laststridesize = tjsch->total_steps%tjsch->stride;
1179:   if (!laststridesize) laststridesize = tjsch->stride;
1180:   PetscRevolveIntCast(tjsch->total_steps,&rtotal_steps);
1181:   PetscRevolveIntCast(stepnum,&rstepnum);
1182:   PetscRevolveIntCast(localstepnum,&rlocalstepnum);
1183:   if (stack->solution_only) {
1184:     /* fill stack */
1185:     if (localstepnum == 0 && stepnum <= tjsch->total_steps-laststridesize) {
1186:       if (tjsch->save_stack) {
1187:         StackLoadAll(tj,ts,stack,stridenum);
1188:         InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1189:         FastForwardRevolve(tjsch->rctx);
1190:         tjsch->skip_trajectory = PETSC_TRUE;
1191:         TurnForward(ts);
1192:         ReCompute(ts,tjsch,stridenum*tjsch->stride-1,stridenum*tjsch->stride);
1193:         tjsch->skip_trajectory = PETSC_FALSE;
1194:       } else {
1195:         LoadSingle(tj,ts,stack,stridenum);
1196:         InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1197:         TurnForward(ts);
1198:         ReCompute(ts,tjsch,(stridenum-1)*tjsch->stride,stridenum*tjsch->stride);
1199:       }
1200:       return 0;
1201:     }
1202:     /* restore a checkpoint */
1203:     StackTop(stack,&e);
1204:     UpdateTS(ts,stack,e,stepnum,PETSC_TRUE);
1205:     /* start with restoring a checkpoint */
1206:     tjsch->rctx->capo = rstepnum;
1207:     tjsch->rctx->oldcapo = tjsch->rctx->capo;
1208:     shift = rstepnum-rlocalstepnum;
1209:     whattodo = revolve_action(&tjsch->rctx->check,&tjsch->rctx->capo,&tjsch->rctx->fine,tjsch->rctx->snaps_in,&tjsch->rctx->info,&tjsch->rctx->where);
1210:     printwhattodo(tj->monitor,whattodo,tjsch->rctx,shift);
1211:     TurnForward(ts);
1212:     ReCompute(ts,tjsch,e->stepnum,stepnum);
1213:     if (e->stepnum+1 == stepnum) {
1214:       StackPop(stack,&e);
1215:     }
1216:   } else {
1217:     /* fill stack with info */
1218:     if (localstepnum == 0 && tjsch->total_steps-stepnum >= laststridesize) {
1219:       if (tjsch->save_stack) {
1220:         StackLoadAll(tj,ts,stack,stridenum);
1221:         InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1222:         FastForwardRevolve(tjsch->rctx);
1223:       } else {
1224:         PetscRevolveInt rnum;
1225:         LoadSingle(tj,ts,stack,stridenum);
1226:         InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1227:         PetscRevolveIntCast((stridenum-1)*tjsch->stride+1,&rnum);
1228:         ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,rtotal_steps,rnum,1,PETSC_FALSE,&store);
1229:         if (tj->monitor) {
1230:           PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
1231:           PetscViewerASCIIPrintf(tj->monitor,"Skip the step from %D to %D (stage values already checkpointed)\n",(stridenum-1)*tjsch->stride+tjsch->rctx->oldcapo,(stridenum-1)*tjsch->stride+tjsch->rctx->oldcapo+1);
1232:           PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
1233:         }
1234:         cptype = SOLUTION_STAGES;
1235:         ElementCreate(ts,cptype,stack,&e);
1236:         ElementSet(ts,stack,&e,(stridenum-1)*tjsch->stride+1,ts->ptime,ts->vec_sol);
1237:         StackPush(stack,e);
1238:         TurnForward(ts);
1239:         ReCompute(ts,tjsch,e->stepnum,stridenum*tjsch->stride);
1240:       }
1241:       return 0;
1242:     }
1243:     /* restore a checkpoint */
1244:     StackTop(stack,&e);
1245:     UpdateTS(ts,stack,e,stepnum,PETSC_TRUE);
1246:     /* 2 revolve actions: restore a checkpoint and then advance */
1247:     ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,rtotal_steps,rstepnum,rlocalstepnum,PETSC_FALSE,&store);
1248:     if (tj->monitor) {
1249:       PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
1250:       PetscViewerASCIIPrintf(tj->monitor,"Skip the step from %D to %D (stage values already checkpointed)\n",stepnum-localstepnum+tjsch->rctx->oldcapo,stepnum-localstepnum+tjsch->rctx->oldcapo+1);
1251:       PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
1252:     }
1253:     if (!tjsch->rctx->reverseonestep && tjsch->rctx->stepsleft > 0) tjsch->rctx->stepsleft--;
1254:     if (e->stepnum < stepnum) {
1255:       TurnForward(ts);
1256:       ReCompute(ts,tjsch,e->stepnum,stepnum);
1257:     }
1258:     if (e->stepnum == stepnum) {
1259:       StackPop(stack,&e);
1260:     }
1261:   }
1262:   tjsch->rctx->reverseonestep = PETSC_FALSE;
1263:   return 0;
1264: }

1266: static PetscErrorCode TSTrajectoryMemorySet_TLTR(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscReal time,Vec X)
1267: {
1268:   Stack           *stack = &tjsch->stack;
1269:   PetscInt        store,localstepnum,stridenum,laststridesize;
1270:   StackElement    e;
1271:   PetscBool       done = PETSC_FALSE;
1272:   PetscRevolveInt rlocalstepnum,rstepnum,rtotal_steps;

1274:   if (!stack->solution_only && stepnum == 0) return 0;
1275:   if (stack->solution_only && stepnum == tjsch->total_steps) return 0;

1277:   localstepnum = stepnum%tjsch->stride; /* index at the bottom level (inside a stride) */
1278:   stridenum    = stepnum/tjsch->stride; /* index at the top level */
1279:   laststridesize = tjsch->total_steps%tjsch->stride;
1280:   if (!laststridesize) laststridesize = tjsch->stride;
1281:   if (stack->solution_only && localstepnum == 0 && !tjsch->rctx2->reverseonestep) {
1282:     PetscRevolveIntCast((tjsch->total_steps+tjsch->stride-1)/tjsch->stride,&rtotal_steps);
1283:     PetscRevolveIntCast(stridenum,&rstepnum);
1284:     ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx2,rtotal_steps,rstepnum,rstepnum,PETSC_TRUE,&tjsch->store_stride);
1285:     if (laststridesize < tjsch->stride && stepnum == tjsch->total_steps-laststridesize) {
1286:       InitRevolve(laststridesize,tjsch->max_cps_ram,tjsch->rctx);
1287:     }
1288:   }
1289:   if (!stack->solution_only && localstepnum == 1 && !tjsch->rctx2->reverseonestep) {
1290:     PetscRevolveIntCast((tjsch->total_steps+tjsch->stride-1)/tjsch->stride,&rtotal_steps);
1291:     PetscRevolveIntCast(stridenum,&rstepnum);
1292:     ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx2,rtotal_steps,rstepnum,rstepnum,PETSC_TRUE,&tjsch->store_stride);
1293:     if (laststridesize < tjsch->stride && stepnum == tjsch->total_steps-laststridesize+1) {
1294:       InitRevolve(laststridesize,tjsch->max_cps_ram,tjsch->rctx);
1295:     }
1296:   }
1297:   if (tjsch->store_stride) {
1298:     TopLevelStore(tj,ts,tjsch,stepnum,localstepnum,laststridesize,&done);
1299:     if (done) {
1300:       InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1301:       return 0;
1302:     }
1303:   }
1304:   if (stepnum < tjsch->total_steps-laststridesize) {
1305:     if (tjsch->save_stack && !tjsch->store_stride && !tjsch->rctx2->reverseonestep) return 0; /* store or forward-and-reverse at top level trigger revolve at bottom level */
1306:     if (!tjsch->save_stack && !tjsch->rctx2->reverseonestep) return 0; /* store operation does not require revolve be called at bottom level */
1307:   }
1308:   /* Skipping stepnum=0 for !stack->only is enough for TLR, but not for TLTR. Here we skip the first step for each stride so that the top-level revolve is applied (always at localstepnum=1) ahead of the bottom-level revolve */
1309:   if (!stack->solution_only && localstepnum == 0 && stepnum != tjsch->total_steps && !tjsch->recompute) return 0;
1310:   PetscRevolveIntCast(tjsch->total_steps,&rtotal_steps);
1311:   PetscRevolveIntCast(stepnum,&rstepnum);
1312:   PetscRevolveIntCast(localstepnum,&rlocalstepnum);
1313:   ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,rtotal_steps,rstepnum,rlocalstepnum,PETSC_FALSE,&store);
1314:   if (store == 1) {
1315:     CheckpointType cptype;
1317:     cptype = stack->solution_only ? SOLUTIONONLY : SOLUTION_STAGES;
1318:     ElementCreate(ts,cptype,stack,&e);
1319:     ElementSet(ts,stack,&e,stepnum,time,X);
1320:     StackPush(stack,e);
1321:   }
1322:   return 0;
1323: }

1325: static PetscErrorCode TSTrajectoryMemoryGet_TLTR(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum)
1326: {
1327:   Stack           *stack = &tjsch->stack;
1328:   DiskStack       *diskstack = &tjsch->diskstack;
1329:   PetscInt        localstepnum,stridenum,restoredstridenum,laststridesize,store;
1330:   StackElement    e;
1331:   PetscRevolveInt whattodo,shift;
1332:   PetscRevolveInt rtotal_steps,rstepnum,rlocalstepnum;

1334:   localstepnum = stepnum%tjsch->stride;
1335:   stridenum    = stepnum/tjsch->stride;
1336:   if (stepnum == tjsch->total_steps) {
1337:     TurnBackward(ts);
1338:     tjsch->rctx->reverseonestep = PETSC_FALSE;
1339:     return 0;
1340:   }
1341:   laststridesize = tjsch->total_steps%tjsch->stride;
1342:   if (!laststridesize) laststridesize = tjsch->stride;
1343:   /*
1344:    Last stride can be adjoined directly. All the other strides require that the stack in memory be ready before an adjoint step is taken (at the end of each stride). The following two cases need to be addressed differently:
1345:      Case 1 (save_stack)
1346:        Restore a disk checkpoint; update TS with the last element in the restored data; recompute to the current point.
1347:      Case 2 (!save_stack)
1348:        Restore a disk checkpoint; update TS with the restored point; recompute to the current point.
1349:   */
1350:   if (localstepnum == 0 && stepnum <= tjsch->total_steps-laststridesize) {
1351:     /* restore the top element in the stack for disk checkpoints */
1352:     restoredstridenum = diskstack->container[diskstack->top];
1353:     tjsch->rctx2->reverseonestep = PETSC_FALSE;
1354:     /* top-level revolve must be applied before current step, just like the solution_only mode for single-level revolve */
1355:     if (!tjsch->save_stack && stack->solution_only) { /* start with restoring a checkpoint */
1356:       PetscRevolveIntCast(stridenum,&rstepnum);
1357:       tjsch->rctx2->capo = rstepnum;
1358:       tjsch->rctx2->oldcapo = tjsch->rctx2->capo;
1359:       shift = 0;
1360:       whattodo = revolve2_action(&tjsch->rctx2->check,&tjsch->rctx2->capo,&tjsch->rctx2->fine,tjsch->rctx2->snaps_in,&tjsch->rctx2->info,&tjsch->rctx2->where);
1361:       printwhattodo2(tj->monitor,whattodo,tjsch->rctx2,shift);
1362:     } else { /* 2 revolve actions: restore a checkpoint and then advance */
1363:       PetscRevolveIntCast((tjsch->total_steps+tjsch->stride-1)/tjsch->stride,&rtotal_steps);
1364:       PetscRevolveIntCast(stridenum,&rstepnum);
1365:       ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx2,rtotal_steps,rstepnum,rstepnum,PETSC_TRUE,&tjsch->store_stride);
1366:       if (tj->monitor) {
1367:         PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
1368:         PetscViewerASCIIPrintf(tj->monitor,"[Top Level] Skip the stride from %D to %D (stage values already checkpointed)\n",tjsch->rctx2->oldcapo,tjsch->rctx2->oldcapo+1);
1369:         PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
1370:       }
1371:       if (!tjsch->rctx2->reverseonestep && tjsch->rctx2->stepsleft > 0) tjsch->rctx2->stepsleft--;
1372:     }
1373:     /* fill stack */
1374:     if (stack->solution_only) {
1375:       if (tjsch->save_stack) {
1376:         if (restoredstridenum < stridenum) {
1377:           StackLoadLast(tj,ts,stack,restoredstridenum);
1378:         } else {
1379:           StackLoadAll(tj,ts,stack,restoredstridenum);
1380:         }
1381:         /* recompute one step ahead */
1382:         tjsch->skip_trajectory = PETSC_TRUE;
1383:         TurnForward(ts);
1384:         ReCompute(ts,tjsch,stridenum*tjsch->stride-1,stridenum*tjsch->stride);
1385:         tjsch->skip_trajectory = PETSC_FALSE;
1386:         if (restoredstridenum < stridenum) {
1387:           InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1388:           TurnForward(ts);
1389:           ReCompute(ts,tjsch,restoredstridenum*tjsch->stride,stepnum);
1390:         } else { /* stack ready, fast forward revolve status */
1391:           InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1392:           FastForwardRevolve(tjsch->rctx);
1393:         }
1394:       } else {
1395:         LoadSingle(tj,ts,stack,restoredstridenum);
1396:         InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1397:         TurnForward(ts);
1398:         ReCompute(ts,tjsch,(restoredstridenum-1)*tjsch->stride,stepnum);
1399:       }
1400:     } else {
1401:       if (tjsch->save_stack) {
1402:         if (restoredstridenum < stridenum) {
1403:           StackLoadLast(tj,ts,stack,restoredstridenum);
1404:           /* reset revolve */
1405:           InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1406:           TurnForward(ts);
1407:           ReCompute(ts,tjsch,restoredstridenum*tjsch->stride,stepnum);
1408:         } else { /* stack ready, fast forward revolve status */
1409:           StackLoadAll(tj,ts,stack,restoredstridenum);
1410:           InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1411:           FastForwardRevolve(tjsch->rctx);
1412:         }
1413:       } else {
1414:         LoadSingle(tj,ts,stack,restoredstridenum);
1415:         InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1416:         /* push first element to stack */
1417:         if (tjsch->store_stride || tjsch->rctx2->reverseonestep) {
1418:           CheckpointType cptype = SOLUTION_STAGES;
1419:           shift = (restoredstridenum-1)*tjsch->stride-localstepnum;
1420:           PetscRevolveIntCast(tjsch->total_steps,&rtotal_steps);
1421:           PetscRevolveIntCast((restoredstridenum-1)*tjsch->stride+1,&rstepnum);
1422:           ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,rtotal_steps,rstepnum,1,PETSC_FALSE,&store);
1423:           if (tj->monitor) {
1424:             PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
1425:             PetscViewerASCIIPrintf(tj->monitor,"Skip the step from %D to %D (stage values already checkpointed)\n",(restoredstridenum-1)*tjsch->stride,(restoredstridenum-1)*tjsch->stride+1);
1426:             PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
1427:           }
1428:           ElementCreate(ts,cptype,stack,&e);
1429:           ElementSet(ts,stack,&e,(restoredstridenum-1)*tjsch->stride+1,ts->ptime,ts->vec_sol);
1430:           StackPush(stack,e);
1431:         }
1432:         TurnForward(ts);
1433:         ReCompute(ts,tjsch,(restoredstridenum-1)*tjsch->stride+1,stepnum);
1434:       }
1435:     }
1436:     if (restoredstridenum == stridenum) diskstack->top--;
1437:     tjsch->rctx->reverseonestep = PETSC_FALSE;
1438:     return 0;
1439:   }

1441:   if (stack->solution_only) {
1442:     /* restore a checkpoint */
1443:     StackTop(stack,&e);
1444:     UpdateTS(ts,stack,e,stepnum,PETSC_TRUE);
1445:     /* start with restoring a checkpoint */
1446:     PetscRevolveIntCast(stepnum,&rstepnum);
1447:     PetscRevolveIntCast(localstepnum,&rlocalstepnum);
1448:     tjsch->rctx->capo = rstepnum;
1449:     tjsch->rctx->oldcapo = tjsch->rctx->capo;
1450:     shift = rstepnum-rlocalstepnum;
1451:     whattodo = revolve_action(&tjsch->rctx->check,&tjsch->rctx->capo,&tjsch->rctx->fine,tjsch->rctx->snaps_in,&tjsch->rctx->info,&tjsch->rctx->where);
1452:     printwhattodo(tj->monitor,whattodo,tjsch->rctx,shift);
1453:     TurnForward(ts);
1454:     ReCompute(ts,tjsch,e->stepnum,stepnum);
1455:     if (e->stepnum+1 == stepnum) {
1456:       StackPop(stack,&e);
1457:     }
1458:   } else {
1459:     PetscRevolveInt rlocalstepnum;
1460:     /* restore a checkpoint */
1461:     StackTop(stack,&e);
1462:     UpdateTS(ts,stack,e,stepnum,PETSC_TRUE);
1463:     /* 2 revolve actions: restore a checkpoint and then advance */
1464:     PetscRevolveIntCast(tjsch->total_steps,&rtotal_steps);
1465:     PetscRevolveIntCast(stridenum,&rstepnum);
1466:     PetscRevolveIntCast(localstepnum,&rlocalstepnum);
1467:     ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,rtotal_steps,rstepnum,rlocalstepnum,PETSC_FALSE,&store);
1468:     if (tj->monitor) {
1469:       PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
1470:       PetscViewerASCIIPrintf(tj->monitor,"Skip the step from %D to %D (stage values already checkpointed)\n",stepnum-localstepnum+tjsch->rctx->oldcapo,stepnum-localstepnum+tjsch->rctx->oldcapo+1);
1471:       PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
1472:     }
1473:     if (!tjsch->rctx->reverseonestep && tjsch->rctx->stepsleft > 0) tjsch->rctx->stepsleft--;
1474:     if (e->stepnum < stepnum) {
1475:       TurnForward(ts);
1476:       ReCompute(ts,tjsch,e->stepnum,stepnum);
1477:     }
1478:     if (e->stepnum == stepnum) {
1479:       StackPop(stack,&e);
1480:     }
1481:   }
1482:   tjsch->rctx->reverseonestep = PETSC_FALSE;
1483:   return 0;
1484: }

1486: static PetscErrorCode TSTrajectoryMemorySet_RMS(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscReal time,Vec X)
1487: {
1488:   Stack           *stack = &tjsch->stack;
1489:   PetscInt        store;
1490:   StackElement    e;
1491:   PetscRevolveInt rtotal_steps,rstepnum;

1493:   if (!stack->solution_only && stepnum == 0) return 0;
1494:   if (stack->solution_only && stepnum == tjsch->total_steps) return 0;
1495:   PetscRevolveIntCast(tjsch->total_steps,&rtotal_steps);
1496:   PetscRevolveIntCast(stepnum,&rstepnum);
1497:   ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,rtotal_steps,rstepnum,rstepnum,PETSC_FALSE,&store);
1498:   if (store == 1) {
1499:     CheckpointType cptype;
1501:     cptype = stack->solution_only ? SOLUTIONONLY : SOLUTION_STAGES;
1502:     ElementCreate(ts,cptype,stack,&e);
1503:     ElementSet(ts,stack,&e,stepnum,time,X);
1504:     StackPush(stack,e);
1505:   } else if (store == 2) {
1506:     DumpSingle(tj,ts,stack,tjsch->rctx->check+1);
1507:   }
1508:   return 0;
1509: }

1511: static PetscErrorCode TSTrajectoryMemoryGet_RMS(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum)
1512: {
1513:   Stack           *stack = &tjsch->stack;
1514:   PetscRevolveInt whattodo,shift,rstepnum;
1515:   PetscInt        restart;
1516:   PetscBool       ondisk;
1517:   StackElement    e;

1519:   if (stepnum == 0 || stepnum == tjsch->total_steps) {
1520:     TurnBackward(ts);
1521:     tjsch->rctx->reverseonestep = PETSC_FALSE;
1522:     return 0;
1523:   }
1524:   PetscRevolveIntCast(stepnum,&rstepnum);
1525:   tjsch->rctx->capo = rstepnum;
1526:   tjsch->rctx->oldcapo = tjsch->rctx->capo;
1527:   shift = 0;
1528:   whattodo = revolve_action(&tjsch->rctx->check,&tjsch->rctx->capo,&tjsch->rctx->fine,tjsch->rctx->snaps_in,&tjsch->rctx->info,&tjsch->rctx->where); /* whattodo=restore */
1529:   printwhattodo(tj->monitor,whattodo,tjsch->rctx,shift);
1530:   /* restore a checkpoint */
1531:   restart = tjsch->rctx->capo;
1532:   if (!tjsch->rctx->where) {
1533:     ondisk = PETSC_TRUE;
1534:     LoadSingle(tj,ts,stack,tjsch->rctx->check+1);
1535:     TurnBackward(ts);
1536:   } else {
1537:     ondisk = PETSC_FALSE;
1538:     StackTop(stack,&e);
1539:     UpdateTS(ts,stack,e,stepnum,PETSC_TRUE);
1540:   }
1541:   if (!stack->solution_only) { /* whattodo must be 5 or 8 */
1542:     /* ask Revolve what to do next */
1543:     tjsch->rctx->oldcapo = tjsch->rctx->capo;
1544:     whattodo = revolve_action(&tjsch->rctx->check,&tjsch->rctx->capo,&tjsch->rctx->fine,tjsch->rctx->snaps_in,&tjsch->rctx->info,&tjsch->rctx->where); /* must return 1 or 3 or 4*/
1545:     printwhattodo(tj->monitor,whattodo,tjsch->rctx,shift);
1546:     if (whattodo == 3 || whattodo == 4) tjsch->rctx->reverseonestep = PETSC_TRUE;
1547:     if (whattodo == 1) tjsch->rctx->stepsleft = tjsch->rctx->capo-tjsch->rctx->oldcapo;
1548:     if (tj->monitor) {
1549:       PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
1550:       PetscViewerASCIIPrintf(tj->monitor,"Skip the step from %D to %D (stage values already checkpointed)\n",tjsch->rctx->oldcapo,tjsch->rctx->oldcapo+1);
1551:       PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
1552:     }
1553:     if (!tjsch->rctx->reverseonestep && tjsch->rctx->stepsleft > 0) tjsch->rctx->stepsleft--;
1554:     restart++; /* skip one step */
1555:   }
1556:   if (stack->solution_only || (!stack->solution_only && restart < stepnum)) {
1557:     TurnForward(ts);
1558:     ReCompute(ts,tjsch,restart,stepnum);
1559:   }
1560:   if (!ondisk && ( (stack->solution_only && e->stepnum+1 == stepnum) || (!stack->solution_only && e->stepnum == stepnum))) {
1561:     StackPop(stack,&e);
1562:   }
1563:   tjsch->rctx->reverseonestep = PETSC_FALSE;
1564:   return 0;
1565: }
1566: #endif

1568: #if defined(PETSC_HAVE_CAMS)
1569: /* Optimal offline adjoint checkpointing for multistage time integration methods */
1570: static PetscErrorCode TSTrajectoryMemorySet_AOF(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscReal time,Vec X)
1571: {
1572:   Stack        *stack = &tjsch->stack;
1573:   StackElement  e;

1575:   /* skip if no checkpoint to use. This also avoids an error when num_units_avail=0  */
1576:   if (tjsch->actx->nextcheckpointstep == -1) return 0;
1577:   if (stepnum == 0) { /* When placing the first checkpoint, no need to change the units available */
1578:     if (stack->solution_only) {
1579:       offline_ca(tjsch->actx->lastcheckpointstep,tjsch->actx->num_units_avail,tjsch->actx->endstep,&tjsch->actx->nextcheckpointstep);
1580:     } else {
1581:       /* First two arguments must be -1 when first time calling cams */
1582:       offline_cams(tjsch->actx->lastcheckpointstep,tjsch->actx->lastcheckpointtype,tjsch->actx->num_units_avail,tjsch->actx->endstep,tjsch->actx->num_stages,&tjsch->actx->nextcheckpointstep,&tjsch->actx->nextcheckpointtype);
1583:     }
1584:   }

1586:   if (stack->solution_only && stepnum == tjsch->total_steps) return 0;

1588:   if (tjsch->actx->nextcheckpointstep == stepnum) {

1591:     if (tjsch->actx->nextcheckpointtype == 2) { /* solution + stage values */
1592:       if (tj->monitor) {
1593:         PetscViewerASCIIPrintf(tj->monitor,"Store in checkpoint number %D with stage values and solution (located in RAM)\n",stepnum);
1594:       }
1595:       ElementCreate(ts,SOLUTION_STAGES,stack,&e);
1596:       ElementSet(ts,stack,&e,stepnum,time,X);
1597:     }
1598:     if (tjsch->actx->nextcheckpointtype == 1) {
1599:       if (tj->monitor) {
1600:         PetscViewerASCIIPrintf(tj->monitor,"Store in checkpoint number %D with stage values (located in RAM)\n",stepnum);
1601:       }
1602:       ElementCreate(ts,STAGESONLY,stack,&e);
1603:       ElementSet(ts,stack,&e,stepnum,time,X);
1604:     }
1605:     if (tjsch->actx->nextcheckpointtype == 0) { /* solution only */
1606:       if (tj->monitor) {
1607:         PetscViewerASCIIPrintf(tj->monitor,"Store in checkpoint number %D (located in RAM)\n",stepnum);
1608:       }
1609:       ElementCreate(ts,SOLUTIONONLY,stack,&e);
1610:       ElementSet(ts,stack,&e,stepnum,time,X);
1611:     }
1612:     StackPush(stack,e);

1614:     tjsch->actx->lastcheckpointstep = stepnum;
1615:     if (stack->solution_only) {
1616:       offline_ca(tjsch->actx->lastcheckpointstep,tjsch->actx->num_units_avail,tjsch->actx->endstep,&tjsch->actx->nextcheckpointstep);
1617:       tjsch->actx->num_units_avail--;
1618:     } else {
1619:       tjsch->actx->lastcheckpointtype = tjsch->actx->nextcheckpointtype;
1620:       offline_cams(tjsch->actx->lastcheckpointstep,tjsch->actx->lastcheckpointtype,tjsch->actx->num_units_avail,tjsch->actx->endstep,tjsch->actx->num_stages,&tjsch->actx->nextcheckpointstep,&tjsch->actx->nextcheckpointtype);
1621:       if (tjsch->actx->lastcheckpointtype == 2) tjsch->actx->num_units_avail -= tjsch->actx->num_stages+1;
1622:       if (tjsch->actx->lastcheckpointtype == 1) tjsch->actx->num_units_avail -= tjsch->actx->num_stages;
1623:       if (tjsch->actx->lastcheckpointtype == 0) tjsch->actx->num_units_avail--;
1624:     }
1625:   }
1626:   return 0;
1627: }

1629: static PetscErrorCode TSTrajectoryMemoryGet_AOF(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum)
1630: {
1631:   Stack        *stack = &tjsch->stack;
1632:   StackElement  e;
1633:   PetscInt      estepnum;

1635:   if (stepnum == 0 || stepnum == tjsch->total_steps) {
1636:     TurnBackward(ts);
1637:     return 0;
1638:   }
1639:   /* Restore a checkpoint */
1640:   StackTop(stack,&e);
1641:   estepnum = e->stepnum;
1642:   if (estepnum == stepnum && e->cptype == SOLUTIONONLY) { /* discard the checkpoint if not useful (corner case) */
1643:     StackPop(stack,&e);
1644:     tjsch->actx->num_units_avail++;
1645:     StackTop(stack,&e);
1646:     estepnum = e->stepnum;
1647:   }
1648:   /* Update TS with stage values if an adjoint step can be taken immediately */
1649:   if (HaveStages(e->cptype)) {
1650:     if (tj->monitor) {
1651:       PetscViewerASCIIPrintf(tj->monitor,"Restore in checkpoint number %D with stage values (located in RAM)\n",e->stepnum);
1652:     }
1653:     if (e->cptype == STAGESONLY) tjsch->actx->num_units_avail += tjsch->actx->num_stages;
1654:     if (e->cptype == SOLUTION_STAGES) tjsch->actx->num_units_avail += tjsch->actx->num_stages+1;
1655:   } else {
1656:     if (tj->monitor) {
1657:       PetscViewerASCIIPrintf(tj->monitor,"Restore in checkpoint number %D (located in RAM)\n",e->stepnum);
1658:     }
1659:     tjsch->actx->num_units_avail++;
1660:   }
1661:   UpdateTS(ts,stack,e,stepnum,PETSC_TRUE);
1662:   /* Query the scheduler */
1663:   tjsch->actx->lastcheckpointstep = estepnum;
1664:   tjsch->actx->endstep = stepnum;
1665:   if (stack->solution_only) { /* start with restoring a checkpoint */
1666:     offline_ca(tjsch->actx->lastcheckpointstep,tjsch->actx->num_units_avail,tjsch->actx->endstep,&tjsch->actx->nextcheckpointstep);
1667:   } else { /* 2 revolve actions: restore a checkpoint and then advance */
1668:     tjsch->actx->lastcheckpointtype = e->cptype;
1669:     offline_cams(tjsch->actx->lastcheckpointstep,tjsch->actx->lastcheckpointtype,tjsch->actx->num_units_avail,tjsch->actx->endstep,tjsch->actx->num_stages,&tjsch->actx->nextcheckpointstep, &tjsch->actx->nextcheckpointtype);
1670:   }
1671:   /* Discard the checkpoint if not needed, decrease the number of available checkpoints if it still stays in stack */
1672:   if (HaveStages(e->cptype)) {
1673:     if (estepnum == stepnum) {
1674:       StackPop(stack,&e);
1675:     } else {
1676:       if (e->cptype == STAGESONLY) tjsch->actx->num_units_avail -= tjsch->actx->num_stages;
1677:       if (e->cptype == SOLUTION_STAGES) tjsch->actx->num_units_avail -= tjsch->actx->num_stages+1;
1678:     }
1679:   } else {
1680:     if (estepnum+1 == stepnum) {
1681:       StackPop(stack,&e);
1682:     } else {
1683:       tjsch->actx->num_units_avail--;
1684:     }
1685:   }
1686:   /* Recompute from the restored checkpoint */
1687:   if (stack->solution_only || (!stack->solution_only && estepnum < stepnum)) {
1688:     TurnForward(ts);
1689:     ReCompute(ts,tjsch,estepnum,stepnum);
1690:   }
1691:   return 0;
1692: }
1693: #endif

1695: static PetscErrorCode TSTrajectorySet_Memory(TSTrajectory tj,TS ts,PetscInt stepnum,PetscReal time,Vec X)
1696: {
1697:   TJScheduler *tjsch = (TJScheduler*)tj->data;

1699:   if (!tjsch->recompute) { /* use global stepnum in the forward sweep */
1700:     TSGetStepNumber(ts,&stepnum);
1701:   }
1702:   /* for consistency */
1703:   if (!tjsch->recompute && stepnum == 0) ts->ptime_prev = ts->ptime-ts->time_step;
1704:   switch (tjsch->stype) {
1705:     case NONE:
1706:       if (tj->adjoint_solve_mode) {
1707:         TSTrajectoryMemorySet_N(ts,tjsch,stepnum,time,X);
1708:       } else {
1709:         TSTrajectoryMemorySet_N_2(ts,tjsch,stepnum,time,X);
1710:       }
1711:       break;
1712:     case TWO_LEVEL_NOREVOLVE:
1714:       TSTrajectoryMemorySet_TLNR(tj,ts,tjsch,stepnum,time,X);
1715:       break;
1716: #if defined(PETSC_HAVE_REVOLVE)
1717:     case TWO_LEVEL_REVOLVE:
1719:       TSTrajectoryMemorySet_TLR(tj,ts,tjsch,stepnum,time,X);
1720:       break;
1721:     case TWO_LEVEL_TWO_REVOLVE:
1723:       TSTrajectoryMemorySet_TLTR(tj,ts,tjsch,stepnum,time,X);
1724:       break;
1725:     case REVOLVE_OFFLINE:
1727:       TSTrajectoryMemorySet_ROF(tj,ts,tjsch,stepnum,time,X);
1728:       break;
1729:     case REVOLVE_ONLINE:
1731:       TSTrajectoryMemorySet_RON(tj,ts,tjsch,stepnum,time,X);
1732:       break;
1733:     case REVOLVE_MULTISTAGE:
1735:       TSTrajectoryMemorySet_RMS(tj,ts,tjsch,stepnum,time,X);
1736:       break;
1737: #endif
1738: #if defined(PETSC_HAVE_CAMS)
1739:     case CAMS_OFFLINE:
1741:       TSTrajectoryMemorySet_AOF(tj,ts,tjsch,stepnum,time,X);
1742:       break;
1743: #endif
1744:     default:
1745:       break;
1746:   }
1747:   return 0;
1748: }

1750: static PetscErrorCode TSTrajectoryGet_Memory(TSTrajectory tj,TS ts,PetscInt stepnum,PetscReal *t)
1751: {
1752:   TJScheduler *tjsch = (TJScheduler*)tj->data;

1754:   if (tj->adjoint_solve_mode && stepnum == 0) {
1755:     TSTrajectoryReset(tj); /* reset TSTrajectory so users do not need to reset TSTrajectory */
1756:     return 0;
1757:   }
1758:   switch (tjsch->stype) {
1759:     case NONE:
1760:       if (tj->adjoint_solve_mode) {
1761:         TSTrajectoryMemoryGet_N(ts,tjsch,stepnum);
1762:       } else {
1763:         TSTrajectoryMemoryGet_N_2(ts,tjsch,stepnum);
1764:       }
1765:       break;
1766:     case TWO_LEVEL_NOREVOLVE:
1768:       TSTrajectoryMemoryGet_TLNR(tj,ts,tjsch,stepnum);
1769:       break;
1770: #if defined(PETSC_HAVE_REVOLVE)
1771:     case TWO_LEVEL_REVOLVE:
1773:       TSTrajectoryMemoryGet_TLR(tj,ts,tjsch,stepnum);
1774:       break;
1775:     case TWO_LEVEL_TWO_REVOLVE:
1777:       TSTrajectoryMemoryGet_TLTR(tj,ts,tjsch,stepnum);
1778:       break;
1779:     case REVOLVE_OFFLINE:
1781:       TSTrajectoryMemoryGet_ROF(tj,ts,tjsch,stepnum);
1782:       break;
1783:     case REVOLVE_ONLINE:
1785:       TSTrajectoryMemoryGet_RON(tj,ts,tjsch,stepnum);
1786:       break;
1787:     case REVOLVE_MULTISTAGE:
1789:       TSTrajectoryMemoryGet_RMS(tj,ts,tjsch,stepnum);
1790:       break;
1791: #endif
1792: #if defined(PETSC_HAVE_CAMS)
1793:     case CAMS_OFFLINE:
1795:       TSTrajectoryMemoryGet_AOF(tj,ts,tjsch,stepnum);
1796:       break;
1797: #endif
1798:     default:
1799:       break;
1800:   }
1801:   return 0;
1802: }

1804: PETSC_UNUSED static PetscErrorCode TSTrajectorySetStride_Memory(TSTrajectory tj,PetscInt stride)
1805: {
1806:   TJScheduler *tjsch = (TJScheduler*)tj->data;

1808:   tjsch->stride = stride;
1809:   return 0;
1810: }

1812: static PetscErrorCode TSTrajectorySetMaxCpsRAM_Memory(TSTrajectory tj,PetscInt max_cps_ram)
1813: {
1814:   TJScheduler *tjsch = (TJScheduler*)tj->data;

1816:   tjsch->max_cps_ram = max_cps_ram;
1817:   return 0;
1818: }

1820: static PetscErrorCode TSTrajectorySetMaxCpsDisk_Memory(TSTrajectory tj,PetscInt max_cps_disk)
1821: {
1822:   TJScheduler *tjsch = (TJScheduler*)tj->data;

1824:   tjsch->max_cps_disk = max_cps_disk;
1825:   return 0;
1826: }

1828: static PetscErrorCode TSTrajectorySetMaxUnitsRAM_Memory(TSTrajectory tj,PetscInt max_units_ram)
1829: {
1830:   TJScheduler *tjsch = (TJScheduler*)tj->data;

1833:   tjsch->max_units_ram = max_units_ram;
1834:   return 0;
1835: }

1837: static PetscErrorCode TSTrajectorySetMaxUnitsDisk_Memory(TSTrajectory tj,PetscInt max_units_disk)
1838: {
1839:   TJScheduler *tjsch = (TJScheduler*)tj->data;

1842:   tjsch->max_units_ram = max_units_disk;
1843:   return 0;
1844: }

1846: static PetscErrorCode TSTrajectoryMemorySetType_Memory(TSTrajectory tj,TSTrajectoryMemoryType tj_memory_type)
1847: {
1848:   TJScheduler *tjsch = (TJScheduler*)tj->data;

1851:   tjsch->tj_memory_type = tj_memory_type;
1852:   return 0;
1853: }

1855: #if defined(PETSC_HAVE_REVOLVE)
1856: PETSC_UNUSED static PetscErrorCode TSTrajectorySetRevolveOnline(TSTrajectory tj,PetscBool use_online)
1857: {
1858:   TJScheduler *tjsch = (TJScheduler*)tj->data;

1860:   tjsch->use_online = use_online;
1861:   return 0;
1862: }
1863: #endif

1865: PETSC_UNUSED static PetscErrorCode TSTrajectorySetSaveStack(TSTrajectory tj,PetscBool save_stack)
1866: {
1867:   TJScheduler *tjsch = (TJScheduler*)tj->data;

1869:   tjsch->save_stack = save_stack;
1870:   return 0;
1871: }

1873: PETSC_UNUSED static PetscErrorCode TSTrajectorySetUseDRAM(TSTrajectory tj,PetscBool use_dram)
1874: {
1875:   TJScheduler *tjsch = (TJScheduler*)tj->data;

1877:   tjsch->stack.use_dram = use_dram;
1878:   return 0;
1879: }

1881: /*@C
1882:    TSTrajectoryMemorySetType - sets the software that is used to generate the checkpointing schedule.

1884:    Logically Collective on TSTrajectory

1886:    Input Parameters:
1887: +  tj - the TSTrajectory context
1888: -  tj_memory_type - Revolve or CAMS

1890:    Options Database Key:
1891: .  -ts_trajectory_memory_type <tj_memory_type> - petsc, revolve, cams

1893:    Level: intermediate

1895:    Note:
1896:      By default this will use Revolve if it exists
1897: @*/
1898: PetscErrorCode TSTrajectoryMemorySetType(TSTrajectory tj,TSTrajectoryMemoryType tj_memory_type)
1899: {
1900:   PetscTryMethod(tj,"TSTrajectoryMemorySetType_C",(TSTrajectory,TSTrajectoryMemoryType),(tj,tj_memory_type));
1901:   return 0;
1902: }

1904: /*@C
1905:   TSTrajectorySetMaxCpsRAM - Set maximum number of checkpoints in RAM

1907:   Logically collective

1909:   Input Parameter:
1910: .  tj - tstrajectory context

1912:   Output Parameter:
1913: .  max_cps_ram - maximum number of checkpoints in RAM

1915:   Level: intermediate

1917: .seealso: TSTrajectorySetMaxUnitsRAM()
1918: @*/
1919: PetscErrorCode TSTrajectorySetMaxCpsRAM(TSTrajectory tj,PetscInt max_cps_ram)
1920: {
1921:   PetscUseMethod(tj,"TSTrajectorySetMaxCpsRAM_C",(TSTrajectory,PetscInt),(tj,max_cps_ram));
1922:   return 0;
1923: }

1925: /*@C
1926:   TSTrajectorySetMaxCpsDisk - Set maximum number of checkpoints on disk

1928:   Logically collective

1930:   Input Parameter:
1931: .  tj - tstrajectory context

1933:   Output Parameter:
1934: .  max_cps_disk - maximum number of checkpoints on disk

1936:   Level: intermediate

1938: .seealso: TSTrajectorySetMaxUnitsDisk(), TSTrajectorySetMaxUnitsRAM()
1939: @*/
1940: PetscErrorCode TSTrajectorySetMaxCpsDisk(TSTrajectory tj,PetscInt max_cps_disk)
1941: {
1942:   PetscUseMethod(tj,"TSTrajectorySetMaxCpsDisk_C",(TSTrajectory,PetscInt),(tj,max_cps_disk));
1943:   return 0;
1944: }

1946: /*@C
1947:   TSTrajectorySetMaxUnitsRAM - Set maximum number of checkpointing units in RAM

1949:   Logically collective

1951:   Input Parameter:
1952: .  tj - tstrajectory context

1954:   Output Parameter:
1955: .  max_units_ram - maximum number of checkpointing units in RAM

1957:   Level: intermediate

1959: .seealso: TSTrajectorySetMaxCpsRAM()
1960: @*/
1961: PetscErrorCode TSTrajectorySetMaxUnitsRAM(TSTrajectory tj,PetscInt max_units_ram)
1962: {
1963:   PetscUseMethod(tj,"TSTrajectorySetMaxUnitsRAM_C",(TSTrajectory,PetscInt),(tj,max_units_ram));
1964:   return 0;
1965: }

1967: /*@C
1968:   TSTrajectorySetMaxUnitsDisk - Set maximum number of checkpointing units on disk

1970:   Logically collective

1972:   Input Parameter:
1973: .  tj - tstrajectory context

1975:   Output Parameter:
1976: .  max_units_disk - maximum number of checkpointing units on disk

1978:   Level: intermediate

1980: .seealso: TSTrajectorySetMaxCpsDisk()
1981: @*/
1982: PetscErrorCode TSTrajectorySetMaxUnitsDisk(TSTrajectory tj,PetscInt max_units_disk)
1983: {
1984:   PetscUseMethod(tj,"TSTrajectorySetMaxUnitsDisk_C",(TSTrajectory,PetscInt),(tj,max_units_disk));
1985:   return 0;
1986: }

1988: static PetscErrorCode TSTrajectorySetFromOptions_Memory(PetscOptionItems *PetscOptionsObject,TSTrajectory tj)
1989: {
1990:   TJScheduler    *tjsch = (TJScheduler*)tj->data;
1991:   PetscEnum      etmp;
1992:   PetscInt       max_cps_ram,max_cps_disk,max_units_ram,max_units_disk;
1993:   PetscBool      flg;

1995:   PetscOptionsHead(PetscOptionsObject,"Memory based TS trajectory options");
1996:   {
1997:     PetscOptionsInt("-ts_trajectory_max_cps_ram","Maximum number of checkpoints in RAM","TSTrajectorySetMaxCpsRAM",tjsch->max_cps_ram,&max_cps_ram,&flg);
1998:     if (flg) {
1999:       TSTrajectorySetMaxCpsRAM(tj,max_cps_ram);
2000:     }
2001:     PetscOptionsInt("-ts_trajectory_max_cps_disk","Maximum number of checkpoints on disk","TSTrajectorySetMaxCpsDisk",tjsch->max_cps_disk,&max_cps_disk,&flg);
2002:     if (flg) {
2003:       TSTrajectorySetMaxCpsDisk(tj,max_cps_disk);
2004:     }
2005:     PetscOptionsInt("-ts_trajectory_max_units_ram","Maximum number of checkpointing units in RAM","TSTrajectorySetMaxUnitsRAM",tjsch->max_units_ram,&max_units_ram,&flg);
2006:     if (flg) {
2007:       TSTrajectorySetMaxUnitsRAM(tj,max_units_ram);
2008:     }
2009:     PetscOptionsInt("-ts_trajectory_max_units_disk","Maximum number of checkpointing units on disk","TSTrajectorySetMaxUnitsDisk",tjsch->max_units_disk,&max_units_disk,&flg);
2010:     if (flg) {
2011:       TSTrajectorySetMaxUnitsDisk(tj,max_units_disk);
2012:     }
2013:     PetscOptionsInt("-ts_trajectory_stride","Stride to save checkpoints to file","TSTrajectorySetStride",tjsch->stride,&tjsch->stride,NULL);
2014: #if defined(PETSC_HAVE_REVOLVE)
2015:     PetscOptionsBool("-ts_trajectory_revolve_online","Trick TS trajectory into using online mode of revolve","TSTrajectorySetRevolveOnline",tjsch->use_online,&tjsch->use_online,NULL);
2016: #endif
2017:     PetscOptionsBool("-ts_trajectory_save_stack","Save all stack to disk","TSTrajectorySetSaveStack",tjsch->save_stack,&tjsch->save_stack,NULL);
2018:     PetscOptionsBool("-ts_trajectory_use_dram","Use DRAM for checkpointing","TSTrajectorySetUseDRAM",tjsch->stack.use_dram,&tjsch->stack.use_dram,NULL);
2019:     PetscOptionsEnum("-ts_trajectory_memory_type","Checkpointing scchedule software to use","TSTrajectoryMemorySetType",TSTrajectoryMemoryTypes,(PetscEnum)(int)(tjsch->tj_memory_type),&etmp,&flg);
2020:     if (flg) {
2021:       TSTrajectoryMemorySetType(tj,(TSTrajectoryMemoryType)etmp);
2022:     }
2023:   }
2024:   PetscOptionsTail();
2025:   return 0;
2026: }

2028: static PetscErrorCode TSTrajectorySetUp_Memory(TSTrajectory tj,TS ts)
2029: {
2030:   TJScheduler    *tjsch = (TJScheduler*)tj->data;
2031:   Stack          *stack = &tjsch->stack;
2032: #if defined(PETSC_HAVE_REVOLVE)
2033:   RevolveCTX     *rctx,*rctx2;
2034:   DiskStack      *diskstack = &tjsch->diskstack;
2035:   PetscInt       diskblocks;
2036: #endif
2037:   PetscInt       numY,total_steps;
2038:   PetscBool      fixedtimestep;

2040:   if (ts->adapt) {
2041:     PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&fixedtimestep);
2042:   } else {
2043:     fixedtimestep = PETSC_TRUE;
2044:   }
2045:   total_steps = (PetscInt)(PetscCeilReal((ts->max_time-ts->ptime)/ts->time_step));
2046:   total_steps = total_steps < 0 ? PETSC_MAX_INT : total_steps;
2047:   if (fixedtimestep) tjsch->total_steps = PetscMin(ts->max_steps,total_steps);

2049:   tjsch->stack.solution_only = tj->solution_only;
2050:   TSGetStages(ts,&numY,PETSC_IGNORE);
2051:   if (stack->solution_only) {
2052:     if (tjsch->max_units_ram) tjsch->max_cps_ram = tjsch->max_units_ram;
2053:     else tjsch->max_units_ram = tjsch->max_cps_ram;
2054:     if (tjsch->max_units_disk) tjsch->max_cps_disk = tjsch->max_units_disk;
2055:   } else {
2056:     if (tjsch->max_units_ram) tjsch->max_cps_ram = (ts->stifflyaccurate) ? tjsch->max_units_ram/numY : tjsch->max_units_ram/(numY+1);
2057:     else tjsch->max_units_ram = (ts->stifflyaccurate) ? numY*tjsch->max_cps_ram : (numY+1)*tjsch->max_cps_ram;
2058:     if (tjsch->max_units_disk) tjsch->max_cps_disk = (ts->stifflyaccurate) ? tjsch->max_units_disk/numY : tjsch->max_units_disk/(numY+1);
2059:     else tjsch->max_units_disk = (ts->stifflyaccurate) ? numY*tjsch->max_cps_disk : (numY+1)*tjsch->max_cps_disk;
2060:   }
2061:   if (tjsch->max_cps_ram > 0) stack->stacksize = tjsch->max_units_ram; /* maximum stack size. Could be overallocated. */

2063:   /* Determine the scheduler type */
2064:   if (tjsch->stride > 1) { /* two level mode */
2066:     if (tjsch->max_cps_disk <= 1 && tjsch->max_cps_ram > 1 && tjsch->max_cps_ram <= tjsch->stride-1) tjsch->stype = TWO_LEVEL_REVOLVE; /* use revolve_offline for each stride */
2067:     if (tjsch->max_cps_disk > 1 && tjsch->max_cps_ram > 1 && tjsch->max_cps_ram <= tjsch->stride-1) tjsch->stype = TWO_LEVEL_TWO_REVOLVE;  /* use revolve_offline for each stride */
2068:     if (tjsch->max_cps_disk <= 1 && (tjsch->max_cps_ram >= tjsch->stride || tjsch->max_cps_ram == -1)) tjsch->stype = TWO_LEVEL_NOREVOLVE; /* can also be handled by TWO_LEVEL_REVOLVE */
2069:   } else { /* single level mode */
2070:     if (fixedtimestep) {
2071:       if (tjsch->max_cps_ram >= tjsch->total_steps-1 || tjsch->max_cps_ram == -1)
2072:         tjsch->stype = NONE; /* checkpoint all */
2073:       else { /* choose the schedule software for offline checkpointing */
2074:         switch (tjsch->tj_memory_type) {
2075:           case TJ_PETSC:
2076:             tjsch->stype = NONE;
2077:             break;
2078:           case TJ_CAMS:
2079:             tjsch->stype = CAMS_OFFLINE;
2080:             break;
2081:           case TJ_REVOLVE:
2082:             tjsch->stype = (tjsch->max_cps_disk>1) ? REVOLVE_MULTISTAGE : REVOLVE_OFFLINE;
2083:             break;
2084:           default:
2085:             break;
2086:         }
2087:       }
2088:     } else tjsch->stype = NONE; /* checkpoint all for adaptive time step */
2089: #if defined(PETSC_HAVE_REVOLVE)
2090:     if (tjsch->use_online) tjsch->stype = REVOLVE_ONLINE; /* trick into online (for testing purpose only) */
2091: #endif
2093:   }
2094:   if (tjsch->stype >= CAMS_OFFLINE) {
2095: #ifndef PETSC_HAVE_CAMS
2096:     SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"CAMS is needed when there is not enough memory to checkpoint all time steps according to the user's settings, please reconfigure with the additional option --download-cams.");
2097: #else
2098:     CAMSCTX  *actx;
2099:     PetscInt ns = 0;
2100:     if (stack->solution_only) {
2101:       offline_ca_create(tjsch->total_steps,tjsch->max_cps_ram);
2102:     } else {
2103:       TSGetStages(ts,&ns,PETSC_IGNORE);
2104:       offline_cams_create(tjsch->total_steps,tjsch->max_units_ram,ns,ts->stifflyaccurate);
2105:     }
2106:     PetscNew(&actx);
2107:     actx->lastcheckpointstep    = -1; /* -1 can trigger the initialization of CAMS */
2108:     actx->lastcheckpointtype    = -1; /* -1 can trigger the initialization of CAMS */
2109:     actx->endstep               = tjsch->total_steps;
2110:     actx->num_units_avail       = tjsch->max_units_ram;
2111:     actx->num_stages            = ns;
2112:     tjsch->actx                 = actx;
2113: #endif
2114:   } else if (tjsch->stype > TWO_LEVEL_NOREVOLVE) {
2115: #ifndef PETSC_HAVE_REVOLVE
2116:     SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"revolve is needed when there is not enough memory to checkpoint all time steps according to the user's settings, please reconfigure with the additional option --download-revolve.");
2117: #else
2118:     PetscRevolveInt rfine,rsnaps,rsnaps2;

2120:     switch (tjsch->stype) {
2121:       case TWO_LEVEL_REVOLVE:
2122:         PetscRevolveIntCast(tjsch->stride,&rfine);
2123:         PetscRevolveIntCast(tjsch->max_cps_ram,&rsnaps);
2124:         revolve_create_offline(rfine,rsnaps);
2125:         break;
2126:       case TWO_LEVEL_TWO_REVOLVE:
2127:         diskblocks = tjsch->save_stack ? tjsch->max_cps_disk/(tjsch->max_cps_ram+1) : tjsch->max_cps_disk; /* The block size depends on whether the stack is saved. */
2128:         diskstack->stacksize = diskblocks;
2129:         PetscRevolveIntCast(tjsch->stride,&rfine);
2130:         PetscRevolveIntCast(tjsch->max_cps_ram,&rsnaps);
2131:         revolve_create_offline(rfine,rsnaps);
2132:         PetscRevolveIntCast((tjsch->total_steps+tjsch->stride-1)/tjsch->stride,&rfine);
2133:         PetscRevolveIntCast(diskblocks,&rsnaps);
2134:         revolve2_create_offline(rfine,rsnaps);
2135:         PetscNew(&rctx2);
2136:         rctx2->snaps_in       = rsnaps;
2137:         rctx2->reverseonestep = PETSC_FALSE;
2138:         rctx2->check          = 0;
2139:         rctx2->oldcapo        = 0;
2140:         rctx2->capo           = 0;
2141:         rctx2->info           = 2;
2142:         rctx2->fine           = rfine;
2143:         tjsch->rctx2          = rctx2;
2144:         diskstack->top        = -1;
2145:         PetscMalloc1(diskstack->stacksize,&diskstack->container);
2146:         break;
2147:       case REVOLVE_OFFLINE:
2148:         PetscRevolveIntCast(tjsch->total_steps,&rfine);
2149:         PetscRevolveIntCast(tjsch->max_cps_ram,&rsnaps);
2150:         revolve_create_offline(rfine,rsnaps);
2151:         break;
2152:       case REVOLVE_ONLINE:
2153:         stack->stacksize = tjsch->max_cps_ram;
2154:         PetscRevolveIntCast(tjsch->max_cps_ram,&rsnaps);
2155:         revolve_create_online(rsnaps);
2156:         break;
2157:       case REVOLVE_MULTISTAGE:
2158:         PetscRevolveIntCast(tjsch->total_steps,&rfine);
2159:         PetscRevolveIntCast(tjsch->max_cps_ram,&rsnaps);
2160:         PetscRevolveIntCast(tjsch->max_cps_ram+tjsch->max_cps_disk,&rsnaps2);
2161:         revolve_create_multistage(rfine,rsnaps2,rsnaps);
2162:         break;
2163:       default:
2164:         break;
2165:     }
2166:     PetscNew(&rctx);
2167:     PetscRevolveIntCast(tjsch->max_cps_ram,&rsnaps);
2168:     rctx->snaps_in       = rsnaps; /* for theta methods snaps_in=2*max_cps_ram */
2169:     rctx->reverseonestep = PETSC_FALSE;
2170:     rctx->check          = 0;
2171:     rctx->oldcapo        = 0;
2172:     rctx->capo           = 0;
2173:     rctx->info           = 2;
2174:     if (tjsch->stride > 1) {
2175:       PetscRevolveIntCast(tjsch->stride,&rfine);
2176:     } else {
2177:       PetscRevolveIntCast(tjsch->total_steps,&rfine);
2178:     }
2179:     rctx->fine           = rfine;
2180:     tjsch->rctx          = rctx;
2181:     if (tjsch->stype == REVOLVE_ONLINE) rctx->fine = -1;
2182: #endif
2183:   } else {
2184:     if (tjsch->stype == TWO_LEVEL_NOREVOLVE) stack->stacksize = tjsch->stride-1; /* need tjsch->stride-1 at most */
2185:     if (tjsch->stype == NONE) {
2186:       if (fixedtimestep) stack->stacksize = stack->solution_only ? tjsch->total_steps : tjsch->total_steps-1;
2187:       else { /* adaptive time step */
2188:         /* if max_cps_ram is not specified, use maximal allowed number of steps for stack size */
2189:         if (tjsch->max_cps_ram == -1) stack->stacksize = ts->max_steps < PETSC_MAX_INT ? ts->max_steps : 10000;
2190:         tjsch->total_steps = stack->solution_only ? stack->stacksize : stack->stacksize+1; /* will be updated as time integration advances */
2191:       }
2192:     }
2193:   }

2195:   if ((tjsch->stype >= TWO_LEVEL_NOREVOLVE && tjsch->stype < REVOLVE_OFFLINE) || tjsch->stype == REVOLVE_MULTISTAGE) { /* these types need to use disk */
2196:     TSTrajectorySetUp_Basic(tj,ts);
2197:   }

2199:   stack->stacksize = PetscMax(stack->stacksize,1);
2200:   tjsch->recompute = PETSC_FALSE;
2201:   StackInit(stack,stack->stacksize,numY);
2202:   return 0;
2203: }

2205: static PetscErrorCode TSTrajectoryReset_Memory(TSTrajectory tj)
2206: {
2207: #if defined (PETSC_HAVE_REVOLVE) || defined (PETSC_HAVE_CAMS)
2208:   TJScheduler *tjsch = (TJScheduler*)tj->data;
2209: #endif

2211: #if defined(PETSC_HAVE_REVOLVE)
2212:   if (tjsch->stype > TWO_LEVEL_NOREVOLVE) {
2213:     revolve_reset();
2214:     if (tjsch->stype == TWO_LEVEL_TWO_REVOLVE) {
2215:       revolve2_reset();
2216:       PetscFree(tjsch->diskstack.container);
2217:     }
2218:   }
2219:   if (tjsch->stype > TWO_LEVEL_NOREVOLVE) {
2220:     PetscFree(tjsch->rctx);
2221:     PetscFree(tjsch->rctx2);
2222:   }
2223: #endif
2224: #if defined(PETSC_HAVE_CAMS)
2225:   if (tjsch->stype == CAMS_OFFLINE) {
2226:     if (tjsch->stack.solution_only) offline_ca_destroy();
2227:     else offline_ca_destroy();
2228:     PetscFree(tjsch->actx);
2229:   }
2230: #endif
2231:   return 0;
2232: }

2234: static PetscErrorCode TSTrajectoryDestroy_Memory(TSTrajectory tj)
2235: {
2236:   TJScheduler    *tjsch = (TJScheduler*)tj->data;

2238:   StackDestroy(&tjsch->stack);
2239:   PetscViewerDestroy(&tjsch->viewer);
2240:   PetscObjectComposeFunction((PetscObject)tj,"TSTrajectorySetMaxCpsRAM_C",NULL);
2241:   PetscObjectComposeFunction((PetscObject)tj,"TSTrajectorySetMaxCpsDisk_C",NULL);
2242:   PetscObjectComposeFunction((PetscObject)tj,"TSTrajectorySetMaxUnitsRAM_C",NULL);
2243:   PetscObjectComposeFunction((PetscObject)tj,"TSTrajectorySetMaxUnitsDisk_C",NULL);
2244:   PetscObjectComposeFunction((PetscObject)tj,"TSTrajectoryMemorySetType_C",NULL);
2245:   PetscFree(tjsch);
2246:   return 0;
2247: }

2249: /*MC
2250:       TSTRAJECTORYMEMORY - Stores each solution of the ODE/ADE in memory

2252:   Level: intermediate

2254: .seealso:  TSTrajectoryCreate(), TS, TSTrajectorySetType()

2256: M*/
2257: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Memory(TSTrajectory tj,TS ts)
2258: {
2259:   TJScheduler    *tjsch;

2261:   tj->ops->set            = TSTrajectorySet_Memory;
2262:   tj->ops->get            = TSTrajectoryGet_Memory;
2263:   tj->ops->setup          = TSTrajectorySetUp_Memory;
2264:   tj->ops->setfromoptions = TSTrajectorySetFromOptions_Memory;
2265:   tj->ops->reset          = TSTrajectoryReset_Memory;
2266:   tj->ops->destroy        = TSTrajectoryDestroy_Memory;

2268:   PetscNew(&tjsch);
2269:   tjsch->stype        = NONE;
2270:   tjsch->max_cps_ram  = -1; /* -1 indicates that it is not set */
2271:   tjsch->max_cps_disk = -1; /* -1 indicates that it is not set */
2272:   tjsch->stride       = 0; /* if not zero, two-level checkpointing will be used */
2273: #if defined(PETSC_HAVE_REVOLVE)
2274:   tjsch->use_online   = PETSC_FALSE;
2275: #endif
2276:   tjsch->save_stack   = PETSC_TRUE;

2278:   tjsch->stack.solution_only = tj->solution_only;
2279:   PetscViewerCreate(PetscObjectComm((PetscObject)tj),&tjsch->viewer);
2280:   PetscViewerSetType(tjsch->viewer,PETSCVIEWERBINARY);
2281:   PetscViewerPushFormat(tjsch->viewer,PETSC_VIEWER_NATIVE);
2282:   PetscViewerFileSetMode(tjsch->viewer,FILE_MODE_WRITE);

2284:   PetscObjectComposeFunction((PetscObject)tj,"TSTrajectorySetMaxCpsRAM_C",TSTrajectorySetMaxCpsRAM_Memory);
2285:   PetscObjectComposeFunction((PetscObject)tj,"TSTrajectorySetMaxCpsDisk_C",TSTrajectorySetMaxCpsDisk_Memory);
2286:   PetscObjectComposeFunction((PetscObject)tj,"TSTrajectorySetMaxUnitsRAM_C",TSTrajectorySetMaxUnitsRAM_Memory);
2287:   PetscObjectComposeFunction((PetscObject)tj,"TSTrajectorySetMaxUnitsDisk_C",TSTrajectorySetMaxUnitsDisk_Memory);
2288:   PetscObjectComposeFunction((PetscObject)tj,"TSTrajectoryMemorySetType_C",TSTrajectoryMemorySetType_Memory);
2289:   tj->data = tjsch;
2290:   return 0;
2291: }