rustsim 0.0.1

High-performance agent-based modelling engine - top-level orchestration crate
Documentation
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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
//! Authoritative columnar phase runtime.
//!
//! [`ColumnarRuntime`] is the production-oriented execution path for large
//! populations. Unlike [`rustsim_core::standard::StandardModel`], which keeps
//! agents in an AoS store and steps them one at a time, this runtime owns a
//! persistent [`DeviceSoaStore`] and treats the columnar buffers as the source
//! of truth. AoS stores are upload/download boundaries only.
//!
//! The runtime executes explicit ordered phases. CPU phases operate directly on
//! resident SoA columns; CUDA-resident phases launch kernels against persistent
//! device buffers and only synchronize back to host when a CPU phase or explicit
//! download requires it.

use crate::device_store::{DeviceSoaMutationError, DeviceSoaStore, DeviceSoaStoreF64};
#[cfg(feature = "cuda")]
use crate::device_store::{SoaColumnSchema, SoaColumnType};
use crate::interaction::{DeviceSpatialConfig2D, DeviceSpatialError, DeviceSpatialIndex2D};
use rustsim_core::soa::{SoaExtractable, SoaExtractableF64};
use rustsim_core::store::AgentStore;
use rustsim_core::types::AgentId;
use thiserror::Error;

/// Errors returned by [`ColumnarRuntime`].
#[derive(Debug, Error)]
pub enum ColumnarRuntimeError {
    /// A caller supplied an invalid runtime configuration.
    #[error("invalid columnar runtime configuration: {0}")]
    InvalidConfig(String),

    /// A lifecycle mutation would make the authoritative SoA state invalid.
    #[error("columnar runtime mutation failed: {0}")]
    Mutation(#[from] DeviceSoaMutationError),

    /// A spatial interaction index or phase was configured incorrectly.
    #[error("columnar runtime spatial interaction failed: {0}")]
    Spatial(#[from] DeviceSpatialError),

    /// CUDA initialization, launch, or synchronization failed.
    #[cfg(feature = "cuda")]
    #[error("cuda resident runtime error: {0}")]
    Cuda(String),
}

/// Owned `f32` SoA rows used by production lifecycle commands.
#[derive(Debug, Clone, PartialEq)]
pub struct ColumnarAgentBatch {
    /// Agent IDs in the same row order as every column.
    pub ids: Vec<AgentId>,
    /// Column data in SoA layout. `columns[c][i]` belongs to `ids[i]`.
    pub columns: Vec<Vec<f32>>,
}

impl ColumnarAgentBatch {
    /// Build an owned insert payload.
    pub fn new(ids: Vec<AgentId>, columns: Vec<Vec<f32>>) -> Self {
        Self { ids, columns }
    }

    fn column_refs(&self) -> Vec<&[f32]> {
        self.columns.iter().map(Vec::as_slice).collect()
    }
}

/// Owned `f64` SoA rows used by double-precision lifecycle commands.
#[derive(Debug, Clone, PartialEq)]
pub struct ColumnarAgentBatchF64 {
    /// Agent IDs in the same row order as every column.
    pub ids: Vec<AgentId>,
    /// Column data in SoA layout. `columns[c][i]` belongs to `ids[i]`.
    pub columns: Vec<Vec<f64>>,
}

impl ColumnarAgentBatchF64 {
    /// Build an owned insert payload.
    pub fn new(ids: Vec<AgentId>, columns: Vec<Vec<f64>>) -> Self {
        Self { ids, columns }
    }

    fn column_refs(&self) -> Vec<&[f64]> {
        self.columns.iter().map(Vec::as_slice).collect()
    }
}

/// Authoritative lifecycle mutation for a columnar runtime.
#[derive(Debug, Clone, PartialEq)]
pub enum ColumnarLifecycleCommand {
    /// Append new rows to the authoritative SoA state.
    Insert(ColumnarAgentBatch),
    /// Remove existing agent IDs from the authoritative SoA state.
    Remove(Vec<AgentId>),
    /// Validate then remove and insert as one runtime mutation.
    Replace {
        /// Existing IDs to remove before appending replacement rows.
        remove_ids: Vec<AgentId>,
        /// New rows to append after removals are validated.
        insert: ColumnarAgentBatch,
    },
}

/// Double-precision lifecycle mutation for a columnar runtime.
#[derive(Debug, Clone, PartialEq)]
pub enum ColumnarLifecycleCommandF64 {
    /// Append new rows to the authoritative SoA state.
    Insert(ColumnarAgentBatchF64),
    /// Remove existing agent IDs from the authoritative SoA state.
    Remove(Vec<AgentId>),
    /// Validate then remove and insert as one runtime mutation.
    Replace {
        /// Existing IDs to remove before appending replacement rows.
        remove_ids: Vec<AgentId>,
        /// New rows to append after removals are validated.
        insert: ColumnarAgentBatchF64,
    },
}

/// Summary returned after a lifecycle command mutates authoritative SoA state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ColumnarLifecycleReport {
    /// Number of agents removed by the command.
    pub removed: usize,
    /// Number of agents inserted by the command.
    pub inserted: usize,
    /// Active agent count after the command.
    pub agent_count: usize,
}

/// Declarative ABI contract for a CUDA-resident kernel phase.
#[cfg(feature = "cuda")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CudaKernelAbi {
    /// Number of SoA columns the kernel expects before the trailing row count.
    pub columns: usize,
    /// Precision expected for every column.
    pub column_type: SoaColumnType,
}

