bitflicker64 commented on code in PR #359: URL: https://github.com/apache/hugegraph-computer/pull/359#discussion_r3910387315
########## computer-rust/src/fixtures/tolerance.rs: ########## @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +pub struct DifferentialTolerance; Review Comment: ⚠️ `DifferentialTolerance` is a fieldless struct whose only job is to be a namespace for two functions, and the module is already that namespace. `grep -rn 'DifferentialTolerance' computer-rust/` returns the declaration, the `impl`, and six calls, all six inside this file's own `#[cfg(test)]` block. Nothing in `kernel/`, `ffi/`, or `benches/` touches it. Not asking you to drop the file, since #355 lists correctness fixtures as a work item. Just drop the wrapper: ```rust pub fn l1_distance(actual: &[f64], expected: &[f64]) -> Result<f64, String> { .. } pub fn assert_parity(actual: &[f64], expected: &[f64], epsilon: f64) -> Result<(), String> { .. } ``` Callers become `tolerance::assert_parity(..)`, which reads better than `DifferentialTolerance::assert_parity(..)` anyway. While you are in here: `assert_parity` (lines 48-60) re-implements the length-mismatch check and the per-element finite check from `l1_distance` (lines 21-33), copied `format!` strings and all. Have one call into the other's guard instead of carrying a second copy. ########## computer-rust/src/fixtures/dataset.rs: ########## @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use crate::kernel::csr::CsrGraph; + +pub struct GraphFixture { + pub name: String, + pub num_vertices: u32, + pub edges: Vec<(u32, u32, f64)>, +} + +impl GraphFixture { + /// Returns the Zachary's Karate Club representative graph dataset fixture. + pub fn karate_club() -> Self { Review Comment: ⚠️ This is not Zachary's karate club. The real graph has 34 vertices and 78 edges; this list has 35, and every one of them has source 0, 1, 2, or 3, stopping part-way through vertex 3's row. Counting at head, the vertices appearing at either endpoint are `{0..13, 17, 19, 21, 27, 28, 30, 31, 32}`, so twelve vertices have no incident edge, including vertex 33, which is one of the two hubs the dataset is known for. That matters given what the fixture is for. #355 requires differential parity against ground truth, and `docs/rust-modernization-roadmap.md` in this PR promises L1 distance <= 1e-6 against ground-truth outputs. A parity test comparing this kernel's PageRank against a Java or NetworkX run on the real karate club will disagree on every vertex, and whoever hits it first will go hunting for a bug in the kernel. Either paste the full 78-edge list, or rename to something that does not claim to be the canonical dataset (`karate_club_subset`, or just fold it into `synthetic_*`). ########## computer-rust/src/ffi/c_api.rs: ########## @@ -0,0 +1,217 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#![allow(clippy::not_unsafe_ptr_arg_deref)] + +use crate::kernel::csr::CsrGraph; +use crate::kernel::pagerank::PageRankKernel; +use crate::RUST_KERNEL_VERSION; +use std::ffi::CString; +use std::os::raw::c_char; +use std::slice; +use std::sync::OnceLock; + +pub struct GraphBuilder { + num_vertices: u32, + edges: Vec<(u32, u32, f64)>, + csr: Option<CsrGraph>, +} + +#[no_mangle] +pub extern "C" fn computer_graph_create(num_vertices: u32) -> *mut GraphBuilder { + let builder = Box::new(GraphBuilder { + num_vertices, + edges: Vec::new(), + csr: None, + }); + Box::into_raw(builder) +} + +#[no_mangle] +pub extern "C" fn computer_graph_add_edge( + handle: *mut GraphBuilder, + src: u32, + dst: u32, + weight: f64, +) -> i32 { + if handle.is_null() { + return -1; + } + let builder = unsafe { &mut *handle }; + if builder.csr.is_some() { + return -2; + } + if src >= builder.num_vertices || dst >= builder.num_vertices { + return -1; + } + if weight < 0.0 || !weight.is_finite() { + return -1; + } + builder.edges.push((src, dst, weight)); + 0 +} + +#[no_mangle] +pub extern "C" fn computer_graph_finalize(handle: *mut GraphBuilder) -> i32 { + if handle.is_null() { + return -1; + } + let builder = unsafe { &mut *handle }; + let csr = CsrGraph::from_edges(builder.num_vertices, &builder.edges); + builder.csr = Some(csr); + 0 +} + +#[no_mangle] +pub extern "C" fn computer_graph_compute_pagerank( + handle: *const GraphBuilder, + damping_factor: f64, + max_iterations: u32, + tolerance: f64, + out_scores: *mut f64, + out_capacity: u32, +) -> i32 { + if handle.is_null() || out_scores.is_null() { + return -1; + } + let builder = unsafe { &*handle }; + let csr = match &builder.csr { + Some(c) => c, + None => return -2, + }; + + if out_capacity < csr.num_vertices() { + return -3; + } + + if !damping_factor.is_finite() || damping_factor < 0.0 || damping_factor > 1.0 { + return -4; + } + if !tolerance.is_finite() || tolerance < 0.0 { + return -4; + } + + let kernel = PageRankKernel::new(damping_factor, max_iterations, tolerance); Review Comment: ⚠️ These six lines are a character-for-character copy of the two checks already inside `PageRankKernel::try_new` (pagerank.rs:33-44). They only exist because line 108 calls `PageRankKernel::new`, which is `try_new(..).expect(..)`, so without the pre-check a bad parameter panics instead of returning `-4`. Worth collapsing now rather than in Phase 3, for a reason beyond line count: `Cargo.toml` sets `panic = "abort"` on the release profile. A `pub extern "C"` function that can reach a `panic!` in a `panic = "abort"` cdylib does not unwind and does not return an error code, it takes the host process with it. The roadmap puts a JVM and a Go runtime on the other side of this boundary, so the panicking constructor is the thing to delete before that lands. Drop lines 101-106 and line 108 in favour of: ```rust let kernel = match PageRankKernel::try_new(damping_factor, max_iterations, tolerance) { Ok(k) => k, Err(_) => return -4, }; ``` Then delete `PageRankKernel::new` (pagerank.rs:52-55) entirely. Its only other callers are `benches/kernel_bench.rs:25` and two unit tests, all of which become `try_new(..).unwrap()`. The duplicate is also the usual drift bug: add a third constraint to `try_new` and the C-ABI quietly stops enforcing it. ########## computer-rust/src/kernel/csr.rs: ########## @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#[derive(Debug, Clone, Default)] +pub struct Edge { Review Comment: 🧹 Three dead public symbols, each verified by grep at head as returning only its own definition. `dead_code` will not warn on any of them because they are `pub` in a library crate, so CI stays green while they rot. - `Edge` (this line): exactly one occurrence in the crate. `out_edges` returns `(&[u32], &[f64])` and never constructs it. - `CsrGraph::new` (lines 33-41): zero call sites. `from_edges` is the only constructor anything uses and builds `row_offsets` itself. - `GraphFixture.name` (`fixtures/dataset.rs:21`): written by both constructors, including a `format!` allocation in `synthetic_powerlaw`, never read. Deleting all three is a smaller crate with no behaviour change. Add them back in the PR that has a caller. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
