srsadmm-core 0.1.0

Core library for the srsadmm project, used to solve consensus ADMM problems with serverless compute.
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
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
extern crate nalgebra as na;

use crate::{
    resource::{ProblemResourceImpl, ResourceLocation, StorageConfig},
    utils::LassoError,
};
use memmap2::Mmap;
use rayon::prelude::*;
use std::{fs::File, io::BufWriter, io::Write, path::Path};

/// A distributed matrix variable for ADMM optimization.
///
/// `MatrixVariable` represents a matrix that can be stored and synchronized
/// across different storage backends (local disk, S3, memory). It provides
/// a high-level interface for matrix operations commonly needed in ADMM
/// algorithms while handling the complexity of distributed storage.
///
/// # Storage Locations
///
/// Variables can be stored in multiple locations:
/// - **Local**: On local disk with compression
/// - **S3**: In AWS S3 for distributed access
/// - **Memory**: In RAM for fast access
///
/// # Example
///
/// ```rust,no_run
/// # use srsadmm_core::variable::MatrixVariable;
/// # use srsadmm_core::resource::{ResourceLocation, StorageConfig};
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let storage_config = StorageConfig::new(/* ... */);
///
/// // Create a 100x50 matrix of zeros
/// let mut matrix = MatrixVariable::zeros(
///     "my_matrix".to_string(),
///     100, 50,
///     ResourceLocation::Local,
///     &storage_config
/// ).await;
///
/// // Sync to S3 for distributed access
/// matrix.sync_to(ResourceLocation::S3).await?;
///
/// // Read the matrix data
/// let data = matrix.read().await?;
/// println!("Matrix shape: {}x{}", data.nrows(), data.ncols());
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct MatrixVariable {
    /// Unique identifier for this variable
    id: String,
    /// Underlying resource manager handling storage and synchronization
    _resource: ProblemResourceImpl<na::DMatrix<f32>>,
}

impl MatrixVariable {
    /// Creates a new matrix variable with the specified configuration.
    ///
    /// # Arguments
    ///
    /// * `id` - Unique identifier for this variable
    /// * `default_location` - Primary storage location for the variable
    /// * `storage_config` - Configuration for all storage backends
    ///
    /// # Returns
    ///
    /// A new `MatrixVariable` instance ready for use
    pub fn new(
        id: String,
        default_location: ResourceLocation,
        storage_config: &StorageConfig,
    ) -> Self {
        MatrixVariable {
            id: id.clone(),
            _resource: ProblemResourceImpl::new(id, default_location, storage_config),
        }
    }

    /// Returns the unique identifier for this variable.
    ///
    /// # Returns
    ///
    /// A string slice containing the variable's ID
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Creates a matrix variable from an existing matrix.
    ///
    /// This constructor immediately writes the provided matrix to storage
    /// at the specified default location.
    ///
    /// # Arguments
    ///
    /// * `id` - Unique identifier for this variable
    /// * `default_location` - Primary storage location for the variable
    /// * `storage_config` - Configuration for all storage backends
    /// * `matrix` - The initial matrix data to store
    ///
    /// # Returns
    ///
    /// A new `MatrixVariable` containing the provided matrix data
    pub async fn from_matrix(
        id: String,
        default_location: ResourceLocation,
        storage_config: &StorageConfig,
        matrix: na::DMatrix<f32>,
    ) -> Self {
        let mut variable = MatrixVariable::new(id, default_location, storage_config);
        let _ = variable._resource.write(matrix).await.unwrap();
        variable
    }

    /// Creates a matrix variable initialized with zeros.
    ///
    /// # Arguments
    ///
    /// * `id` - Unique identifier for this variable
    /// * `nrows` - Number of rows in the matrix
    /// * `ncols` - Number of columns in the matrix
    /// * `default_location` - Primary storage location for the variable
    /// * `storage_config` - Configuration for all storage backends
    ///
    /// # Returns
    ///
    /// A new `MatrixVariable` containing a zero matrix of the specified dimensions
    pub async fn zeros(
        id: String,
        nrows: usize,
        ncols: usize,
        default_location: ResourceLocation,
        storage_config: &StorageConfig,
    ) -> Self {
        let matrix = na::DMatrix::<f32>::zeros(nrows, ncols);
        MatrixVariable::from_matrix(id, default_location, storage_config, matrix).await
    }

