Ultra sound solution Impact of C DSP optimization

  • Slides: 38
Download presentation
Ultra sound solution Impact of C++ DSP optimization techniques

Ultra sound solution Impact of C++ DSP optimization techniques

Research Team discussion n n Ultra-sound probe (20 MHz) that sends out signals into

Research Team discussion n n Ultra-sound probe (20 MHz) that sends out signals into body that reflect off moving blood cells in (Artery? Vein? ) Ultra-sound frequency received is Doppler shifted compared to transmitted frequency n n Same as sound when ambulance goes by. Higher if approaching, lower if receding They get the positive frequencies (towards) on the left audio channel and negative frequencies (away) on the right audio channel. 9/9/2020 . ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 2 / 33

Picture looks like this n n Note that the display loses all direction information

Picture looks like this n n Note that the display loses all direction information Can I help them to output the maximum frequency? 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 3 / 33

Captured audio signal Engineering Problems Problem 5 – Different amplitudes common Problem 6 –

Captured audio signal Engineering Problems Problem 5 – Different amplitudes common Problem 6 – Why are funny dead spots not lining up in left and right channels? Handling stereo not mono signals Incorrect labeling / misinterpreation Problem 7 – How to remove dead-spots? 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 4 / 33

Max frequency – definition 1 Frequency below which X% of the frequencies fall n

Max frequency – definition 1 Frequency below which X% of the frequencies fall n Noisy signal for large thresholds > 80% n 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 5 / 33

After XPI Stage 2 n n n n Have a working algorithm concept Engineering

After XPI Stage 2 n n n n Have a working algorithm concept Engineering problem 1 – Complex math (a + jb) on SHARC! Engineering Problem 2 – Define maximum frequency zillions of blood cells – therefore distribution of frequencies Workable prototype – discuss more with customer Engineering Problem 3 – SHARC D/A can’t handle DC signal Workable prototype – discuss more with customer Engineering Problem 4 – Can SHARC handle all this in real-time? Problem 5 – Is different amplitudes of input channels common? Yes Problem 6 – Why are funny dead spots not lining up in left and right channels? Artifact – mislabeled and misinterpreted sampled Problem 7 – How to remove dead-spots? – Discuss more with customer 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 6 / 33

Process. Block DONE OUTSIDE INTERRUPT AVOIDS RACE 9/9/2020 ENCM 515 – Ultrasound Problem Copyright

Process. Block DONE OUTSIDE INTERRUPT AVOIDS RACE 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 7 / 33

Real life problem -- Stereo Minor changes to Audio Premptive Task 9/9/2020 ENCM 515

Real life problem -- Stereo Minor changes to Audio Premptive Task 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 8 / 33

Make “C – code more general n n Moved buffer[ ] to external files

Make “C – code more general n n Moved buffer[ ] to external files Unknown size of arrays being processed 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 9 / 33

Switch to Release mode n Switch to optimizing compiler (Release. NWC) means can no

Switch to Release mode n Switch to optimizing compiler (Release. NWC) means can no longer set breakpoints – Fix with these steps 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 10 / 33

First look at code Timing -- software loop with r 2 as loop counter

First look at code Timing -- software loop with r 2 as loop counter – test at end N * (10 – 1) cycles (jump is not db) -1 for 1 parallel instruction 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 11 / 33

Use Compiler Info button 3 Stalls – 2 on software jump. 1 on ?

Use Compiler Info button 3 Stalls – 2 on software jump. 1 on ? 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 12 / 33

Obvious things to do n n n We are already processing left and right

Obvious things to do n n n We are already processing left and right channels in one program Switch to left audio in dm memory and right audio in pm memory Need to do n n Make right buffers ‘pm’ Change prototype of function to padd pm 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 13 / 33

n n As expected 2 cycles saved Parallel dm and pm reads and writes

n n As expected 2 cycles saved Parallel dm and pm reads and writes 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 14 / 33

Why software loop? n Switch does know what to do about size of loop

Why software loop? n Switch does know what to do about size of loop so can’t oprtimize loop 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca THIS PRAGMA IS A CONTRACT BETWEEN THE DEVELOPER AND COMPILE DON’T LIE 15 / 33

This does not compile n Pragma variables not handled by preprocessor 9/9/2020 ENCM 515

This does not compile n Pragma variables not handled by preprocessor 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 16 / 33

