Skip to content

Commit 52c8fb9

Browse files
Attempt to solve the issue of duplicating Grid & FKTable functions
1 parent 4626bb1 commit 52c8fb9

3 files changed

Lines changed: 39 additions & 59 deletions

File tree

examples/cpp/evolve-grid-identity.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,14 @@ int main() {
211211

212212
// [ convolve the FK Table ]
213213
std::vector<double> dxsec_fktable(bins);
214-
pineappl_fk_table_convolve(fktable, xfx, pdf_states, nullptr,
215-
nullptr, dxsec_fktable.data());
214+
pineappl_fktable_convolve(fktable, xfx, pdf_states, nullptr,
215+
nullptr, 1, nullptr, dxsec_fktable.data());
216216

217217
// Print the results
218218
print_results(dxsec_grid, dxsec_fktable);
219219

220220
pineappl_fktable_write(fktable, "evolved-grid.pineappl.lz4");
221221

222222
pineappl_grid_delete(grid);
223-
pineappl_fk_table_delete(fktable);
223+
pineappl_fktable_delete(fktable);
224224
}

examples/cpp/evolve-grid.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,14 @@ int main() {
269269

270270
// [ convolve the FK Table ]
271271
std::vector<double> dxsec_fktable(bins);
272-
pineappl_fk_table_convolve(fktable, xfx, pdf_states, nullptr,
273-
nullptr, dxsec_fktable.data());
272+
pineappl_fktable_convolve(fktable, xfx, pdf_states, nullptr,
273+
nullptr, 1, nullptr, dxsec_fktable.data());
274274

275275
// Print the results
276276
print_results(dxsec_grid, dxsec_fktable);
277277

278278
pineappl_fktable_write(fktable, "evolved-grid.pineappl.lz4");
279279

280280
pineappl_grid_delete(grid);
281-
pineappl_fk_table_delete(fktable);
281+
pineappl_fktable_delete(fktable);
282282
}

pineappl_capi/src/lib.rs

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,7 +2185,7 @@ pub unsafe extern "C" fn pineappl_grid_evolve(
21852185
xi: *const f64,
21862186
ren1: *const f64,
21872187
alphas: *const f64,
2188-
) -> Box<FkTable> {
2188+
) -> Box<Grid> {
21892189
let grid = unsafe { &mut *grid };
21902190

21912191
let max_orders = unsafe { slice::from_raw_parts(max_orders, 2) };
@@ -2268,94 +2268,74 @@ pub unsafe extern "C" fn pineappl_grid_evolve(
22682268
},
22692269
);
22702270

2271-
Box::new(fk_table.unwrap())
2271+
Box::new(fk_table.unwrap().grid().clone())
22722272
}
22732273

22742274
/// Delete an FK table.
22752275
#[no_mangle]
22762276
#[allow(unused_variables)]
2277-
pub extern "C" fn pineappl_fk_table_delete(fktable: Option<Box<FkTable>>) {}
2277+
pub extern "C" fn pineappl_fktable_delete(fktable: Option<Box<FkTable>>) {}
22782278