    /// Creates a matrix variable initialized with ones.
    ///
    /// # Arguments
    ///
    /// * `id` - Unique identifier for this variable
    /// * `nrows` - Number of rows in the matrix
    /// * `ncols` - Number of columns in the matrix
    /// * `default_location` - Primary storage location for the variable
    /// * `storage_config` - Configuration for all storage backends
    ///
    /// # Returns
    ///
    /// A new `MatrixVariable` containing a matrix of ones with the specified dimensions
    pub async fn ones(
        id: String,
        nrows: usize,
        ncols: usize,
        default_location: ResourceLocation,
        storage_config: &StorageConfig,
    ) -> Self {
        let matrix = na::DMatrix::<f32>::from_element(nrows, ncols, 1.0);
        MatrixVariable::from_matrix(id, default_location, storage_config, matrix).await
    }

    /// Creates a matrix variable initialized as an identity matrix.
    ///
    /// # Arguments
    ///
    /// * `id` - Unique identifier for this variable
    /// * `nrows` - Number of rows in the matrix
    /// * `ncols` - Number of columns in the matrix
    /// * `default_location` - Primary storage location for the variable
    /// * `storage_config` - Configuration for all storage backends
    ///
    /// # Returns
    ///
    /// A new `MatrixVariable` containing an identity matrix of the specified dimensions
    pub async fn eye(
        id: String,
        nrows: usize,
        ncols: usize,
        default_location: ResourceLocation,
        storage_config: &StorageConfig,
    ) -> Self {
        let matrix = na::DMatrix::identity(nrows, ncols);
        MatrixVariable::from_matrix(id, default_location, storage_config, matrix).await
    }

    /// Synchronizes the matrix variable to the default location.
    ///
    /// # Returns
    ///
    /// * `Ok(())` if the synchronization is successful.
    /// * `Err(Box<dyn std::error::Error>)` if the synchronization fails.
    ///
    pub async fn sync(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        self._resource.sync().await
    }

    /// Synchronizes the matrix variable to a specific location.
    ///
    /// # Arguments
    ///
    /// * `location` - The storage location to synchronize to.
    ///
    /// # Returns
    ///
    /// * `Ok(())` if the synchronization is successful.
    /// * `Err(Box<dyn std::error::Error>)` if the synchronization fails.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use srsadmm_core::variable::MatrixVariable;
    /// use srsadmm_core::resource::ResourceLocation;
    /// async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut matrix = MatrixVariable::new("my_matrix".to_string(), ResourceLocation::Local, &storage_config);
    /// matrix.sync_to(ResourceLocation::S3).await?;
    /// Ok(())
    /// }
    /// ```
    pub async fn sync_to(
        &mut self,
        location: ResourceLocation,
    ) -> Result<(), Box<dyn std::error::Error>> {
        self._resource.sync_to(location).await
    }

    /// Returns the default storage location for this variable.
    ///
    /// # Returns
    ///
    /// The default storage location for this variable
    pub fn default_location(&self) -> ResourceLocation {
        self._resource.default_location()
    }

    /// Reads the matrix data from storage.
    ///
    /// This method reads the matrix from the most recently updated storage location,
    /// preferring memory > local > S3 for performance.
    ///
    /// # Returns
    ///
    /// * `Ok(DMatrix<f32>)` - The matrix data
    /// * `Err(Box<dyn Error>)` - If reading fails or no valid data is found
    pub async fn read(&self) -> Result<na::DMatrix<f32>, Box<dyn std::error::Error>> {
        self._resource.read().await
    }

    /// Reads and caches the matrix data from a specific location.
    ///
    /// This method first synchronizes the variable to the specified location,
    /// then reads and returns the matrix data.
    ///
    /// # Arguments
    ///
    /// * `location` - The storage location to read from
    ///
    /// # Returns
    ///
    /// * `Ok(DMatrix<f32>)` - The matrix data
    /// * `Err(Box<dyn Error>)` - If synchronization or reading fails
    pub async fn read_and_cache(
        &mut self,
        location: ResourceLocation,
    ) -> Result<na::DMatrix<f32>, Box<dyn std::error::Error>> {
        self._resource.read_and_cache(location).await
    }

    /// Writes new matrix data to the default storage location.
    ///
    /// This method overwrites the current matrix data with the provided matrix
    /// and marks the default location as having the most recent update.
    ///
    /// # Arguments
    ///
    /// * `matrix` - The new matrix data to store
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If writing succeeds
    /// * `Err(Box<dyn Error>)` - If writing fails
    pub async fn write(
        &mut self,
        matrix: na::DMatrix<f32>,
    ) -> Result<(), Box<dyn std::error::Error>> {
        self._resource.write(matrix).await
    }

