aboutsummaryrefslogtreecommitdiff
path: root/step_control.c
blob: 5dba741e3e44d2644daf93365263741d5c999498 (plain)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
 * Copyright 2020 Anton Khirnov <anton@khirnov.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <float.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "common.h"
#include "log.h"
#include "mg2d_constants.h"
#include "step_control.h"

#define SC_HIST_SIZE 16

struct StepControl {
    /* estimated value for a good step */
    double hint;
    /* minimum step size, fail if this value is reached */
    double step_min;

    uint64_t step_counter;

    double step[SC_HIST_SIZE];
    double fact[SC_HIST_SIZE];

    double conv_epsilon;

    int hist_len;
    int idx_bracket;
    int idx_diverge;

    struct random_data rd;
    char rd_buf[16];

    MG2DLogger logger;
};

static void sci_find_bracket(StepControl *sc)
{
    int idx_max = -1;
    double val_max = 0.0;

    for (int i = 0; i < sc->hist_len; i++) {
        if (sc->fact[i] > val_max) {
            val_max = sc->fact[i];
            idx_max = i;
        }
    }

    if (val_max > 1.0 && idx_max > 0 && idx_max < sc->hist_len - 1) {
        mg2di_assert(sc->fact[idx_max - 1] < val_max &&
                     sc->fact[idx_max + 1] < val_max);
        sc->idx_bracket = idx_max - 1;
    }
}

static void sci_recheck_bracket(StepControl *sc)
{
    if (sc->idx_bracket >= 0 &&
        (sc->fact[sc->idx_bracket + 1] <= sc->fact[sc->idx_bracket] ||
         sc->fact[sc->idx_bracket + 1] <= sc->fact[sc->idx_bracket + 2]))
        sc->idx_bracket = -1;
    sci_find_bracket(sc);
}

static void sci_hist_clear(StepControl *sc)
{
    for (int i = 0; i < SC_HIST_SIZE; i++) {
        sc->step[i] = DBL_MAX;
        sc->fact[i] = DBL_MAX;
    }

    sc->hist_len    = 0;
    sc->idx_bracket = -1;
    sc->idx_diverge = -1;
}

static void sci_hist_append(StepControl *sc, double step, double fact)
{
    mg2di_assert(sc->hist_len < SC_HIST_SIZE);
    sc->step[sc->hist_len] = step;
    sc->fact[sc->hist_len] = fact;
    sc->hist_len++;
}

static void sci_hist_insert(StepControl *sc, double step, double fact, int idx)
{
    mg2di_assert(idx < SC_HIST_SIZE);
    mg2di_assert(sc->hist_len < SC_HIST_SIZE);

    memmove(sc->step + idx + 1, sc->step + idx,
            sizeof(*sc->step) * MAX(sc->hist_len - idx, 0));
    memmove(sc->fact + idx + 1, sc->fact + idx,
            sizeof(*sc->fact) * MAX(sc->hist_len - idx, 0));

    sc->step[idx] = step;
    sc->fact[idx] = fact;

    if (sc->idx_diverge >= idx)
        sc->idx_diverge++;
    if (sc->idx_bracket >= 0) {
        if (sc->idx_bracket >= idx)
            sc->idx_bracket++;
        else if (sc->idx_bracket >= idx - 2)
            sc->idx_bracket = -1;
    }

    sc->hist_len++;
    if (sc->idx_bracket == -1)
        sci_find_bracket(sc);
}

