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
use crate::slot::Slot::{Branch, FoldHood, Nbr, Rep};
use crate::vm::round_vm::RoundVM;
use std::str::FromStr;
pub mod builtins;
pub mod execution;
pub mod macros;
/// Observes the value of an expression across neighbors, producing a “field of fields”.
///
/// # Arguments
///
/// * `vm` the current VM
/// * `expr` the expression to evaluate
///
/// # Generic Parameters
///
/// * `A` The type of value returned by the expression.
/// * `F` - The type of the closure, which must be a closure that takes a `RoundVM` as argument and returns a tuple `(RoundVM, A)`.
///
/// # Returns
///
/// the value of the expression
pub fn nbr<A: Clone + 'static + FromStr, F>(mut vm: RoundVM, expr: F) -> (RoundVM, A)
where
F: Fn(RoundVM) -> (RoundVM, A),
{
vm.nest_in(Nbr(vm.index().clone()));
let (mut vm_, val) = match vm.neighbor() {
Some(nbr) if nbr.clone() != vm.self_id() => match vm.neighbor_val::<A>() {
Ok(val) => (vm.clone(), val.clone()),
_ => expr(vm.clone()),
},
_ => expr(vm),
};
let res = vm_.nest_write(vm_.unless_folding_on_others(), val);
vm_.nest_out(true);
(vm_, res)
}
/// Iteratively updates the value of the input expression at each device using the last computed value.
///
/// # Arguments
///
/// * `vm` the current VM
/// * `init` the initial value
/// * `fun` the function to apply to the value
///
/// # Generic Parameters
///
/// * `A` The type of value returned by the expression.
/// * `F` - The type of the closure, which must be a closure that takes no arguments and returns a value of type `A`.
/// * `G` - The type of the closure, which must be a closure that takes a tuple `(RoundVM, A)` and returns a tuple `(RoundVM, A)`.
///
/// # Returns
///
/// the updated value
pub fn rep<A: Clone + 'static + FromStr, F, G>(mut vm: RoundVM, init: F, fun: G) -> (RoundVM, A)
where
F: Fn(RoundVM) -> (RoundVM, A),
G: Fn(RoundVM, A) -> (RoundVM, A),
{
vm.nest_in(Rep(vm.index().clone()));
let (mut vm_, val) = locally(vm, |vm1| {
if vm1.previous_round_val::<A>().is_ok() {
let prev = vm1.previous_round_val::<A>().unwrap().clone();
fun(vm1, prev)
} else {
let init_args = init(vm1);
fun(init_args.0, init_args.1)
}
});
let res = vm_.nest_write(vm_.unless_folding_on_others(), val);
vm_.nest_out(true);
(vm_, res)
}
/// Aggregates the results of the neighbor computation.
///
/// # Arguments
///
/// * `vm` the current VM
/// * `init` the initial value
/// * `aggr` the function to apply to the value
/// * `expr` the expression to evaluate
///
/// # Generic Parameters
///
/// * `A` The type of value returned by the expression.
/// * `F` - The type of inti, which must be a closure that takes no arguments and returns a value of type `A`.
/// * `G` - The type of aggr, which must be a closure that takes a tuple `(A, A)` and returns a value of type `A`.
/// * `H` - The type of expr, which must be a closure that takes a `RoundVM` as argument and returns a tuple `(RoundVM, A)`.
///
/// # Returns
///
/// the aggregated value
pub fn foldhood<A: Clone + 'static + FromStr, F, G, H>(
mut vm: RoundVM,
init: F,
aggr: G,
expr: H,
) -> (RoundVM, A)
where
F: Fn(RoundVM) -> (RoundVM, A),
G: Fn(A, A) -> A,
H: Fn(RoundVM) -> (RoundVM, A) + Copy,
{
vm.nest_in(FoldHood(vm.index().clone()));
let nbrs = vm.aligned_neighbours::<A>().clone();
let (vm_, local_init) = locally(vm, |vm_| init(vm_));
let temp_vec: Vec<A> = Vec::new();
let (vm__, nbrs_vec) = nbrs_computation(vm_, expr, temp_vec, nbrs, local_init.clone());
let (mut vm___, res) = isolate(vm__, |vm_| {
let val = nbrs_vec
.iter()
.fold(local_init.clone(), |x, y| aggr(x, y.clone()));
(vm_, val)
});
let res_ = vm___.nest_write(true, res);
vm___.nest_out(true);
(vm___, res_)
}
/// A utility function used by the `foldhood` function.
fn nbrs_computation<A: Clone + 'static>(
vm: RoundVM,
expr: impl Fn(RoundVM) -> (RoundVM, A),
mut tmp: Vec<A>,
mut ids: Vec<i32>,
init: A,
) -> (RoundVM, Vec<A>) {
if ids.len() == 0 {
return (vm, tmp);
} else {
let current_id = ids.pop();
let (vm_, res, expr_) = folded_eval(vm, expr, current_id);
tmp.push(res.unwrap_or(init.clone()).clone());
nbrs_computation(vm_, expr_, tmp, ids, init)
}
}
/// Partitions the domain into two subspaces that do not interact with each other.
///
/// # Arguments
///
/// * `vm` the current VM
/// * `cond` the condition to evaluate
/// * `thn` the expression to evaluate if the condition is true
/// * `els` the expression to evaluate if the condition is false
///
/// # Generic Parameters
///
/// * `A` The type of value returned by the expression.
/// * `B` - The type of cond, which must be a closure that takes no arguments and returns a value of type `bool`.
/// * `F` - The type of thn and els, which must be a closure that takes a `RoundVM` as argument and returns a tuple `(RoundVM, A)`.
///
/// # Returns
///
/// the value of the expression
pub fn branch<A: Clone + 'static + FromStr, B, TH, EL>(
mut vm: RoundVM,
cond: B,
thn: TH,
els: EL,
) -> (RoundVM, A)
where
B: Fn() -> bool,
TH: Fn(RoundVM) -> (RoundVM, A),
EL: Fn(RoundVM) -> (RoundVM, A),
{
vm.nest_in(Branch(vm.index().clone()));
let (vm, tag) = locally(vm, |_vm1| (_vm1, cond()));
let (mut vm_, val): (RoundVM, A) = match vm.neighbor() {
Some(nbr) if nbr.clone() != vm.self_id() => {
let val_clone = vm.neighbor_val::<A>().unwrap().clone();
(vm, val_clone)
}
_ => {
if tag {
locally(vm, |vm1| thn(vm1))
} else {
locally(vm, |vm1| els(vm1))
}
}
};
let res = vm_.nest_write(vm_.unless_folding_on_others(), val);
vm_.nest_out(tag);
(vm_, res)
}
/// Returns the id of the current device.
///
/// # Arguments
///
/// * `vm` the current VM
///
/// # Returns
///
/// the id of the current device
pub fn mid(vm: RoundVM) -> (RoundVM, i32) {
let mid = vm.self_id().clone();
(vm, mid)
}
/// Evaluates the given expression locally and return the result.
///
/// # Arguments
///
/// * `expr` The expression to evaluate.
///
/// # Generic Parameters
///
/// * `A` - The type of value returned by the expression.
/// * `F` - The type of the closure, which must be a mutable closure that takes no arguments and returns a value of type `A`.
///
/// # Returns
///
/// The result of the closure `expr`.
fn locally<A: Clone + 'static>(
mut vm: RoundVM,
expr: impl Fn(RoundVM) -> (RoundVM, A),
) -> (RoundVM, A) {
let current_neighbour = vm.neighbor().map(|id| id.clone());
vm.status = vm.status.fold_out();
let (mut vm_, result) = expr(vm);
vm_.status = vm_.status.fold_into(current_neighbour);
(vm_, result)
}
fn isolate<A: Clone + 'static, F>(mut vm: RoundVM, expr: F) -> (RoundVM, A)
where
F: Fn(RoundVM) -> (RoundVM, A),
{
vm.status = vm.status.fold_out();
let (mut vm_, result) = expr(vm);
vm_.status = vm_.status.fold_into(None);
(vm_, result)
}
/// Perform a folded evaluation of the given expression in the given neighbor and return the result.
/// Used by the `foldhood` function.
/// Same behavior as the folded_eval function in src/core/lang/round_vm.rs.
///
/// # Arguments
///
/// * `vm` - The current VM.
/// * `expr` - The expression to evaluate, which should return a value of type `A`.
/// * `id` - The id of the neighbor. It is of type `i32`.
///
/// # Generic Parameters
///
/// * `A` - The type of value returned by the expression.
/// * `F` - The type of the expr, which must be a closure that takes o `RoundVM` as an argument and returns a tuple of type `(RoundVM, A)`.
///
/// # Returns
///
/// A tuple of type `(RoundVM, Option<A>, F)`.
fn folded_eval<A: Clone + 'static, F>(
mut vm: RoundVM,
expr: F,
id: Option<i32>,
) -> (RoundVM, Option<A>, F)
where
F: Fn(RoundVM) -> (RoundVM, A),
{
vm.status = vm.status.push();
vm.status = vm.status.fold_into(id);
let (mut vm_, res) = expr(vm);
vm_.status = vm_.status.pop();
(vm_, Some(res), expr)
}