    pub fn s3_key(&self) -> String {
        self._resource.s3_key()
    }

    pub fn s3_bucket(&self) -> String {
        self._resource.s3_bucket()
    }

    pub fn set_default_location(&mut self, location: ResourceLocation) {
        self._resource.set_default_location(location);
    }

    /// Mark this variable as updated in a specific location
    /// Useful when external processes (like Lambda) write to S3
    pub fn mark_updated(&mut self, location: ResourceLocation) {
        self._resource.mark_updated(location);
    }

    pub async fn sub(
        &mut self,
        other: &mut MatrixVariable,
    ) -> Result<MatrixVariable, Box<dyn std::error::Error>> {
        // Ensure both variables are in the same location (use self's default location)
        let target_location = self.default_location();
        self._resource.sync_to(target_location).await?;
        other._resource.sync_to(target_location).await?;

        let self_matrix = self._resource.read().await?;
        let other_matrix = other._resource.read().await?;
        let result = self_matrix - other_matrix;
        Ok(MatrixVariable::from_matrix(
            format!("{}-sub-{}", self.id, other.id),
            target_location,
            self._resource.storage_config(),
            result,
        )
        .await)
    }

    pub async fn add(
        &mut self,
        other: &mut MatrixVariable,
    ) -> Result<(), Box<dyn std::error::Error>> {
        // Ensure both variables are in the same location (use self's default location)
        let target_location = self.default_location();
        self._resource.sync_to(target_location).await?;
        other._resource.sync_to(target_location).await?;

        let self_matrix = self._resource.read().await?;
        let other_matrix = other._resource.read().await?;
        let result = self_matrix + other_matrix;
        self._resource.write(result).await?;
        Ok(())
    }

    pub async fn add_identity(&mut self, factor: f32) -> Result<(), Box<dyn std::error::Error>> {
        self._resource.sync().await?;
        let matrix = self._resource.read().await?;
        let nrows = matrix.nrows();
        let ncols = matrix.ncols();
        let eye = na::DMatrix::<f32>::identity(nrows, ncols);
        let result = matrix + eye * factor;
        self._resource.write(result).await?;
        Ok(())
    }

    pub async fn inverse(&mut self) -> Result<MatrixVariable, Box<dyn std::error::Error>> {
        self._resource.sync().await?;
        let matrix = self._resource.read().await?;
        let inverse = matrix.try_inverse().ok_or("Matrix is not invertible")?;
        Ok(MatrixVariable::from_matrix(
            format!("{}-inverse", self.id),
            self.default_location(),
            self._resource.storage_config(),
            inverse,
        )
        .await)
    }

    pub async fn map_rows_by_subproblem<R, F>(
        &self,
        n_subproblems: usize,
        mut f: F,
    ) -> Result<Vec<R>, Box<dyn std::error::Error>>
    where
        F: FnMut(na::DMatrix<f32>, usize) -> R,
    {
        let matrix = self._resource.read().await?;
        let nrows = matrix.nrows();
        let mut results = Vec::new();

        let base_chunk_size = nrows / n_subproblems;
        let remainder = nrows % n_subproblems;

        let mut current_row = 0;

        for i in 0..n_subproblems {
            let chunk_size = if i < remainder {
                base_chunk_size + 1
            } else {
                base_chunk_size
            };
            let chunk = matrix.rows(current_row, chunk_size).clone_owned();
            results.push(f(chunk, i));
            current_row += chunk_size;
        }

        Ok(results)
    }

