Skip to content

Commit

Permalink
Update GPU backend
Browse files Browse the repository at this point in the history
  • Loading branch information
cgouert committed Nov 9, 2023
1 parent 19533a7 commit c3e92af
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ rand = "0.8.5"
rayon = "1.7.0"
termion = "2.0.1"
tfhe = { version = "0.4.1", features = ["boolean", "shortint", "integer", "x86_64-unix"] }
concrete-core = {git = "https://github.com/TrustworthyComputing/concrete-core", rev = "77f30a2", version = "=1.0.1", features=["backend_default", "backend_default_parallel", "backend_cuda", "x86_64", "concrete-cuda"], optional = true}
concrete-core = {git = "https://github.com/TrustworthyComputing/concrete-core", rev = "fcb6f65", version = "=1.0.1", features=["backend_default", "backend_default_parallel", "backend_cuda", "x86_64", "concrete-cuda"], optional = true}
thiserror = "1.0"

[[bin]]
Expand Down
4 changes: 2 additions & 2 deletions src/bin/helm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fn main() {
// Gate mode (GPU)
let mut start = Instant::now();
let (lwe_dim, glwe_dim, poly_size) = (
LweDimension(630),
LweDimension(512),
GlweDimension(1),
PolynomialSize(1024),
);
Expand All @@ -157,7 +157,7 @@ fn main() {
let mut parallel_engine =
DefaultParallelEngine::new(Box::new(UnixSeeder::new(random_u128)))
.unwrap();
let mut cuda_engine = CudaEngine::new(()).unwrap();
let cuda_engine = CudaEngine::new(()).unwrap();

// Generate the keys
let h_input_key =
Expand Down
31 changes: 15 additions & 16 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::gates::{Gate, GateType};
#[cfg(feature = "gpu")]
use concrete_core::prelude::*;
#[cfg(feature = "gpu")]
use concrete_core::specification::parameters::{GlweDimension, LweDimension, PolynomialSize};
use concrete_core::specification::parameters::LweDimension;
use itertools::Itertools;
use rayon::prelude::*;
use std::{
Expand Down Expand Up @@ -794,86 +794,85 @@ impl<'a> EvalCircuit<LweCiphertext32> for CircuitCuda<'a> {
.par_iter_mut()
.for_each(|(gate_type, out_vec)| {
let d_ct_vec_1 = d_input_vecs_1.get(&gate_type).unwrap();
let cuda_engine_mut = &mut self.cuda_engine.clone();
if *gate_type == GateType::Not {
cuda_engine_mut
self.cuda_engine
.discard_not_lwe_ciphertext_vector(
&mut out_vec.write().unwrap().as_mut().unwrap(),
d_ct_vec_1,
0,
)
.unwrap();
} else {
let d_ct_vec_2 = d_input_vecs_2.get(&gate_type).unwrap();
if *gate_type == GateType::And {
cuda_engine_mut.inc_stream_idx(1);
cuda_engine_mut
self.cuda_engine
.discard_and_lwe_ciphertext_vector(
&mut out_vec.write().unwrap().as_mut().unwrap(),
d_ct_vec_1,
d_ct_vec_2,
&self.device_bootstrap_key,
&self.device_keyswitch_key,
1,
)
.unwrap();
} else if *gate_type == GateType::Nand {
cuda_engine_mut.inc_stream_idx(2);
cuda_engine_mut
self.cuda_engine
.discard_nand_lwe_ciphertext_vector(
&mut out_vec.write().unwrap().as_mut().unwrap(),
d_ct_vec_1,
d_ct_vec_2,
&self.device_bootstrap_key,
&self.device_keyswitch_key,
2,
)
.unwrap();
} else if *gate_type == GateType::Or {
cuda_engine_mut.inc_stream_idx(3);
cuda_engine_mut
self.cuda_engine
.discard_or_lwe_ciphertext_vector(
&mut out_vec.write().unwrap().as_mut().unwrap(),
d_ct_vec_1,
d_ct_vec_2,
&self.device_bootstrap_key,
&self.device_keyswitch_key,
3,
)
.unwrap();
} else if *gate_type == GateType::Nor {
cuda_engine_mut.inc_stream_idx(4);
cuda_engine_mut
self.cuda_engine
.discard_nor_lwe_ciphertext_vector(
&mut out_vec.write().unwrap().as_mut().unwrap(),
d_ct_vec_1,
d_ct_vec_2,
&self.device_bootstrap_key,
&self.device_keyswitch_key,
4,
)
.unwrap();
} else if *gate_type == GateType::Xor {
cuda_engine_mut.inc_stream_idx(5);
cuda_engine_mut
self.cuda_engine
.discard_xor_lwe_ciphertext_vector(
&mut out_vec.write().unwrap().as_mut().unwrap(),
d_ct_vec_1,
d_ct_vec_2,
&self.device_bootstrap_key,
&self.device_keyswitch_key,
5,
)
.unwrap();
} else if *gate_type == GateType::Xnor {
cuda_engine_mut.inc_stream_idx(6);
cuda_engine_mut
self.cuda_engine
.discard_xnor_lwe_ciphertext_vector(
&mut out_vec.write().unwrap().as_mut().unwrap(),
d_ct_vec_1,
d_ct_vec_2,
&self.device_bootstrap_key,
&self.device_keyswitch_key,
6,
)
.unwrap();
}
}
});
println!("DONE");
// Keep track of current index for each output vector
let mut vec_idx_by_gate_type = d_output_ctxt_vecs
.iter()
Expand Down
4 changes: 2 additions & 2 deletions tests/circuit_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn encrypted_16_bit_multiplier_gpu() {
let mut default_engine = DefaultEngine::new(Box::new(UnixSeeder::new(unsafe_secret))).unwrap();
let mut parallel_engine =
DefaultParallelEngine::new(Box::new(UnixSeeder::new(unsafe_secret))).unwrap();
let mut cuda_engine = CudaEngine::new(()).unwrap();
let cuda_engine = CudaEngine::new(()).unwrap();

// Generate the keys
let h_input_key = default_engine.generate_new_lwe_secret_key(lwe_dim).unwrap();
Expand Down Expand Up @@ -224,7 +224,7 @@ fn encrypted_32_bit_multiplier_gpu() {
let mut default_engine = DefaultEngine::new(Box::new(UnixSeeder::new(unsafe_secret))).unwrap();
let mut parallel_engine =
DefaultParallelEngine::new(Box::new(UnixSeeder::new(unsafe_secret))).unwrap();
let mut cuda_engine = CudaEngine::new(()).unwrap();
let cuda_engine = CudaEngine::new(()).unwrap();

// Generate the keys
let h_input_key = default_engine.generate_new_lwe_secret_key(lwe_dim).unwrap();
Expand Down

0 comments on commit c3e92af

Please sign in to comment.