/ concept-collection / fastandaccurate
Sign in
concept-collection / fastandaccurate
fastandaccurate / src / harness / timing.ts
55 lines · 2.1 KBCodeBlameHistory
ad5dc23Three new instances, near-field target sets, and an adaptive timing policyJeremy Magland 1// The timing policy, shared by the numbl runner and the MATLAB runner so
2// that both measure the same way (see docs/problems/laplace-dirichlet-2d.md).
3//
4// A fixed repeat count times a cheap point badly: a solve of 0.1 ms is
5// dominated by whatever the operating system was doing during those few
6// hundred microseconds, and the min of five such samples still scatters by
7// a factor of several from one sweep to the next, which is what made the
8// low-resolution end of the work-precision curves noisy. So the count is
9// adaptive: keep running timed repetitions until they have accumulated a
10// real amount of work, subject to a floor on the count and a cap so that
11// an expensive solve is not repeated forever. The reported time is still
12// the minimum over the timed runs, and every timing is recorded.
14export interface TimingPolicy {
15 /** Timed runs never stop below this count. */
16 minTimedRuns: number;
17 /** Timed runs continue past the minimum until they have accumulated
18 * this much time in total. */
19 timeBudgetSeconds: number;
20 /** Hard cap on the count. It bounds both the time a cheap point can
21 * spend and the number of timings a result file has to carry. */
22 maxTimedRuns: number;
25export const DEFAULT_TIMING: TimingPolicy = {
26 minTimedRuns: 5,
27 timeBudgetSeconds: 0.5,
28 maxTimedRuns: 50,
29};
31/**
32 * MATLAB lines that run `call` under the policy and leave one timing per
33 * run in `timesVar` (a column vector, trimmed to the number actually
34 * run). The loop lives on the MATLAB side so that all timing is tic/toc
35 * inside the solver's own runtime.
36 */
37export function timedRunLines(
38 call: string,
39 timesVar: string,
40 policy: TimingPolicy
41): string[] {
42 const { minTimedRuns: min, maxTimedRuns: max, timeBudgetSeconds: budget } = policy;
43 return [
44 `${timesVar} = zeros(${max}, 1);`,
45 "res_nrun = 0;",
46 "res_tot = 0;",
47 `while res_nrun < ${max} && (res_nrun < ${min} || res_tot < ${budget})`,
48 ` tic; ${call}; res_one = toc;`,
49 " res_nrun = res_nrun + 1;",
50 ` ${timesVar}(res_nrun) = res_one;`,
51 " res_tot = res_tot + res_one;",
52 "end",
53 `${timesVar} = ${timesVar}(1:res_nrun);`,
54 ];
moveopenescclose