    pub async fn map_rows_by_subproblem_with<R, F>(
        &self,
        var2: &MatrixVariable,
        n_subproblems: usize,
        mut f: F,
    ) -> Result<Vec<R>, Box<dyn std::error::Error>>
    where
        F: FnMut(na::DMatrix<f32>, na::DMatrix<f32>, usize) -> R,
    {
        let matrix = self._resource.read().await?;
        let matrix2 = var2._resource.read().await?;
        let nrows = matrix.nrows();
        let nrows2 = matrix2.nrows();
        let ncols = matrix.ncols();
        let ncols2 = matrix2.ncols();
        let mut results = Vec::new();

        if nrows != nrows2 {
            return Err(LassoError::from_string(format!(
                "Matrix rows do not match: {}x{} and {}x{}",
                nrows, ncols, nrows2, ncols2
            ))
            .into());
        }

        let base_chunk_size = nrows / n_subproblems;
        let remainder = nrows % n_subproblems;

        let mut current_row = 0;

        for i in 0..n_subproblems {
            let chunk_size = if i < remainder {
                base_chunk_size + 1
            } else {
                base_chunk_size
            };
            let chunk = matrix.rows(current_row, chunk_size).clone_owned();
            let chunk2 = matrix2.rows(current_row, chunk_size).clone_owned();
            results.push(f(chunk, chunk2, i));
            current_row += chunk_size;
        }
        Ok(results)
    }

    pub async fn map_rows_by_subproblem_with_rayon<R, F>(
        &self,
        var2: &MatrixVariable,
        n_subproblems: usize,
        n_threads: usize,
        f: F,
    ) -> Result<Vec<R>, Box<dyn std::error::Error>>
    where
        F: Fn(na::DMatrix<f32>, na::DMatrix<f32>, usize) -> R + Send + Sync,
        R: Send,
    {
        let matrix = self._resource.read().await?;
        let matrix2 = var2._resource.read().await?;
        let nrows = matrix.nrows();
        let nrows2 = matrix2.nrows();
        let ncols = matrix.ncols();
        let ncols2 = matrix2.ncols();

        if nrows != nrows2 {
            return Err(LassoError::from_string(format!(
                "Matrix rows do not match: {}x{} and {}x{}",
                nrows, ncols, nrows2, ncols2
            ))
            .into());
        }

        let base_chunk_size = nrows / n_subproblems;
        let remainder = nrows % n_subproblems;

        // Precompute chunk info
        let chunk_data = (0..n_subproblems)
            .map(|i| {
                let chunk_size = if i < remainder {
                    base_chunk_size + 1
                } else {
                    base_chunk_size
                };
                let chunk_index = base_chunk_size * i + if i < remainder { i } else { remainder };
                (chunk_index, chunk_size)
            })
            .collect::<Vec<_>>();

        let pool = rayon::ThreadPoolBuilder::new()
            .num_threads(n_threads)
            .build()
            .unwrap();

        let results = pool.install(|| {
            chunk_data
                .into_par_iter()
                .zip(0..n_subproblems)
                .map(|((chunk_index, chunk_size), i)| {
                    let chunk = matrix.rows(chunk_index, chunk_size).clone_owned();
                    let chunk2 = matrix2.rows(chunk_index, chunk_size).clone_owned();
                    f(chunk, chunk2, i)
                })
                .collect::<Vec<_>>()
        });

        Ok(results)
    }

    pub async fn map_rows_by_subproblem_with_async<R, F>(
        &self,
        var2: &MatrixVariable,
        n_subproblems: usize,
        f: F,
    ) -> Result<Vec<R>, Box<dyn std::error::Error>>
    where
        F: AsyncFn(na::DMatrix<f32>, na::DMatrix<f32>, usize) -> R,
    {
        let matrix = self._resource.read().await?;
        let matrix2 = var2._resource.read().await?;
        let nrows = matrix.nrows();
        let nrows2 = matrix2.nrows();
        let ncols = matrix.ncols();
        let ncols2 = matrix2.ncols();

        if nrows != nrows2 {
            return Err(LassoError::from_string(format!(
                "Matrix rows do not match: {}x{} and {}x{}",
                nrows, ncols, nrows2, ncols2
            ))
            .into());
        }

        let base_chunk_size = nrows / n_subproblems;
        let remainder = nrows % n_subproblems;

        let mut current_row = 0;

        let futures = (0..n_subproblems)
            .map(|i| {
                let chunk_size = if i < remainder {
                    base_chunk_size + 1
                } else {
                    base_chunk_size
                };
                let chunk = matrix.rows(current_row, chunk_size).clone_owned();
                let chunk2 = matrix2.rows(current_row, chunk_size).clone_owned();
                current_row += chunk_size;
                f(chunk, chunk2, i)
            })
            .collect::<Vec<_>>();

        let results = futures::future::join_all(futures).await;
        Ok(results)
    }
}