#[cfg(feature = "cuda")]
impl CudaKernelAbi {
    /// ABI for kernels receiving all columns as `float*` plus a trailing row count.
    pub const fn f32_columns(columns: usize) -> Self {
        Self {
            columns,
            column_type: SoaColumnType::F32,
        }
    }

    fn validate(&self, schema: &[SoaColumnSchema]) -> Result<(), ColumnarRuntimeError> {
        if schema.len() != self.columns {
            return Err(ColumnarRuntimeError::InvalidConfig(format!(
                "CUDA kernel ABI expects {} columns but runtime has {}",
                self.columns,
                schema.len()
            )));
        }
        for (column, entry) in schema.iter().enumerate() {
            if entry.column_type != self.column_type {
                return Err(ColumnarRuntimeError::InvalidConfig(format!(
                    "CUDA kernel ABI expects {:?} at column {} but runtime schema has {:?}",
                    self.column_type, column, entry.column_type
                )));
            }
        }
        Ok(())
    }
}

/// Timing for a single executed phase.
#[derive(Debug, Clone)]
pub struct ColumnarPhaseTiming {
    /// Phase index in the runtime's ordered phase list.
    pub phase_index: usize,
    /// Human-readable phase name.
    pub name: &'static str,
    /// Backend used for this phase.
    pub backend: ColumnarPhaseBackend,
    /// Wall-clock time in microseconds.
    ///
    /// For CUDA-resident phases this is launch-submission time; call
    /// [`ColumnarRuntime::sync_to_host`] or [`ColumnarRuntime::download`] when
    /// host-visible completion is required.
    pub elapsed_us: u128,
}

/// Timing for one complete columnar runtime step.
#[derive(Debug, Clone)]
pub struct ColumnarStepTiming {
    /// Step index before the step was advanced.
    pub step_index: u64,
    /// Number of agents processed.
    pub agent_count: usize,
    /// Per-phase timings.
    pub phases: Vec<ColumnarPhaseTiming>,
    /// Total host-side wall-clock time for the step.
    pub total_us: u128,
}

/// Backend used to execute a phase.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColumnarPhaseBackend {
    /// Single-threaded CPU phase over resident SoA columns.
    Cpu,
    /// Rayon chunked CPU phase over resident SoA columns.
    #[cfg(feature = "rayon")]
    Rayon,
    /// CUDA-resident kernel launch.
    #[cfg(feature = "cuda")]
    CudaResident,
}

/// Runtime whose authoritative state is a persistent SoA store.
pub struct ColumnarRuntime {
    store: DeviceSoaStore,
    phases: Vec<ColumnarPhase>,
    step_index: u64,
    #[cfg(feature = "cuda")]
    cuda_ptx: Option<String>,
    #[cfg(feature = "cuda")]
    cuda_initialized: bool,
}

type CpuPhaseFn = dyn FnMut(&mut [Vec<f32>], usize);
type CpuPhaseFnF64 = dyn FnMut(&mut [Vec<f64>], usize);
type SpatialCpuPhaseFn = dyn FnMut(&DeviceSpatialIndex2D, &mut [Vec<f32>], usize);
type SpatialCpuPhaseFnF64 = dyn FnMut(&DeviceSpatialIndex2D, &mut [Vec<f64>], usize);

