Skip to main content

webgraph_cli/transform/
transpose.rs

1/*
2 * SPDX-FileCopyrightText: 2023 Inria
3 * SPDX-FileCopyrightText: 2023 Tommaso Fontana
4 * SPDX-FileCopyrightText: 2026 Sebastiano Vigna
5 *
6 * SPDX-License-Identifier: Apache-2.0 OR MIT
7 */
8
9use crate::*;
10use anyhow::Result;
11use dsi_bitstream::{dispatch::factory::CodesReaderFactoryHelper, prelude::*};
12use dsi_progress_logger::prelude::*;
13use std::path::PathBuf;
14use webgraph::prelude::*;
15
16#[derive(Parser, Debug)]
17#[command(name = "transpose", about = "Transposes a graph in the BV format.", long_about = None, next_line_help = true)]
18pub struct CliArgs {
19    /// The basename of the graph.​
20    pub src: PathBuf,
21    /// The basename of the transposed graph.​
22    pub dst: PathBuf,
23
24    #[arg(short, long)]
25    /// Uses the sequential algorithm (does not need offsets).​
26    pub sequential: bool,
27
28    #[clap(flatten)]
29    pub num_threads: NumThreadsArg,
30
31    #[clap(flatten)]
32    pub memory_usage: MemoryUsageArg,
33
34    #[arg(long, conflicts_with = "sequential")]
35    /// Uses the degree cumulative function to balance work by arcs rather than
36    /// by nodes; the DCF must have been pre-built with `webgraph build dcf`.​
37    pub dcf: bool,
38
39    #[clap(flatten)]
40    pub ca: CompressArgs,
41
42    #[clap(flatten)]
43    pub log_interval: LogIntervalArg,
44}
45
46pub fn main(args: CliArgs) -> Result<()> {
47    create_parent_dir(&args.dst)?;
48
49    dispatch_endianness!(&args.src, |E| {
50        if args.sequential {
51            transpose_seq::<E>(args)
52        } else {
53            transpose_par::<E>(args)
54        }
55    })
56}
57
58/// Sequential version of [`transpose_par`].
59pub fn transpose_seq<E: Endianness>(args: CliArgs) -> Result<()>
60where
61    MmapHelper<u32>: CodesReaderFactoryHelper<E>,
62{
63    let thread_pool = crate::get_thread_pool(args.num_threads.num_threads);
64
65    // TODO!: speed it up by using random access graph if possible
66    let seq_graph = webgraph::graphs::bvgraph::sequential::BvGraphSeq::with_basename(&args.src)
67        .endianness::<E>()
68        .load()?;
69
70    // transpose the graph
71    let mut pl = progress_logger![
72        display_memory = true,
73        log_interval = args.log_interval.log_interval
74    ];
75    let sorted =
76        webgraph::transform::transpose_seq(&seq_graph, args.memory_usage.memory_usage, &mut pl)?;
77
78    // Use uniform cutpoints for compression of the transposed graph
79    // (the source DCF does not match the transpose's degree distribution)
80    let target_endianness = args.ca.target_endianness::<E>();
81    let mut builder = args.ca.bv_comp_conf(&args.dst).progress_logger(&mut pl);
82    thread_pool.install(|| par_comp!(builder, sorted, target_endianness))?;
83
84    Ok(())
85}
86
87/// Parallel version of [`transpose_seq`].
88pub fn transpose_par<E: Endianness>(args: CliArgs) -> Result<()>
89where
90    MmapHelper<u32>: CodesReaderFactoryHelper<E>,
91    for<'a> LoadModeCodesReader<'a, E, Mmap>: BitSeek + Clone + Send + Sync,
92{
93    let thread_pool = crate::get_thread_pool(args.num_threads.num_threads);
94
95    let graph = webgraph::graphs::bvgraph::BvGraph::with_basename(&args.src)
96        .endianness::<E>()
97        .load()?;
98
99    let par_graph = crate::par_graph(
100        graph,
101        &args.src,
102        args.dcf,
103        thread_pool.current_num_threads(),
104    )?;
105
106    // transpose the graph
107    let mut pl = progress_logger![
108        display_memory = true,
109        log_interval = args.log_interval.log_interval
110    ];
111    let sorted =
112        webgraph::transform::transpose_par(&par_graph, args.memory_usage.memory_usage, &mut pl)?;
113
114    let target_endianness = args.ca.target_endianness::<E>();
115    let mut builder = args.ca.bv_comp_conf(&args.dst).progress_logger(&mut pl);
116    thread_pool.install(|| par_comp!(builder, sorted, target_endianness))?;
117    Ok(())
118}