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
use crate::array::{
iterator::{
local_iterator::{IndexedLocalIterator, LocalIterator},
private::{InnerIter, Sealed},
IterLockFuture,
},
r#unsafe::UnsafeArray,
LamellarArray,
};
use crate::memregion::Dist;
/// An iterator over immutable (nonoverlapping) local chunks (of size chunk_size) of an [UnsafeArray]
/// This struct is created by calling [UnsafeArray::local_chunks]
#[derive(Clone)]
pub struct UnsafeLocalChunks<T: Dist> {
chunk_size: usize,
index: usize, //global index within the array local data
end_index: usize, //global index within the array local data
array: UnsafeArray<T>,
}
impl<T: Dist> InnerIter for UnsafeLocalChunks<T> {
fn lock_if_needed(&self, _s: Sealed) -> Option<IterLockFuture> {
None
}
fn iter_clone(&self, _s: Sealed) -> Self {
UnsafeLocalChunks {
chunk_size: self.chunk_size,
index: self.index,
end_index: self.end_index,
array: self.array.clone(),
}
}
}
/// An iterator over immutable (nonoverlapping) local chunks (of size chunk_size) of an [UnsafeArray]
/// This struct is created by calling [UnsafeArray::local_chunks_mut]
#[derive(Clone)]
pub struct UnsafeLocalChunksMut<T: Dist> {
chunk_size: usize,
index: usize, //global index within the array local data
end_index: usize, //global index within the array local data
array: UnsafeArray<T>,
}
impl<T: Dist> InnerIter for UnsafeLocalChunksMut<T> {
fn lock_if_needed(&self, _s: Sealed) -> Option<IterLockFuture> {
None
}
fn iter_clone(&self, _s: Sealed) -> Self {
UnsafeLocalChunksMut {
chunk_size: self.chunk_size,
index: self.index,
end_index: self.end_index,
array: self.array.clone(),
}
}
}
impl<T: Dist + 'static> LocalIterator for UnsafeLocalChunks<T> {
type Item = &'static [T];
type Array = UnsafeArray<T>;
fn init(&self, start_i: usize, cnt: usize, _s: Sealed) -> Self {
//these are with respect to the single elements, not chunk indexing and cnt
let end_i = std::cmp::min(
(start_i + cnt) * self.chunk_size,
self.array.num_elems_local(),
);
let new_start_i = start_i * self.chunk_size;
// println!(
// "start_i {} new_start_i {} end_i {} cnt: {}",
// start_i, new_start_i, end_i, cnt
// );
UnsafeLocalChunks {
chunk_size: self.chunk_size,
index: new_start_i,
end_index: end_i,
array: self.array.clone(),
}
}
fn array(&self) -> Self::Array {
self.array.clone()
}
fn next(&mut self) -> Option<Self::Item> {
// println!("next index {} end_index: {}", self.index, self.end_index);
if self.index < self.end_index {
let start_i = self.index;
self.index += self.chunk_size;
let end_i = std::cmp::min(self.index, self.end_index);
// println!(
// "start_i {} end_i {} self.index {} self.end_index {}",
// start_i, end_i, self.index, self.end_index
// );
Some(unsafe {
std::slice::from_raw_parts_mut(
self.array.local_as_mut_ptr().offset(start_i as isize),
end_i - start_i,
)
})
} else {
None
}
}
fn elems(&self, in_elems: usize) -> usize {
in_elems / self.chunk_size + (in_elems % self.chunk_size != 0) as usize
}
fn advance_index(&mut self, count: usize) {
self.index = std::cmp::min(self.index + count * self.chunk_size, self.end_index);
}
}
impl<T: Dist + 'static> IndexedLocalIterator for UnsafeLocalChunks<T> {
fn iterator_index(&self, index: usize) -> Option<usize> {
if index * self.chunk_size < self.array.len() {
Some(index) //everyone at this point as calculated the actual index (cause we are local only) so just return it
} else {
None
}
}
}
impl<T: Dist + 'static> LocalIterator for UnsafeLocalChunksMut<T> {
type Item = &'static mut [T];
type Array = UnsafeArray<T>;
fn init(&self, start_i: usize, cnt: usize, _s: Sealed) -> Self {
//these are with respect to the single elements, not chunk indexing and cnt
let end_i = std::cmp::min(
(start_i + cnt) * self.chunk_size,
self.array.num_elems_local(),
);
let new_start_i = start_i * self.chunk_size;
// println!(
// "start_i {} new_start_i {} end_i {} cnt: {}",
// start_i, new_start_i, end_i, cnt
// );
UnsafeLocalChunksMut {
chunk_size: self.chunk_size,
index: new_start_i,
end_index: end_i,
array: self.array.clone(),
}
}
fn array(&self) -> Self::Array {
self.array.clone()
}
fn next(&mut self) -> Option<Self::Item> {
// println!("next index {} end_index: {}", self.index, self.end_index);
if self.index < self.end_index {
let start_i = self.index;
self.index += self.chunk_size;
let end_i = std::cmp::min(self.index, self.end_index);
// println!(
// "start_i {} end_i {} self.index {} self.end_index {}",
// start_i, end_i, self.index, self.end_index
// );
Some(unsafe {
std::slice::from_raw_parts_mut(
self.array.local_as_mut_ptr().offset(start_i as isize),
end_i - start_i,
)
})
} else {
None
}
}
fn elems(&self, in_elems: usize) -> usize {
in_elems / self.chunk_size + (in_elems % self.chunk_size != 0) as usize
}
fn advance_index(&mut self, count: usize) {
self.index = std::cmp::min(self.index + count * self.chunk_size, self.end_index);
}
}
impl<T: Dist + 'static> IndexedLocalIterator for UnsafeLocalChunksMut<T> {
fn iterator_index(&self, index: usize) -> Option<usize> {
if index * self.chunk_size < self.array.len() {
Some(index) //everyone at this point as calculated the actual index (cause we are local only) so just return it
} else {
None
}
}
}
impl<T: Dist> UnsafeArray<T> {
/// immutably iterate over fixed sized chunks(slices) of the local data of this array.
/// the returned iterator is a lamellar [LocalIterator]
///
/// # Examples
///```
/// use lamellar::array::prelude::*;
///
/// let world = LamellarWorldBuilder::new().build();
/// let array: UnsafeArray<usize> = UnsafeArray::new(&world,40,Distribution::Block).block();
/// let my_pe = world.my_pe();
///
/// let _ = unsafe{array.local_chunks(5).enumerate().for_each(move|(i,chunk)| {
/// println!("PE: {my_pe} i: {i} chunk: {chunk:?}");
/// })}.spawn();
/// array.wait_all();
///
/// ```
pub unsafe fn local_chunks(&self, chunk_size: usize) -> UnsafeLocalChunks<T> {
UnsafeLocalChunks {
chunk_size,
index: 0,
end_index: 0,
array: self.clone(),
}
}
/// mutably iterate over fixed sized chunks(slices) of the local data of this array.
/// the returned iterator is a lamellar [LocalIterator]
///
/// # Examples
///```
/// use lamellar::array::prelude::*;
///
/// let world = LamellarWorldBuilder::new().build();
/// let array: UnsafeArray<usize> = UnsafeArray::new(&world,40,Distribution::Block).block();
/// let my_pe = world.my_pe();
///
/// unsafe{
/// let _ = array.local_chunks_mut(5).enumerate().for_each(move|(i,chunk)| {
/// println!("PE: {my_pe} i: {i} chunk: {chunk:?}");
/// }).spawn();
/// }
/// array.wait_all();
///
/// ```
pub unsafe fn local_chunks_mut(&self, chunk_size: usize) -> UnsafeLocalChunksMut<T> {
UnsafeLocalChunksMut {
chunk_size,
index: 0,
end_index: 0,
array: self.clone(),
}
}
}