enum ColumnarPhase {
    Cpu {
        name: &'static str,
        function: Box<CpuPhaseFn>,
    },
    SpatialCpu2D {
        name: &'static str,
        config: DeviceSpatialConfig2D,
        function: Box<SpatialCpuPhaseFn>,
    },
    #[cfg(feature = "rayon")]
    Rayon {
        name: &'static str,
        chunk_size: usize,
        function: Box<dyn Fn(usize, &mut [&mut [f32]]) + Send + Sync>,
    },
    #[cfg(feature = "cuda")]
    CudaResident {
        name: &'static str,
        kernel_name: String,
        block_size: u32,
    },
}

impl ColumnarRuntime {
    /// Build a columnar runtime by uploading an existing AoS [`AgentStore`].
    ///
    /// After construction, the [`DeviceSoaStore`] inside this runtime is the
    /// authoritative simulation state. Call [`download`](Self::download) only
    /// at observation, checkpoint, or interop boundaries.
    pub fn upload<A, S>(store: &S) -> Self
    where
        A: SoaExtractable,
        S: AgentStore<A>,
    {
        Self::from_device_store(DeviceSoaStore::upload::<A, S>(store))
    }

    /// Build a runtime from an existing persistent [`DeviceSoaStore`].
    pub fn from_device_store(store: DeviceSoaStore) -> Self {
        Self {
            store,
            phases: Vec::new(),
            step_index: 0,
            #[cfg(feature = "cuda")]
            cuda_ptx: None,
            #[cfg(feature = "cuda")]
            cuda_initialized: false,
        }
    }

    /// Consume the runtime and return its authoritative store.
    pub fn into_device_store(self) -> DeviceSoaStore {
        self.store
    }

    /// Replace the authoritative store and step index while preserving phases.
    ///
    /// This is the restore boundary used by the production control plane:
    /// configured CPU/rayon/CUDA phases remain attached to the runtime, while
    /// the simulation state is rewound to a checkpoint. CUDA-resident state is
    /// reinitialized lazily on the next CUDA phase.
    pub fn restore_device_store(&mut self, store: DeviceSoaStore, step_index: u64) {
        self.store = store;
        self.step_index = step_index;
        #[cfg(feature = "cuda")]
        {
            self.cuda_initialized = false;
        }
    }

    /// Access the authoritative store.
    pub fn device_store(&self) -> &DeviceSoaStore {
        &self.store
    }

    /// Mutably access the authoritative store.
    ///
    /// When CUDA residency is active, prefer the runtime's phase and scatter
    /// methods so host/device dirty flags stay coordinated.
    pub fn device_store_mut(&mut self) -> &mut DeviceSoaStore {
        #[cfg(feature = "cuda")]
        self.store.mark_host_dirty();
        &mut self.store
    }

    /// Current step index.
    pub fn step_index(&self) -> u64 {
        self.step_index
    }

    /// Number of active agents.
    pub fn agent_count(&self) -> usize {
        self.store.agent_count()
    }

    /// Number of SoA columns.
    pub fn num_columns(&self) -> usize {
        self.store.num_columns()
    }

    /// Agent IDs in current row order.
    pub fn ids(&self) -> &[AgentId] {
        self.store.ids()
    }

    /// Return the current row for `id`, if present.
    pub fn row_of(&self, id: AgentId) -> Option<usize> {
        self.store.row_of(id)
    }

    /// Whether the authoritative runtime state contains `id`.
    pub fn contains_id(&self, id: AgentId) -> bool {
        self.store.contains_id(id)
    }

    /// Smallest non-negative [`AgentId`] not currently present in the runtime.
    pub fn next_available_id(&self) -> AgentId {
        self.store.next_available_id()
    }

    /// Column names supplied by [`SoaExtractable`].
    pub fn column_names(&self) -> &[&'static str] {
        self.store.column_names()
    }

    /// Add a serial CPU phase.
    pub fn add_cpu_phase(
        &mut self,
        name: &'static str,
        function: impl FnMut(&mut [Vec<f32>], usize) + 'static,
    ) {
        self.phases.push(ColumnarPhase::Cpu {
            name,
            function: Box::new(function),
        });
    }

