This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phases.c
234 lines (201 loc) · 7.11 KB
/
phases.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
** $Id: phases.c,v 1.10 2011/01/03 21:51:23 rolf Exp $
**
** Rolf Riesen, September 2009 -- 2010, Sandia National Laboratories
** All time values are stored in doubles and are in minutes
**
** This file is part of app_model. App_model is free software and
** is distributed under the terms of the GNU General Public License
** Version 3. See the file LICENSE for details.
** Copyright 2009 Sandia Corporation. Under the terms of Contract
** DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
** retains certain rights in this software.
**
*/
#include <stdio.h>
#include <assert.h>
#include "phases.h"
#include "globals.h"
#define MIN(a, b) ((a) < (b) ? (a) : (b))
/*
** Do a restart
** Parameters:
** IN next_interrupt Time when the next interrupt will occur
** IN restart_time How much time it takes to do a restart
** IN verbose Controls amount of debug output
** IN/OUT elapsed_time Current time
*/
void
do_restart(double next_interrupt, double restart_time, int verbose, double *elapsed_time)
{
if (next_interrupt > (*elapsed_time + restart_time)) {
/*
** We have enough time to finish this restart before the
** next interrupt occurs.
*/
*elapsed_time= *elapsed_time + restart_time;
restart_cnt++;
total_restart_time= total_restart_time + restart_time;
if (verbose > 3) {
fprintf(stderr, "%12.1f\" restart time %12.1f\", count %d\n", *elapsed_time,
restart_time, restart_cnt);
}
} else {
/*
** We will get interrupted again while restarting
** Advance current time and try again.
*/
wasted_restart_time= wasted_restart_time + (next_interrupt - *elapsed_time);
*elapsed_time= next_interrupt;
failed_restart_cnt++;
if (verbose > 2) {
fprintf(stderr, "%12.1f\" restart %d/%d failed\n", *elapsed_time, failed_restart_cnt, restart_cnt);
}
}
} /* end of do_restart() */
/*
** Do rework
** Parameters:
** IN next_interrupt Time when the next interrupt will occur
** IN rework_time How much rework is there to do?
** IN tau Time between checkpoints
** IN verbose Controls amount of debug output
** IN/OUT elapsed_time Current time
**
** Return how much work we have done (could be good or wasted work)
*/
double
do_rework(double next_interrupt, double rework_time, int verbose, double *elapsed_time)
{
double rework_done;
/*
** How much rework can we do, until the next interrupt
** or the next checkpoint?
*/
rework_done= MIN((next_interrupt - *elapsed_time), rework_time);
*elapsed_time= *elapsed_time + rework_done;
/* Has the interrupt arrived? */
if (*elapsed_time >= next_interrupt) {
/* Rework was interrupted */
wasted_rework_time= wasted_rework_time + rework_done;
failed_rework_cnt++;
if (verbose > 2) {
fprintf(stderr, "%12.1f\" rework time (partial) %12.1f/%.0f\", count %d\n", *elapsed_time,
rework_done, MIN((next_interrupt - *elapsed_time), rework_time), failed_rework_cnt);
}
} else if (rework_done >= rework_time) {
/* We are done with rework */
total_rework_time= total_rework_time + rework_done;
/*
** If we make it through the rest of the segment to the next checkpoint,
** rework_done will count as work done, but not yet!
*/
rework_cnt++;
if (verbose > 3) {
fprintf(stderr, "%12.1f\" rework (saved) done %12.1f\", work done so far %12.1f\", count %d\n",
*elapsed_time, rework_done, total_work_time, rework_cnt);
}
} else {
/* This means we had more rework than tau! */
assert(FALSE);
}
return rework_done;
} /* end of do_rework() */
/*
** Resume work until next interrupt (and do checkpoints)
** Parameters:
** IN next_interrupt Time when the next interrupt will occur
** IN work_time Total amount of work that needs to be done
** IN/OUT rework_time How much time has been done in this segment, and
** how much time will need to be done in next segment
** IN time_left_ How much time until checkpoint?
** IN checkpoint_time How much time to write a checkpoint
** IN verbose Controls amount of debug output
** IN/OUT elapsed_time Current time
**
** Return false, if there is more work to do.
*/
int
do_work(double next_interrupt, double work_time, double *rework_time,
double time_left_this_segment, double tau, double checkpoint_time,
int verbose, double *elapsed_time)
{
double work_done;
double work_left;
double checkpoint_done;
int first_segment;
first_segment= TRUE;
while (TRUE) {
/*
** How much work can we do, until the next interrupt
** or the next checkpoint?
*/
work_left= work_time - total_work_time;
if (first_segment) {
/* Also add in the rework time done earlier in this segment */
work_left= work_left - *rework_time;
}
work_done= MIN(time_left_this_segment, (next_interrupt - *elapsed_time));
work_done= MIN(work_done, work_left);
*elapsed_time= *elapsed_time + work_done;
if (first_segment) {
/* Also add in the rework time done earlier in this segment */
work_done += *rework_time;
first_segment= FALSE;
}
/* Has the interrupt arrived? */
if (*elapsed_time >= next_interrupt) {
failed_work_cnt++;
*rework_time= work_done;
if (verbose > 2) {
fprintf(stderr, "%12.1f\" work time (partial) %12.1f/%.0f\", count %d\n", *elapsed_time,
work_done, MIN(time_left_this_segment, work_left), failed_work_cnt);
}
break;
}
/* Must be checkpoint time */
if (next_interrupt > (*elapsed_time + checkpoint_time)) {
/* We have time to write a checkpoint */
work_cnt++;
total_work_time= total_work_time + work_done;
if ((work_time - total_work_time) <= 0.0) {
/* We are done with work */
if (verbose > 3) {
fprintf(stderr, "%12.1f\" work (saved) DONE %12.1f\", so far %12.1f\", count %d\n", *elapsed_time,
work_done, total_work_time, work_cnt);
}
return TRUE; /* done */
}
*elapsed_time= *elapsed_time + checkpoint_time;
checkpoint_cnt++;
total_checkpoint_time= total_checkpoint_time + checkpoint_time;
*rework_time= 0.0;
time_left_this_segment= tau;
if (verbose > 3) {
fprintf(stderr, "%12.1f\" work (saved) time %12.1f\", so far %12.1f\", count %d\n", *elapsed_time,
work_done, total_work_time, work_cnt);
fprintf(stderr, "%12.1f\" checkpoint time %12.1f\", count %d\n", *elapsed_time,
checkpoint_time, checkpoint_cnt);
}
} else {
/* We will get interrupted during a checkpoint write */
checkpoint_done= next_interrupt - *elapsed_time;
*elapsed_time= *elapsed_time + checkpoint_done;
wasted_checkpoint_time= wasted_checkpoint_time + checkpoint_done;
failed_checkpoint_cnt++;
*rework_time= work_done;
if (verbose > 2) {
fprintf(stderr, "%12.1f\" work (not saved) time %12.1f\", count %d\n", *elapsed_time,
work_done, failed_work_cnt);
fprintf(stderr, "%12.1f\" failed checkpoint time %12.1f/%.0f\", count %d\n", *elapsed_time,
checkpoint_done, checkpoint_time, failed_checkpoint_cnt);
}
break;
}
}
if ((work_time - total_work_time) > 0.0) {
return FALSE; /* not done */
} else {
return TRUE; /* done */
}
} /* end of do_work() */