use crate::{
IntSliceFormat, LogIntervalArg, build_info, get_thread_pool, num_threads_parser, timed,
};
use anyhow::Result;
use clap::Parser;
use dsi_bitstream::prelude::factory::CodesReaderFactoryHelper;
use dsi_bitstream::prelude::*;
use dsi_progress_logger::progress_logger;
use std::path::PathBuf;
use webgraph::prelude::*;
#[derive(Parser, Debug)]
#[command(name = "webgraph-sccs", version=build_info::version_string(), max_term_width = 100, next_line_help = true, after_help = include_str!("common_env.txt"))]
pub struct Cli {
#[clap(flatten)]
args: CliArgs,
}
#[derive(Parser, Debug)]
pub struct CliArgs {
pub basename: PathBuf,
pub sccs: PathBuf,
#[arg(short = 's', long)]
pub sizes: Option<PathBuf>,
#[arg(short, long)]
pub renumber: bool,
#[arg(short = 'j', long, default_value_t = rayon::current_num_threads().max(1), value_parser = num_threads_parser)]
pub num_threads: usize,
#[arg(long, value_enum, default_value_t = IntSliceFormat::Ascii)]
pub fmt: IntSliceFormat,
#[clap(flatten)]
pub log_interval: LogIntervalArg,
}
pub fn cli_main<I, T>(args: I) -> Result<()>
where
I: IntoIterator<Item = T>,
T: Into<std::ffi::OsString> + Clone,
{
timed(|| main(Cli::parse_from(args).args))
}
pub fn main(args: CliArgs) -> Result<()> {
dispatch_endianness!(&args.basename, |E| sccs::<E>(args))
}
pub fn sccs<E: Endianness>(args: CliArgs) -> Result<()>
where
MemoryFactory<E, MmapHelper<u32>>: CodesReaderFactoryHelper<E>,
for<'a> LoadModeCodesReader<'a, E, LoadMmap>: BitSeek,
{
log::info!("Loading the graph from {}", args.basename.display());
let graph = BvGraph::with_basename(&args.basename)
.mode::<LoadMmap>()
.flags(MemoryFlags::TRANSPARENT_HUGE_PAGES | MemoryFlags::RANDOM_ACCESS)
.endianness::<E>()
.load()?;
let mut pl = progress_logger![
expected_updates = Some(graph.num_nodes()),
log_interval = args.log_interval.log_interval
];
let mut sccs = webgraph_algo::sccs::tarjan(graph, &mut pl);
log::info!(
"Found {} strongly connected components",
sccs.num_components()
);
if args.renumber {
log::info!("Renumbering components by decreasing size");
let component_sizes = if args.num_threads == 1 {
log::debug!("Using sequential algorithm");
sccs.sort_by_size()
} else {
let thread_pool = get_thread_pool(args.num_threads);
log::debug!("Using parallel algorithm with {} threads", args.num_threads);
thread_pool.install(|| sccs.par_sort_by_size())
};
if let Some(sizes_path) = &args.sizes {
let max = component_sizes.first().copied();
args.fmt.store(sizes_path, &component_sizes, max)?;
}
} else if let Some(sizes_path) = &args.sizes {
log::info!("Computing the sizes of the components");
let sizes = sccs.compute_sizes();
args.fmt.store(sizes_path, &sizes, None)?;
};
args.fmt.store(
&args.sccs,
sccs.components(),
Some(sccs.num_components() - 1),
)?;
Ok(())
}