    /// Add a 2-D spatial CPU phase over authoritative SoA state.
    ///
    /// Each step rebuilds a row-ordered spatial index from the configured
    /// coordinate columns, then invokes `function(index, columns, n)`. This is
    /// the production replacement for AoS-side neighbor/message systems when a
    /// run is owned by `ColumnarRuntime`.
    pub fn add_spatial_cpu_phase_2d(
        &mut self,
        name: &'static str,
        config: DeviceSpatialConfig2D,
        function: impl FnMut(&DeviceSpatialIndex2D, &mut [Vec<f32>], usize) + 'static,
    ) -> Result<(), ColumnarRuntimeError> {
        DeviceSpatialIndex2D::build_f32(&self.store, config)?;
        self.phases.push(ColumnarPhase::SpatialCpu2D {
            name,
            config,
            function: Box::new(function),
        });
        Ok(())
    }

    /// Build a 2-D spatial index from the current authoritative SoA columns.
    pub fn spatial_index_2d(
        &mut self,
        config: DeviceSpatialConfig2D,
    ) -> Result<DeviceSpatialIndex2D, ColumnarRuntimeError> {
        self.sync_to_host()?;
        Ok(DeviceSpatialIndex2D::build_f32(&self.store, config)?)
    }

    /// Add a chunked Rayon CPU phase.
    #[cfg(feature = "rayon")]
    pub fn add_rayon_phase(
        &mut self,
        name: &'static str,
        chunk_size: usize,
        function: impl Fn(usize, &mut [&mut [f32]]) + Send + Sync + 'static,
    ) -> Result<(), ColumnarRuntimeError> {
        if chunk_size == 0 {
            return Err(ColumnarRuntimeError::InvalidConfig(
                "chunk_size must be positive".to_string(),
            ));
        }
        self.phases.push(ColumnarPhase::Rayon {
            name,
            chunk_size,
            function: Box::new(function),
        });
        Ok(())
    }

    /// Set the PTX module used by subsequent CUDA-resident phases.
    #[cfg(feature = "cuda")]
    pub fn set_cuda_ptx(&mut self, ptx_source: impl Into<String>) {
        self.cuda_ptx = Some(ptx_source.into());
        self.cuda_initialized = false;
        self.store.release_cuda();
    }

    /// Add a CUDA-resident kernel phase.
    ///
    /// Kernels are launched against the runtime's persistent device columns.
    /// Consecutive CUDA phases are submitted as a single resident sequence,
    /// with no host/device transfer between phases.
    #[cfg(feature = "cuda")]
    pub fn add_cuda_resident_phase(
        &mut self,
        name: &'static str,
        kernel_name: impl Into<String>,
        block_size: u32,
    ) -> Result<(), ColumnarRuntimeError> {
        if block_size == 0 {
            return Err(ColumnarRuntimeError::InvalidConfig(
                "block_size must be positive".to_string(),
            ));
        }
        self.phases.push(ColumnarPhase::CudaResident {
            name,
            kernel_name: kernel_name.into(),
            block_size,
        });
        Ok(())
    }

    /// Add a CUDA-resident phase after validating its declared ABI against the runtime schema.
    #[cfg(feature = "cuda")]
    pub fn add_cuda_resident_phase_checked(
        &mut self,
        name: &'static str,
        kernel_name: impl Into<String>,
        block_size: u32,
        abi: CudaKernelAbi,
    ) -> Result<(), ColumnarRuntimeError> {
        abi.validate(self.store.schema())?;
        self.add_cuda_resident_phase(name, kernel_name, block_size)
    }

    /// Number of configured phases.
    pub fn phase_count(&self) -> usize {
        self.phases.len()
    }

