Skip to content

Commit 8cce41b

Browse files
committed
more
Signed-off-by: Adam Gutglick <adam@spiraldb.com>
1 parent 6d16cce commit 8cce41b

45 files changed

Lines changed: 234 additions & 235 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

fuzz/src/lib.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,23 @@ mod native_runtime {
4343
use vortex::VortexSessionDefault;
4444
use vortex_io::runtime::BlockingRuntime;
4545
use vortex_io::runtime::current::CurrentThreadRuntime;
46-
use vortex_io::session::RuntimeSessionExt;
46+
use vortex_io::session::RuntimeSessionBuilderExt;
4747
use vortex_session::VortexSession;
4848

4949
pub static RUNTIME: LazyLock<CurrentThreadRuntime> = LazyLock::new(CurrentThreadRuntime::new);
5050
pub static SESSION: LazyLock<VortexSession> = LazyLock::new(|| {
5151
#[allow(unused_mut)]
52-
let mut session = VortexSession::default().with_handle(RUNTIME.handle());
52+
let mut builder = VortexSession::default_builder().with_handle(RUNTIME.handle());
5353
#[cfg(all(feature = "cuda", target_os = "linux"))]
5454
// Even if the CUDA feature is enabled we need to check at
5555
// runtime whether CUDA is available in the current environment.
56+
if vortex_cuda::cuda_available() {
57+
builder = builder.with::<vortex_cuda::CudaSession>();
58+
}
59+
let session = builder.build();
60+
#[cfg(all(feature = "cuda", target_os = "linux"))]
5661
if vortex_cuda::cuda_available() {
5762
use vortex_cuda::CudaSessionExt;
58-
session = session
59-
.into_builder()
60-
.with::<vortex_cuda::CudaSession>()
61-
.build();
6263
vortex_cuda::initialize_cuda(session.cuda_session());
6364
}
6465
session
@@ -78,11 +79,14 @@ mod wasm_runtime {
7879

7980
use vortex::VortexSessionDefault;
8081
use vortex_io::runtime::wasm::WasmRuntime;
81-
use vortex_io::session::RuntimeSessionExt;
82+
use vortex_io::session::RuntimeSessionBuilderExt;
8283
use vortex_session::VortexSession;
8384

84-
pub static SESSION: LazyLock<VortexSession> =
85-
LazyLock::new(|| VortexSession::default().with_handle(WasmRuntime::handle()));
85+
pub static SESSION: LazyLock<VortexSession> = LazyLock::new(|| {
86+
VortexSession::default_builder()
87+
.with_handle(WasmRuntime::handle())
88+
.build()
89+
});
8690
}
8791

8892
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]

vortex-array/src/arrays/chunked/vtable/canonical.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ mod tests {
320320
use crate::dtype::PType::I32;
321321
use crate::memory::DefaultHostAllocator;
322322
use crate::memory::HostAllocator;
323-
use crate::memory::MemorySessionExt;
323+
use crate::memory::MemorySessionBuilderExt;
324324
use crate::memory::WritableHostBuffer;
325325
use crate::scalar::Scalar;
326326
use crate::validity::Validity;
@@ -639,10 +639,10 @@ mod tests {
639639
fn list_canonicalize_uses_memory_session_allocator() {
640640
let allocations = Arc::new(AtomicUsize::new(0));
641641
let session = crate::array_session()
642-
.build()
643642
.with_allocator(Arc::new(CountingAllocator {
644643
allocations: Arc::clone(&allocations),
645-
}));
644+
}))
645+
.build();
646646
let mut ctx = ExecutionCtx::new(session);
647647

648648
let l1 = ListArray::try_new(

vortex-array/src/expr/proto.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ mod tests {
117117
fn unknown_expression_id_allow_unknown() {
118118
let session = VortexSession::builder()
119119
.with::<ScalarFnSession>()
120-
.build()
121-
.allow_unknown();
120+
.allow_unknown()
121+
.build();
122122

123123
let expr_proto = pb::Expr {
124124
id: "vortex.test.foreign_scalar_fn".to_string(),

vortex-array/src/memory.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use vortex_error::vortex_ensure;
1818
use vortex_error::vortex_err;
1919
use vortex_session::SessionExt;
2020
use vortex_session::SessionVar;
21-
use vortex_session::VortexSession;
21+
use vortex_session::VortexSessionBuilder;
2222

2323
/// Mutable host buffer contract used by [`WritableHostBuffer`].
2424
pub trait HostBufferMut: Send + 'static {
@@ -221,17 +221,23 @@ pub trait MemorySessionExt: SessionExt {
221221
fn allocator(&self) -> HostAllocatorRef {
222222
self.memory().allocator()
223223
}
224-
225-
/// Returns a new session configured to use `allocator` as its host allocator.
226-
fn with_allocator(self, allocator: HostAllocatorRef) -> VortexSession {
227-
let mut builder = self.session().into_builder();
228-
builder.get_mut::<MemorySession>().set_allocator(allocator);
229-
builder.build()
230-
}
231224
}
232225

233226
impl<S: SessionExt> MemorySessionExt for S {}
234227

228+
/// Extension trait for configuring session-scoped memory before a session is built.
229+
pub trait MemorySessionBuilderExt {
230+
/// Configure the host allocator.
231+
fn with_allocator(self, allocator: HostAllocatorRef) -> Self;
232+
}
233+
234+
impl MemorySessionBuilderExt for VortexSessionBuilder {
235+
fn with_allocator(mut self, allocator: HostAllocatorRef) -> Self {
236+
self.get_mut::<MemorySession>().set_allocator(allocator);
237+
self
238+
}
239+
}
240+
235241
/// Default host allocator.
236242
#[derive(Debug, Default)]
237243
pub struct DefaultHostAllocator;

vortex-array/src/session/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,14 @@ mod tests {
157157

158158
#[test]
159159
fn initialize_registers_builtin_kernels_into_empty_array_session() {
160-
let session = VortexSession::builder()
161-
.with_some(ArraySession::empty())
162-
.build();
163-
164-
assert!(!session.kernels().has_execute_parent(Binary.id(), Bool.id()));
160+
let mut builder = VortexSession::builder().with_some(ArraySession::empty());
165161

166-
let mut builder = session.into_builder();
162+
assert!(
163+
!builder
164+
.get_mut::<ArraySession>()
165+
.kernels()
166+
.has_execute_parent(Binary.id(), Bool.id())
167+
);
167168
crate::initialize(&mut builder);
168169
let session = builder.build();
169170

vortex-bench/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ pub use output::BenchmarkOutput;
6565
pub use output::create_output_writer;
6666
use vortex::VortexSessionDefault;
6767
pub use vortex::error::vortex_panic;
68-
use vortex::io::session::RuntimeSessionExt;
68+
use vortex::io::session::RuntimeSessionBuilderExt;
6969
use vortex::session::VortexSession;
7070

7171
// All benchmarks run with mimalloc for consistency.
7272
#[global_allocator]
7373
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
7474

7575
pub static SESSION: LazyLock<VortexSession> =
76-
LazyLock::new(|| VortexSession::default().with_tokio());
76+
LazyLock::new(|| VortexSession::default_builder().with_tokio().build());
7777

7878
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
7979
pub struct Target {

vortex-cuda/ffi/src/lib.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::ptr;
1414
use arrow_schema::ffi::FFI_ArrowSchema;
1515
use vortex::error::VortexResult;
1616
use vortex::error::vortex_ensure;
17+
use vortex::error::vortex_err;
1718
use vortex::session::VortexSession;
1819
use vortex_cuda::CudaSession;
1920
use vortex_cuda::arrow::ArrowDeviceArray;
@@ -34,20 +35,14 @@ use vortex_ffi::vx_session_ref;
3435
const VX_CUDA_OK: c_int = 0;
3536
const VX_CUDA_ERR: c_int = 1;
3637

37-
/// Return a Vortex session with a [`CudaSession`] session variable.
38-
///
39-
/// If `session` already has CUDA support, this returns a clone of it. Otherwise it
40-
/// returns a new session cloned from `session` with a default [`CudaSession`] attached.
41-
fn session_with_cuda(session: &VortexSession) -> VortexResult<VortexSession> {
42-
if session.get_opt::<CudaSession>().is_some() {
43-
return Ok(session.clone());
38+
fn session_with_cuda(session: &VortexSession) -> VortexResult<&VortexSession> {
39+
if session.get_opt::<CudaSession>().is_none() {
40+
return Err(vortex_err!(
41+
InvalidArgument: "CUDA session state is not configured; create the session with vx_cuda_session_new"
42+
));
4443
}
4544

46-
Ok(session
47-
.clone()
48-
.into_builder()
49-
.with_some(CudaSession::try_default()?)
50-
.build())
45+
Ok(session)
5146
}
5247

5348
/// Create a CUDA Vortex session.
@@ -65,7 +60,7 @@ pub unsafe extern "C-unwind" fn vx_cuda_session_new(
6560
try_or(error_out, ptr::null_mut(), || {
6661
let cuda_session = CudaSession::try_default()?;
6762
Ok(vx_session_new_with(|session| {
68-
session.into_builder().with_some(cuda_session).build()
63+
session.with_some(cuda_session)
6964
}))
7065
})
7166
}
@@ -101,7 +96,7 @@ pub unsafe extern "C-unwind" fn vx_cuda_array_export_arrow_device(
10196

10297
let session = session_with_cuda(unsafe { vx_session_ref(session) }?)?;
10398
let array = unsafe { vx_array_ref(array) }?.clone();
104-
let mut ctx = CudaSession::create_execution_ctx(&session)?;
99+
let mut ctx = CudaSession::create_execution_ctx(session)?;
105100
let exported =
106101
futures::executor::block_on(array.export_device_array_with_schema(&mut ctx))?;
107102

@@ -146,7 +141,7 @@ pub unsafe extern "C-unwind" fn vx_cuda_partition_scan_arrow_device_stream(
146141

147142
let session = session_with_cuda(unsafe { vx_session_ref(session) }?)?;
148143
// Drive the stream on the same runtime the partition's scan spawned its work onto.
149-
let device_stream = array_stream.export_device_array_stream(&session, ffi_runtime())?;
144+
let device_stream = array_stream.export_device_array_stream(session, ffi_runtime())?;
150145

151146
unsafe { ptr::write(out_stream, device_stream) };
152147
Ok(VX_CUDA_OK)
@@ -177,6 +172,12 @@ mod tests {
177172
Box::into_raw(Box::new(session)).cast::<vx_session>()
178173
}
179174

175+
fn test_cuda_session() -> VortexResult<VortexSession> {
176+
Ok(vortex::array::array_session()
177+
.with_some(CudaSession::try_default()?)
178+
.build())
179+
}
180+
180181
unsafe fn free_test_session(session: *mut vx_session) {
181182
unsafe { drop(Box::from_raw(session.cast::<VortexSession>())) };
182183
}
@@ -216,9 +217,9 @@ mod tests {
216217
}
217218

218219
#[cuda_test]
219-
fn test_export_primitive_arrow_device() {
220+
fn test_export_primitive_arrow_device() -> VortexResult<()> {
220221
let mut error = ptr::null_mut();
221-
let session = test_session(VortexSession::default());
222+
let session = test_session(test_cuda_session()?);
222223
let array = test_array(PrimitiveArray::from_iter(0u32..5));
223224
let mut schema = FFI_ArrowSchema::empty();
224225
let mut device_array = empty_device_array();
@@ -235,7 +236,7 @@ mod tests {
235236
assert_eq!(status, VX_CUDA_OK);
236237
assert!(error.is_null());
237238

238-
let field = Field::try_from(&schema).expect("schema should be a field");
239+
let field = Field::try_from(&schema)?;
239240
assert_eq!(field.name(), "");
240241
assert_eq!(device_array.array.length, 5);
241242
assert_eq!(device_array.array.n_buffers, 2);
@@ -249,12 +250,13 @@ mod tests {
249250
free_test_array(array);
250251
free_test_session(session);
251252
}
253+
Ok(())
252254
}
253255

254256
#[cuda_test]
255257
fn test_export_struct_arrow_device_table() -> VortexResult<()> {
256258
let mut error = ptr::null_mut();
257-
let session = test_session(VortexSession::default());
259+
let session = test_session(test_cuda_session()?);
258260
let array = test_array(StructArray::try_new(
259261
["ids", "values"].into(),
260262
vec![

vortex-cuda/src/hybrid_dispatch/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ mod tests {
302302
use vortex::encodings::fastlanes;
303303
use vortex::encodings::zstd::ZstdBuffers;
304304

305-
let mut builder = crate::cuda_session().into_builder();
305+
let mut builder = crate::cuda_session_builder();
306306
fastlanes::initialize(&mut builder);
307307
builder.get_mut::<ArraySession>().register(ZstdBuffers);
308308
let session = builder.build();

vortex-cuda/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ pub fn initialize_cuda(session: &CudaSession) {
142142
/// # Panics
143143
///
144144
/// Panics if CUDA device 0 cannot be initialized (the same contract as [`CudaSession::default`]).
145+
#[cfg(any(test, feature = "_test-harness"))]
146+
pub fn cuda_session_builder() -> vortex::session::VortexSessionBuilder {
147+
vortex::array::array_session().with::<CudaSession>()
148+
}
149+
145150
#[cfg(any(test, feature = "_test-harness"))]
146151
pub fn cuda_session() -> vortex::session::VortexSession {
147-
vortex::array::array_session().with::<CudaSession>().build()
152+
cuda_session_builder().build()
148153
}

vortex-cxx/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use scalar::*;
1818
use vortex::VortexSessionDefault;
1919
use vortex::io::runtime::BlockingRuntime;
2020
use vortex::io::runtime::current::CurrentThreadRuntime;
21-
use vortex::io::session::RuntimeSessionExt;
21+
use vortex::io::session::RuntimeSessionBuilderExt;
2222
use vortex::session::VortexSession;
2323
use write::*;
2424

@@ -28,8 +28,11 @@ use write::*;
2828
// this runtime.
2929
pub(crate) static RUNTIME: LazyLock<CurrentThreadRuntime> =
3030
LazyLock::new(CurrentThreadRuntime::new);
31-
pub(crate) static SESSION: LazyLock<VortexSession> =
32-
LazyLock::new(|| VortexSession::default().with_handle(RUNTIME.handle()));
31+
pub(crate) static SESSION: LazyLock<VortexSession> = LazyLock::new(|| {
32+
VortexSession::default_builder()
33+
.with_handle(RUNTIME.handle())
34+
.build()
35+
});
3336

3437
#[cxx::bridge(namespace = "vortex::ffi")]
3538
#[allow(let_underscore_drop)]

0 commit comments

Comments
 (0)