zksync-gpu-prover 0.155.11

ZKsync GPU prover utilities
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use super::*;
use crate::cuda_bindings::{device_info, DeviceBuf, GpuContext, GpuResult};
use core::ops::Range;
use gpu_ffi::*;

mod copying_operations;
mod polynomial_identifiers;
mod polynomial_operations;
mod proving_operations;
#[cfg(test)]
mod tests;
pub use copying_operations::*;
use itertools::Itertools;
pub use polynomial_identifiers::*;
pub use polynomial_operations::*;
pub use proving_operations::*;

const NUM_MSM_RESULT_POINTS: usize = 254;
const NUM_POLY_EVAL_RESULT_ELEMS: usize = 1;

pub struct DeviceMemoryManager<F: PrimeField, MC: ManagerConfigs> {
    pub(crate) ctx: Vec<GpuContext>,
    pub(crate) slots: Vec<(Vec<DeviceBuf<F>>, SlotStatus)>,
    pub(crate) host_slots: Vec<(AsyncVec<F>, SlotStatus)>,
    pub(crate) host_buf_for_msm: AsyncVec<G1>,
    pub(crate) host_buf_for_poly_eval: AsyncVec<Fr>,
    _configs: std::marker::PhantomData<MC>,
}

impl<F: PrimeField, MC: ManagerConfigs> DeviceMemoryManager<F, MC> {
    pub fn init(device_ids: &[usize], bases: &[CompactG1Affine]) -> GpuResult<Self> {
        assert_eq!(
            bases.len(),
            MC::FULL_SLOT_SIZE,
            "number of bases should be equal to size of full slot"
        );
        dbg!(device_ids);
        let num_devices = device_ids.len();
        assert_eq!(num_devices, MC::NUM_GPUS);
        let mut ctx = vec![];
        for (device_id, bases_chunk) in device_ids.iter().zip(bases.chunks(MC::SLOT_SIZE)) {
            let mut context = GpuContext::new_with_affinity(*device_id, device_ids)?;

            context.set_up_ff()?;
            context.set_up_ntt()?;
            context.set_up_pn()?;
            context.set_up_msm(bases_chunk)?;

            ctx.push(context);
        }
        let mut manager = Self {
            ctx,
            slots: Vec::with_capacity(MC::NUM_SLOTS),
            host_slots: Vec::with_capacity(MC::NUM_HOST_SLOTS),
            host_buf_for_msm: AsyncVec::allocate_new(NUM_MSM_RESULT_POINTS),
            host_buf_for_poly_eval: AsyncVec::allocate_new(NUM_POLY_EVAL_RESULT_ELEMS),
            _configs: std::marker::PhantomData::<MC>,
        };

        manager.allocate_slots().unwrap();
        manager.allocate_host_slots().unwrap();
        manager.allocate_mem_pool().unwrap();

        Ok(manager)
    }

    pub fn allocate_new(bases: &[CompactG1Affine]) -> GpuResult<Self> {
        assert_eq!(
            bases.len(),
            MC::FULL_SLOT_SIZE,
            "number of bases should be equal to size of full slot"
        );

        let mut ctx = vec![];
        for (device_id, bases_chunk) in bases.chunks(MC::SLOT_SIZE).enumerate() {
            let mut context = GpuContext::new(device_id)?;

            context.set_up_ff()?;
            context.set_up_ntt()?;
            context.set_up_pn()?;
            context.set_up_msm(bases_chunk)?;

            ctx.push(context);
        }

        Ok(Self {
            ctx,
            slots: Vec::with_capacity(MC::NUM_SLOTS),
            host_slots: Vec::with_capacity(MC::NUM_HOST_SLOTS),
            host_buf_for_msm: AsyncVec::allocate_new(NUM_MSM_RESULT_POINTS),
            host_buf_for_poly_eval: AsyncVec::allocate_new(NUM_POLY_EVAL_RESULT_ELEMS),
            _configs: std::marker::PhantomData::<MC>,
        })
    }

    pub fn new_from_ctx(ctx: Vec<GpuContext>) -> Self {
        assert_eq!(ctx.len(), MC::NUM_GPUS, "number of GpuContexts is wrong");
        for id in 0..MC::NUM_GPUS {
            // assert_eq!(ctx[id].device_id(), id, "enumeration of GpuContex is wrong");
            assert!(ctx[id].ff, "ff should be set up for GpuContex");
            assert!(ctx[id].ntt, "ntt should be set up for GpuContex");
            assert!(
                ctx[id].bases.is_none(),
                "msm should be set up for GpuContex"
            );
        }

        Self {
            ctx,
            slots: Vec::with_capacity(MC::NUM_SLOTS),
            host_slots: Vec::with_capacity(MC::NUM_HOST_SLOTS),
            host_buf_for_msm: AsyncVec::allocate_new(NUM_MSM_RESULT_POINTS),
            host_buf_for_poly_eval: AsyncVec::allocate_new(NUM_POLY_EVAL_RESULT_ELEMS),
            _configs: std::marker::PhantomData::<MC>,
        }
    }

