-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathclient_utils.rs
More file actions
173 lines (146 loc) · 4.86 KB
/
client_utils.rs
File metadata and controls
173 lines (146 loc) · 4.86 KB
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
use std::fs;
use crate::{
frontend::{Config, SIMDField},
utils::misc::{next_power_of_two, prev_power_of_two},
zkcuda::{
context::ComputationGraph,
proving_system::{
expander::structs::{ExpanderProverSetup, ExpanderVerifierSetup},
expander_parallelized::{
cmd_utils::start_server, server_ctrl::parse_port_number,
shared_memory_utils::SharedMemoryEngine,
},
CombinedProof, Expander,
},
},
};
use super::server_ctrl::{RequestType, SERVER_IP, SERVER_PORT};
use expander_utils::timer::Timer;
use gkr_engine::GKREngine;
use reqwest::Client;
use serdes::ExpSerde;
pub struct ClientHttpHelper;
impl ClientHttpHelper {
pub async fn request_setup(setup_file: &str) {
Self::post_request(RequestType::Setup(setup_file.to_string())).await;
}
pub async fn request_prove() {
Self::post_request(RequestType::Prove).await;
}
pub async fn request_exit() {
Self::post_request(RequestType::Exit).await;
}
pub async fn post_request(request: RequestType) {
let client = Client::new();
let port = {
let port = SERVER_PORT.lock().unwrap();
*port
};
let server_url = format!("{SERVER_IP}:{port}");
let server_url = format!("http://{server_url}/");
let res = client
.post(server_url)
.json(&request)
.send()
.await
.expect("Failed to send request");
if res.status().is_success() {
println!("Request successful");
} else {
eprintln!("Request failed: {}", res.status());
}
}
}
pub fn client_parse_args() -> Option<String> {
let args = std::env::args().collect::<Vec<_>>();
let mut string = None;
for (i, arg) in args.iter().take(args.len() - 1).enumerate() {
if arg == "--server-binary" || arg == "-s" {
string = Some(args[i + 1].clone());
break;
}
}
string
}
pub fn client_launch_server_and_setup<C, ECCConfig>(
server_binary: &str,
computation_graph: &ComputationGraph<ECCConfig>,
allow_oversubscribe: bool,
batch_pcs: bool,
) -> (
ExpanderProverSetup<C::FieldConfig, C::PCSConfig>,
ExpanderVerifierSetup<C::FieldConfig, C::PCSConfig>,
)
where
C: GKREngine,
ECCConfig: Config<FieldConfig = C::FieldConfig>,
{
let setup_timer = Timer::new("setup", true);
println!("Starting server with binary: {server_binary}");
let mut bytes = vec![];
computation_graph.serialize_into(&mut bytes).unwrap();
println!("Serialized computation graph, size: {}", bytes.len());
// append current timestamp to the file name to avoid conflicts
let setup_filename = format!(
"/tmp/computation_graph_{}.bin",
chrono::Utc::now().timestamp_millis()
);
fs::write(&setup_filename, bytes).expect("Failed to write computation graph to file");
let max_parallel_count = computation_graph
.proof_templates()
.iter()
.map(|t| t.parallel_count())
.max()
.unwrap_or(1);
let max_parallel_count = next_power_of_two(max_parallel_count);
let mpi_size = if allow_oversubscribe {
max_parallel_count
} else {
let num_cpus = std::env::var("ZKML_NUM_CPUS")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or_else(num_cpus::get_physical);
let num_cpus = prev_power_of_two(num_cpus);
if max_parallel_count > num_cpus {
num_cpus
} else {
max_parallel_count
}
};
let port = parse_port_number();
let server_url = format!("{SERVER_IP}:{port}");
start_server::<C>(server_binary, mpi_size, port, batch_pcs);
// Keep trying until the server is ready
loop {
match wait_async(Client::new().get(format!("http://{server_url}/")).send()) {
Ok(_) => break,
Err(_) => std::thread::sleep(std::time::Duration::from_secs(1)),
}
}
wait_async(ClientHttpHelper::request_setup(&setup_filename));
setup_timer.stop();
SharedMemoryEngine::read_pcs_setup_from_shared_memory()
}
pub fn client_send_witness_and_prove<C, ECCConfig>(
device_memories: Vec<Vec<SIMDField<ECCConfig>>>,
) -> CombinedProof<ECCConfig, Expander<C>>
where
C: GKREngine,
ECCConfig: Config<FieldConfig = C::FieldConfig>,
{
let timer = Timer::new("prove", true);
SharedMemoryEngine::write_witness_to_shared_memory::<C::FieldConfig>(device_memories);
wait_async(ClientHttpHelper::request_prove());
let proof = SharedMemoryEngine::read_proof_from_shared_memory();
timer.stop();
proof
}
/// Run an async function in a blocking context.
#[inline(always)]
pub fn wait_async<F, T>(f: F) -> T
where
F: std::future::Future<Output = T>,
{
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(f)
}