Cell Software Solutions Programming Model

51
Systems and Technology Group GDC:06 03/19/06 © 2006 IBM Corporation Cell Software Solutions Programming Model Duc Vianney, Ph. D. Cell Ecosystem & Solutions Development, IBM Corporation Programmability Programmer Productivity Raw Hardware Performance BE Design

Transcript of Cell Software Solutions Programming Model

Page 1: Cell Software Solutions Programming Model

Systems and Technology Group

GDC:06 03/19/06 © 2006 IBM Corporation

Cell Software Solutions Programming Model

Duc Vianney, Ph. D.Cell Ecosystem & Solutions Development, IBM Corporation

Programmability

ProgrammerProductivity

Raw HardwarePerformance BE

Design

Page 2: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation2 GDC:06 03/19/06

AcknowledgmentsPeter Hofstee, Ph.D., STIDCDan Brokenshire, STIDCJeff Derby, Ph.D., E&TS Performance AnalysisMichael Perrone, Ph.D., Cell Solutions Development, IBM Researchamong many others ….

TrademarksCell Broadband Engine ™ is a trademark of Sony Computer Entertainment, Inc.

Page 3: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation3 GDC:06 03/19/06

Outline

Cell overviewCell hardware/software solutions challengesCell programming models– PPE-centric vs. SPE-centric– PPE programming models– SPE programming models– Function offload – Overlapping DMA and computation – Heterogeneous multi-thread

Cell software solutions development guidelinesCell performance tips and practicesCell Ideal & non-ideal software

Page 4: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation4 GDC:06 03/19/06

Cell Overview

CBE or “Cell Broadband Engine”– Also known as: BE, Cell processor

CBE includes:– PPC core with “traditional” memory

subsystem– 8 “synergistic processing elements”– very high bandwidth internal

interconnect “Element Interconnect Bus” (BE data ring)

– I/O interfaces (2)

SPU

LS

MFC

SPE

EIB

L2

L1

PPCVMX

PPE

MAINMEM

C I/O NC I/O

OFFCHIP

SPU

LS

MFC

SPE

SPU

LS

MFC

SPE

SPU

LS

MFC

SPE

SPU

LS

MFC

SPE

SPU

LS

MFC

SPE

SPU

LS

MFC

SPE

SPU

LS

MFC

SPE

OFFCHIP

Page 5: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation5 GDC:06 03/19/06

Cell Overview – system statistics

Memory

25.6GB/s

L2 (512KB)

L1 (32KB I+D)

PU / VMX

25.6GB/s

51.2GB/s 51.2GB/s

BE Data Ring (204.8GB/s)

51.2GB/s

25GB/s 35GB/s

SPU (w/ 128 128b registers)

LS(256KB)

SPU (w/ 128 128b registers)

LS(256KB)

SPU (w/ 128 128b registers)

LS(256KB)

SPU (w/ 128 128b registers)

LS(256KB)

SPU (w/ 128 128b registers)

LS(256KB)

SPU (w/ 128 128b registers)

LS(256KB)

25.6GB/s

25.6GB/s

25.6GB/s

25.6GB/s

51.2GB/s 51.2GB/s 51.2GB/s 51.2GB/s 51.2GB/s 51.2GB/s

51.2GB/s

added value of BE compute power and bandwidth

traditional computation

25.6GB/s

BIF &I/O

85.6 GB/s

25.6GB/s

25.6GB/s

25.6GB/s

25.6GB/s

25.6GB/s

25.6GB/s

25.6GB/s

25.6GB/s

SPU (w/ 128 128b registers)

LS(256KB)

SPU (w/ 128 128b registers)

LS(256KB)

51.2GB/s 51.2GB/s

25.6GB/s

25.6GB/s25.6GB/s

25.6GB/s

Cell includes 1PPE + 8SPEs– provided more than 8x compute

capability than traditional processors• de-coupled SIMD engines for

growth and scalability– simple PPC micro-architecture

• in-order; dual issue• dual-thread support• minimal chip area / power

budget– 8 SPU SIMD engines provide

tremendous compute power• in-order; dual issue• dedicated resources

