nandi/vflutter_ffipublic Fork 0
4506a586d7400b00d732dcb97167be75ab41cab1
Commits
Clone
git clone https://git.rickub.com/nandi/vflutter_ffi.git
git clone ssh://git@rickub.com/nandi/vflutter_ffi.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Replace V with Nim 472eb49 · on 4506a586d7400b00d732dcb97167be75ab41cab1 · nandithebull · 8h ago
tile_sweep.dart · 82 lines · 2.8 KBDart Blame HistoryRaw
 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
// Finds where band parallelism stops paying on this machine.
//
//   just sweep
//
// The interesting result is that the best tile count is well above the core
// count. Bands cost very different amounts — rows crossing the set's interior
// run the full iteration cap, rows in open space escape almost immediately —
// and a frame is not finished until its slowest band is. Cutting finer lets a
// free core start the next small band instead of idling, so over-decomposing
// wins even though it cannot add parallelism.
import 'dart:io';

import 'package:nimflutter_ffi/nimflutter_ffi.dart' as v;

const _view = v.FractalView(width: 800, height: 600, maxIter: 500);

Future<void> main() async {
  // Warm the library and the code paths before timing anything.
  v.render(const v.FractalView(width: 64, height: 64, maxIter: 50));

  print('${_view.width}x${_view.height}, maxIter ${_view.maxIter}, '
      '${Platform.numberOfProcessors} logical cores\n');

  final serial = _best(() => v.render(_view));
  print('tiles    best ms   speedup');
  print('${"serial".padRight(8)} ${serial.toString().padRight(9)} 1.00x');

  for (final tiles in [1, 2, 4, 8, 12, 16, 24, 32, 48, 64]) {
    final ms = await _bestAsync(() => v.renderParallel(_view, tiles: tiles));
    print('${tiles.toString().padRight(8)} ${ms.toString().padRight(9)} '
        '${(serial / ms).toStringAsFixed(2)}x');
  }

  print('\nwhy more tiles than cores helps — per-band cost spread:');
  for (final tiles in [8, 32]) {
    _imbalance(tiles);
  }
}

/// Times each band of a [tiles]-way split serially, so the numbers describe
/// the work itself rather than how it happened to be scheduled.
void _imbalance(int tiles) {
  final rows = (_view.height / tiles).ceil();
  final times = <int>[];
  for (var t = 0; t < tiles; t++) {
    final y0 = t * rows;
    final y1 = ((t + 1) * rows).clamp(0, _view.height);
    if (y0 >= y1) continue;
    final sw = Stopwatch()..start();
    v.renderBand(_view, y0, y1);
    sw.stop();
    times.add(sw.elapsedMicroseconds);
  }
  final total = times.reduce((a, b) => a + b);
  final slowest = times.reduce((a, b) => a > b ? a : b);
  final mean = total / times.length;
  print('  $tiles bands: slowest ${(slowest / 1000).toStringAsFixed(1)} ms '
      'vs mean ${(mean / 1000).toStringAsFixed(1)} ms '
      '(${(slowest / mean).toStringAsFixed(1)}x — the frame waits on this)');
}

int _best(void Function() f) {
  var best = 1 << 30;
  for (var i = 0; i < 3; i++) {
    final sw = Stopwatch()..start();
    f();
    sw.stop();
    best = sw.elapsedMilliseconds < best ? sw.elapsedMilliseconds : best;
  }
  return best;
}

Future<int> _bestAsync(Future<void> Function() f) async {
  var best = 1 << 30;
  for (var i = 0; i < 3; i++) {
    final sw = Stopwatch()..start();
    await f();
    sw.stop();
    best = sw.elapsedMilliseconds < best ? sw.elapsedMilliseconds : best;
  }
  return best;
}