@@ -7,7 +7,6 @@ use gkr_engine::{
77} ;
88use polynomials:: RefMultiLinearPoly ;
99use serdes:: ExpSerde ;
10- use std:: time:: Instant ;
1110use sumcheck:: ProverScratchPad ;
1211
1312use crate :: {
@@ -18,28 +17,6 @@ use crate::{
1817 } ,
1918} ;
2019
21- /// 获取当前进程的内存使用情况 (RSS, 单位: KB)
22- fn get_memory_usage_kb ( ) -> Option < u64 > {
23- #[ cfg( target_os = "linux" ) ]
24- {
25- if let Ok ( content) = std:: fs:: read_to_string ( "/proc/self/statm" ) {
26- let parts: Vec < & str > = content. split_whitespace ( ) . collect ( ) ;
27- if parts. len ( ) >= 2 {
28- // statm 第二个字段是 RSS (以页为单位)
29- // Linux 页大小通常是 4KB
30- if let Ok ( rss_pages) = parts[ 1 ] . parse :: < u64 > ( ) {
31- return Some ( rss_pages * 4 ) ; // 转换为 KB
32- }
33- }
34- }
35- None
36- }
37- #[ cfg( not( target_os = "linux" ) ) ]
38- {
39- None
40- }
41- }
42-
4320/// ECCCircuit -> ExpanderCircuit
4421/// Returns an additional prover scratch pad for later use in GKR.
4522pub fn prepare_expander_circuit < F , ECCConfig > (
@@ -51,156 +28,12 @@ where
5128 ECCConfig : Config ,
5229 ECCConfig :: FieldConfig : FieldEngine < CircuitField = F :: CircuitField > ,
5330{
54- // 记录开始时间
55- let start_time = Instant :: now ( ) ;
56- eprintln ! ( "[prepare_expander_circuit] ============== Start ==============" ) ;
57-
58- // 记录开始时的内存
59- let mem_before = get_memory_usage_kb ( ) ;
60- eprintln ! (
61- "[prepare_expander_circuit] Memory before: {:?} KB" ,
62- mem_before
63- ) ;
64-
65- // Step 1: export_to_expander().flatten()
66- let step1_start = Instant :: now ( ) ;
6731 let mut expander_circuit = kernel. layered_circuit ( ) . export_to_expander ( ) . flatten ( ) ;
68- let step1_duration = step1_start. elapsed ( ) ;
69- eprintln ! (
70- "[prepare_expander_circuit] Step 1 (export_to_expander + flatten) took: {:.3}s" ,
71- step1_duration. as_secs_f64( )
72- ) ;
73-
74- // Step 2: 打印电路大小信息
75- let step2_start = Instant :: now ( ) ;
76- let num_layers = expander_circuit. layers . len ( ) ;
77- let mut total_gates = 0usize ;
78- let mut total_add_gates = 0usize ;
79- let mut total_mul_gates = 0usize ;
80- let mut total_const_gates = 0usize ;
81-
82- for layer in expander_circuit. layers . iter ( ) {
83- total_add_gates += layer. add . len ( ) ;
84- total_mul_gates += layer. mul . len ( ) ;
85- total_const_gates += layer. const_ . len ( ) ;
86- total_gates += layer. add . len ( ) + layer. mul . len ( ) + layer. const_ . len ( ) ;
87- }
88-
89- eprintln ! ( "[prepare_expander_circuit] Circuit stats:" ) ;
90- eprintln ! ( " - num_layers: {}" , num_layers) ;
91- eprintln ! ( " - total_gates: {}" , total_gates) ;
92- eprintln ! ( " - total_add_gates: {}" , total_add_gates) ;
93- eprintln ! ( " - total_mul_gates: {}" , total_mul_gates) ;
94- eprintln ! ( " - total_const_gates: {}" , total_const_gates) ;
95- eprintln ! ( " - log_input_size: {}" , expander_circuit. log_input_size( ) ) ;
96- let step2_duration = step2_start. elapsed ( ) ;
97- eprintln ! (
98- "[prepare_expander_circuit] Step 2 (circuit stats calculation) took: {:.3}s" ,
99- step2_duration. as_secs_f64( )
100- ) ;
101-
102- // 记录 export_to_expander().flatten() 后的内存
103- let mem_after_flatten = get_memory_usage_kb ( ) ;
104- eprintln ! (
105- "[prepare_expander_circuit] Memory after flatten: {:?} KB" ,
106- mem_after_flatten
107- ) ;
108- if let ( Some ( before) , Some ( after) ) = ( mem_before, mem_after_flatten) {
109- eprintln ! (
110- "[prepare_expander_circuit] Memory delta (flatten): {} KB ({:.2} MB)" ,
111- after as i64 - before as i64 ,
112- ( after as i64 - before as i64 ) as f64 / 1024.0
113- ) ;
114- }
115-
116- // Step 3: pre_process_gkr
117- let step3_start = Instant :: now ( ) ;
11832 expander_circuit. pre_process_gkr ( ) ;
119- let step3_duration = step3_start. elapsed ( ) ;
120- eprintln ! (
121- "[prepare_expander_circuit] Step 3 (pre_process_gkr) took: {:.3}s" ,
122- step3_duration. as_secs_f64( )
123- ) ;
124-
33+
12534 let ( max_num_input_var, max_num_output_var) = super :: utils:: max_n_vars ( & expander_circuit) ;
126- eprintln ! ( " - max_num_input_var: {}" , max_num_input_var) ;
127- eprintln ! ( " - max_num_output_var: {}" , max_num_output_var) ;
128-
129- // 记录 pre_process_gkr 后的内存
130- let mem_after_preprocess = get_memory_usage_kb ( ) ;
131- eprintln ! (
132- "[prepare_expander_circuit] Memory after pre_process_gkr: {:?} KB" ,
133- mem_after_preprocess
134- ) ;
135- if let ( Some ( before) , Some ( after) ) = ( mem_after_flatten, mem_after_preprocess) {
136- eprintln ! (
137- "[prepare_expander_circuit] Memory delta (pre_process_gkr): {} KB ({:.2} MB)" ,
138- after as i64 - before as i64 ,
139- ( after as i64 - before as i64 ) as f64 / 1024.0
140- ) ;
141- }
142-
143- // Step 4: create ProverScratchPad
144- let step4_start = Instant :: now ( ) ;
14535 let prover_scratch =
14636 ProverScratchPad :: < F > :: new ( max_num_input_var, max_num_output_var, mpi_world_size) ;
147- let step4_duration = step4_start. elapsed ( ) ;
148- eprintln ! (
149- "[prepare_expander_circuit] Step 4 (create ProverScratchPad) took: {:.3}s" ,
150- step4_duration. as_secs_f64( )
151- ) ;
152-
153- // 记录分配 ProverScratchPad 后的内存
154- let mem_after_scratch = get_memory_usage_kb ( ) ;
155- eprintln ! (
156- "[prepare_expander_circuit] Memory after ProverScratchPad: {:?} KB" ,
157- mem_after_scratch
158- ) ;
159- if let ( Some ( before) , Some ( after) ) = ( mem_after_preprocess, mem_after_scratch) {
160- eprintln ! (
161- "[prepare_expander_circuit] Memory delta (ProverScratchPad): {} KB ({:.2} MB)" ,
162- after as i64 - before as i64 ,
163- ( after as i64 - before as i64 ) as f64 / 1024.0
164- ) ;
165- }
166-
167- // 总内存增量
168- if let ( Some ( before) , Some ( after) ) = ( mem_before, mem_after_scratch) {
169- eprintln ! (
170- "[prepare_expander_circuit] Total memory delta: {} KB ({:.2} MB)" ,
171- after as i64 - before as i64 ,
172- ( after as i64 - before as i64 ) as f64 / 1024.0
173- ) ;
174- }
175-
176- // 总时间统计
177- let total_duration = start_time. elapsed ( ) ;
178- eprintln ! ( "[prepare_expander_circuit] ============== Summary ==============" ) ;
179- eprintln ! (
180- "[prepare_expander_circuit] Step 1 (export + flatten): {:.3}s ({:.1}%)" ,
181- step1_duration. as_secs_f64( ) ,
182- step1_duration. as_secs_f64( ) / total_duration. as_secs_f64( ) * 100.0
183- ) ;
184- eprintln ! (
185- "[prepare_expander_circuit] Step 2 (circuit stats): {:.3}s ({:.1}%)" ,
186- step2_duration. as_secs_f64( ) ,
187- step2_duration. as_secs_f64( ) / total_duration. as_secs_f64( ) * 100.0
188- ) ;
189- eprintln ! (
190- "[prepare_expander_circuit] Step 3 (pre_process_gkr): {:.3}s ({:.1}%)" ,
191- step3_duration. as_secs_f64( ) ,
192- step3_duration. as_secs_f64( ) / total_duration. as_secs_f64( ) * 100.0
193- ) ;
194- eprintln ! (
195- "[prepare_expander_circuit] Step 4 (scratch pad): {:.3}s ({:.1}%)" ,
196- step4_duration. as_secs_f64( ) ,
197- step4_duration. as_secs_f64( ) / total_duration. as_secs_f64( ) * 100.0
198- ) ;
199- eprintln ! (
200- "[prepare_expander_circuit] Total time: {:.3}s" ,
201- total_duration. as_secs_f64( )
202- ) ;
203- eprintln ! ( "[prepare_expander_circuit] ====================================" ) ;
20437
20538 ( expander_circuit, prover_scratch)
20639}
0 commit comments