Actual source code: trajmemory.c
petsc-3.11.4 2019-09-28
1: #include <petsc/private/tsimpl.h>
2: #include <petscsys.h>
3: #if defined(PETSC_HAVE_REVOLVE)
4: #include <revolve_c.h>
5: #endif
7: PetscLogEvent TSTrajectory_DiskWrite, TSTrajectory_DiskRead;
8: static PetscErrorCode TSTrajectorySet_Memory(TSTrajectory,TS,PetscInt,PetscReal,Vec);
10: typedef enum {NONE,TWO_LEVEL_NOREVOLVE,TWO_LEVEL_REVOLVE,TWO_LEVEL_TWO_REVOLVE,REVOLVE_OFFLINE,REVOLVE_ONLINE,REVOLVE_MULTISTAGE} SchedulerType;
12: typedef struct _StackElement {
13: PetscInt stepnum;
14: Vec X;
15: Vec *Y;
16: PetscReal time;
17: PetscReal timeprev; /* for no solution_only mode */
18: PetscReal timenext; /* for solution_only mode */
19: } *StackElement;
21: #if defined(PETSC_HAVE_REVOLVE)
22: typedef struct _RevolveCTX {
23: PetscBool reverseonestep;
24: PetscInt where;
25: PetscInt snaps_in;
26: PetscInt stepsleft;
27: PetscInt check;
28: PetscInt oldcapo;
29: PetscInt capo;
30: PetscInt fine;
31: PetscInt info;
32: } RevolveCTX;
33: #endif
35: typedef struct _Stack {
36: PetscInt stacksize;
37: PetscInt top;
38: StackElement *container;
39: PetscInt numY;
40: PetscBool solution_only;
41: PetscBool use_dram;
42: } Stack;
44: typedef struct _DiskStack {
45: PetscInt stacksize;
46: PetscInt top;
47: PetscInt *container;
48: } DiskStack;
50: typedef struct _TJScheduler {
51: SchedulerType stype;
52: #if defined(PETSC_HAVE_REVOLVE)
53: RevolveCTX *rctx,*rctx2;
54: PetscBool use_online;
55: PetscInt store_stride;
56: #endif
57: PetscBool recompute;
58: PetscBool skip_trajectory;
59: PetscBool save_stack;
60: PetscInt max_cps_ram; /* maximum checkpoints in RAM */
61: PetscInt max_cps_disk; /* maximum checkpoints on disk */
62: PetscInt stride;
63: PetscInt total_steps; /* total number of steps */
64: Stack stack;
65: DiskStack diskstack;
66: } TJScheduler;
68: static PetscErrorCode TurnForwardWithStepsize(TS ts,PetscReal nextstepsize)
69: {
73: /* reverse the direction */
74: TSSetTimeStep(ts,nextstepsize);
75: return(0);
76: }
78: static PetscErrorCode TurnForward(TS ts)
79: {
80: PetscReal stepsize;
84: /* reverse the direction */
85: TSGetTimeStep(ts,&stepsize);
86: TSSetTimeStep(ts,-stepsize);
87: return(0);
88: }
90: static PetscErrorCode TurnBackward(TS ts)
91: {
92: PetscReal stepsize;
96: if (!ts->trajectory->adjoint_solve_mode) return(0);
97: /* reverse the direction */
98: stepsize = ts->ptime_prev-ts->ptime;
99: TSSetTimeStep(ts,stepsize);
100: return(0);
101: }
103: static PetscErrorCode ElementCreate(TS ts,Stack *stack,StackElement *e)
104: {
105: Vec X;
106: Vec *Y;
110: if (stack->use_dram) {
111: PetscMallocSetDRAM();
112: }
113: PetscCalloc1(1,e);
114: TSGetSolution(ts,&X);
115: VecDuplicate(X,&(*e)->X);
116: if (stack->numY > 0 && !stack->solution_only) {
117: TSGetStages(ts,&stack->numY,&Y);
118: VecDuplicateVecs(Y[0],stack->numY,&(*e)->Y);
119: }
120: if (stack->use_dram) {
121: PetscMallocResetDRAM();
122: }
123: return(0);
124: }
126: static PetscErrorCode ElementSet(TS ts,Stack *stack,StackElement *e,PetscInt stepnum,PetscReal time,Vec X)
127: {
128: Vec *Y;
129: PetscInt i;
130: PetscReal timeprev;
134: VecCopy(X,(*e)->X);
135: if (stack->numY > 0 && !stack->solution_only) {
136: TSGetStages(ts,&stack->numY,&Y);
137: for (i=0;i<stack->numY;i++) {
138: VecCopy(Y[i],(*e)->Y[i]);
139: }
140: }
141: (*e)->stepnum = stepnum;
142: (*e)->time = time;
143: /* for consistency */
144: if (stepnum == 0) {
145: (*e)->timeprev = (*e)->time - ts->time_step;
146: } else {
147: TSGetPrevTime(ts,&timeprev);
148: (*e)->timeprev = timeprev;
149: }
150: return(0);
151: }
153: static PetscErrorCode ElementDestroy(Stack *stack,StackElement e)
154: {
158: if (stack->use_dram) {
159: PetscMallocSetDRAM();
160: }
161: VecDestroy(&e->X);
162: if (stack->numY > 0 && !stack->solution_only) {
163: VecDestroyVecs(stack->numY,&e->Y);
164: }
165: PetscFree(e);
166: if (stack->use_dram) {
167: PetscMallocResetDRAM();
168: }
169: return(0);
170: }
172: static PetscErrorCode StackResize(Stack *stack,PetscInt newsize)
173: {
174: StackElement *newcontainer;
175: PetscInt i;
179: PetscMalloc1(newsize*sizeof(StackElement),&newcontainer);
180: for (i=0;i<stack->stacksize;i++) {
181: newcontainer[i] = stack->container[i];
182: }
183: PetscFree(stack->container);
184: stack->container = newcontainer;
185: stack->stacksize = newsize;
186: return(0);
187: }
189: static PetscErrorCode StackPush(Stack *stack,StackElement e)
190: {
192: if (stack->top+1 >= stack->stacksize) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MEMC,"Maximum stack size (%D) exceeded",stack->stacksize);
193: stack->container[++stack->top] = e;
194: return(0);
195: }
197: static PetscErrorCode StackPop(Stack *stack,StackElement *e)
198: {
200: *e = NULL;
201: if (stack->top == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEMC,"Empty stack");
202: *e = stack->container[stack->top--];
203: return(0);
204: }
206: static PetscErrorCode StackTop(Stack *stack,StackElement *e)
207: {
209: *e = stack->container[stack->top];
210: return(0);
211: }
213: static PetscErrorCode StackCreate(Stack *stack,PetscInt size,PetscInt ny)
214: {
218: stack->top = -1;
219: stack->numY = ny;
221: PetscMalloc1(size*sizeof(StackElement),&stack->container);
222: return(0);
223: }
225: static PetscErrorCode StackDestroy(Stack *stack)
226: {
227: PetscInt i,n;
228: StackElement e = NULL;
232: if (!stack->container) return(0);
233: if (stack->top > -1) {
234: n = stack->top+1; /* number of elements in the stack */
235: for (i=0;i<n;i++) {
236: StackPop(stack,&e);
237: ElementDestroy(stack,e);
238: }
239: }
240: PetscFree(stack->container);
241: return(0);
242: }
244: static PetscErrorCode StackFind(Stack *stack,StackElement *e,PetscInt index)
245: {
247: *e = NULL;
248: if (index < 0 || index > stack->top) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Invalid index %D",index);
249: *e = stack->container[index];
250: return(0);
251: }
253: static PetscErrorCode OutputBIN(MPI_Comm comm,const char *filename,PetscViewer *viewer)
254: {
258: PetscViewerCreate(comm,viewer);
259: PetscViewerSetType(*viewer,PETSCVIEWERBINARY);
260: PetscViewerPushFormat(*viewer,PETSC_VIEWER_NATIVE);
261: PetscViewerFileSetMode(*viewer,FILE_MODE_WRITE);
262: PetscViewerFileSetName(*viewer,filename);
263: return(0);
264: }
266: static PetscErrorCode WriteToDisk(PetscInt stepnum,PetscReal time,PetscReal timeprev,Vec X,Vec *Y,PetscInt numY,PetscBool solution_only,PetscViewer viewer)
267: {
268: PetscInt i;
272: PetscViewerBinaryWrite(viewer,&stepnum,1,PETSC_INT,PETSC_FALSE);
273: VecView(X,viewer);
274: for (i=0;!solution_only && i<numY;i++) {
275: VecView(Y[i],viewer);
276: }
277: PetscViewerBinaryWrite(viewer,&time,1,PETSC_REAL,PETSC_FALSE);
278: PetscViewerBinaryWrite(viewer,&timeprev,1,PETSC_REAL,PETSC_FALSE);
279: return(0);
280: }
282: static PetscErrorCode ReadFromDisk(PetscInt *stepnum,PetscReal *time,PetscReal *timeprev,Vec X,Vec *Y,PetscInt numY,PetscBool solution_only,PetscViewer viewer)
283: {
284: PetscInt i;
288: PetscViewerBinaryRead(viewer,stepnum,1,NULL,PETSC_INT);
289: VecLoad(X,viewer);
290: for (i=0;!solution_only && i<numY;i++) {
291: VecLoad(Y[i],viewer);
292: }
293: PetscViewerBinaryRead(viewer,time,1,NULL,PETSC_REAL);
294: PetscViewerBinaryRead(viewer,timeprev,1,NULL,PETSC_REAL);
295: return(0);
296: }
298: static PetscErrorCode StackDumpAll(TSTrajectory tj,TS ts,Stack *stack,PetscInt id)
299: {
300: Vec *Y;
301: PetscInt i;
302: StackElement e = NULL;
303: PetscViewer viewer;
304: char filename[PETSC_MAX_PATH_LEN];
306: MPI_Comm comm;
309: PetscObjectGetComm((PetscObject)ts,&comm);
310: if (tj->monitor) {
311: PetscViewerASCIIPushTab(tj->monitor);
312: PetscViewerASCIIPrintf(tj->monitor,"Dump stack id %D to file\n",id);
313: PetscViewerASCIIPopTab(tj->monitor);
314: }
315: if (id == 1) {
316: PetscMPIInt rank;
317: MPI_Comm_rank(comm,&rank);
318: if (!rank) {
319: PetscRMTree("SA-data");
320: PetscMkdir("SA-data");
321: }
322: }
323: PetscSNPrintf(filename,sizeof(filename),"SA-data/SA-STACK%06d.bin",id);
324: OutputBIN(comm,filename,&viewer);
325: for (i=0;i<stack->stacksize;i++) {
326: e = stack->container[i];
327: PetscLogEventBegin(TSTrajectory_DiskWrite,tj,ts,0,0);
328: WriteToDisk(e->stepnum,e->time,e->timeprev,e->X,e->Y,stack->numY,stack->solution_only,viewer);
329: PetscLogEventEnd(TSTrajectory_DiskWrite,tj,ts,0,0);
330: ts->trajectory->diskwrites++;
331: }
332: /* 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 */
333: TSGetStages(ts,&stack->numY,&Y);
334: PetscLogEventBegin(TSTrajectory_DiskWrite,tj,ts,0,0);
335: WriteToDisk(ts->steps,ts->ptime,ts->ptime_prev,ts->vec_sol,Y,stack->numY,stack->solution_only,viewer);
336: PetscLogEventEnd(TSTrajectory_DiskWrite,tj,ts,0,0);
337: ts->trajectory->diskwrites++;
338: for (i=0;i<stack->stacksize;i++) {
339: StackPop(stack,&e);
340: ElementDestroy(stack,e);
341: }
342: PetscViewerDestroy(&viewer);
343: return(0);
344: }
346: static PetscErrorCode StackLoadAll(TSTrajectory tj,TS ts,Stack *stack,PetscInt id)
347: {
348: Vec *Y;
349: PetscInt i;
350: StackElement e;
351: PetscViewer viewer;
352: char filename[PETSC_MAX_PATH_LEN];
356: if (tj->monitor) {
357: PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
358: PetscViewerASCIIPrintf(tj->monitor,"Load stack from file\n");
359: PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
360: }
361: PetscSNPrintf(filename,sizeof filename,"SA-data/SA-STACK%06d.bin",id);
362: PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
363: for (i=0;i<stack->stacksize;i++) {
364: ElementCreate(ts,stack,&e);
365: StackPush(stack,e);
366: PetscLogEventBegin(TSTrajectory_DiskRead,tj,ts,0,0);
367: ReadFromDisk(&e->stepnum,&e->time,&e->timeprev,e->X,e->Y,stack->numY,stack->solution_only,viewer);
368: PetscLogEventEnd(TSTrajectory_DiskRead,tj,ts,0,0);
369: ts->trajectory->diskreads++;
370: }
371: /* load the last step into TS */
372: TSGetStages(ts,&stack->numY,&Y);
373: PetscLogEventBegin(TSTrajectory_DiskRead,tj,ts,0,0);
374: ReadFromDisk(&ts->steps,&ts->ptime,&ts->ptime_prev,ts->vec_sol,Y,stack->numY,stack->solution_only,viewer);
375: PetscLogEventEnd(TSTrajectory_DiskRead,tj,ts,0,0);
376: ts->trajectory->diskreads++;
377: TurnBackward(ts);
378: PetscViewerDestroy(&viewer);
379: return(0);
380: }
382: #if defined(PETSC_HAVE_REVOLVE)
383: static PetscErrorCode StackLoadLast(TSTrajectory tj,TS ts,Stack *stack,PetscInt id)
384: {
385: Vec *Y;
386: PetscInt size;
387: PetscViewer viewer;
388: char filename[PETSC_MAX_PATH_LEN];
389: #if defined(PETSC_HAVE_MPIIO)
390: PetscBool usempiio;
391: #endif
392: int fd;
393: off_t off,offset;
397: if (tj->monitor) {
398: PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
399: PetscViewerASCIIPrintf(tj->monitor,"Load last stack element from file\n");
400: PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
401: }
402: TSGetStages(ts,&stack->numY,&Y);
403: VecGetSize(Y[0],&size);
404: /* VecView writes to file two extra int's for class id and number of rows */
405: 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;
407: PetscSNPrintf(filename,sizeof filename,"SA-data/SA-STACK%06d.bin",id);
408: PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
409: #if defined(PETSC_HAVE_MPIIO)
410: PetscViewerBinaryGetUseMPIIO(viewer,&usempiio);
411: if (usempiio) {
412: PetscViewerBinaryGetMPIIODescriptor(viewer,(MPI_File*)&fd);
413: PetscBinarySynchronizedSeek(PETSC_COMM_WORLD,fd,off,PETSC_BINARY_SEEK_END,&offset);
414: } else {
415: #endif
416: PetscViewerBinaryGetDescriptor(viewer,&fd);
417: PetscBinarySeek(fd,off,PETSC_BINARY_SEEK_END,&offset);
418: #if defined(PETSC_HAVE_MPIIO)
419: }
420: #endif
421: /* load the last step into TS */
422: PetscLogEventBegin(TSTrajectory_DiskRead,tj,ts,0,0);
423: ReadFromDisk(&ts->steps,&ts->ptime,&ts->ptime_prev,ts->vec_sol,Y,stack->numY,stack->solution_only,viewer);
424: PetscLogEventEnd(TSTrajectory_DiskRead,tj,ts,0,0);
425: ts->trajectory->diskreads++;
426: TurnBackward(ts);
427: PetscViewerDestroy(&viewer);
428: return(0);
429: }
430: #endif
432: static PetscErrorCode DumpSingle(TSTrajectory tj,TS ts,Stack *stack,PetscInt id)
433: {
434: Vec *Y;
435: PetscInt stepnum;
436: PetscViewer viewer;
437: char filename[PETSC_MAX_PATH_LEN];
439: MPI_Comm comm;
442: PetscObjectGetComm((PetscObject)ts,&comm);
443: if (tj->monitor) {
444: PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
445: PetscViewerASCIIPrintf(tj->monitor,"Dump a single point from file\n");
446: PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
447: }
448: TSGetStepNumber(ts,&stepnum);
449: if (id == 1) {
450: PetscMPIInt rank;
451: MPI_Comm_rank(comm,&rank);
452: if (!rank) {
453: PetscRMTree("SA-data");
454: PetscMkdir("SA-data");
455: }
456: }
457: PetscSNPrintf(filename,sizeof(filename),"SA-data/SA-CPS%06d.bin",id);
458: OutputBIN(comm,filename,&viewer);
460: TSGetStages(ts,&stack->numY,&Y);
461: PetscLogEventBegin(TSTrajectory_DiskWrite,tj,ts,0,0);
462: WriteToDisk(stepnum,ts->ptime,ts->ptime_prev,ts->vec_sol,Y,stack->numY,stack->solution_only,viewer);
463: PetscLogEventEnd(TSTrajectory_DiskWrite,tj,ts,0,0);
464: ts->trajectory->diskwrites++;
466: PetscViewerDestroy(&viewer);
467: return(0);
468: }
470: static PetscErrorCode LoadSingle(TSTrajectory tj,TS ts,Stack *stack,PetscInt id)
471: {
472: Vec *Y;
473: PetscViewer viewer;
474: char filename[PETSC_MAX_PATH_LEN];
478: if (tj->monitor) {
479: PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
480: PetscViewerASCIIPrintf(tj->monitor,"Load a single point from file\n");
481: PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
482: }
483: PetscSNPrintf(filename,sizeof filename,"SA-data/SA-CPS%06d.bin",id);
484: PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
486: TSGetStages(ts,&stack->numY,&Y);
487: PetscLogEventBegin(TSTrajectory_DiskRead,tj,ts,0,0);
488: ReadFromDisk(&ts->steps,&ts->ptime,&ts->ptime_prev,ts->vec_sol,Y,stack->numY,stack->solution_only,viewer);
489: PetscLogEventEnd(TSTrajectory_DiskRead,tj,ts,0,0);
490: ts->trajectory->diskreads++;
492: PetscViewerDestroy(&viewer);
493: return(0);
494: }
496: static PetscErrorCode UpdateTS(TS ts,Stack *stack,StackElement e, PetscBool adjoint_mode)
497: {
498: Vec *Y;
499: PetscInt i;
503: VecCopy(e->X,ts->vec_sol);
504: if (!stack->solution_only) {
505: TSGetStages(ts,&stack->numY,&Y);
506: for (i=0;i<stack->numY;i++) {
507: VecCopy(e->Y[i],Y[i]);
508: }
509: }
510: if (adjoint_mode) {
511: TSSetTimeStep(ts,e->timeprev-e->time); /* stepsize will be negative */
512: } else {
513: TSSetTimeStep(ts,e->time-e->timeprev); /* stepsize will be positive */
514: }
515: ts->ptime = e->time;
516: ts->ptime_prev = e->timeprev;
517: return(0);
518: }
520: static PetscErrorCode ReCompute(TS ts,TJScheduler *tjsch,PetscInt stepnumbegin,PetscInt stepnumend)
521: {
522: Stack *stack = &tjsch->stack;
523: PetscInt i;
527: TSSetStepNumber(ts,stepnumbegin);/* global step number */
528: for (i=stepnumbegin;i<stepnumend;i++) { /* assume fixed step size */
529: if (stack->solution_only && !tjsch->skip_trajectory) { /* revolve online need this */
530: /* don't use the public interface as it will update the TSHistory: this need a better fix */
531: TSTrajectorySet_Memory(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);
532: }
533: TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);
534: TSStep(ts);
535: if (!stack->solution_only && !tjsch->skip_trajectory) {
536: /* don't use the public interface as it will update the TSHistory: this need a better fix */
537: TSTrajectorySet_Memory(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);
538: }
539: TSEventHandler(ts);
540: if (!ts->steprollback) {
541: TSPostStep(ts);
542: }
543: }
544: TurnBackward(ts);
545: ts->trajectory->recomps += stepnumend-stepnumbegin; /* recomputation counter */
546: TSSetStepNumber(ts,stepnumend);
547: return(0);
548: }
550: static PetscErrorCode TopLevelStore(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscInt localstepnum,PetscInt laststridesize,PetscBool *done)
551: {
552: Stack *stack = &tjsch->stack;
553: DiskStack *diskstack = &tjsch->diskstack;
554: PetscInt stridenum;
558: *done = PETSC_FALSE;
559: stridenum = stepnum/tjsch->stride;
560: /* make sure saved checkpoint id starts from 1
561: skip last stride when using stridenum+1
562: skip first stride when using stridenum */
563: if (stack->solution_only) {
564: if (tjsch->save_stack) {
565: if (localstepnum == tjsch->stride-1 && stepnum < tjsch->total_steps-laststridesize) { /* current step will be saved without going through stack */
566: StackDumpAll(tj,ts,stack,stridenum+1);
567: if (tjsch->stype == TWO_LEVEL_TWO_REVOLVE) diskstack->container[++diskstack->top] = stridenum+1;
568: *done = PETSC_TRUE;
569: }
570: } else {
571: if (localstepnum == 0 && stepnum < tjsch->total_steps-laststridesize) {
572: DumpSingle(tj,ts,stack,stridenum+1);
573: if (tjsch->stype == TWO_LEVEL_TWO_REVOLVE) diskstack->container[++diskstack->top] = stridenum+1;
574: *done = PETSC_TRUE;
575: }
576: }
577: } else {
578: if (tjsch->save_stack) {
579: if (localstepnum == 0 && stepnum < tjsch->total_steps && stepnum != 0) { /* skip the first stride */
580: StackDumpAll(tj,ts,stack,stridenum);
581: if (tjsch->stype == TWO_LEVEL_TWO_REVOLVE) diskstack->container[++diskstack->top] = stridenum;
582: *done = PETSC_TRUE;
583: }
584: } else {
585: if (localstepnum == 1 && stepnum < tjsch->total_steps-laststridesize) {
586: DumpSingle(tj,ts,stack,stridenum+1);
587: if (tjsch->stype == TWO_LEVEL_TWO_REVOLVE) diskstack->container[++diskstack->top] = stridenum+1;
588: *done = PETSC_TRUE;
589: }
590: }
591: }
592: return(0);
593: }
595: static PetscErrorCode SetTrajN(TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscReal time,Vec X)
596: {
597: Stack *stack = &tjsch->stack;
598: StackElement e;
602: /* skip the last step */
603: if (ts->reason) { /* only affect the forward run */
604: /* update total_steps in the end of forward run */
605: if (stepnum != tjsch->total_steps) tjsch->total_steps = stepnum;
606: if (stack->solution_only) {
607: /* get rid of the solution at second last step */
608: StackPop(stack,&e);
609: ElementDestroy(stack,e);
610: }
611: return(0);
612: }
613: /* do not save trajectory at the recompute stage for solution_only mode */
614: if (stack->solution_only && tjsch->recompute) return(0);
615: /* skip the first step for no_solution_only mode */
616: if (!stack->solution_only && stepnum == 0) return(0);
618: /* resize the stack */
619: if (stack->top+1 == stack->stacksize) {
620: StackResize(stack,2*stack->stacksize);
621: }
622: /* update timenext for the previous step; necessary for step adaptivity */
623: if (stack->top > -1) {
624: StackTop(stack,&e);
625: e->timenext = ts->ptime;
626: }
627: if (stepnum < stack->top) {
628: SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_MEMC,"Illegal modification of a non-top stack element");
629: }
630: ElementCreate(ts,stack,&e);
631: ElementSet(ts,stack,&e,stepnum,time,X);
632: StackPush(stack,e);
633: return(0);
634: }
636: static PetscErrorCode SetTrajN_2(TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscReal time,Vec X)
637: {
638: Stack *stack = &tjsch->stack;
639: StackElement e;
643: if (stack->top+1 == stack->stacksize) {
644: StackResize(stack,2*stack->stacksize);
645: }
646: /* update timenext for the previous step; necessary for step adaptivity */
647: if (stack->top > -1) {
648: StackTop(stack,&e);
649: e->timenext = ts->ptime;
650: }
651: if (stepnum < stack->top) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_MEMC,"Illegal modification of a non-top stack element");
652: ElementCreate(ts,stack,&e);
653: ElementSet(ts,stack,&e,stepnum,time,X);
654: StackPush(stack,e);
655: return(0);
656: }
658: static PetscErrorCode GetTrajN(TS ts,TJScheduler *tjsch,PetscInt stepnum)
659: {
660: Stack *stack = &tjsch->stack;
661: StackElement e;
662: PetscInt ns;
666: if (stepnum == tjsch->total_steps) {
667: TurnBackward(ts);
668: return(0);
669: }
670: /* restore a checkpoint */
671: StackTop(stack,&e);
672: UpdateTS(ts,stack,e,PETSC_TRUE);
673: TSGetStages(ts,&ns,NULL);
674: if (stack->solution_only && ns) { /* recompute one step */
675: tjsch->recompute = PETSC_TRUE;
676: TurnForwardWithStepsize(ts,e->timenext-e->time);
677: ReCompute(ts,tjsch,e->stepnum,stepnum);
678: }
679: StackPop(stack,&e);
680: ElementDestroy(stack,e);
681: return(0);
682: }
684: static PetscErrorCode GetTrajN_2(TS ts,TJScheduler *tjsch,PetscInt stepnum)
685: {
686: Stack *stack = &tjsch->stack;
687: StackElement e = NULL;
691: StackFind(stack,&e,stepnum);
692: if (stepnum != e->stepnum) SETERRQ2(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Inconsistent steps! %D != %D",stepnum,e->stepnum);
693: UpdateTS(ts,stack,e,PETSC_FALSE);
694: return(0);
695: }
697: static PetscErrorCode SetTrajTLNR(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscReal time,Vec X)
698: {
699: Stack *stack = &tjsch->stack;
700: PetscInt localstepnum,laststridesize;
701: StackElement e;
702: PetscBool done;
706: if (!stack->solution_only && stepnum == 0) return(0);
707: if (stack->solution_only && stepnum == tjsch->total_steps) return(0);
708: if (tjsch->save_stack && tjsch->recompute) return(0);
710: localstepnum = stepnum%tjsch->stride;
711: /* (stridesize-1) checkpoints are saved in each stride; an extra point is added by StackDumpAll() */
712: laststridesize = tjsch->total_steps%tjsch->stride;
713: if (!laststridesize) laststridesize = tjsch->stride;
715: if (!tjsch->recompute) {
716: TopLevelStore(tj,ts,tjsch,stepnum,localstepnum,laststridesize,&done);
717: if (!tjsch->save_stack && stepnum < tjsch->total_steps-laststridesize) return(0);
718: }
719: if (!stack->solution_only && localstepnum == 0) return(0); /* skip last point in each stride at recompute stage or last stride */
720: if (stack->solution_only && localstepnum == tjsch->stride-1) return(0); /* skip last step in each stride at recompute stage or last stride */
722: ElementCreate(ts,stack,&e);
723: ElementSet(ts,stack,&e,stepnum,time,X);
724: StackPush(stack,e);
725: return(0);
726: }
728: static PetscErrorCode GetTrajTLNR(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum)
729: {
730: Stack *stack = &tjsch->stack;
731: PetscInt id,localstepnum,laststridesize;
732: StackElement e;
736: if (stepnum == tjsch->total_steps) {
737: TurnBackward(ts);
738: return(0);
739: }
741: localstepnum = stepnum%tjsch->stride;
742: laststridesize = tjsch->total_steps%tjsch->stride;
743: if (!laststridesize) laststridesize = tjsch->stride;
744: if (stack->solution_only) {
745: /* fill stack with info */
746: if (localstepnum == 0 && tjsch->total_steps-stepnum >= laststridesize) {
747: id = stepnum/tjsch->stride;
748: if (tjsch->save_stack) {
749: StackLoadAll(tj,ts,stack,id);
750: tjsch->recompute = PETSC_TRUE;
751: tjsch->skip_trajectory = PETSC_TRUE;
752: TurnForward(ts);
753: ReCompute(ts,tjsch,id*tjsch->stride-1,id*tjsch->stride);
754: tjsch->skip_trajectory = PETSC_FALSE;
755: } else {
756: LoadSingle(tj,ts,stack,id);
757: tjsch->recompute = PETSC_TRUE;
758: TurnForward(ts);
759: ReCompute(ts,tjsch,(id-1)*tjsch->stride,id*tjsch->stride);
760: }
761: return(0);
762: }
763: /* restore a checkpoint */
764: StackPop(stack,&e);
765: UpdateTS(ts,stack,e,PETSC_TRUE);
766: tjsch->recompute = PETSC_TRUE;
767: tjsch->skip_trajectory = PETSC_TRUE;
768: TurnForward(ts);
769: ReCompute(ts,tjsch,e->stepnum,stepnum);
770: tjsch->skip_trajectory = PETSC_FALSE;
771: ElementDestroy(stack,e);
772: } else {
773: /* fill stack with info */
774: if (localstepnum == 0 && tjsch->total_steps-stepnum >= laststridesize) {
775: id = stepnum/tjsch->stride;
776: if (tjsch->save_stack) {
777: StackLoadAll(tj,ts,stack,id);
778: } else {
779: LoadSingle(tj,ts,stack,id);
780: ElementCreate(ts,stack,&e);
781: ElementSet(ts,stack,&e,(id-1)*tjsch->stride+1,ts->ptime,ts->vec_sol);
782: StackPush(stack,e);
783: tjsch->recompute = PETSC_TRUE;
784: TurnForward(ts);
785: ReCompute(ts,tjsch,e->stepnum,id*tjsch->stride);
786: }
787: return(0);
788: }
789: /* restore a checkpoint */
790: StackPop(stack,&e);
791: UpdateTS(ts,stack,e,PETSC_TRUE);
792: ElementDestroy(stack,e);
793: }
794: return(0);
795: }
797: #if defined(PETSC_HAVE_REVOLVE)
798: static PetscErrorCode printwhattodo(PetscViewer viewer,PetscInt whattodo,RevolveCTX *rctx,PetscInt shift)
799: {
803: if (!viewer) return(0);
805: switch(whattodo) {
806: case 1:
807: PetscViewerASCIIPrintf(viewer,"Advance from %D to %D\n",rctx->oldcapo+shift,rctx->capo+shift);
808: break;
809: case 2:
810: PetscViewerASCIIPrintf(viewer,"Store in checkpoint number %D (located in RAM)\n",rctx->check);
811: break;
812: case 3:
813: PetscViewerASCIIPrintf(viewer,"First turn: Initialize adjoints and reverse first step\n");
814: break;
815: case 4:
816: PetscViewerASCIIPrintf(viewer,"Forward and reverse one step\n");
817: break;
818: case 5:
819: PetscViewerASCIIPrintf(viewer,"Restore in checkpoint number %D (located in RAM)\n",rctx->check);
820: break;
821: case 7:
822: PetscViewerASCIIPrintf(viewer,"Store in checkpoint number %D (located on disk)\n",rctx->check);
823: break;
824: case 8:
825: PetscViewerASCIIPrintf(viewer,"Restore in checkpoint number %D (located on disk)\n",rctx->check);
826: break;
827: case -1:
828: PetscViewerASCIIPrintf(viewer,"Error!");
829: break;
830: }
831: return(0);
832: }
834: static PetscErrorCode printwhattodo2(PetscViewer viewer,PetscInt whattodo,RevolveCTX *rctx,PetscInt shift)
835: {
839: if (!viewer) return(0);
841: switch(whattodo) {
842: case 1:
843: PetscViewerASCIIPrintf(viewer,"[Top Level] Advance from stride %D to stride %D\n",rctx->oldcapo+shift,rctx->capo+shift);
844: break;
845: case 2:
846: PetscViewerASCIIPrintf(viewer,"[Top Level] Store in checkpoint number %D\n",rctx->check);
847: break;
848: case 3:
849: PetscViewerASCIIPrintf(viewer,"[Top Level] First turn: Initialize adjoints and reverse first stride\n");
850: break;
851: case 4:
852: PetscViewerASCIIPrintf(viewer,"[Top Level] Forward and reverse one stride\n");
853: break;
854: case 5:
855: PetscViewerASCIIPrintf(viewer,"[Top Level] Restore in checkpoint number %D\n",rctx->check);
856: break;
857: case 7:
858: PetscViewerASCIIPrintf(viewer,"[Top Level] Store in top-level checkpoint number %D\n",rctx->check);
859: break;
860: case 8:
861: PetscViewerASCIIPrintf(viewer,"[Top Level] Restore in top-level checkpoint number %D\n",rctx->check);
862: break;
863: case -1:
864: PetscViewerASCIIPrintf(viewer,"[Top Level] Error!");
865: break;
866: }
867: return(0);
868: }
870: static PetscErrorCode InitRevolve(PetscInt fine,PetscInt snaps,RevolveCTX *rctx)
871: {
873: revolve_reset();
874: revolve_create_offline(fine,snaps);
875: rctx->snaps_in = snaps;
876: rctx->fine = fine;
877: rctx->check = 0;
878: rctx->capo = 0;
879: rctx->reverseonestep = PETSC_FALSE;
880: /* check stepsleft? */
881: return(0);
882: }
884: static PetscErrorCode FastForwardRevolve(RevolveCTX *rctx)
885: {
886: PetscInt whattodo;
889: whattodo = 0;
890: while(whattodo!=3) { /* we have to fast forward revolve to the beginning of the backward sweep due to unfriendly revolve interface */
891: whattodo = revolve_action(&rctx->check,&rctx->capo,&rctx->fine,rctx->snaps_in,&rctx->info,&rctx->where);
892: }
893: return(0);
894: }
896: static PetscErrorCode ApplyRevolve(PetscViewer viewer,SchedulerType stype,RevolveCTX *rctx,PetscInt total_steps,PetscInt stepnum,PetscInt localstepnum,PetscBool toplevel,PetscInt *store)
897: {
899: PetscInt shift,whattodo;
902: *store = 0;
903: if (rctx->stepsleft > 0) { /* advance the solution without checkpointing anything as Revolve requires */
904: rctx->stepsleft--;
905: return(0);
906: }
907: /* let Revolve determine what to do next */
908: shift = stepnum-localstepnum;
909: rctx->oldcapo = rctx->capo;
910: rctx->capo = localstepnum;
912: if (!toplevel) whattodo = revolve_action(&rctx->check,&rctx->capo,&rctx->fine,rctx->snaps_in,&rctx->info,&rctx->where);
913: else whattodo = revolve2_action(&rctx->check,&rctx->capo,&rctx->fine,rctx->snaps_in,&rctx->info,&rctx->where);
914: if (stype == REVOLVE_ONLINE && whattodo == 8) whattodo = 5;
915: if (stype == REVOLVE_ONLINE && whattodo == 7) whattodo = 2;
916: if (!toplevel) {printwhattodo(viewer,whattodo,rctx,shift);}
917: else {printwhattodo2(viewer,whattodo,rctx,shift);}
918: if (whattodo == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in the Revolve library");
919: if (whattodo == 1) { /* advance some time steps */
920: if (stype == REVOLVE_ONLINE && rctx->capo >= total_steps-1) {
921: revolve_turn(total_steps,&rctx->capo,&rctx->fine);
922: if (!toplevel) {printwhattodo(viewer,whattodo,rctx,shift);}
923: else {printwhattodo2(viewer,whattodo,rctx,shift);}
924: }
925: rctx->stepsleft = rctx->capo-rctx->oldcapo-1;
926: }
927: if (whattodo == 3 || whattodo == 4) { /* ready for a reverse step */
928: rctx->reverseonestep = PETSC_TRUE;
929: }
930: if (whattodo == 5) { /* restore a checkpoint and ask Revolve what to do next */
931: rctx->oldcapo = rctx->capo;
932: 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*/
933: else whattodo = revolve2_action(&rctx->check,&rctx->capo,&rctx->fine,rctx->snaps_in,&rctx->info,&rctx->where);
934: if (!toplevel) {printwhattodo(viewer,whattodo,rctx,shift);}
935: else {printwhattodo2(viewer,whattodo,rctx,shift);}
936: if (whattodo == 3 || whattodo == 4) rctx->reverseonestep = PETSC_TRUE;
937: if (whattodo == 1) rctx->stepsleft = rctx->capo-rctx->oldcapo;
938: }
939: if (whattodo == 7) { /* save the checkpoint to disk */
940: *store = 2;
941: rctx->oldcapo = rctx->capo;
942: whattodo = revolve_action(&rctx->check,&rctx->capo,&rctx->fine,rctx->snaps_in,&rctx->info,&rctx->where); /* must return 1 */
943: printwhattodo(viewer,whattodo,rctx,shift);
944: rctx->stepsleft = rctx->capo-rctx->oldcapo-1;
945: }
946: if (whattodo == 2) { /* store a checkpoint to RAM and ask Revolve how many time steps to advance next */
947: *store = 1;
948: rctx->oldcapo = rctx->capo;
949: if (!toplevel) whattodo = revolve_action(&rctx->check,&rctx->capo,&rctx->fine,rctx->snaps_in,&rctx->info,&rctx->where); /* must return 1 */
950: else whattodo = revolve2_action(&rctx->check,&rctx->capo,&rctx->fine,rctx->snaps_in,&rctx->info,&rctx->where);
951: if (!toplevel) {printwhattodo(viewer,whattodo,rctx,shift);}
952: else {printwhattodo2(viewer,whattodo,rctx,shift);}
953: if (stype == REVOLVE_ONLINE && rctx->capo >= total_steps-1) {
954: revolve_turn(total_steps,&rctx->capo,&rctx->fine);
955: printwhattodo(viewer,whattodo,rctx,shift);
956: }
957: rctx->stepsleft = rctx->capo-rctx->oldcapo-1;
958: }
959: return(0);
960: }
962: static PetscErrorCode SetTrajROF(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscReal time,Vec X)
963: {
964: Stack *stack = &tjsch->stack;
965: PetscInt store;
966: StackElement e;
970: if (!stack->solution_only && stepnum == 0) return(0);
971: if (stack->solution_only && stepnum == tjsch->total_steps) return(0);
972: ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,tjsch->total_steps,stepnum,stepnum,PETSC_FALSE,&store);
973: if (store == 1) {
974: if (stepnum < stack->top) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_MEMC,"Illegal modification of a non-top stack element");
975: ElementCreate(ts,stack,&e);
976: ElementSet(ts,stack,&e,stepnum,time,X);
977: StackPush(stack,e);
978: }
979: return(0);
980: }
982: static PetscErrorCode GetTrajROF(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum)
983: {
984: Stack *stack = &tjsch->stack;
985: PetscInt whattodo,shift,store;
986: StackElement e;
990: if (stepnum == 0 || stepnum == tjsch->total_steps) {
991: TurnBackward(ts);
992: tjsch->rctx->reverseonestep = PETSC_FALSE;
993: return(0);
994: }
995: /* restore a checkpoint */
996: StackTop(stack,&e);
997: UpdateTS(ts,stack,e,PETSC_TRUE);
998: if (stack->solution_only) { /* start with restoring a checkpoint */
999: tjsch->rctx->capo = stepnum;
1000: tjsch->rctx->oldcapo = tjsch->rctx->capo;
1001: shift = 0;
1002: whattodo = revolve_action(&tjsch->rctx->check,&tjsch->rctx->capo,&tjsch->rctx->fine,tjsch->rctx->snaps_in,&tjsch->rctx->info,&tjsch->rctx->where);
1003: printwhattodo(tj->monitor,whattodo,tjsch->rctx,shift);
1004: } else { /* 2 revolve actions: restore a checkpoint and then advance */
1005: ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,tjsch->total_steps,stepnum,stepnum,PETSC_FALSE,&store);
1006: if (tj->monitor) {
1007: PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
1008: PetscViewerASCIIPrintf(tj->monitor,"Skip the step from %D to %D (stage values already checkpointed)\n",tjsch->rctx->oldcapo,tjsch->rctx->oldcapo+1);
1009: PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
1010: }
1011: if (!tjsch->rctx->reverseonestep && tjsch->rctx->stepsleft > 0) tjsch->rctx->stepsleft--;
1012: }
1013: if (stack->solution_only || (!stack->solution_only && e->stepnum < stepnum)) {
1014: tjsch->recompute = PETSC_TRUE;
1015: TurnForward(ts);
1016: ReCompute(ts,tjsch,e->stepnum,stepnum);
1017: }
1018: if ((stack->solution_only && e->stepnum+1 == stepnum) || (!stack->solution_only && e->stepnum == stepnum)) {
1019: StackPop(stack,&e);
1020: ElementDestroy(stack,e);
1021: }
1022: tjsch->rctx->reverseonestep = PETSC_FALSE;
1023: return(0);
1024: }
1026: static PetscErrorCode SetTrajRON(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscReal time,Vec X)
1027: {
1028: Stack *stack = &tjsch->stack;
1029: Vec *Y;
1030: PetscInt i,store;
1031: PetscReal timeprev;
1032: StackElement e;
1033: RevolveCTX *rctx = tjsch->rctx;
1037: if (!stack->solution_only && stepnum == 0) return(0);
1038: if (stack->solution_only && stepnum == tjsch->total_steps) return(0);
1039: ApplyRevolve(tj->monitor,tjsch->stype,rctx,tjsch->total_steps,stepnum,stepnum,PETSC_FALSE,&store);
1040: if (store == 1) {
1041: if (rctx->check != stack->top+1) { /* overwrite some non-top checkpoint in the stack */
1042: StackFind(stack,&e,rctx->check);
1043: VecCopy(X,e->X);
1044: if (stack->numY > 0 && !stack->solution_only) {
1045: TSGetStages(ts,&stack->numY,&Y);
1046: for (i=0;i<stack->numY;i++) {
1047: VecCopy(Y[i],e->Y[i]);
1048: }
1049: }
1050: e->stepnum = stepnum;
1051: e->time = time;
1052: TSGetPrevTime(ts,&timeprev);
1053: e->timeprev = timeprev;
1054: } else {
1055: if (stepnum < stack->top) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_MEMC,"Illegal modification of a non-top stack element");
1056: ElementCreate(ts,stack,&e);
1057: ElementSet(ts,stack,&e,stepnum,time,X);
1058: StackPush(stack,e);
1059: }
1060: }
1061: return(0);
1062: }
1064: static PetscErrorCode GetTrajRON(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum)
1065: {
1066: Stack *stack = &tjsch->stack;
1067: PetscInt whattodo,shift;
1068: StackElement e;
1072: if (stepnum == 0 || stepnum == tjsch->total_steps) {
1073: TurnBackward(ts);
1074: tjsch->rctx->reverseonestep = PETSC_FALSE;
1075: return(0);
1076: }
1077: tjsch->rctx->capo = stepnum;
1078: tjsch->rctx->oldcapo = tjsch->rctx->capo;
1079: shift = 0;
1080: whattodo = revolve_action(&tjsch->rctx->check,&tjsch->rctx->capo,&tjsch->rctx->fine,tjsch->rctx->snaps_in,&tjsch->rctx->info,&tjsch->rctx->where); /* whattodo=restore */
1081: if (whattodo == 8) whattodo = 5;
1082: printwhattodo(tj->monitor,whattodo,tjsch->rctx,shift);
1083: /* restore a checkpoint */
1084: StackFind(stack,&e,tjsch->rctx->check);
1085: UpdateTS(ts,stack,e,PETSC_TRUE);
1086: if (!stack->solution_only) { /* whattodo must be 5 */
1087: /* ask Revolve what to do next */
1088: tjsch->rctx->oldcapo = tjsch->rctx->capo;
1089: 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*/
1090: printwhattodo(tj->monitor,whattodo,tjsch->rctx,shift);
1091: if (whattodo == 3 || whattodo == 4) tjsch->rctx->reverseonestep = PETSC_TRUE;
1092: if (whattodo == 1) tjsch->rctx->stepsleft = tjsch->rctx->capo-tjsch->rctx->oldcapo;
1093: if (tj->monitor) {
1094: PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
1095: PetscViewerASCIIPrintf(tj->monitor,"Skip the step from %D to %D (stage values already checkpointed)\n",tjsch->rctx->oldcapo,tjsch->rctx->oldcapo+1);
1096: PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
1097: }
1098: if (!tjsch->rctx->reverseonestep && tjsch->rctx->stepsleft > 0) tjsch->rctx->stepsleft--;
1099: }
1100: if (stack->solution_only || (!stack->solution_only && e->stepnum < stepnum)) {
1101: tjsch->recompute = PETSC_TRUE;
1102: TurnForward(ts);
1103: ReCompute(ts,tjsch,e->stepnum,stepnum);
1104: }
1105: tjsch->rctx->reverseonestep = PETSC_FALSE;
1106: return(0);
1107: }
1109: static PetscErrorCode SetTrajTLR(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscReal time,Vec X)
1110: {
1111: Stack *stack = &tjsch->stack;
1112: PetscInt store,localstepnum,laststridesize;
1113: StackElement e;
1114: PetscBool done = PETSC_FALSE;
1118: if (!stack->solution_only && stepnum == 0) return(0);
1119: if (stack->solution_only && stepnum == tjsch->total_steps) return(0);
1121: localstepnum = stepnum%tjsch->stride;
1122: laststridesize = tjsch->total_steps%tjsch->stride;
1123: if (!laststridesize) laststridesize = tjsch->stride;
1125: if (!tjsch->recompute) {
1126: TopLevelStore(tj,ts,tjsch,stepnum,localstepnum,laststridesize,&done);
1127: /* revolve is needed for the last stride; different starting points for last stride between solutin_only and !solutin_only */
1128: if (!stack->solution_only && !tjsch->save_stack && stepnum <= tjsch->total_steps-laststridesize) return(0);
1129: if (stack->solution_only && !tjsch->save_stack && stepnum < tjsch->total_steps-laststridesize) return(0);
1130: }
1131: if (tjsch->save_stack && done) {
1132: InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1133: return(0);
1134: }
1135: if (laststridesize < tjsch->stride) {
1136: 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 */
1137: InitRevolve(laststridesize,tjsch->max_cps_ram,tjsch->rctx);
1138: }
1139: 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 */
1140: InitRevolve(laststridesize,tjsch->max_cps_ram,tjsch->rctx);
1141: }
1142: }
1143: ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,tjsch->total_steps,stepnum,localstepnum,PETSC_FALSE,&store);
1144: if (store == 1) {
1145: if (localstepnum < stack->top) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_MEMC,"Illegal modification of a non-top stack element");
1146: ElementCreate(ts,stack,&e);
1147: ElementSet(ts,stack,&e,stepnum,time,X);
1148: StackPush(stack,e);
1149: }
1150: return(0);
1151: }
1153: static PetscErrorCode GetTrajTLR(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum)
1154: {
1155: Stack *stack = &tjsch->stack;
1156: PetscInt whattodo,shift;
1157: PetscInt localstepnum,stridenum,laststridesize,store;
1158: StackElement e;
1162: localstepnum = stepnum%tjsch->stride;
1163: stridenum = stepnum/tjsch->stride;
1164: if (stepnum == tjsch->total_steps) {
1165: TurnBackward(ts);
1166: tjsch->rctx->reverseonestep = PETSC_FALSE;
1167: return(0);
1168: }
1169: laststridesize = tjsch->total_steps%tjsch->stride;
1170: if (!laststridesize) laststridesize = tjsch->stride;
1171: if (stack->solution_only) {
1172: /* fill stack */
1173: if (localstepnum == 0 && stepnum <= tjsch->total_steps-laststridesize) {
1174: if (tjsch->save_stack) {
1175: StackLoadAll(tj,ts,stack,stridenum);
1176: InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1177: FastForwardRevolve(tjsch->rctx);
1178: tjsch->recompute = PETSC_TRUE;
1179: tjsch->skip_trajectory = PETSC_TRUE;
1180: TurnForward(ts);
1181: ReCompute(ts,tjsch,stridenum*tjsch->stride-1,stridenum*tjsch->stride);
1182: tjsch->skip_trajectory = PETSC_FALSE;
1183: } else {
1184: LoadSingle(tj,ts,stack,stridenum);
1185: InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1186: tjsch->recompute = PETSC_TRUE;
1187: TurnForward(ts);
1188: ReCompute(ts,tjsch,(stridenum-1)*tjsch->stride,stridenum*tjsch->stride);
1189: }
1190: return(0);
1191: }
1192: /* restore a checkpoint */
1193: StackTop(stack,&e);
1194: UpdateTS(ts,stack,e,PETSC_TRUE);
1195: /* start with restoring a checkpoint */
1196: tjsch->rctx->capo = stepnum;
1197: tjsch->rctx->oldcapo = tjsch->rctx->capo;
1198: shift = stepnum-localstepnum;
1199: whattodo = revolve_action(&tjsch->rctx->check,&tjsch->rctx->capo,&tjsch->rctx->fine,tjsch->rctx->snaps_in,&tjsch->rctx->info,&tjsch->rctx->where);
1200: printwhattodo(tj->monitor,whattodo,tjsch->rctx,shift);
1201: tjsch->recompute = PETSC_TRUE;
1202: TurnForward(ts);
1203: ReCompute(ts,tjsch,e->stepnum,stepnum);
1204: if (e->stepnum+1 == stepnum) {
1205: StackPop(stack,&e);
1206: ElementDestroy(stack,e);
1207: }
1208: } else {
1209: /* fill stack with info */
1210: if (localstepnum == 0 && tjsch->total_steps-stepnum >= laststridesize) {
1211: if (tjsch->save_stack) {
1212: StackLoadAll(tj,ts,stack,stridenum);
1213: InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1214: FastForwardRevolve(tjsch->rctx);
1215: } else {
1216: LoadSingle(tj,ts,stack,stridenum);
1217: InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1218: ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,tjsch->total_steps,(stridenum-1)*tjsch->stride+1,1,PETSC_FALSE,&store);
1219: if (tj->monitor) {
1220: PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
1221: PetscViewerASCIIPrintf(tj->monitor,"Skip the step from %D to %D (stage values already checkpointed)\n",(stridenum-1)*tjsch->stride+tjsch->rctx->oldcapo,(stridenum-1)*tjsch->stride+tjsch->rctx->oldcapo+1);
1222: PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
1223: }
1224: ElementCreate(ts,stack,&e);
1225: ElementSet(ts,stack,&e,(stridenum-1)*tjsch->stride+1,ts->ptime,ts->vec_sol);
1226: StackPush(stack,e);
1227: tjsch->recompute = PETSC_TRUE;
1228: TurnForward(ts);
1229: ReCompute(ts,tjsch,e->stepnum,stridenum*tjsch->stride);
1230: }
1231: return(0);
1232: }
1233: /* restore a checkpoint */
1234: StackTop(stack,&e);
1235: UpdateTS(ts,stack,e,PETSC_TRUE);
1236: /* 2 revolve actions: restore a checkpoint and then advance */
1237: ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,tjsch->total_steps,stepnum,localstepnum,PETSC_FALSE,&store);
1238: if (tj->monitor) {
1239: PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
1240: PetscViewerASCIIPrintf(tj->monitor,"Skip the step from %D to %D (stage values already checkpointed)\n",stepnum-localstepnum+tjsch->rctx->oldcapo,stepnum-localstepnum+tjsch->rctx->oldcapo+1);
1241: PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
1242: }
1243: if (!tjsch->rctx->reverseonestep && tjsch->rctx->stepsleft > 0) tjsch->rctx->stepsleft--;
1244: if (e->stepnum < stepnum) {
1245: tjsch->recompute = PETSC_TRUE;
1246: TurnForward(ts);
1247: ReCompute(ts,tjsch,e->stepnum,stepnum);
1248: }
1249: if (e->stepnum == stepnum) {
1250: StackPop(stack,&e);
1251: ElementDestroy(stack,e);
1252: }
1253: }
1254: tjsch->rctx->reverseonestep = PETSC_FALSE;
1255: return(0);
1256: }
1258: static PetscErrorCode SetTrajTLTR(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscReal time,Vec X)
1259: {
1260: Stack *stack = &tjsch->stack;
1261: PetscInt store,localstepnum,stridenum,laststridesize;
1262: StackElement e;
1263: PetscBool done = PETSC_FALSE;
1267: if (!stack->solution_only && stepnum == 0) return(0);
1268: if (stack->solution_only && stepnum == tjsch->total_steps) return(0);
1270: localstepnum = stepnum%tjsch->stride; /* index at the bottom level (inside a stride) */
1271: stridenum = stepnum/tjsch->stride; /* index at the top level */
1272: laststridesize = tjsch->total_steps%tjsch->stride;
1273: if (!laststridesize) laststridesize = tjsch->stride;
1274: if (stack->solution_only && localstepnum == 0 && !tjsch->rctx2->reverseonestep) {
1275: ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx2,(tjsch->total_steps+tjsch->stride-1)/tjsch->stride,stridenum,stridenum,PETSC_TRUE,&tjsch->store_stride);
1276: if (laststridesize < tjsch->stride && stepnum == tjsch->total_steps-laststridesize) {
1277: InitRevolve(laststridesize,tjsch->max_cps_ram,tjsch->rctx);
1278: }
1279: }
1280: if (!stack->solution_only && localstepnum == 1 && !tjsch->rctx2->reverseonestep) {
1281: ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx2,(tjsch->total_steps+tjsch->stride-1)/tjsch->stride,stridenum,stridenum,PETSC_TRUE,&tjsch->store_stride);
1282: if (laststridesize < tjsch->stride && stepnum == tjsch->total_steps-laststridesize+1) {
1283: InitRevolve(laststridesize,tjsch->max_cps_ram,tjsch->rctx);
1284: }
1285: }
1286: if (tjsch->store_stride) {
1287: TopLevelStore(tj,ts,tjsch,stepnum,localstepnum,laststridesize,&done);
1288: if (done) {
1289: InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1290: return(0);
1291: }
1292: }
1293: if (stepnum < tjsch->total_steps-laststridesize) {
1294: if (tjsch->save_stack && !tjsch->store_stride && !tjsch->rctx2->reverseonestep) return(0); /* store or forward-and-reverse at top level trigger revolve at bottom level */
1295: if (!tjsch->save_stack && !tjsch->rctx2->reverseonestep) return(0); /* store operation does not require revolve be called at bottom level */
1296: }
1297: /* 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 */
1298: if (!stack->solution_only && localstepnum == 0 && stepnum != tjsch->total_steps && !tjsch->recompute) return(0);
1299: ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,tjsch->total_steps,stepnum,localstepnum,PETSC_FALSE,&store);
1300: if (store == 1) {
1301: if (localstepnum < stack->top) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_MEMC,"Illegal modification of a non-top stack element");
1302: ElementCreate(ts,stack,&e);
1303: ElementSet(ts,stack,&e,stepnum,time,X);
1304: StackPush(stack,e);
1305: }
1306: return(0);
1307: }
1309: static PetscErrorCode GetTrajTLTR(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum)
1310: {
1311: Stack *stack = &tjsch->stack;
1312: DiskStack *diskstack = &tjsch->diskstack;
1313: PetscInt whattodo,shift;
1314: PetscInt localstepnum,stridenum,restoredstridenum,laststridesize,store;
1315: StackElement e;
1319: localstepnum = stepnum%tjsch->stride;
1320: stridenum = stepnum/tjsch->stride;
1321: if (stepnum == tjsch->total_steps) {
1322: TurnBackward(ts);
1323: tjsch->rctx->reverseonestep = PETSC_FALSE;
1324: return(0);
1325: }
1326: laststridesize = tjsch->total_steps%tjsch->stride;
1327: if (!laststridesize) laststridesize = tjsch->stride;
1328: /*
1329: 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:
1330: Case 1 (save_stack)
1331: Restore a disk checkpoint; update TS with the last element in the restored data; recompute to the current point.
1332: Case 2 (!save_stack)
1333: Restore a disk checkpoint; update TS with the restored point; recompute to the current point.
1334: */
1335: if (localstepnum == 0 && stepnum <= tjsch->total_steps-laststridesize) {
1336: /* restore the top element in the stack for disk checkpoints */
1337: restoredstridenum = diskstack->container[diskstack->top];
1338: tjsch->rctx2->reverseonestep = PETSC_FALSE;
1339: /* top-level revolve must be applied before current step, just like the solution_only mode for single-level revolve */
1340: if (!tjsch->save_stack && stack->solution_only) { /* start with restoring a checkpoint */
1341: tjsch->rctx2->capo = stridenum;
1342: tjsch->rctx2->oldcapo = tjsch->rctx2->capo;
1343: shift = 0;
1344: whattodo = revolve2_action(&tjsch->rctx2->check,&tjsch->rctx2->capo,&tjsch->rctx2->fine,tjsch->rctx2->snaps_in,&tjsch->rctx2->info,&tjsch->rctx2->where);
1345: printwhattodo2(tj->monitor,whattodo,tjsch->rctx2,shift);
1346: } else { /* 2 revolve actions: restore a checkpoint and then advance */
1347: ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx2,(tjsch->total_steps+tjsch->stride-1)/tjsch->stride,stridenum,stridenum,PETSC_TRUE,&tjsch->store_stride);
1348: if (tj->monitor) {
1349: PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
1350: PetscViewerASCIIPrintf(tj->monitor,"[Top Level] Skip the stride from %D to %D (stage values already checkpointed)\n",tjsch->rctx2->oldcapo,tjsch->rctx2->oldcapo+1);
1351: PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
1352: }
1353: if (!tjsch->rctx2->reverseonestep && tjsch->rctx2->stepsleft > 0) tjsch->rctx2->stepsleft--;
1354: }
1355: /* fill stack */
1356: if (stack->solution_only) {
1357: if (tjsch->save_stack) {
1358: if (restoredstridenum < stridenum) {
1359: StackLoadLast(tj,ts,stack,restoredstridenum);
1360: } else {
1361: StackLoadAll(tj,ts,stack,restoredstridenum);
1362: }
1363: /* recompute one step ahead */
1364: tjsch->recompute = PETSC_TRUE;
1365: tjsch->skip_trajectory = PETSC_TRUE;
1366: TurnForward(ts);
1367: ReCompute(ts,tjsch,stridenum*tjsch->stride-1,stridenum*tjsch->stride);
1368: tjsch->skip_trajectory = PETSC_FALSE;
1369: if (restoredstridenum < stridenum) {
1370: InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1371: tjsch->recompute = PETSC_TRUE;
1372: TurnForward(ts);
1373: ReCompute(ts,tjsch,restoredstridenum*tjsch->stride,stepnum);
1374: } else { /* stack ready, fast forward revolve status */
1375: InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1376: FastForwardRevolve(tjsch->rctx);
1377: }
1378: } else {
1379: LoadSingle(tj,ts,stack,restoredstridenum);
1380: InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1381: tjsch->recompute = PETSC_TRUE;
1382: TurnForward(ts);
1383: ReCompute(ts,tjsch,(restoredstridenum-1)*tjsch->stride,stepnum);
1384: }
1385: } else {
1386: if (tjsch->save_stack) {
1387: if (restoredstridenum < stridenum) {
1388: StackLoadLast(tj,ts,stack,restoredstridenum);
1389: /* reset revolve */
1390: InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1391: tjsch->recompute = PETSC_TRUE;
1392: TurnForward(ts);
1393: ReCompute(ts,tjsch,restoredstridenum*tjsch->stride,stepnum);
1394: } else { /* stack ready, fast forward revolve status */
1395: StackLoadAll(tj,ts,stack,restoredstridenum);
1396: InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1397: FastForwardRevolve(tjsch->rctx);
1398: }
1399: } else {
1400: LoadSingle(tj,ts,stack,restoredstridenum);
1401: InitRevolve(tjsch->stride,tjsch->max_cps_ram,tjsch->rctx);
1402: /* push first element to stack */
1403: if (tjsch->store_stride || tjsch->rctx2->reverseonestep) {
1404: shift = (restoredstridenum-1)*tjsch->stride-localstepnum;
1405: ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,tjsch->total_steps,(restoredstridenum-1)*tjsch->stride+1,1,PETSC_FALSE,&store);
1406: if (tj->monitor) {
1407: PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
1408: PetscViewerASCIIPrintf(tj->monitor,"Skip the step from %D to %D (stage values already checkpointed)\n",(restoredstridenum-1)*tjsch->stride,(restoredstridenum-1)*tjsch->stride+1);
1409: PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
1410: }
1411: ElementCreate(ts,stack,&e);
1412: ElementSet(ts,stack,&e,(restoredstridenum-1)*tjsch->stride+1,ts->ptime,ts->vec_sol);
1413: StackPush(stack,e);
1414: }
1415: tjsch->recompute = PETSC_TRUE;
1416: TurnForward(ts);
1417: ReCompute(ts,tjsch,(restoredstridenum-1)*tjsch->stride+1,stepnum);
1418: }
1419: }
1420: if (restoredstridenum == stridenum) diskstack->top--;
1421: tjsch->rctx->reverseonestep = PETSC_FALSE;
1422: return(0);
1423: }
1425: if (stack->solution_only) {
1426: /* restore a checkpoint */
1427: StackTop(stack,&e);
1428: UpdateTS(ts,stack,e,PETSC_TRUE);
1429: /* start with restoring a checkpoint */
1430: tjsch->rctx->capo = stepnum;
1431: tjsch->rctx->oldcapo = tjsch->rctx->capo;
1432: shift = stepnum-localstepnum;
1433: whattodo = revolve_action(&tjsch->rctx->check,&tjsch->rctx->capo,&tjsch->rctx->fine,tjsch->rctx->snaps_in,&tjsch->rctx->info,&tjsch->rctx->where);
1434: printwhattodo(tj->monitor,whattodo,tjsch->rctx,shift);
1435: tjsch->recompute = PETSC_TRUE;
1436: TurnForward(ts);
1437: ReCompute(ts,tjsch,e->stepnum,stepnum);
1438: if (e->stepnum+1 == stepnum) {
1439: StackPop(stack,&e);
1440: ElementDestroy(stack,e);
1441: }
1442: } else {
1443: /* restore a checkpoint */
1444: StackTop(stack,&e);
1445: UpdateTS(ts,stack,e,PETSC_TRUE);
1446: /* 2 revolve actions: restore a checkpoint and then advance */
1447: ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,tjsch->total_steps,stepnum,localstepnum,PETSC_FALSE,&store);
1448: if (tj->monitor) {
1449: PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
1450: PetscViewerASCIIPrintf(tj->monitor,"Skip the step from %D to %D (stage values already checkpointed)\n",stepnum-localstepnum+tjsch->rctx->oldcapo,stepnum-localstepnum+tjsch->rctx->oldcapo+1);
1451: PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
1452: }
1453: if (!tjsch->rctx->reverseonestep && tjsch->rctx->stepsleft > 0) tjsch->rctx->stepsleft--;
1454: if (e->stepnum < stepnum) {
1455: tjsch->recompute = PETSC_TRUE;
1456: TurnForward(ts);
1457: ReCompute(ts,tjsch,e->stepnum,stepnum);
1458: }
1459: if (e->stepnum == stepnum) {
1460: StackPop(stack,&e);
1461: ElementDestroy(stack,e);
1462: }
1463: }
1464: tjsch->rctx->reverseonestep = PETSC_FALSE;
1465: return(0);
1466: }
1468: static PetscErrorCode SetTrajRMS(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum,PetscReal time,Vec X)
1469: {
1470: Stack *stack = &tjsch->stack;
1471: PetscInt store;
1472: StackElement e;
1476: if (!stack->solution_only && stepnum == 0) return(0);
1477: if (stack->solution_only && stepnum == tjsch->total_steps) return(0);
1478: ApplyRevolve(tj->monitor,tjsch->stype,tjsch->rctx,tjsch->total_steps,stepnum,stepnum,PETSC_FALSE,&store);
1479: if (store == 1){
1480: if (stepnum < stack->top) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_MEMC,"Illegal modification of a non-top stack element");
1481: ElementCreate(ts,stack,&e);
1482: ElementSet(ts,stack,&e,stepnum,time,X);
1483: StackPush(stack,e);
1484: } else if (store == 2) {
1485: DumpSingle(tj,ts,stack,tjsch->rctx->check+1);
1486: }
1487: return(0);
1488: }
1490: static PetscErrorCode GetTrajRMS(TSTrajectory tj,TS ts,TJScheduler *tjsch,PetscInt stepnum)
1491: {
1492: Stack *stack = &tjsch->stack;
1493: PetscInt whattodo,shift;
1494: PetscInt restart;
1495: PetscBool ondisk;
1496: StackElement e;
1500: if (stepnum == 0 || stepnum == tjsch->total_steps) {
1501: TurnBackward(ts);
1502: tjsch->rctx->reverseonestep = PETSC_FALSE;
1503: return(0);
1504: }
1505: tjsch->rctx->capo = stepnum;
1506: tjsch->rctx->oldcapo = tjsch->rctx->capo;
1507: shift = 0;
1508: whattodo = revolve_action(&tjsch->rctx->check,&tjsch->rctx->capo,&tjsch->rctx->fine,tjsch->rctx->snaps_in,&tjsch->rctx->info,&tjsch->rctx->where); /* whattodo=restore */
1509: printwhattodo(tj->monitor,whattodo,tjsch->rctx,shift);
1510: /* restore a checkpoint */
1511: restart = tjsch->rctx->capo;
1512: if (!tjsch->rctx->where) {
1513: ondisk = PETSC_TRUE;
1514: LoadSingle(tj,ts,stack,tjsch->rctx->check+1);
1515: TurnBackward(ts);
1516: } else {
1517: ondisk = PETSC_FALSE;
1518: StackTop(stack,&e);
1519: UpdateTS(ts,stack,e,PETSC_TRUE);
1520: }
1521: if (!stack->solution_only) { /* whattodo must be 5 or 8 */
1522: /* ask Revolve what to do next */
1523: tjsch->rctx->oldcapo = tjsch->rctx->capo;
1524: 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*/
1525: printwhattodo(tj->monitor,whattodo,tjsch->rctx,shift);
1526: if (whattodo == 3 || whattodo == 4) tjsch->rctx->reverseonestep = PETSC_TRUE;
1527: if (whattodo == 1) tjsch->rctx->stepsleft = tjsch->rctx->capo-tjsch->rctx->oldcapo;
1528: if (tj->monitor) {
1529: PetscViewerASCIIAddTab(tj->monitor,((PetscObject)tj)->tablevel);
1530: PetscViewerASCIIPrintf(tj->monitor,"Skip the step from %D to %D (stage values already checkpointed)\n",tjsch->rctx->oldcapo,tjsch->rctx->oldcapo+1);
1531: PetscViewerASCIISubtractTab(tj->monitor,((PetscObject)tj)->tablevel);
1532: }
1533: if (!tjsch->rctx->reverseonestep && tjsch->rctx->stepsleft > 0) tjsch->rctx->stepsleft--;
1534: restart++; /* skip one step */
1535: }
1536: if (stack->solution_only || (!stack->solution_only && restart < stepnum)) {
1537: tjsch->recompute = PETSC_TRUE;
1538: TurnForward(ts);
1539: ReCompute(ts,tjsch,restart,stepnum);
1540: }
1541: if (!ondisk && ( (stack->solution_only && e->stepnum+1 == stepnum) || (!stack->solution_only && e->stepnum == stepnum) )) {
1542: StackPop(stack,&e);
1543: ElementDestroy(stack,e);
1544: }
1545: tjsch->rctx->reverseonestep = PETSC_FALSE;
1546: return(0);
1547: }
1548: #endif
1550: static PetscErrorCode TSTrajectorySet_Memory(TSTrajectory tj,TS ts,PetscInt stepnum,PetscReal time,Vec X)
1551: {
1552: TJScheduler *tjsch = (TJScheduler*)tj->data;
1556: if (!tjsch->recompute) { /* use global stepnum in the forward sweep */
1557: TSGetStepNumber(ts,&stepnum);
1558: }
1559: /* for consistency */
1560: if (!tjsch->recompute && stepnum == 0) ts->ptime_prev = ts->ptime-ts->time_step;
1561: switch (tjsch->stype) {
1562: case NONE:
1563: if (tj->adjoint_solve_mode) {
1564: SetTrajN(ts,tjsch,stepnum,time,X);
1565: } else {
1566: SetTrajN_2(ts,tjsch,stepnum,time,X);
1567: }
1568: break;
1569: case TWO_LEVEL_NOREVOLVE:
1570: if (!tj->adjoint_solve_mode) SETERRQ(PetscObjectComm((PetscObject)tj),PETSC_ERR_SUP,"Not implemented");
1571: SetTrajTLNR(tj,ts,tjsch,stepnum,time,X);
1572: break;
1573: #if defined(PETSC_HAVE_REVOLVE)
1574: case TWO_LEVEL_REVOLVE:
1575: if (!tj->adjoint_solve_mode) SETERRQ(PetscObjectComm((PetscObject)tj),PETSC_ERR_SUP,"Not implemented");
1576: SetTrajTLR(tj,ts,tjsch,stepnum,time,X);
1577: break;
1578: case TWO_LEVEL_TWO_REVOLVE:
1579: if (!tj->adjoint_solve_mode) SETERRQ(PetscObjectComm((PetscObject)tj),PETSC_ERR_SUP,"Not implemented");
1580: SetTrajTLTR(tj,ts,tjsch,stepnum,time,X);
1581: break;
1582: case REVOLVE_OFFLINE:
1583: if (!tj->adjoint_solve_mode) SETERRQ(PetscObjectComm((PetscObject)tj),PETSC_ERR_SUP,"Not implemented");
1584: SetTrajROF(tj,ts,tjsch,stepnum,time,X);
1585: break;
1586: case REVOLVE_ONLINE:
1587: if (!tj->adjoint_solve_mode) SETERRQ(PetscObjectComm((PetscObject)tj),PETSC_ERR_SUP,"Not implemented");
1588: SetTrajRON(tj,ts,tjsch,stepnum,time,X);
1589: break;
1590: case REVOLVE_MULTISTAGE:
1591: if (!tj->adjoint_solve_mode) SETERRQ(PetscObjectComm((PetscObject)tj),PETSC_ERR_SUP,"Not implemented");
1592: SetTrajRMS(tj,ts,tjsch,stepnum,time,X);
1593: break;
1594: #endif
1595: default:
1596: break;
1597: }
1598: return(0);
1599: }
1601: static PetscErrorCode TSTrajectoryGet_Memory(TSTrajectory tj,TS ts,PetscInt stepnum,PetscReal *t)
1602: {
1603: TJScheduler *tjsch = (TJScheduler*)tj->data;
1607: if (tj->adjoint_solve_mode && stepnum == 0) return(0);
1608: switch (tjsch->stype) {
1609: case NONE:
1610: if (tj->adjoint_solve_mode) {
1611: GetTrajN(ts,tjsch,stepnum);
1612: } else {
1613: GetTrajN_2(ts,tjsch,stepnum);
1614: }
1615: break;
1616: case TWO_LEVEL_NOREVOLVE:
1617: if (!tj->adjoint_solve_mode) SETERRQ(PetscObjectComm((PetscObject)tj),PETSC_ERR_SUP,"Not implemented");
1618: GetTrajTLNR(tj,ts,tjsch,stepnum);
1619: break;
1620: #if defined(PETSC_HAVE_REVOLVE)
1621: case TWO_LEVEL_REVOLVE:
1622: if (!tj->adjoint_solve_mode) SETERRQ(PetscObjectComm((PetscObject)tj),PETSC_ERR_SUP,"Not implemented");
1623: GetTrajTLR(tj,ts,tjsch,stepnum);
1624: break;
1625: case TWO_LEVEL_TWO_REVOLVE:
1626: if (!tj->adjoint_solve_mode) SETERRQ(PetscObjectComm((PetscObject)tj),PETSC_ERR_SUP,"Not implemented");
1627: GetTrajTLTR(tj,ts,tjsch,stepnum);
1628: break;
1629: case REVOLVE_OFFLINE:
1630: if (!tj->adjoint_solve_mode) SETERRQ(PetscObjectComm((PetscObject)tj),PETSC_ERR_SUP,"Not implemented");
1631: GetTrajROF(tj,ts,tjsch,stepnum);
1632: break;
1633: case REVOLVE_ONLINE:
1634: if (!tj->adjoint_solve_mode) SETERRQ(PetscObjectComm((PetscObject)tj),PETSC_ERR_SUP,"Not implemented");
1635: GetTrajRON(tj,ts,tjsch,stepnum);
1636: break;
1637: case REVOLVE_MULTISTAGE:
1638: if (!tj->adjoint_solve_mode) SETERRQ(PetscObjectComm((PetscObject)tj),PETSC_ERR_SUP,"Not implemented");
1639: GetTrajRMS(tj,ts,tjsch,stepnum);
1640: break;
1641: #endif
1642: default:
1643: break;
1644: }
1645: return(0);
1646: }
1648: PETSC_UNUSED static PetscErrorCode TSTrajectorySetStride_Memory(TSTrajectory tj,PetscInt stride)
1649: {
1650: TJScheduler *tjsch = (TJScheduler*)tj->data;
1653: tjsch->stride = stride;
1654: return(0);
1655: }
1657: PETSC_UNUSED static PetscErrorCode TSTrajectorySetMaxCpsRAM_Memory(TSTrajectory tj,PetscInt max_cps_ram)
1658: {
1659: TJScheduler *tjsch = (TJScheduler*)tj->data;
1662: tjsch->max_cps_ram = max_cps_ram;
1663: return(0);
1664: }
1666: PETSC_UNUSED static PetscErrorCode TSTrajectorySetMaxCpsDisk_Memory(TSTrajectory tj,PetscInt max_cps_disk)
1667: {
1668: TJScheduler *tjsch = (TJScheduler*)tj->data;
1671: tjsch->max_cps_disk = max_cps_disk;
1672: return(0);
1673: }
1675: #if defined(PETSC_HAVE_REVOLVE)
1676: PETSC_UNUSED static PetscErrorCode TSTrajectorySetRevolveOnline(TSTrajectory tj,PetscBool use_online)
1677: {
1678: TJScheduler *tjsch = (TJScheduler*)tj->data;
1681: tjsch->use_online = use_online;
1682: return(0);
1683: }
1684: #endif
1686: PETSC_UNUSED static PetscErrorCode TSTrajectorySetSaveStack(TSTrajectory tj,PetscBool save_stack)
1687: {
1688: TJScheduler *tjsch = (TJScheduler*)tj->data;
1691: tjsch->save_stack = save_stack;
1692: return(0);
1693: }
1695: PETSC_UNUSED static PetscErrorCode TSTrajectorySetUseDRAM(TSTrajectory tj,PetscBool use_dram)
1696: {
1697: TJScheduler *tjsch = (TJScheduler*)tj->data;
1700: tjsch->stack.use_dram = use_dram;
1701: return(0);
1702: }
1704: static PetscErrorCode TSTrajectorySetFromOptions_Memory(PetscOptionItems *PetscOptionsObject,TSTrajectory tj)
1705: {
1706: TJScheduler *tjsch = (TJScheduler*)tj->data;
1710: PetscOptionsHead(PetscOptionsObject,"Memory based TS trajectory options");
1711: {
1712: PetscOptionsInt("-ts_trajectory_max_cps_ram","Maximum number of checkpoints in RAM","TSTrajectorySetMaxCpsRAM_Memory",tjsch->max_cps_ram,&tjsch->max_cps_ram,NULL);
1713: PetscOptionsInt("-ts_trajectory_max_cps_disk","Maximum number of checkpoints on disk","TSTrajectorySetMaxCpsDisk_Memory",tjsch->max_cps_disk,&tjsch->max_cps_disk,NULL);
1714: PetscOptionsInt("-ts_trajectory_stride","Stride to save checkpoints to file","TSTrajectorySetStride_Memory",tjsch->stride,&tjsch->stride,NULL);
1715: #if defined(PETSC_HAVE_REVOLVE)
1716: PetscOptionsBool("-ts_trajectory_revolve_online","Trick TS trajectory into using online mode of revolve","TSTrajectorySetRevolveOnline",tjsch->use_online,&tjsch->use_online,NULL);
1717: #endif
1718: PetscOptionsBool("-ts_trajectory_save_stack","Save all stack to disk","TSTrajectorySetSaveStack",tjsch->save_stack,&tjsch->save_stack,NULL);
1719: PetscOptionsBool("-ts_trajectory_use_dram","Use DRAM for checkpointing","TSTrajectorySetUseDRAM",tjsch->stack.use_dram,&tjsch->stack.use_dram,NULL);
1720: }
1721: PetscOptionsTail();
1722: tjsch->stack.solution_only = tj->solution_only;
1723: return(0);
1724: }
1726: static PetscErrorCode TSTrajectorySetUp_Memory(TSTrajectory tj,TS ts)
1727: {
1728: TJScheduler *tjsch = (TJScheduler*)tj->data;
1729: Stack *stack = &tjsch->stack;
1730: #if defined(PETSC_HAVE_REVOLVE)
1731: RevolveCTX *rctx,*rctx2;
1732: DiskStack *diskstack = &tjsch->diskstack;
1733: PetscInt diskblocks;
1734: #endif
1735: PetscInt numY,total_steps;
1736: PetscBool fixedtimestep;
1740: if (ts->adapt) {
1741: PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&fixedtimestep);
1742: } else fixedtimestep = PETSC_TRUE;
1743: total_steps = (PetscInt)(PetscCeilReal((ts->max_time-ts->ptime)/ts->time_step));
1744: total_steps = total_steps < 0 ? PETSC_MAX_INT : total_steps;
1745: if (fixedtimestep) tjsch->total_steps = PetscMin(ts->max_steps,total_steps);
1746: if (tjsch->max_cps_ram > 0) stack->stacksize = tjsch->max_cps_ram;
1748: if (tjsch->stride > 1) { /* two level mode */
1749: if (tjsch->save_stack && tjsch->max_cps_disk > 1 && tjsch->max_cps_disk <= tjsch->max_cps_ram) SETERRQ(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.");
1750: 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 */
1751: 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 */
1752: 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 */
1753: } else { /* single level mode */
1754: if (fixedtimestep) {
1755: if (tjsch->max_cps_ram >= tjsch->total_steps-1 || tjsch->max_cps_ram < 1) tjsch->stype = NONE; /* checkpoint all */
1756: else tjsch->stype = (tjsch->max_cps_disk>1) ? REVOLVE_MULTISTAGE : REVOLVE_OFFLINE;
1757: } else tjsch->stype = NONE; /* checkpoint all for adaptive time step */
1758: #if defined(PETSC_HAVE_REVOLVE)
1759: if (tjsch->use_online) tjsch->stype = REVOLVE_ONLINE; /* trick into online (for testing purpose only) */
1760: #endif
1761: }
1763: if (tjsch->stype > TWO_LEVEL_NOREVOLVE) {
1764: #ifndef PETSC_HAVE_REVOLVE
1765: 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.");
1766: #else
1767: switch (tjsch->stype) {
1768: case TWO_LEVEL_REVOLVE:
1769: revolve_create_offline(tjsch->stride,tjsch->max_cps_ram);
1770: break;
1771: case TWO_LEVEL_TWO_REVOLVE:
1772: 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. */
1773: diskstack->stacksize = diskblocks;
1774: revolve_create_offline(tjsch->stride,tjsch->max_cps_ram);
1775: revolve2_create_offline((tjsch->total_steps+tjsch->stride-1)/tjsch->stride,diskblocks);
1776: PetscCalloc1(1,&rctx2);
1777: rctx2->snaps_in = diskblocks;
1778: rctx2->reverseonestep = PETSC_FALSE;
1779: rctx2->check = 0;
1780: rctx2->oldcapo = 0;
1781: rctx2->capo = 0;
1782: rctx2->info = 2;
1783: rctx2->fine = (tjsch->total_steps+tjsch->stride-1)/tjsch->stride;
1784: tjsch->rctx2 = rctx2;
1785: diskstack->top = -1;
1786: PetscMalloc1(diskstack->stacksize*sizeof(PetscInt),&diskstack->container);
1787: break;
1788: case REVOLVE_OFFLINE:
1789: revolve_create_offline(tjsch->total_steps,tjsch->max_cps_ram);
1790: break;
1791: case REVOLVE_ONLINE:
1792: stack->stacksize = tjsch->max_cps_ram;
1793: revolve_create_online(tjsch->max_cps_ram);
1794: break;
1795: case REVOLVE_MULTISTAGE:
1796: revolve_create_multistage(tjsch->total_steps,tjsch->max_cps_ram+tjsch->max_cps_disk,tjsch->max_cps_ram);
1797: break;
1798: default:
1799: break;
1800: }
1801: PetscCalloc1(1,&rctx);
1802: rctx->snaps_in = tjsch->max_cps_ram; /* for theta methods snaps_in=2*max_cps_ram */
1803: rctx->reverseonestep = PETSC_FALSE;
1804: rctx->check = 0;
1805: rctx->oldcapo = 0;
1806: rctx->capo = 0;
1807: rctx->info = 2;
1808: rctx->fine = (tjsch->stride > 1) ? tjsch->stride : tjsch->total_steps;
1809: tjsch->rctx = rctx;
1810: if (tjsch->stype == REVOLVE_ONLINE) rctx->fine = -1;
1811: #endif
1812: } else {
1813: if (tjsch->stype == TWO_LEVEL_NOREVOLVE) stack->stacksize = tjsch->stride-1; /* need tjsch->stride-1 at most */
1814: if (tjsch->stype == NONE) {
1815: if (fixedtimestep) stack->stacksize = stack->solution_only ? tjsch->total_steps : tjsch->total_steps-1;
1816: else { /* adaptive time step */
1817: /* if max_cps_ram is not specified, use maximal allowed number of steps for stack size */
1818: if(tjsch->max_cps_ram == -1) stack->stacksize = ts->max_steps < PETSC_MAX_INT ? ts->max_steps : 10000;
1819: tjsch->total_steps = stack->solution_only ? stack->stacksize : stack->stacksize+1; /* will be updated as time integration advances */
1820: }
1821: }
1822: }
1824: tjsch->recompute = PETSC_FALSE;
1825: TSGetStages(ts,&numY,PETSC_IGNORE);
1826: StackCreate(stack,stack->stacksize,numY);
1827: return(0);
1828: }
1830: static PetscErrorCode TSTrajectoryReset_Memory(TSTrajectory tj)
1831: {
1832: TJScheduler *tjsch = (TJScheduler*)tj->data;
1836: if (tjsch->stype > TWO_LEVEL_NOREVOLVE) {
1837: #if defined(PETSC_HAVE_REVOLVE)
1838: revolve_reset();
1839: if (tjsch->stype == TWO_LEVEL_TWO_REVOLVE) {
1840: revolve2_reset();
1841: PetscFree(tjsch->diskstack.container);
1842: }
1843: #endif
1844: }
1845: StackDestroy(&tjsch->stack);
1846: #if defined(PETSC_HAVE_REVOLVE)
1847: if (tjsch->stype > TWO_LEVEL_NOREVOLVE) {
1848: PetscFree(tjsch->rctx);
1849: PetscFree(tjsch->rctx2);
1850: }
1851: #endif
1852: return(0);
1853: }
1855: static PetscErrorCode TSTrajectoryDestroy_Memory(TSTrajectory tj)
1856: {
1857: TJScheduler *tjsch = (TJScheduler*)tj->data;
1861: PetscFree(tjsch);
1862: return(0);
1863: }
1865: /*MC
1866: TSTRAJECTORYMEMORY - Stores each solution of the ODE/ADE in memory
1868: Level: intermediate
1870: .seealso: TSTrajectoryCreate(), TS, TSTrajectorySetType()
1872: M*/
1873: PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Memory(TSTrajectory tj,TS ts)
1874: {
1875: TJScheduler *tjsch;
1879: tj->ops->set = TSTrajectorySet_Memory;
1880: tj->ops->get = TSTrajectoryGet_Memory;
1881: tj->ops->setup = TSTrajectorySetUp_Memory;
1882: tj->ops->setfromoptions = TSTrajectorySetFromOptions_Memory;
1883: tj->ops->reset = TSTrajectoryReset_Memory;
1884: tj->ops->destroy = TSTrajectoryDestroy_Memory;
1886: PetscCalloc1(1,&tjsch);
1887: tjsch->stype = NONE;
1888: tjsch->max_cps_ram = -1; /* -1 indicates that it is not set */
1889: tjsch->max_cps_disk = -1; /* -1 indicates that it is not set */
1890: tjsch->stride = 0; /* if not zero, two-level checkpointing will be used */
1891: #if defined(PETSC_HAVE_REVOLVE)
1892: tjsch->use_online = PETSC_FALSE;
1893: #endif
1894: tjsch->save_stack = PETSC_TRUE;
1896: tjsch->stack.solution_only = tj->solution_only;
1898: tj->data = tjsch;
1899: return(0);
1900: }