/// A distributed scalar variable for ADMM optimization.
///
/// `ScalarVariable` represents a single floating-point value that can be stored
/// and synchronized across different storage backends (local disk, S3, memory).
/// This is useful for storing parameters, objective values, or other scalar
/// quantities in distributed ADMM algorithms.
///
/// # Example
///
/// ```rust,no_run
/// # use srsadmm_core::variable::ScalarVariable;
/// # use srsadmm_core::resource::{ResourceLocation, StorageConfig};
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let storage_config = StorageConfig::new(/* ... */);
///
/// // Create a scalar variable with initial value
/// let mut scalar = ScalarVariable::from_scalar(
///     "lambda".to_string(),
///     ResourceLocation::Local,
///     &storage_config,
///     0.1
/// ).await;
///
/// // Read the current value
/// let value = scalar.read().await?;
/// println!("Current value: {}", value);
///
/// // Update the value
/// scalar.write(0.2).await?;
/// # Ok(())
/// # }
/// ```
pub struct ScalarVariable {
    /// Unique identifier for this scalar variable
    #[allow(dead_code)]
    id: String,
    /// Underlying resource manager handling storage and synchronization
    _resource: ProblemResourceImpl<f32>,
}

impl ScalarVariable {
    /// Creates a new scalar variable with the specified configuration.
    ///
    /// # Arguments
    ///
    /// * `id` - Unique identifier for this variable
    /// * `default_location` - Primary storage location for the variable
    /// * `storage_config` - Configuration for all storage backends
    ///
    /// # Returns
    ///
    /// A new `ScalarVariable` instance ready for use
    pub fn new(
        id: String,
        default_location: ResourceLocation,
        storage_config: &StorageConfig,
    ) -> Self {
        ScalarVariable {
            id: id.clone(),
            _resource: ProblemResourceImpl::new(id, default_location, storage_config),
        }
    }

    /// Creates a scalar variable from an initial value.
    ///
    /// This constructor immediately writes the provided scalar value to storage
    /// at the specified default location.
    ///
    /// # Arguments
    ///
    /// * `id` - Unique identifier for this variable
    /// * `default_location` - Primary storage location for the variable
    /// * `storage_config` - Configuration for all storage backends
    /// * `scalar` - The initial scalar value to store
    ///
    /// # Returns
    ///
    /// A new `ScalarVariable` containing the provided scalar value
    pub async fn from_scalar(
        id: String,
        default_location: ResourceLocation,
        storage_config: &StorageConfig,
        scalar: f32,
    ) -> Self {
        let mut variable = ScalarVariable::new(id, default_location, storage_config);
        let _ = variable._resource.write(scalar).await.unwrap();
        variable
    }

    pub async fn read(&self) -> Result<f32, Box<dyn std::error::Error>> {
        self._resource.read().await
    }

    pub async fn read_and_cache(
        &mut self,
        location: ResourceLocation,
    ) -> Result<f32, Box<dyn std::error::Error>> {
        self._resource.read_and_cache(location).await
    }

    pub async fn write(&mut self, scalar: f32) -> Result<(), Box<dyn std::error::Error>> {
        self._resource.write(scalar).await
    }

    pub fn s3_key(&self) -> String {
        self._resource.s3_key()
    }

    pub fn s3_bucket(&self) -> String {
        self._resource.s3_bucket()
    }
}

#[derive(Clone, Copy, PartialEq)]
pub enum MatrixStorageType {
    Columns = 0,
    Rows = 1,
}

impl std::fmt::Display for MatrixStorageType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MatrixStorageType::Columns => write!(f, "Columns"),
            MatrixStorageType::Rows => write!(f, "Rows"),
        }
    }
}

/// DataMatrixVariables are variables that store very large matrices that require
/// quick row/column slice access. They are only stored on the local file system
/// and don't have a direct "read()" method, but instead have methods for reading
/// and iterating over chunks of rows/columns.
pub struct DataMatrixVariable {
    id: String,
    _resource: ProblemResourceImpl<na::DMatrix<f32>>,
    storage_type: MatrixStorageType,
    nrows: usize,
    ncols: usize,
}

impl DataMatrixVariable {
    pub(crate) fn new(
        id: String,
        storage_config: &StorageConfig,
        storage_type: MatrixStorageType,
        nrows: usize,
        ncols: usize,
    ) -> Self {
        DataMatrixVariable {
            id: id.clone(),
            _resource: ProblemResourceImpl::new(id, ResourceLocation::Local, storage_config),
            storage_type,
            nrows,
            ncols,
        }
    }

