server.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. use criterion::BenchmarkGroup;
  2. use criterion::measurement::WallTime;
  3. use criterion::{black_box, criterion_group, criterion_main, Criterion};
  4. use pprof::criterion::{Output, PProfProfiler};
  5. use rand::Rng;
  6. use spiral_rs::aligned_memory::AlignedMemory64;
  7. use spiral_rs::client::*;
  8. use spiral_rs::poly::*;
  9. use spiral_rs::server::*;
  10. use spiral_rs::util::*;
  11. use std::time::Duration;
  12. fn test_full_processing(group: &mut BenchmarkGroup<WallTime>) {
  13. let params = get_expansion_testing_params();
  14. let mut seeded_rng = get_seeded_rng();
  15. let target_idx = seeded_rng.gen::<usize>() % (params.db_dim_1 + params.db_dim_2);
  16. let mut client = Client::init(&params, &mut seeded_rng);
  17. let public_params = client.generate_keys();
  18. let query = client.generate_query(target_idx);
  19. let (_, db) = generate_random_db_and_get_item(&params, target_idx);
  20. group.bench_function("server_processing", |b| {
  21. b.iter(|| {
  22. black_box(process_query(
  23. black_box(&params),
  24. black_box(&public_params),
  25. black_box(&query),
  26. black_box(db.as_slice()),
  27. ));
  28. });
  29. });
  30. }
  31. fn criterion_benchmark(c: &mut Criterion) {
  32. let mut group = c.benchmark_group("server");
  33. group
  34. .sample_size(10)
  35. .measurement_time(Duration::from_secs(30));
  36. let params = get_expansion_testing_params();
  37. let v_neg1 = params.get_v_neg1();
  38. let mut seeded_rng = get_seeded_rng();
  39. let mut client = Client::init(&params, &mut seeded_rng);
  40. let public_params = client.generate_keys();
  41. let mut v = Vec::new();
  42. for _ in 0..params.poly_len {
  43. v.push(PolyMatrixNTT::zero(&params, 2, 1));
  44. }
  45. let scale_k = params.modulus / params.pt_modulus;
  46. let mut sigma = PolyMatrixRaw::zero(&params, 1, 1);
  47. sigma.data[7] = scale_k;
  48. v[0] = client.encrypt_matrix_reg(&sigma.ntt());
  49. let v_w_left = public_params.v_expansion_left.unwrap();
  50. let v_w_right = public_params.v_expansion_right.unwrap();
  51. // note: the benchmark on AVX2 is 545ms for the c++ impl
  52. group.bench_function("coefficient_expansion", |b| {
  53. b.iter(|| {
  54. coefficient_expansion(
  55. black_box(&mut v),
  56. black_box(client.g),
  57. black_box(client.stop_round),
  58. black_box(&params),
  59. black_box(&v_w_left),
  60. black_box(&v_w_right),
  61. black_box(&v_neg1),
  62. black_box(params.t_gsw * params.db_dim_2),
  63. )
  64. });
  65. });
  66. let mut seeded_rng = get_seeded_rng();
  67. let trials = params.n * params.n;
  68. let dim0 = 1 << params.db_dim_1;
  69. let num_per = 1 << params.db_dim_2;
  70. let num_items = dim0 * num_per;
  71. let db_size_words = trials * num_items * params.poly_len;
  72. let mut db = vec![0u64; db_size_words];
  73. for i in 0..db_size_words {
  74. db[i] = seeded_rng.gen();
  75. }
  76. let v_reg_sz = dim0 * 2 * params.poly_len;
  77. let mut v_reg_reoriented = AlignedMemory64::new(v_reg_sz);
  78. for i in 0..v_reg_sz {
  79. v_reg_reoriented[i] = seeded_rng.gen();
  80. }
  81. let mut out = Vec::with_capacity(num_per);
  82. for _ in 0..dim0 {
  83. out.push(PolyMatrixNTT::zero(&params, 2, 1));
  84. }
  85. // note: the benchmark on AVX2 is 45ms for the c++ impl
  86. group.bench_function("first_dimension_processing", |b| {
  87. b.iter(|| {
  88. multiply_reg_by_database(
  89. black_box(&mut out),
  90. black_box(db.as_slice()),
  91. black_box(v_reg_reoriented.as_slice()),
  92. black_box(&params),
  93. black_box(dim0),
  94. black_box(num_per)
  95. )
  96. });
  97. });
  98. // full server processing benchmark
  99. test_full_processing(&mut group);
  100. group.finish();
  101. }
  102. // criterion_group!(benches, criterion_benchmark);
  103. criterion_group! {
  104. name = benches;
  105. config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
  106. targets = criterion_benchmark
  107. }
  108. criterion_main!(benches);