Struct Graph

Source
pub struct Graph<'a, N> { /* private fields */ }
Expand description

This structure holds a graph in the Compressed Sparse Row format for compression of data size. This graph is represented via Memory Mapping, allowing the graph to be loaded into memory as required. This makes it possible to load any-size graphs, even those that do not fit into memory!

Implementations§

Source§

impl<'a, N> Graph<'a, N>
where N: ValidGraphType + 'a,

Source

pub fn from_txt_adjacency_list<T>( stream: T, folder_name: &str, ) -> Result<Graph<'a, N>, Error>
where T: Read + Sized,

Convenience method for reading an input stream in text format. Each line should contain two numbers, separated by a space. The graph will be converted to the underlying CSR representation, and stored in folder_name.

Examples found in repository?
examples/from_txt.rs (line 15)
1fn main() {
2    let (input_graph, output_directory) = {
3        let args = std::env::args().skip(1).collect::<Vec<_>>();
4
5        if args.len() != 2 {
6            println!("Usage: bfs <converted graph directory> <output directory>");
7            return;
8        }
9
10        (args[0].clone(), args[1].clone())
11    };
12
13    let source_file = std::fs::File::open(input_graph).unwrap();
14
15    graph_csr::Graph::<u32>::from_txt_adjacency_list(source_file, &output_directory).unwrap();
16}
Source

pub fn from_binary_adjancency<T>( stream: T, destination_folder_name: &str, ) -> Result<Graph<'a, N>, Error>
where T: Read + Sized,

Same as from_txt_adjacency, except this time it assumes the edge list to be in binary representation.

Source

pub fn from_adjacency_list<T>( stream: T, folder_name: &str, ) -> Result<Graph<'a, N>, Error>
where T: Iterator<Item = Result<(N, N)>> + Sized,

Given a SORTED (by source) adjancency list file source_file_name, transforms this file into the underlying binary representation in CSR and returns a version of the Graph in this format. The graph will be stored in folder_name.

Source

pub fn load_graph(graph_folder: &str) -> Result<Graph<'a, N>, Error>

Loads a graph from the underlying representation and returns it as a Graph struct.

Examples found in repository?
examples/wcc.rs (line 21)
1fn main() {
2    let (input_graph, output_file) = {
3        let args = std::env::args().skip(1).collect::<Vec<_>>();
4
5        if args.len() != 1 && args.len() != 2 {
6            println!("Usage: wcc <converted graph directory> [output_file]");
7            return;
8        }
9
10        let output = {
11            if args.len() == 2 {
12                Some(args[1].clone())
13            } else {
14                None
15            }
16        };
17
18        (args[0].clone(), output)
19    };
20
21    let graph = graph_csr::Graph::<u32>::load_graph(&input_graph).unwrap();
22
23    let mut compute_graph = graph_csr::compute::ComputeGraph::<u32, u32>::new(&graph);
24
25    // Initialize
26    compute_graph.fill_active(true);
27    for i in 0..graph.n_nodes() {
28        compute_graph.set_data(i, i as u32);
29    }
30    compute_graph.step(); // Set data
31
32    let mut i = 0;
33    while compute_graph.n_active() > 0 {
34        let time_start = std::time::Instant::now();
35        compute_graph.push(|src, dst| graph_csr::compute::helper::atomic_min(src, dst, |v| v));
36        let time_end = std::time::Instant::now();
37        compute_graph.step();
38
39        i += 1;
40        println!(
41            "Iteration {} took {}ms",
42            i,
43            (time_end - time_start).as_millis()
44        );
45    }
46
47    // Print results
48    print!("[ ");
49    for (idx, data) in compute_graph
50        .get_data_as_slice()
51        .iter()
52        .enumerate()
53        .take(30)
54    {
55        print!(
56            "{}:{}, ",
57            idx,
58            data.load(std::sync::atomic::Ordering::Relaxed)
59        );
60    }
61    println!("]");
62
63    // Save file
64    if let Some(output_file) = output_file {
65        println!("Saving file to {}", output_file);
66        compute_graph.save_data_to_file(&output_file).unwrap();
67    }
68}
More examples
Hide additional examples
examples/bfs.rs (line 21)
1fn main() {
2    let (input_graph, src_node, output_file) = {
3        let args = std::env::args().skip(1).collect::<Vec<_>>();
4
5        if args.len() != 2 && args.len() != 3 {
6            println!("Usage: bfs <converted graph directory> <src_node> [output_file]");
7            return;
8        }
9
10        let output = {
11            if args.len() == 3 {
12                Some(args[2].clone())
13            } else {
14                None
15            }
16        };
17
18        (args[0].clone(), args[1].parse::<usize>().unwrap(), output)
19    };
20
21    let graph = graph_csr::Graph::<u32>::load_graph(&input_graph).unwrap();
22    let mut compute_graph = graph_csr::compute::ComputeGraph::<u32, u32>::new(&graph);
23
24    // Initialize nodes
25    compute_graph.fill_active(false);
26    compute_graph.fill_data(u32::MAX);
27
28    // Initialize source
29    compute_graph.set_active(src_node, true);
30    compute_graph.set_data(src_node, 0);
31
32    compute_graph.step(); // Set data
33
34    let mut i = 0;
35    while compute_graph.n_active() > 0 {
36        let time_start = std::time::Instant::now();
37
38        compute_graph.push(|src, dst| graph_csr::compute::helper::atomic_min(src, dst, |v| v + 1));
39        compute_graph.step();
40
41        let time_end = std::time::Instant::now();
42
43        i += 1;
44        println!(
45            "Iteration {} took {}ms",
46            i,
47            (time_end - time_start).as_millis()
48        );
49    }
50
51    // Print results
52    print!("[ ");
53    for (idx, data) in compute_graph
54        .get_data_as_slice()
55        .iter()
56        .enumerate()
57        .take(30)
58    {
59        print!(
60            "{}:{}, ",
61            idx,
62            data.load(std::sync::atomic::Ordering::Relaxed)
63        );
64    }
65    println!("]");
66
67    // Save file
68    if let Some(output_file) = output_file {
69        println!("Saving file to {}", output_file);
70        compute_graph.save_data_to_file(&output_file).unwrap();
71    }
72}
Source

