Skip to main content

Executor

Struct Executor 

Source
pub struct Executor {
    pub gpu_threshold: usize,
    /* private fields */
}
Expand description

Runtime dispatcher between the CPU and (optional) GPU backends.

Fields§

§gpu_threshold: usize

Batches smaller than this use the CPU even when a GPU is present, because the GPU’s upload/dispatch/download round-trip (~100µs) dominates for small inputs. Public so callers can tune it for their hardware.

Implementations§

Source§

impl Executor

Source

pub fn init() -> Self

Probe for a GPU and build the executor. This is the only constructor.

Source

pub fn has_gpu(&self) -> bool

Whether a GPU backend is available.

Source

pub fn cpu(&self) -> &CpuBackend

Borrow the CPU backend directly (used by benchmarks).

Examples found in repository?
examples/benchmark_backends.rs (line 45)
22fn main() {
23    let exec = executor();
24    let ch = Channels::standard(32);
25    let has_gpu = exec.gpu().is_some();
26
27    println!("== adele-ring :: backend benchmark (32 channels) ==");
28    println!(
29        "GPU available: {}\n",
30        if has_gpu {
31            exec.gpu().map(|g| g.adapter_name().to_string()).unwrap_or_default()
32        } else {
33            "no (CPU-only)".to_string()
34        }
35    );
36
37    println!("{:>10} | {:>14} | {:>12} | winner", "batch_size", "cpu_rayon_us", "gpu_us");
38    println!("{}", "-".repeat(56));
39
40    for &size in &[1usize, 16, 128, 1024, 16_384, 65_536] {
41        let a = RnsBatch::from_rns_ints(&vec![RnsInt::from_i64(123, ch.clone()); size]);
42        let b = RnsBatch::from_rns_ints(&vec![RnsInt::from_i64(456, ch.clone()); size]);
43
44        let iters = if size <= 128 { 2000 } else { 100 };
45        let cpu_us = time_backend(|| exec.cpu().batch_rns_add(&a, &b), iters);
46
47        let (gpu_str, winner) = if let Some(gpu) = exec.gpu() {
48            let gpu_us = time_backend(|| gpu.batch_rns_add(&a, &b), iters);
49            let w = if cpu_us <= gpu_us { "CPU" } else { "GPU" };
50            (format!("{gpu_us:>12.2}"), w)
51        } else {
52            ("         n/a".to_string(), "CPU")
53        };
54
55        println!("{size:>10} | {cpu_us:>14.2} | {gpu_str} | {winner}");
56    }
57
58    println!(
59        "\nNote: CPU wins for small batches (GPU upload/dispatch overhead ~100us);\n\
60         GPU pulls ahead once the batch is large enough to amortize that fixed cost."
61    );
62}
Source

pub fn gpu(&self) -> Option<&GpuBackend>

Borrow the GPU backend if present (used by benchmarks).

Examples found in repository?
examples/benchmark_backends.rs (line 25)
22fn main() {
23    let exec = executor();
24    let ch = Channels::standard(32);
25    let has_gpu = exec.gpu().is_some();
26
27    println!("== adele-ring :: backend benchmark (32 channels) ==");
28    println!(
29        "GPU available: {}\n",
30        if has_gpu {
31            exec.gpu().map(|g| g.adapter_name().to_string()).unwrap_or_default()
32        } else {
33            "no (CPU-only)".to_string()
34        }
35    );
36
37    println!("{:>10} | {:>14} | {:>12} | winner", "batch_size", "cpu_rayon_us", "gpu_us");
38    println!("{}", "-".repeat(56));
39
40    for &size in &[1usize, 16, 128, 1024, 16_384, 65_536] {
41        let a = RnsBatch::from_rns_ints(&vec![RnsInt::from_i64(123, ch.clone()); size]);
42        let b = RnsBatch::from_rns_ints(&vec![RnsInt::from_i64(456, ch.clone()); size]);
43
44        let iters = if size <= 128 { 2000 } else { 100 };
45        let cpu_us = time_backend(|| exec.cpu().batch_rns_add(&a, &b), iters);
46
47        let (gpu_str, winner) = if let Some(gpu) = exec.gpu() {
48            let gpu_us = time_backend(|| gpu.batch_rns_add(&a, &b), iters);
49            let w = if cpu_us <= gpu_us { "CPU" } else { "GPU" };
50            (format!("{gpu_us:>12.2}"), w)
51        } else {
52            ("         n/a".to_string(), "CPU")
53        };
54
55        println!("{size:>10} | {cpu_us:>14.2} | {gpu_str} | {winner}");
56    }
57
58    println!(
59        "\nNote: CPU wins for small batches (GPU upload/dispatch overhead ~100us);\n\
60         GPU pulls ahead once the batch is large enough to amortize that fixed cost."
61    );
62}
Source

pub fn add(&self, a: &RnsBatch, b: &RnsBatch) -> RnsBatch

Elementwise batch addition.

Source

pub fn mul(&self, a: &RnsBatch, b: &RnsBatch) -> RnsBatch

Elementwise batch multiplication.

Source

pub fn crt(&self, batch: &RnsBatch) -> Vec<BigUint>

CRT reconstruction — always CPU-side (Garner’s algorithm is sequential).

Trait Implementations§

Source§

impl Default for Executor

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,