diff --git a/fpga/hls/CMakeLists.txt b/fpga/hls/CMakeLists.txt index f2d7a0e8..052914e8 100644 --- a/fpga/hls/CMakeLists.txt +++ b/fpga/hls/CMakeLists.txt @@ -62,6 +62,7 @@ MAKE_HLS_MODULE(mask_missing.cpp mask_missing) MAKE_HLS_MODULE(integration.cpp integration) MAKE_HLS_MODULE(spot_finder.cpp spot_finder) MAKE_HLS_MODULE(spot_finder.cpp spot_finder_update_sum) +MAKE_HLS_MODULE(spot_finder.cpp spot_finder_line_sum) MAKE_HLS_MODULE(axis_broadcast.cpp axis_broadcast) MAKE_HLS_MODULE(axis_256_to_512.cpp axis_256_to_512) MAKE_HLS_MODULE(axis_256_to_512.cpp axis_32_to_512) @@ -84,6 +85,7 @@ SET (HLS_IPS psi_ch_hls_data_collection_fsm_1_0.zip psi_ch_hls_mask_missing_1_0.zip psi_ch_hls_frame_generator_1_0.zip psi_ch_hls_spot_finder_update_sum_1_0.zip + psi_ch_hls_spot_finder_line_sum_1_0.zip psi_ch_hls_spot_finder_1_0.zip psi_ch_hls_integration_1_0.zip psi_ch_hls_axis_broadcast_1_0.zip diff --git a/fpga/hls/spot_finder.cpp b/fpga/hls/spot_finder.cpp index 8429a064..40a0d923 100644 --- a/fpga/hls/spot_finder.cpp +++ b/fpga/hls/spot_finder.cpp @@ -203,6 +203,71 @@ void spot_finder_update_sum(STREAM_512 &data_in, data_out << packet_in; } +void spot_finder_line_sum(STREAM_512 &data_in, + STREAM_512 &data_out, + hls::stream> &sum_in, + hls::stream> &sum2_in, + hls::stream> &valid_in, + hls::stream> &sum_out, + hls::stream> &sum2_out, + hls::stream> &valid_out) { +#pragma HLS INTERFACE axis port=data_in +#pragma HLS INTERFACE axis port=data_out +#pragma HLS INTERFACE axis port=sum_in +#pragma HLS INTERFACE axis port=sum2_in +#pragma HLS INTERFACE axis port=valid_in +#pragma HLS INTERFACE axis port=sum_out +#pragma HLS INTERFACE axis port=sum2_out +#pragma HLS INTERFACE axis port=valid_out + packet_512_t packet_in; + data_in >> packet_in; + data_out << packet_in; + + ap_uint column_sum_val_save = 0; + ap_uint column_sum2_val_save = 0; + ap_uint column_valid_val_save = 0; + + data_in >> packet_in; + while (!packet_in.user) { + + ap_uint column_sum = 0; + ap_uint column_sum2 = 0; + ap_uint column_valid = 0; + + ap_uint line_sum; + ap_uint line_sum2; + ap_uint line_valid; +#pragma HLS PIPELINE II=33 + + for (int i = 0; i < 32; i++) { + + data_out << packet_in; + data_in >> packet_in; + + sum_in >> column_sum; + sum2_in >> column_sum2; + valid_in >> column_valid; + + sum_out << prefix_sum((column_sum, column_sum_val_save)); + sum2_out << prefix_sum((column_sum2, column_sum2_val_save)); + valid_out << prefix_sum((column_valid, column_valid_val_save)); + + column_sum_val_save = column_sum(SUM_BITWIDTH * 32 - 1, SUM_BITWIDTH * (32 - 2 * FPGA_NBX)); + column_sum2_val_save = column_sum2(SUM2_BITWIDTH * 32 - 1, SUM2_BITWIDTH * (32 - 2 * FPGA_NBX)); + column_valid_val_save = column_valid(MASK_SUM_BITWIDTH * 32 - 1, MASK_SUM_BITWIDTH * (32 - 2 * FPGA_NBX)); + } + + sum_out << prefix_sum((ap_uint(0), column_sum_val_save)); + sum2_out << prefix_sum((ap_uint(0), column_sum2_val_save)); + valid_out << prefix_sum((ap_uint(0), column_valid_val_save)); + + column_sum_val_save = 0; + column_sum2_val_save = 0; + column_valid_val_save = 0; + } + data_out << packet_in; +} + void spot_finder(STREAM_512 &data_in, STREAM_512 &data_out, diff --git a/fpga/hls/spot_finder.h b/fpga/hls/spot_finder.h index e4394e0e..c05025f6 100644 --- a/fpga/hls/spot_finder.h +++ b/fpga/hls/spot_finder.h @@ -28,6 +28,24 @@ void update_sum(ap_uint &ext_val1, const ap_uint ext_val2) { ext_val1 = pack32(val1); } +template +ap_uint prefix_sum(const ap_uint ext_column_sum) { +#pragma HLS PIPELINE II=1 + ap_int column_sum[32 + 2 * FPGA_NBX]; + ap_int line_sum[32]; + + for (int i = 0; i < 32 + 2 * FPGA_NBX; i++) + column_sum[i] = ext_column_sum(i * N + (N - 1), i * N); + + for (int i = 0; i < 32; i++) { + line_sum[i] = column_sum[i]; + for (int j = 1; j < 2 * FPGA_NBX + 1; j++) + line_sum[i] += column_sum[i + j]; + } + + return pack32(line_sum); +} + template ap_uint prefix_sum(const ap_uint ext_column_sum, const ap_uint ext_column_sum_old) { @@ -35,7 +53,6 @@ ap_uint prefix_sum(const ap_uint ext_column_sum, ap_int column_sum[32 + 2 * FPGA_NBX]; ap_uint retval; ap_int line_sum[32]; - ap_int tmp_sum = 0; for (int i = 0; i < 2 * FPGA_NBX; i++) column_sum[i] = ext_column_sum_old(i*N+(N-1), i*N); @@ -43,16 +60,19 @@ ap_uint prefix_sum(const ap_uint ext_column_sum, for (int i = 0; i < 32; i++) column_sum[i+ 2 * FPGA_NBX] = ext_column_sum(i * N + (N - 1), i * N); - for (int i = 0 ; i < 2 * FPGA_NBX; i++) { - tmp_sum += column_sum[i]; + for (int i = 0; i < 32; i++) { + line_sum[i] = 0; + for (int j = 0; j < 2 * FPGA_NBX + 1; j++) + line_sum[i] += column_sum[i + j]; } +/* for (int i = 0; i < 32; i++) { tmp_sum += column_sum[i + 2 * FPGA_NBX]; line_sum[i] = tmp_sum; tmp_sum -= column_sum[i]; } - +*/ return pack32(line_sum); } @@ -76,6 +96,15 @@ void spot_finder_update_sum(STREAM_512 &data_in, hls::stream> &sum2_out, hls::stream> &valid_out); +void spot_finder_line_sum(STREAM_512 &data_in, + STREAM_512 &data_out, + hls::stream> &sum_in, + hls::stream> &sum2_in, + hls::stream> &valid_in, + hls::stream> &sum_out, + hls::stream> &sum2_out, + hls::stream> &valid_out); + ap_uint<1> check_threshold(ap_int<16> val, ap_int sum, ap_int sum2, diff --git a/tests/FPGASpotFindingUnitTest.cpp b/tests/FPGASpotFindingUnitTest.cpp index e69c5d90..488f051e 100644 --- a/tests/FPGASpotFindingUnitTest.cpp +++ b/tests/FPGASpotFindingUnitTest.cpp @@ -128,6 +128,26 @@ TEST_CASE("FPGA_update_sum" , "[FPGA][SpotFinder]") { REQUIRE(arr_out[i] == 8 * i + 2); } +int sum_consecutive(int x0, int n) { + int ret = 0; + for (int i = 0; i < n; i++) + ret += x0 + i; + return ret; +} + +TEST_CASE("FPGA_prefix_sum" , "[FPGA][SpotFinder]") { + ap_uint<16*(32+2*FPGA_NBX)> input = 0; + for (int i = 0; i < 32+2*FPGA_NBX; i++) + input(i*16+15, i*16) = i; + + auto output = prefix_sum<16>(input); + ap_uint<16> output_unpacked[32]; + unpack32(output, output_unpacked); + REQUIRE(output_unpacked[0] == sum_consecutive(0, 2 * FPGA_NBX + 1)); + REQUIRE(output_unpacked[2] == sum_consecutive(2, 2 * FPGA_NBX + 1)); + REQUIRE(output_unpacked[7] == sum_consecutive(7, 2 * FPGA_NBX + 1)); +} + bool Isigma_cpu(double val, double sum, double sum2, float threshold) { double mean = sum / ((2*FPGA_NBX+1) * (2*FPGA_NBX+1)); double mean2 = sum2 / ((2*FPGA_NBX+1) * (2*FPGA_NBX+1)); @@ -161,7 +181,6 @@ TEST_CASE("FPGA_spot_check_threshold","[FPGA][SpotFinder]") { } } - TEST_CASE("FPGA_spot_finder_update_sum","[FPGA][SpotFinder]") { STREAM_512 input; STREAM_512 output; @@ -244,3 +263,49 @@ TEST_CASE("FPGA_spot_finder_update_sum","[FPGA][SpotFinder]") { CHECK(valid[1+511*1024] == FPGA_NBX + 1); } +TEST_CASE("FPGA_spot_finder_line_sum","[FPGA][SpotFinder]") { + STREAM_512 input; + STREAM_512 output; + + hls::stream> sum_in; + hls::stream> sum2_in; + hls::stream> valid_in; + + hls::stream> sum_out; + hls::stream> sum2_out; + hls::stream> valid_out; + + ap_int sum_unpacked[32]; + ap_int sum2_unpacked[32]; + ap_int valid_unpacked[32]; + + input << packet_512_t{.user = 0}; + for (int i = 0; i < 32; i++) + input << packet_512_t{.data = 0, .user = 0}; + + input << packet_512_t{.user = 1}; + + for (int i = 0; i < 32; i++) { + for (int j = 0; j < 32; j++) { + sum_unpacked[j] = i * 32 + j; + sum2_unpacked[j] = 8934 + (i * 32 + j); + valid_unpacked[j] = i; + } + sum_in << pack32(sum_unpacked); + sum2_in << pack32(sum2_unpacked); + valid_in << pack32(valid_unpacked); + } + + spot_finder_line_sum(input, output, sum_in, sum2_in, valid_in, sum_out, sum2_out, valid_out); + + REQUIRE(input.size() == 0); + + REQUIRE(output.size() == 32 + 2); + REQUIRE(sum_out.size() == 33); + REQUIRE(sum2_out.size() == 33); + REQUIRE(valid_out.size() == 33); + + for (int i = 0; i < 32; i++) { + + } +} \ No newline at end of file