static void sci_hist_drop(StepControl *sc, int idx_start, int idx_end)
{
    idx_start = CLIP(idx_start, 0, SC_HIST_SIZE);
    idx_end   = CLIP(idx_end, idx_start, SC_HIST_SIZE);

    memmove(sc->step + idx_start, sc->step + idx_end,
            sizeof(*sc->step) * MAX(sc->hist_len - idx_end, 0));
    memmove(sc->fact + idx_start, sc->fact + idx_end,
            sizeof(*sc->fact) * MAX(sc->hist_len - idx_end, 0));

    if (sc->idx_diverge >= idx_start) {
        if (sc->idx_diverge >= idx_end)
            sc->idx_diverge -= idx_end - idx_start;
        else
            sc->idx_diverge = -1;
    }
    if (sc->idx_bracket >= 0) {
        if (sc->idx_bracket >= idx_end)
            sc->idx_bracket -= idx_end - idx_start;
        else if (sc->idx_bracket >= idx_start - 2)
            sc->idx_bracket = -1;
    }
    sc->hist_len -= idx_end - idx_start;
    if (sc->idx_bracket == -1)
        sci_find_bracket(sc);
}

int sc_alloc(StepControl **psc, MG2DLogger logger)
{
    StepControl *sc;
    struct timespec tv;

    sc = calloc(1, sizeof(*sc));
    if (!sc)
        return -ENOMEM;

    sci_hist_clear(sc);
    sc->hint         = DBL_MAX;
    sc->step_min     = DBL_MAX;
    sc->step_counter = 0;

    clock_gettime(CLOCK_REALTIME, &tv);
    initstate_r(tv.tv_nsec, sc->rd_buf, sizeof(sc->rd_buf), &sc->rd);

    sc->logger = logger;

    *psc = sc;

    return 0;
}

void sc_free(StepControl **psc)
{
    StepControl *sc = *psc;

    if (!sc)
        return;

    free(sc);
    *psc = NULL;
}

void sc_init(StepControl *sc, double hint, double step_min)
{
    sc->hint             = hint > 0.0 ? hint : 0.25;
    sc->conv_epsilon     = sc->hint * 1e-3;
    sc->step_min         = step_min >= 0.0 ? step_min : sc->conv_epsilon;
    sc->step_counter     = 0;

    mg2di_log(&sc->logger, MG2D_LOG_DEBUG, "stepcontrol init: hint %g step_min %g\n", hint, step_min);
}

double sc_step_get(StepControl *sc)
{
#define RETURN_STEP(sc, x, desc)                                                     \
do {                                                                            \
    mg2di_log(&(sc)->logger, MG2D_LOG_DEBUG, "step get: %g (%s)\n", x, desc); \
    return x;                                                                   \
} while (0)

    sc->step_counter++;

    // no history present, try the hint
    if (!sc->hist_len)
        RETURN_STEP(sc, sc->hint, "no history");

    // don't have the bracket yet
    if (sc->idx_bracket < 0) {
        const double high_step = sc->step[sc->hist_len - 1];
        const double high_fact = sc->fact[sc->hist_len - 1];

        // don't know where divergence happens
        // -> try a higher step
        if (high_fact > 1.0)
            RETURN_STEP(sc, high_step * 1.2, "no bracket, step high");

        // convergence factor should decrease towards smaller timesteps
        // so just try lower ones until we get a bracket
        RETURN_STEP(sc, MAX(sc->step_min, sc->step[0] * 0.8), "no bracket, step low");
    }

    // got a bracket, golden-section-search it until convergence
    mg2di_assert(sc->hist_len - sc->idx_bracket >= 3);
    mg2di_assert(sc->fact[sc->idx_bracket]     < sc->fact[sc->idx_bracket + 1]);
    mg2di_assert(sc->fact[sc->idx_bracket + 2] < sc->fact[sc->idx_bracket + 1]);
    {
        const double dist0 = sc->step[sc->idx_bracket + 1] - sc->step[sc->idx_bracket];
        const double dist1 = sc->step[sc->idx_bracket + 2] - sc->step[sc->idx_bracket + 1];

        // try a random step upwards once in a while to escape local minima
        if (!(sc->step_counter & ((1 << 6) - 1))) {
            // scale such that half the steps fall within 1.25 * upper bracket
            // with logarithmic growth
            const double scale = log(1.5) / (0.25 * sc->step[sc->idx_bracket + 2]);
            double offset;
            int32_t rval;

            random_r(&sc->rd, &rval);
            mg2di_assert(rval >= 0);

            offset = log(1.0 + (double)rval/RAND_MAX) / scale;

            RETURN_STEP(sc, sc->step[sc->idx_bracket + 2] + offset, "random step");
        }

        // converged
        if (dist0 < sc->conv_epsilon && dist1 < sc->conv_epsilon)
            RETURN_STEP(sc, sc->step[sc->idx_bracket + 1], "converged");

        if (dist0 > dist1)
            RETURN_STEP(sc, sc->step[sc->idx_bracket] + 0.61803 * dist0, "bisect low");
        else
            RETURN_STEP(sc, sc->step[sc->idx_bracket + 2] - 0.61803 * dist1, "bisect high");
    }
}