    /// Execute one ordered phase step.
    pub fn step(&mut self) -> Result<ColumnarStepTiming, ColumnarRuntimeError> {
        let total_start = std::time::Instant::now();
        let step_index = self.step_index;
        let agent_count = self.agent_count();
        let mut timings = Vec::with_capacity(self.phases.len());

        let mut phase_index = 0;
        while phase_index < self.phases.len() {
            match &mut self.phases[phase_index] {
                ColumnarPhase::Cpu { name, function } => {
                    #[cfg(feature = "cuda")]
                    self.store
                        .sync_to_host()
                        .map_err(ColumnarRuntimeError::Cuda)?;

                    let phase_start = std::time::Instant::now();
                    function(self.store.columns_mut(), agent_count);
                    #[cfg(feature = "cuda")]
                    self.store.mark_host_dirty();
                    timings.push(ColumnarPhaseTiming {
                        phase_index,
                        name,
                        backend: ColumnarPhaseBackend::Cpu,
                        elapsed_us: phase_start.elapsed().as_micros(),
                    });
                    phase_index += 1;
                }
                ColumnarPhase::SpatialCpu2D {
                    name,
                    config,
                    function,
                } => {
                    #[cfg(feature = "cuda")]
                    self.store
                        .sync_to_host()
                        .map_err(ColumnarRuntimeError::Cuda)?;

                    let phase_start = std::time::Instant::now();
                    let index = DeviceSpatialIndex2D::build_f32(&self.store, *config)?;
                    function(&index, self.store.columns_mut(), agent_count);
                    #[cfg(feature = "cuda")]
                    self.store.mark_host_dirty();
                    timings.push(ColumnarPhaseTiming {
                        phase_index,
                        name,
                        backend: ColumnarPhaseBackend::Cpu,
                        elapsed_us: phase_start.elapsed().as_micros(),
                    });
                    phase_index += 1;
                }
                #[cfg(feature = "rayon")]
                ColumnarPhase::Rayon {
                    name,
                    chunk_size,
                    function,
                } => {
                    #[cfg(feature = "cuda")]
                    self.store
                        .sync_to_host()
                        .map_err(ColumnarRuntimeError::Cuda)?;

                    let phase_start = std::time::Instant::now();
                    {
                        let mut slices: Vec<&mut [f32]> = self
                            .store
                            .columns_mut()
                            .iter_mut()
                            .map(|column| column.as_mut_slice())
                            .collect();
                        let function_ref: &(dyn Fn(usize, &mut [&mut [f32]]) + Send + Sync) =
                            &**function;
                        crate::parallel::par_apply_chunks_multi(
                            &mut slices,
                            *chunk_size,
                            |start, chunk| function_ref(start, chunk),
                        );
                    }
                    #[cfg(feature = "cuda")]
                    self.store.mark_host_dirty();
                    timings.push(ColumnarPhaseTiming {
                        phase_index,
                        name,
                        backend: ColumnarPhaseBackend::Rayon,
                        elapsed_us: phase_start.elapsed().as_micros(),
                    });
                    phase_index += 1;
                }
                #[cfg(feature = "cuda")]
                ColumnarPhase::CudaResident { .. } => {
                    let start_index = phase_index;
                    let mut names = Vec::new();
                    let mut kernels = Vec::new();
                    let mut block_size = None;

                    while phase_index < self.phases.len() {
                        match &self.phases[phase_index] {
                            ColumnarPhase::CudaResident {
                                name,
                                kernel_name,
                                block_size: phase_block_size,
                            } => {
                                if let Some(expected) = block_size {
                                    if expected != *phase_block_size {
                                        break;
                                    }
                                } else {
                                    block_size = Some(*phase_block_size);
                                }
                                names.push(*name);
                                kernels.push(kernel_name.clone());
                                phase_index += 1;
                            }
                            _ => break,
                        }
                    }

                    let elapsed = self.run_cuda_sequence(&kernels, block_size.unwrap_or(256))?;
                    for (offset, (name, elapsed_us)) in names.into_iter().zip(elapsed).enumerate() {
                        timings.push(ColumnarPhaseTiming {
                            phase_index: start_index + offset,
                            name,
                            backend: ColumnarPhaseBackend::CudaResident,
                            elapsed_us,
                        });
                    }
                }
            }
        }

        self.step_index = self.step_index.saturating_add(1);
        Ok(ColumnarStepTiming {
            step_index,
            agent_count,
            phases: timings,
            total_us: total_start.elapsed().as_micros(),
        })
    }

    /// Execute `steps` ordered phase steps.
    pub fn run(&mut self, steps: usize) -> Result<Vec<ColumnarStepTiming>, ColumnarRuntimeError> {
        let mut timings = Vec::with_capacity(steps);
        for _ in 0..steps {
            timings.push(self.step()?);
        }
        Ok(timings)
    }

    /// Synchronize device-resident data back to host columns when CUDA is active.
    pub fn sync_to_host(&mut self) -> Result<(), ColumnarRuntimeError> {
        #[cfg(feature = "cuda")]
        {
            self.store
                .sync_to_host()
                .map_err(ColumnarRuntimeError::Cuda)?;
        }
        Ok(())
    }

