Date: 2026-05-01T06:12:10+00:00

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:   PetscFunctionBegin;
     14:   #if defined(PETSC_USE_64BIT_INDICES)
     15:   *b = 0;
     16:   PetscCheck(a <= PETSC_REVOLVE_INT_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Parameter is too large for Revolve, which is restricted to 32-bit integers");
     17:   #endif
     18:   *b = (PetscRevolveInt)a;
     19:   PetscFunctionReturn(PETSC_SUCCESS);
     20: }
     21: #endif
     22: #if defined(PETSC_HAVE_CAMS)
     23:   #include <offline_schedule.h>
     24: #endif

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

     29: typedef enum {
     30:   NONE,
     31:   TWO_LEVEL_NOREVOLVE,
     32:   TWO_LEVEL_REVOLVE,
     33:   TWO_LEVEL_TWO_REVOLVE,
     34:   REVOLVE_OFFLINE,
     35:   REVOLVE_ONLINE,
     36:   REVOLVE_MULTISTAGE,
     37:   CAMS_OFFLINE
     38: } SchedulerType;

     40: typedef enum {
     41:   UNSET           = -1,
     42:   SOLUTIONONLY    = 0,
     43:   STAGESONLY      = 1,
     44:   SOLUTION_STAGES = 2
     45: } CheckpointType;

     47: const char *const TSTrajectoryMemoryTypes[] = {"REVOLVE", "CAMS", "PETSC", "TSTrajectoryMemoryType", "TJ_", NULL};

     49: #define HaveSolution(m) ((m) == SOLUTIONONLY || (m) == SOLUTION_STAGES)
     50: #define HaveStages(m)   ((m) == STAGESONLY || (m) == SOLUTION_STAGES)

     52: typedef struct _StackElement {
     53:   PetscInt       stepnum;
     54:   Vec            X;
     55:   Vec           *Y;
     56:   PetscReal      time;
     57:   PetscReal      timeprev; /* for no solution_only mode */
     58:   PetscReal      timenext; /* for solution_only mode */
     59:   CheckpointType cptype;
     60: } *StackElement;

     62: #if defined(PETSC_HAVE_REVOLVE)
     63: typedef struct _RevolveCTX {
     64:   PetscBool       reverseonestep;
     65:   PetscRevolveInt where;
     66:   PetscRevolveInt snaps_in;
     67:   PetscRevolveInt stepsleft;
     68:   PetscRevolveInt check;
     69:   PetscRevolveInt oldcapo;
     70:   PetscRevolveInt capo;
     71:   PetscRevolveInt fine;
     72:   PetscRevolveInt info;
     73: } RevolveCTX;
     74: #endif

     76: #if defined(PETSC_HAVE_CAMS)
     77: typedef struct _CAMSCTX {
     78:   PetscInt lastcheckpointstep;
     79:   PetscInt lastcheckpointtype;
     80:   PetscInt num_units_avail;
     81:   PetscInt endstep;
     82:   PetscInt num_stages;
     83:   PetscInt nextcheckpointstep;
     84:   PetscInt nextcheckpointtype; /* (0) solution only (1) stages (2) solution+stages */
     85:   PetscInt info;
     86: } CAMSCTX;
     87: #endif

     89: typedef struct _Stack {
     90:   PetscInt      stacksize;
     91:   PetscInt      top;
     92:   StackElement *container;
     93:   PetscInt      nallocated;
     94:   PetscInt      numY;
     95:   PetscBool     solution_only;
     96:   PetscBool     use_dram;
     97: } Stack;

     99: typedef struct _DiskStack {
    100:   PetscInt  stacksize;
    101:   PetscInt  top;
    102:   PetscInt *container;
    103: } DiskStack;

    105: typedef struct _TJScheduler {
    106:   SchedulerType          stype;
    107:   TSTrajectoryMemoryType tj_memory_type;
    108: #if defined(PETSC_HAVE_REVOLVE)
    109:   RevolveCTX *rctx, *rctx2;
    110:   PetscBool   use_online;
    111:   PetscInt    store_stride;
    112: #endif
    113: #if defined(PETSC_HAVE_CAMS)
    114:   CAMSCTX *actx;
    115: #endif
    116:   PetscBool   recompute;
    117:   PetscBool   skip_trajectory;
    118:   PetscBool   save_stack;
    119:   PetscInt    max_units_ram;  /* maximum checkpointing units in RAM */
    120:   PetscInt    max_units_disk; /* maximum checkpointing units on disk */
    121:   PetscInt    max_cps_ram;    /* maximum checkpoints in RAM */
    122:   PetscInt    max_cps_disk;   /* maximum checkpoints on disk */
    123:   PetscInt    stride;
    124:   PetscInt    total_steps; /* total number of steps */
    125:   Stack       stack;
    126:   DiskStack   diskstack;
    127:   PetscViewer viewer;
    128: } TJScheduler;

    130: static PetscErrorCode TurnForwardWithStepsize(TS ts, PetscReal nextstepsize)
    131: {
    132:   PetscFunctionBegin;
    133:   /* reverse the direction */
    134:   PetscCall(TSSetTimeStep(ts, nextstepsize));
    135:   PetscFunctionReturn(PETSC_SUCCESS);
    136: }

    138: static PetscErrorCode TurnForward(TS ts)
    139: {
    140:   PetscReal stepsize;

    142:   PetscFunctionBegin;
    143:   /* reverse the direction */
    144:   PetscCall(TSGetTimeStep(ts, &stepsize));
    145:   PetscCall(TSSetTimeStep(ts, -stepsize));
    146:   PetscFunctionReturn(PETSC_SUCCESS);
    147: }

    149: static PetscErrorCode TurnBackward(TS ts)
    150: {
    151:   PetscReal stepsize;

    153:   PetscFunctionBegin;
    154:   if (!ts->trajectory->adjoint_solve_mode) PetscFunctionReturn(PETSC_SUCCESS);
    155:   /* reverse the direction */
    156:   stepsize = ts->ptime_prev - ts->ptime;
    157:   PetscCall(TSSetTimeStep(ts, stepsize));
    158:   PetscFunctionReturn(PETSC_SUCCESS);
    159: }

    161: static PetscErrorCode ElementCreate(TS ts, CheckpointType cptype, Stack *stack, StackElement *e)
    162: {
    163:   Vec  X;
    164:   Vec *Y;

    166:   PetscFunctionBegin;
    167:   if (stack->top < stack->stacksize - 1 && stack->container[stack->top + 1]) {
    168:     *e = stack->container[stack->top + 1];
    169:     if (HaveSolution(cptype) && !(*e)->X) {
    170:       PetscCall(TSGetSolution(ts, &X));
    171:       PetscCall(VecDuplicate(X, &(*e)->X));
    172:     }
    173:     if (cptype == 1 && (*e)->X) PetscCall(VecDestroy(&(*e)->X));
    174:     if (HaveStages(cptype) && !(*e)->Y) {
    175:       PetscCall(TSGetStages(ts, &stack->numY, &Y));
    176:       if (stack->numY) PetscCall(VecDuplicateVecs(Y[0], stack->numY, &(*e)->Y));
    177:     }
    178:     if (cptype == 0 && (*e)->Y) PetscCall(VecDestroyVecs(stack->numY, &(*e)->Y));
    179:     (*e)->cptype = cptype;
    180:     PetscFunctionReturn(PETSC_SUCCESS);
    181:   }
    182:   if (stack->use_dram) PetscCall(PetscMallocSetDRAM());
    183:   PetscCall(PetscNew(e));
    184:   if (HaveSolution(cptype)) {
    185:     PetscCall(TSGetSolution(ts, &X));
    186:     PetscCall(VecDuplicate(X, &(*e)->X));
    187:   }
    188:   if (HaveStages(cptype)) {
    189:     PetscCall(TSGetStages(ts, &stack->numY, &Y));
    190:     if (stack->numY) PetscCall(VecDuplicateVecs(Y[0], stack->numY, &(*e)->Y));
    191:   }
    192:   if (stack->use_dram) PetscCall(PetscMallocResetDRAM());
    193:   stack->nallocated++;
    194:   (*e)->cptype = cptype;
    195:   PetscFunctionReturn(PETSC_SUCCESS);
    196: }

    198: static PetscErrorCode ElementSet(TS ts, Stack *stack, StackElement *e, PetscInt stepnum, PetscReal time, Vec X)
    199: {
    200:   Vec      *Y;
    201:   PetscInt  i;
    202:   PetscReal timeprev;

    204:   PetscFunctionBegin;
    205:   if (HaveSolution((*e)->cptype)) PetscCall(VecCopy(X, (*e)->X));
    206:   if (HaveStages((*e)->cptype)) {
    207:     PetscCall(TSGetStages(ts, &stack->numY, &Y));
    208:     for (i = 0; i < stack->numY; i++) PetscCall(VecCopy(Y[i], (*e)->Y[i]));
    209:   }
    210:   (*e)->stepnum = stepnum;
    211:   (*e)->time    = time;
    212:   /* for consistency */
    213:   if (stepnum == 0) {
    214:     (*e)->timeprev = (*e)->time - ts->time_step;
    215:   } else {
    216:     PetscCall(TSGetPrevTime(ts, &timeprev));
    217:     (*e)->timeprev = timeprev;
    218:   }
    219:   PetscFunctionReturn(PETSC_SUCCESS);
    220: }

    222: static PetscErrorCode ElementDestroy(Stack *stack, StackElement e)
    223: {
    224:   PetscFunctionBegin;
    225:   if (stack->use_dram) PetscCall(PetscMallocSetDRAM());
    226:   PetscCall(VecDestroy(&e->X));
    227:   if (e->Y) PetscCall(VecDestroyVecs(stack->numY, &e->Y));
    228:   PetscCall(PetscFree(e));
    229:   if (stack->use_dram) PetscCall(PetscMallocResetDRAM());
    230:   stack->nallocated--;
    231:   PetscFunctionReturn(PETSC_SUCCESS);
    232: }

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

    239:   PetscFunctionBegin;
    240:   PetscCall(PetscCalloc1(newsize * sizeof(StackElement), &newcontainer));
    241:   for (i = 0; i < stack->stacksize; i++) newcontainer[i] = stack->container[i];
    242:   PetscCall(PetscFree(stack->container));
    243:   stack->container = newcontainer;
    244:   stack->stacksize = newsize;
    245:   PetscFunctionReturn(PETSC_SUCCESS);
    246: }

    248: static PetscErrorCode StackPush(Stack *stack, StackElement e)
    249: {
    250:   PetscFunctionBegin;
    251:   PetscCheck(stack->top + 1 < stack->stacksize, PETSC_COMM_SELF, PETSC_ERR_MEMC, "Maximum stack size (%" PetscInt_FMT ") exceeded", stack->stacksize);
    252:   stack->container[++stack->top] = e;
    253:   PetscFunctionReturn(PETSC_SUCCESS);
    254: }

    256: static PetscErrorCode StackPop(Stack *stack, StackElement *e)
    257: {
    258:   PetscFunctionBegin;
    259:   *e = NULL;
    260:   PetscCheck(stack->top != -1, PETSC_COMM_SELF, PETSC_ERR_MEMC, "Empty stack");
    261:   *e = stack->container[stack->top--];
    262:   PetscFunctionReturn(PETSC_SUCCESS);
    263: }

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

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

    278:   if (!stack->container) PetscCall(PetscCalloc1(size, &stack->container));
    279:   PetscFunctionReturn(PETSC_SUCCESS);
    280: }

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

    286:   PetscFunctionBegin;
    287:   if (!stack->container) PetscFunctionReturn(PETSC_SUCCESS);
    288:   PetscCheck(stack->top + 1 <= n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Stack size does not match element counter %" PetscInt_FMT, n);
    289:   for (PetscInt i = 0; i < n; i++) PetscCall(ElementDestroy(stack, stack->container[i]));
    290:   PetscCall(PetscFree(stack->container));
    291:   PetscFunctionReturn(PETSC_SUCCESS);
    292: }

    294: static PetscErrorCode StackFind(Stack *stack, StackElement *e, PetscInt index)
    295: {
    296:   PetscFunctionBegin;
    297:   *e = NULL;
    298:   PetscCheck(index >= 0 && index <= stack->top, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid index %" PetscInt_FMT, index);
    299:   *e = stack->container[index];
    300:   PetscFunctionReturn(PETSC_SUCCESS);
    301: }

    303: static PetscErrorCode WriteToDisk(PetscBool stifflyaccurate, PetscInt stepnum, PetscReal time, PetscReal timeprev, Vec X, Vec *Y, PetscInt numY, CheckpointType cptype, PetscViewer viewer)
    304: {
    305:   PetscFunctionBegin;
    306:   PetscCall(PetscViewerBinaryWrite(viewer, &stepnum, 1, PETSC_INT));
    307:   if (HaveSolution(cptype)) PetscCall(VecView(X, viewer));
    308:   if (HaveStages(cptype)) {
    309:     for (PetscInt i = 0; i < numY; i++) {
    310:       /* 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. */
    311:       if (stifflyaccurate && i == numY - 1 && HaveSolution(cptype)) continue;
    312:       PetscCall(VecView(Y[i], viewer));
    313:     }
    314:   }
    315:   PetscCall(PetscViewerBinaryWrite(viewer, &time, 1, PETSC_REAL));
    316:   PetscCall(PetscViewerBinaryWrite(viewer, &timeprev, 1, PETSC_REAL));
    317:   PetscFunctionReturn(PETSC_SUCCESS);
    318: }

    320: static PetscErrorCode ReadFromDisk(PetscBool stifflyaccurate, PetscInt *stepnum, PetscReal *time, PetscReal *timeprev, Vec X, Vec *Y, PetscInt numY, CheckpointType cptype, PetscViewer viewer)
    321: {
    322:   PetscFunctionBegin;
    323:   PetscCall(PetscViewerBinaryRead(viewer, stepnum, 1, NULL, PETSC_INT));
    324:   if (HaveSolution(cptype)) PetscCall(VecLoad(X, viewer));
    325:   if (HaveStages(cptype)) {
    326:     for (PetscInt i = 0; i < numY; i++) {
    327:       /* 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. */
    328:       if (stifflyaccurate && i == numY - 1 && HaveSolution(cptype)) continue;
    329:       PetscCall(VecLoad(Y[i], viewer));
    330:     }
    331:   }
    332:   PetscCall(PetscViewerBinaryRead(viewer, time, 1, NULL, PETSC_REAL));
    333:   PetscCall(PetscViewerBinaryRead(viewer, timeprev, 1, NULL, PETSC_REAL));
    334:   PetscFunctionReturn(PETSC_SUCCESS);
    335: }

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

    346:   PetscFunctionBegin;
    347:   PetscCall(PetscObjectGetComm((PetscObject)ts, &comm));
    348:   if (tj->monitor) {
    349:     PetscCall(PetscViewerASCIIPushTab(tj->monitor));
    350:     PetscCall(PetscViewerASCIIPrintf(tj->monitor, "Dump stack id %" PetscInt_FMT " to file\n", id));
    351:     PetscCall(PetscViewerASCIIPopTab(tj->monitor));
    352:   }
    353:   PetscCall(PetscSNPrintf(filename, sizeof(filename), "%s/TS-STACK%06" PetscInt_FMT ".bin", tj->dirname, id));
    354:   PetscCall(PetscViewerFileSetName(tjsch->viewer, filename));
    355:   PetscCall(PetscViewerSetUp(tjsch->viewer));
    356:   ndumped = stack->top + 1;
    357:   PetscCall(PetscViewerBinaryWrite(tjsch->viewer, &ndumped, 1, PETSC_INT));
    358:   for (PetscInt i = 0; i < ndumped; i++) {
    359:     e          = stack->container[i];
    360:     cptype_int = (PetscInt)e->cptype;
    361:     PetscCall(PetscViewerBinaryWrite(tjsch->viewer, &cptype_int, 1, PETSC_INT));
    362:     PetscCall(PetscLogEventBegin(TSTrajectory_DiskWrite, tj, ts, 0, 0));
    363:     PetscCall(WriteToDisk(ts->stifflyaccurate, e->stepnum, e->time, e->timeprev, e->X, e->Y, stack->numY, e->cptype, tjsch->viewer));
    364:     PetscCall(PetscLogEventEnd(TSTrajectory_DiskWrite, tj, ts, 0, 0));
    365:     ts->trajectory->diskwrites++;
    366:     PetscCall(StackPop(stack, &e));
    367:   }
    368:   /* 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 */
    369:   PetscCall(TSGetStages(ts, &stack->numY, &Y));
    370:   PetscCall(PetscLogEventBegin(TSTrajectory_DiskWrite, tj, ts, 0, 0));
    371:   PetscCall(WriteToDisk(ts->stifflyaccurate, ts->steps, ts->ptime, ts->ptime_prev, ts->vec_sol, Y, stack->numY, SOLUTION_STAGES, tjsch->viewer));
    372:   PetscCall(PetscLogEventEnd(TSTrajectory_DiskWrite, tj, ts, 0, 0));
    373:   ts->trajectory->diskwrites++;
    374:   PetscFunctionReturn(PETSC_SUCCESS);
    375: }

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

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

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

    429:   PetscFunctionBegin;
    430:   if (tj->monitor) {
    431:     PetscCall(PetscViewerASCIIAddTab(tj->monitor, ((PetscObject)tj)->tablevel));
    432:     PetscCall(PetscViewerASCIIPrintf(tj->monitor, "Load last stack element from file\n"));
    433:     PetscCall(PetscViewerASCIISubtractTab(tj->monitor, ((PetscObject)tj)->tablevel));
    434:   }
    435:   PetscCall(TSGetStages(ts, &stack->numY, &Y));
    436:   PetscCall(VecGetSize(Y[0], &size));
    437:   /* VecView writes to file two extra int's for class id and number of rows */
    438:   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;

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

    467: static PetscErrorCode DumpSingle(TSTrajectory tj, TS ts, Stack *stack, PetscInt id)
    468: {
    469:   Vec         *Y;
    470:   PetscInt     stepnum;
    471:   TJScheduler *tjsch = (TJScheduler *)tj->data;
    472:   char         filename[PETSC_MAX_PATH_LEN];
    473:   MPI_Comm     comm;

    475:   PetscFunctionBegin;
    476:   PetscCall(PetscObjectGetComm((PetscObject)ts, &comm));
    477:   if (tj->monitor) {
    478:     PetscCall(PetscViewerASCIIAddTab(tj->monitor, ((PetscObject)tj)->tablevel));
    479:     PetscCall(PetscViewerASCIIPrintf(tj->monitor, "Dump a single point from file\n"));
    480:     PetscCall(PetscViewerASCIISubtractTab(tj->monitor, ((PetscObject)tj)->tablevel));
    481:   }
    482:   PetscCall(TSGetStepNumber(ts, &stepnum));
    483:   PetscCall(PetscSNPrintf(filename, sizeof(filename), "%s/TS-CPS%06" PetscInt_FMT ".bin", tj->dirname, id));
    484:   PetscCall(PetscViewerFileSetName(tjsch->viewer, filename));
    485:   PetscCall(PetscViewerSetUp(tjsch->viewer));

    487:   PetscCall(TSGetStages(ts, &stack->numY, &Y));
    488:   PetscCall(PetscLogEventBegin(TSTrajectory_DiskWrite, tj, ts, 0, 0));
    489:   PetscCall(WriteToDisk(ts->stifflyaccurate, stepnum, ts->ptime, ts->ptime_prev, ts->vec_sol, Y, stack->numY, SOLUTION_STAGES, tjsch->viewer));
    490:   PetscCall(PetscLogEventEnd(TSTrajectory_DiskWrite, tj, ts, 0, 0));
    491:   ts->trajectory->diskwrites++;
    492:   PetscFunctionReturn(PETSC_SUCCESS);
    493: }

    495: static PetscErrorCode LoadSingle(TSTrajectory tj, TS ts, Stack *stack, PetscInt id)
    496: {
    497:   Vec        *Y;
    498:   PetscViewer viewer;
    499:   char        filename[PETSC_MAX_PATH_LEN];

    501:   PetscFunctionBegin;
    502:   if (tj->monitor) {
    503:     PetscCall(PetscViewerASCIIAddTab(tj->monitor, ((PetscObject)tj)->tablevel));
    504:     PetscCall(PetscViewerASCIIPrintf(tj->monitor, "Load a single point from file\n"));
    505:     PetscCall(PetscViewerASCIISubtractTab(tj->monitor, ((PetscObject)tj)->tablevel));
    506:   }
    507:   PetscCall(PetscSNPrintf(filename, sizeof filename, "%s/TS-CPS%06" PetscInt_FMT ".bin", tj->dirname, id));
    508:   PetscCall(PetscViewerBinaryOpen(PetscObjectComm((PetscObject)tj), filename, FILE_MODE_READ, &viewer));
    509:   PetscCall(PetscViewerBinarySetSkipInfo(viewer, PETSC_TRUE));
    510:   PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_NATIVE));
    511:   PetscCall(TSGetStages(ts, &stack->numY, &Y));
    512:   PetscCall(PetscLogEventBegin(TSTrajectory_DiskRead, tj, ts, 0, 0));
    513:   PetscCall(ReadFromDisk(ts->stifflyaccurate, &ts->steps, &ts->ptime, &ts->ptime_prev, ts->vec_sol, Y, stack->numY, SOLUTION_STAGES, viewer));
    514:   PetscCall(PetscLogEventEnd(TSTrajectory_DiskRead, tj, ts, 0, 0));
    515:   ts->trajectory->diskreads++;
    516:   PetscCall(PetscViewerDestroy(&viewer));
    517:   PetscFunctionReturn(PETSC_SUCCESS);
    518: }

    520: static PetscErrorCode UpdateTS(TS ts, Stack *stack, StackElement e, PetscInt stepnum, PetscBool adjoint_mode)
    521: {
    522:   Vec     *Y;
    523:   PetscInt i;

    525:   PetscFunctionBegin;
    526:   /* In adjoint mode we do not need to copy solution if the stepnum is the same */
    527:   if (!adjoint_mode || (HaveSolution(e->cptype) && e->stepnum != stepnum)) PetscCall(VecCopy(e->X, ts->vec_sol));
    528:   if (HaveStages(e->cptype)) {
    529:     PetscCall(TSGetStages(ts, &stack->numY, &Y));
    530:     if (e->stepnum && e->stepnum == stepnum) {
    531:       for (i = 0; i < stack->numY; i++) PetscCall(VecCopy(e->Y[i], Y[i]));
    532:     } else if (ts->stifflyaccurate) {
    533:       PetscCall(VecCopy(e->Y[stack->numY - 1], ts->vec_sol));
    534:     }
    535:   }
    536:   if (adjoint_mode) {
    537:     PetscCall(TSSetTimeStep(ts, e->timeprev - e->time)); /* stepsize will be negative */
    538:   } else {
    539:     PetscCall(TSSetTimeStep(ts, e->time - e->timeprev)); /* stepsize will be positive */
    540:   }
    541:   ts->ptime       = e->time;
    542:   ts->ptime_prev  = e->timeprev;
    543:   ts->steprestart = PETSC_TRUE; /* prevent methods with FSAL using information from the previous time step which is not available when current checkpoint is restored */
    544:   PetscFunctionReturn(PETSC_SUCCESS);
    545: }

    547: static PetscErrorCode ReCompute(TS ts, TJScheduler *tjsch, PetscInt stepnumbegin, PetscInt stepnumend)
    548: {
    549:   Stack   *stack = &tjsch->stack;
    550:   PetscInt i;

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

    576: static PetscErrorCode TopLevelStore(TSTrajectory tj, TS ts, TJScheduler *tjsch, PetscInt stepnum, PetscInt localstepnum, PetscInt laststridesize, PetscBool *done)
    577: {
    578:   Stack     *stack     = &tjsch->stack;
    579:   DiskStack *diskstack = &tjsch->diskstack;
    580:   PetscInt   stridenum;

    582:   PetscFunctionBegin;
    583:   *done     = PETSC_FALSE;
    584:   stridenum = stepnum / tjsch->stride;
    585:   /* make sure saved checkpoint id starts from 1
    586:      skip last stride when using stridenum+1
    587:      skip first stride when using stridenum */
    588:   if (stack->solution_only) {
    589:     if (tjsch->save_stack) {
    590:       if (localstepnum == tjsch->stride - 1 && stepnum < tjsch->total_steps - laststridesize) { /* current step will be saved without going through stack */
    591:         PetscCall(StackDumpAll(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:     } else {
    596:       if (localstepnum == 0 && stepnum < tjsch->total_steps - laststridesize) {
    597:         PetscCall(DumpSingle(tj, ts, stack, stridenum + 1));
    598:         if (tjsch->stype == TWO_LEVEL_TWO_REVOLVE) diskstack->container[++diskstack->top] = stridenum + 1;
    599:         *done = PETSC_TRUE;
    600:       }
    601:     }
    602:   } else {
    603:     if (tjsch->save_stack) {
    604:       if (localstepnum == 0 && stepnum < tjsch->total_steps && stepnum != 0) { /* skip the first stride */
    605:         PetscCall(StackDumpAll(tj, ts, stack, stridenum));
    606:         if (tjsch->stype == TWO_LEVEL_TWO_REVOLVE) diskstack->container[++diskstack->top] = stridenum;
    607:         *done = PETSC_TRUE;
    608:       }
    609:     } else {
    610:       if (localstepnum == 1 && stepnum < tjsch->total_steps - laststridesize) {
    611:         PetscCall(DumpSingle(tj, ts, stack, stridenum + 1));
    612:         if (tjsch->stype == TWO_LEVEL_TWO_REVOLVE) diskstack->container[++diskstack->top] = stridenum + 1;
    613:         *done = PETSC_TRUE;
    614:       }
    615:     }
    616:   }
    617:   PetscFunctionReturn(PETSC_SUCCESS);
    618: }

    620: static PetscErrorCode TSTrajectoryMemorySet_N(TS ts, TJScheduler *tjsch, PetscInt stepnum, PetscReal time, Vec X)
    621: {
    622:   Stack         *stack = &tjsch->stack;
    623:   StackElement   e;
    624:   CheckpointType cptype;

    626:   PetscFunctionBegin;
    627:   /* skip the last step */
    628:   if (ts->reason) { /* only affect the forward run */
    629:     /* update total_steps in the end of forward run */
    630:     if (stepnum != tjsch->total_steps) tjsch->total_steps = stepnum;
    631:     if (stack->solution_only) {
    632:       /* get rid of the solution at second last step */
    633:       PetscCall(StackPop(stack, &e));
    634:     }
    635:     PetscFunctionReturn(PETSC_SUCCESS);
    636:   }
    637:   /*  do not save trajectory at the recompute stage for solution_only mode */
    638:   if (stack->solution_only && tjsch->recompute) PetscFunctionReturn(PETSC_SUCCESS);
    639:   /* skip the first step for no_solution_only mode */
    640:   if (!stack->solution_only && stepnum == 0) PetscFunctionReturn(PETSC_SUCCESS);

    642:   /* resize the stack */
    643:   if (stack->top + 1 == stack->stacksize) PetscCall(StackResize(stack, 2 * stack->stacksize));
    644:   /* update timenext for the previous step; necessary for step adaptivity */
    645:   if (stack->top > -1) {
    646:     PetscCall(StackTop(stack, &e));
    647:     e->timenext = ts->ptime;
    648:   }
    649:   PetscCheck(stepnum >= stack->top, PetscObjectComm((PetscObject)ts), PETSC_ERR_MEMC, "Illegal modification of a non-top stack element");
    650:   cptype = stack->solution_only ? SOLUTIONONLY : STAGESONLY;
    651:   PetscCall(ElementCreate(ts, cptype, stack, &e));
    652:   PetscCall(ElementSet(ts, stack, &e, stepnum, time, X));
    653:   PetscCall(StackPush(stack, e));
    654:   PetscFunctionReturn(PETSC_SUCCESS);
    655: }

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

    663:   PetscFunctionBegin;
    664:   if (stack->top + 1 == stack->stacksize) PetscCall(StackResize(stack, 2 * stack->stacksize));
    665:   /* update timenext for the previous step; necessary for step adaptivity */
    666:   if (stack->top > -1) {
    667:     PetscCall(StackTop(stack, &e));
    668:     e->timenext = ts->ptime;
    669:   }
    670:   PetscCheck(stepnum >= stack->top, PetscObjectComm((PetscObject)ts), PETSC_ERR_MEMC, "Illegal modification of a non-top stack element");
    671:   cptype = stack->solution_only ? SOLUTIONONLY : SOLUTION_STAGES; /* Always include solution in a checkpoint in non-adjoint mode */
    672:   PetscCall(ElementCreate(ts, cptype, stack, &e));
    673:   PetscCall(ElementSet(ts, stack, &e, stepnum, time, X));
    674:   PetscCall(StackPush(stack, e));
    675:   PetscFunctionReturn(PETSC_SUCCESS);
    676: }

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

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

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

    708:   PetscFunctionBegin;
    709:   PetscCall(StackFind(stack, &e, stepnum));
    710:   PetscCheck(stepnum == e->stepnum, PetscObjectComm((PetscObject)ts), PETSC_ERR_PLIB, "Inconsistent steps! %" PetscInt_FMT " != %" PetscInt_FMT, stepnum, e->stepnum);
    711:   PetscCall(UpdateTS(ts, stack, e, stepnum, PETSC_FALSE));
    712:   PetscFunctionReturn(PETSC_SUCCESS);
    713: }

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

    723:   PetscFunctionBegin;
    724:   if (!stack->solution_only && stepnum == 0) PetscFunctionReturn(PETSC_SUCCESS);
    725:   if (stack->solution_only && stepnum == tjsch->total_steps) PetscFunctionReturn(PETSC_SUCCESS);
    726:   if (tjsch->save_stack && tjsch->recompute) PetscFunctionReturn(PETSC_SUCCESS);

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

    733:   if (!tjsch->recompute) {
    734:     PetscCall(TopLevelStore(tj, ts, tjsch, stepnum, localstepnum, laststridesize, &done));
    735:     if (!tjsch->save_stack && stepnum < tjsch->total_steps - laststridesize) PetscFunctionReturn(PETSC_SUCCESS);
    736:   }
    737:   if (!stack->solution_only && localstepnum == 0) PetscFunctionReturn(PETSC_SUCCESS);                /* skip last point in each stride at recompute stage or last stride */
    738:   if (stack->solution_only && localstepnum == tjsch->stride - 1) PetscFunctionReturn(PETSC_SUCCESS); /* skip last step in each stride at recompute stage or last stride */

    740:   cptype = stack->solution_only ? SOLUTIONONLY : STAGESONLY;
    741:   PetscCall(ElementCreate(ts, cptype, stack, &e));
    742:   PetscCall(ElementSet(ts, stack, &e, stepnum, time, X));
    743:   PetscCall(StackPush(stack, e));
    744:   PetscFunctionReturn(PETSC_SUCCESS);
    745: }

    747: static PetscErrorCode TSTrajectoryMemoryGet_TLNR(TSTrajectory tj, TS ts, TJScheduler *tjsch, PetscInt stepnum)
    748: {
    749:   Stack       *stack = &tjsch->stack;
    750:   PetscInt     id, localstepnum, laststridesize;
    751:   StackElement e;

    753:   PetscFunctionBegin;
    754:   if (stepnum == tjsch->total_steps) {
    755:     PetscCall(TurnBackward(ts));
    756:     PetscFunctionReturn(PETSC_SUCCESS);
    757:   }

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

    809: #if defined(PETSC_HAVE_REVOLVE)
    810: static PetscErrorCode printwhattodo(PetscViewer viewer, PetscRevolveInt whattodo, RevolveCTX *rctx, PetscRevolveInt shift)
    811: {
    812:   PetscFunctionBegin;
    813:   if (!viewer) PetscFunctionReturn(PETSC_SUCCESS);

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

    844: static PetscErrorCode printwhattodo2(PetscViewer viewer, PetscRevolveInt whattodo, RevolveCTX *rctx, PetscRevolveInt shift)
    845: {
    846:   PetscFunctionBegin;
    847:   if (!viewer) PetscFunctionReturn(PETSC_SUCCESS);

    849:   switch (whattodo) {
    850:   case 1:
    851:     PetscCall(PetscViewerASCIIPrintf(viewer, "[Top Level] Advance from stride %d to stride %d\n", rctx->oldcapo + shift, rctx->capo + shift));
    852:     break;
    853:   case 2:
    854:     PetscCall(PetscViewerASCIIPrintf(viewer, "[Top Level] Store in checkpoint number %d\n", rctx->check));
    855:     break;
    856:   case 3:
    857:     PetscCall(PetscViewerASCIIPrintf(viewer, "[Top Level] First turn: Initialize adjoints and reverse first stride\n"));
    858:     break;
    859:   case 4:
    860:     PetscCall(PetscViewerASCIIPrintf(viewer, "[Top Level] Forward and reverse one stride\n"));
    861:     break;
    862:   case 5:
    863:     PetscCall(PetscViewerASCIIPrintf(viewer, "[Top Level] Restore in checkpoint number %d\n", rctx->check));
    864:     break;
    865:   case 7:
    866:     PetscCall(PetscViewerASCIIPrintf(viewer, "[Top Level] Store in top-level checkpoint number %d\n", rctx->check));
    867:     break;
    868:   case 8:
    869:     PetscCall(PetscViewerASCIIPrintf(viewer, "[Top Level] Restore in top-level checkpoint number %d\n", rctx->check));
    870:     break;
    871:   case -1:
    872:     PetscCall(PetscViewerASCIIPrintf(viewer, "[Top Level] Error!"));
    873:     break;
    874:   }
    875:   PetscFunctionReturn(PETSC_SUCCESS);
    876: }

    878: static PetscErrorCode InitRevolve(PetscInt fine, PetscInt snaps, RevolveCTX *rctx)
    879: {
    880:   PetscRevolveInt rsnaps, rfine;

    882:   PetscFunctionBegin;
    883:   PetscCall(PetscRevolveIntCast(snaps, &rsnaps));
    884:   PetscCall(PetscRevolveIntCast(fine, &rfine));
    885:   revolve_reset();
    886:   revolve_create_offline(rfine, rsnaps);
    887:   rctx->snaps_in       = rsnaps;
    888:   rctx->fine           = rfine;
    889:   rctx->check          = 0;
    890:   rctx->capo           = 0;
    891:   rctx->reverseonestep = PETSC_FALSE;
    892:   /* check stepsleft? */
    893:   PetscFunctionReturn(PETSC_SUCCESS);
    894: }

    896: static PetscErrorCode FastForwardRevolve(RevolveCTX *rctx)
    897: {
    898:   PetscRevolveInt whattodo;

    900:   PetscFunctionBegin;
    901:   whattodo = 0;
    902:   while (whattodo != 3) { /* we have to fast forward revolve to the beginning of the backward sweep due to unfriendly revolve interface */
    903:     whattodo = revolve_action(&rctx->check, &rctx->capo, &rctx->fine, rctx->snaps_in, &rctx->info, &rctx->where);
    904:   }
    905:   PetscFunctionReturn(PETSC_SUCCESS);
    906: }

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

    912:   PetscFunctionBegin;
    913:   *store = 0;
    914:   if (rctx->stepsleft > 0) { /* advance the solution without checkpointing anything as Revolve requires */
    915:     rctx->stepsleft--;
    916:     PetscFunctionReturn(PETSC_SUCCESS);
    917:   }
    918:   /* let Revolve determine what to do next */
    919:   shift         = stepnum - localstepnum;
    920:   rctx->oldcapo = rctx->capo;
    921:   rctx->capo    = localstepnum;

    923:   if (!toplevel) whattodo = revolve_action(&rctx->check, &rctx->capo, &rctx->fine, rctx->snaps_in, &rctx->info, &rctx->where);
    924:   else whattodo = revolve2_action(&rctx->check, &rctx->capo, &rctx->fine, rctx->snaps_in, &rctx->info, &rctx->where);
    925:   if (stype == REVOLVE_ONLINE && whattodo == 8) whattodo = 5;
    926:   if (stype == REVOLVE_ONLINE && whattodo == 7) whattodo = 2;
    927:   if (!toplevel) PetscCall(printwhattodo(viewer, whattodo, rctx, shift));
    928:   else PetscCall(printwhattodo2(viewer, whattodo, rctx, shift));
    929:   PetscCheck(whattodo != -1, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in the Revolve library");
    930:   if (whattodo == 1) { /* advance some time steps */
    931:     if (stype == REVOLVE_ONLINE && rctx->capo >= total_steps - 1) {
    932:       revolve_turn(total_steps, &rctx->capo, &rctx->fine);
    933:       if (!toplevel) PetscCall(printwhattodo(viewer, whattodo, rctx, shift));
    934:       else PetscCall(printwhattodo2(viewer, whattodo, rctx, shift));
    935:     }
    936:     rctx->stepsleft = rctx->capo - rctx->oldcapo - 1;
    937:   }
    938:   if (whattodo == 3 || whattodo == 4) { /* ready for a reverse step */
    939:     rctx->reverseonestep = PETSC_TRUE;
    940:   }
    941:   if (whattodo == 5) { /* restore a checkpoint and ask Revolve what to do next */
    942:     rctx->oldcapo = rctx->capo;
    943:     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*/
    944:     else whattodo = revolve2_action(&rctx->check, &rctx->capo, &rctx->fine, rctx->snaps_in, &rctx->info, &rctx->where);
    945:     if (!toplevel) PetscCall(printwhattodo(viewer, whattodo, rctx, shift));
    946:     else PetscCall(printwhattodo2(viewer, whattodo, rctx, shift));
    947:     if (whattodo == 3 || whattodo == 4) rctx->reverseonestep = PETSC_TRUE;
    948:     if (whattodo == 1) rctx->stepsleft = rctx->capo - rctx->oldcapo;
    949:   }
    950:   if (whattodo == 7) { /* save the checkpoint to disk */
    951:     *store        = 2;
    952:     rctx->oldcapo = rctx->capo;
    953:     whattodo      = revolve_action(&rctx->check, &rctx->capo, &rctx->fine, rctx->snaps_in, &rctx->info, &rctx->where); /* must return 1 */
    954:     PetscCall(printwhattodo(viewer, whattodo, rctx, shift));
    955:     rctx->stepsleft = rctx->capo - rctx->oldcapo - 1;
    956:   }
    957:   if (whattodo == 2) { /* store a checkpoint to RAM and ask Revolve how many time steps to advance next */
    958:     *store        = 1;
    959:     rctx->oldcapo = rctx->capo;
    960:     if (!toplevel) whattodo = revolve_action(&rctx->check, &rctx->capo, &rctx->fine, rctx->snaps_in, &rctx->info, &rctx->where); /* must return 1 */
    961:     else whattodo = revolve2_action(&rctx->check, &rctx->capo, &rctx->fine, rctx->snaps_in, &rctx->info, &rctx->where);
    962:     if (!toplevel) PetscCall(printwhattodo(viewer, whattodo, rctx, shift));
    963:     else PetscCall(printwhattodo2(viewer, whattodo, rctx, shift));
    964:     if (stype == REVOLVE_ONLINE && rctx->capo >= total_steps - 1) {
    965:       revolve_turn(total_steps, &rctx->capo, &rctx->fine);
    966:       PetscCall(printwhattodo(viewer, whattodo, rctx, shift));
    967:     }
    968:     rctx->stepsleft = rctx->capo - rctx->oldcapo - 1;
    969:   }
    970:   PetscFunctionReturn(PETSC_SUCCESS);
    971: }

    973: static PetscErrorCode TSTrajectoryMemorySet_ROF(TSTrajectory tj, TS ts, TJScheduler *tjsch, PetscInt stepnum, PetscReal time, Vec X)
    974: {
    975:   Stack          *stack = &tjsch->stack;
    976:   PetscInt        store;
    977:   StackElement    e;
    978:   PetscRevolveInt rtotal_steps, rstepnum;
    979:   CheckpointType  cptype;

    981:   PetscFunctionBegin;
    982:   if (!stack->solution_only && stepnum == 0) PetscFunctionReturn(PETSC_SUCCESS);
    983:   if (stack->solution_only && stepnum == tjsch->total_steps) PetscFunctionReturn(PETSC_SUCCESS);
    984:   PetscCall(PetscRevolveIntCast(tjsch->total_steps, &rtotal_steps));
    985:   PetscCall(PetscRevolveIntCast(stepnum, &rstepnum));
    986:   PetscCall(ApplyRevolve(tj->monitor, tjsch->stype, tjsch->rctx, rtotal_steps, rstepnum, rstepnum, PETSC_FALSE, &store));
    987:   if (store == 1) {
    988:     PetscCheck(stepnum >= stack->top, PetscObjectComm((PetscObject)ts), PETSC_ERR_MEMC, "Illegal modification of a non-top stack element");
    989:     cptype = stack->solution_only ? SOLUTIONONLY : SOLUTION_STAGES;
    990:     PetscCall(ElementCreate(ts, cptype, stack, &e));
    991:     PetscCall(ElementSet(ts, stack, &e, stepnum, time, X));
    992:     PetscCall(StackPush(stack, e));
    993:   }
    994:   PetscFunctionReturn(PETSC_SUCCESS);
    995: }

    997: static PetscErrorCode TSTrajectoryMemoryGet_ROF(TSTrajectory tj, TS ts, TJScheduler *tjsch, PetscInt stepnum)
    998: {
    999:   Stack          *stack = &tjsch->stack;
    1000:   PetscInt        store;
    1001:   PetscRevolveInt whattodo, shift, rtotal_steps, rstepnum;
    1002:   StackElement    e;

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

    1039: static PetscErrorCode TSTrajectoryMemorySet_RON(TSTrajectory tj, TS ts, TJScheduler *tjsch, PetscInt stepnum, PetscReal time, Vec X)
    1040: {
    1041:   Stack          *stack = &tjsch->stack;
    1042:   Vec            *Y;
    1043:   PetscInt        i, store;
    1044:   PetscReal       timeprev;
    1045:   StackElement    e;
    1046:   RevolveCTX     *rctx = tjsch->rctx;
    1047:   PetscRevolveInt rtotal_steps, rstepnum;
    1048:   CheckpointType  cptype;

    1050:   PetscFunctionBegin;
    1051:   if (!stack->solution_only && stepnum == 0) PetscFunctionReturn(PETSC_SUCCESS);
    1052:   if (stack->solution_only && stepnum == tjsch->total_steps) PetscFunctionReturn(PETSC_SUCCESS);
    1053:   PetscCall(PetscRevolveIntCast(tjsch->total_steps, &rtotal_steps));
    1054:   PetscCall(PetscRevolveIntCast(stepnum, &rstepnum));
    1055:   PetscCall(ApplyRevolve(tj->monitor, tjsch->stype, rctx, rtotal_steps, rstepnum, rstepnum, PETSC_FALSE, &store));
    1056:   if (store == 1) {
    1057:     if (rctx->check != stack->top + 1) { /* overwrite some non-top checkpoint in the stack */
    1058:       PetscCall(StackFind(stack, &e, rctx->check));
    1059:       if (HaveSolution(e->cptype)) PetscCall(VecCopy(X, e->X));
    1060:       if (HaveStages(e->cptype)) {
    1061:         PetscCall(TSGetStages(ts, &stack->numY, &Y));
    1062:         for (i = 0; i < stack->numY; i++) PetscCall(VecCopy(Y[i], e->Y[i]));
    1063:       }
    1064:       e->stepnum = stepnum;
    1065:       e->time    = time;
    1066:       PetscCall(TSGetPrevTime(ts, &timeprev));
    1067:       e->timeprev = timeprev;
    1068:     } else {
    1069:       PetscCheck(stepnum >= stack->top, PetscObjectComm((PetscObject)ts), PETSC_ERR_MEMC, "Illegal modification of a non-top stack element");
    1070:       cptype = stack->solution_only ? SOLUTIONONLY : SOLUTION_STAGES;
    1071:       PetscCall(ElementCreate(ts, cptype, stack, &e));
    1072:       PetscCall(ElementSet(ts, stack, &e, stepnum, time, X));
    1073:       PetscCall(StackPush(stack, e));
    1074:     }
    1075:   }
    1076:   PetscFunctionReturn(PETSC_SUCCESS);
    1077: }

    1079: static PetscErrorCode TSTrajectoryMemoryGet_RON(TSTrajectory tj, TS ts, TJScheduler *tjsch, PetscInt stepnum)
    1080: {
    1081:   Stack          *stack = &tjsch->stack;
    1082:   PetscRevolveInt whattodo, shift, rstepnum;
    1083:   StackElement    e;

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

    1123: static PetscErrorCode TSTrajectoryMemorySet_TLR(TSTrajectory tj, TS ts, TJScheduler *tjsch, PetscInt stepnum, PetscReal time, Vec X)
    1124: {
    1125:   Stack          *stack = &tjsch->stack;
    1126:   PetscInt        store, localstepnum, laststridesize;
    1127:   StackElement    e;
    1128:   PetscBool       done = PETSC_FALSE;
    1129:   PetscRevolveInt rtotal_steps, rstepnum, rlocalstepnum;
    1130:   CheckpointType  cptype;

    1132:   PetscFunctionBegin;
    1133:   if (!stack->solution_only && stepnum == 0) PetscFunctionReturn(PETSC_SUCCESS);
    1134:   if (stack->solution_only && stepnum == tjsch->total_steps) PetscFunctionReturn(PETSC_SUCCESS);

    1136:   localstepnum   = stepnum % tjsch->stride;
    1137:   laststridesize = tjsch->total_steps % tjsch->stride;
    1138:   if (!laststridesize) laststridesize = tjsch->stride;

    1140:   if (!tjsch->recompute) {
    1141:     PetscCall(TopLevelStore(tj, ts, tjsch, stepnum, localstepnum, laststridesize, &done));
    1142:     /* revolve is needed for the last stride; different starting points for last stride between solutin_only and !solutin_only */
    1143:     if (!stack->solution_only && !tjsch->save_stack && stepnum <= tjsch->total_steps - laststridesize) PetscFunctionReturn(PETSC_SUCCESS);
    1144:     if (stack->solution_only && !tjsch->save_stack && stepnum < tjsch->total_steps - laststridesize) PetscFunctionReturn(PETSC_SUCCESS);
    1145:   }
    1146:   if (tjsch->save_stack && done) {
    1147:     PetscCall(InitRevolve(tjsch->stride, tjsch->max_cps_ram, tjsch->rctx));
    1148:     PetscFunctionReturn(PETSC_SUCCESS);
    1149:   }
    1150:   if (laststridesize < tjsch->stride) {
    1151:     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 */
    1152:       PetscCall(InitRevolve(laststridesize, tjsch->max_cps_ram, tjsch->rctx));
    1153:     }
    1154:     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 */
    1155:       PetscCall(InitRevolve(laststridesize, tjsch->max_cps_ram, tjsch->rctx));
    1156:     }
    1157:   }
    1158:   PetscCall(PetscRevolveIntCast(tjsch->total_steps, &rtotal_steps));
    1159:   PetscCall(PetscRevolveIntCast(stepnum, &rstepnum));
    1160:   PetscCall(PetscRevolveIntCast(localstepnum, &rlocalstepnum));
    1161:   PetscCall(ApplyRevolve(tj->monitor, tjsch->stype, tjsch->rctx, rtotal_steps, rstepnum, rlocalstepnum, PETSC_FALSE, &store));
    1162:   if (store == 1) {
    1163:     PetscCheck(localstepnum >= stack->top, PetscObjectComm((PetscObject)ts), PETSC_ERR_MEMC, "Illegal modification of a non-top stack element");
    1164:     cptype = stack->solution_only ? SOLUTIONONLY : SOLUTION_STAGES;
    1165:     PetscCall(ElementCreate(ts, cptype, stack, &e));
    1166:     PetscCall(ElementSet(ts, stack, &e, stepnum, time, X));
    1167:     PetscCall(StackPush(stack, e));
    1168:   }
    1169:   PetscFunctionReturn(PETSC_SUCCESS);
    1170: }

    1172: static PetscErrorCode TSTrajectoryMemoryGet_TLR(TSTrajectory tj, TS ts, TJScheduler *tjsch, PetscInt stepnum)
    1173: {
    1174:   Stack          *stack = &tjsch->stack;
    1175:   PetscRevolveInt whattodo, shift, rstepnum, rlocalstepnum, rtotal_steps;
    1176:   PetscInt        localstepnum, stridenum, laststridesize, store;
    1177:   StackElement    e;
    1178:   CheckpointType  cptype;

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

    1273: static PetscErrorCode TSTrajectoryMemorySet_TLTR(TSTrajectory tj, TS ts, TJScheduler *tjsch, PetscInt stepnum, PetscReal time, Vec X)
    1274: {
    1275:   Stack          *stack = &tjsch->stack;
    1276:   PetscInt        store, localstepnum, stridenum, laststridesize;
    1277:   StackElement    e;
    1278:   PetscBool       done = PETSC_FALSE;
    1279:   PetscRevolveInt rlocalstepnum, rstepnum, rtotal_steps;

    1281:   PetscFunctionBegin;
    1282:   if (!stack->solution_only && stepnum == 0) PetscFunctionReturn(PETSC_SUCCESS);
    1283:   if (stack->solution_only && stepnum == tjsch->total_steps) PetscFunctionReturn(PETSC_SUCCESS);

    1285:   localstepnum   = stepnum % tjsch->stride; /* index at the bottom level (inside a stride) */
    1286:   stridenum      = stepnum / tjsch->stride; /* index at the top level */
    1287:   laststridesize = tjsch->total_steps % tjsch->stride;
    1288:   if (!laststridesize) laststridesize = tjsch->stride;
    1289:   if (stack->solution_only && localstepnum == 0 && !tjsch->rctx2->reverseonestep) {
    1290:     PetscCall(PetscRevolveIntCast((tjsch->total_steps + tjsch->stride - 1) / tjsch->stride, &rtotal_steps));
    1291:     PetscCall(PetscRevolveIntCast(stridenum, &rstepnum));
    1292:     PetscCall(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) PetscCall(InitRevolve(laststridesize, tjsch->max_cps_ram, tjsch->rctx));
    1294:   }
    1295:   if (!stack->solution_only && localstepnum == 1 && !tjsch->rctx2->reverseonestep) {
    1296:     PetscCall(PetscRevolveIntCast((tjsch->total_steps + tjsch->stride - 1) / tjsch->stride, &rtotal_steps));
    1297:     PetscCall(PetscRevolveIntCast(stridenum, &rstepnum));
    1298:     PetscCall(ApplyRevolve(tj->monitor, tjsch->stype, tjsch->rctx2, rtotal_steps, rstepnum, rstepnum, PETSC_TRUE, &tjsch->store_stride));
    1299:     if (laststridesize < tjsch->stride && stepnum == tjsch->total_steps - laststridesize + 1) PetscCall(InitRevolve(laststridesize, tjsch->max_cps_ram, tjsch->rctx));
    1300:   }
    1301:   if (tjsch->store_stride) {
    1302:     PetscCall(TopLevelStore(tj, ts, tjsch, stepnum, localstepnum, laststridesize, &done));
    1303:     if (done) {
    1304:       PetscCall(InitRevolve(tjsch->stride, tjsch->max_cps_ram, tjsch->rctx));
    1305:       PetscFunctionReturn(PETSC_SUCCESS);
    1306:     }
    1307:   }
    1308:   if (stepnum < tjsch->total_steps - laststridesize) {
    1309:     if (tjsch->save_stack && !tjsch->store_stride && !tjsch->rctx2->reverseonestep) PetscFunctionReturn(PETSC_SUCCESS); /* store or forward-and-reverse at top level trigger revolve at bottom level */
    1310:     if (!tjsch->save_stack && !tjsch->rctx2->reverseonestep) PetscFunctionReturn(PETSC_SUCCESS);                        /* store operation does not require revolve be called at bottom level */
    1311:   }
    1312:   /* 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 */
    1313:   if (!stack->solution_only && localstepnum == 0 && stepnum != tjsch->total_steps && !tjsch->recompute) PetscFunctionReturn(PETSC_SUCCESS);
    1314:   PetscCall(PetscRevolveIntCast(tjsch->total_steps, &rtotal_steps));
    1315:   PetscCall(PetscRevolveIntCast(stepnum, &rstepnum));
    1316:   PetscCall(PetscRevolveIntCast(localstepnum, &rlocalstepnum));
    1317:   PetscCall(ApplyRevolve(tj->monitor, tjsch->stype, tjsch->rctx, rtotal_steps, rstepnum, rlocalstepnum, PETSC_FALSE, &store));
    1318:   if (store == 1) {
    1319:     CheckpointType cptype;
    1320:     PetscCheck(localstepnum >= stack->top, PetscObjectComm((PetscObject)ts), PETSC_ERR_MEMC, "Illegal modification of a non-top stack element");
    1321:     cptype = stack->solution_only ? SOLUTIONONLY : SOLUTION_STAGES;
    1322:     PetscCall(ElementCreate(ts, cptype, stack, &e));
    1323:     PetscCall(ElementSet(ts, stack, &e, stepnum, time, X));
    1324:     PetscCall(StackPush(stack, e));
    1325:   }
    1326:   PetscFunctionReturn(PETSC_SUCCESS);
    1327: }

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

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

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

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

    1494:   PetscFunctionBegin;
    1495:   if (!stack->solution_only && stepnum == 0) PetscFunctionReturn(PETSC_SUCCESS);
    1496:   if (stack->solution_only && stepnum == tjsch->total_steps) PetscFunctionReturn(PETSC_SUCCESS);
    1497:   PetscCall(PetscRevolveIntCast(tjsch->total_steps, &rtotal_steps));
    1498:   PetscCall(PetscRevolveIntCast(stepnum, &rstepnum));
    1499:   PetscCall(ApplyRevolve(tj->monitor, tjsch->stype, tjsch->rctx, rtotal_steps, rstepnum, rstepnum, PETSC_FALSE, &store));
    1500:   if (store == 1) {
    1501:     CheckpointType cptype;
    1502:     PetscCheck(stepnum >= stack->top, PetscObjectComm((PetscObject)ts), PETSC_ERR_MEMC, "Illegal modification of a non-top stack element");
    1503:     cptype = stack->solution_only ? SOLUTIONONLY : SOLUTION_STAGES;
    1504:     PetscCall(ElementCreate(ts, cptype, stack, &e));
    1505:     PetscCall(ElementSet(ts, stack, &e, stepnum, time, X));
    1506:     PetscCall(StackPush(stack, e));
    1507:   } else if (store == 2) {
    1508:     PetscCall(DumpSingle(tj, ts, stack, tjsch->rctx->check + 1));
    1509:   }
    1510:   PetscFunctionReturn(PETSC_SUCCESS);
    1511: }

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

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

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

    1576:   PetscFunctionBegin;
    1577:   /* skip if no checkpoint to use. This also avoids an error when num_units_avail=0  */
    1578:   if (tjsch->actx->nextcheckpointstep == -1) PetscFunctionReturn(PETSC_SUCCESS);
    1579:   if (stepnum == 0) { /* When placing the first checkpoint, no need to change the units available */
    1580:     if (stack->solution_only) PetscCallExternal(offline_ca, tjsch->actx->lastcheckpointstep, tjsch->actx->num_units_avail, tjsch->actx->endstep, &tjsch->actx->nextcheckpointstep);
    1581:     else {
    1582:       /* First two arguments must be -1 when first time calling cams */
    1583:       PetscCallExternal(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);
    1584:     }
    1585:   }

    1587:   if (stack->solution_only && stepnum == tjsch->total_steps) PetscFunctionReturn(PETSC_SUCCESS);

    1589:   if (tjsch->actx->nextcheckpointstep == stepnum) {
    1590:     PetscCheck(stepnum >= stack->top, PetscObjectComm((PetscObject)ts), PETSC_ERR_MEMC, "Illegal modification of a non-top stack element");

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

    1609:     tjsch->actx->lastcheckpointstep = stepnum;
    1610:     if (stack->solution_only) {
    1611:       PetscCallExternal(offline_ca, tjsch->actx->lastcheckpointstep, tjsch->actx->num_units_avail, tjsch->actx->endstep, &tjsch->actx->nextcheckpointstep);
    1612:       tjsch->actx->num_units_avail--;
    1613:     } else {
    1614:       tjsch->actx->lastcheckpointtype = tjsch->actx->nextcheckpointtype;
    1615:       PetscCallExternal(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);
    1616:       if (tjsch->actx->lastcheckpointtype == 2) tjsch->actx->num_units_avail -= tjsch->actx->num_stages + 1;
    1617:       if (tjsch->actx->lastcheckpointtype == 1) tjsch->actx->num_units_avail -= tjsch->actx->num_stages;
    1618:       if (tjsch->actx->lastcheckpointtype == 0) tjsch->actx->num_units_avail--;
    1619:     }
    1620:   }
    1621:   PetscFunctionReturn(PETSC_SUCCESS);
    1622: }

    1624: static PetscErrorCode TSTrajectoryMemoryGet_AOF(TSTrajectory tj, TS ts, TJScheduler *tjsch, PetscInt stepnum)
    1625: {
    1626:   Stack       *stack = &tjsch->stack;
    1627:   StackElement e;
    1628:   PetscInt     estepnum;

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

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

    1687:   PetscFunctionBegin;
    1688:   if (!tjsch->recompute) { /* use global stepnum in the forward sweep */
    1689:     PetscCall(TSGetStepNumber(ts, &stepnum));
    1690:   }
    1691:   /* for consistency */
    1692:   if (!tjsch->recompute && stepnum == 0) ts->ptime_prev = ts->ptime - ts->time_step;
    1693:   switch (tjsch->stype) {
    1694:   case NONE:
    1695:     if (tj->adjoint_solve_mode) {
    1696:       PetscCall(TSTrajectoryMemorySet_N(ts, tjsch, stepnum, time, X));
    1697:     } else {
    1698:       PetscCall(TSTrajectoryMemorySet_N_2(ts, tjsch, stepnum, time, X));
    1699:     }
    1700:     break;
    1701:   case TWO_LEVEL_NOREVOLVE:
    1702:     PetscCheck(tj->adjoint_solve_mode, PetscObjectComm((PetscObject)tj), PETSC_ERR_SUP, "Not implemented");
    1703:     PetscCall(TSTrajectoryMemorySet_TLNR(tj, ts, tjsch, stepnum, time, X));
    1704:     break;
    1705: #if defined(PETSC_HAVE_REVOLVE)
    1706:   case TWO_LEVEL_REVOLVE:
    1707:     PetscCheck(tj->adjoint_solve_mode, PetscObjectComm((PetscObject)tj), PETSC_ERR_SUP, "Not implemented");
    1708:     PetscCall(TSTrajectoryMemorySet_TLR(tj, ts, tjsch, stepnum, time, X));
    1709:     break;
    1710:   case TWO_LEVEL_TWO_REVOLVE:
    1711:     PetscCheck(tj->adjoint_solve_mode, PetscObjectComm((PetscObject)tj), PETSC_ERR_SUP, "Not implemented");
    1712:     PetscCall(TSTrajectoryMemorySet_TLTR(tj, ts, tjsch, stepnum, time, X));
    1713:     break;
    1714:   case REVOLVE_OFFLINE:
    1715:     PetscCheck(tj->adjoint_solve_mode, PetscObjectComm((PetscObject)tj), PETSC_ERR_SUP, "Not implemented");
    1716:     PetscCall(TSTrajectoryMemorySet_ROF(tj, ts, tjsch, stepnum, time, X));
    1717:     break;
    1718:   case REVOLVE_ONLINE:
    1719:     PetscCheck(tj->adjoint_solve_mode, PetscObjectComm((PetscObject)tj), PETSC_ERR_SUP, "Not implemented");
    1720:     PetscCall(TSTrajectoryMemorySet_RON(tj, ts, tjsch, stepnum, time, X));
    1721:     break;
    1722:   case REVOLVE_MULTISTAGE:
    1723:     PetscCheck(tj->adjoint_solve_mode, PetscObjectComm((PetscObject)tj), PETSC_ERR_SUP, "Not implemented");
    1724:     PetscCall(TSTrajectoryMemorySet_RMS(tj, ts, tjsch, stepnum, time, X));
    1725:     break;
    1726: #endif
    1727: #if defined(PETSC_HAVE_CAMS)
    1728:   case CAMS_OFFLINE:
    1729:     PetscCheck(tj->adjoint_solve_mode, PetscObjectComm((PetscObject)tj), PETSC_ERR_SUP, "Not implemented");
    1730:     PetscCall(TSTrajectoryMemorySet_AOF(tj, ts, tjsch, stepnum, time, X));
    1731:     break;
    1732: #endif
    1733:   default:
    1734:     break;
    1735:   }
    1736:   PetscFunctionReturn(PETSC_SUCCESS);
    1737: }

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

    1743:   PetscFunctionBegin;
    1744:   if (tj->adjoint_solve_mode && stepnum == 0) {
    1745:     PetscCall(TSTrajectoryReset(tj)); /* reset TSTrajectory so users do not need to reset TSTrajectory */
    1746:     PetscFunctionReturn(PETSC_SUCCESS);
    1747:   }
    1748:   switch (tjsch->stype) {
    1749:   case NONE:
    1750:     if (tj->adjoint_solve_mode) {
    1751:       PetscCall(TSTrajectoryMemoryGet_N(ts, tjsch, stepnum));
    1752:     } else {
    1753:       PetscCall(TSTrajectoryMemoryGet_N_2(ts, tjsch, stepnum));
    1754:     }
    1755:     break;
    1756:   case TWO_LEVEL_NOREVOLVE:
    1757:     PetscCheck(tj->adjoint_solve_mode, PetscObjectComm((PetscObject)tj), PETSC_ERR_SUP, "Not implemented");
    1758:     PetscCall(TSTrajectoryMemoryGet_TLNR(tj, ts, tjsch, stepnum));
    1759:     break;
    1760: #if defined(PETSC_HAVE_REVOLVE)
    1761:   case TWO_LEVEL_REVOLVE:
    1762:     PetscCheck(tj->adjoint_solve_mode, PetscObjectComm((PetscObject)tj), PETSC_ERR_SUP, "Not implemented");
    1763:     PetscCall(TSTrajectoryMemoryGet_TLR(tj, ts, tjsch, stepnum));
    1764:     break;
    1765:   case TWO_LEVEL_TWO_REVOLVE:
    1766:     PetscCheck(tj->adjoint_solve_mode, PetscObjectComm((PetscObject)tj), PETSC_ERR_SUP, "Not implemented");
    1767:     PetscCall(TSTrajectoryMemoryGet_TLTR(tj, ts, tjsch, stepnum));
    1768:     break;
    1769:   case REVOLVE_OFFLINE:
    1770:     PetscCheck(tj->adjoint_solve_mode, PetscObjectComm((PetscObject)tj), PETSC_ERR_SUP, "Not implemented");
    1771:     PetscCall(TSTrajectoryMemoryGet_ROF(tj, ts, tjsch, stepnum));
    1772:     break;
    1773:   case REVOLVE_ONLINE:
    1774:     PetscCheck(tj->adjoint_solve_mode, PetscObjectComm((PetscObject)tj), PETSC_ERR_SUP, "Not implemented");
    1775:     PetscCall(TSTrajectoryMemoryGet_RON(tj, ts, tjsch, stepnum));
    1776:     break;
    1777:   case REVOLVE_MULTISTAGE:
    1778:     PetscCheck(tj->adjoint_solve_mode, PetscObjectComm((PetscObject)tj), PETSC_ERR_SUP, "Not implemented");
    1779:     PetscCall(TSTrajectoryMemoryGet_RMS(tj, ts, tjsch, stepnum));
    1780:     break;
    1781: #endif
    1782: #if defined(PETSC_HAVE_CAMS)
    1783:   case CAMS_OFFLINE:
    1784:     PetscCheck(tj->adjoint_solve_mode, PetscObjectComm((PetscObject)tj), PETSC_ERR_SUP, "Not implemented");
    1785:     PetscCall(TSTrajectoryMemoryGet_AOF(tj, ts, tjsch, stepnum));
    1786:     break;
    1787: #endif
    1788:   default:
    1789:     break;
    1790:   }
    1791:   PetscFunctionReturn(PETSC_SUCCESS);
    1792: }

    1794: PETSC_UNUSED static PetscErrorCode TSTrajectorySetStride_Memory(TSTrajectory tj, PetscInt stride)
    1795: {
    1796:   TJScheduler *tjsch = (TJScheduler *)tj->data;

    1798:   PetscFunctionBegin;
    1799:   tjsch->stride = stride;
    1800:   PetscFunctionReturn(PETSC_SUCCESS);
    1801: }

    1803: static PetscErrorCode TSTrajectorySetMaxCpsRAM_Memory(TSTrajectory tj, PetscInt max_cps_ram)
    1804: {
    1805:   TJScheduler *tjsch = (TJScheduler *)tj->data;

    1807:   PetscFunctionBegin;
    1808:   tjsch->max_cps_ram = max_cps_ram;
    1809:   PetscFunctionReturn(PETSC_SUCCESS);
    1810: }

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

    1816:   PetscFunctionBegin;
    1817:   tjsch->max_cps_disk = max_cps_disk;
    1818:   PetscFunctionReturn(PETSC_SUCCESS);
    1819: }

    1821: static PetscErrorCode TSTrajectorySetMaxUnitsRAM_Memory(TSTrajectory tj, PetscInt max_units_ram)
    1822: {
    1823:   TJScheduler *tjsch = (TJScheduler *)tj->data;

    1825:   PetscFunctionBegin;
    1826:   PetscCheck(tjsch->max_cps_ram, PetscObjectComm((PetscObject)tj), PETSC_ERR_ARG_INCOMP, "Conflict with -ts_trjaectory_max_cps_ram or TSTrajectorySetMaxCpsRAM. You can set max_cps_ram or max_units_ram, but not both at the same time.");
    1827:   tjsch->max_units_ram = max_units_ram;
    1828:   PetscFunctionReturn(PETSC_SUCCESS);
    1829: }

    1831: static PetscErrorCode TSTrajectorySetMaxUnitsDisk_Memory(TSTrajectory tj, PetscInt max_units_disk)
    1832: {
    1833:   TJScheduler *tjsch = (TJScheduler *)tj->data;

    1835:   PetscFunctionBegin;
    1836:   PetscCheck(tjsch->max_cps_disk, PetscObjectComm((PetscObject)tj), PETSC_ERR_ARG_INCOMP, "Conflict with -ts_trjaectory_max_cps_disk or TSTrajectorySetMaxCpsDisk. You can set max_cps_disk or max_units_disk, but not both at the same time.");
    1837:   tjsch->max_units_ram = max_units_disk;
    1838:   PetscFunctionReturn(PETSC_SUCCESS);
    1839: }

    1841: static PetscErrorCode TSTrajectoryMemorySetType_Memory(TSTrajectory tj, TSTrajectoryMemoryType tj_memory_type)
    1842: {
    1843:   TJScheduler *tjsch = (TJScheduler *)tj->data;

    1845:   PetscFunctionBegin;
    1846:   PetscCheck(!tj->setupcalled, PetscObjectComm((PetscObject)tj), PETSC_ERR_ARG_WRONGSTATE, "Cannot change schedule software after TSTrajectory has been setup or used");
    1847:   tjsch->tj_memory_type = tj_memory_type;
    1848:   PetscFunctionReturn(PETSC_SUCCESS);
    1849: }

    1851: #if defined(PETSC_HAVE_REVOLVE)
    1852: PETSC_UNUSED static PetscErrorCode TSTrajectorySetRevolveOnline(TSTrajectory tj, PetscBool use_online)
    1853: {
    1854:   TJScheduler *tjsch = (TJScheduler *)tj->data;

    1856:   PetscFunctionBegin;
    1857:   tjsch->use_online = use_online;
    1858:   PetscFunctionReturn(PETSC_SUCCESS);
    1859: }
    1860: #endif

    1862: PETSC_UNUSED static PetscErrorCode TSTrajectorySetSaveStack(TSTrajectory tj, PetscBool save_stack)
    1863: {
    1864:   TJScheduler *tjsch = (TJScheduler *)tj->data;

    1866:   PetscFunctionBegin;
    1867:   tjsch->save_stack = save_stack;
    1868:   PetscFunctionReturn(PETSC_SUCCESS);
    1869: }

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

    1875:   PetscFunctionBegin;
    1876:   tjsch->stack.use_dram = use_dram;
    1877:   PetscFunctionReturn(PETSC_SUCCESS);
    1878: }

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

    1883:   Logically Collective

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

    1889:   Options Database Key:
    1890: . -ts_trajectory_memory_type tj_memory_type - petsc, revolve, cams

    1892:   Level: intermediate

    1894: .seealso: [](ch_ts), `TSTrajectory`, `TSTrajectorySetMaxUnitsRAM()`, `TSTrajectoryMemoryType`
    1895: @*/
    1896: PetscErrorCode TSTrajectoryMemorySetType(TSTrajectory tj, TSTrajectoryMemoryType tj_memory_type)
    1897: {
    1898:   PetscFunctionBegin;
    1899:   PetscTryMethod(tj, "TSTrajectoryMemorySetType_C", (TSTrajectory, TSTrajectoryMemoryType), (tj, tj_memory_type));
    1900:   PetscFunctionReturn(PETSC_SUCCESS);
    1901: }

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

    1906:   Logically Collective

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

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

    1914:   Level: intermediate

    1916: .seealso: [](ch_ts), `TSTrajectory`, `TSTrajectorySetMaxUnitsRAM()`
    1917: @*/
    1918: PetscErrorCode TSTrajectorySetMaxCpsRAM(TSTrajectory tj, PetscInt max_cps_ram)
    1919: {
    1920:   PetscFunctionBegin;
    1921:   PetscUseMethod(tj, "TSTrajectorySetMaxCpsRAM_C", (TSTrajectory, PetscInt), (tj, max_cps_ram));
    1922:   PetscFunctionReturn(PETSC_SUCCESS);
    1923: }

    1925: /*@
    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: [](ch_ts), `TSTrajectory`, `TSTrajectorySetMaxUnitsDisk()`, `TSTrajectorySetMaxUnitsRAM()`
    1939: @*/
    1940: PetscErrorCode TSTrajectorySetMaxCpsDisk(TSTrajectory tj, PetscInt max_cps_disk)
    1941: {
    1942:   PetscFunctionBegin;
    1943:   PetscUseMethod(tj, "TSTrajectorySetMaxCpsDisk_C", (TSTrajectory, PetscInt), (tj, max_cps_disk));
    1944:   PetscFunctionReturn(PETSC_SUCCESS);
    1945: }

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

    1950:   Logically Collective

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

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

    1958:   Level: intermediate

    1960: .seealso: [](ch_ts), `TSTrajectory`, `TSTrajectorySetMaxCpsRAM()`
    1961: @*/
    1962: PetscErrorCode TSTrajectorySetMaxUnitsRAM(TSTrajectory tj, PetscInt max_units_ram)
    1963: {
    1964:   PetscFunctionBegin;
    1965:   PetscUseMethod(tj, "TSTrajectorySetMaxUnitsRAM_C", (TSTrajectory, PetscInt), (tj, max_units_ram));
    1966:   PetscFunctionReturn(PETSC_SUCCESS);
    1967: }

    1969: /*@
    1970:   TSTrajectorySetMaxUnitsDisk - Set maximum number of checkpointing units on disk

    1972:   Logically Collective

    1974:   Input Parameter:
    1975: . tj - tstrajectory context

    1977:   Output Parameter:
    1978: . max_units_disk - maximum number of checkpointing units on disk

    1980:   Level: intermediate

    1982: .seealso: [](ch_ts), `TSTrajectory`, `TSTrajectorySetMaxCpsDisk()`
    1983: @*/
    1984: PetscErrorCode TSTrajectorySetMaxUnitsDisk(TSTrajectory tj, PetscInt max_units_disk)
    1985: {
    1986:   PetscFunctionBegin;
    1987:   PetscUseMethod(tj, "TSTrajectorySetMaxUnitsDisk_C", (TSTrajectory, PetscInt), (tj, max_units_disk));
    1988:   PetscFunctionReturn(PETSC_SUCCESS);
    1989: }

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

    1998:   PetscFunctionBegin;
    1999:   PetscOptionsHeadBegin(PetscOptionsObject, "Memory based TS trajectory options");
    2000:   {
    2001:     PetscCall(PetscOptionsInt("-ts_trajectory_max_cps_ram", "Maximum number of checkpoints in RAM", "TSTrajectorySetMaxCpsRAM", tjsch->max_cps_ram, &max_cps_ram, &flg));
    2002:     if (flg) PetscCall(TSTrajectorySetMaxCpsRAM(tj, max_cps_ram));
    2003:     PetscCall(PetscOptionsInt("-ts_trajectory_max_cps_disk", "Maximum number of checkpoints on disk", "TSTrajectorySetMaxCpsDisk", tjsch->max_cps_disk, &max_cps_disk, &flg));
    2004:     if (flg) PetscCall(TSTrajectorySetMaxCpsDisk(tj, max_cps_disk));
    2005:     PetscCall(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) PetscCall(TSTrajectorySetMaxUnitsRAM(tj, max_units_ram));
    2007:     PetscCall(PetscOptionsInt("-ts_trajectory_max_units_disk", "Maximum number of checkpointing units on disk", "TSTrajectorySetMaxUnitsDisk", tjsch->max_units_disk, &max_units_disk, &flg));
    2008:     if (flg) PetscCall(TSTrajectorySetMaxUnitsDisk(tj, max_units_disk));
    2009:     PetscCall(PetscOptionsInt("-ts_trajectory_stride", "Stride to save checkpoints to file", "TSTrajectorySetStride", tjsch->stride, &tjsch->stride, NULL));
    2010: #if defined(PETSC_HAVE_REVOLVE)
    2011:     PetscCall(PetscOptionsBool("-ts_trajectory_revolve_online", "Trick TS trajectory into using online mode of revolve", "TSTrajectorySetRevolveOnline", tjsch->use_online, &tjsch->use_online, NULL));
    2012: #endif
    2013:     PetscCall(PetscOptionsBool("-ts_trajectory_save_stack", "Save all stack to disk", "TSTrajectorySetSaveStack", tjsch->save_stack, &tjsch->save_stack, NULL));
    2014:     PetscCall(PetscOptionsBool("-ts_trajectory_use_dram", "Use DRAM for checkpointing", "TSTrajectorySetUseDRAM", tjsch->stack.use_dram, &tjsch->stack.use_dram, NULL));
    2015:     PetscCall(PetscOptionsEnum("-ts_trajectory_memory_type", "Checkpointing schedule software to use", "TSTrajectoryMemorySetType", TSTrajectoryMemoryTypes, (PetscEnum)(int)tjsch->tj_memory_type, &etmp, &flg));
    2016:     if (flg) PetscCall(TSTrajectoryMemorySetType(tj, (TSTrajectoryMemoryType)etmp));
    2017:   }
    2018:   PetscOptionsHeadEnd();
    2019:   PetscFunctionReturn(PETSC_SUCCESS);
    2020: }

    2022: static PetscErrorCode TSTrajectorySetUp_Memory(TSTrajectory tj, TS ts)
    2023: {
    2024:   TJScheduler *tjsch = (TJScheduler *)tj->data;
    2025:   Stack       *stack = &tjsch->stack;
    2026: #if defined(PETSC_HAVE_REVOLVE)
    2027:   RevolveCTX *rctx, *rctx2;
    2028:   DiskStack  *diskstack = &tjsch->diskstack;
    2029:   PetscInt    diskblocks;
    2030: #endif
    2031:   PetscInt  numY, total_steps;
    2032:   PetscBool fixedtimestep;

    2034:   PetscFunctionBegin;
    2035:   if (ts->adapt) {
    2036:     PetscCall(PetscObjectTypeCompare((PetscObject)ts->adapt, TSADAPTNONE, &fixedtimestep));
    2037:   } else {
    2038:     fixedtimestep = PETSC_TRUE;
    2039:   }
    2040:   total_steps = (PetscInt)(PetscCeilReal((ts->max_time - ts->ptime) / ts->time_step));
    2041:   total_steps = total_steps < 0 ? PETSC_INT_MAX : total_steps;
    2042:   if (fixedtimestep) tjsch->total_steps = PetscMin(ts->max_steps, total_steps);

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

    2058:   /* Determine the scheduler type */
    2059:   if (tjsch->stride > 1) { /* two level mode */
    2060:     PetscCheck(!tjsch->save_stack || tjsch->max_cps_disk <= 1 || tjsch->max_cps_disk > tjsch->max_cps_ram, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_INCOMP, "The specified disk capacity is not enough to store a full stack of RAM checkpoints. You might want to change the disk capacity or use single level checkpointing instead.");
    2061:     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 */
    2062:     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 */
    2063:     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 */
    2064:   } else {                                                                                                                                  /* single level mode */
    2065:     if (fixedtimestep) {
    2066:       if (tjsch->max_cps_ram >= tjsch->total_steps - 1 || tjsch->max_cps_ram == -1) tjsch->stype = NONE; /* checkpoint all */
    2067:       else { /* choose the schedule software for offline checkpointing */ switch (tjsch->tj_memory_type) {
    2068:         case TJ_PETSC:
    2069:           tjsch->stype = NONE;
    2070:           break;
    2071:         case TJ_CAMS:
    2072:           tjsch->stype = CAMS_OFFLINE;
    2073:           break;
    2074:         case TJ_REVOLVE:
    2075:           tjsch->stype = (tjsch->max_cps_disk > 1) ? REVOLVE_MULTISTAGE : REVOLVE_OFFLINE;
    2076:           break;
    2077:         default:
    2078:           break;
    2079:         }
    2080:       }
    2081:     } else tjsch->stype = NONE; /* checkpoint all for adaptive time step */
    2082: #if defined(PETSC_HAVE_REVOLVE)
    2083:     if (tjsch->use_online) tjsch->stype = REVOLVE_ONLINE; /* trick into online (for testing purpose only) */
    2084: #endif
    2085:     PetscCheck(tjsch->stype == NONE || tjsch->max_cps_ram >= 1 || tjsch->max_cps_disk >= 1, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_INCOMP, "The specified storage capacity is insufficient for one checkpoint, which is the minimum");
    2086:   }
    2087:   if (tjsch->stype >= CAMS_OFFLINE) {
    2088: #if !defined(PETSC_HAVE_CAMS)
    2089:     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.");
    2090: #else
    2091:     CAMSCTX *actx;
    2092:     PetscInt ns = 0;
    2093:     if (stack->solution_only) {
    2094:       offline_ca_create(tjsch->total_steps, tjsch->max_cps_ram);
    2095:     } else {
    2096:       PetscCall(TSGetStages(ts, &ns, PETSC_IGNORE));
    2097:       offline_cams_create(tjsch->total_steps, tjsch->max_units_ram, ns, ts->stifflyaccurate);
    2098:     }
    2099:     PetscCall(PetscNew(&actx));
    2100:     actx->lastcheckpointstep = -1; /* -1 can trigger the initialization of CAMS */
    2101:     actx->lastcheckpointtype = -1; /* -1 can trigger the initialization of CAMS */
    2102:     actx->endstep            = tjsch->total_steps;
    2103:     actx->num_units_avail    = tjsch->max_units_ram;
    2104:     actx->num_stages         = ns;
    2105:     tjsch->actx              = actx;
    2106: #endif
    2107:   } else if (tjsch->stype > TWO_LEVEL_NOREVOLVE) {
    2108: #if !defined(PETSC_HAVE_REVOLVE)
    2109:     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.");
    2110: #else
    2111:     PetscRevolveInt rfine, rsnaps, rsnaps2;

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

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

    2191:   stack->stacksize = PetscMax(stack->stacksize, 1);
    2192:   tjsch->recompute = PETSC_FALSE;
    2193:   PetscCall(StackInit(stack, stack->stacksize, numY));
    2194:   PetscFunctionReturn(PETSC_SUCCESS);
    2195: }

    2197: static PetscErrorCode TSTrajectoryReset_Memory(TSTrajectory tj)
    2198: {
    2199: #if defined(PETSC_HAVE_REVOLVE) || defined(PETSC_HAVE_CAMS)
    2200:   TJScheduler *tjsch = (TJScheduler *)tj->data;
    2201: #endif

    2203:   PetscFunctionBegin;
    2204: #if defined(PETSC_HAVE_REVOLVE)
    2205:   if (tjsch->stype > TWO_LEVEL_NOREVOLVE) {
    2206:     revolve_reset();
    2207:     if (tjsch->stype == TWO_LEVEL_TWO_REVOLVE) {
    2208:       revolve2_reset();
    2209:       PetscCall(PetscFree(tjsch->diskstack.container));
    2210:     }
    2211:   }
    2212:   if (tjsch->stype > TWO_LEVEL_NOREVOLVE) {
    2213:     PetscCall(PetscFree(tjsch->rctx));
    2214:     PetscCall(PetscFree(tjsch->rctx2));
    2215:   }
    2216: #endif
    2217: #if defined(PETSC_HAVE_CAMS)
    2218:   if (tjsch->stype == CAMS_OFFLINE) {
    2219:     if (tjsch->stack.solution_only) offline_ca_destroy();
    2220:     else offline_ca_destroy();
    2221:     PetscCall(PetscFree(tjsch->actx));
    2222:   }
    2223: #endif
    2224:   PetscFunctionReturn(PETSC_SUCCESS);
    2225: }

    2227: static PetscErrorCode TSTrajectoryDestroy_Memory(TSTrajectory tj)
    2228: {
    2229:   TJScheduler *tjsch = (TJScheduler *)tj->data;

    2231:   PetscFunctionBegin;
    2232:   PetscCall(StackDestroy(&tjsch->stack));
    2233:   PetscCall(PetscViewerDestroy(&tjsch->viewer));
    2234:   PetscCall(PetscObjectComposeFunction((PetscObject)tj, "TSTrajectorySetMaxCpsRAM_C", NULL));
    2235:   PetscCall(PetscObjectComposeFunction((PetscObject)tj, "TSTrajectorySetMaxCpsDisk_C", NULL));
    2236:   PetscCall(PetscObjectComposeFunction((PetscObject)tj, "TSTrajectorySetMaxUnitsRAM_C", NULL));
    2237:   PetscCall(PetscObjectComposeFunction((PetscObject)tj, "TSTrajectorySetMaxUnitsDisk_C", NULL));
    2238:   PetscCall(PetscObjectComposeFunction((PetscObject)tj, "TSTrajectoryMemorySetType_C", NULL));
    2239:   PetscCall(PetscFree(tjsch));
    2240:   PetscFunctionReturn(PETSC_SUCCESS);
    2241: }

    2243: /*MC
    2244:       TSTRAJECTORYMEMORY - Stores each solution of the ODE/ADE in memory

    2246:   Level: intermediate

    2248: .seealso: [](ch_ts), `TSTrajectoryCreate()`, `TS`, `TSTrajectorySetType()`, `TSTrajectoryType`, `TSTrajectory`
    2249: M*/
    2250: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Memory(TSTrajectory tj, TS ts)
    2251: {
    2252:   TJScheduler *tjsch;

    2254:   PetscFunctionBegin;
    2255:   tj->ops->set            = TSTrajectorySet_Memory;
    2256:   tj->ops->get            = TSTrajectoryGet_Memory;
    2257:   tj->ops->setup          = TSTrajectorySetUp_Memory;
    2258:   tj->ops->setfromoptions = TSTrajectorySetFromOptions_Memory;
    2259:   tj->ops->reset          = TSTrajectoryReset_Memory;
    2260:   tj->ops->destroy        = TSTrajectoryDestroy_Memory;

    2262:   PetscCall(PetscNew(&tjsch));
    2263:   tjsch->stype        = NONE;
    2264:   tjsch->max_cps_ram  = -1; /* -1 indicates that it is not set */
    2265:   tjsch->max_cps_disk = -1; /* -1 indicates that it is not set */
    2266:   tjsch->stride       = 0;  /* if not zero, two-level checkpointing will be used */
    2267: #if defined(PETSC_HAVE_REVOLVE)
    2268:   tjsch->use_online = PETSC_FALSE;
    2269: #endif
    2270:   tjsch->save_stack = PETSC_TRUE;

    2272:   tjsch->stack.solution_only = tj->solution_only;
    2273:   PetscCall(PetscViewerCreate(PetscObjectComm((PetscObject)tj), &tjsch->viewer));
    2274:   PetscCall(PetscViewerSetType(tjsch->viewer, PETSCVIEWERBINARY));
    2275:   PetscCall(PetscViewerPushFormat(tjsch->viewer, PETSC_VIEWER_NATIVE));
    2276:   PetscCall(PetscViewerFileSetMode(tjsch->viewer, FILE_MODE_WRITE));

    2278:   PetscCall(PetscObjectComposeFunction((PetscObject)tj, "TSTrajectorySetMaxCpsRAM_C", TSTrajectorySetMaxCpsRAM_Memory));
    2279:   PetscCall(PetscObjectComposeFunction((PetscObject)tj, "TSTrajectorySetMaxCpsDisk_C", TSTrajectorySetMaxCpsDisk_Memory));
    2280:   PetscCall(PetscObjectComposeFunction((PetscObject)tj, "TSTrajectorySetMaxUnitsRAM_C", TSTrajectorySetMaxUnitsRAM_Memory));
    2281:   PetscCall(PetscObjectComposeFunction((PetscObject)tj, "TSTrajectorySetMaxUnitsDisk_C", TSTrajectorySetMaxUnitsDisk_Memory));
    2282:   PetscCall(PetscObjectComposeFunction((PetscObject)tj, "TSTrajectoryMemorySetType_C", TSTrajectoryMemorySetType_Memory));
    2283:   tj->data = tjsch;
    2284:   PetscFunctionReturn(PETSC_SUCCESS);
    2285: }