Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/gpu/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

use super::{GpuConfig, GpuContext};
use anyhow::Result;
use std::sync::Arc;
use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock};
use tracing::info;
use wgpu::{BindGroupLayout, ComputePipeline};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum WorkgroupVariant {
Wg64,
Wg128,
Expand All @@ -31,6 +32,17 @@ pub struct KangarooPipeline {

impl KangarooPipeline {
pub fn new(ctx: &GpuContext, variant: WorkgroupVariant) -> Result<Self> {
static PIPELINE_CACHE: OnceLock<
Mutex<HashMap<(usize, WorkgroupVariant), KangarooPipeline>>,
> = OnceLock::new();
Comment thread
oritwoen marked this conversation as resolved.

let device_key = Arc::as_ptr(&ctx.device) as usize;
let cache = PIPELINE_CACHE.get_or_init(|| Mutex::new(HashMap::new()));
let mut guard = cache.lock().expect("pipeline cache poisoned");
Comment thread
oritwoen marked this conversation as resolved.
if let Some(pipeline) = guard.get(&(device_key, variant)).cloned() {
return Ok(pipeline);
Comment thread
oritwoen marked this conversation as resolved.
}

info!("Loading shader sources...");

let field = crate::gpu_crypto::shaders::FIELD_WGSL;
Expand Down Expand Up @@ -148,10 +160,14 @@ impl KangarooPipeline {
});
info!("Compute pipeline created");

Ok(Self {
let pipeline = Self {
pipeline: Arc::new(pipeline),
bind_group_layout: Arc::new(bind_group_layout),
variant,
})
};

guard.insert((device_key, variant), pipeline.clone());

Ok(pipeline)
}
}
Loading