primal-barracuda 0.4.0

barraCuda — BARrier-free Rust Abstracted Cross-platform Unified Dimensional Algebra. Vendor-agnostic GPU compute via WGSL.
Documentation
// SPDX-License-Identifier: AGPL-3.0-or-later
//! DF64 protein folding shaders for AlphaFold2-style structure prediction.
//!
//! These shaders use double-float (f32-pair) arithmetic for high precision on
//! FP32-only hardware. Compile via [`crate::device::WgpuDevice::compile_shader_df64`].

use crate::device::WgpuDevice;
use crate::error::Result;

// ── Shader sources ──

/// WGSL source for torsion angle computation (DF64).
pub const WGSL_TORSION_ANGLES_DF64: &str =
    include_str!("../shaders/folding/torsion_angles_df64.wgsl");
/// WGSL source for pairwise distance matrix (DF64).
pub const WGSL_DISTANCE_MATRIX_DF64: &str =
    include_str!("../shaders/folding/distance_matrix_df64.wgsl");
/// WGSL source for RMSD computation (DF64).
pub const WGSL_RMSD_DF64: &str = include_str!("../shaders/folding/rmsd_df64.wgsl");
/// WGSL source for contact map from distance matrix (DF64).
pub const WGSL_CONTACT_MAP_DF64: &str = include_str!("../shaders/folding/contact_map_df64.wgsl");
/// WGSL source for Lennard-Jones potential (DF64).
pub const WGSL_LENNARD_JONES_DF64: &str =
    include_str!("../shaders/folding/lennard_jones_df64.wgsl");
/// WGSL source for Coulomb electrostatic energy (DF64).
pub const WGSL_COULOMB_ELECTROSTATIC_DF64: &str =
    include_str!("../shaders/folding/coulomb_electrostatic_df64.wgsl");
/// WGSL source for hydrogen bond energy (DF64).
pub const WGSL_HYDROGEN_BOND_DF64: &str =
    include_str!("../shaders/folding/hydrogen_bond_df64.wgsl");
/// WGSL source for solvation energy (DF64).
pub const WGSL_SOLVATION_ENERGY_DF64: &str =
    include_str!("../shaders/folding/solvation_energy_df64.wgsl");
/// WGSL source for gradient descent step (DF64).
pub const WGSL_GRADIENT_DESCENT_DF64: &str =
    include_str!("../shaders/folding/gradient_descent_df64.wgsl");
/// WGSL source for simulated annealing (DF64).
pub const WGSL_SIMULATED_ANNEALING_DF64: &str =
    include_str!("../shaders/folding/simulated_annealing_df64.wgsl");
/// WGSL source for backbone restraints (DF64).
pub const WGSL_BACKBONE_RESTRAINTS_DF64: &str =
    include_str!("../shaders/folding/backbone_restraints_df64.wgsl");
/// WGSL source for side chain packing (DF64).
pub const WGSL_SIDE_CHAIN_PACKING_DF64: &str =
    include_str!("../shaders/folding/side_chain_packing_df64.wgsl");
/// WGSL source for MSA attention (DF64).
pub const WGSL_MSA_ATTENTION_DF64: &str =
    include_str!("../shaders/folding/msa_attention_df64.wgsl");
/// WGSL source for pair representation (DF64).
pub const WGSL_PAIR_REPRESENTATION_DF64: &str =
    include_str!("../shaders/folding/pair_representation_df64.wgsl");
/// WGSL source for structure module (DF64).
pub const WGSL_STRUCTURE_MODULE_DF64: &str =
    include_str!("../shaders/folding/structure_module_df64.wgsl");

/// Protein folding DF64 shader operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FoldingOp {
    /// Phi/psi/omega backbone torsion angles from Cα coordinates.
    TorsionAngles,
    /// Pairwise Cα distance matrix.
    DistanceMatrix,
    /// Root Mean Square Deviation (squared deviations for reduction).
    Rmsd,
    /// Binary contact map from distance matrix with threshold.
    ContactMap,
    /// 12-6 Lennard-Jones potential for van der Waals.
    LennardJones,
    /// Coulomb electrostatic energy with dielectric.
    CoulombElectrostatic,
    /// Hydrogen bond energy (donor-acceptor geometry).
    HydrogenBond,
    /// Implicit solvation (GBSA-style, per-atom).
    SolvationEnergy,
    /// Steepest descent energy minimization step.
    GradientDescent,
    /// Simulated annealing Metropolis acceptance.
    SimulatedAnnealing,
    /// Harmonic restraints on backbone atoms.
    BackboneRestraints,
    /// Rotamer scoring for side chain placement.
    SideChainPacking,
    /// MSA row/column attention scores.
    MsaAttention,
    /// Pair features update (outer product mean).
    PairRepresentation,
    /// IPA (Invariant Point Attention) frame update.
    StructureModule,
}

