webgraph-cli 0.5.0

Command line interface for the Rust port of the WebGraph framework (http://webgraph.di.unimi.it/).
Documentation
/*
 * SPDX-FileCopyrightText: 2023 Inria
 * SPDX-FileCopyrightText: 2023 Sebastiano Vigna
 * SPDX-FileCopyrightText: 2023 Tommaso Fontana
 *
 * SPDX-License-Identifier: Apache-2.0 OR MIT
 */

use anyhow::Result;
use clap::Parser;
use dsi_progress_logger::prelude::*;
use std::collections::VecDeque;
use std::path::PathBuf;
use sux::prelude::BitVec;
use sux::traits::BitVecOpsMut;
use webgraph::prelude::*;

#[derive(Parser, Debug)]
#[command(name = "bf-visit", about = "Benchmarks a breadth-first visit.", long_about = None, next_line_help = true)]
pub struct CliArgs {
    /// The basename of the graph.​
    pub basename: PathBuf,
    /// Static dispatch (default parameters for the BV format).​
    #[arg(short = 'S', long = "static")]
    pub _static: bool,
    /// Number of repeats (usually to warm up the cache or memory mapping).​
    #[arg(short = 'R', long, default_value_t = 1)]
    pub repeats: usize,

    #[clap(long, default_value = "false")]
    /// Use memory mapping instead of loading the graph into memory.​
    pub mmap: bool,
}

pub fn main(args: CliArgs) -> Result<()> {
    // Loads the graph with the given endianness and load mode, and visits it.
    macro_rules! load_and_visit {
        ($E:ty, $mode:ty) => {{
            let config = BvGraph::with_basename(&args.basename)
                .mode::<$mode>()
                .flags(MemoryFlags::TRANSPARENT_HUGE_PAGES | MemoryFlags::RANDOM_ACCESS)
                .endianness::<$E>();
            if args._static {
                visit(config.dispatch::<Static>().load()?)
            } else {
                visit(config.load()?)
            }
        }};
    }

    for _ in 0..args.repeats {
        dispatch_endianness!(&args.basename, |E| if args.mmap {
            load_and_visit!(E, Mmap)
        } else {
            load_and_visit!(E, LoadMmap)
        })?;
    }
    Ok(())
}

fn visit(graph: impl RandomAccessGraph) -> Result<()> {
    let num_nodes = graph.num_nodes();
    let mut seen: BitVec = BitVec::new(num_nodes);
    let mut queue = VecDeque::new();

    let mut pl = progress_logger![
        display_memory = true,
        item_name = "node",
        local_speed = true,
        expected_updates = Some(num_nodes),
    ];
    pl.start("Visiting graph...");

    for start in 0..num_nodes {
        if seen[start] {
            continue;
        }
        queue.push_back(start as _);
        seen.set(start, true);

        while !queue.is_empty() {
            pl.light_update();
            let current_node = queue.pop_front().unwrap();
            for succ in graph.successors(current_node) {
                if !seen[succ] {
                    queue.push_back(succ);
                    seen.set(succ as _, true);
                }
            }
        }
    }

    pl.done();

    Ok(())
}