static void sci_log(StepControl *sc)
{
    char buf[1024], *p = buf;
    for (int i = 0; i < sc->hist_len; i++) {
        if (sc->idx_bracket == i)
            p += snprintf(p, sizeof(buf) - (p - buf), "[");
        if (sc->idx_diverge == i)
            p += snprintf(p, sizeof(buf) - (p - buf), "|");
        p += snprintf(p, sizeof(buf) - (p - buf), "%10.8g", sc->step[i]);
        if (sc->idx_bracket >= 0 && sc->idx_bracket == i - 2)
            p += snprintf(p, sizeof(buf) - (p - buf), "]");
        p += snprintf(p, sizeof(buf) - (p - buf), "\t");
    }
    if (sc->hist_len)
        mg2di_log(&sc->logger, MG2D_LOG_DEBUG, "%s\n", buf);

    p = buf;
    for (int i = 0; i < sc->hist_len; i++) {
        if (sc->idx_bracket == i)
            p += snprintf(p, sizeof(buf) - (p - buf), "[");
        if (sc->idx_diverge == i)
            p += snprintf(p, sizeof(buf) - (p - buf), "|");
        p += snprintf(p, sizeof(buf) - (p - buf), "%10.8g", sc->fact[i]);
        if (sc->idx_bracket >= 0 && sc->idx_bracket == i - 2)
            p += snprintf(p, sizeof(buf) - (p - buf), "]");
        p += snprintf(p, sizeof(buf) - (p - buf), "\t");
    }
    if (sc->hist_len)
        mg2di_log(&sc->logger, MG2D_LOG_DEBUG, "%s\n", buf);
}

