Debian Codegraph @

src/SPERR/test_scripts/graveyard/example_speck3d.cpp

raw
1 #include "SPECK3D.h"
2 #include "CDF97.h"
3
4 #include <algorithm>
5 #include <cstdlib>
6 #include <cstring>
7 #include <fstream>
8 #include <iostream>
9 #include <chrono>
10
11
12 // #define FIRST_STEP
13
14
15 extern "C" // C Function calls, and don't include the C header!
16 {
17 int sam_read_n_bytes( const char*, size_t, void* );
18 int sam_write_n_doubles( const char*, size_t, const double* );
19 int sam_get_statsd( const double* arr1, const double* arr2, size_t len,
20 double* rmse, double* lmax, double* psnr,
21 double* arr1min, double* arr1max );
22 }
23
24
25 int main( int argc, char* argv[] )
26 {
27
28 if( argc != 6 )
29 {
30
31 #ifdef QZ_TERM
32 std::cerr << "Usage: ./a.out input_filename dim_x dim_y dim_z qz_levels" << std::endl;
33 #else
34 std::cerr << "Usage: ./a.out input_filename dim_x dim_y dim_z cratio" << std::endl;
35 #endif
36
37 return 1;
38 }
39
40 char output[256];
41 std::strcpy( output, argv[0] );
42 std::strcat( output, ".tmp" );
43
44 const char* input = argv[1];
45 const size_t dim_x = std::atol( argv[2] );
46 const size_t dim_y = std::atol( argv[3] );
47 const size_t dim_z = std::atol( argv[4] );
48 const size_t total_vals = dim_x * dim_y * dim_z;
49
50 #ifdef QZ_TERM
51 const int qz_levels = std::atoi( argv[5] );
52 #else
53 const float cratio = std::atof( argv[5] );
54 #endif
55
56 #ifdef NO_CPP14
57 speck::buffer_type_f in_buf( new float[total_vals] );
58 #else
59 speck::buffer_type_f in_buf = std::make_unique<float[]>( total_vals );
60 #endif
61
62 // Let's read in binaries as 4-byte floats
63 if( sam_read_n_bytes( input, sizeof(float) * total_vals, in_buf.get() ) )
64 {
65 std::cerr << "Input read error!" << std::endl;
66 return 1;
67 }
68
69 // Take input to go through DWT.
70 speck::CDF97 cdf;
71 cdf.set_dims( dim_x, dim_y, dim_z );
72 cdf.copy_data( in_buf, total_vals );
73 const auto startT = std::chrono::high_resolution_clock::now();
74 cdf.dwt3d();
75
76 // Do a speck encoding
77 speck::SPECK3D encoder;
78 encoder.set_dims( dim_x, dim_y, dim_z );
79 encoder.set_image_mean( cdf.get_mean() );
80 encoder.take_coeffs( cdf.release_data(), total_vals );
81
82 #ifdef QZ_TERM
83 #ifdef FIRST_STEP
84 encoder.set_quantization_iterations( qz_levels );
85 #else
86 encoder.set_quantization_term_level( qz_levels );
87 #endif
88 #else
89 const size_t total_bits = size_t(32.0f * total_vals / cratio);
90 encoder.set_bit_budget( total_bits );
91 #endif
92
93 encoder.encode();
94 encoder.write_to_disk( output );
95
96 // Do a speck decoding
97 speck::SPECK3D decoder;
98 decoder.read_from_disk( output );
99
100 #ifdef QZ_TERM
101 decoder.set_bit_budget( 0 );
102 #else
103 decoder.set_bit_budget( total_bits );
104 #endif
105
106 decoder.decode();
107
108 // Do an inverse wavelet transform
109 speck::CDF97 idwt;
110 size_t dim_x_r, dim_y_r, dim_z_r;
111 decoder.get_dims( dim_x_r, dim_y_r, dim_z_r );
112 idwt.set_dims( dim_x_r, dim_y_r, dim_z_r );
113 idwt.set_mean( decoder.get_image_mean() );
114 idwt.take_data( decoder.release_coeffs_double(), dim_x_r * dim_y_r * dim_z_r );
115 idwt.idwt3d();
116
117 // Finish timer and print timing
118 const auto endT = std::chrono::high_resolution_clock::now();
119 const std::chrono::duration<double> diffT = endT - startT;
120 //std::cout << "Time for SPECK in milliseconds: " << diffT.count() * 1000.0f << std::endl;
121
122 // Compare the result with the original input in double precision
123
124 #ifdef QZ_TERM
125 float bpp = float(encoder.get_num_of_bits()) / float(total_vals);
126 #endif
127
128
129 #ifdef FIRST_STEP
130 speck::buffer_type_d in_bufd = std::make_unique<double[]>( total_vals );
131 for( size_t i = 0; i < total_vals; i++ )
132 in_bufd[i] = in_buf[i];
133
134 double rmse, lmax, psnr, arr1min, arr1max;
135 sam_get_statsd( in_bufd.get(), idwt.get_read_only_data().get(),
136 total_vals, &rmse, &lmax, &psnr, &arr1min, &arr1max );
137 printf("Sam stats: rmse = %f, lmax = %f, psnr = %fdB, orig_min = %f, orig_max = %f\n",
138 rmse, lmax, psnr, arr1min, arr1max );
139 in_bufd.reset( nullptr );
140
141 printf("With %d levels of quantization, average BPP = %f, and qz terminates at level %d\n",
142 qz_levels, bpp, encoder.get_quantization_term_level() );
143
144 // For the first step, print out the error values at the following positions:
145 // 0, 50, 100, 200, 300, 400, 500, 1000
146
147 std::vector<float> diff;
148 diff.reserve( total_vals );
149 for( size_t i = 0; i < total_vals; i++ )
150 diff.push_back( std::abs(in_buf[i] - float(idwt.get_read_only_data()[i]) ));
151 std::partial_sort( diff.begin(), diff.begin() + 1001, diff.end(), std::greater<float>() );
152 std::array<size_t, 8> idx { 0, 50, 100, 200, 300, 400, 500, 1000 };
153 for( auto i : idx )
154 std::cout << diff[i] << ", ";
155 std::cout << std::endl;
156
157 #else
158
159 // Count how many data points end up having errors greater than the thresholds.
160 //
161 const size_t num_of_th = 8;
162 // Variable dbz, qz_levels = 17
163 //std::array<float, num_of_th> threshold{0.734512, 0.596821, 0.56641, 0.535465,
164 // 0.519047, 0.504593, 0.491455, 0.459518 };
165 // Variable hvort, qz_levels = 14 -9
166 //std::array<float, num_of_th> threshold{0.00615794, 0.00417704, 0.00400225, 0.00382341,
167 // 0.00369853, 0.00361842, 0.00356053, 0.00338412 };
168 // Variable prespert, qz_levels = 20 -7
169 //std::array<float, num_of_th> threshold{0.0246198, 0.0170196, 0.01632, 0.0154797,
170 // 0.0149632, 0.0146113, 0.0143417, 0.0134916};
171 // Variable qc, qz_levels = 20
172 //std::array<float, num_of_th> threshold{0.010118, 0.00804029, 0.00763616, 0.00728243,
173 // 0.00708556, 0.00691819, 0.00680445, 0.00638294};
174 // Variable qg, qz_levels = 23 -11
175 //std::array<float, num_of_th> threshold{0.00146534, 0.0010819, 0.00103881, 0.000984068,
176 // 0.000951942, 0.000932336, 0.000913382, 0.000861526};
177 // Variable qr, qz_levels = 20 -9
178 //std::array<float, num_of_th> threshold{0.00518513, 0.00401469, 0.00384271, 0.00364681,
179 // 0.00351448, 0.00343102, 0.00336915, 0.0031701};
180 // Variable streamvort, qz_levels = 14 -9
181 //std::array<float, num_of_th> threshold{0.00578914, 0.00446857, 0.00430652, 0.00411505,
182 // 0.00397266, 0.00388597, 0.00380701, 0.00359325};
183 // Variable thrhopert, qz_levels = 18 -6
184 std::array<float, num_of_th> threshold{0.0559266, 0.0360634, 0.0344715, 0.03269,
185 0.0315752, 0.0308189, 0.0302553, 0.0283475 };
186 // Variable uinterp, qz_levels = 19 -5
187 //std::array<float, num_of_th> threshold{0.0894642, 0.0709276, 0.0674381, 0.0647449,
188 // 0.0627775, 0.0612926, 0.0602803, 0.0566506};
189 // Variable vortmag, qz_levels = 15 -9
190 //std::array<float, num_of_th> threshold{0.00588624, 0.00424516, 0.00401397, 0.00382658,
191 // 0.0037123, 0.00362666, 0.00357628, 0.00338255};
192
193 std::vector<float> diff;
194 diff.reserve( total_vals );
195 for( size_t i = 0; i < total_vals; i++ )
196 diff.push_back( std::abs(in_buf[i] - float(idwt.get_read_only_data()[i]) ));
197
198 std::array<size_t, num_of_th> count;
199 count.fill( 0 );
200 for( auto& e : diff ) {
201 for( size_t i = 0; i < num_of_th; i++ ) {
202 if( e > threshold[i] ) {
203 while( i < num_of_th ) {
204 count[i]++;
205 i++;
206 }
207 }
208 }
209 }
210
211 // First print out the effective bpp, then print out the number of outliers
212 std::cout << bpp << ", ";
213 for( auto& e : count )
214 std::cout << e << ", ";
215 std::cout << std::endl;
216 #endif
217
218
219
220 #ifdef EXPERIMENT
221 // Experiment 1:
222 // Sort the differences and then write a tenth of it to disk.
223 std::vector<speck::Outlier> LOS( total_vals, speck::Outlier{} );
224 for( size_t i = 0; i < total_vals; i++ ) {
225 LOS[i].location = i;
226 LOS[i].error = in_buf[i] - float(idwt.get_read_only_data()[i]);
227 }
228
229 const size_t num_of_outliers = total_vals / 10;
230 std::partial_sort( LOS.begin(), LOS.begin() + num_of_outliers, LOS.end(),
231 [](auto& a, auto& b) { return (std::abs(a.error) > std::abs(b.error)); } );
232
233 for( size_t i = 0; i < 10; i++ )
234 printf("outliers: (%ld, %f)\n", LOS[i].location, LOS[i].error );
235
236 std::ofstream file( "top_outliers", std::ios::binary );
237 if( file.is_open() ) {
238 file.write( reinterpret_cast<char*>(LOS.data()), sizeof(speck::Outlier) * num_of_outliers );
239 file.close();
240 }
241 #endif
242
243 }