caminos-lib 0.6.3

A modular interconnection network simulator.
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

use ::rand::{rngs::StdRng};
use std::fmt::Debug;
use std::convert::TryInto;
use quantifiable_derive::Quantifiable;//the derive macro
use super::{
	Topology,TopologyBuilderArgument,CartesianData,Location,
};
use crate::{
	config_parser::ConfigurationValue,
	quantify::Quantifiable,
};

//For the slimfly we need for x,y in the field, i integer
//pow(x,i)
//x-y
//x*y
//field.size()
trait FlatRing : Debug + Quantifiable
{
	fn size(&self) -> usize;
	// Given an `integer` find the element in the ring corresponding to `integer` times the multiplicative unit of the ring.
	fn index_from_i32(&self, integer: i32) -> usize;
	fn add(&self, a:usize, b:usize) -> usize;
	fn sub(&self, a:usize, b:usize) -> usize;
	fn mul(&self, a:usize, b:usize) -> usize;
	fn pow(&self, a:usize, exp:u32) -> usize
	{
		let mut current=1;
		let mut rem_exp=exp;
		let mut factor=a;
		while rem_exp > 0
		{
			if rem_exp % 2 == 1
			{
				current=self.mul(current,factor);
			}
			rem_exp/=2;
			factor = self.mul(factor,factor);
		}
		current
	}
	///return (x,y) with a^x=y and y with the least possible representation.
	///Assuming 0=zero, 1=one.
	fn order(&self, a:usize) -> (usize,usize)
	{
		let mut ret=(1,a);
		if a==0
		{
			return ret;
		}
		let mut current = a;
		//let n = self.size();
		let mut exp=1;
		loop{
			current=self.mul(current,a);
			if current==ret.1
			{
				//We have found a cycle
				return ret;
			}
			exp+=1;
			if current==1
			{
				return (exp,current);
			}
			if current<ret.1
			{
				//update
				ret=(exp,current);
			}
		}
	}
	fn is_primitive(&self, a:usize) -> bool
	{
		let n = self.size();
		//FIXME: n even
		let half:u32 = ((n-1)/2).try_into().unwrap();
		let prev = self.pow(a,half);
		prev!=1 && self.mul(prev,prev)==1
	}
}

#[derive(Debug,Quantifiable)]
struct IntegerIdealRing
{
	modulo: usize
}

impl FlatRing for IntegerIdealRing
{
	fn size(&self) -> usize
	{
		self.modulo
	}
	fn index_from_i32(&self, integer: i32) -> usize
	{
		let mut ret:i32 = integer;
		let m :i32 = self.modulo as i32;
		while ret<0
		{
			ret += m;
		}
		ret %= m;
		ret as usize
	}
	fn add(&self, a:usize, b:usize) -> usize
	{
		(a+b) % self.modulo
	}
	fn sub(&self, a:usize, b:usize) -> usize
	{
		(self.modulo + a -b) % self.modulo
	}
	fn mul(&self, a:usize, b:usize) -> usize
	{
		(a*b) % self.modulo
	}
}


struct SlimFlyCoordinates
{
	//in field
	local: usize,
	//in field
	global: usize,
	//0 o 1
	block: usize,
}

impl SlimFlyCoordinates
{
	fn unpack(index:usize,size:usize) -> SlimFlyCoordinates
	{
		let local = index % size;
		let other = index / size;
		let global = other % size;
		let block = other / size;
		SlimFlyCoordinates{
			local,
			global,
			block,
		}
	}
	fn pack(&self,size:usize) -> usize
	{
		(self.block * size + self.global)*size + self.local
	}
}


#[derive(Debug,Quantifiable)]
pub struct SlimFly
{
	field: Box<dyn FlatRing>,
	primitive: usize,
	servers_per_router: usize,
	paley_sets: [Vec<usize>;2],
	neg_paley_sets: [Vec<usize>;2],
}

