1
1
#include < scamp5.hpp>
2
2
#include < vector>
3
-
4
3
#include " MISC/MISC_FUNCS.hpp"
5
4
using namespace SCAMP5_PE ;
6
5
7
6
vs_stopwatch frame_timer;
8
7
vs_stopwatch output_timer;
9
8
vs_stopwatch sum_timer;
10
9
11
- const int record_length = 100 ;
12
-
13
-
14
- std::pair<int , int > computeMeanAndVariance (const std::vector<uint16_t >& data)
15
- {
16
- int sum = 0 ;
17
- for (uint8_t value : data)
18
- {
19
- sum += value;
20
- }
21
- int mean = sum / data.size ();
10
+ // Variables used for calculating statistics for the difference global summation functions
11
+ int sum64_max, sum64_min;
12
+ int sum16_max, sum16_min;
13
+ int sum_fast_max, sum_fast_min;
22
14
23
- int sqDiffSum = 0 ;
24
- for (uint8_t value : data)
25
- {
26
- int diff = value - mean;
27
- sqDiffSum += diff * diff;
28
- }
15
+ const int record_length = 100 ;
16
+ int record_cntr = 0 ;
17
+ std::vector<uint16_t > sum64_record (record_length);
18
+ std::vector<uint16_t > sum16_record (record_length);
19
+ std::vector<uint16_t > sum_fast_record (record_length);
29
20
30
- int variance = sqDiffSum / (data.size () - 1 );
21
+ // Function used to compute moving average & variance of the output from each of the global summation functions
22
+ std::pair<int , int > calculate_mean_and_variance_of_data_array (const std::vector<uint16_t >& data);
31
23
32
- return {mean, variance};
33
- }
24
+ void compute_maximum_and_minimum_of_global_sum_functions ();
34
25
35
26
int main ()
36
27
{
@@ -46,8 +37,12 @@ int main()
46
37
int display_size = 2 ;
47
38
auto display_00 = vs_gui_add_display (" Image to Sum" ,0 ,0 ,display_size);
48
39
40
+ // Add display to plot the results from each global summation function
49
41
auto display_plot = vs_gui_add_display (" Red:Sum64, Yellow:Sum16, Green:Sum_fast" ,0 ,display_size,display_size,style_plot);
50
- vs_gui_set_scope (display_plot,0 ,100 ,255 );
42
+ const int plot_min = 0 ;
43
+ const int plot_max = 100 ;
44
+ const int plot_time_frame = 256 ;
45
+ vs_gui_set_scope (display_plot,plot_min,plot_max,plot_time_frame);
51
46
52
47
int use_test_input = 0 ;
53
48
vs_gui_add_switch (" use test input" , use_test_input == 1 , &use_test_input);
@@ -58,37 +53,8 @@ int main()
58
53
int input_value = 0 ;
59
54
vs_gui_add_slider (" input_value" ,-127 ,127 ,input_value,&input_value);
60
55
61
- int record_cntr = 0 ;
62
- std::vector<uint16_t > sum64_record (record_length);
63
- std::vector<uint16_t > sum16_record (record_length);
64
- std::vector<uint16_t > sum_fast_record (record_length);
65
-
66
- int sum64_max, sum64_min;
67
- int sum16_max, sum16_min;
68
- int sum_fast_max, sum_fast_min;
69
-
70
- // Compute the maximum & minimum values for each type of summation
71
- {
72
- scamp5_in (F,-127 );
73
- scamp5_kernel_begin ();
74
- mov (A,F);
75
- add (A,A,F);
76
- add (A,A,F);
77
- scamp5_kernel_end ();
78
- sum64_min = scamp5_global_sum_64 (A) + 1 ;// +1s to avoid possibility of divide by 0 later
79
- sum16_min = scamp5_global_sum_16 (A) + 1 ;
80
- sum_fast_min = scamp5_global_sum_fast (A) + 1 ;
81
-
82
- scamp5_in (F,127 );
83
- scamp5_kernel_begin ();
84
- mov (A,F);
85
- add (A,A,F);
86
- add (A,A,F);
87
- scamp5_kernel_end ();
88
- sum64_max = scamp5_global_sum_64 (A);
89
- sum16_max = scamp5_global_sum_16 (A);
90
- sum_fast_max = scamp5_global_sum_fast (A);
91
- }
56
+ // Compute the range of possible results from each summation function
57
+ compute_maximum_and_minimum_of_global_sum_functions ();
92
58
93
59
// Frame Loop
94
60
while (1 )
@@ -99,7 +65,7 @@ int main()
99
65
vs_frame_loop_control ();
100
66
101
67
// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
102
- // Capture / Create the analogue image to test globabl summation upon
68
+ // CAPTURE OR CREATE THE ANALOGUE DATA TO TEST GLOBAL SUMMATION UPON
103
69
104
70
if (!use_test_input)
105
71
{
@@ -121,7 +87,9 @@ int main()
121
87
// Create an image with a certain proportion of pixels the maximum value (and other pixels the minimum value)
122
88
scamp5_in (E,127 );
123
89
scamp5_in (F,-127 );
124
- scamp5_load_rect (S6,0 ,0 ,255 ,input_value+127 );
90
+ scamp5_load_rect (S6,0 ,0 ,255 ,input_value+127 );// Load a rectangle occupying of proportion of the register plane / image
91
+
92
+ // In PEs outside the rectangle place -127, PEs inside the rectangle place +127
125
93
scamp5_kernel_begin ();
126
94
mov (A,F);
127
95
WHERE (S6);
@@ -132,7 +100,7 @@ int main()
132
100
}
133
101
134
102
// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
135
- // Perform global summation using each method
103
+ // PERFORM GLOBAL SUMMATION USING EACH FUNCTION
136
104
137
105
sum_timer.reset ();
138
106
int16_t sum64_result = scamp5_global_sum_64 (A);
@@ -146,7 +114,10 @@ int main()
146
114
int16_t sum_fast_result = scamp5_global_sum_fast (A);
147
115
int sum_fast_time = sum_timer.get_usec ();
148
116
149
- // Calculate normalised results
117
+ // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
118
+ // COMPUTE AND RECORD THE NORMALISED RESULTS FOP EACH FUNCTION
119
+
120
+ // Calculate normalised results, places each value in the range 0-100
150
121
int sum64_result_normalised = (100 *sum64_result)/(sum64_max - sum64_min);
151
122
int sum16_result_normalised = (100 *sum16_result)/(sum16_max - sum16_min);
152
123
int sum_fast_result_normalised = (100 *sum_fast_result)/(sum_fast_max - sum_fast_min);
@@ -155,42 +126,93 @@ int main()
155
126
sum64_record[record_cntr] = sum64_result_normalised;
156
127
sum16_record[record_cntr] = sum16_result_normalised;
157
128
sum_fast_record[record_cntr] = sum_fast_result_normalised;
158
- record_cntr = (record_cntr+1 )%record_length;
129
+ record_cntr = (record_cntr+1 )%record_length;// Update the cntr to the next location in the array
159
130
160
- auto [sum64_mean, sum64_var] = computeMeanAndVariance (sum64_record);
161
- auto [sum16_mean, sum16_var] = computeMeanAndVariance (sum16_record);
162
- auto [sum_fast_mean, sum_fast_var] = computeMeanAndVariance (sum_fast_record);
131
+ auto [sum64_mean, sum64_var] = calculate_mean_and_variance_of_data_array (sum64_record);
132
+ auto [sum16_mean, sum16_var] = calculate_mean_and_variance_of_data_array (sum16_record);
133
+ auto [sum_fast_mean, sum_fast_var] = calculate_mean_and_variance_of_data_array (sum_fast_record);
134
+
135
+ // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
136
+ // OUTPUT
163
137
164
138
vs_post_text (" sum64 - val %d, normalised val %d, mean %d, variance %d, time %d \n " ,sum64_result, sum64_result_normalised,sum64_mean,sum64_var, sum64_time);
165
139
vs_post_text (" sum16 - val %d, normalised val %d , mean %d, variance %d, time %d \n " ,sum16_result,sum16_result_normalised,sum16_mean,sum16_var,sum16_time);
166
140
vs_post_text (" sum_fast - val %d, normalised val %d , mean %d, variance %d, time %d \n " ,sum_fast_result,sum_fast_result_normalised,sum_fast_mean,sum_fast_var,sum_fast_time);
167
141
168
- // Post data for plot of execution times of each method
169
- int32_t plot_data[4 ];
142
+ // Create an array of data with the latest values to plot
143
+ int32_t plot_data[3 ];
170
144
plot_data[0 ] = sum64_result_normalised;
171
145
plot_data[1 ] = sum16_result_normalised;
172
146
plot_data[2 ] = sum_fast_result_normalised;
173
- vs_post_set_channel (display_plot);
174
- vs_post_int32 (plot_data,1 ,3 );
175
-
176
-
177
- // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
178
- // OUTPUT IMAGES
147
+ vs_post_set_channel (display_plot);// Set target to "post" data to, the display setup for plotting
148
+ vs_post_int32 (plot_data,1 ,3 );// Post the array of data, this should now be plotted in the display
179
149
180
150
output_timer.reset ();
181
- scamp5_output_image (A,display_00);
182
- int output_time_microseconds = output_timer.get_usec ();// get the time taken for image output
151
+ {
152
+ scamp5_output_image (A,display_00);
153
+ }
154
+ int image_output_time_microseconds = output_timer.get_usec ();// get the time taken for image output
183
155
184
156
// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
185
157
// OUTPUT TEXT INFO
186
158
187
159
int frame_time_microseconds = frame_timer.get_usec (); // get the time taken this frame
188
160
int max_possible_frame_rate = 1000000 /frame_time_microseconds; // calculate the possible max FPS
189
- int image_output_time_percentage = (output_time_microseconds *100 )/frame_time_microseconds; // calculate the % of frame time which is used for image output
161
+ int image_output_time_percentage = (image_output_time_microseconds *100 )/frame_time_microseconds; // calculate the % of frame time which is used for image output
190
162
vs_post_text (" frame time %d microseconds(%%%d image output), potential FPS ~%d \n " ,frame_time_microseconds,image_output_time_percentage,max_possible_frame_rate); // display this values on host
191
163
}
192
164
193
165
return 0 ;
194
166
}
195
167
168
+ std::pair<int , int > calculate_mean_and_variance_of_data_array (const std::vector<uint16_t >& data)
169
+ {
170
+ // Calculate sum of all data & mean
171
+ int sum = 0 ;
172
+ for (uint8_t value : data)
173
+ {
174
+ sum += value;
175
+ }
176
+ int mean = sum / data.size ();
177
+
178
+ // Calculate sum of squared differences from the mean
179
+ int sum_of_sqrd_differences = 0 ;
180
+ for (uint8_t value : data)
181
+ {
182
+ int diff = value - mean;
183
+ sum_of_sqrd_differences += diff * diff;
184
+ }
185
+ int variance = sum_of_sqrd_differences / data.size ();
186
+
187
+ // return both mean and variance as a pair
188
+ return {mean, variance};
189
+ }
190
+
191
+ // Compute the maximum & minimum values for each type of summation
192
+ void compute_maximum_and_minimum_of_global_sum_functions ()
193
+ {
194
+ // Make register plane A as negative as possible
195
+ scamp5_in (F,-127 );
196
+ scamp5_kernel_begin ();
197
+ mov (A,F);
198
+ add (A,A,F);
199
+ scamp5_kernel_end ();
200
+
201
+ sum64_min = scamp5_global_sum_64 (A) + 1 ;// +1s to avoid possibility of divide by 0 later
202
+ sum16_min = scamp5_global_sum_16 (A) + 1 ;
203
+ sum_fast_min = scamp5_global_sum_fast (A) + 1 ;
204
+
205
+ // Make register plane A as positive as possible
206
+ scamp5_in (F,127 );
207
+ scamp5_kernel_begin ();
208
+ mov (A,F);
209
+ add (A,A,F);
210
+ add (A,A,F);
211
+ scamp5_kernel_end ();
212
+
213
+ sum64_max = scamp5_global_sum_64 (A);
214
+ sum16_max = scamp5_global_sum_16 (A);
215
+ sum_fast_max = scamp5_global_sum_fast (A);
216
+ }
217
+
196
218
0 commit comments