scirs2_core/distributed/
array.rs1use crate::error::CoreResult;
7
8#[derive(Debug)]
10pub struct DistributedArray<T> {
11 local_chunk: Vec<T>,
12 total_size: usize,
13 chunk_start: usize,
14 chunk_end: usize,
15 nodeid: String,
16}
17
18impl<T> DistributedArray<T>
19where
20 T: Clone + Send + Sync,
21{
22 pub fn new(local_chunk: Vec<T>, total_size: usize, chunk_start: usize, nodeid: String) -> Self {
24 let chunk_end = chunk_start + local_chunk.len();
25 Self {
26 local_chunk,
27 total_size,
28 chunk_start,
29 chunk_end,
30 nodeid,
31 }
32 }
33
34 pub fn local_data(&self) -> &[T] {
36 &self.local_chunk
37 }
38
39 pub fn local_size(&self) -> usize {
41 self.local_chunk.len()
42 }
43
44 pub fn total_size(&self) -> usize {
46 self.total_size
47 }
48
49 pub fn chunk_start(&self) -> usize {
51 self.chunk_start
52 }
53
54 pub fn chunk_end(&self) -> usize {
56 self.chunk_end
57 }
58
59 pub fn nodeid(&self) -> &str {
61 &self.nodeid
62 }
63}
64
65#[derive(Debug)]
67pub struct DistributedArrayManager {
68 nodeid: String,
69 clustersize: usize,
70}
71
72impl DistributedArrayManager {
73 pub fn new(nodeid: String, clustersize: usize) -> Self {
75 Self {
76 nodeid,
77 clustersize,
78 }
79 }
80
81 pub fn distribute_array<T>(&self, data: Vec<T>) -> CoreResult<DistributedArray<T>>
83 where
84 T: Clone + Send + Sync,
85 {
86 let total_size = data.len();
87 let _chunk_size = total_size.div_ceil(self.clustersize);
88
89 let chunk_start = 0;
92 let local_chunk = data;
93
94 Ok(DistributedArray::new(
95 local_chunk,
96 total_size,
97 chunk_start,
98 self.nodeid.clone(),
99 ))
100 }
101
102 pub fn gather_results<T>(&self, localresult: Vec<T>) -> CoreResult<Vec<T>>
104 where
105 T: Clone + Send + Sync,
106 {
107 Ok(localresult)
109 }
110}
111
112#[cfg(test)]
113mod tests {
114 use super::*;
115
116 #[test]
117 fn test_distributed_array_creation() {
118 let data = vec![1, 2, 3, 4, 5];
119 let nodeid = "node1".to_string();
120 let array = DistributedArray::new(data.clone(), 10, 0, nodeid.clone());
121
122 assert_eq!(array.local_data(), &data);
123 assert_eq!(array.local_size(), 5);
124 assert_eq!(array.total_size(), 10);
125 assert_eq!(array.chunk_start(), 0);
126 assert_eq!(array.chunk_end(), 5);
127 assert_eq!(array.nodeid(), "node1");
128 }
129
130 #[test]
131 fn test_distributed_array_manager() {
132 let manager = DistributedArrayManager::new("node1".to_string(), 3);
133 let data = vec![1, 2, 3, 4, 5, 6];
134
135 let distributed = manager.distribute_array(data).unwrap();
136 assert_eq!(distributed.total_size(), 6);
137 }
138}