– 128 128-bit registers– 256KB local store– 2x25.6GB/s DMA, etc.

• up to 16-way SIMD for exploiting data parallelism

• 25.6 SP GFlops/1.6 DP GFlops per SPU

– EIB (BE data ring) for intra-BE and external communication• 204.8GB/s peak bandwidth • 25.6GB/s memory b/w• 35GB/s (out)+25GB/s (in) IO

Page 6: Cell Software Solutions Programming Model

Systems and Technology Group

© 2005 IBM CorporationGDC:06 03/19/066

Page 7: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation7 GDC:06 03/19/06

Cell Solutions ChallengesHardware challenges – Small Main Memory per chip (512MB per Cell Processor) and small local memory store per SPE– Single Precision floating point uses truncated rounding (decreased accuracy in favor of speed)Software challenges– Operating System requires patches– Programming model & non-homogeneous multiprocessing

• Limited set of optimized libraries; tool chain in progress, no commercial-level Fortran– Enablement/Integration with existing products

• Middleware, commercial libraries, GPFS, managementSolutions design challenges– Two Levels of Parallelism

• Regular vector data that is SIMD-able• Independent tasks that may be executed in parallel

– Computational• SIMD engines on 8 SPEs and 1 PPE (multi-threaded)• Parallel sequence to be distributed over 8 SPE / 1 PPE• 256KB local store per SPE usage (data + code)

– Communicational• DMA and Bus bandwidth

– DMA granularity – 128 bytes– DMA bandwidth among LS and System memory, and DMA latency

• Traffic control– Exploit computational complexity and data locality to lower data traffic requirement

• Shared memory / Message passing abstraction overhead• Synchronization

Page 8: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation8 GDC:06 03/19/06

The Need for Cell Programming Model

Cell provides a massive computational capacity.Cell provides a huge communicational bandwidth.The resources are distributed.A properly selected Cell programming model provides a programmer a systematic and cost-effective framework to apply Cell resources to a particular class of applications.A Cell programming model may be supported by language constructs, runtime, libraries, or object-oriented frameworks.

Page 9: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation9 GDC:06 03/19/06

Programming Model Influencers

PowerPC 64 compliant

Direct problem state mapping

VM address translation and protection

SW managed DMA engines

Coherent shared memory

Multi-threading

Multiple execution units

SIMD

Bandwidth Reservations

High speed EIB

High speed coherent interconnect

SPE Events

Aliased LS memory

DMA list supporting scatter / gather

Resource Management Tables

Atomic operations

Signal Notification Registers

Mailboxes

Large SPE context

Heterogeneous

DMA alignment & size restrictions

Limited local store size

Page 10: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation10 GDC:06 03/19/06

Programming Models

One focus is on how an application can be partitioned across theprocessing elements– PPE, SPEs

Partitioning involves consideration of and trade-offs among:– processing load– program structure– data flow– data and code movement via DMA– loading of bus and bus attachments– desired performance

Several models:– “PPE-centric” vs. “SPE-centric”– “data-serial” vs. “data-parallel”– others …

Page 11: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation11 GDC:06 03/19/06

“PPE-Centric” & “SPE-Centric” Models

“PPE-Centric”:– an offload model– main line application code runs in PPC core– individual functions extracted and offloaded to SPEs– SPUs wait to be given work by the PPC core

“SPE-Centric”:– most of the application code distributed among SPEs– PPC core runs little more than a resource manager for the SPEs (e.g.

maintaining in main memory control blocks with work lists for the SPEs)– SPE fetches next work item (what function to execute, pointer to data, etc.)

from main memory (or its own memory) when it completes current work item

Page 12: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation12 GDC:06 03/19/06

Single Cell Programming Models

SPE LS

SPE LS

PPE thread

Large small

Multi-SPE

BE-level

Effective AddressSpaceSingle Cell environment:

PPE programming modelsSPE Programming models– Small single-SPE models– Large single-SPE models– Multi-SPE parallel programming

models

Cell Embedded SPE Object Format (CESOF)

Page 13: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation13 GDC:06 03/19/06