    /// Download the authoritative columnar state into an AoS [`AgentStore`].
    pub fn download<A, S>(&mut self, store: &S) -> Result<(), ColumnarRuntimeError>
    where
        A: SoaExtractable,
        S: AgentStore<A>,
    {
        self.sync_to_host()?;
        self.store.download::<A, S>(store);
        Ok(())
    }

    /// Remove agents from the authoritative SoA state.
    pub fn scatter_remove(&mut self, dead_ids: &[AgentId]) -> Result<(), ColumnarRuntimeError> {
        self.sync_to_host()?;
        self.store.try_scatter_remove(dead_ids)?;
        #[cfg(feature = "cuda")]
        self.store.mark_host_dirty();
        Ok(())
    }

    /// Append agents to the authoritative SoA state.
    pub fn scatter_insert(
        &mut self,
        new_ids: &[AgentId],
        new_columns: &[&[f32]],
    ) -> Result<(), ColumnarRuntimeError> {
        self.sync_to_host()?;
        self.store.try_scatter_insert(new_ids, new_columns)?;
        #[cfg(feature = "cuda")]
        self.store.mark_host_dirty();
        Ok(())
    }

    /// Apply an authoritative lifecycle command to the production SoA runtime.
    ///
    /// This is the preferred mutation boundary for production runs: all IDs
    /// and column shapes are validated before state changes, CUDA-resident
    /// state is synchronized once, and the returned report records the new
    /// active population size.
    pub fn apply_lifecycle(
        &mut self,
        command: ColumnarLifecycleCommand,
    ) -> Result<ColumnarLifecycleReport, ColumnarRuntimeError> {
        self.sync_to_host()?;
        let (removed, inserted) = match command {
            ColumnarLifecycleCommand::Insert(batch) => {
                let refs = batch.column_refs();
                let inserted = batch.ids.len();
                self.store.try_scatter_insert(&batch.ids, &refs)?;
                (0, inserted)
            }
            ColumnarLifecycleCommand::Remove(ids) => {
                let removed = ids.len();
                self.store.try_scatter_remove(&ids)?;
                (removed, 0)
            }
            ColumnarLifecycleCommand::Replace { remove_ids, insert } => {
                let refs = insert.column_refs();
                let removed = remove_ids.len();
                let inserted = insert.ids.len();
                self.store
                    .try_scatter_replace(&remove_ids, &insert.ids, &refs)?;
                (removed, inserted)
            }
        };
        #[cfg(feature = "cuda")]
        self.store.mark_host_dirty();
        Ok(ColumnarLifecycleReport {
            removed,
            inserted,
            agent_count: self.agent_count(),
        })
    }

    /// Apply several lifecycle commands in order.
    pub fn apply_lifecycle_batch<I>(
        &mut self,
        commands: I,
    ) -> Result<Vec<ColumnarLifecycleReport>, ColumnarRuntimeError>
    where
        I: IntoIterator<Item = ColumnarLifecycleCommand>,
    {
        let mut reports = Vec::new();
        for command in commands {
            reports.push(self.apply_lifecycle(command)?);
        }
        Ok(reports)
    }

    #[cfg(feature = "cuda")]
    fn run_cuda_sequence(
        &mut self,
        kernels: &[String],
        block_size: u32,
    ) -> Result<Vec<u128>, ColumnarRuntimeError> {
        if kernels.is_empty() {
            return Ok(Vec::new());
        }
        let ptx = self.cuda_ptx.as_deref().ok_or_else(|| {
            ColumnarRuntimeError::InvalidConfig(
                "set_cuda_ptx must be called before CUDA resident phases execute".to_string(),
            )
        })?;
        if !self.cuda_initialized || !self.store.has_cuda_resident() {
            self.store
                .init_cuda(ptx, &kernels[0])
                .map_err(ColumnarRuntimeError::Cuda)?;
            self.cuda_initialized = true;
        }

        let kernel_refs: Vec<&str> = kernels.iter().map(String::as_str).collect();
        self.store
            .run_sequence_cuda_resident(&kernel_refs, block_size)
            .map(|items| items.into_iter().map(|(_, elapsed)| elapsed).collect())
            .map_err(ColumnarRuntimeError::Cuda)
    }
}

/// Authoritative double-precision columnar runtime.
pub struct ColumnarRuntimeF64 {
    store: DeviceSoaStoreF64,
    phases: Vec<ColumnarPhaseF64>,
    step_index: u64,
}

