throughput.c (13355B)
1 /* See LICENSE for license details. */ 2 /* TODO(rnp): 3 * [ ]: for finer grained evaluation of throughput latency just queue a data upload 4 * without replacing the data. 5 * [ ]: bug: we aren't inserting rf data between each frame 6 */ 7 8 #define LIB_FN function 9 #include "ogl_beamformer_lib.c" 10 11 #include <signal.h> 12 #include <stdarg.h> 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include <zstd.h> 16 17 global i32 g_output_points[4] = {512, 1, 1024, 1}; 18 global v2 g_axial_extent = {{ 10e-3f, 165e-3f}}; 19 global v2 g_lateral_extent = {{-60e-3f, 60e-3f}}; 20 global f32 g_f_number = 0.5f; 21 22 typedef struct { 23 b32 loop; 24 b32 cuda; 25 u32 frame_number; 26 27 char **remaining; 28 i32 remaining_count; 29 } Options; 30 31 #define ZEMP_BP_MAGIC (uint64_t)0x5042504D455AFECAull 32 typedef struct { 33 u64 magic; 34 u32 version; 35 u16 decode_mode; 36 u16 beamform_mode; 37 u32 raw_data_dim[4]; 38 u32 decoded_data_dim[4]; 39 f32 xdc_element_pitch[2]; 40 f32 xdc_transform[16]; /* NOTE: column major order */ 41 i16 channel_mapping[256]; 42 f32 transmit_angles[256]; 43 f32 focal_depths[256]; 44 i16 sparse_elements[256]; 45 i16 hadamard_rows[256]; 46 f32 speed_of_sound; 47 f32 center_frequency; 48 f32 sampling_frequency; 49 f32 time_offset; 50 u32 transmit_mode; 51 } zemp_bp_v1; 52 53 global b32 g_should_exit; 54 55 #define die(...) die_((char *)__func__, __VA_ARGS__) 56 function no_return void 57 die_(char *function_name, char *format, ...) 58 { 59 if (function_name) 60 fprintf(stderr, "%s: ", function_name); 61 62 va_list ap; 63 64 va_start(ap, format); 65 vfprintf(stderr, format, ap); 66 va_end(ap); 67 68 os_exit(1); 69 } 70 71 #if OS_LINUX 72 73 #include <fcntl.h> 74 #include <sys/stat.h> 75 #include <unistd.h> 76 77 function void os_init_timer(void) { } 78 79 function f64 80 os_get_time(void) 81 { 82 f64 result = (f64)os_get_timer_counter() / (f64)os_get_timer_frequency(); 83 return result; 84 } 85 86 function s8 87 os_read_file_simp(char *fname) 88 { 89 s8 result; 90 i32 fd = open(fname, O_RDONLY); 91 if (fd < 0) 92 die("couldn't open file: %s\n", fname); 93 94 struct stat st; 95 if (stat(fname, &st) < 0) 96 die("couldn't stat file\n"); 97 98 result.len = st.st_size; 99 result.data = malloc((uz)st.st_size); 100 if (!result.data) 101 die("couldn't alloc space for reading\n"); 102 103 iz rlen = read(fd, result.data, (u32)st.st_size); 104 close(fd); 105 106 if (rlen != st.st_size) 107 die("couldn't read file: %s\n", fname); 108 109 return result; 110 } 111 112 #elif OS_WINDOWS 113 114 function void 115 os_init_timer(void) 116 { 117 os_w32_context.timer_frequency = os_get_timer_frequency(); 118 } 119 120 function f64 121 os_get_time(void) 122 { 123 f64 result = (f64)os_get_timer_counter() / (f64)os_w32_context.timer_frequency; 124 return result; 125 } 126 127 function s8 128 os_read_file_simp(char *fname) 129 { 130 s8 result; 131 iptr h = CreateFileA(fname, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0); 132 if (h == INVALID_FILE) 133 die("couldn't open file: %s\n", fname); 134 135 w32_file_info fileinfo; 136 if (!GetFileInformationByHandle(h, &fileinfo)) 137 die("couldn't get file info\n", stderr); 138 139 result.len = fileinfo.nFileSizeLow; 140 result.data = malloc(fileinfo.nFileSizeLow); 141 if (!result.data) 142 die("couldn't alloc space for reading\n"); 143 144 i32 rlen = 0; 145 if (!ReadFile(h, result.data, (i32)fileinfo.nFileSizeLow, &rlen, 0) && rlen != (i32)fileinfo.nFileSizeLow) 146 die("couldn't read file: %s\n", fname); 147 CloseHandle(h); 148 149 return result; 150 } 151 152 #else 153 #error Unsupported Platform 154 #endif 155 156 function void 157 stream_ensure_termination(Stream *s, u8 byte) 158 { 159 b32 found = 0; 160 if (!s->errors && s->widx > 0) 161 found = s->data[s->widx - 1] == byte; 162 if (!found) { 163 s->errors |= s->cap - 1 < s->widx; 164 if (!s->errors) 165 s->data[s->widx++] = byte; 166 } 167 } 168 169 function void * 170 decompress_zstd_data(s8 raw) 171 { 172 uz requested_size = ZSTD_getFrameContentSize(raw.data, (uz)raw.len); 173 void *out = malloc(requested_size); 174 if (out) { 175 uz decompressed = ZSTD_decompress(out, requested_size, raw.data, (uz)raw.len); 176 if (decompressed != requested_size) { 177 free(out); 178 out = 0; 179 } 180 } 181 return out; 182 } 183 184 function zemp_bp_v1 * 185 read_zemp_bp_v1(u8 *path) 186 { 187 s8 raw = os_read_file_simp((char *)path); 188 zemp_bp_v1 *result = 0; 189 if (raw.len == sizeof(zemp_bp_v1) && *(u64 *)raw.data == ZEMP_BP_MAGIC) { 190 if (((zemp_bp_v1 *)raw.data)->version == 1) 191 result = (zemp_bp_v1 *)raw.data; 192 } 193 return result; 194 } 195 196 function void 197 beamformer_parameters_from_zemp_bp_v1(zemp_bp_v1 *zbp, BeamformerParameters *out) 198 { 199 mem_copy(out->xdc_transform, zbp->xdc_transform, sizeof(out->xdc_transform)); 200 mem_copy(out->xdc_element_pitch, zbp->xdc_element_pitch, sizeof(out->xdc_element_pitch)); 201 mem_copy(out->raw_data_dimensions, zbp->raw_data_dim, sizeof(out->raw_data_dimensions)); 202 203 out->sample_count = zbp->decoded_data_dim[0]; 204 out->channel_count = zbp->decoded_data_dim[1]; 205 out->acquisition_count = zbp->decoded_data_dim[2]; 206 out->decode_mode = (u8)zbp->decode_mode; 207 out->das_shader_id = zbp->beamform_mode; 208 out->time_offset = zbp->time_offset; 209 out->sampling_frequency = zbp->sampling_frequency; 210 out->demodulation_frequency = zbp->center_frequency; 211 out->speed_of_sound = zbp->speed_of_sound; 212 } 213 214 #define shift_n(v, c, n) v += n, c -= n 215 #define shift(v, c) shift_n(v, c, 1) 216 217 function void 218 usage(char *argv0) 219 { 220 die("%s [--loop] [--cuda] [--frame n] base_path study\n" 221 " --loop: reupload data forever\n" 222 " --cuda: use cuda for decoding\n" 223 " --frame n: use frame n of the data for display\n", 224 argv0); 225 } 226 227 function b32 228 s8_equal(s8 a, s8 b) 229 { 230 b32 result = a.len == b.len; 231 for (iz i = 0; result && i < a.len; i++) 232 result &= a.data[i] == b.data[i]; 233 return result; 234 } 235 236 function Options 237 parse_argv(i32 argc, char *argv[]) 238 { 239 Options result = {0}; 240 241 char *argv0 = argv[0]; 242 shift(argv, argc); 243 244 while (argc > 0) { 245 s8 arg = c_str_to_s8(*argv); 246 247 if (s8_equal(arg, s8("--loop"))) { 248 shift(argv, argc); 249 result.loop = 1; 250 } else if (s8_equal(arg, s8("--cuda"))) { 251 shift(argv, argc); 252 result.cuda = 1; 253 } else if (s8_equal(arg, s8("--frame"))) { 254 shift(argv, argc); 255 if (argc) { 256 result.frame_number = (u32)atoi(*argv); 257 shift(argv, argc); 258 } 259 } else if (arg.len > 0 && arg.data[0] == '-') { 260 usage(argv0); 261 } else { 262 break; 263 } 264 } 265 266 result.remaining = argv; 267 result.remaining_count = argc; 268 269 return result; 270 } 271 272 function i16 * 273 decompress_data_at_work_index(Stream *path_base, u32 index) 274 { 275 stream_append_byte(path_base, '_'); 276 stream_append_u64_width(path_base, index, 2); 277 stream_append_s8(path_base, s8(".zst")); 278 stream_ensure_termination(path_base, 0); 279 280 s8 compressed_data = os_read_file_simp((char *)path_base->data); 281 i16 *result = decompress_zstd_data(compressed_data); 282 if (!result) 283 die("failed to decompress data: %s\n", path_base->data); 284 free(compressed_data.data); 285 286 return result; 287 } 288 289 function b32 290 send_frame(i16 *restrict i16_data, BeamformerParameters *restrict bp) 291 { 292 u32 data_size = bp->raw_data_dimensions[0] * bp->raw_data_dimensions[1] * sizeof(i16); 293 b32 result = beamformer_push_data_with_compute(i16_data, data_size, BeamformerViewPlaneTag_XZ, 0); 294 if (!result && !g_should_exit) printf("lib error: %s\n", beamformer_get_last_error_string()); 295 296 return result; 297 } 298 299 function void 300 execute_study(s8 study, Arena arena, Stream path, Options *options) 301 { 302 fprintf(stderr, "showing: %.*s\n", (i32)study.len, study.data); 303 304 stream_append_s8(&path, study); 305 stream_ensure_termination(&path, OS_PATH_SEPARATOR_CHAR); 306 stream_append_s8(&path, study); 307 i32 path_work_index = path.widx; 308 309 stream_append_s8(&path, s8(".bp")); 310 stream_ensure_termination(&path, 0); 311 312 zemp_bp_v1 *zbp = read_zemp_bp_v1(path.data); 313 if (!zbp) die("failed to unpack parameters file\n"); 314 315 BeamformerParameters bp = {0}; 316 beamformer_parameters_from_zemp_bp_v1(zbp, &bp); 317 318 mem_copy(bp.output_points, g_output_points, sizeof(bp.output_points)); 319 bp.output_points[3] = 1; 320 321 bp.output_min_coordinate[0] = g_lateral_extent.x; 322 bp.output_min_coordinate[1] = 0; 323 bp.output_min_coordinate[2] = g_axial_extent.x; 324 325 bp.output_max_coordinate[0] = g_lateral_extent.y; 326 bp.output_max_coordinate[1] = 0; 327 bp.output_max_coordinate[2] = g_axial_extent.y; 328 329 bp.f_number = g_f_number; 330 bp.beamform_plane = 0; 331 bp.interpolation_mode = BeamformerInterpolationMode_Cubic; 332 333 bp.decimation_rate = 1; 334 bp.demodulation_frequency = bp.sampling_frequency / 4; 335 336 /* NOTE(rnp): v1 files didn't specify sampling mode. it was almost always 4X */ 337 bp.sampling_mode = BeamformerSamplingMode_4X; 338 339 #if 0 340 BeamformerFilterParameters kaiser = {0}; 341 kaiser.Kaiser.beta = 5.65f; 342 kaiser.Kaiser.cutoff_frequency = 2.0e6f; 343 kaiser.Kaiser.length = 36; 344 345 beamformer_create_filter(BeamformerFilterKind_Kaiser, (f32 *)&kaiser.Kaiser, 346 sizeof(kaiser.Kaiser) / sizeof(f32), bp.sampling_frequency / 2, 0, 0, 0); 347 beamformer_set_pipeline_stage_parameters(0, 0); 348 #endif 349 350 #if 1 351 BeamformerFilterParameters matched = {0}; 352 typeof(matched.MatchedChirp) *mp = &matched.MatchedChirp; 353 mp->duration = 18e-6f; 354 mp->min_frequency = 2.9e6f - bp.demodulation_frequency; 355 mp->max_frequency = 6.0e6f - bp.demodulation_frequency; 356 357 bp.time_offset += mp->duration / 2; 358 359 beamformer_create_filter(BeamformerFilterKind_MatchedChirp, (f32 *)&matched.MatchedChirp, 360 sizeof(matched.MatchedChirp) / sizeof(f32), bp.sampling_frequency / 2, 361 1, 0, 0); 362 beamformer_set_pipeline_stage_parameters(0, 0); 363 #endif 364 365 if (zbp->sparse_elements[0] == -1) { 366 for (i16 i = 0; i < countof(zbp->sparse_elements); i++) 367 zbp->sparse_elements[i] = i; 368 } 369 370 b32 tx_rows = (zbp->transmit_mode & (1 << 1)) == 0; 371 b32 rx_rows = (zbp->transmit_mode & (1 << 0)) == 0; 372 u8 packed_tx_rx = 0; 373 if (tx_rows) packed_tx_rx |= BeamformerRCAOrientation_Rows << 4; 374 else packed_tx_rx |= BeamformerRCAOrientation_Columns << 4; 375 if (rx_rows) packed_tx_rx |= BeamformerRCAOrientation_Rows << 0; 376 else packed_tx_rx |= BeamformerRCAOrientation_Columns << 0; 377 378 if (bp.das_shader_id == BeamformerAcquisitionKind_HERCULES || 379 bp.das_shader_id == BeamformerAcquisitionKind_UHERCULES) 380 { 381 bp.single_focus = 1; 382 bp.single_orientation = 1; 383 384 bp.transmit_receive_orientation = packed_tx_rx; 385 bp.focal_vector[0] = zbp->transmit_angles[0]; 386 bp.focal_vector[1] = zbp->focal_depths[0]; 387 } else { 388 alignas(64) v2 focal_vectors[BeamformerMaxChannelCount]; 389 for (u32 i = 0; i < countof(focal_vectors); i++) 390 focal_vectors[i] = (v2){{zbp->transmit_angles[i], zbp->focal_depths[i]}}; 391 beamformer_push_focal_vectors((f32 *)focal_vectors, countof(focal_vectors)); 392 393 alignas(64) u8 transmit_receive_orientations[BeamformerMaxChannelCount]; 394 for (u32 i = 0; i < countof(transmit_receive_orientations); i++) 395 transmit_receive_orientations[i] = packed_tx_rx; 396 beamformer_push_transmit_receive_orientations(transmit_receive_orientations, 397 countof(transmit_receive_orientations)); 398 } 399 400 beamformer_push_channel_mapping(zbp->channel_mapping, countof(zbp->channel_mapping)); 401 beamformer_push_sparse_elements(zbp->sparse_elements, countof(zbp->sparse_elements)); 402 beamformer_push_parameters(&bp); 403 404 i32 shader_stages[16]; 405 u32 shader_stage_count = 0; 406 shader_stages[shader_stage_count++] = BeamformerShaderKind_Demodulate; 407 if (options->cuda) shader_stages[shader_stage_count++] = BeamformerShaderKind_CudaDecode; 408 else shader_stages[shader_stage_count++] = BeamformerShaderKind_Decode; 409 shader_stages[shader_stage_count++] = BeamformerShaderKind_DAS; 410 411 beamformer_push_pipeline(shader_stages, shader_stage_count, BeamformerDataKind_Int16); 412 413 beamformer_set_global_timeout(1000); 414 415 stream_reset(&path, path_work_index); 416 i16 *data = decompress_data_at_work_index(&path, options->frame_number); 417 418 if (options->loop) { 419 BeamformerLiveImagingParameters lip = {.active = 1}; 420 beamformer_set_live_parameters(&lip); 421 422 u32 frame = 0; 423 f32 times[32] = {0}; 424 f32 data_size = (f32)(bp.raw_data_dimensions[0] * bp.raw_data_dimensions[1] * sizeof(*data)); 425 f64 start = os_get_time(); 426 for (;!g_should_exit;) { 427 if (send_frame(data, &bp)) { 428 f64 now = os_get_time(); 429 f32 delta = (f32)(now - start); 430 start = now; 431 432 if ((frame % 16) == 0) { 433 f32 sum = 0; 434 for (u32 i = 0; i < countof(times); i++) 435 sum += times[i] / countof(times); 436 printf("Frame Time: %8.3f [ms] | 32-Frame Average: %8.3f [ms] | %8.3f GB/s\n", 437 delta * 1e3, sum * 1e3, data_size / (sum * (GB(1)))); 438 } 439 440 times[frame % countof(times)] = delta; 441 frame++; 442 } 443 i32 flag = beamformer_live_parameters_get_dirty_flag(); 444 if (flag != -1 && (1 << flag) == BeamformerLiveImagingDirtyFlags_StopImaging) 445 break; 446 } 447 448 lip.active = 0; 449 beamformer_set_live_parameters(&lip); 450 } else { 451 for (u32 i = 0; i < zbp->raw_data_dim[2]; i++) 452 send_frame(data + i * bp.raw_data_dimensions[0] * bp.raw_data_dimensions[1], &bp); 453 } 454 455 free(zbp); 456 free(data); 457 } 458 459 function void 460 sigint(i32 _signo) 461 { 462 g_should_exit = 1; 463 } 464 465 extern i32 466 main(i32 argc, char *argv[]) 467 { 468 Options options = parse_argv(argc, argv); 469 470 if (!BETWEEN(options.remaining_count, 1, 2)) 471 usage(argv[0]); 472 473 os_init_timer(); 474 475 signal(SIGINT, sigint); 476 477 Arena arena = os_alloc_arena(KB(8)); 478 Stream path = stream_alloc(&arena, KB(4)); 479 stream_append_s8(&path, c_str_to_s8(options.remaining[0])); 480 stream_ensure_termination(&path, OS_PATH_SEPARATOR_CHAR); 481 482 execute_study(c_str_to_s8(options.remaining[1]), arena, path, &options); 483 484 return 0; 485 }