PPE Programming Model (participation)PPE is a 64-bit PowerPC core, hosting operating systems and hypervisorPPE program inherits traditional programming modelsCell environment: a PPE program serves as a controller or facilitator– CESOF support provides SPE image handles to the PPE runtime– PPE program establishes a runtime environment for SPE programs

• e.g. memory mapping, exception handling, SPE run control– It allocates and manages Cell system resources

• SPE scheduling, hypervisor CBEA resource management– It provides OS services to SPE programs and threads

• e.g. printf, file I/O

Page 14: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation14 GDC:06 03/19/06

Small Single-SPE Models

Single tasked environmentSmall enough to fit into a 256KB- local storeSufficient for many dedicated workloadsSeparated SPE and PPE address spaces – LS / EAExplicit input and output of the SPE program– Program arguments and exit code per SPE ABI– DMA– Mailboxes– SPE side system callsFoundation for a function offload model or a synchronous RPC model– Facilitated by interface description language (IDL)

Page 15: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation15 GDC:06 03/19/06

Small Single-SPE Models – tools and environment

SPE compiler/linker compiles and links an SPE executableThe SPE executable image is embedded as reference-able RO data in the PPE executable (CESOF)A Cell programmer controls an SPE program via a PPE controlling process and its SPE management library– i.e. loads, initializes, starts/stops an SPE program The PPE controlling process, OS/PPE, and runtime/(PPE or SPE) together establish the SPE runtime environment, e.g. argument passing, memory mapping, system call service.

Page 16: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation16 GDC:06 03/19/06

Large Single-SPE Programming Models

Data or code working set cannot fit completely into a local storeThe PPE controlling process, kernel, and libspe runtime set up the system memory mapping as SPE’s secondary memory storeThe SPE program accesses the secondary memory store via its software-controlled SPE DMA engine -Memory Flow Controller (MFC)

SPE Program

System Memory

PPE controller maps system memory for

SPE DMA trans.

Local Store

DMA transactions

Page 17: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation17 GDC:06 03/19/06

Large Single-SPE Programming Models – I/O data

System memory for large size input / output data– e.g. Streaming model

int g_ip[512*1024]

System memory

int g_op[512*1024]

int ip[32]

int op[32]

SPE program: op = func(ip)

DMA

DMA

Local store

Page 18: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation18 GDC:06 03/19/06

Large Single-SPE Programming Models

System memory as secondary memory store– Manual management of data buffers– Automatic software-managed data cache

• Software cache framework libraries• Compiler runtime support

Global objects

System memory

SW cache entriesSPE program

Local store

Page 19: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation19 GDC:06 03/19/06

Large Single-SPE Programming Models

System memory as secondary memory store– Manual loading of plug-in into code buffer

• Plug-in framework libraries– Automatic software-managed code overlay

• Compiler generated overlaying code

System memory

Local store

SPE plug-in b

SPE plug-in a

SPE plug-in e

SPE plug-in a

SPE plug-in b

SPE plug-in c

SPE plug-in d

SPE plug-in e

SPE plug-in f

Page 20: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation20 GDC:06 03/19/06

Large Single-SPE Prog. Models – Job Queue

Code and data packaged together as inputs to an SPE kernel programA multi-tasking model (more discussion later)

Job queue

System memory

Local store

code/data ncode/data n+1code/data n+2

code/data …

Code nData n

SPE kernel

DMA

Page 21: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation21 GDC:06 03/19/06

Large Single-SPE Programming Models - DMA

DMA latency handling is critical to overall performance for SPE programs moving large data or codeData pre-fetching is a key technique to hide DMA latency– e.g. double-buffering

Time

I Buf 1 (n) O Buf 1 (n)

I Buf 2 (n+1) O Buf 2 (n-1)

SPE program: Func (n)

outputn-2 inputn Outputn-1

Func (inputn)

Inputn+1

Func (inputn+1)Func (inputn-1)

outputn Inputn+2DMAs

SPE exec.

DMAs

SPE exec.

Page 22: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation22 GDC:06 03/19/06