enum ColumnarPhaseF64 {
    Cpu {
        name: &'static str,
        function: Box<CpuPhaseFnF64>,
    },
    SpatialCpu2D {
        name: &'static str,
        config: DeviceSpatialConfig2D,
        function: Box<SpatialCpuPhaseFnF64>,
    },
}

impl ColumnarRuntimeF64 {
    /// Build a double-precision columnar runtime by uploading an AoS store.
    pub fn upload<A, S>(store: &S) -> Self
    where
        A: SoaExtractableF64,
        S: AgentStore<A>,
    {
        Self::from_device_store(DeviceSoaStoreF64::upload::<A, S>(store))
    }

    /// Build a runtime from an existing persistent f64 SoA store.
    pub fn from_device_store(store: DeviceSoaStoreF64) -> Self {
        Self {
            store,
            phases: Vec::new(),
            step_index: 0,
        }
    }

    /// Consume the runtime and return its authoritative store.
    pub fn into_device_store(self) -> DeviceSoaStoreF64 {
        self.store
    }

    /// Replace the authoritative store and step index while preserving phases.
    pub fn restore_device_store(&mut self, store: DeviceSoaStoreF64, step_index: u64) {
        self.store = store;
        self.step_index = step_index;
    }

    /// Access the authoritative f64 store.
    pub fn device_store(&self) -> &DeviceSoaStoreF64 {
        &self.store
    }

    /// Mutably access the authoritative f64 store.
    pub fn device_store_mut(&mut self) -> &mut DeviceSoaStoreF64 {
        &mut self.store
    }

    /// Current step index.
    pub fn step_index(&self) -> u64 {
        self.step_index
    }

    /// Number of active agents.
    pub fn agent_count(&self) -> usize {
        self.store.agent_count()
    }

    /// Number of SoA columns.
    pub fn num_columns(&self) -> usize {
        self.store.num_columns()
    }

    /// Agent IDs in current row order.
    pub fn ids(&self) -> &[AgentId] {
        self.store.ids()
    }

    /// Return the current row for `id`, if present.
    pub fn row_of(&self, id: AgentId) -> Option<usize> {
        self.store.row_of(id)
    }

    /// Whether the authoritative runtime state contains `id`.
    pub fn contains_id(&self, id: AgentId) -> bool {
        self.store.contains_id(id)
    }

    /// Smallest non-negative [`AgentId`] not currently present in the runtime.
    pub fn next_available_id(&self) -> AgentId {
        self.store.next_available_id()
    }

    /// Column names supplied by `SoaExtractableF64`.
    pub fn column_names(&self) -> &[&'static str] {
        self.store.column_names()
    }

    /// Add a serial CPU phase over f64 columns.
    pub fn add_cpu_phase(
        &mut self,
        name: &'static str,
        function: impl FnMut(&mut [Vec<f64>], usize) + 'static,
    ) {
        self.phases.push(ColumnarPhaseF64::Cpu {
            name,
            function: Box::new(function),
        });
    }

    /// Add a 2-D spatial CPU phase over authoritative f64 SoA state.
    pub fn add_spatial_cpu_phase_2d(
        &mut self,
        name: &'static str,
        config: DeviceSpatialConfig2D,
        function: impl FnMut(&DeviceSpatialIndex2D, &mut [Vec<f64>], usize) + 'static,
    ) -> Result<(), ColumnarRuntimeError> {
        DeviceSpatialIndex2D::build_f64(&self.store, config)?;
        self.phases.push(ColumnarPhaseF64::SpatialCpu2D {
            name,
            config,
            function: Box::new(function),
        });
        Ok(())
    }

    /// Build a 2-D spatial index from the current authoritative f64 SoA columns.
    pub fn spatial_index_2d(
        &self,
        config: DeviceSpatialConfig2D,
    ) -> Result<DeviceSpatialIndex2D, ColumnarRuntimeError> {
        Ok(DeviceSpatialIndex2D::build_f64(&self.store, config)?)
    }

    /// Number of configured phases.
    pub fn phase_count(&self) -> usize {
        self.phases.len()
    }