    pub fn from_matrix(
        id: String,
        matrix: na::DMatrix<f32>,
        storage_type: MatrixStorageType,
        storage_config: &StorageConfig,
    ) -> Self {
        let var = DataMatrixVariable::new(
            id,
            storage_config,
            storage_type,
            matrix.nrows(),
            matrix.ncols(),
        );
        let path = var._resource.local_path();
        write_data_matrix_to_file(&matrix, &path, storage_type).unwrap();
        var
    }

    pub fn from_problem_file(id: String, file_path: &Path, storage_config: &StorageConfig) -> Self {
        let new_path = Path::new(&storage_config.local.root)
            .join(format!("{}{}", storage_config.local.prefix, id))
            .with_extension("bin");
        std::fs::copy(file_path, &new_path).unwrap();
        let file = File::open(new_path).unwrap();
        let mmap = unsafe { Mmap::map(&file).unwrap() };
        let header = mmap[0..24].to_vec();
        let nrows = u64::from_le_bytes(header[8..16].try_into().unwrap()) as usize;
        let ncols = u64::from_le_bytes(header[16..24].try_into().unwrap()) as usize;
        let storage_type = match header[0] {
            0 => MatrixStorageType::Columns,
            1 => MatrixStorageType::Rows,
            _ => panic!("Invalid storage type"),
        };
        let var = DataMatrixVariable::new(id, storage_config, storage_type, nrows, ncols);
        var
    }

    pub fn read_chunk(
        &self,
        chunk_index: usize,
        chunk_size: usize,
    ) -> Result<na::DMatrix<f32>, Box<dyn std::error::Error>> {
        let mut path = self._resource.local_path();
        path.set_extension(""); // Not using zstd for datamatrices but backend adds it to extension
        let file = File::open(path)?;
        let mmap = unsafe { Mmap::map(&file)? };
        let header = mmap[0..24].to_vec();

        let nrows = u64::from_le_bytes(header[8..16].try_into().unwrap()) as usize;
        let ncols = u64::from_le_bytes(header[16..24].try_into().unwrap()) as usize;

        return match self.storage_type {
            MatrixStorageType::Columns => {
                if chunk_index + chunk_size > ncols {
                    return Err(LassoError::from_string(format!(
                        "Chunk index and size exceed number of columns: {} + {} > {}",
                        chunk_index, chunk_size, ncols
                    ))
                    .into());
                }

                let start_index = 24 + chunk_index * 4 * nrows;
                let end_index = start_index + chunk_size * 4 * nrows;
                let data = &mmap[start_index..end_index];
                let data_f32: &[f32] = unsafe {
                    std::slice::from_raw_parts(data.as_ptr() as *const f32, nrows * chunk_size)
                };

                let matrix = na::DMatrix::<f32>::from_column_slice(nrows, chunk_size, data_f32);
                Ok(matrix)
            }
            MatrixStorageType::Rows => {
                if chunk_index + chunk_size > nrows {
                    return Err(LassoError::from_string(format!(
                        "Chunk index and size exceed number of rows: {} + {} > {}",
                        chunk_index, chunk_size, nrows
                    ))
                    .into());
                }
                let start_index = 24 + chunk_index * ncols * 4;
                let end_index = start_index + chunk_size * ncols * 4;
                let data = &mmap[start_index..end_index];
                let data_f32: &[f32] = unsafe {
                    std::slice::from_raw_parts(data.as_ptr() as *const f32, chunk_size * ncols)
                };
                let matrix = na::DMatrix::<f32>::from_row_slice(chunk_size, ncols, data_f32);
                Ok(matrix)
            }
        };
    }

    pub fn iter_subproblems<T, F>(
        &self,
        n_subproblems: usize,
        f: F,
    ) -> Result<Vec<T>, Box<dyn std::error::Error>>
    where
        F: Fn(na::DMatrix<f32>, usize) -> T,
    {
        let base_chunk_size = self.nrows() / n_subproblems;
        let remainder = self.nrows() % n_subproblems;

        let chunk_data = (0..n_subproblems)
            .map(|i| {
                let chunk_size = if i < remainder {
                    base_chunk_size + 1
                } else {
                    base_chunk_size
                };
                let chunk_index = base_chunk_size * i + if i < remainder { i } else { remainder };
                (chunk_index, chunk_size)
            })
            .collect::<Vec<_>>();

        let results = chunk_data
            .into_iter()
            .enumerate()
            .map(|(i, (chunk_index, chunk_size))| {
                let chunk = self
                    .read_chunk(chunk_index, chunk_size)
                    .expect("Failed to read chunk");
                f(chunk, i)
            })
            .collect::<Vec<_>>();

        Ok(results)
    }