Large Single-SPE Programming Models - CESOF

Cell Embedded SPE Object Format (CESOF) and PPE/SPE toolchains support the resolution of SPE references to the global system memory objects in the effective-address space.

_EAR_g_foo structure

Local Store Space

Effective Address Space

Char g_foo[512]

Char local_foo[512]

DMA transactions

CESOF EARsymbol resolution

Page 23: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation23 GDC:06 03/19/06

Parallel Programming Models

Traditional parallel programming models applicableBased on interacting single-SPE programsParallel SPE program synchronization mechanism

• Cache line-based MFC atomic update commands similar to the PowerPC lwarx, ldarx, stwcx, and stdcx instructions

• SPE input and output mailboxes with PPE• SPE signal notification / register• SPE events and interrupts• SPE busy poll of shared memory location

Page 24: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation24 GDC:06 03/19/06

Parallel Programming Models – Shared Memory

Access data by address– Random access in natureCESOF support for shared effective-address variables With proper locking mechanism, large SPE programs may access shared memory objects located in the effective-address spaceCompiler OpenMP support

Page 25: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation25 GDC:06 03/19/06

Parallel Programming Models – Shared MemoryExploit standalone SPE programs – “SPUlet”Multiple SPE cooperative computing through shared system memory.PPE assists in OS services

File and network I/O, shared memory access, etc…

PPE

SPE 0

SPE 1

SPE n. . .

Shared

System

Memory

OS Request

Data Access

Page 26: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation26 GDC:06 03/19/06

Parallel Programming Models – Streaming

Large array of data fed through a group of SPE programsA special case of job queue with regular dataEach SPE program locks on the shared job queue to obtain next jobFor uneven jobs, workloads are self-balanced among available SPEs

PPE

SPE1Kernel()

SPE0Kernel()

SPE7Kernel()

System Memory

In

.

I7

I6

I5

I4

I3

I2

I1

I0

On

.

O7

O6

O5

O4

O3

O2

O1

O0

…..

Page 27: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation27 GDC:06 03/19/06

Parallel Programming Models – Message Passing

Access data by connection– Sequential in natureApplicable to SPE programs where addressable data space only spans over local storeThe message connection is still built on top of the shared memory model Compared with software-cache shared memory model– More efficient runtime is possible, no address info handling overhead once

connected– LS to LS DMA optimized for data streaming through pipeline model

Page 28: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation28 GDC:06 03/19/06

Parallel Programming Models – Pipeline

Use LS to LS DMA bandwidth, not system memory bandwidthFlexibility in connecting pipeline functionsLarger collective code size per pipelineLoad-balance is harder

PPE

SPE1Kernel1()

SPE0Kernel0()

SPE7Kernel7()

System Memory

In

.

.

I6

I5

I4

I3

I2

I1

I0

On

.

.

O6

O5

O4

O3

O2

O1

O0

…..DMA DMA

Page 29: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation29 GDC:06 03/19/06

Multi-tasking SPEs – LS resident multi-tasking

Simplest multi-tasking programming modelNo memory protection among tasks Co-operative, Non-preemptive, event-driven scheduling

acadxacd

Task a

Task b

Task c

Task d

Task x

EventDispatcher

Local Store

SPE n

Event Queue

Page 30: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation30 GDC:06 03/19/06

Multi-tasking SPEs – Self-managed multi-tasking

Non-LS residentBlocked job context is swapped out of LS and scheduled back later to the job queue once unblocked

Job queue

System memory

Local store

task ntask n+1task n+2

Task …

Code nData n

SPE kerneltask n’

task queue

Page 31: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation31 GDC:06 03/19/06

Multi-tasking SPEs – Kernel managed

Kernel-level SPE management model– SPE as a device resource– SPE as a heterogeneous processor– SPE resource represented as a file systemSPE scheduling and virtualization– Maps running threads over a physical SPE or a group of SPEs– More concurrent logical SPE tasks than the number of physical SPEs – High context save/restore overhead

• favors run-to-completion scheduling policy– Supports pre-emptive scheduling when needed– Supports memory protection

