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,
impl<'a, N> Graph<'a, N>where
N: ValidGraphType + 'a,
Sourcepub fn from_txt_adjacency_list<T>(
stream: T,
folder_name: &str,
) -> Result<Graph<'a, N>, Error>
pub fn from_txt_adjacency_list<T>( stream: T, folder_name: &str, ) -> Result<Graph<'a, N>, Error>
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?
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}
Sourcepub fn from_binary_adjancency<T>(
stream: T,
destination_folder_name: &str,
) -> Result<Graph<'a, N>, Error>
pub fn from_binary_adjancency<T>( stream: T, destination_folder_name: &str, ) -> Result<Graph<'a, N>, Error>
Same as from_txt_adjacency, except this time it assumes the edge list to be in binary representation.
Sourcepub fn from_adjacency_list<T>(
stream: T,
folder_name: &str,
) -> Result<Graph<'a, N>, Error>
pub fn from_adjacency_list<T>( stream: T, folder_name: &str, ) -> Result<Graph<'a, N>, Error>
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
.
Sourcepub fn load_graph(graph_folder: &str) -> Result<Graph<'a, N>, Error>
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?
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
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}
Sourcepub fn iter(&'a self) -> impl Iterator<Item = &[N]> + 'a
pub fn iter(&'a self) -> impl Iterator<Item = &[N]> + 'a
Returns an iterator over the edge list of each node.
pub fn par_iter(&'a self) -> impl ParallelIterator<Item = (usize, &[N])> + 'a
Sourcepub fn n_nodes(&self) -> usize
pub fn n_nodes(&self) -> usize
Returns the number of nodes existing in the graph
Examples found in repository?
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}