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
// use lamellar::array::prelude::*;
// use lamellar::active_messaging::prelude::*;
// use rand::seq::SliceRandom;
// use rand::thread_rng;
// #[AmData]
// struct ApplyPePrefix{
// array: LocalLockArray<usize>,
// sum: usize,
// }
// #[am]
// impl LamellarAM for ApplyPePrefix {
// async fn exec(&self) {
// self.array.write_local_chunks(1).await.for_each(move|mut chunk| {
// for i in chunk.iter_mut() {
// *i += self.sum;
// }
// }).await;
// }
// }
// fn main() {
// let world = lamellar::LamellarWorldBuilder::new().build();
// let my_pe = world.my_pe();
// let num_pes = world.num_pes();
// let array_len = 100;
// // the array we want to prefix sum
// let array = LocalLockArray::<usize>::new(world.team(), array_len, Distribution::Block).block();
// let permuted_array =AtomicArray::<usize>::new(world.team(), array_len, Distribution::Block).block();
// //an array to hold the sum of elements on each pe
// let pe_sums = AtomicArray::<usize>::new(world.team(), num_pes, Distribution::Block).block();
// //initialize array
// array.local_iter_mut().for_each(|i| *i = 1).block();
// array.print();
// let chunk_size = array.num_elems_local() / world.num_threads_per_pe();
// let local_chunk_sums = array.write_local_chunks(chunk_size).block().map(|mut chunk| {
// let mut sum = 0;
// for i in chunk.iter_mut() {
// sum += *i;
// *i = sum;
// }
// sum
// }).collect::<Vec<_>>(Distribution::Block).block();
// //calculate the local sum for each pe, and store it into local element of pe_sums
// pe_sums.local_data().at(0).store(local_chunk_sums.iter().sum::<usize>());
// //calculate the local prefix sums
// let _ = array.write_local_chunks(chunk_size).block().enumerate().for_each(move |(i,mut chunk)| {
// let sum = local_chunk_sums[0..i].iter().sum::<usize>();
// for i in chunk.iter_mut() {
// *i += sum;
// }
// }).spawn(); //using a safe array we dont actually care if this finishes before we move on
// pe_sums.barrier();// ensure all pes have writen to pe_sums
// //calculate the pe prefix sums to reduce communication we only do this on pe 0
// if my_pe == 0{
// let mut sum = 0;
// for (pe, pe_sum) in pe_sums.onesided_iter().into_iter().enumerate().skip(1){
// sum += pe_sum;
// // this following is a bit inefficient as have to send the indices for each batch_add but simple to use the array api
// // let mut pe_indices = array.first_global_index_for_pe(pe).unwrap()..=array.last_global_index_for_pe(pe).unwrap();
// // let _ = array.batch_add(&mut pe_indices as &mut dyn Iterator<Item=usize>, sum).spawn();
// //alteratively we can do this with an AM with much less overhead
// let _ = world.spawn_am_pe(pe,ApplyPePrefix {
// array: array.clone(),
// sum,
// });
// }
// }
// world.wait_all();
// world.barrier();
// array.print();
// let permuted_array =AtomicArray::<usize>::new(world.team(), array_len, Distribution::Block).block();
// let mut pe_indices = (array.first_global_index_for_pe(my_pe).unwrap()..=array.last_global_index_for_pe(my_pe).unwrap()).collect::<Vec<_>>();
// let mut local_perm_indices = (0..array.num_elems_local()).collect::<Vec<_>>();
// let mut rng = thread_rng();
// // Shuffle the vector
// pe_indices.shuffle(&mut rng);
// local_perm_indices.shuffle(&mut rng);
// // let permuted_array_clone = permuted_array.clone();
// // array.write_local_chunks(chunk_size).block().enumerate().for_each(move |(i,chunk)| {
// // // if we know the permutation is local
// // // let permuted_local = permuted_array_clone.local_data();
// // // for (p_i, elem) in local_perm_indices[i*chunk_size..std::cmp::min((i+1)*chunk_size,local_perm_indices.len())].iter().zip(chunk.iter()){
// // // permuted_local.at(*p_i).store(*elem);
// // // }
// // // if the permute may contain remote ops -- apply permute to each element individually
// // // for (p_i,elem) in pe_indices[i*chunk_size..std::cmp::min((i+1)*chunk_size,pe_indices.len())].iter().zip(chunk.iter()){
// // // let _ = permuted_array_clone.store(*p_i, *elem).spawn();
// // // }
// // // the above is pretty slow as single element operations are currently not optimized
// // // instead we can use the batch store operation
// // let permuted_indices = pe_indices[i*chunk_size..std::cmp::min((i+1)*chunk_size,pe_indices.len())].iter().map(|e| *e).collect::<Vec<_>>();
// // let _ = permuted_array_clone.batch_store(permuted_indices, chunk.as_ref()).spawn();
// // }).block();
// // world.wait_all();
// // world.barrier();
// // permuted_array.print();
// //likely the best though would be simply do a batch store of the local data, using the permuted indices
// let local_data = array.read_local_data().block();
// let _ = permuted_array.batch_store(pe_indices, &local_data).spawn();
// permuted_array.print();
// }
use Instant;
use *;
//use lamellar::array::Distribution;
// These seem to be necessary in order for it to compile.
/*
fn global_shuffle(A: &UnsafeArray::<SortElement>,
B: &UnsafeArray::<SortElement>,
world: &LamellarWorld,
n_per_task: usize) {
// Permute elements of A, storing the result into B
// The actual sort benchmark will use the current key and
// saved count information for the current task (which can
// be read back using 'tid'), in order to
// compute the destination index.
unsafe {
let _ =
A.local_chunks(n_per_task)
.enumerate().for_each(|(_tid,task_slice)| {
//
for elt in task_slice.iter() {
B.store(elt.key as usize, *elt);
}
});
}
}*/