Page 32: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation32 GDC:06 03/19/06

25 GB/sec Memory Inteface

“data-serial” vs. “data-parallel” Models

256 GB/sec Coherent Ring

MFC

Local Store

SPU

N

AUC

MFC

Local Store

SPU

N

AUC

Power Processor(PPE)

L2 Cache

NCULocal Store

SPUMFC

N

AUC

Local Store

SPUMFC

N

AUC

N N

N N

CellBroadbandEngine

MFC

Local Store

SPU

N

AU

C

MFC

Local Store

SPU

N

AU

C

MFC

Loca

l Sto

re

SPU

N

AU

C

MFC

Loca

l Sto

re

SPU

N

AU

C

Function offload – Application accelerator model

Application specific acceleratorsHeterogeneous multi-threading

Page 33: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation33 GDC:06 03/19/06

Function Offload Power Core

(PPE)

System Memory

SPU

Local Store

MFC

N

SPU

Local Store

MFC

N

Multi-stage Pipeline SPU

Local Store

MFC

N

SPU

Local Store

MFC

N

SPU

Local Store

MFC

N

Parallel-stages

Power Core(PPE)

System Memory

Dedicated Function (problem/privileged subsystem)– Programmer writes/uses SPU enabled "libraries"

• Graphics Pipeline• Audio Processing• MPEG Encoding/Decoding• Encryption / Decryption

– Main Application in PPE, invokes SPU bound services• RPC Like Function Call• I/O Device Like Interface (FIFO/ Command Queue)

– 1 or more SPUs cooperating in subsystem• Problem State (Application Allocated)• Privileged State (OS Allocated)

– Code-to-data or data-to-code pipelining possible– Very efficient in real-time data streaming applications

Page 34: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation34 GDC:06 03/19/06

A Pipelined Model

Data-serialExample: three function groups, so three SPEsDataflow is unidirectionalSynchronization is important– time spent in each function group should be about the same– but may complicate tuning and optimization of code

Main data movement is SPE-to-SPE– can be push or pull

Page 35: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation35 GDC:06 03/19/06

A Data-Partitioned Model

Data-parallelExample: data blocks partitioned into three sub-blocks, so three SPEsMay require coordination among SPEs between functions– e.g. if there is interaction between data sub-blocks

Essentially all data movement is SPE-to main memory or main memory-to-SPE

Page 36: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation36 GDC:06 03/19/06

Application Specific Acceleration Model Application Specific Acceleration Model –– SPE Accelerated SubsystemsSPE Accelerated Subsystems

System Memory Parameter Area

PPEPowerPCApplication

Cell AwareOS (Linux)

Compression/Decompression

RealtimeMPEG

Encoding

Graphics Subsystem

DataEncryption

DataDecryption

OpenGL Encrypt Decrypt Encoding

O/S Service or Library

mpeg_encode()

MFC

Local Store

SPU

N

AUC

MFC

Local Store

SPU

N

AUC

MFC

Local Store

SPU

N

AUC

MFC

Local Store

SPU

N

AUC

MFC

Local Store

SPU

N

AUC

MFC

Local Store

SPU

N

AUC

MFC

Local Store

SPU

N

AUC

MFC

Local Store

SPU

N

AUC

Decoding

Application Specific AcceleratorsAcceleration provided by OS or application libraries

Application portability maintained with platform specific libraries

Function Offload

Page 37: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation37 GDC:06 03/19/06

Overlapping DMA and Computation Model

DMA transactions see latency in addition to transfer time– e.g. SPE DMA get from main memory may see a 475-cycle

latency

Double (or multiple) buffering of data can hide DMA latencies under computation, e.g. the following is done simultaneously:– process current input buffer and write output to current output

buffer in SPE LS– DMA next input buffer from main memory– DMA previous output buffer to main memory

requires blocking of inner loops

Trade-offs because SPE LS is relatively small– double buffering consumes more LS– single buffering has a performance impact due to DMA

latency

Page 38: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation38 GDC:06 03/19/06

Application Source& Libraries

PPE object files SPE object files

SPESPE SPE SPE