    pub fn allocate_mem_pool(&mut self) -> GpuResult<()> {
        for id in 0..MC::NUM_GPUS {
            self.ctx[id].set_up_mem_pool()?;
        }
        Ok(())
    }

    // pub fn allocate_dummy_static_memory(&mut self) -> GpuResult<()> {
    //     for device_id in 0..MC::NUM_GPUS {
    //         let info = device_info(device_id as i32)?;

    //         const LOG_SLACK: u32 = 25;
    //         let size = (((info.free >> LOG_SLACK) - 1) << LOG_SLACK) >> 5;

    //         self.dummy_buf.push(DeviceBuf::<F>::alloc_static(
    //             &self.ctx[device_id],
    //             size as usize,
    //         )?);
    //     }
    //     Ok(())
    // }

    // pub fn deallocate_dummy_static_memory(&mut self) -> GpuResult<()> {
    //     self.dummy_buf.clear();

    //     Ok(())
    // }

    pub fn allocate_slots(&mut self) -> GpuResult<()> {
        assert_eq!(self.slots.len(), 0, "slots are already allocated");

        let mut slots: Vec<Vec<DeviceBuf<F>>> = (0..MC::NUM_SLOTS).map(|_| vec![]).collect();

        for ctx_id in 0..MC::NUM_GPUS {
            let mut big_splited_buf =
                DeviceBuf::<F>::alloc_static(&self.ctx[ctx_id], MC::SLOT_SIZE * MC::NUM_SLOTS)?
                    .split(MC::NUM_SLOTS);

            for slot_id in (0..MC::NUM_SLOTS).rev() {
                slots[slot_id].push(big_splited_buf.pop().unwrap());
            }
        }

        for slot in slots.into_iter() {
            self.slots.push((slot, SlotStatus::Free));
        }
        Ok(())
    }

    pub fn allocate_host_slots(&mut self) -> GpuResult<()> {
        assert_eq!(self.host_slots.len(), 0, "host slots are already allocated");

        for idx in 0..MC::NUM_HOST_SLOTS {
            let host_slot = AsyncVec::allocate_new(MC::FULL_SLOT_SIZE);
            self.host_slots.push((host_slot, SlotStatus::Free));
        }
        Ok(())
    }

    pub fn new_empty_slot(&mut self, id: PolyId, form: PolyForm) {
        assert!(
            self.get_slot_idx(id, form).is_none(),
            "There already exists polynomial {:?} {:?}",
            id,
            form
        );
        let idx = self
            .free_slot_idx()
            .expect(&format!("No free slot for {:?} {:?}", id, form));
        self.slots[idx].1 = SlotStatus::Busy(id, form);
    }

    pub fn sync(&mut self) -> GpuResult<()> {
        for ctx in self.ctx.iter() {
            ctx.sync()?;
        }
        Ok(())
    }

    pub fn free_slot(&mut self, id: PolyId, form: PolyForm) {
        let idx = self
            .get_slot_idx(id, form)
            .expect(&format!("No such polynomial: {:?} {:?}", id, form));
        self.slots[idx].1 = SlotStatus::Free;
    }
    pub fn free_all_slots(&mut self) {
        for slot in self.slots.iter_mut() {
            slot.1 = SlotStatus::Free
        }
    }

    pub fn free_host_slot(&mut self, id: PolyId, form: PolyForm) {
        let idx = self
            .get_host_slot_idx(id, form)
            .expect(&format!("No such polynomial: {:?} {:?}", id, form));
        self.host_slots[idx].1 = SlotStatus::Free;
    }

    pub fn get_host_slot_values(&self, id: PolyId, form: PolyForm) -> GpuResult<&[F]> {
        let idx = self
            .get_host_slot_idx(id, form)
            .expect(&format!("No such polynomial: {:?} {:?}", id, form));
        self.host_slots[idx].0.get_values()
    }

    pub fn get_host_slot_values_mut(&mut self, id: PolyId, form: PolyForm) -> GpuResult<&mut [F]> {
        let idx = self
            .get_host_slot_idx(id, form)
            .expect(&format!("No such polynomial: {:?} {:?}", id, form));
        self.host_slots[idx].0.get_values_mut()
    }

    pub fn get_free_host_slot_values_mut(
        &mut self,
        id: PolyId,
        form: PolyForm,
    ) -> GpuResult<&mut [F]> {
        assert!(
            self.get_host_slot_idx(id, form).is_none(),
            "There is already such polynomial: {:?} {:?}",
            id,
            form
        );
        let idx = self
            .free_host_slot_idx()
            .expect(&format!("No free host slot for {:?} {:?}", id, form));

        self.host_slots[idx].1 = SlotStatus::Busy(id, form);
        self.host_slots[idx].0.get_values_mut()
    }