22792279
/// Write `fktable` to a file with name `filename`. If `filename` ends in `.lz4` the Fk table is
22802280
/// automatically LZ4 compressed.
22812281
///
22822282
/// # Safety
22832283
///
2284-
/// If `fktable` does not point to a valid `FkTable` object, for example when `fktable` is a null
2284+
/// If `fktable` does not point to a valid `Grid` object, for example when `fktable` is a null
22852285
/// pointer, this function is not safe to call. The parameter `filename` must be a non-`NULL`,
22862286
/// non-empty, and valid C string pointing to a non-existing, but writable file.
22872287
///
22882288
/// # Panics
22892289
///
22902290
/// TODO
22912291
#[no_mangle]
2292-
pub unsafe extern "C" fn pineappl_fktable_write(fktable: *const FkTable, filename: *const c_char) {
2293-
let fktable = unsafe { &*fktable };
2294-
let filename = unsafe { CStr::from_ptr(filename) };
2295-
let filename = filename.to_string_lossy();
2296-
let path = Path::new(filename.as_ref());
2297-
let writer = File::create(path).unwrap();
2298-
2299-
if path.extension().is_some_and(|ext| ext == "lz4") {
2300-
fktable.grid().write_lz4(writer).unwrap();
2301-
} else {
2302-
fktable.grid().write(writer).unwrap();
2292+
pub unsafe extern "C" fn pineappl_fktable_write(fktable: *const Grid, filename: *const c_char) {
2293+
unsafe {
2294+
pineappl_grid_write(fktable, filename);
23032295
}
23042296
}
23052297

23062298
/// A generalization of the convolution function for FK tables.
23072299
///
23082300
/// # Safety
23092301
///
2310-
/// If `fktable` does not point to a valid `FkTable` object, for example when `fktable` is
2302+
/// If `fktable` does not point to a valid `Grid` object, for example when `fktable` is
23112303
/// a null pointer, this function is not safe to call. The function pointer `xfx` must not
23122304
/// be null pointers and point to valid functions. The parameter `channel_mask` must either
23132305
/// be null pointers or point to arrays that are as long as `fktable` has channels, respectively.
23142306
/// Finally, `results` must be as long as `FkTable` has bins.
23152307
#[no_mangle]
2316-
pub unsafe extern "C" fn pineappl_fk_table_convolve(
2317-
fktable: *const FkTable,
2308+
pub unsafe extern "C" fn pineappl_fktable_convolve(
2309+
fktable: *const Grid,
23182310
xfx: extern "C" fn(pdg_id: i32, x: f64, q2: f64, state: *mut c_void) -> f64,
23192311
pdfs_state: *mut *mut c_void,
23202312
channel_mask: *const bool,
23212313
bin_indices: *const usize,
2314+
nb_scales: usize,
2315+
mu_scales: *const f64,
23222316
results: *mut f64,
23232317
) {
2324-
let fktable = unsafe { &*fktable };
2325-
2326-
let channel_mask = if channel_mask.is_null() {
2327-
&[]
2328-
} else {
2329-
unsafe { slice::from_raw_parts(channel_mask, fktable.channels().len()) }
2330-
};
2331-
2332-
let bin_indices = if bin_indices.is_null() {
2333-
&[]
2334-
} else {
2335-
unsafe { slice::from_raw_parts(bin_indices, fktable.grid().bwfl().len()) }
2336-
};
2337-
2338-
// TODO: Better way to do this?
2339-
let mut als = |_| 1.0;
2340-
2341-
let pdfs_slices =
2342-
unsafe { slice::from_raw_parts(pdfs_state, fktable.grid().convolutions().len()) };
2343-
let mut xfx_funcs: Vec<_> = pdfs_slices
2344-
.iter()
2345-
.map(|&state| move |id, x, q2| xfx(id, x, q2, state))
2346-
.collect();
2347-
2348-
// Construct the Convolution cache
2349-
let mut convolution_cache = ConvolutionCache::new(
2350-
fktable.grid().convolutions().to_vec(),
2351-
xfx_funcs
2352-
.iter_mut()
2353-
.map(|fx| fx as &mut dyn FnMut(i32, f64, f64) -> f64)
2354-
.collect(),
2355-
&mut als,
2356-
);
2318+
// define a dummy `alphas` function with the expected C ABI and signatures
2319+
const extern "C" fn alphas(_q2: f64, _state: *mut c_void) -> f64 {
2320+
1.0
2321+
}
23572322

2358-
let results = unsafe { slice::from_raw_parts_mut(results, fktable.grid().bwfl().len()) };
2323+
let order_mask = std::ptr::null();
2324+
let alphas_state = std::ptr::null_mut();
23592325

2360-
results.copy_from_slice(&fktable.convolve(&mut convolution_cache, bin_indices, channel_mask));
2326+
unsafe {
2327+
pineappl_grid_convolve(
2328+
fktable,
2329+
xfx,
2330+
alphas,
2331+
pdfs_state,
2332+
alphas_state,
2333+
order_mask,
2334+
channel_mask,
2335+
bin_indices,
2336+
nb_scales,
2337+
mu_scales,
2338+
results,
2339+
);
2340+
}
23612341
}

0 commit comments

Comments
 (0)