Physical SPEsSPESPE SPE SPE

Cell AwareOS ( Linux)SPE Virtualization / Scheduling Layer (m->n SPE threads)

New SPE tasks/threadsExisting PPE tasks/threads

Physical PPE

PPEMT1 MT2

Heterogeneous Multi-Thread Model

PPE - control processorSPE - data plane processor

Current Linux Operating System Runtime Strategy– PPE Threads, SPE Threads– SPE DMA EA = PPE Process EA Space– OS supports Create/Destroy SPE tasks– Atomic Update Primitives used for Mutex– SPE Context Fully Managed

• Context Save/Restore for Debug• Virtualization Mode (indirect access)• Direct Access Mode (realtime)

– OS assignment of SPE threads to SPEs

• Programmer directed using affinity mask & NUMA topological services

Page 39: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation39 GDC:06 03/19/06

Cell Software Solutions Development Guidelines

Algorithm complexity studyData layout/locality and Data flow analysisExperimental partitioning and mapping of the algorithm and program structure to the architectureDevelop PPE Control, PPE Scalar codeDevelop PPE Control, partitioned SPE scalar code– Communication, synchronization, latency handlingTransform SPE scalar code to SPE SIMD codeRe-balance the computation / data movementOther optimization considerations– PPE SIMD, system bottle-neck, load balance

Page 40: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation40 GDC:06 03/19/06

Application Algorithm Types (Cell Affinity)Pair and Sequence comparisons– Examples: Rich Media Mining, Bioinformatics, SPAM filtering, monitoring,

surveillanceData transformation– Examples: Transcoding (e.g. Mpeg to 2 Mpeg 4), XSLT, repurposing, Affine

transforms (graphics), encryption/compression, decryption/decompression, Video compression/transformation, visualization

Computation– Examples: Ray Tracing, low precision/Game Physics, Matrix multiply, FFT– Any computations that can be reliably done in SP FP– Data Parallel Floating Point (SIMD)

• Examples: FSS (Monte Carlo), DSP Algorithms, Ray Tracing, FFT

Page 41: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation41 GDC:06 03/19/06

Code Performance and Optimization

Many factors affect code performance– and most if not all of these are under the control of the programmer

Start at the beginning: application partitioning strategy– some approaches and trade-offs may be difficult to see a priori– but there are guidelines that can be followed

Certain items should always be kept in mind when performance is important– data alignment issues– instruction-set characteristics– microarchitecture characteristics

What follows is a list of tips and practices

Page 42: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation42 GDC:06 03/19/06

Performance Tips & PracticesApplication partitioning– investigate different alternatives– offload most work to SPE. In general, the greater the percentage of total code

that runs in SPEs instead of in the PPE, the better (“SPE-centric” rather than “PPE-centric”)

– allocate work that are autonomous and non-synchronized– pay attention to data type differences between PPE and SPE

DMA– use SPE-initiated DMAs rather than PPE-initiated DMAs– use multiple-buffering in SPEs to hide DMA latencies under useful processing– use cache-line (128B) alignment of blocks being transferred whenever possible

PPE code management– PPE code can include software-directed prefetching (e.g. ‘dcbt’ instruction –

prefetch a single cache line from memory to both L1 and L2 caches)– avoid PPE pre-accesses to large datasets intended primarily for use by SPEs

(so that SPE-initiated DMAs transfer from main memory and not from PPE’s L2)– exploit PPE multi-threading capability– allocate large data sets from large pages to reduce TLB thrashing. The CBE

supports 3 concurrent page sizes: 4KB, and 2 from 64KB, 1MB and 16MB.

Page 43: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation43 GDC:06 03/19/06

More Performance Tips & PracticesSIMD– investigate different vectorization alternatives– take advantage of data reorganization within the register file (which can often

occur in parallel with computation)– use ‘vector select’ instruction to deal with some ‘if-then-else’ constructs

Data Structures– design data structures for efficient SPE data accesses. – be aware of data alignment, access patterns, and location– MFC supports transfer of 1,2,4,8,n*16 (up to 16K) bytes. Transfer less than

