From 2591c0d940f8757f6631e4b08ace3e760110990d 2020-02-20 12:10:59 From: Christopher Esterhuyse Date: 2020-02-20 12:10:59 Subject: [PATCH] one buffer, multiple ranges --- diff --git a/src/runtime/experimental/api.rs b/src/runtime/experimental/api.rs index bdcf40deb46f750359e13754152b86f3ca4608f9..b10895a690b4d201f592a51f5579fa74664f0560 100644 --- a/src/runtime/experimental/api.rs +++ b/src/runtime/experimental/api.rs @@ -184,7 +184,6 @@ impl Connected { ) -> Result { for (batch_index, bit_subset) in bit_subsets.iter().enumerate() { println!("batch_index {:?}", batch_index); - use super::bits::BitChunkIter; let chunk_iter = bit_subset.iter().copied(); for index in BitChunkIter::new(chunk_iter) { println!(" index {:?}", index); @@ -233,12 +232,67 @@ pub enum PortOpRs<'a> { Out { msg: &'a [u8], port: &'a OutPort, optional: bool }, } -fn c_sync_set( +unsafe fn c_sync_set( connected: &mut Connected, inbuflen: usize, - inbuf: *mut u8, + inbufptr: *mut u8, opslen: usize, - ops: *mut PortOp, + opsptr: *mut PortOp, ) -> i32 { + let buf = as_mut_slice(inbuflen, inbufptr); + let ops = as_mut_slice(opslen, opsptr); + let (subset_index, wrote) = sync_inner(connected, buf); + assert_eq!(0, subset_index); + for op in ops { + if let Some(range) = wrote.get(&op.port) { + op.msgptr = inbufptr.add(range.start); + op.msglen = range.end - range.start; + } + } + 0 +} + +use super::bits::{usizes_for_bits, BitChunkIter}; +unsafe fn c_sync_subset( + connected: &mut Connected, + inbuflen: usize, + inbufptr: *mut u8, + opslen: usize, + opsptr: *mut PortOp, + subsetslen: usize, + subsetsptr: *const *const usize, +) -> i32 { + let buf: &mut [u8] = as_mut_slice(inbuflen, inbufptr); + let ops: &mut [PortOp] = as_mut_slice(opslen, opsptr); + let subsets: &[*const usize] = as_const_slice(subsetslen, subsetsptr); + let subsetlen = usizes_for_bits(opslen); + // don't yet know subsetptr; which subset fires unknown! + + let (subset_index, wrote) = sync_inner(connected, buf); + let subsetptr: *const usize = subsets[subset_index]; + let subset: &[usize] = as_const_slice(subsetlen, subsetptr); + + for index in BitChunkIter::new(subset.iter().copied()) { + let op = &mut ops[index as usize]; + if let Some(range) = wrote.get(&op.port) { + op.msgptr = inbufptr.add(range.start); + op.msglen = range.end - range.start; + } + } + subset_index as i32 +} + +// dummy fn for the actual synchronous round +fn sync_inner<'c, 'b>( + _connected: &'c mut Connected, + _buf: &'b mut [u8], +) -> (usize, &'b HashMap>) { todo!() } + +unsafe fn as_mut_slice<'a, T>(len: usize, ptr: *mut T) -> &'a mut [T] { + std::slice::from_raw_parts_mut(ptr, len) +} +unsafe fn as_const_slice<'a, T>(len: usize, ptr: *const T) -> &'a [T] { + std::slice::from_raw_parts(ptr, len) +} diff --git a/src/runtime/experimental/bits.rs b/src/runtime/experimental/bits.rs index 553fab5548708099898c42a979c9e33e3e8ff470..2588c4eefd54d7da286f77468a0a5996de9ef02d 100644 --- a/src/runtime/experimental/bits.rs +++ b/src/runtime/experimental/bits.rs @@ -6,12 +6,15 @@ use crate::common::*; /// observe that the bits per chunk are ordered from least to most significant bits, yielding smaller to larger usizes. /// assumes chunk_iter will yield no more than std::u32::MAX / 32 chunks -const fn usize_bytes() -> usize { +pub const fn usize_bytes() -> usize { std::mem::size_of::() } -const fn usize_bits() -> usize { +pub const fn usize_bits() -> usize { usize_bytes() * 8 } +pub const fn usizes_for_bits(bits: usize) -> usize { + (bits + (usize_bits() - 1)) / usize_bits() +} pub(crate) struct BitChunkIter> { cached: usize, @@ -145,10 +148,6 @@ impl Debug for BitMatrix { } } impl BitMatrix { - #[inline] - const fn chunk_len_ceil(value: usize) -> usize { - (value + usize_bits() - 1) & !(usize_bits() - 1) - } #[inline] const fn row_of(entity: usize) -> usize { entity / usize_bits() @@ -159,7 +158,7 @@ impl BitMatrix { } #[inline] const fn column_chunks(entity_bound: usize) -> usize { - Self::chunk_len_ceil(entity_bound) / usize_bits() + usizes_for_bits(entity_bound + 1) } #[inline] fn offsets_unchecked(&self, at: Pair) -> [usize; 2] {