    pub fn rename_slot(&mut self, id: PolyId, new_id: PolyId, form: PolyForm) {
        assert!(
            self.get_slot_idx(new_id, form).is_none(),
            "There is already such polynomial: {:?} {:?}",
            id,
            form
        );
        let idx = self
            .get_slot_idx(id, form)
            .expect(&format!("No such polynomial: {:?} {:?}", id, form));

        self.slots[idx].1 = SlotStatus::Busy(new_id, form)
    }

    pub fn polynomials_on_device(&self) -> Vec<(PolyId, PolyForm)> {
        assert_eq!(
            self.slots.len(),
            MC::NUM_SLOTS,
            "slots are not allocated yet"
        );

        let mut res = vec![];
        for poly in self.slots.iter() {
            match poly.1 {
                SlotStatus::Busy(id, form) => res.push((id, form)),
                _ => {}
            }
        }
        res
    }

    pub fn number_of_free_slots(&self) -> usize {
        assert_eq!(
            self.slots.len(),
            MC::NUM_SLOTS,
            "slots are not allocated yet"
        );

        let mut res = 0;
        for poly in self.slots.iter() {
            if poly.1 == SlotStatus::Free {
                res += 1;
            }
        }
        res
    }

    pub fn free_slot_idx(&self) -> Option<usize> {
        assert_eq!(
            self.slots.len(),
            MC::NUM_SLOTS,
            "slots are not allocated yet"
        );

        for idx in 0..MC::NUM_SLOTS {
            if self.slots[idx].1 == SlotStatus::Free {
                return Some(idx);
            }
        }
        None
    }

    pub fn get_slot_idx(&self, id: PolyId, form: PolyForm) -> Option<usize> {
        assert_eq!(
            self.slots.len(),
            MC::NUM_SLOTS,
            "slots are not allocated yet"
        );

        for idx in 0..MC::NUM_SLOTS {
            if self.slots[idx].1 == SlotStatus::Busy(id, form) {
                return Some(idx);
            }
        }
        None
    }

    pub fn free_host_slot_idx(&self) -> Option<usize> {
        assert_eq!(
            self.host_slots.len(),
            MC::NUM_HOST_SLOTS,
            "host slots are not allocated yet"
        );

        for idx in 0..MC::NUM_HOST_SLOTS {
            if self.host_slots[idx].1 == SlotStatus::Free {
                return Some(idx);
            }
        }
        None
    }

    pub fn get_host_slot_idx(&self, id: PolyId, form: PolyForm) -> Option<usize> {
        assert_eq!(
            self.host_slots.len(),
            MC::NUM_HOST_SLOTS,
            "host slots are not allocated yet"
        );

        for idx in 0..MC::NUM_HOST_SLOTS {
            if self.host_slots[idx].1 == SlotStatus::Busy(id, form) {
                return Some(idx);
            }
        }
        None
    }

    pub fn get_free_big_slot_idx(&self, size: usize) -> Option<usize> {
        assert!(
            self.slots.len() == MC::NUM_SLOTS,
            "slots are not allocated yet"
        );
        assert!(
            size <= MC::NUM_SLOTS,
            "requested size on big slot is bigger than number of slots"
        );

        for slot_idx in (size..MC::NUM_SLOTS).rev() {
            let mut is_free = true;
            for j in 0..size {
                if self.slots[slot_idx - j].1 != SlotStatus::Free {
                    is_free = false;
                    break;
                }
            }

            if is_free {
                return Some(slot_idx - size + 1);
            }
        }

        None
    }

    fn get_ctx_id_by_device_id(&self, device_id: usize) -> usize {
        let available_devices = self.ctx.iter().map(|c| c.device_id()).join(", ");
        self.ctx
            .iter()
            .position(|c| c.device_id() == device_id)
            .expect(&format!(
                "unknown device id: {}, available: {}",
                device_id, available_devices
            ))
    }
}

impl<F: PrimeField, MC: ManagerConfigs> Drop for DeviceMemoryManager<F, MC> {
    fn drop(&mut self) {
        for ctx_id in 0..MC::NUM_GPUS {
            for slot in self.slots.iter_mut() {
                slot.0[ctx_id].read_event.sync().unwrap();
                slot.0[ctx_id].write_event.sync().unwrap();
            }

            if self.slots.len() > 0 {
                let ptr = self.slots[0].0[ctx_id].as_mut_ptr(0..0);
                if unsafe { bc_free(ptr as *mut c_void) } != 0 {
                    panic!("Can't free memory of DeviceBufs of DeviceMemoryManager");
                }
            }
        }
    }
}