|
| 1 | +// Copyright Supranational LLC |
| 2 | +// Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +#[cfg(test)] |
| 6 | +mod tests; |
| 7 | + |
| 8 | +use rust_kzg_blst::types::fr::FsFr; |
| 9 | +use std::ops::DerefMut; |
| 10 | +use subspace_core_primitives::crypto::Scalar; |
| 11 | +use subspace_core_primitives::{PosProof, PosSeed, Record}; |
| 12 | + |
| 13 | +extern "C" { |
| 14 | + /// # Returns |
| 15 | + /// * `usize` - The number of available GPUs. |
| 16 | + fn gpu_count() -> usize; |
| 17 | + |
| 18 | + /// # Parameters |
| 19 | + /// * `k: The size parameter for the table. |
| 20 | + /// * `seed: A pointer to the seed data. |
| 21 | + /// * `lg_record_size: The logarithm of the record size. |
| 22 | + /// * `challenge_index: A mutable pointer to store the index of the challenge. |
| 23 | + /// * `record: A pointer to the record data. |
| 24 | + /// * `chunks_scratch: A mutable pointer to a scratch space for chunk data. |
| 25 | + /// * `proof_count: A mutable pointer to store the count of proofs. |
| 26 | + /// * `source_record_chunks: A mutable pointer to the source record chunks. |
| 27 | + /// * `parity_record_chunks: A mutable pointer to the parity record chunks. |
| 28 | + /// * `gpu_id: The ID of the GPU to use. |
| 29 | + /// |
| 30 | + /// # Returns |
| 31 | + /// * `sppark::Error` - An error code indicating the result of the operation. |
| 32 | + /// |
| 33 | + /// # Assumptions |
| 34 | + /// * `seed` must be a valid pointer to a 32-byte. |
| 35 | + /// * `record` must be a valid pointer to the record data (`*const Record`), with a length of `1 << lg_record_size`. |
| 36 | + /// * `source_record_chunks` and `parity_record_chunks` must be valid mutable pointers to `Scalar` elements, each with a length of `1 << lg_record_size`. |
| 37 | + /// * `chunks_scratch` must be a valid mutable pointer where up to `challenges_count` 32-byte chunks of GPU-calculated data will be written. |
| 38 | + /// * `gpu_id` must be a valid identifier of an available GPU. The available GPUs can be determined by using the `gpu_count` function. |
| 39 | + fn generate_and_encode_pospace_dispatch( |
| 40 | + k: u32, |
| 41 | + seed: *const [u8; 32], |
| 42 | + lg_record_size: u32, |
| 43 | + challenge_index: *mut u32, |
| 44 | + record: *const [u8; 32], |
| 45 | + chunks_scratch: *mut [u8; 32], |
| 46 | + proof_count: *mut u32, |
| 47 | + parity_record_chunks: *mut FsFr, |
| 48 | + gpu_id: i32, |
| 49 | + ) -> sppark::Error; |
| 50 | +} |
| 51 | + |
| 52 | +/// Returns [`RocmDevice`] for each available device |
| 53 | +pub fn rocm_devices() -> Vec<RocmDevice> { |
| 54 | + let num_devices = unsafe { gpu_count() }; |
| 55 | + |
| 56 | + (0i32..) |
| 57 | + .take(num_devices) |
| 58 | + .map(|gpu_id| RocmDevice { gpu_id }) |
| 59 | + .collect() |
| 60 | +} |
| 61 | + |
| 62 | +/// Wrapper data structure encapsulating a single ROCm-capable device |
| 63 | +#[derive(Debug)] |
| 64 | +pub struct RocmDevice { |
| 65 | + gpu_id: i32, |
| 66 | +} |
| 67 | + |
| 68 | +impl RocmDevice { |
| 69 | + /// ROCm device ID |
| 70 | + pub fn id(&self) -> i32 { |
| 71 | + self.gpu_id |
| 72 | + } |
| 73 | + |
| 74 | + /// Generates and encodes PoSpace on the GPU. |
| 75 | + /// |
| 76 | + /// This function performs the generation and encoding of PoSpace |
| 77 | + /// on a GPU. It uses the specified parameters to perform the computations and |
| 78 | + /// ensures that errors are properly handled by returning a `Result` type. |
| 79 | + /// |
| 80 | + /// # Parameters |
| 81 | + /// |
| 82 | + /// ## Input |
| 83 | + /// |
| 84 | + /// - `k`: The size parameter for the table. |
| 85 | + /// - `seed`: A 32-byte seed used for the table generation process. |
| 86 | + /// - `record`: A slice of bytes (`&[u8]`). These records are the data on which the proof of space will be generated. |
| 87 | + /// - `gpu_id`: ID of the GPU to use. This parameter specifies which GPU to use for the computation. |
| 88 | + /// |
| 89 | + /// ## Output |
| 90 | + /// |
| 91 | + /// - `source_record_chunks`: A mutable vector of original data chunks of type FsFr, each 32 bytes in size. |
| 92 | + /// - `parity_record_chunks`: A mutable vector of parity chunks derived from the source, each 32 bytes in size. |
| 93 | + /// - `proof_count`: A mutable reference to the proof count. This value will be updated with the number of proofs generated. |
| 94 | + /// - `chunks_scratch`: A mutable vector used to store the processed chunks. This vector holds the final results after combining record chunks and proof hashes. |
| 95 | + /// - `challenge_index`: A mutable vector used to map the challenges to specific parts of the data. |
| 96 | + pub fn generate_and_encode_pospace( |
| 97 | + &self, |
| 98 | + seed: &PosSeed, |
| 99 | + record: &mut Record, |
| 100 | + encoded_chunks_used_output: impl ExactSizeIterator<Item = impl DerefMut<Target = bool>>, |
| 101 | + ) -> Result<(), String> { |
| 102 | + let record_len = Record::NUM_CHUNKS; |
| 103 | + let challenge_len = Record::NUM_S_BUCKETS; |
| 104 | + let lg_record_size = record_len.ilog2(); |
| 105 | + |
| 106 | + if challenge_len > u32::MAX as usize { |
| 107 | + return Err(String::from("challenge_len is too large to fit in u32")); |
| 108 | + } |
| 109 | + |
| 110 | + let mut proof_count = 0u32; |
| 111 | + let mut chunks_scratch_gpu = Vec::<[u8; Scalar::FULL_BYTES]>::with_capacity(challenge_len); |
| 112 | + let mut challenge_index_gpu = Vec::<u32>::with_capacity(challenge_len); |
| 113 | + let mut parity_record_chunks = Vec::<Scalar>::with_capacity(Record::NUM_CHUNKS); |
| 114 | + |
| 115 | + let error = unsafe { |
| 116 | + generate_and_encode_pospace_dispatch( |
| 117 | + u32::from(PosProof::K), |
| 118 | + &**seed, |
| 119 | + lg_record_size, |
| 120 | + challenge_index_gpu.as_mut_ptr(), |
| 121 | + record.as_ptr(), |
| 122 | + chunks_scratch_gpu.as_mut_ptr(), |
| 123 | + &mut proof_count, |
| 124 | + Scalar::slice_mut_to_repr(&mut parity_record_chunks).as_mut_ptr(), |
| 125 | + self.gpu_id, |
| 126 | + ) |
| 127 | + }; |
| 128 | + |
| 129 | + if error.code != 0 { |
| 130 | + return Err(error.to_string()); |
| 131 | + } |
| 132 | + |
| 133 | + let proof_count = proof_count as usize; |
| 134 | + unsafe { |
| 135 | + chunks_scratch_gpu.set_len(proof_count); |
| 136 | + challenge_index_gpu.set_len(proof_count); |
| 137 | + parity_record_chunks.set_len(Record::NUM_CHUNKS); |
| 138 | + } |
| 139 | + |
| 140 | + let mut encoded_chunks_used = vec![false; challenge_len]; |
| 141 | + let source_record_chunks = record.to_vec(); |
| 142 | + |
| 143 | + let mut chunks_scratch = challenge_index_gpu |
| 144 | + .into_iter() |
| 145 | + .zip(chunks_scratch_gpu) |
| 146 | + .collect::<Vec<_>>(); |
| 147 | + |
| 148 | + chunks_scratch |
| 149 | + .sort_unstable_by(|(a_out_index, _), (b_out_index, _)| a_out_index.cmp(b_out_index)); |
| 150 | + |
| 151 | + // We don't need all the proofs |
| 152 | + chunks_scratch.truncate(proof_count.min(Record::NUM_CHUNKS)); |
| 153 | + |
| 154 | + for (out_index, _chunk) in &chunks_scratch { |
| 155 | + encoded_chunks_used[*out_index as usize] = true; |
| 156 | + } |
| 157 | + |
| 158 | + encoded_chunks_used_output |
| 159 | + .zip(&encoded_chunks_used) |
| 160 | + .for_each(|(mut output, input)| *output = *input); |
| 161 | + |
| 162 | + record |
| 163 | + .iter_mut() |
| 164 | + .zip( |
| 165 | + chunks_scratch |
| 166 | + .into_iter() |
| 167 | + .map(|(_out_index, chunk)| chunk) |
| 168 | + .chain( |
| 169 | + source_record_chunks |
| 170 | + .into_iter() |
| 171 | + .zip(parity_record_chunks) |
| 172 | + .flat_map(|(a, b)| [a, b.to_bytes()]) |
| 173 | + .zip(encoded_chunks_used.iter()) |
| 174 | + // Skip chunks that were used previously |
| 175 | + .filter_map(|(record_chunk, encoded_chunk_used)| { |
| 176 | + if *encoded_chunk_used { |
| 177 | + None |
| 178 | + } else { |
| 179 | + Some(record_chunk) |
| 180 | + } |
| 181 | + }), |
| 182 | + ), |
| 183 | + ) |
| 184 | + .for_each(|(output_chunk, input_chunk)| { |
| 185 | + *output_chunk = input_chunk; |
| 186 | + }); |
| 187 | + |
| 188 | + Ok(()) |
| 189 | + } |
| 190 | +} |
0 commit comments