16B must be naturally aligned to minimize EIB bus utilization

SPE inner loops– use unrolling and software pipelining where possible– keep issue rules in mind– be aware that sometimes increasing the number of instructions in an inner loop

can actually reduce the cycle count per pass through the loop– use the SPE static timing analysis tool (see below)

Page 44: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation44 GDC:06 03/19/06

Still More Performance Tips & PracticesSPE branch behavior– make sure ‘branch hint’ instructions are properly scheduled (the compiler is generally very

good at this, but …)– avoid data-dependent branches where possible (e,g, by using ‘select’ operation)

SPE fixed-point arithmetic– avoid multiplication of ints (SPE does not have a 32x32 fixed-point multiply)– keep in mind that SPE has no fixed-point saturating arithmetic

SPE intrinsics– use inline assembly intrinsics to control of instruction scheduling, data loads/stores, looping

and branching, etc.

SPE issue behavior– SPE contains 2 instruction pipelines with instructions pre-assigned to execute on only one

of the pipelines– two instructions are issued every clock cycle assuming

• there are no dependencies and operands are available• the even addressed instruction is a pipeline 0 instruction and the odd addressed

instruction is a pipeline 1 instruction• the instructions are ordered pipeline 0 followed by pipeline 1

Need to choose the instructions wisely to improve dual issue rates and reduce latencies

Page 45: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation45 GDC:06 03/19/06

SPE Static Timing Analysis

Static timing analyzer is provided in the SDK with the SPE XLC compiler– version for use with SPE gcc compiler also provided

Provides a static timing analysis of compiled SPE code– based on issue rules, pipeline latencies, and static dependencies– assumes all branches not taken– cannot account for data-dependent behavior

To use the static timing analysis tool:– set environment variable SPU_TIMING=1– execute ‘make SPE_code_filename.s’ (assembler output from compiler)– file SPE_code_filename.s.timing also generated

Page 46: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation46 GDC:06 03/19/06

Typical Cell Solutions Development QuestionsWhat are the core algorithms of the application area and their characteristics/requirements?– Languages (C, C++, Fortran … POSIX sockets & threads, MPI, OpenMP, UPC)– Single Precision or Double Precision– Time to solution examples and problem scaling– Typical kLoC– Library dependencies– Tools/ISV dependenciesWhat is the per CPU memory requirement? memory b/w requirement? I/O requirement?What is the characteristic ops/byte?What are the interconnect requirements for interprocessor communication – latency and bandwidth?What type of programming model does the application support, e.g. SIMD, MIMD, Embarrassingly Parallel, Collective Communication, Shared Memory …What is the current preferred platform for these applications and why?Are there any further application requirements (e.g. dynamic and resilient infrastructure, security, heterogeneous data sources, etc) that are important when deciding on system architecture?What are some example ISVs or Open Source implementations?Are there any applicable benchmarks (e.g. GUPS, SPEC)?How could you envision Cell as an accelerator for these algorithms?

Page 47: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation47 GDC:06 03/19/06

Cell Solutions Affinity AreasDigital Media– Image processing– Video processing– Visualization– Compression / decompression– Encryption / Decryption– Digital Signal Processing– Digital Content CreationGraphics– Graphics Transformations– Lighting– Ray Tracing– Ray casting– Rich Media MiningFloating Point Intensive Applications– Single precision Physics – Single precision HPC– Sonar

Pattern Matching– String manipulation

(search engine)– Parsing, transformation,

and Translation (XSLT)– Audio processing– Language translation– Speech recognition– Filtering & PruningOffload Engines– TCP/IP – Math– Network Security and

IntrusionBiology– Genomics– Gene Sequencing– Medical Imaging– BioinformaticsBusiness IntelligenceFinancial Service

Streaming Data processing ApplicationsHigh-Energy PhysicsAstrophysicsCFDMolecular DynamicsRadar Cross SectionHigh-Resolution Atmospheric/OceanElectromagneticsComputational ChemistryCrash SimulationMonte CarloLow-Res ClimateSeismic MigrationReservoir SimulationsNVH/StructuresEDANetworking