Variable as end of loop Compile will not optimize when loop parameter is declared

Variable as end of loop Compile will not optimize when loop parameter is declared external, or internal or static n 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 17 / 33

Loop parameters all constants known to compiler n Drop from 8 cycles to 2

Loop parameters all constants known to compiler n Drop from 8 cycles to 2 cycles as compiler knows enough to switch to hardware loop control – STALLS FROM JUMP GONE 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 18 / 33

Where am I getting all my info? 9/9/2020 ENCM 515 – Ultrasound Problem Copyright

Where am I getting all my info? 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 19 / 33

Can we switch to SIMD mode VECTORIZATION MAY NOT BE POSSIBLE IF COMPILER DOES

Can we switch to SIMD mode VECTORIZATION MAY NOT BE POSSIBLE IF COMPILER DOES NOT KNOW ABOUT ALIGNMENT OF ARRAYS (How arrays placed in memory) 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 20 / 33

Impact of vectorization n n Before -- loop count was 0 x 80 With

Impact of vectorization n n Before -- loop count was 0 x 80 With memory operations of the form r 2 = dm(i 4, m 6) where m 6 = 1 meaning code is doing r 2 = i 4++; 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 21 / 33

New instructions – SIMD mode n n Bit set mode 1 0 x 200000

New instructions – SIMD mode n n Bit set mode 1 0 x 200000 (bit clr mode 1) Processor doing r 2 = dm(i 5, 2) n n Same as r 2 = dm(i 5, 1) AND s 2 = dm(i 5, 1) Loading two registers 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 22 / 33

Try using #pragma inline n BEFORE 9/9/2020 AFTER (20 cycles faster? ) ENCM 515

Try using #pragma inline n BEFORE 9/9/2020 AFTER (20 cycles faster? ) ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 23 / 33

C++ showing out of order execution WARNING 9/9/2020 ENCM 515 – Ultrasound Problem Copyright

C++ showing out of order execution WARNING 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 24 / 33

Lets do “inline” n Process. One. Block( ) is called by four subroutines –

Lets do “inline” n Process. One. Block( ) is called by four subroutines – lets in 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 25 / 33

Mixed mode view is interesting 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca

Mixed mode view is interesting 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 26 / 33

Mixed Mode n n Out of order execution with 4 copies of the code

Mixed Mode n n Out of order execution with 4 copies of the code for Do. Copy. Block( ) (one for each of Process 0, Process 1, Process 2, Process 3) NO CODE OF Process. One. Block( ) 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 27 / 33

Speed improvement n Moving from software loop and using dm and pm memories caused

Speed improvement n Moving from software loop and using dm and pm memories caused a change from 8 cycles / pt to 2 cycles for two points processed in SIMD n n Moving to IN_LINE causes a change of around 120 cycles for each subroutine call n n (4 CALLS * 7 CYCLES SAVED * N POINTS PROCESSED) (4 CALLS * 120 CYCLES SAVED) N = 128 -- (4 * 1800 to 4 * 120) 480 Mhz processor -- 15 us to 1 us LESSON LEARNT – SPEND YOUR TIME OPTIMIZING THE LOOPS – REST IS SMALLER AND GETS SMALLER WITH LARGER N 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 28 / 33

Other improvements depend on code Characteristics specifics 9/9/2020 ENCM 515 – Ultrasound Problem Copyright

Other improvements depend on code Characteristics specifics 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 29 / 33

9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 30 / 33

9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 30 / 33

Profile guided optimization 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 31 /

Profile guided optimization 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 31 / 33

Memory alignment can be important After first char fetch, system and move to move

Memory alignment can be important After first char fetch, system and move to move 8 chars in SIMD 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 32 / 33

9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 33 / 33

9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 33 / 33

Conditional code (manual PGO) 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 34

Conditional code (manual PGO) 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 34 / 33

Correct ways to process loops 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca

Correct ways to process loops 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 35 / 33

9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 36 / 33

9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 36 / 33

9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 37 / 33

9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 37 / 33

n n n #pragma all_aligned #pragma loop_unroll N #pragma SIMD_for #pragma align num #pragma

n n n #pragma all_aligned #pragma loop_unroll N #pragma SIMD_for #pragma align num #pragma alignment_region( and #pragma alignment_region_end 9/9/2020 ENCM 515 – Ultrasound Problem Copyright smithmr@ucalgary. ca 38 / 33