    /// Execute one ordered double-precision phase step.
    pub fn step(&mut self) -> Result<ColumnarStepTiming, ColumnarRuntimeError> {
        let total_start = std::time::Instant::now();
        let step_index = self.step_index;
        let agent_count = self.agent_count();
        let mut timings = Vec::with_capacity(self.phases.len());

        for (phase_index, phase) in self.phases.iter_mut().enumerate() {
            match phase {
                ColumnarPhaseF64::Cpu { name, function } => {
                    let phase_start = std::time::Instant::now();
                    function(self.store.columns_mut(), agent_count);
                    timings.push(ColumnarPhaseTiming {
                        phase_index,
                        name,
                        backend: ColumnarPhaseBackend::Cpu,
                        elapsed_us: phase_start.elapsed().as_micros(),
                    });
                }
                ColumnarPhaseF64::SpatialCpu2D {
                    name,
                    config,
                    function,
                } => {
                    let phase_start = std::time::Instant::now();
                    let index = DeviceSpatialIndex2D::build_f64(&self.store, *config)?;
                    function(&index, self.store.columns_mut(), agent_count);
                    timings.push(ColumnarPhaseTiming {
                        phase_index,
                        name,
                        backend: ColumnarPhaseBackend::Cpu,
                        elapsed_us: phase_start.elapsed().as_micros(),
                    });
                }
            }
        }

        self.step_index = self.step_index.saturating_add(1);
        Ok(ColumnarStepTiming {
            step_index,
            agent_count,
            phases: timings,
            total_us: total_start.elapsed().as_micros(),
        })
    }

    /// Execute `steps` ordered phase steps.
    pub fn run(&mut self, steps: usize) -> Result<Vec<ColumnarStepTiming>, ColumnarRuntimeError> {
        let mut timings = Vec::with_capacity(steps);
        for _ in 0..steps {
            timings.push(self.step()?);
        }
        Ok(timings)
    }

    /// No-op parity method with the CUDA-enabled f32 runtime.
    pub fn sync_to_host(&mut self) -> Result<(), ColumnarRuntimeError> {
        Ok(())
    }

    /// Download the authoritative f64 columnar state into an AoS `AgentStore`.
    pub fn download<A, S>(&mut self, store: &S) -> Result<(), ColumnarRuntimeError>
    where
        A: SoaExtractableF64,
        S: AgentStore<A>,
    {
        self.store.download::<A, S>(store);
        Ok(())
    }

    /// Remove agents from the authoritative f64 SoA state.
    pub fn scatter_remove(&mut self, dead_ids: &[AgentId]) -> Result<(), ColumnarRuntimeError> {
        self.store.try_scatter_remove(dead_ids)?;
        Ok(())
    }

    /// Append agents to the authoritative f64 SoA state.
    pub fn scatter_insert(
        &mut self,
        new_ids: &[AgentId],
        new_columns: &[&[f64]],
    ) -> Result<(), ColumnarRuntimeError> {
        self.store.try_scatter_insert(new_ids, new_columns)?;
        Ok(())
    }

    /// Apply an authoritative lifecycle command to the double-precision runtime.
    pub fn apply_lifecycle(
        &mut self,
        command: ColumnarLifecycleCommandF64,
    ) -> Result<ColumnarLifecycleReport, ColumnarRuntimeError> {
        let (removed, inserted) = match command {
            ColumnarLifecycleCommandF64::Insert(batch) => {
                let refs = batch.column_refs();
                let inserted = batch.ids.len();
                self.store.try_scatter_insert(&batch.ids, &refs)?;
                (0, inserted)
            }
            ColumnarLifecycleCommandF64::Remove(ids) => {
                let removed = ids.len();
                self.store.try_scatter_remove(&ids)?;
                (removed, 0)
            }
            ColumnarLifecycleCommandF64::Replace { remove_ids, insert } => {
                let refs = insert.column_refs();
                let removed = remove_ids.len();
                let inserted = insert.ids.len();
                self.store
                    .try_scatter_replace(&remove_ids, &insert.ids, &refs)?;
                (removed, inserted)
            }
        };
        Ok(ColumnarLifecycleReport {
            removed,
            inserted,
            agent_count: self.agent_count(),
        })
    }

    /// Apply several double-precision lifecycle commands in order.
    pub fn apply_lifecycle_batch<I>(
        &mut self,
        commands: I,
    ) -> Result<Vec<ColumnarLifecycleReport>, ColumnarRuntimeError>
    where
        I: IntoIterator<Item = ColumnarLifecycleCommandF64>,
    {
        let mut reports = Vec::new();
        for command in commands {
            reports.push(self.apply_lifecycle(command)?);
        }
        Ok(reports)
    }
}