impl Topology for SlimFly
{
	fn num_routers(&self) -> usize
	{
		//self.plane.size()
		let n=self.field.size();
		n*n*2
	}
	fn num_servers(&self) -> usize
	{
		self.servers_per_router * self.num_routers()
	}
	///Neighbours of a router: Location+link class index
	///Routers should be before servers
	fn neighbour(&self, router_index:usize, port:usize) -> (Location,usize)
	{
		let n = self.field.size();
		let router_coords = SlimFlyCoordinates::unpack(router_index,n);
		if port < self.paley_sets[0].len()
		{
			//local link. class 0.
			//let neighbour_local_partial = self.field.add(router_coords.local,self.paley_set[port]);
			//let neighbour_local = if router_coords.block==0 { neighbour_local_partial } else { self.field.mul(neighbour_local_partial,self.primitive) };
			let neighbour_local= self.field.add(router_coords.local,self.paley_sets[router_coords.block][port]);
			return (Location::RouterPort{
				router_index: SlimFlyCoordinates{local:neighbour_local,..router_coords}.pack(n),
				//router_port: self.paley_set.len()-1-port,
				//router_port: self.neg_paley_set[port],
				router_port: self.neg_paley_sets[router_coords.block][port],
			},0);
		}
		let offset=port - self.paley_sets[0].len();
		if offset < self.field.size()
		{
			//global link. class 1.
			//y2=y1 - x1*x2
			//y1=y2 + x1*x2
			let global_product = self.field.mul(router_coords.global,offset);
			let neighbour_local = if router_coords.block==0
			{
				self.field.sub(router_coords.local,global_product)
			}
			else
			{
				self.field.add(router_coords.local,global_product)
			};
			return (Location::RouterPort{
				router_index: SlimFlyCoordinates{local:neighbour_local,global:offset,block:1-router_coords.block}.pack(n),
				router_port: self.paley_sets[0].len() + router_coords.global,
			},1);
		}
		let offset = offset - self.field.size();
		(Location::ServerPort(offset+router_index*self.servers_per_router),2)
		//let neighs = self.plane.incident_points(router_index).unwrap_or_else(|_|panic!("invalid router_index={}",router_index));
		//if port<neighs.len()
		//{
		//	let (neighbour_router, neighbour_port) = neighs[port];
		//	if neighbour_router == router_index
		//	{
		//		//Remove loops.
		//		(Location::None,0)
		//	}
		//	else
		//	{
		//		(Location::RouterPort{
		//			router_index: neighbour_router,
		//			router_port: neighbour_port,
		//		},0)
		//	}
		//}
		//else
		//{
		//	let offset = port - neighs.len();
		//	(Location::ServerPort(offset+router_index*self.servers_per_router),1)
		//}
	}
	///The neighbour of a server: Location+link class index
	fn server_neighbour(&self, server_index:usize) -> (Location,usize)
	{
		let router_index = server_index/self.servers_per_router;
		let router_port = (server_index % self.servers_per_router) + self.degree(router_index);
		(Location::RouterPort{
			router_index,
			router_port,
		},2)
	}
	///the greatest distance from server to server
	fn diameter(&self) -> usize
	{
		2
	}
	///Distance from a router to another.
	fn distance(&self,origin:usize,destination:usize) -> usize
	{
		if origin==destination
		{
			0
		} else
		{
			let origin_coords = SlimFlyCoordinates::unpack(origin,self.field.size());
			let destination_coords = SlimFlyCoordinates::unpack(destination,self.field.size());
			if origin_coords.block==destination_coords.block && origin_coords.global==destination_coords.global
			{
				let local_diff=self.field.sub(origin_coords.local,destination_coords.local);
				//let shifted_diff = if origin_coords.block==0 { local_diff } else { self.field.mul(local_diff,self.primitive) };
				//if self.paley_set.contains(&shifted_diff)
				if self.paley_sets[origin_coords.block].contains(&local_diff)
				{
					//local link (class 0)
					return 1;
				}
			}
			if origin_coords.block!=destination_coords.block
			{
				let (left,right) = if origin_coords.block==0 {(origin_coords,destination_coords)} else {(destination_coords,origin_coords)};
				let local_diff = self.field.sub(left.local,right.local);
				let global_prod = self.field.mul(left.global,right.global);
				if local_diff==global_prod
				{
					//global link (class 1)
					return 1;
				}
			}
			2
		}
	}
	///Number of shortest paths from a router to another.
	fn amount_shortest_paths(&self,_origin:usize,_destination:usize) -> usize
	{
		todo!();
	}
	///Average number of shortest paths from a router to another.
	fn average_amount_shortest_paths(&self) -> f32
	{
		todo!();
	}
	//fn eigenvalue_powerdouble(&self) -> f32
	fn maximum_degree(&self) -> usize
	{
		self.paley_sets[0].len() + self.field.size()
	}
	fn minimum_degree(&self) -> usize
	{
		self.paley_sets[0].len() + self.field.size()
	}
	/// Number of ports used to other routers.
	fn degree(&self, _router_index: usize) -> usize
	{
		self.paley_sets[0].len() + self.field.size()
	}
	fn ports(&self, router_index: usize) -> usize
	{
		self.degree(router_index) + self.servers_per_router
	}
	///Specific for some toologies, but must be checkable for anyone
	fn cartesian_data(&self) -> Option<&CartesianData>
	{
		None
	}
	///Specific for some toologies, but must be checkable for anyone
	fn coordinated_routing_record(&self, _coordinates_a:&[usize], _coordinates_b:&[usize], _rng: Option<&mut StdRng>)->Vec<i32>
	{
		unimplemented!()
	}
	///Specific for some toologies, but must be checkable for anyone
	/// Indicates if going from input_port to output_port implies a direction change. Used for the bubble routing.
	fn is_direction_change(&self, _router_index:usize, _input_port: usize, _output_port: usize) -> bool
	{
		false
	}
	fn up_down_distance(&self,_origin:usize,_destination:usize) -> Option<(usize,usize)>
	{
		None
	}
}