    pub fn iter_subproblems_with<T, F>(
        &self,
        var2: &DataMatrixVariable,
        n_subproblems: usize,
        f: F,
    ) -> Result<Vec<T>, Box<dyn std::error::Error>>
    where
        F: Fn(na::DMatrix<f32>, na::DMatrix<f32>, usize) -> T,
    {
        let base_chunk_size = self.nrows() / n_subproblems;
        let remainder = self.nrows() % n_subproblems;

        if self.storage_type != var2.storage_type {
            return Err(LassoError::from_string(format!(
                "Storage types do not match: {} and {}",
                self.storage_type, var2.storage_type
            ))
            .into());
        }

        match self.storage_type {
            MatrixStorageType::Columns => {
                if self.ncols() != var2.ncols() {
                    return Err(LassoError::from_string(format!(
                        "Number of columns do not match: {} and {}",
                        self.ncols(),
                        var2.ncols()
                    ))
                    .into());
                }
            }
            MatrixStorageType::Rows => {
                if self.nrows() != var2.nrows() {
                    return Err(LassoError::from_string(format!(
                        "Number of rows do not match: {} and {}",
                        self.nrows(),
                        var2.nrows()
                    ))
                    .into());
                }
            }
        }

        let chunk_data = (0..n_subproblems)
            .map(|i| {
                let chunk_size = if i < remainder {
                    base_chunk_size + 1
                } else {
                    base_chunk_size
                };
                let chunk_index = base_chunk_size * i + if i < remainder { i } else { remainder };
                (chunk_index, chunk_size)
            })
            .collect::<Vec<_>>();

        let results = chunk_data
            .into_iter()
            .enumerate()
            .map(|(i, (chunk_index, chunk_size))| {
                let chunk = self
                    .read_chunk(chunk_index, chunk_size)
                    .expect("Failed to read chunk");
                let chunk2 = var2
                    .read_chunk(chunk_index, chunk_size)
                    .expect("Failed to read chunk");
                f(chunk, chunk2, i)
            })
            .collect::<Vec<_>>();

        Ok(results)
    }

    pub fn iter_subproblems_rayon<T, F>(
        &self,
        n_subproblems: usize,
        n_threads: usize,
        f: F,
    ) -> Result<Vec<T>, Box<dyn std::error::Error>>
    where
        F: Fn(na::DMatrix<f32>, usize) -> T + Send + Sync,
        T: Send,
    {
        let pool = rayon::ThreadPoolBuilder::new()
            .num_threads(n_threads)
            .build()
            .unwrap();
        let base_chunk_size = self.nrows() / n_subproblems;
        let remainder = self.nrows() % n_subproblems;

        let chunk_data = (0..n_subproblems)
            .map(|i| {
                let chunk_size = if i < remainder {
                    base_chunk_size + 1
                } else {
                    base_chunk_size
                };
                let chunk_index = base_chunk_size * i + if i < remainder { i } else { remainder };
                (chunk_index, chunk_size)
            })
            .collect::<Vec<_>>();

        let results = pool.install(|| {
            chunk_data
                .into_par_iter()
                .zip(0..n_subproblems)
                .map(|((chunk_index, chunk_size), i)| {
                    let chunk = self
                        .read_chunk(chunk_index, chunk_size)
                        .expect("Failed to read chunk");
                    f(chunk, i)
                })
                .collect::<Vec<_>>()
        });
        Ok(results)
    }