pub fn iter(&'a self) -> impl Iterator<Item = &[N]> + 'a

Returns an iterator over the edge list of each node.

Source

pub fn par_iter(&'a self) -> impl ParallelIterator<Item = (usize, &[N])> + 'a
where N: Send + Sync,

Source

pub fn n_nodes(&self) -> usize

Returns the number of nodes existing in the graph

Examples found in repository?
examples/wcc.rs (line 27)
1fn main() {
2    let (input_graph, output_file) = {
3        let args = std::env::args().skip(1).collect::<Vec<_>>();
4
5        if args.len() != 1 && args.len() != 2 {
6            println!("Usage: wcc <converted graph directory> [output_file]");
7            return;
8        }
9
10        let output = {
11            if args.len() == 2 {
12                Some(args[1].clone())
13            } else {
14                None
15            }
16        };
17
18        (args[0].clone(), output)
19    };
20
21    let graph = graph_csr::Graph::<u32>::load_graph(&input_graph).unwrap();
22
23    let mut compute_graph = graph_csr::compute::ComputeGraph::<u32, u32>::new(&graph);
24
25    // Initialize
26    compute_graph.fill_active(true);
27    for i in 0..graph.n_nodes() {
28        compute_graph.set_data(i, i as u32);
29    }
30    compute_graph.step(); // Set data
31
32    let mut i = 0;
33    while compute_graph.n_active() > 0 {
34        let time_start = std::time::Instant::now();
35        compute_graph.push(|src, dst| graph_csr::compute::helper::atomic_min(src, dst, |v| v));
36        let time_end = std::time::Instant::now();
37        compute_graph.step();
38
39        i += 1;
40        println!(
41            "Iteration {} took {}ms",
42            i,
43            (time_end - time_start).as_millis()
44        );
45    }
46
47    // Print results
48    print!("[ ");
49    for (idx, data) in compute_graph
50        .get_data_as_slice()
51        .iter()
52        .enumerate()
53        .take(30)
54    {
55        print!(
56            "{}:{}, ",
57            idx,
58            data.load(std::sync::atomic::Ordering::Relaxed)
59        );
60    }
61    println!("]");
62
63    // Save file
64    if let Some(output_file) = output_file {
65        println!("Saving file to {}", output_file);
66        compute_graph.save_data_to_file(&output_file).unwrap();
67    }
68}
Source

pub fn n_edges(&self) -> usize

Returns the number of edges existing in the graph

Auto Trait Implementations§

§

impl<'a, N> Freeze for Graph<'a, N>

§

impl<'a, N> RefUnwindSafe for Graph<'a, N>
where N: RefUnwindSafe,

§

impl<'a, N> !Send for Graph<'a, N>

§

impl<'a, N> !Sync for Graph<'a, N>

§

impl<'a, N> Unpin for Graph<'a, N>

§

impl<'a, N> !UnwindSafe for Graph<'a, N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.