From aa2b1ca91368c163910937443edf9d58aed39f17 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 15 Apr 2025 17:50:13 +0200 Subject: [PATCH 1/4] Mark v0 functions deprecated --- pineappl_capi/src/lib.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index c28ee961..0bfe06ec 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -368,7 +368,7 @@ pub unsafe extern "C" fn pineappl_grid_clone(grid: *const Grid) -> Box { /// See [`pineappl_grid_convolve_with_one`]. #[deprecated( since = "0.8.0", - note = "please use `pineappl_grid_convolve_with_one` instead" + note = "use `pineappl_grid_convolve_with_one` instead" )] #[no_mangle] pub unsafe extern "C" fn pineappl_grid_convolute_with_one( @@ -406,7 +406,7 @@ pub unsafe extern "C" fn pineappl_grid_convolute_with_one( /// See [`pineappl_grid_convolve_with_two`]. #[deprecated( since = "0.8.0", - note = "please use `pineappl_grid_convolve_with_two` instead" + note = "use `pineappl_grid_convolve_with_two` instead" )] #[no_mangle] pub unsafe extern "C" fn pineappl_grid_convolute_with_two( @@ -601,6 +601,7 @@ pub extern "C" fn pineappl_grid_delete(grid: Option>) {} /// /// If `grid` does not point to a valid `Grid` object, for example when `grid` is the null pointer, /// this function is not safe to call. +#[deprecated(since = "1.0.0", note = "use `pineappl_grid_fill2` instead")] #[no_mangle] pub unsafe extern "C" fn pineappl_grid_fill( grid: *mut Grid, @@ -625,6 +626,7 @@ pub unsafe extern "C" fn pineappl_grid_fill( /// /// If `grid` does not point to a valid `Grid` object, for example when `grid` is the null pointer, /// this function is not safe to call. +#[deprecated(since = "1.0.0", note = "use `pineappl_grid_fill_all2` instead")] #[no_mangle] pub unsafe extern "C" fn pineappl_grid_fill_all( grid: *mut Grid, @@ -650,6 +652,7 @@ pub unsafe extern "C" fn pineappl_grid_fill_all( /// If `grid` does not point to a valid `Grid` object, for example when `grid` is the null pointer, /// this function is not safe to call. Additionally, all remaining pointer parameters must be /// arrays as long as specified by `size`. +#[deprecated(since = "1.0.0", note = "use `pineappl_grid_fill_array2` instead")] #[no_mangle] pub unsafe extern "C" fn pineappl_grid_fill_array( grid: *mut Grid, @@ -684,6 +687,7 @@ pub unsafe extern "C" fn pineappl_grid_fill_array( /// /// If `grid` does not point to a valid `Grid` object, for example when `grid` is the null pointer, /// this function is not safe to call. +#[deprecated(since = "1.0.0", note = "use `pineappl_grid_channels` instead")] #[no_mangle] pub unsafe extern "C" fn pineappl_grid_lumi(grid: *const Grid) -> Box { let grid = unsafe { &*grid }; @@ -698,6 +702,7 @@ pub unsafe extern "C" fn pineappl_grid_lumi(grid: *const Grid) -> Box { /// If `grid` does not point to a valid `Grid` object, for example when `grid` is the null pointer, /// this function is not safe to call. The pointer `order_params` must point to an array as large /// as four times the number of orders in `grid`. +#[deprecated(since = "1.0.0", note = "use `pineappl_grid_order_params2` instead")] #[no_mangle] pub unsafe extern "C" fn pineappl_grid_order_params(grid: *const Grid, order_params: *mut u32) { let grid = unsafe { &*grid }; @@ -750,6 +755,7 @@ pub unsafe extern "C" fn pineappl_grid_order_count(grid: *const Grid) -> usize { /// # Panics /// /// TODO +#[deprecated(since = "1.0.0", note = "use `pineappl_grid_new2` instead")] #[no_mangle] #[must_use] pub unsafe extern "C" fn pineappl_grid_new( @@ -890,6 +896,7 @@ pub unsafe extern "C" fn pineappl_grid_scale(grid: *mut Grid, factor: f64) { /// /// If `grid` does not point to a valid `Grid` object, for example when `grid` is the null pointer, /// this function is not safe to call. +#[deprecated(since = "1.0.0", note = "use `pineappl_grid_split_channels` instead")] #[no_mangle] pub unsafe extern "C" fn pineappl_grid_split_lumi(grid: *mut Grid) { let grid = unsafe { &mut *grid }; @@ -1005,6 +1012,7 @@ pub unsafe extern "C" fn pineappl_grid_scale_by_order( /// # Panics /// /// TODO +#[deprecated(since = "1.0.0", note = "use `pineappl_grid_metadata` instead")] #[no_mangle] pub unsafe extern "C" fn pineappl_grid_key_value( grid: *const Grid, @@ -1043,6 +1051,7 @@ pub unsafe extern "C" fn pineappl_grid_key_value( /// # Panics /// /// TODO +#[deprecated(since = "1.0.0", note = "use `pineappl_grid_set_metadata` instead")] #[no_mangle] pub unsafe extern "C" fn pineappl_grid_set_key_value( grid: *mut Grid, @@ -1153,6 +1162,7 @@ pub unsafe extern "C" fn pineappl_grid_write(grid: *const Grid, filename: *const /// The parameter `lumi` must point to a valid `Lumi` object created by `pineappl_lumi_new`. /// `pdg_id_pairs` must be an array with length `2 * combinations`, and `factors` with length of /// `combinations`. +#[deprecated(since = "1.0.0", note = "use `pineappl_channels_add` instead")] #[no_mangle] pub unsafe extern "C" fn pineappl_lumi_add( lumi: *mut Lumi, @@ -1183,6 +1193,7 @@ pub unsafe extern "C" fn pineappl_lumi_add( /// /// The parameter `lumi` must point to a valid `Lumi` object created by `pineappl_lumi_new` or /// `pineappl_grid_lumi`. +#[deprecated(since = "1.0.0", note = "use `pineappl_channels_combinations` instead")] #[no_mangle] pub unsafe extern "C" fn pineappl_lumi_combinations(lumi: *const Lumi, entry: usize) -> usize { let lumi = unsafe { &*lumi }; @@ -1196,6 +1207,7 @@ pub unsafe extern "C" fn pineappl_lumi_combinations(lumi: *const Lumi, entry: us /// /// The parameter `lumi` must point to a valid `Lumi` object created by `pineappl_lumi_new` or /// `pineappl_grid_lumi`. +#[deprecated(since = "1.0.0", note = "use `pineappl_channels_count` instead")] #[no_mangle] pub unsafe extern "C" fn pineappl_lumi_count(lumi: *const Lumi) -> usize { let lumi = unsafe { &*lumi }; @@ -1204,6 +1216,7 @@ pub unsafe extern "C" fn pineappl_lumi_count(lumi: *const Lumi) -> usize { } /// Delete luminosity function previously created with `pineappl_lumi_new`. +#[deprecated(since = "1.0.0", note = "use `pineappl_channels_delete` instead")] #[no_mangle] #[allow(unused_variables)] pub extern "C" fn pineappl_lumi_delete(lumi: Option>) {} @@ -1217,6 +1230,7 @@ pub extern "C" fn pineappl_lumi_delete(lumi: Option>) {} /// `pineappl_grid_lumi`. The parameter `factors` must point to an array as long as the size /// returned by `pineappl_lumi_combinations` and `pdg_ids` must point to an array that is twice as /// long. +#[deprecated(since = "1.0.0", note = "use `pineappl_channels_entry` instead")] #[no_mangle] pub unsafe extern "C" fn pineappl_lumi_entry( lumi: *const Lumi, @@ -1243,6 +1257,7 @@ pub unsafe extern "C" fn pineappl_lumi_entry( /// Creates a new luminosity function and returns a pointer to it. If no longer needed, the object /// should be deleted using `pineappl_lumi_delete`. +#[deprecated(since = "1.0.0", note = "use `pineappl_channels_new` instead")] #[no_mangle] #[must_use] pub extern "C" fn pineappl_lumi_new() -> Box { @@ -1260,6 +1275,7 @@ pub struct KeyVal { } /// Delete the previously created object pointed to by `key_vals`. +#[deprecated(since = "1.0.0", note = "")] #[no_mangle] #[allow(unused_variables)] pub extern "C" fn pineappl_keyval_delete(key_vals: Option>) {} @@ -1270,6 +1286,7 @@ pub extern "C" fn pineappl_keyval_delete(key_vals: Option>) {} /// /// The parameter `key_vals` must point to a valid `KeyVal` object created by /// `pineappl_keyval_new`. `key` must be a valid C string. +#[deprecated(since = "1.0.0", note = "")] #[no_mangle] #[must_use] pub unsafe extern "C" fn pineappl_keyval_bool(key_vals: *const KeyVal, key: *const c_char) -> bool { @@ -1285,6 +1302,7 @@ pub unsafe extern "C" fn pineappl_keyval_bool(key_vals: *const KeyVal, key: *con /// /// The parameter `key_vals` must point to a valid `KeyVal` object created by /// `pineappl_keyval_new`. `key` must be a valid C string. +#[deprecated(since = "1.0.0", note = "")] #[no_mangle] #[must_use] pub unsafe extern "C" fn pineappl_keyval_double( @@ -1303,6 +1321,7 @@ pub unsafe extern "C" fn pineappl_keyval_double( /// /// The parameter `key_vals` must point to a valid `KeyVal` object created by /// `pineappl_keyval_new`. `key` must be a valid C string. +#[deprecated(since = "1.0.0", note = "")] #[no_mangle] #[must_use] pub unsafe extern "C" fn pineappl_keyval_int(key_vals: *const KeyVal, key: *const c_char) -> i32 { @@ -1318,6 +1337,7 @@ pub unsafe extern "C" fn pineappl_keyval_int(key_vals: *const KeyVal, key: *cons /// /// The parameter `key_vals` must point to a valid `KeyVal` object created by /// `pineappl_keyval_new`. `key` must be a valid C string. +#[deprecated(since = "1.0.0", note = "")] #[no_mangle] #[must_use] pub unsafe extern "C" fn pineappl_keyval_string( @@ -1331,6 +1351,7 @@ pub unsafe extern "C" fn pineappl_keyval_string( } /// Return a pointer to newly-created `pineappl_keyval` object. +#[deprecated(since = "1.0.0", note = "")] #[no_mangle] #[must_use] pub extern "C" fn pineappl_keyval_new() -> Box { @@ -1343,6 +1364,7 @@ pub extern "C" fn pineappl_keyval_new() -> Box { /// /// The parameter `key_vals` must point to a valid `KeyVal` object created by /// `pineappl_keyval_new`. `key` must be a valid C string. +#[deprecated(since = "1.0.0", note = "")] #[no_mangle] pub unsafe extern "C" fn pineappl_keyval_set_bool( key_vals: *mut KeyVal, @@ -1363,6 +1385,7 @@ pub unsafe extern "C" fn pineappl_keyval_set_bool( /// /// The parameter `key_vals` must point to a valid `KeyVal` object created by /// `pineappl_keyval_new`. `key` must be a valid C string. +#[deprecated(since = "1.0.0", note = "")] #[no_mangle] pub unsafe extern "C" fn pineappl_keyval_set_double( key_vals: *mut KeyVal, @@ -1383,6 +1406,7 @@ pub unsafe extern "C" fn pineappl_keyval_set_double( /// /// The parameter `key_vals` must point to a valid `KeyVal` object created by /// `pineappl_keyval_new`. `key` must be a valid C string. +#[deprecated(since = "1.0.0", note = "")] #[no_mangle] pub unsafe extern "C" fn pineappl_keyval_set_int( key_vals: *mut KeyVal, @@ -1403,6 +1427,7 @@ pub unsafe extern "C" fn pineappl_keyval_set_int( /// /// The parameter `key_vals` must point to a valid `KeyVal` object created by /// `pineappl_keyval_new`. `key` must be a valid C string. +#[deprecated(since = "1.0.0", note = "")] #[no_mangle] pub unsafe extern "C" fn pineappl_keyval_set_string( key_vals: *mut KeyVal, From f4bdde0bbba070f2144837cc8fe6c7b23cf0e569 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 15 Apr 2025 18:08:40 +0200 Subject: [PATCH 2/4] Change `pineappl_grid_new2` function - renamed `InterpTuples` to `InterpInfo` and named members like `Interp` - made members of `InterpInfo` public - reordered parameters of `pineappl_grid_new2` to have the same ordering as `Grid::new` - added new parameter `interpolations` that counts the number of interpolations, which in general is different from the number of convolutions plus one (previous assumption) - removed `nb_convolutions`, which can be inferred from the channels --- examples/cpp/advanced-filling.cpp | 6 +- examples/cpp/fill-grid.cpp | 6 +- pineappl_capi/cbindgen.toml | 2 +- pineappl_capi/src/lib.rs | 111 ++++++++++++++++-------------- 4 files changed, 68 insertions(+), 57 deletions(-) diff --git a/examples/cpp/advanced-filling.cpp b/examples/cpp/advanced-filling.cpp index 8c7a4a72..38382deb 100644 --- a/examples/cpp/advanced-filling.cpp +++ b/examples/cpp/advanced-filling.cpp @@ -56,7 +56,7 @@ int main() { pineappl_map scales_mapping = PINEAPPL_MAP_APPL_GRID_H0; // Mapping method pineappl_map moment_mapping = PINEAPPL_MAP_APPL_GRID_F2; pineappl_interp_meth interpolation_meth = PINEAPPL_INTERP_METH_LAGRANGE; - pineappl_interp_tuples interpolations[3] = { + pineappl_interp interpolations[3] = { { 1e2, 1e8, 40, 3, scales_reweight, scales_mapping, interpolation_meth }, // Interpolation fo `scales` { 2e-7, 1.0, 50, 3, moment_reweight, moment_mapping, interpolation_meth }, // Interpolation fo `x1` { 2e-7, 1.0, 50, 3, moment_reweight, moment_mapping, interpolation_meth }, // Interpolation fo `x2` @@ -71,8 +71,8 @@ int main() { // create a new grid with the previously defined channels, 3 perturbative orders defined by the // exponents in `orders`, 24 bins given as the 25 limits in `bins` and potential extra // parameters in `keyval`. - auto* grid = pineappl_grid_new2(pid_basis, channels, orders.size() / 5, orders.data(), bins.size() - 1, - bins.data(), nb_convolutions, convolution_types, pdg_ids, kinematics, interpolations, mu_scales); + auto* grid = pineappl_grid_new2(bins.size() - 1, bins.data(), orders.size() / 5, orders.data(), + channels, pid_basis, convolution_types, pdg_ids, 3, interpolations, kinematics, mu_scales); // now we no longer need `channels` pineappl_channels_delete(channels); diff --git a/examples/cpp/fill-grid.cpp b/examples/cpp/fill-grid.cpp index b45c4939..2722594a 100644 --- a/examples/cpp/fill-grid.cpp +++ b/examples/cpp/fill-grid.cpp @@ -201,7 +201,7 @@ int main() { pineappl_map scales_mapping = PINEAPPL_MAP_APPL_GRID_H0; // Mapping method pineappl_map moment_mapping = PINEAPPL_MAP_APPL_GRID_F2; pineappl_interp_meth interpolation_meth = PINEAPPL_INTERP_METH_LAGRANGE; - pineappl_interp_tuples interpolations[3] = { + pineappl_interp interpolations[3] = { { 1e2, 1e8, 40, 3, scales_reweight, scales_mapping, interpolation_meth }, // Interpolation fo `scales` { 2e-7, 1.0, 50, 3, moment_reweight, moment_mapping, interpolation_meth }, // Interpolation fo `x1` { 2e-7, 1.0, 50, 3, moment_reweight, moment_mapping, interpolation_meth }, // Interpolation fo `x2` @@ -216,8 +216,8 @@ int main() { // create a new grid with the previously defined channels, 3 perturbative orders defined by the // exponents in `orders`, 24 bins given as the 25 limits in `bins` and potential extra // parameters in `keyval`. - auto* grid = pineappl_grid_new2(pid_basis, channels, orders.size() / 5, orders.data(), bins.size() - 1, - bins.data(), nb_convolutions, convolution_types, pdg_ids, kinematics, interpolations, mu_scales); + auto* grid = pineappl_grid_new2(bins.size() - 1, bins.data(), orders.size() / 5, orders.data(), + channels, pid_basis, convolution_types, pdg_ids, 3, interpolations, kinematics, mu_scales); // now we no longer need `keyval` and `channels` pineappl_channels_delete(channels); diff --git a/pineappl_capi/cbindgen.toml b/pineappl_capi/cbindgen.toml index 8a07de99..8a79d97d 100644 --- a/pineappl_capi/cbindgen.toml +++ b/pineappl_capi/cbindgen.toml @@ -43,7 +43,7 @@ rename_variants = "ScreamingSnakeCase" "ReweightMeth" = "pineappl_reweight_meth" "Map" = "pineappl_map" "InterpMeth" = "pineappl_interp_meth" -"InterpTuples" = "pineappl_interp_tuples" +"Interp" = "pineappl_interp" "KeyVal" = "pineappl_keyval" "SubGrid" = "pineappl_subgrid" "GridOptFlags" = "pineappl_gof" diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 0bfe06ec..863b6c59 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -59,7 +59,7 @@ use itertools::izip; use pineappl::boc::{Bin, BinsWithFillLimits, Channel, Kinematics, Order, ScaleFuncForm, Scales}; use pineappl::convolutions::{Conv, ConvType, ConvolutionCache}; use pineappl::grid::{Grid, GridOptFlags}; -use pineappl::interpolation::{Interp, InterpMeth, Map, ReweightMeth}; +use pineappl::interpolation::{Interp as InterpMain, InterpMeth, Map, ReweightMeth}; use pineappl::packed_array::ravel_multi_index; use pineappl::pids::PidBasis; use pineappl::subgrid::Subgrid; @@ -91,7 +91,7 @@ pub const PINEAPPL_GOF_STRIP_EMPTY_CHANNELS: GridOptFlags = GridOptFlags::STRIP_ // TODO: make sure no `panic` calls leave functions marked as `extern "C"` -fn grid_interpolation_params(key_vals: Option<&KeyVal>) -> Vec { +fn grid_interpolation_params(key_vals: Option<&KeyVal>) -> Vec { let mut q2_min = 1e2; let mut q2_max = 1e8; let mut q2_nodes = 40; @@ -213,7 +213,7 @@ fn grid_interpolation_params(key_vals: Option<&KeyVal>) -> Vec { } vec![ - Interp::new( + InterpMain::new( q2_min, q2_max, q2_nodes, @@ -222,7 +222,7 @@ fn grid_interpolation_params(key_vals: Option<&KeyVal>) -> Vec { Map::ApplGridH0, InterpMeth::Lagrange, ), - Interp::new( + InterpMain::new( x1_min, x1_max, x1_nodes, @@ -231,7 +231,7 @@ fn grid_interpolation_params(key_vals: Option<&KeyVal>) -> Vec { Map::ApplGridF2, InterpMeth::Lagrange, ), - Interp::new( + InterpMain::new( x2_min, x2_max, x2_nodes, @@ -1465,27 +1465,21 @@ pub struct Channels(Vec); /// Type for defining the interpolation object #[repr(C)] -pub struct InterpTuples { - node_min: f64, - node_max: f64, - nb_nodes: usize, - interp_degree: usize, - reweighting_method: ReweightMeth, - mapping: Map, - interpolation_method: InterpMeth, -} - -#[must_use] -fn construct_interpolation(interp: &InterpTuples) -> Interp { - Interp::new( - interp.node_min, - interp.node_max, - interp.nb_nodes, - interp.interp_degree, - interp.reweighting_method, - interp.mapping, - interp.interpolation_method, - ) +pub struct Interp { + /// TODO + pub min: f64, + /// TODO + pub max: f64, + /// TODO + pub nodes: usize, + /// TODO + pub order: usize, + /// TODO + pub reweight: ReweightMeth, + /// TODO + pub map: Map, + /// TODO + pub interp_meth: InterpMeth, } /// An exact duplicate of `pineappl_lumi_new` to make naming (lumi -> channel) consistent. @@ -1605,30 +1599,32 @@ pub extern "C" fn pineappl_channels_delete(channels: Option>) {} /// `0` -> `ScaleFuncForm::NoScale`, ..., `n` -> `ScaleFuncForm::Scale(n - 1)`. /// /// # Safety +/// /// TODO /// /// # Panics +/// /// TODO #[no_mangle] #[must_use] pub unsafe extern "C" fn pineappl_grid_new2( - pid_basis: PidBasis, - channels: *const Channels, - orders: usize, - order_params: *const u8, bins: usize, bin_limits: *const f64, - nb_convolutions: usize, + orders: usize, + order_params: *const u8, + channels: *const Channels, + pid_basis: PidBasis, convolution_types: *const ConvType, - pdg_ids: *const c_int, + convolution_pdg_ids: *const c_int, + interpolations: usize, + interp_info: *const Interp, kinematics: *const Kinematics, - interpolations: *const InterpTuples, mu_scales: *const usize, ) -> Box { - // Luminosity channels - let channels = unsafe { &*channels }; - - // Perturbative orders + let bins = BinsWithFillLimits::from_fill_limits( + unsafe { slice::from_raw_parts(bin_limits, bins + 1) }.to_vec(), + ) + .unwrap(); let order_params = unsafe { slice::from_raw_parts(order_params, 5 * orders) }; let orders: Vec<_> = order_params .chunks(5) @@ -1640,32 +1636,47 @@ pub unsafe extern "C" fn pineappl_grid_new2( logxia: s[4], }) .collect(); - - let bins = BinsWithFillLimits::from_fill_limits( - unsafe { slice::from_raw_parts(bin_limits, bins + 1) }.to_vec(), - ) - .unwrap(); + let channels = unsafe { &*channels }; // Construct the convolution objects + let convolutions = channels.0[0].entry()[0].0.len(); let convolution_types = - unsafe { slice::from_raw_parts(convolution_types, nb_convolutions).to_vec() }; - let pdg_ids = unsafe { slice::from_raw_parts(pdg_ids, nb_convolutions).to_vec() }; - let convolutions = izip!(convolution_types.iter(), pdg_ids.iter()) - .map(|(&conv, &pdg_value)| Conv::new(conv, pdg_value)) + unsafe { slice::from_raw_parts(convolution_types, convolutions).to_vec() }; + let convolution_pdg_ids = + unsafe { slice::from_raw_parts(convolution_pdg_ids, convolutions).to_vec() }; + let convolutions = convolution_types + .iter() + .zip(convolution_pdg_ids) + .map(|(&convolution_type, pdg_id)| Conv::new(convolution_type, pdg_id)) .collect(); // Grid interpolations - let interp_slices = unsafe { std::slice::from_raw_parts(interpolations, nb_convolutions + 1) }; - let interp_vecs: Vec = interp_slices.iter().map(construct_interpolation).collect(); + let interp_slices = unsafe { std::slice::from_raw_parts(interp_info, interpolations) }; + let interp_vecs: Vec<_> = interp_slices + .iter() + .map(|interp| { + InterpMain::new( + interp.min, + interp.max, + interp.nodes, + interp.order, + interp.reweight, + interp.map, + interp.interp_meth, + ) + }) + .collect(); // Construct the kinematic variables - let kinematics = unsafe { slice::from_raw_parts(kinematics, interp_vecs.len()).to_vec() }; + let kinematics = unsafe { slice::from_raw_parts(kinematics, interp_vecs.len()) }.to_vec(); // Scales. An array containing the values of {ren, fac, frg} let mu_scales = unsafe { std::slice::from_raw_parts(mu_scales, 3) }; - let mu_scales_vec: Vec = mu_scales + let mu_scales_vec: Vec<_> = mu_scales .iter() .map(|&scale| { + // TODO: this doesn't allow all other `ScaleFuncForm`, for instance + // `ScaleFuncForm::QuadraticSum` if scale == 0 { ScaleFuncForm::NoScale } else { From 34ae3b80a5ff91dc0d14d65d04ddaa4e09e76594 Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 15 Apr 2025 19:14:49 +0200 Subject: [PATCH 3/4] Specify the number of convolutions once in `pineappl_channels_new` --- examples/cpp/advanced-filling.cpp | 4 ++-- examples/cpp/fill-grid.cpp | 11 ++++------- pineappl_capi/src/lib.rs | 20 +++++++++++--------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/examples/cpp/advanced-filling.cpp b/examples/cpp/advanced-filling.cpp index 38382deb..ccc603cb 100644 --- a/examples/cpp/advanced-filling.cpp +++ b/examples/cpp/advanced-filling.cpp @@ -11,10 +11,10 @@ int main() { // Construct the channel object based on the nb of convolutions std::size_t nb_convolutions = 2; std::size_t nb_channels = 2; - auto* channels = pineappl_channels_new(); + auto* channels = pineappl_channels_new(nb_convolutions); int32_t pids[] = { 2, -2, 4, -4 }; double factors[] = { 1.0, 1.0 }; - pineappl_channels_add(channels, nb_channels, nb_convolutions, pids, factors); + pineappl_channels_add(channels, nb_channels, pids, factors); std::size_t channel_count = 1; diff --git a/examples/cpp/fill-grid.cpp b/examples/cpp/fill-grid.cpp index 2722594a..81a6413a 100644 --- a/examples/cpp/fill-grid.cpp +++ b/examples/cpp/fill-grid.cpp @@ -115,11 +115,8 @@ int main() { // --- // Create all channels - // this object will contain all channels (initial states) that we define - auto* channels = pineappl_channels_new(); - - // Specify the dimension of the channel, ie the number of convolutions required - std::size_t nb_convolutions = 2; + // this object will contain all channels (for two initial states) that we define + auto* channels = pineappl_channels_new(2); // photon-photon initial state, where `22` is the photon (PDG MC ids) int32_t pids1[] = { 22, 22 }; @@ -128,7 +125,7 @@ int main() { double factors1[] = { 1.0 }; // define the channel #0 - pineappl_channels_add(channels, 1, nb_convolutions, pids1, factors1); + pineappl_channels_add(channels, 1, pids1, factors1); // create another channel, which we won't fill, however @@ -143,7 +140,7 @@ int main() { // can also pass `nullptr` // define the channel #1 - pineappl_channels_add(channels, 3, nb_convolutions, pids2, nullptr); + pineappl_channels_add(channels, 3, pids2, nullptr); // --- // Specify the perturbative orders that will be filled into the grid diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 863b6c59..0a704cee 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -1461,7 +1461,7 @@ pub unsafe extern "C" fn pineappl_string_delete(string: *mut c_char) { /// Type for defining a Channel function. #[derive(Default)] -pub struct Channels(Vec); +pub struct Channels(Vec, usize); /// Type for defining the interpolation object #[repr(C)] @@ -1486,8 +1486,8 @@ pub struct Interp { /// should be deleted using `pineappl_channels_delete`. #[no_mangle] #[must_use] -pub extern "C" fn pineappl_channels_new() -> Box { - Box::default() +pub extern "C" fn pineappl_channels_new(convolutions: usize) -> Box { + Box::new(Channels(Vec::new(), convolutions)) } /// Adds a generalized linear combination of initial states to the Luminosity. @@ -1503,13 +1503,12 @@ pub extern "C" fn pineappl_channels_new() -> Box { pub unsafe extern "C" fn pineappl_channels_add( channels: *mut Channels, combinations: usize, - nb_convolutions: usize, pdg_id_combinations: *const i32, factors: *const f64, ) { let channels = unsafe { &mut *channels }; let pdg_id_pairs = - unsafe { slice::from_raw_parts(pdg_id_combinations, nb_convolutions * combinations) }; + unsafe { slice::from_raw_parts(pdg_id_combinations, channels.1 * combinations) }; let factors = if factors.is_null() { vec![1.0; combinations] } else { @@ -1518,9 +1517,9 @@ pub unsafe extern "C" fn pineappl_channels_add( channels.0.push(Channel::new( pdg_id_pairs - .chunks(nb_convolutions) + .chunks(channels.1) .zip(factors) - .map(|x| ((0..nb_convolutions).map(|i| x.0[i]).collect(), x.1)) + .map(|x| ((0..channels.1).map(|i| x.0[i]).collect(), x.1)) .collect(), )); } @@ -1535,7 +1534,10 @@ pub unsafe extern "C" fn pineappl_channels_add( pub unsafe extern "C" fn pineappl_grid_channels(grid: *const Grid) -> Box { let grid = unsafe { &*grid }; - Box::new(Channels(grid.channels().to_vec())) + Box::new(Channels( + grid.channels().to_vec(), + grid.convolutions().len(), + )) } /// An exact duplicate of `pineappl_lumi_count` to make naming (lumi -> channel) consistent. @@ -1639,7 +1641,7 @@ pub unsafe extern "C" fn pineappl_grid_new2( let channels = unsafe { &*channels }; // Construct the convolution objects - let convolutions = channels.0[0].entry()[0].0.len(); + let convolutions = channels.1; let convolution_types = unsafe { slice::from_raw_parts(convolution_types, convolutions).to_vec() }; let convolution_pdg_ids = From 7748c54e27022fb46f9229a61f40f7daeba9230f Mon Sep 17 00:00:00 2001 From: Christopher Schwan Date: Tue, 15 Apr 2025 19:26:46 +0200 Subject: [PATCH 4/4] Convert tuple into struct --- pineappl_capi/src/lib.rs | 59 ++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/pineappl_capi/src/lib.rs b/pineappl_capi/src/lib.rs index 0a704cee..5032579f 100644 --- a/pineappl_capi/src/lib.rs +++ b/pineappl_capi/src/lib.rs @@ -1460,8 +1460,11 @@ pub unsafe extern "C" fn pineappl_string_delete(string: *mut c_char) { // Here starts the generalized C-API interface. /// Type for defining a Channel function. -#[derive(Default)] -pub struct Channels(Vec, usize); +#[derive(Clone)] +pub struct Channels { + channels: Vec, + convolutions: usize, +} /// Type for defining the interpolation object #[repr(C)] @@ -1487,7 +1490,10 @@ pub struct Interp { #[no_mangle] #[must_use] pub extern "C" fn pineappl_channels_new(convolutions: usize) -> Box { - Box::new(Channels(Vec::new(), convolutions)) + Box::new(Channels { + channels: Vec::new(), + convolutions, + }) } /// Adds a generalized linear combination of initial states to the Luminosity. @@ -1506,20 +1512,23 @@ pub unsafe extern "C" fn pineappl_channels_add( pdg_id_combinations: *const i32, factors: *const f64, ) { - let channels = unsafe { &mut *channels }; + let &mut Channels { + ref mut channels, + convolutions, + } = unsafe { &mut *channels }; let pdg_id_pairs = - unsafe { slice::from_raw_parts(pdg_id_combinations, channels.1 * combinations) }; + unsafe { slice::from_raw_parts(pdg_id_combinations, convolutions * combinations) }; let factors = if factors.is_null() { vec![1.0; combinations] } else { unsafe { slice::from_raw_parts(factors, combinations) }.to_vec() }; - channels.0.push(Channel::new( + channels.push(Channel::new( pdg_id_pairs - .chunks(channels.1) + .chunks(convolutions) .zip(factors) - .map(|x| ((0..channels.1).map(|i| x.0[i]).collect(), x.1)) + .map(|x| ((0..convolutions).map(|i| x.0[i]).collect(), x.1)) .collect(), )); } @@ -1534,10 +1543,10 @@ pub unsafe extern "C" fn pineappl_channels_add( pub unsafe extern "C" fn pineappl_grid_channels(grid: *const Grid) -> Box { let grid = unsafe { &*grid }; - Box::new(Channels( - grid.channels().to_vec(), - grid.convolutions().len(), - )) + Box::new(Channels { + channels: grid.channels().to_vec(), + convolutions: grid.convolutions().len(), + }) } /// An exact duplicate of `pineappl_lumi_count` to make naming (lumi -> channel) consistent. @@ -1548,9 +1557,9 @@ pub unsafe extern "C" fn pineappl_grid_channels(grid: *const Grid) -> Box usize { - let channels = unsafe { &*channels }; + let Channels { channels, .. } = unsafe { &*channels }; - channels.0.len() + channels.len() } /// An exact duplicate of `pineappl_lumi_combinations` to make naming (lumi -> channel) consistent. @@ -1564,9 +1573,9 @@ pub unsafe extern "C" fn pineappl_channels_combinations( channels: *const Channels, entry: usize, ) -> usize { - let channels = unsafe { &*channels }; + let Channels { channels, .. } = unsafe { &*channels }; - channels.0[entry].entry().len() + channels[entry].entry().len() } /// An exact duplicate of `pineappl_lumi_delete` to make naming (lumi -> channel) consistent. @@ -1638,10 +1647,12 @@ pub unsafe extern "C" fn pineappl_grid_new2( logxia: s[4], }) .collect(); - let channels = unsafe { &*channels }; + let Channels { + channels, + convolutions, + } = unsafe { &*channels }.clone(); // Construct the convolution objects - let convolutions = channels.1; let convolution_types = unsafe { slice::from_raw_parts(convolution_types, convolutions).to_vec() }; let convolution_pdg_ids = @@ -1690,7 +1701,7 @@ pub unsafe extern "C" fn pineappl_grid_new2( Box::new(Grid::new( bins, orders, - channels.0.clone(), + channels, pid_basis, convolutions, interp_vecs, @@ -1814,11 +1825,11 @@ pub unsafe extern "C" fn pineappl_channels_entry( pdg_ids: *mut i32, factors: *mut f64, ) { - let channels = unsafe { &*channels }; - let entry = channels.0[entry].entry(); - // if the channel has no entries we assume no convolutions, which is OK we don't copy anything - // in this case - let convolutions = entry.get(0).map_or(0, |x| x.0.len()); + let Channels { + channels, + convolutions, + } = unsafe { &*channels }; + let entry = channels[entry].entry(); let pdg_ids = unsafe { slice::from_raw_parts_mut(pdg_ids, convolutions * entry.len()) }; let factors = unsafe { slice::from_raw_parts_mut(factors, entry.len()) };