    pub fn iter_subproblems_with_rayon<T, F>(
        &self,
        var2: &DataMatrixVariable,
        n_subproblems: usize,
        n_threads: usize,
        f: F,
    ) -> Result<Vec<T>, Box<dyn std::error::Error>>
    where
        F: Fn(na::DMatrix<f32>, na::DMatrix<f32>, usize) -> T + Send + Sync,
        T: Send,
    {
        let pool = rayon::ThreadPoolBuilder::new()
            .num_threads(n_threads)
            .build()
            .unwrap();
        let base_chunk_size = self.nrows() / n_subproblems;
        let remainder = self.nrows() % n_subproblems;

        if self.storage_type != var2.storage_type {
            return Err(LassoError::from_string(format!(
                "Storage types do not match: {} and {}",
                self.storage_type, var2.storage_type
            ))
            .into());
        }

        match self.storage_type {
            MatrixStorageType::Columns => {
                if self.ncols() != var2.ncols() {
                    return Err(LassoError::from_string(format!(
                        "Number of columns do not match: {} and {}",
                        self.ncols(),
                        var2.ncols()
                    ))
                    .into());
                }
            }
            MatrixStorageType::Rows => {
                if self.nrows() != var2.nrows() {
                    return Err(LassoError::from_string(format!(
                        "Number of rows do not match: {} and {}",
                        self.nrows(),
                        var2.nrows()
                    ))
                    .into());
                }
            }
        }

        let chunk_data = (0..n_subproblems)
            .map(|i| {
                let chunk_size = if i < remainder {
                    base_chunk_size + 1
                } else {
                    base_chunk_size
                };
                let chunk_index = base_chunk_size * i + if i < remainder { i } else { remainder };
                (chunk_index, chunk_size)
            })
            .collect::<Vec<_>>();

        let results = pool.install(|| {
            chunk_data
                .into_par_iter()
                .zip(0..n_subproblems)
                .map(|((chunk_index, chunk_size), i)| {
                    let chunk = self
                        .read_chunk(chunk_index, chunk_size)
                        .expect("Failed to read chunk");
                    let chunk2 = var2
                        .read_chunk(chunk_index, chunk_size)
                        .expect("Failed to read chunk");
                    f(chunk, chunk2, i)
                })
                .collect::<Vec<_>>()
        });
        Ok(results)
    }

    pub fn id(&self) -> &str {
        &self.id
    }

    pub fn nrows(&self) -> usize {
        self.nrows
    }

    pub fn ncols(&self) -> usize {
        self.ncols
    }
}

// Custom matrix format:
// 1. Header: 8 bytes for storage type (0 = columns, 1 = rows)
// 2. Header: 8 bytes for number of rows
// 3. Header: 8 bytes for number of columns
// 4. Data: matrix data in row or column major order

pub(crate) fn write_data_matrix_to_file(
    matrix: &na::DMatrix<f32>,
    file_path: &Path,
    storage_type: MatrixStorageType,
) -> Result<(), Box<dyn std::error::Error>> {
    let batch_size = 1024;
    let file = File::create(file_path)?;
    let nrows = matrix.nrows() as u64;
    let ncols = matrix.ncols() as u64;
    let buf_size = match storage_type {
        MatrixStorageType::Columns => nrows * 4,
        MatrixStorageType::Rows => ncols * 4,
    };
    let mut writer = BufWriter::with_capacity(buf_size as usize, file);
    let header = u64_to_bytes(storage_type as u64);
    writer.write_all(&header)?;
    let header = [nrows, ncols];
    let header = header
        .iter()
        .map(|&v| u64_to_bytes(v))
        .flatten()
        .collect::<Vec<_>>();
    writer.write_all(&header)?;
    let batches = match storage_type {
        MatrixStorageType::Columns => (0..matrix.ncols()).step_by(batch_size),
        MatrixStorageType::Rows => (0..matrix.nrows()).step_by(batch_size),
    };
    match storage_type {
        MatrixStorageType::Columns => {
            for col in batches {
                let start_col = col;
                let n_cols = std::cmp::min(start_col + batch_size, matrix.ncols()) - start_col;
                let col_view = matrix.columns(start_col, n_cols).clone_owned();
                let col_data = col_view.as_slice();
                let byte_slice = unsafe {
                    std::slice::from_raw_parts(col_data.as_ptr() as *const u8, col_data.len() * 4)
                };
                writer.write_all(byte_slice)?;
            }
        }
        MatrixStorageType::Rows => {
            for row in batches {
                let start_row = row;
                let n_rows = std::cmp::min(start_row + batch_size, matrix.nrows()) - start_row;
                let row_data_t = matrix.rows(start_row, n_rows).clone_owned().transpose();
                let row_data = row_data_t.as_slice();
                let byte_slice = unsafe {
                    std::slice::from_raw_parts(row_data.as_ptr() as *const u8, row_data.len() * 4)
                };
                writer.write_all(byte_slice)?;
            }
        }
    }
    Ok(())
}

pub(crate) fn u64_to_bytes(value: u64) -> [u8; 8] {
    let mut bytes = [0; 8];
    bytes.copy_from_slice(&value.to_le_bytes());
    bytes
}