Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf ·...

36
Lukas Mroz 1 Processing Gigabytes in Seconds Processing Gigabytes in Seconds - Memory and Performance Issues for 2D/3D Image Processing with Java Lukas Mroz TIANI MedGraph

Transcript of Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf ·...

Page 1: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 1Processing Gigabytes in Seconds

Processing Gigabytes inSeconds -

Memory and Performance Issues for 2D/3DImage Processing with Java

Lukas MrozTIANI MedGraph

Page 2: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 2Processing Gigabytes in Seconds

Overview

u Context of this talk: Visualization and

processing of image and volume data

u Memory: problems and solutions

u Performance: squeezing the CPU-cycles

Page 3: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 3Processing Gigabytes in Seconds

Part I

Introduction

Page 4: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 4Processing Gigabytes in Seconds

Medical Imaging

ComputedTomography

20-2000 images512x512x2B

Mammography

2 imagesup to

64MB/image

Page 5: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 5Processing Gigabytes in Seconds

Medical Imaging

Computationally simple Ops,

interactivity crucial.

u Scaling

u Contrast modification

u Filtering

Page 6: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz

Volume Visualization

u A set of slice images forms volume with

(density) value at each sample location

(3D pixel = voxel)

Page 7: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz

Volume Visualization

viewing raylight emission

u How to depict 512x512x512=128Mvoxelson 1024x1024 pixel screen?

u Assign optical properties to each point,

simulate lighting

Page 8: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz

Volume Visualization

u Needs lots of memory(512x512x512x2B=256MB/volume) +optional add-on data.

u All of the data isprocessed to createoutput image

Page 9: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 9Processing Gigabytes in Seconds

Part II

So what about the memory?

Page 10: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 10Processing Gigabytes in Seconds

Memory

Image and volume processing:

handling of large arrys of primitive types

(byte/short/.../double)

Page 11: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 11Processing Gigabytes in Seconds

Available memory

Native (C/C++)

u Virtual memory

size, maximum is

process address

space

(2GB for Windows)

Java

u Maximum VM heap

size defined by -mx

parameter

(64MB default)

u If heap > RAM size

thrashing by GC

Page 12: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 12Processing Gigabytes in Seconds

Allocation of memory

Native (C/C++):

500x alloc/free in

~7ms.

Java

500x new/kill ref in

-mx64M: 7500ms

-mx400M: 1500ms

Why? Init with 0s, GC

Example: process 500 images of 1MB sizeusing a temporary 1MB buffer.alloc/free buffer for each image

Page 13: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 13Processing Gigabytes in Seconds

Facts & Requirements

u Mem allocation slow.

u Array clear on init slow and not necessary

u Mem limited by RAM size

u Medical images: many images of same

size, same operations on many objects.

Page 14: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 14Processing Gigabytes in Seconds

Memory: a Solution

u Array pool to avoid re-allocation/GC of

compatible arrays (same type/size)

u Mem/disk array cache to get more data into

mem

u Combination of both for increased

efficiency

Page 15: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 15Processing Gigabytes in Seconds

Caching Pool

Types of entries:

u Persistent: put object into cache, get back

it‘s original content.

u Personalized: put object into cache, get

back with content if sufficient mem.

u Anonymous: content not relevant, just

avoids re-allocation

Page 16: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 16Processing Gigabytes in Seconds

Persistent Objects

u PoolGuard thread transfers persistent

objects to disk (depends on user activity,

mem-status), pool-size management.

u As soon as on disk: treat as personalized.

Page 17: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 17Processing Gigabytes in Seconds

Personalized Objects

u Linked via SoftReference: Cleared by GC

on low mem.

u Objects reused for other purposes on LRU

basis (to satisfy persistent/anonymous req).

Page 18: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 18Processing Gigabytes in Seconds

Anonymous Objects

u Linked via WeakReference: Cleared by GC

after some time.

u Objects used to avoid new allocation.

Page 19: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 19Processing Gigabytes in Seconds

Problems with Integration

u PoolGuard just tries to guess current

memory status. Tighter cooperation with

GC needed.

u Efficient IO for non-byte arrays

Page 20: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 20Processing Gigabytes in Seconds

IO of Non-Byte Arrays

u Serialization of arrays creates new instance

on read (no reuse!)

u java.nio: Buffers, reads 30MB in 240ms

u Native read/write array methods: reads

30MB in 140ms

Page 21: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 21Processing Gigabytes in Seconds

What Did we Gain?

u 500x alloc/free 1MB anonymous in 20ms (vs

1500ms with new, 7ms in C)