impl FoldingOp {
    /// WGSL source for this operation.
    #[must_use]
    pub fn source(&self) -> &'static str {
        match self {
            Self::TorsionAngles => WGSL_TORSION_ANGLES_DF64,
            Self::DistanceMatrix => WGSL_DISTANCE_MATRIX_DF64,
            Self::Rmsd => WGSL_RMSD_DF64,
            Self::ContactMap => WGSL_CONTACT_MAP_DF64,
            Self::LennardJones => WGSL_LENNARD_JONES_DF64,
            Self::CoulombElectrostatic => WGSL_COULOMB_ELECTROSTATIC_DF64,
            Self::HydrogenBond => WGSL_HYDROGEN_BOND_DF64,
            Self::SolvationEnergy => WGSL_SOLVATION_ENERGY_DF64,
            Self::GradientDescent => WGSL_GRADIENT_DESCENT_DF64,
            Self::SimulatedAnnealing => WGSL_SIMULATED_ANNEALING_DF64,
            Self::BackboneRestraints => WGSL_BACKBONE_RESTRAINTS_DF64,
            Self::SideChainPacking => WGSL_SIDE_CHAIN_PACKING_DF64,
            Self::MsaAttention => WGSL_MSA_ATTENTION_DF64,
            Self::PairRepresentation => WGSL_PAIR_REPRESENTATION_DF64,
            Self::StructureModule => WGSL_STRUCTURE_MODULE_DF64,
        }
    }

    /// Shader label for debugging.
    #[must_use]
    pub fn label(&self) -> &'static str {
        match self {
            Self::TorsionAngles => "torsion_angles_df64",
            Self::DistanceMatrix => "distance_matrix_df64",
            Self::Rmsd => "rmsd_df64",
            Self::ContactMap => "contact_map_df64",
            Self::LennardJones => "lennard_jones_df64",
            Self::CoulombElectrostatic => "coulomb_electrostatic_df64",
            Self::HydrogenBond => "hydrogen_bond_df64",
            Self::SolvationEnergy => "solvation_energy_df64",
            Self::GradientDescent => "gradient_descent_df64",
            Self::SimulatedAnnealing => "simulated_annealing_df64",
            Self::BackboneRestraints => "backbone_restraints_df64",
            Self::SideChainPacking => "side_chain_packing_df64",
            Self::MsaAttention => "msa_attention_df64",
            Self::PairRepresentation => "pair_representation_df64",
            Self::StructureModule => "structure_module_df64",
        }
    }
}

/// Compile a folding DF64 shader for the given device and operation.
///
/// # Errors
///
/// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
/// readback fails (e.g. device lost or out of memory).
pub fn compile_folding_shader(device: &WgpuDevice, op: FoldingOp) -> Result<wgpu::ShaderModule> {
    let source = op.source();
    let label = op.label();
    Ok(device.compile_shader_df64(source, Some(label)))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_folding_shaders_compile() {
        let device = crate::device::test_pool::get_test_device_sync();
        let ops = [
            FoldingOp::TorsionAngles,
            FoldingOp::DistanceMatrix,
            FoldingOp::Rmsd,
            FoldingOp::ContactMap,
            FoldingOp::LennardJones,
            FoldingOp::CoulombElectrostatic,
            FoldingOp::HydrogenBond,
            FoldingOp::SolvationEnergy,
            FoldingOp::GradientDescent,
            FoldingOp::SimulatedAnnealing,
            FoldingOp::BackboneRestraints,
            FoldingOp::SideChainPacking,
            FoldingOp::MsaAttention,
            FoldingOp::PairRepresentation,
            FoldingOp::StructureModule,
        ];
        for op in ops {
            let result = compile_folding_shader(&device, op);
            assert!(result.is_ok(), "{} shader failed to compile", op.label());
        }
    }
}