impl SlimFly
{
	pub fn new(arg:TopologyBuilderArgument) -> SlimFly
	{
		let mut prime=None;
		let mut primitive=None;
		let mut servers_per_router=None;
		if let &ConfigurationValue::Object(ref cv_name, ref cv_pairs)=arg.cv
		{
			if cv_name!="SlimFly"
			{
				panic!("A SlimFly must be created from a `SlimFly` object not `{}`",cv_name);
			}
			for &(ref name,ref value) in cv_pairs
			{
				match name.as_ref()
				{
					"prime" => match value
					{
						&ConfigurationValue::Number(f) => prime=Some(f as usize),
						_ => panic!("bad value for prime"),
					},
					"primitive" => match value
					{
						&ConfigurationValue::Number(f) => primitive=Some(f as usize),
						_ => panic!("bad value for primitive"),
					},
					"servers_per_router" => match value
					{
						&ConfigurationValue::Number(f) => servers_per_router=Some(f as usize),
						_ => panic!("bad value for servers_per_router"),
					},
					"legend_name" => (),
					_ => panic!("Nothing to do with field {} in RandomRegularGraph",name),
				}
			}
		}
		else
		{
			panic!("Trying to create a NeighboursLists from a non-Object");
		}
		let prime=prime.expect("There were no prime");
		let servers_per_router=servers_per_router.expect("There were no servers_per_router");
		let field = IntegerIdealRing{modulo:prime};
		let primitive=primitive.unwrap_or_else(||{
			let n=field.size();
			(2..n).find(|x|field.is_primitive(*x)).unwrap_or_else(||panic!("Could not find a primtive element in the ring {:?}",field))
		});
		let epsilon:i32 = {
			let p4 = prime % 4;
			if p4==3 {-1} else {1}
		};
		let paley_set:Vec<usize>=match epsilon
		{
			1 =>
			{
				let limit :u32 = (prime as u32-1)/2;
				(0..limit).map(|k|field.pow(primitive,2*k)).collect()
			},
			-1 =>
			{
				let limit :u32= (prime as u32-3)/4;
				(0..=limit).map(|k|2*k).chain( (0..=limit).map(|k|(prime as u32-1)/2 + 2*k) ).map(|exp|field.pow(primitive,exp)).collect()
			},
			0 =>
			{
				let limit :u32= prime as u32/2;
				(0..limit).map(|k|field.pow(primitive,2*k)).collect()
			},
			_ => panic!("{} cannot be a prime",prime),
		};
		println!("primitive={} paley_set={:?} len={} (q-eps)/2={}",primitive,paley_set,paley_set.len(),(prime as i32-epsilon)/2);
		let second_paley_set=paley_set.iter().map(|x|field.mul(*x,primitive)).collect();
		let paley_sets:[Vec<usize>;2]=[paley_set,second_paley_set];
		//let neg_paley_sets=(0..=1).map(|b|(0..paley_sets[b].len()).map(|k|{
		//	let elem=paley_sets[b][k];
		//	let neg_elem=field.sub(0,elem);
		//	paley_sets[b].iter().enumerate().find(|(_,x)|**x==neg_elem).expect("the Paley should be circulant").0
		//}).collect()).collect::<Vec<_>>().try_into().unwrap();
		let neg_paley_sets=
		{
			let builder=|b:usize|{
				(0..paley_sets[b].len()).map(|k|{
					let elem=paley_sets[b][k];
					let neg_elem=field.sub(0,elem);
					paley_sets[b].iter().enumerate().find(|(_,x)|**x==neg_elem).expect("the Paley should be circulant").0
			}).collect()};
			[builder(0),builder(1)]
		};
		SlimFly{
			field: Box::new(field),
			primitive,
			servers_per_router,
			paley_sets,
			neg_paley_sets,
		}
	}
}