Page 48: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation48 GDC:06 03/19/06

Ideal Cell SoftwareAlgorithms that fit GPU’s (Graphics Processing Units)

• GPU’s are being used for more than just graphics today thanks to PCI Express

• A list of types and examples ported to GPUs can be found at: http://www.gpgpu.org/ and is partially duplicated here:

GPGPU.org Application listingCategoriesGPGPU (182)

Advanced Rendering (20) Global Illumination (9) Image-Based Modeling & Rendering (5) Audio and Signal Processing (3) Computational Geometry (10) GIS (1) Surfaces and Modeling (2) Conferences (3) Contests (1) Database (4) Sort & Search (1) GPUs (6) High-Level Languages (11) Image And Volume Processing (29) Computer Vision (3) Med & Bio (1) Miscellaneous (36) Books (5) Courses (11) Developer Resources (12) Journals (2) Research Groups (2) Press (1) Scientific Computing (40) Data Compression (2) Data Structures (1) Dynamics Simulation (1) Numerical Algorithms (2) Site News (6) Stream Processing (1) Tools (8)

Page 49: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation49 GDC:06 03/19/06

Problem can be re-codedTypical code is double-buffered gather-compute-scatterStructured – Predictable non-trivial memory access pattern– Can build scatter-gather lists– Easier for memory fetch & SIMD operations– Data prefetch possible – Non branchy instruction pipeline – Data more tolerant, but has the same cautionMultiple Operations on Data– Many operations on same data before reloading Easy Parallelize and SIMD– Little or nor collective communication required– No Global or Shared memory or nested loopsCompute IntenseFits Streaming Model – Small computation kernel through which you stream

a large body of data– Algorithms that fit Graphics Processing Units– GPU’s are being used for more than just graphics

today thanks to PCI ExpressFocus on 32b float, or <=32b integerExamples:– FFTw ( best result about 100GFlops )– Terrain Rendering Engine– Volume rendering– Crypto codes ( RSA, SHA, DES, etc. etc. etc.)– Media codes ( MPEG 2, MPEG 4, H.264, JPEG )

Ideal Cell Software (up to 100x)

Page 50: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation50 GDC:06 03/19/06

Non-Ideal SoftwareBranchy data– Instruction “branchiness” may be partially mitigated through different methods (e.g. calculating

both sides of the branch and using select)Not structured– Not SIMD friendlyPointer Indirection or multiple levels of pointer indirection (fetching becomes hard)Data load granularity less than 16 bytes (will cause performance degradation)– DMA <128 Byte – SPE to local store is <16 ByteNot easily parallelizedTightly coupled algorithms requiring synchronization

Page 51: Cell Software Solutions Programming Model

Systems and Technology Group

© 2006 IBM Corporation51 GDC:06 03/19/06

(c) Copyright International Business Machines Corporation 2005.All Rights Reserved. Printed in the United Sates April 2005.

The following are trademarks of International Business Machines Corporation in the United States, or other countries, or both. IBM IBM Logo Power Architecture

Other company, product and service names may be trademarks or service marks of others.

All information contained in this document is subject to change without notice. The products described in this document are NOT intended for use in applications such as implantation, life support, or other hazardous uses where malfunction could result in death, bodily injury, or catastrophic property damage. The information contained in this document does not affect or change IBM product specifications or warranties. Nothing in this document shall operate as an express or implied license or indemnity under the intellectual property rights of IBM or third parties. All information contained in this document was obtained in specific environments, and is presented as an illustration. The results obtained in other operating environments may vary.

While the information contained herein is believed to be accurate, such information is preliminary, and should not be relied upon for accuracy or completeness, and no representations or warranties of accuracy or completeness are made.

THE INFORMATION CONTAINED IN THIS DOCUMENT IS PROVIDED ON AN "AS IS" BASIS. In no event will IBM be liable for damages arising directly or indirectly from any use of the information contained in this document.

IBM Microelectronics Division The IBM home page is http://www.ibm.com1580 Route 52, Bldg. 504 The IBM Microelectronics Division home page is Hopewell Junction, NY 12533-6351 http://www.chips.ibm.com