int sc_step_confirm(StepControl *sc, const double step,
                    const double norm_old, const double norm_new)
{
    const double conv_fact = norm_old / norm_new;

    // signal failure if we reached minimum step
    if (step <= sc->step_min) {
        mg2di_log(&sc->logger, MG2D_LOG_ERROR, "Minimum step reached\n");
        return -EINVAL;
    }

    // divergence
    if (conv_fact <= 1.0) {
        // if the step is not larger then the largest known diverging step,
        // add it into history
        if (sc->idx_diverge < 0              ||
            sc->step[sc->idx_diverge] > step) {
            const int idx_converge = sc->idx_diverge >= 0 ?
                                     sc->idx_diverge - 1 : sc->hist_len - 1;

            // largest known coverging step doesn't converge anymore,
            // clear history
            if (idx_converge >= 0 && sc->step[idx_converge] >= step)
                sci_hist_clear(sc);
            // drop larger diverging steps
            if (sc->idx_diverge >= 0)
                sci_hist_drop(sc, sc->idx_diverge, sc->hist_len);
            sci_hist_append(sc, step, conv_fact);
            sc->idx_diverge = sc->hist_len - 1;
            sci_find_bracket(sc);
        }

        mg2di_log(&sc->logger, MG2D_LOG_DEBUG, "step reject: %g %g\n", step, conv_fact);
        sci_log(sc);

        return 1;
    }

    // convergence
#define FINISH(sc, reason)                                              \
do {                                                                    \
    mg2di_log(&sc->logger, MG2D_LOG_DEBUG, "step confirm: %s %g %g\n",  \
              reason, step, conv_fact);                                 \
    goto finish;                                                        \
} while (0);

    // history empty, add first element
    if (!sc->hist_len) {
        sci_hist_append(sc, step, conv_fact);
        FINISH(sc, "history empty");
    }

    // previously diverging step is now converging
    // drop all diverging steps from history
    if (sc->idx_diverge >= 0 && step >= sc->step[sc->idx_diverge]) {
        sci_hist_drop(sc, sc->idx_diverge, sc->hist_len);
        sci_hist_append(sc, step, conv_fact);
        sci_recheck_bracket(sc);
        FINISH(sc, "diverge->converge");
    }

    // no bracket
    if (sc->idx_bracket < 0) {
        int idx_floor = -1;

        for (int i = 0; i < sc->hist_len; i++) {
            if (sc->step[i] < step)
                idx_floor = i;
            else
                break;
        }

        // insert the new step where it belongs and check if we now have a
        // bracket
        sci_hist_insert(sc, step, conv_fact, idx_floor + 1);
        sci_find_bracket(sc);

        //  garbage-collect history
        if (sc->idx_bracket >= 0) {
            // bracket found: drop all converging steps beside the bracket
            sci_hist_drop(sc, 0, sc->idx_bracket);
            sci_hist_drop(sc, sc->idx_bracket + 3,
                          sc->idx_diverge >= 0 ? sc->idx_diverge : sc->hist_len);
        } else {
            // bracket still not found -> keep at most:
            //  - lowest converging step
            //  - highest converging step
            sci_hist_drop(sc, 1, sc->idx_diverge >= 0 ? sc->idx_diverge - 1 : sc->hist_len - 1);
        }

        FINISH(sc, "no bracket");
    }

    // have bracket

    // got a step outside of the bracket
    if (step <= sc->step[sc->idx_bracket] ||
        step >= sc->step[sc->idx_bracket + 2]) {
        if (conv_fact >= sc->fact[sc->idx_bracket + 1]) {
            sci_hist_clear(sc);
            sci_hist_append(sc, step, conv_fact);
        }
        FINISH(sc, "outside bracket");
    }

    // step within epsilon of the center
    // update central value and check if that breaks the bracket
    if (fabs(step - sc->step[sc->idx_bracket + 1]) <= sc->conv_epsilon) {
        sc->fact[sc->idx_bracket + 1] = conv_fact;
        sci_recheck_bracket(sc);
        FINISH(sc, "step center");
    }

    if (step < sc->step[sc->idx_bracket + 1]) {
        // step inside lower interval
        if (conv_fact > sc->fact[sc->idx_bracket + 1]) {
            // replace upper bound
            sc->step[sc->idx_bracket + 2] = sc->step[sc->idx_bracket + 1];
            sc->fact[sc->idx_bracket + 2] = sc->fact[sc->idx_bracket + 1];
            if (sc->idx_diverge == sc->idx_bracket + 2)
                sc->idx_diverge = -1;

            sc->step[sc->idx_bracket + 1] = step;
            sc->fact[sc->idx_bracket + 1] = conv_fact;
        } else {
            // replace lower bound
            sc->step[sc->idx_bracket] = step;
            sc->fact[sc->idx_bracket] = conv_fact;
        }
    } else {
        // step inside upper interval
        if (conv_fact > sc->fact[sc->idx_bracket + 1]) {
            // replace lower bound
            sc->step[sc->idx_bracket] = sc->step[sc->idx_bracket + 1];
            sc->fact[sc->idx_bracket] = sc->fact[sc->idx_bracket + 1];
            sc->step[sc->idx_bracket + 1] = step;
            sc->fact[sc->idx_bracket + 1] = conv_fact;
        } else {
            sc->step[sc->idx_bracket + 2] = step;
            sc->fact[sc->idx_bracket + 2] = conv_fact;
            if (sc->idx_diverge == sc->idx_bracket + 2)
                sc->idx_diverge = -1;
        }
    }
    FINISH(sc, "bisect");

finish:
    mg2di_log(&sc->logger, MG2D_LOG_DEBUG, "converge: %g %g\n", step, conv_fact);
    sci_log(sc);
    return 0;
}