u Work with 4.5GB of

images (~10000) in

a -mx96M VM

Page 22: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 22Processing Gigabytes in Seconds

Part III

Performance without compromises

Page 23: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 23Processing Gigabytes in Seconds

Simple Image Processing

u Most images grayscale, any pixel

resolution between 8-16 bit. => indexed

color model, 256-65536 entry LUT.

u Java2D classes well optimized for

RGB/8/12/16 bit grayscale images.

u BufferedImage cache/pool can be

integrated into array Pool

Page 24: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 24Processing Gigabytes in Seconds

Some History...

u Severe preformance drop for indexed color

models from java 1.1=>1.2

u (Native) optimizations for various pixel

formats followed later

u Treating images a simple arrays saves

memory

... so I don‘t use Java2D goodies :-(

Page 25: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 25Processing Gigabytes in Seconds

Volume Visualizationu No Java API yet :-)

u Computationally extremely demanding:interpolation, shading computation for100M+ of points in the volume.

u Lots of parameter tuning before you seewhat you‘re looking for:interactive feedback crucial!=> struggle for every CPU-cycle even whenusing assembler!

Page 26: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 26Processing Gigabytes in Seconds

Cache Coherence

u Volume is a 3D array, access patterndepends on viewing direction.Cache misses (factor 1-5 slowdown)

u Solution: Re-organize your data soprocessing is allways sequential

u Volume vis. specific:Identify & extractrelevant parts ofvolume for rendering

Page 27: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 27Processing Gigabytes in Seconds

Pre-computing Things into Tablesu Frequent computations into tables

=> lut[idx] instead of Math.sqrt(a+b*idx/...)

u Possible problems:

- accuracy (no float index :-)

- memory demands for large tables

- cache pollution by large/many tables

- memory access is a CPU bottle-neck

Page 28: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 28Processing Gigabytes in Seconds

Tables for Volume Rendering

Tables well suited for shading computation.

LUT[4096]

Surface normal vector

Instead of

RGB

Page 29: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 29Processing Gigabytes in Seconds

Compact Loopfor all voxels rear to front

get sample position

simple projection to screen

get density value and norm vect.

simple lookup for color

compose to screen

Page 30: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 30Processing Gigabytes in Seconds

Code Sizeu Various operations for various „objects“

within volume simulate

shading, xray, surfaces,

transparent materials

u Code allmost identical

u Per-voxel call kills performance

u No macros, no templates :-(

Page 31: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 31Processing Gigabytes in Seconds

Code Size

for(;p<term;p++){

posv=sorted[p];gray=v16[(posv>>16)&0xFFF]; // get gray valuepos=ofs+(posv&0xFF)+((posv>>8)&0xFF)*iidiag;val=iimg[pos];iimg[pos]=((val&0xFF)*invop+

(gray&0xFF00)>>8)|(((((val&0xFF00)*invop+(gray&0xFF0000))>>8)&0xFF00)|(((val>>8)&0xFF00)*invop+((gray>>8)&0xFF0000))&0xFF0000);

}

Add some variations

Page 32: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 32Processing Gigabytes in Seconds

Code Size

u Most code just replicated

u Hard to read and maintain

u JIT fails for too large methods:

execution at mobile-phone speed:-(((

Page 33: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 33Processing Gigabytes in Seconds

Comparison to C/C++u This type of native code (compact loop, no

calls) runs 20-40% faster than Java(Reasons: no array-bounds checking,better optimization )?

u Missing macros/templates make Java codemessy

u Missing unsigned types annoying.(require lots of &0xFF, &0xFFFF in code...)

Page 34: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 34Processing Gigabytes in Seconds

Summaryu More efficient array management and

allocation in Java using own cache/pool.u Better control over GC needed.u Java code 20-40% slower than same

C/C++ code.u Experience: Well optimized Java code

competitive to most nativeimplementations.

u Optimized Java code tends to getunreadable :-(

Page 35: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 35Processing Gigabytes in Seconds

That‘s Itu More on the JVision workstation:

http://www.tiani.com

u Volume rendering applet with RTVR library:

http://http://www.vrvis.at/vis/research/rtvr/

and

http://www.tiani.com/mediaservice/interacti

vegallery/

Page 36: Processing Gigabytes in Secondspeople.apache.org/~sgoeschl/download/jugat/2003-02-03_1.pdf · 2003-03-31 · Lukas Mroz Processing Gigabytes in Seconds 25 Volume Visualization uNo

Lukas Mroz 36Processing Gigabytes in Seconds

Thanks to

... for keeping me awake