pub struct Graph { /* private fields */ }Implementations§
Source§impl Graph
impl Graph
Sourcepub fn new(offsets: Vec<u32>, sources: Vec<u32>) -> Result<Graph, &'static str>
pub fn new(offsets: Vec<u32>, sources: Vec<u32>) -> Result<Graph, &'static str>
The edges in each destination’s CSR range are its predecessors.
Examples found in repository?
examples/propagate.rs (line 24)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9 let mut args = std::env::args().skip(1);
10 let input = std::fs::read_to_string(args.next().ok_or("missing graph")?)?;
11 let hybrid = args.next().as_deref() == Some("hybrid");
12 let total = Instant::now();
13 let mut offsets = vec![0];
14 let mut sources = Vec::new();
15 let mut seeds = Vec::new();
16 for line in input.lines() {
17 let mut fields = line.split_whitespace();
18 seeds.push(fields.next().ok_or("missing seed")?.parse::<u32>()?);
19 for field in fields {
20 sources.push(field.parse::<u32>()?);
21 }
22 offsets.push(u32::try_from(sources.len())?);
23 }
24 let graph = Graph::new(offsets.clone(), sources.clone())?;
25 let verified = OnceLock::new();
26 let output = OnceLock::new();
27 let jobs = Plan::new(vec![
28 Task {
29 dependencies: vec![],
30 device: Device::Cpu,
31 memory_bytes: 16 << 20,
32 },
33 Task {
34 dependencies: vec![],
35 device: if hybrid { Device::Gpu } else { Device::Cpu },
36 memory_bytes: 16 << 20,
37 },
38 Task {
39 dependencies: vec![0, 1],
40 device: Device::Cpu,
41 memory_bytes: 0,
42 },
43 ])?;
44 let timings = jobs.execute(
45 Limits {
46 cpu: 2,
47 gpu: 1,
48 memory_bytes: 32 << 20,
49 },
50 |task, device| {
51 match task {
52 0 => {
53 verified
54 .set(graph.cpu(&seeds, true)?)
55 .map_err(|_| "duplicate reference")?;
56 }
57 1 => {
58 let mut session = ResidentGraph::new(
59 Graph::new(offsets.clone(), sources.clone())?,
60 match device {
61 Device::Cpu => Backend::Cpu,
62 Device::Gpu => Backend::Cuda,
63 },
64 );
65 #[cfg(all(windows, target_arch = "x86_64"))]
66 eprintln!("device_init={:?}", session.device_info());
67 let result = session.solve(&seeds)?;
68 output.set(result).map_err(|_| "duplicate result")?;
69 }
70 2 => {
71 if verified.get().unwrap() != &output.get().unwrap().facts {
72 return Err("device and CPU results differ".into());
73 }
74 }
75 _ => unreachable!(),
76 }
77 Ok(())
78 },
79 )?;
80 let result = output.get().unwrap();
81 println!(
82 "nodes={} backend={:?} fallback={:?} solve_ns={} setup_through_parity_ns={} timings={timings:?}",
83 seeds.len(),
84 result.backend,
85 result.fallback,
86 result.wall_ns,
87 total.elapsed().as_nanos()
88 );
89 if hybrid && !matches!(result.backend, Backend::Cuda) {
90 return Err("CUDA did not execute".into());
91 }
92 Ok(())
93}Sourcepub fn cpu(
&self,
seeds: &[u32],
parallel: bool,
) -> Result<Vec<u32>, &'static str>
pub fn cpu( &self, seeds: &[u32], parallel: bool, ) -> Result<Vec<u32>, &'static str>
Examples found in repository?
examples/propagate.rs (line 54)
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9 let mut args = std::env::args().skip(1);
10 let input = std::fs::read_to_string(args.next().ok_or("missing graph")?)?;
11 let hybrid = args.next().as_deref() == Some("hybrid");
12 let total = Instant::now();
13 let mut offsets = vec![0];
14 let mut sources = Vec::new();
15 let mut seeds = Vec::new();
16 for line in input.lines() {
17 let mut fields = line.split_whitespace();
18 seeds.push(fields.next().ok_or("missing seed")?.parse::<u32>()?);
19 for field in fields {
20 sources.push(field.parse::<u32>()?);
21 }
22 offsets.push(u32::try_from(sources.len())?);
23 }
24 let graph = Graph::new(offsets.clone(), sources.clone())?;
25 let verified = OnceLock::new();
26 let output = OnceLock::new();
27 let jobs = Plan::new(vec![
28 Task {
29 dependencies: vec![],
30 device: Device::Cpu,
31 memory_bytes: 16 << 20,
32 },
33 Task {
34 dependencies: vec![],
35 device: if hybrid { Device::Gpu } else { Device::Cpu },
36 memory_bytes: 16 << 20,
37 },
38 Task {
39 dependencies: vec![0, 1],
40 device: Device::Cpu,
41 memory_bytes: 0,
42 },
43 ])?;
44 let timings = jobs.execute(
45 Limits {
46 cpu: 2,
47 gpu: 1,
48 memory_bytes: 32 << 20,
49 },
50 |task, device| {
51 match task {
52 0 => {
53 verified
54 .set(graph.cpu(&seeds, true)?)
55 .map_err(|_| "duplicate reference")?;
56 }
57 1 => {
58 let mut session = ResidentGraph::new(
59 Graph::new(offsets.clone(), sources.clone())?,
60 match device {
61 Device::Cpu => Backend::Cpu,
62 Device::Gpu => Backend::Cuda,
63 },
64 );
65 #[cfg(all(windows, target_arch = "x86_64"))]
66 eprintln!("device_init={:?}", session.device_info());
67 let result = session.solve(&seeds)?;
68 output.set(result).map_err(|_| "duplicate result")?;
69 }
70 2 => {
71 if verified.get().unwrap() != &output.get().unwrap().facts {
72 return Err("device and CPU results differ".into());
73 }
74 }
75 _ => unreachable!(),
76 }
77 Ok(())
78 },
79 )?;
80 let result = output.get().unwrap();
81 println!(
82 "nodes={} backend={:?} fallback={:?} solve_ns={} setup_through_parity_ns={} timings={timings:?}",
83 seeds.len(),
84 result.backend,
85 result.fallback,
86 result.wall_ns,
87 total.elapsed().as_nanos()
88 );
89 if hybrid && !matches!(result.backend, Backend::Cuda) {
90 return Err("CUDA did not execute".into());
91 }
92 Ok(())
93}Auto Trait Implementations§
impl Forget for Graph
impl Freeze for Graph
impl Move for Graph
impl RefUnwindSafe for Graph
impl Send for Graph
impl Sync for Graph
impl Unpin for Graph
impl UnsafeUnpin for Graph
impl UnwindSafe for Graph
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more