Skip to content

Commit e1be0ab

Browse files
committed
Update EX_GLOBAL_SUM.hpp
1 parent 952d0cc commit e1be0ab

File tree

1 file changed

+94
-72
lines changed

1 file changed

+94
-72
lines changed

EXAMPLES/EX_GLOBAL_SUM.hpp

Lines changed: 94 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,27 @@
11
#include <scamp5.hpp>
22
#include <vector>
3-
43
#include "MISC/MISC_FUNCS.hpp"
54
using namespace SCAMP5_PE;
65

76
vs_stopwatch frame_timer;
87
vs_stopwatch output_timer;
98
vs_stopwatch sum_timer;
109

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;
2214

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);
2920

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);
3123

32-
return {mean, variance};
33-
}
24+
void compute_maximum_and_minimum_of_global_sum_functions();
3425

3526
int main()
3627
{
@@ -46,8 +37,12 @@ int main()
4637
int display_size = 2;
4738
auto display_00 = vs_gui_add_display("Image to Sum",0,0,display_size);
4839

40+
//Add display to plot the results from each global summation function
4941
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);
5146

5247
int use_test_input = 0;
5348
vs_gui_add_switch("use test input", use_test_input == 1, &use_test_input);
@@ -58,37 +53,8 @@ int main()
5853
int input_value = 0;
5954
vs_gui_add_slider("input_value",-127,127,input_value,&input_value);
6055

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();
9258

9359
// Frame Loop
9460
while(1)
@@ -99,7 +65,7 @@ int main()
9965
vs_frame_loop_control();
10066

10167
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
102-
//Capture / Create the analogue image to test globabl summation upon
68+
//CAPTURE OR CREATE THE ANALOGUE DATA TO TEST GLOBAL SUMMATION UPON
10369

10470
if(!use_test_input)
10571
{
@@ -121,7 +87,9 @@ int main()
12187
//Create an image with a certain proportion of pixels the maximum value (and other pixels the minimum value)
12288
scamp5_in(E,127);
12389
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
12593
scamp5_kernel_begin();
12694
mov(A,F);
12795
WHERE(S6);
@@ -132,7 +100,7 @@ int main()
132100
}
133101

134102
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
135-
//Perform global summation using each method
103+
//PERFORM GLOBAL SUMMATION USING EACH FUNCTION
136104

137105
sum_timer.reset();
138106
int16_t sum64_result = scamp5_global_sum_64(A);
@@ -146,7 +114,10 @@ int main()
146114
int16_t sum_fast_result = scamp5_global_sum_fast(A);
147115
int sum_fast_time = sum_timer.get_usec();
148116

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
150121
int sum64_result_normalised = (100*sum64_result)/(sum64_max - sum64_min);
151122
int sum16_result_normalised = (100*sum16_result)/(sum16_max - sum16_min);
152123
int sum_fast_result_normalised = (100*sum_fast_result)/(sum_fast_max - sum_fast_min);
@@ -155,42 +126,93 @@ int main()
155126
sum64_record[record_cntr] = sum64_result_normalised;
156127
sum16_record[record_cntr] = sum16_result_normalised;
157128
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
159130

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
163137

164138
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);
165139
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);
166140
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);
167141

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];
170144
plot_data[0] = sum64_result_normalised;
171145
plot_data[1] = sum16_result_normalised;
172146
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
179149

180150
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
183155

184156
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
185157
//OUTPUT TEXT INFO
186158

187159
int frame_time_microseconds = frame_timer.get_usec(); //get the time taken this frame
188160
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
190162
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
191163
}
192164

193165
return 0;
194166
}
195167

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+
196218

0 commit comments

Comments
 (0)