@@ -9,10 +9,10 @@ extern unsigned long _heap_end;
99extern char * __brkval;
1010
1111#define SAMPLE_PERIOD_MICRO_S 100000
12- #define MAX_RAM_USAGE_SAMPLES 2000
1312
14- volatile uint ram_usage[MAX_RAM_USAGE_SAMPLES];
15- volatile uint samples = 0 ;
13+ volatile unsigned ram_usage[MAX_RAM_USAGE_SAMPLES];
14+ unsigned int ram_usage_by_layer[53 ] = {0 };
15+ volatile unsigned samples = 0 ;
1616
1717// / @brief Determine how much RAM is being used at the current instant (Currently only for HEAP)
1818void saveRAMUsage () {
@@ -22,8 +22,13 @@ void saveRAMUsage() {
2222 }
2323}
2424
25+ uint inference_time_layer_wise[53 ] = {0 };
26+ uint wait_layer_wise[53 ] = {0 };
27+ uint inference_time = 0 ;
28+
29+ uint wait_total = 0 ;
30+
2531IntervalTimer ramUsageTimer; // Interval timer to keep track of RAM usage
26- bool first_run = true ; // bool to keep track of whether this is the first run or not
2732#endif
2833
2934WriteTypes type = Stop; // Current write type
@@ -34,9 +39,6 @@ bool overflow_flag = false;
3439int rec_count = 0 ;
3540int ino_count = 0 ;
3641void setup () {
37- #ifdef PROFILING
38- ramUsageTimer.begin (saveRAMUsage,SAMPLE_PERIOD_MICRO_S);// Save RAM usage at 1 ms intervals
39- #endif
4042 setup_filesys ();
4143 {
4244 setup_communication ();
@@ -48,9 +50,15 @@ void setup() {
4850 read_line_by_line (LINES_FILENAME, lines);
4951 }
5052 #ifdef PROFILING
51- int inference_start = millis ();
53+ uint inference_start = millis ();
54+ ramUsageTimer.begin (saveRAMUsage,SAMPLE_PERIOD_MICRO_S);// Save RAM usage at 1 ms intervals
5255 #endif
5356 for (int j = 0 ; j < 53 ; j++) {
57+ #ifdef PROFILING
58+ uint layer_start = millis ();
59+ uint wait_layer = 0 ;
60+ uint wait_phase_begin = 0 ;
61+ #endif
5462 Serial.print (" Current layer: " );
5563 Serial.println (j);
5664 if (j < 52 ){
@@ -64,9 +72,15 @@ void setup() {
6472 Serial.println (" input is nullptr!" );
6573 }
6674 }
75+ #ifdef PROFILING
76+ wait_phase_begin = millis ();
77+ #endif
6778 while (rec_count != input_length[j]){
6879 check_and_receive (rec_count,input_distribution);
6980 }
81+ #ifdef PROFILING
82+ wait_layer += millis () - wait_phase_begin;
83+ #endif
7084 Serial.println (" finished..." );
7185 rec_count = 0 ;
7286 }
@@ -92,6 +106,9 @@ void setup() {
92106 }
93107 distributed_computation (first_line, input_distribution, result, overflow, input_length[j]);
94108 handle_residual (result,result_length[j],j,residual_connection,zps,scales);
109+ #ifdef PROFILING
110+ ram_usage_by_layer[j] = 524288 - ((char *)&_heap_end - __brkval);
111+ #endif
95112 if (input_distribution != nullptr ) delete[] input_distribution;
96113 }
97114 if (overflow_flag) {
@@ -100,7 +117,13 @@ void setup() {
100117 }
101118 input_distribution = new byte[input_length[j + 1 ]];
102119 Serial.println (" waiting for permission..." );
120+ #ifdef PROFILING
121+ wait_phase_begin = millis ();
122+ #endif
103123 wait_for_permission (rec_count,input_distribution);
124+ #ifdef PROFILING
125+ wait_layer += millis () - wait_phase_begin;
126+ #endif
104127 Serial.println (" premission granted, sending results..." );
105128 if (j < 51 ) {
106129 char to_send[MESSAGE_SIZE];
@@ -155,7 +178,13 @@ void setup() {
155178 }
156179 // check regularly to avoid clogging
157180 if (rec_count < input_length[j + 1 ]) {
181+ #ifdef PROFILING
182+ wait_phase_begin = millis ();
183+ #endif
158184 check_and_receive ( rec_count, input_distribution);
185+ #ifdef PROFILING
186+ wait_layer += millis () - wait_phase_begin;
187+ #endif
159188 }
160189 }
161190 // send the rest of the data
@@ -168,6 +197,12 @@ void setup() {
168197 if (overflow_flag) dataFile.close ();
169198 to_send[1 ] = Complete; // signal the end
170199 send_message_to_coordinator (to_send);
200+ #ifdef PROFILING
201+ unsigned int temp_usage = 524288 - ((char *)&_heap_end - __brkval);
202+ if (temp_usage > ram_usage_by_layer[j]) {
203+ ram_usage_by_layer[j] = temp_usage;
204+ }
205+ #endif
171206 }
172207 else if (j == 51 ){
173208 char to_send[MESSAGE_SIZE];
@@ -190,6 +225,9 @@ void setup() {
190225 }
191226 to_send[1 ] = Complete;
192227 send_message_to_coordinator (to_send);
228+ #ifdef PROFILING
229+ ram_usage_by_layer[j] = 524288 - ((char *)&_heap_end - __brkval);
230+ #endif
193231 }
194232 // /////////////////////////
195233 }
@@ -202,7 +240,13 @@ void setup() {
202240 Serial.println (rec_count);
203241 Serial.println (" not enough inputs, receiving..." );
204242 while (rec_count != input_length[j]){
243+ #ifdef PROFILING
244+ wait_phase_begin = millis ();
245+ #endif
205246 check_and_receive (rec_count,input_distribution);
247+ #ifdef PROFILING
248+ wait_layer += millis () - wait_phase_begin;
249+ #endif
206250 }
207251 Serial.println (" finished..." );
208252 rec_count = 0 ;
@@ -230,25 +274,22 @@ void setup() {
230274 results[1 ] = Inference_Results;
231275 write_length (results, res_count);
232276 client.write (results, MESSAGE_SIZE);
277+ #ifdef PROFILING
278+ ram_usage_by_layer[j] = 524288 - ((char *)&_heap_end - __brkval);
279+ #endif
233280 }
281+ #ifdef PROFILING
282+ inference_time_layer_wise[j] = (millis () - layer_start);
283+ wait_layer_wise[j] = wait_layer;
284+ wait_total += wait_layer;
285+ #endif
234286 }
235287 #ifdef PROFILING
236- Serial.print (" Inference took " );
237- Serial.print (((float ) (millis () - inference_start)) / 1000.0 );
238- Serial.println (" s" );
239- ramUsageTimer.end ();
288+ inference_time = millis () - inference_start;
240289 #endif
241290}
242291void loop () {
243292 if (Serial.available ()) {
244- #ifdef PROFILING
245- if (first_run) {
246- for (uint i = 0 ; i < MAX_RAM_USAGE_SAMPLES; i++) {
247- Serial.print (ram_usage[i]);
248- Serial.print (" , " );
249- }
250- }
251- #endif
252293 menu_handler ();
253294 }
254295 // sendUDPMessage("1 to 2", ip2, localPort);
0 commit comments