-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcache.html
More file actions
860 lines (738 loc) · 30 KB
/
cache.html
File metadata and controls
860 lines (738 loc) · 30 KB
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Trajectory Caching System - Albert Framework</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
line-height: 1.6;
color: #333;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
h1, h2, h3 {
color: #1976d2;
}
h1 {
font-size: 2.5em;
margin-bottom: 10px;
}
.subtitle {
font-size: 1.2em;
color: #666;
margin-bottom: 30px;
}
.hero-stats {
background: linear-gradient(135deg, #1976d2 0%, #42a5f5 100%);
color: white;
padding: 40px;
border-radius: 12px;
margin-bottom: 40px;
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 30px;
margin-top: 20px;
}
.stat-box {
text-align: center;
}
.stat-number {
font-size: 3em;
font-weight: bold;
display: block;
}
.stat-label {
font-size: 1.1em;
opacity: 0.9;
}
.hero-stats h2 {
color: white;
margin-top: 0;
}
.performance-table {
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.05);
margin-bottom: 30px;
overflow-x: auto;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 12px;
text-align: left;
border-bottom: 1px solid #eee;
}
th {
background-color: #f8f9fa;
font-weight: 600;
color: #1976d2;
}
.speedup {
color: #4caf50;
font-weight: bold;
}
.diagram-container {
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.05);
margin-bottom: 30px;
}
.mermaid {
text-align: center;
}
.code-block {
background: #f8f9fa;
border: 1px solid #e9ecef;
border-radius: 6px;
padding: 20px;
overflow-x: auto;
margin: 20px 0;
white-space: pre-wrap;
}
pre {
margin: 0;
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
font-size: 14px;
white-space: pre-wrap;
}
.feature-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
margin: 30px 0;
}
.feature-card {
background: white;
padding: 25px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.05);
}
.feature-card h3 {
margin-top: 0;
color: #1976d2;
}
.alert {
background: #e3f2fd;
border-left: 4px solid #1976d2;
padding: 15px 20px;
margin: 20px 0;
border-radius: 4px;
}
.alert-title {
font-weight: bold;
margin-bottom: 5px;
}
.implementation-section {
background: #f8f9fa;
padding: 30px;
border-radius: 8px;
margin: 30px 0;
}
.performance-chart {
margin: 30px 0;
text-align: center;
}
.highlight {
background: #fff3cd;
padding: 2px 6px;
border-radius: 3px;
}
.cache-key-example {
background: #1e1e1e;
color: #d4d4d4;
padding: 20px;
border-radius: 8px;
overflow-x: auto;
font-family: 'Courier New', monospace;
font-size: 13px;
margin: 15px 0;
}
.comment { color: #6a9955; }
.string { color: #ce9178; }
.keyword { color: #569cd6; }
.number { color: #b5cea8; }
.advanced-features {
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.05);
margin-bottom: 30px;
}
.feature-list {
list-style: none;
padding: 0;
}
.feature-list li {
padding: 10px 0;
border-bottom: 1px solid #f0f0f0;
position: relative;
padding-left: 30px;
}
.feature-list li::before {
content: '✓';
position: absolute;
left: 0;
color: #4caf50;
font-weight: bold;
}
#cache-flow {
width: 100%;
height: 400px;
}
/* Mobile responsive styles */
@media (max-width: 768px) {
body {
padding: 10px;
font-size: 14px;
}
h1 {
font-size: 1.8em;
}
h2 {
font-size: 1.4em;
}
h3 {
font-size: 1.2em;
}
.subtitle {
font-size: 1em;
}
.hero-stats {
padding: 25px;
}
.stats-grid {
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 20px;
}
.stat-number {
font-size: 2em;
}
.stat-label {
font-size: 0.9em;
}
.performance-table {
padding: 20px;
margin-bottom: 20px;
overflow-x: auto;
}
table {
font-size: 12px;
min-width: 500px;
}
th, td {
padding: 8px;
}
.diagram-container {
padding: 20px;
margin-bottom: 20px;
}
.code-block, .cache-key-example {
padding: 15px;
font-size: 12px;
}
pre {
font-size: 12px;
}
.feature-grid {
grid-template-columns: 1fr;
gap: 15px;
margin: 20px 0;
}
.feature-card {
padding: 20px;
}
.alert {
padding: 12px 15px;
margin: 15px 0;
}
.implementation-section {
padding: 20px;
margin: 20px 0;
}
.advanced-features {
padding: 20px;
margin-bottom: 20px;
}
#cache-flow {
height: 300px;
}
.highlight {
padding: 2px 4px;
font-size: 0.9em;
}
}
@media (max-width: 480px) {
body {
padding: 8px;
font-size: 13px;
}
h1 {
font-size: 1.5em;
}
h2 {
font-size: 1.2em;
}
.hero-stats {
padding: 20px;
}
.stat-number {
font-size: 1.5em;
}
.performance-table {
padding: 15px;
}
table {
font-size: 11px;
}
th, td {
padding: 6px;
}
.diagram-container, .feature-card, .advanced-features {
padding: 15px;
}
.code-block, .cache-key-example {
padding: 10px;
font-size: 11px;
}
pre {
font-size: 11px;
}
#cache-flow {
height: 250px;
}
.implementation-section {
padding: 15px;
}
}
</style>
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
<script>mermaid.initialize({startOnLoad:true});</script>
</head>
<body>
<h1>Trajectory Caching System</h1>
<p class="subtitle">PyTorch-Powered Performance Acceleration for Gravitational Physics Simulations</p>
<div class="hero-stats">
<h2>Performance vs. Geodesic Integration</h2>
<div class="stats-grid">
<div class="stat-box">
<span class="stat-number">29,323x</span>
<span class="stat-label">Faster than RK8 Integration</span>
</div>
<div class="stat-box">
<span class="stat-number">8.6ms</span>
<span class="stat-label">vs 4+ min computation</span>
</div>
<div class="stat-box">
<span class="stat-number">13,702x</span>
<span class="stat-label">Average Speedup</span>
</div>
<div class="stat-box">
<span class="stat-number">30.6MB</span>
<span class="stat-label">Cache for 1M Steps</span>
</div>
</div>
</div>
<h2>Overview</h2>
<p>The Albert framework's trajectory caching system leverages PyTorch's efficient tensor serialization to provide dramatic performance improvements over direct geodesic integration. By intelligently caching computed trajectories, we transform multi-minute calculations into millisecond operations.</p>
<div class="performance-table">
<h3>Cache Performance vs. Geodesic Integration</h3>
<table>
<thead>
<tr>
<th>Trajectory Steps</th>
<th>Geodesic Integration<br/>(First Run)</th>
<th>Cached Load<br/>(Subsequent Runs)</th>
<th>Speedup</th>
<th>Cache Size</th>
</tr>
</thead>
<tbody>
<tr>
<td>10,000</td>
<td>2.75 s<br/><small>RK8 integration</small></td>
<td>2.5 ms<br/><small>PyTorch load</small></td>
<td class="speedup">1,109.9x</td>
<td>~0.3 MB</td>
</tr>
<tr>
<td>100,000</td>
<td>25.80 s<br/><small>RK8 integration</small></td>
<td>2.4 ms<br/><small>PyTorch load</small></td>
<td class="speedup">10,673.7x</td>
<td>~3.1 MB</td>
</tr>
<tr>
<td>1,000,000</td>
<td>4m 12.7s<br/><small>RK8 integration</small></td>
<td>8.6 ms<br/><small>PyTorch load</small></td>
<td class="speedup">29,323.3x</td>
<td>~30.6 MB</td>
</tr>
</tbody>
</table>
<p style="margin-top: 15px; font-size: 0.9em; color: #666;">
<strong>Note:</strong> Integration times are from solving the full geodesic equations using 8th-order Dormand-Prince
with adaptive step sizing and constraint preservation.
</p>
</div>
<h2>How It Works</h2>
<div class="diagram-container">
<h3>Enhanced Cache Architecture</h3>
<canvas id="cache-flow"></canvas>
</div>
<h2>What Computation Does the Cache Avoid?</h2>
<div class="implementation-section">
<h3>The Expensive Geodesic Integration Process</h3>
<p>When computing trajectories without caching, the system must perform the following expensive operations:</p>
<div class="feature-grid">
<div class="feature-card">
<h3>🔢 RK8 Integration Steps</h3>
<p>For each of the 10,000 to 1,000,000+ time steps:</p>
<ul>
<li>Evaluate metric tensor g<sub>μν</sub>(r,θ,φ)</li>
<li>Compute 13 RK8 sub-steps</li>
<li>Update position and velocity</li>
<li>Adaptive step size control</li>
</ul>
<p><strong>Cost:</strong> O(N) where N = number of steps</p>
</div>
<div class="feature-card">
<h3>📐 Christoffel Symbols</h3>
<p>At each integration step, compute:</p>
<ul>
<li>40 independent Christoffel symbols</li>
<li>Γ<sup>μ</sup><sub>νρ</sub> = ½g<sup>μσ</sup>(∂<sub>ν</sub>g<sub>σρ</sub> + ...)</li>
<li>Metric derivatives via autograd</li>
<li>Tensor contractions</li>
</ul>
<p><strong>Cost:</strong> 40 × N tensor operations</p>
</div>
<div class="feature-card">
<h3>⚖️ Constraint Preservation</h3>
<p>Maintain physical constraints:</p>
<ul>
<li>Energy conservation: E = constant</li>
<li>Angular momentum: L<sub>z</sub> = constant</li>
<li>4-velocity normalization: g<sub>μν</sub>u<sup>μ</sup>u<sup>ν</sup> = -c²</li>
<li>Numerical stability checks</li>
</ul>
<p><strong>Cost:</strong> Additional computations per step</p>
</div>
</div>
<div class="alert" style="margin-top: 20px;">
<div class="alert-title">Computational Complexity</div>
<p>A 1-million step trajectory requires:</p>
<ul style="margin-top: 10px;">
<li>• 1,000,000 metric evaluations</li>
<li>• 13,000,000 RK8 sub-steps</li>
<li>• 40,000,000 Christoffel symbol computations</li>
<li>• Continuous constraint checking</li>
</ul>
<p style="margin-top: 10px;">The cache replaces all of this with a <strong>single 8.6ms tensor load</strong> from disk!</p>
</div>
</div>
<h2>Enhanced Cache Key Generation</h2>
<div class="advanced-features">
<h3>Intelligent Parameter Hashing</h3>
<p>The updated cache system (cache.py) implements sophisticated key generation that includes:</p>
<div class="cache-key-example">
<span class="comment"># Example cache filename for quantum-corrected Schwarzschild:</span>
Schwarzschild_electron_a7f3b2c1d9e4f6_steps_1000000.pt
<span class="comment">│ │ │ │</span>
<span class="comment">│ │ │ └── Number of steps (allows partial loading)</span>
<span class="comment">│ │ └──────────────────── SHA256 hash of all parameters (16 chars)</span>
<span class="comment">│ └────────────────────────────── Particle name for easy identification</span>
<span class="comment">└──────────────────────────────────────────── Sanitized theory name</span>
</div>
<h4>Parameters Included in Hash:</h4>
<ul class="feature-list">
<li><strong>Core Parameters:</strong> r0, dtau, dtype, particle properties (mass, charge, spin)</li>
<li><strong>Theory Identification:</strong> Theory name, module, class, and metric-specific parameters</li>
<li><strong>Numerical Constants:</strong> All thresholds and integration factors that affect computation</li>
<li><strong>Physical Constants:</strong> M_si, c_si, G_si for unit conversions</li>
<li><strong>Black Hole Configuration:</strong> Preset name and mass parameters</li>
<li><strong>Quantum Parameters:</strong> quantum_interval, quantum_beta for QFT corrections</li>
<li><strong>Software Version:</strong> Ensures cache invalidation on framework updates</li>
</ul>
</div>
<div class="implementation-section">
<h3>Advanced Cache Features</h3>
<div class="feature-grid">
<div class="feature-card">
<h3>🔄 Partial Trajectory Loading</h3>
<p>Load segments from longer cached trajectories:</p>
<div class="code-block">
<pre><span class="comment"># Request 10k steps, but 100k trajectory exists</span>
<span class="comment"># Cache automatically loads and truncates</span>
trajectory = cache.load_trajectory(
cache_path, device,
max_steps=<span class="number">10000</span> <span class="comment"># Only load first 10k</span>
)</pre>
</div>
</div>
<div class="feature-card">
<h3>📊 Metadata Storage</h3>
<p>Performance metrics saved alongside trajectories:</p>
<div class="code-block">
<pre>metadata = {
<span class="string">'computation_time'</span>: <span class="number">252.7</span>,
<span class="string">'theory_name'</span>: <span class="string">'Schwarzschild'</span>,
<span class="string">'convergence_error'</span>: <span class="number">1.2e-13</span>,
<span class="string">'energy_drift'</span>: <span class="number">3.8e-14</span>
}</pre>
</div>
</div>
<div class="feature-card">
<h3>🗂️ Hierarchical Organization</h3>
<p>Cache organized by black hole preset:</p>
<div class="code-block">
<pre>cache/trajectories/1.0.0/
├── stellar_massive/
│ ├── Schwarzschild_*.pt
│ └── Kerr_*.pt
├── primordial_mini/
│ └── quantum_theories_*.pt
└── supermassive/
└── agn_models_*.pt</pre>
</div>
</div>
</div>
</div>
<h2>PyTorch Integration</h2>
<div class="implementation-section">
<h3>Optimized Tensor Serialization</h3>
<p>The caching system leverages PyTorch's native tensor serialization for maximum efficiency:</p>
<div class="code-block">
<pre><span class="keyword">class</span> TrajectoryCache:
<span class="keyword">def</span> save_trajectory(self, trajectory: Tensor, cache_path: str,
dtype: torch.dtype, metadata: Optional[Dict] = None):
<span class="string">"""Save with optional metadata for analysis."""</span>
<span class="comment"># Convert to specified precision</span>
torch.save(trajectory.to(dtype=dtype), cache_path)
<span class="comment"># Save companion metadata file</span>
<span class="keyword">if</span> metadata:
metadata_path = cache_path.replace(<span class="string">'.pt'</span>, <span class="string">'_metadata.json'</span>)
<span class="keyword">with</span> open(metadata_path, <span class="string">'w'</span>) <span class="keyword">as</span> f:
json.dump(metadata, f, indent=<span class="number">2</span>)
<span class="keyword">def</span> load_trajectory(self, cache_path: str, device: torch.device,
max_steps: Optional[int] = None):
<span class="string">"""Load with automatic device placement and truncation."""</span>
trajectory = torch.load(cache_path, map_location=device)
<span class="comment"># Return only requested steps</span>
<span class="keyword">if</span> max_steps and trajectory.shape[<span class="number">0</span>] > max_steps:
<span class="keyword">return</span> trajectory[:max_steps]
<span class="keyword">return</span> trajectory</pre>
</div>
</div>
<h2>Real-World Impact</h2>
<div class="performance-table">
<h3>Use Case Performance Gains</h3>
<table>
<thead>
<tr>
<th>Use Case</th>
<th>Without Cache</th>
<th>With Cache</th>
<th>Benefit</th>
</tr>
</thead>
<tbody>
<tr>
<td>Parameter Sweep (100 variations)</td>
<td>7+ hours<br/><small>100 × full integration</small></td>
<td>4.2 minutes<br/><small>1 × integration + 99 × cache</small></td>
<td>100× faster research</td>
</tr>
<tr>
<td>Multi-Validator Testing</td>
<td>Recompute for each test<br/><small>7 validators × 4 min</small></td>
<td>Single computation<br/><small>4 min + 7 × 8.6ms</small></td>
<td>7× speedup</td>
</tr>
<tr>
<td>Interactive Visualization</td>
<td>4+ minute wait<br/><small>per parameter change</small></td>
<td>8.6ms response<br/><small>real-time updates</small></td>
<td>Enables interactivity</td>
</tr>
<tr>
<td>CI/CD Pipeline</td>
<td>Hours per run<br/><small>full test suite</small></td>
<td>Minutes per run<br/><small>cached baselines</small></td>
<td>10× faster builds</td>
</tr>
</tbody>
</table>
</div>
<h2>Cache Management</h2>
<div class="code-block">
<pre><span class="keyword">from</span> physics_agent.cache <span class="keyword">import</span> TrajectoryCache
cache = TrajectoryCache()
<span class="comment"># Get cache statistics</span>
info = cache.get_cache_info(<span class="string">"physics_agent/theories/schwarzschild"</span>)
print(f<span class="string">"Cache size: {info['cache_size_bytes'] / 1e6:.1f} MB"</span>)
print(f<span class="string">"Trajectory steps: {info['cache_properties']['steps']}"</span>)
<span class="comment"># Clear old cache files (outdated naming schemes)</span>
cache.clear_old_cache()
<span class="comment"># Clear all cache if needed</span>
cache.clear_cache(confirm=True)</pre>
</div>
<div class="alert" style="margin-top: 40px;">
<div class="alert-title">Bottom Line</div>
<p>A <strong>30MB cache investment</strong> yields a <strong>29,323× performance return</strong> for million-step trajectories. The intelligent hashing system ensures cache correctness across all parameter variations while the hierarchical organization maintains clarity. For production workloads, the trajectory cache transforms Albert from a research tool into a real-time physics engine.</p>
</div>
<script>
// Cache flow diagram
const canvas = document.getElementById('cache-flow');
const ctx = canvas.getContext('2d');
canvas.width = canvas.offsetWidth;
canvas.height = 400;
// Define cache flow components
const components = [
{name: 'Trajectory Request', x: 50, y: 180, width: 150, height: 60, color: '#4caf50'},
{name: 'Parameter Hash\n(SHA256)', x: 250, y: 100, width: 140, height: 60, color: '#ff9800'},
{name: 'Cache Lookup', x: 250, y: 260, width: 140, height: 60, color: '#ff9800'},
{name: 'Cache Hit', x: 450, y: 100, width: 120, height: 50, color: '#4caf50'},
{name: 'Cache Miss', x: 450, y: 260, width: 120, height: 50, color: '#f44336'},
{name: 'Load Tensor\n(8.6ms)', x: 620, y: 100, width: 120, height: 60, color: '#2196f3'},
{name: 'Compute\n(4+ min)', x: 620, y: 220, width: 120, height: 60, color: '#e74c3c'},
{name: 'Save Cache', x: 620, y: 320, width: 120, height: 50, color: '#9c27b0'},
{name: 'Return Result', x: 850, y: 180, width: 130, height: 60, color: '#1976d2'}
];
const connections = [
{from: 0, to: 1, label: 'Extract params'},
{from: 0, to: 2, label: 'Generate key'},
{from: 1, to: 3, condition: 'exists'},
{from: 2, to: 4, condition: '!exists'},
{from: 3, to: 5},
{from: 4, to: 6},
{from: 5, to: 8},
{from: 6, to: 7},
{from: 7, to: 8},
{from: 6, to: 8, curve: true}
];
// Animation state
let particles = [];
let time = 0;
class CacheParticle {
constructor(path) {
this.path = path;
this.progress = 0;
this.speed = 0.015;
this.color = path.includes(4) ? '#ff5252' : '#4caf50';
}
update() {
this.progress += this.speed;
if (this.progress > 1) {
this.progress = 0;
}
}
draw() {
const conn = connections[this.path];
const from = components[conn.from];
const to = components[conn.to];
let x, y;
if (conn.curve) {
// Curved path for compute->result
const t = this.progress;
const cx = (from.x + to.x) / 2 + 100;
const cy = (from.y + to.y) / 2;
x = (1-t)*(1-t)*from.x + 2*(1-t)*t*cx + t*t*to.x;
y = (1-t)*(1-t)*from.y + 2*(1-t)*t*cy + t*t*to.y;
} else {
x = from.x + from.width/2 + (to.x - from.x - from.width/2 + to.width/2) * this.progress;
y = from.y + from.height/2 + (to.y + to.height/2 - from.y - from.height/2) * this.progress;
}
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(x, y, 5, 0, Math.PI * 2);
ctx.fill();
}
}
// Create initial particles
setInterval(() => {
if (Math.random() > 0.5) {
// Cache hit path
particles.push(new CacheParticle(0));
particles.push(new CacheParticle(2));
particles.push(new CacheParticle(4));
} else {
// Cache miss path
particles.push(new CacheParticle(1));
particles.push(new CacheParticle(3));
particles.push(new CacheParticle(5));
particles.push(new CacheParticle(7));
particles.push(new CacheParticle(8));
}
}, 1000);
function drawCacheFlow() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw connections
ctx.strokeStyle = '#ddd';
ctx.lineWidth = 2;
connections.forEach(conn => {
const from = components[conn.from];
const to = components[conn.to];
ctx.beginPath();
if (conn.curve) {
const cx = (from.x + to.x) / 2 + 100;
const cy = (from.y + to.y) / 2;
ctx.moveTo(from.x + from.width, from.y + from.height/2);
ctx.quadraticCurveTo(cx, cy, to.x, to.y + to.height/2);
} else {
ctx.moveTo(from.x + from.width/2, from.y + from.height/2);
ctx.lineTo(to.x + to.width/2, to.y + to.height/2);
}
ctx.stroke();
// Draw labels
if (conn.label) {
ctx.fillStyle = '#666';
ctx.font = '11px Arial';
const mx = (from.x + from.width/2 + to.x + to.width/2) / 2;
const my = (from.y + from.height/2 + to.y + to.height/2) / 2;
ctx.fillText(conn.label, mx - 30, my - 5);
}
});
// Draw components
components.forEach(comp => {
// Shadow
ctx.fillStyle = 'rgba(0,0,0,0.1)';
ctx.fillRect(comp.x + 3, comp.y + 3, comp.width, comp.height);
// Component
ctx.fillStyle = comp.color;
ctx.fillRect(comp.x, comp.y, comp.width, comp.height);
// Text
ctx.fillStyle = 'white';
ctx.font = 'bold 12px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
const lines = comp.name.split('\n');
lines.forEach((line, i) => {
ctx.fillText(line, comp.x + comp.width/2, comp.y + comp.height/2 + (i - lines.length/2 + 0.5) * 14);
});
});
// Update and draw particles
particles = particles.filter(p => {
p.update();
p.draw();
return p.progress < 1;
});
time++;
requestAnimationFrame(drawCacheFlow);
}
drawCacheFlow();
</script>
</body>
</html>