~mht/cra

d77988e86d405d551b0f5bf5ff301835ac53bb26 — Martin Hafskjold Thoresen 5 months ago 7e22ace
cargo fmt

Turns out Kakoune is messing up my indentation >:(
3 files changed, 101 insertions(+), 88 deletions(-)

M src/log.rs
M src/main.rs
M src/output.rs
M src/log.rs => src/log.rs +59 -47
@@ 19,9 19,9 @@ pub struct Add {
    /// This is true when this add was an exhaustive add, namely that low(this) \notin other.
    pub exhaustive: bool,
    /// The ID of the reduced simplex
   	this_j: usize,
   	/// The ID of the other simplex
   	other_j: usize,
    this_j: usize,
    /// The ID of the other simplex
    other_j: usize,
}

/// Stats for reducting one simplex.


@@ 38,29 38,28 @@ pub struct SimplexReduction {
/// Stats for a whole run of the reduction algorithm.
#[derive(Debug)]
pub struct Reduction {
	pub reductions: Vec<SimplexReduction>
    pub reductions: Vec<SimplexReduction>,
}

/// This is the full data that we output.
#[derive(Debug)]
#[derive(Serialize)]
#[derive(Debug, Serialize)]
pub struct Aggregate {
    /// Simplex IDs of all additions.
    adds: Vec<(usize, usize)>,

	/// The number of adds required to reduce a simplex, by dimension.
    /// The number of adds required to reduce a simplex, by dimension.
    adds_required_by_dim: HashMap<isize, f64>,
    /// The average number of additions required to reduce a simplex, by dimension.
    avg_adds_required: HashMap<isize, f64>,

	/// The size of a simplex when we add them to reduce the first.
    /// The size of a simplex when we add them to reduce the first.
    add_size_by_dim: HashMap<isize, Vec<(usize, usize)>>,
    /// Average of the sizes.
    avg_add_size_by_dim: HashMap<isize, (f64, f64)>,
    /// Histogram of the sizes
    histogram_add_size_by_dim: HashMap<isize, HashMap<usize, (usize, usize)>>,

	/// The sum of sizes of simplices when we reduce
    /// The sum of sizes of simplices when we reduce
    add_cost_by_dim: HashMap<isize, Vec<usize>>,
    avg_add_cost_by_dim: HashMap<isize, f64>,
    histogram_add_cost_by_dim: HashMap<isize, HashMap<usize, usize>>,


@@ 69,7 68,7 @@ pub struct Aggregate {
impl Reduction {
    pub fn new() -> Self {
        Self {
            reductions: Vec::new()
            reductions: Vec::new(),
        }
    }



@@ 85,7 84,7 @@ impl Reduction {
            }
        }

		let mut adds_required_by_dim = HashMap::new();
        let mut adds_required_by_dim = HashMap::new();
        let mut count = HashMap::new();
        for r in self.reductions.iter() {
            *adds_required_by_dim.entry(r.dim).or_insert(0.0) += r.adds.len() as f64;


@@ 97,10 96,11 @@ impl Reduction {
            avg_adds_required.insert(k, adds_required_by_dim.get(&k).unwrap() / c as f64);
        }


        let mut add_size_by_dim = HashMap::<isize, Vec<(usize, usize)>>::new();
        for r in self.reductions.iter() {
            if r.adds.len() == 0 { continue; }
            if r.adds.len() == 0 {
                continue;
            }
            let v = add_size_by_dim.entry(r.dim).or_insert(Vec::new());
            for a in r.adds.iter() {
                v.push((a.this_size, a.other_size));


@@ 109,25 109,33 @@ impl Reduction {

        let mut avg_add_size_by_dim = HashMap::new();
        for (&dim, adds) in add_size_by_dim.iter() {
            if adds.len() == 0 { continue; }
            if adds.len() == 0 {
                continue;
            }
            let avg_f = adds.iter().map(|(f, _s)| *f).sum::<usize>() as f64 / adds.len() as f64;
            let avg_s = adds.iter().map(|(_f, s)| *s).sum::<usize>() as f64 / adds.len() as f64;
            avg_add_size_by_dim.insert(dim, (avg_f, avg_s));
       	}
        }

       	let mut histogram_add_size_by_dim = HashMap::new();
        let mut histogram_add_size_by_dim = HashMap::new();
        for r in self.reductions.iter() {
            if r.adds.len() == 0 { continue; }
            let histogram: &mut HashMap<usize, (usize, usize)> = histogram_add_size_by_dim.entry(r.dim).or_insert(HashMap::new());
            if r.adds.len() == 0 {
                continue;
            }
            let histogram: &mut HashMap<usize, (usize, usize)> = histogram_add_size_by_dim
                .entry(r.dim)
                .or_insert(HashMap::new());
            for add in r.adds.iter() {
                histogram.entry(add.this_size).or_insert((0, 0)).0 += 1;
                histogram.entry(add.other_size).or_insert((0, 0)).1 += 1;
            }
       	}
        }

        let mut add_cost_by_dim = HashMap::<isize, Vec<usize>>::new();
        for r in self.reductions.iter() {
            if r.adds.len() == 0 { continue; }
            if r.adds.len() == 0 {
                continue;
            }
            let v = add_cost_by_dim.entry(r.dim).or_insert(Vec::new());
            for a in r.adds.iter() {
                v.push(a.this_size + a.other_size);


@@ 136,20 144,25 @@ impl Reduction {

        let mut avg_add_cost_by_dim = HashMap::new();
        for (&dim, adds) in add_cost_by_dim.iter() {
            if adds.len() == 0 { continue; }
            if adds.len() == 0 {
                continue;
            }
            let avg = adds.iter().map(|f| *f).sum::<usize>() as f64 / adds.len() as f64;
            avg_add_cost_by_dim.insert(dim, avg);
       	}
        }

       	let mut histogram_add_cost_by_dim = HashMap::new();
        let mut histogram_add_cost_by_dim = HashMap::new();
        for r in self.reductions.iter() {
            if r.adds.len() == 0 { continue; }
            let histogram: &mut HashMap<usize, usize> = histogram_add_cost_by_dim.entry(r.dim).or_insert(HashMap::new());
            if r.adds.len() == 0 {
                continue;
            }
            let histogram: &mut HashMap<usize, usize> = histogram_add_cost_by_dim
                .entry(r.dim)
                .or_insert(HashMap::new());
            for add in r.adds.iter() {
                *histogram.entry(add.this_size + add.other_size).or_insert(0) += 1;
            }
       	}

        }

        Aggregate {
            adds,


@@ 167,25 180,25 @@ impl Reduction {

impl Add {
    pub fn regular(this: &Simplex, other: &Simplex) -> Self {
		Self {
    		dim: this.dim,
    		this_size: this.faces.len(),
    		other_size: other.faces.len(),
    		exhaustive: false,
    		this_j: this.j,
    		other_j: other.j,
		}
        Self {
            dim: this.dim,
            this_size: this.faces.len(),
            other_size: other.faces.len(),
            exhaustive: false,
            this_j: this.j,
            other_j: other.j,
        }
    }

    pub fn exhaustive(this: &Simplex, other: &Simplex) -> Self {
		Self {
    		dim: this.dim,
    		this_size: this.faces.len(),
    		other_size: other.faces.len(),
    		exhaustive: true,
    		this_j: this.j,
    		other_j: other.j,
		}
        Self {
            dim: this.dim,
            this_size: this.faces.len(),
            other_size: other.faces.len(),
            exhaustive: true,
            this_j: this.j,
            other_j: other.j,
        }
    }
}



@@ 198,8 211,7 @@ impl SimplexReduction {
        }
    }

	pub fn add(self: &mut Self, add: Add) {
    	self.adds.push(add)
	}
    pub fn add(self: &mut Self, add: Add) {
        self.adds.push(add)
    }
}


M src/main.rs => src/main.rs +5 -4
@@ 363,15 363,16 @@ is it's sequence number in the file. See `data/tet.boundary` for an example of a

    if let Some(path) = matches.value_of("stats") {
        let mut stats_file = open_file_or_stdout(path).unwrap();
        let json = serde_json::to_string(&reduction.log.aggregate()).expect("failed to convert stats to .json");
        let json = serde_json::to_string(&reduction.log.aggregate())
            .expect("failed to convert stats to .json");
        write!(stats_file, "{}", json).expect("failed to write stats to file");
        was_useful = true;
    }

    // if let Some(path) = matches.value_of("textable") {
        // let mut tex_file = File::create(Path::new(path)).unwrap();
        // TODO: FIXME
        // output::tex_table(&stats, &stats, &mut tex_file).unwrap();
    // let mut tex_file = File::create(Path::new(path)).unwrap();
    // TODO: FIXME
    // output::tex_table(&stats, &stats, &mut tex_file).unwrap();
    // }

    if let Some(path) = matches.value_of("graphviz") {

M src/output.rs => src/output.rs +37 -37
@@ 397,11 397,11 @@ plot '{}' using 1:2 pt 7 ps 0.25 title "pairs", x"#,
//     fn sum(s: &[usize]) -> f64 {
//         s.iter().sum::<usize>() as f64
//     }
// 
//
//     fn avg(s: &[usize]) -> f64 {
//         sum(s) / s.len() as f64
//     }
// 
//
//     fn percent(hundred: f64, ours: f64) -> String {
//         if hundred == 0.0 {
//             format!("")


@@ 409,7 409,7 @@ plot '{}' using 1:2 pt 7 ps 0.25 title "pairs", x"#,
//             format!("{:.2}\\%", 100.0 * ours / hundred)
//         }
//     }
// 
//
//     let header_align = std::iter::repeat("r l")
//         .take(MAX_DIM)
//         .collect::<Vec<&str>>()


@@ 426,7 426,7 @@ plot '{}' using 1:2 pt 7 ps 0.25 title "pairs", x"#,
// "#,
//         header_align, header_top
//     )?;
// 
//
//     let total = reg.num_of_dimens.iter().sum::<usize>();
//     let mut line = format!("Simplices & {}", total);
//     for &n in reg.num_of_dimens.iter() {


@@ 437,7 437,7 @@ plot '{}' using 1:2 pt 7 ps 0.25 title "pairs", x"#,
//         ));
//     }
//     writeln!(writer, "{}\\\\", line)?;
// 
//
//     let total = reg.skip_d.iter().sum::<usize>();
//     let mut line = format!("Skipped & {}", total);
//     for &n in reg.skip_d.iter() {


@@ 448,7 448,7 @@ plot '{}' using 1:2 pt 7 ps 0.25 title "pairs", x"#,
//         ));
//     }
//     writeln!(writer, "{}\\\\", line)?;
// 
//
//     let total = reg.zeroed_d.iter().sum::<usize>();
//     let mut line = format!("Zeroed & {}", total);
//     for &n in reg.zeroed_d.iter() {


@@ 459,7 459,7 @@ plot '{}' using 1:2 pt 7 ps 0.25 title "pairs", x"#,
//         ));
//     }
//     writeln!(writer, "{}\\\\", line)?;
// 
//
//     let total = reg.placed_full_d.iter().sum::<usize>();
//     let mut line = format!("Stored & {}", total);
//     for &n in reg.placed_full_d.iter() {


@@ 470,26 470,26 @@ plot '{}' using 1:2 pt 7 ps 0.25 title "pairs", x"#,
//         ));
//     }
//     writeln!(writer, "{}\\\\", line)?;
// 
//
//     writeln!(writer, "\\end{{tabular}}\n")?;
//     writeln!(writer, "\\vspace{{1cm}}")?;
// 
//
//     let mut v: Vec<(String, String, String, String)> = Vec::new();
// 
//
//     v.push((
//         format!("Column additions"),
//         thousand_split(reg.col_adds),
//         thousand_split(exh.col_adds),
//         percent(reg.col_adds as f64, exh.col_adds as f64),
//     ));
// 
//
//     v.push((
//         format!("Estimate \\texttt{{column\\_add}} cost"),
//         thousand_split(sum(&reg.add_size_sum) as usize),
//         thousand_split(sum(&exh.add_size_sum) as usize),
//         percent(sum(&reg.add_size_sum), sum(&exh.add_size_sum)),
//     ));
// 
//
//     v.push((
//         format!("\\texttt{{simplex\\_with\\_low}} queries"),
//         thousand_split(reg.simplex_with_low_queries),


@@ 499,14 499,14 @@ plot '{}' using 1:2 pt 7 ps 0.25 title "pairs", x"#,
//             exh.simplex_with_low_queries as f64,
//         ),
//     ));
// 
//
//     v.push((
//         format!("Time spent (ns)"),
//         thousand_split(reg.time as usize),
//         thousand_split(exh.time as usize),
//         percent(reg.time as f64, exh.time as f64),
//     ));
// 
//
//     let as0 = |s: &[(usize, usize)]| s.iter().map(|(a, _)| a).cloned().collect::<Vec<_>>();
//     v.push((
//         format!("Size of first operand"),


@@ 514,7 514,7 @@ plot '{}' using 1:2 pt 7 ps 0.25 title "pairs", x"#,
//         format!("{:.4}", avg(&as0(&exh.add_size))),
//         percent(avg(&as0(&reg.add_size)), avg(&as0(&exh.add_size))),
//     ));
// 
//
//     let as1 = |s: &[(usize, usize)]| s.iter().map(|(_, b)| b).cloned().collect::<Vec<_>>();
//     v.push((
//         format!("Size of second operand"),


@@ 522,35 522,35 @@ plot '{}' using 1:2 pt 7 ps 0.25 title "pairs", x"#,
//         format!("{:.4}", avg(&as1(&exh.add_size))),
//         percent(avg(&as1(&reg.add_size)), avg(&as1(&exh.add_size))),
//     ));
// 
//
//     v.push((
//         format!("Iters for reducing a column"),
//         format!("{:.4}", avg(&reg.num_iters)),
//         format!("{:.4}", avg(&exh.num_iters)),
//         percent(avg(&reg.num_iters), avg(&exh.num_iters)),
//     ));
// 
//
//     v.push((
//         format!("Cost of one column addition"),
//         format!("{:.4}", avg(&reg.add_size_sum)),
//         format!("{:.4}", avg(&exh.add_size_sum)),
//         percent(avg(&reg.add_size_sum), avg(&exh.add_size_sum)),
//     ));
// 
//
//     v.push((
//         format!("Iters to exh.\\ reduce a col"),
//         format!(""),
//         format!("{:.4}", avg(&exh.ex_reductions)),
//         format!(""),
//     ));
// 
//
//     v.push((
//         format!("Searches to find \\texttt{{j=low(k)}}"),
//         format!(""),
//         format!("{:.4}", avg(&exh.ex_searches)),
//         format!(""),
//     ));
// 
//
//     // TODO: 4 could be bigger!
//     let mut reg_counts = [0; 4];
//     for &d in &reg.col_add_dimens {


@@ 560,7 560,7 @@ plot '{}' using 1:2 pt 7 ps 0.25 title "pairs", x"#,
//     for &d in &exh.col_add_dimens {
//         exh_counts[(d + 1) as usize] += 1;
//     }
// 
//
//     for (d1, (&r, &e)) in reg_counts.iter().zip(exh_counts.iter()).enumerate().skip(1) {
//         v.push((
//             format!("Column adds with $d={}$", d1 as isize - 1),


@@ 569,7 569,7 @@ plot '{}' using 1:2 pt 7 ps 0.25 title "pairs", x"#,
//             percent(r as f64, e as f64),
//         ));
//     }
// 
//
//     write!(
//         writer,
//         r#"\begin{{tabular}}{{r r r r}}


@@ 581,7 581,7 @@ plot '{}' using 1:2 pt 7 ps 0.25 title "pairs", x"#,
//         write!(writer, "  {} & {} & {} & {}\\\\\n", l, a, b, p)?;
//     }
//     writeln!(writer, "\\end{{tabular}}")?;
// 
//
//     Ok(())
// }



@@ 694,47 694,47 @@ pub fn run_gnuplot(script: &str, out_file: &Path) {
//
// pub fn stats<W: Write>(st: &Statistics, writer: &mut W) -> Result<(), std::io::Error> {
//     let mut print_pairs = Vec::new();
// 
//
//     print_pairs.push(("Column additions", thousand_split(st.col_adds)));
// 
//
//     let adds = st.add_size_sum.iter().sum::<usize>() as usize;
//     print_pairs.push(("Estimate total 'column_add' cost", thousand_split(adds)));
// 
//
//     print_pairs.push((
//         "`simplex_with_low` queries",
//         thousand_split(st.simplex_with_low_queries),
//     ));
// 
//
//     print_pairs.push(("Time spent (ns)", thousand_split(st.time as usize)));
// 
//
//     // ~10 cycles per L2 hit seems reasonable?
//     let instr_cost_estimate = st.simplex_with_low_queries * 10 + adds;
//     print_pairs.push((
//         "Instruction cost estimate",
//         thousand_split(instr_cost_estimate),
//     ));
// 
//
//     let add_size0 =
//         st.add_size.iter().map(|(a, _)| a).sum::<usize>() as f64 / st.add_size.len() as f64;
//     print_pairs.push(("Average size of first operand", format!("{}", add_size0)));
// 
//
//     let add_size1 =
//         st.add_size.iter().map(|(_, b)| b).sum::<usize>() as f64 / st.add_size.len() as f64;
//     print_pairs.push(("Average size of second operand", format!("{}", add_size1)));
// 
//
//     let num_iters = st.num_iters.iter().sum::<usize>() as f64 / st.num_iters.len() as f64;
//     print_pairs.push((
//         "Average #iterations before reducing a column",
//         format!("{}", num_iters),
//     ));
// 
//
//     let column_add_cost =
//         st.add_size_sum.iter().sum::<usize>() as f64 / st.add_size_sum.len() as f64;
//     print_pairs.push((
//         "Average cost of one column addition",
//         format!("{}", column_add_cost),
//     ));
// 
//
//     if st.ex_reductions.len() > 0 {
//         let ex_reductions =
//             st.ex_reductions.iter().sum::<usize>() as f64 / st.ex_reductions.len() as f64;


@@ 743,7 743,7 @@ pub fn run_gnuplot(script: &str, out_file: &Path) {
//             format!("{}", ex_reductions),
//         ));
//     }
// 
//
//     if st.ex_searches.len() > 0 {
//         let ex_searches = st.ex_searches.iter().sum::<usize>() as f64 / st.ex_searches.len() as f64;
//         print_pairs.push((


@@ 751,14 751,14 @@ pub fn run_gnuplot(script: &str, out_file: &Path) {
//             format!("{}", ex_searches),
//         ));
//     }
// 
//
//     print_pairs.push(("Number of zeroed columns", thousand_split(st.zeroed)));
//     print_pairs.push(("Number of single columns", thousand_split(st.placed_single)));
//     print_pairs.push((
//         "Number of remaining columns",
//         thousand_split(st.placed_full),
//     ));
// 
//
//     let mut last_pos = 0;
//     for (i, &n) in st.num_of_dimens.iter().enumerate() {
//         if n > 0 {


@@ 771,7 771,7 @@ pub fn run_gnuplot(script: &str, out_file: &Path) {
//     for d in 0..last_pos + 1 {
//         print_pairs.push((&strs[d], thousand_split(st.num_of_dimens[d])));
//     }
// 
//
//     let max_str = print_pairs.iter().map(|(s, _)| s.len()).max().unwrap();
//     for (left, right) in print_pairs {
//         write!(writer, "{:>2$}: {}\n", left, right, max_str)?;