51

Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel [email protected] Asaf Shelly

Embed Size (px)

Citation preview

Page 1: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly
Page 2: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Multiprocessing & The .Net Parallel Extensions

Guy Ben HaimSenior Application

Engineer

[email protected]

www.intel.com/software/

Asaf ShellySenior Consultant

Pacific [email protected]

www.AsyncOp.com

Page 3: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Session Objectives and Agenda• Multicore• Parallel Software• .Net Parallel Extensions• Q&A• Summary

Page 4: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

What is Multicore

Pentium

Pentium

Pentium

Pentium

Pentium Processor

Dual Core

Quad Core

Page 5: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Moore’s Law – GHz to MulticorePerformance

2006

Intel MC Assistance•Threading•Multi-tasking•Training •Tools

Performance ThroughPerformance ThroughMulti-CoreMulti-Core

Performance ThroughPerformance Throughfrequencyfrequency

-+

Page 6: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Intel Processor Advancement

Multiple execution cores ramping across Intel platforms

Page 7: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Why Multi Core?

Power

Performance

2 GHz

100%

Page 8: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

CPU that is 20% Faster

Power

Performance

2.4 GHz 2 GHz

174%

100%

113%100%

Page 9: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

CPU that is 20% Slower

Power

Performance

1.6 GHz

100%

2 GHz

50%

87%

2.4 GHz

174%

100%

113%

Page 10: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Multi Core: Energy Efficient Performance

Power

Performance

1.6 GHz

100%

2 GHz

100%

174%

2.4 GHz

174%

100%

113%

174%

Page 11: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

What does it mean Multi What does it mean Multi Cores?Cores?

PerformancePerformance

Page 12: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Start Thinking Parallel

• Software Today• Instructions – Assembly – Making it workInstructions – Assembly – Making it work

• Thinking like a CPUThinking like a CPU

• Functions – C, Pascal, Basic – Faster CodeFunctions – C, Pascal, Basic – Faster Code• Procedural ThinkingProcedural Thinking

• Objects – C++, Java, C#, Delphi, VB – Manage Objects – C++, Java, C#, Delphi, VB – Manage CodeCode• OOD, OOP – Thinking in objectsOOD, OOP – Thinking in objects

• Tasks - ? – Optimize RuntimeTasks - ? – Optimize Runtime• Thinking Parallel

Page 13: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Situation Today

• Experts, Freelance Specialists, Skilled Groups

• API is not intuitive• Hard to understand execution flow• Problematic Design Patterns• Little awareness of tools• Hidden Problems• Hard to test and debug

Page 14: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Understanding Parallel Computing• Resources• Ownership• Global data / Shared data• Collisions and Race Conditions

• Task Design• Conjunction Points

Page 15: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Task Oriented Design

Modify

Write

Open

Modify

Scan ScanScanScan ScanScan

Page 16: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Simple For

for ( int y = 0; y < bmp.Height; y++ ){ for ( int x = 0; x < bmp.Width; x++ ) { Pixels[ x, y ] = bmp.GetPixel( x, y );

}}

Page 17: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Parallel For

Parallel.For( 0, bmp.Height, y =>{ for ( int x = 0; x < bmp.Width; x++ ) { Pixels[ x, y ] = bmp.GetPixel( x, y );

}});

Page 18: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

.Net Parallel Extensions - Performance

Page 19: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Parallel Class

• Parallel.For• Parallel.Do• Parallel.ForEach• Inplace code / Function• Object Type

Page 20: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Parallel Do

Parallel Quick Sort:

void QuicksortParallel( , , ){ int pivot = Partition(arr, left, right); Parallel.Do( () => QuicksortParallel(arr, left, pivot - 1), () => QuicksortParallel(arr, pivot + 1,

right));}

Page 21: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

PLINQ

Page 22: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

.Net Parallel Extensions – PLINQ

Page 23: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Task Parallel Library

• Parallel For, Do, ForEach• PLINQ• Tasks over Threads• Tasks over Cores• TaskManager• Conjunction Points

Page 24: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

.Net Parallel Extensions – Tasks Parallel Library

Page 25: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

.Net Parallel Extensions – RayTracer

Page 26: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Tips

• Shared are Globals• Parallel Loops are not loops• Define data as Loop internal• Race Conditions are still here• Don’t use Locks!!• Don’t use MUTEXs

Page 27: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Threading Tools

Intel® Thread Checker•Used to create correct multi-threaded code

Intel® Thread Profiler•Used to analyze performance

Intel Software Solutions Group :

http://www.intel.com/software

Page 28: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Data Race example• Serial program

• What is value of A_SUM:

A_Sum = 4 R

S1: x = 1.0; y = 2.0 ; A1 = 0;S2: A1 = x * y;S3: A_SUM = 2 * A1;

x

y A1

Page 29: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Data Race example (Cont.)• Initiate x = 1.0; y = 2.0 ; A1 = 0;

Thread1A1 = x * y

Thread2A_SUM = 2 * A1

• What is value of x if:• Thread1 runs before Thread2?• Thread2 runs before Thread1?

Execution order is not guaranteed

x

y

A_Sum = 4

A_Sum = 0

A1

Page 30: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Intel® Thread Checker Diagnostics

Page 31: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Source Code Viewer

Page 32: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Performance Profile

0

1

2

3

1 2 3 4 Threads

Sp

eed

up

Possible causes for this scalability profile:1. Insufficient parallel work2. Load imbalance3. Synchronization overhead4. Memory bandwidth limitations

Page 33: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Finding Serial and Parallel Time

Page 34: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Load Imbalance

Multi Threading should be Multi Threading should be managed managed Programming should consider load imbalanceProgramming should consider load imbalance

Page 35: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Load Imbalance• Unequal work loads lead to idle threads and

wasted time

Busy

Idle

Time

Thread 0

Thread 1

Thread 2

Thread 3

Start threads

Join thread

s

Page 36: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Synchronization

Programming should consider Synchronizations issuesProgramming should consider Synchronizations issues

Page 37: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Synchronization

• By definition, synchronization serializes execution• Lock contention means more idle time for threads

Busy Idle In Critical

Thread 0

Thread 1

Thread 2

Thread 3

Time

Page 38: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Real example : Before fix

Serial Parallel

Switching Overhead

Page 39: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Real example: After fix

Serial Parallel

2 X Speed Up

Page 40: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Summary

Parallelize or Perish !

Page 41: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Do we really want Parallel Code?Do users even care?

2005 2007 2008 2010

Page 42: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Change In Mindset

Everything is stopped. Waiting for the Everything is stopped. Waiting for the photographerphotographer

Everyone is working independentlyEveryone is working independently

Page 43: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Change In Mindset

Developers are writing functionsDevelopers are writing functions

Developers are managing tasksDevelopers are managing tasks

Page 44: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Change In Mindset

Doing things the way we always haveDoing things the way we always have

Things are going to be differentThings are going to be different

Page 45: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Keep yourself in the loop

• Public event by Pacific Software

• Register to the User Group

• Asynchronous Operations Web Site has all the online resources that you need... and morehttp://www.AsyncOp.com

• Register to my five day course titled Multiprocessing Traps and Pitfalls

• Use our poster to let people know that you know

Page 46: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly
Page 47: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Resources

• Download the Microsoft .Net Parallel Extensionshttp://www.microsoft.com/downloads/details.aspx?FamilyID=e848dc1d-5be3-4941-8705-024bc7f180ba&displaylang=en

• Asynchronous Operations Web Site http://www.AsyncOp.com

• Intel’s Multicorehttp://www.intel.com/multi-core

• Pacificsoft Training and Consultinghttp://www.Pacificsoft.com

• Microsoft Forum for Parallel Computinghttp://forums.microsoft.com/MSDN/default.aspx?ForumGroupID=551&SiteID=1

Page 48: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

Make a difference

• Let us know what you think

• Feedback for the .Net Parallel Extensions Dev team

• Video blog about parallel computing

• Fill the feedback form …

Page 49: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

משוב למלא !כדאיאיך ממלאים?

בעקבות מייל שישלח בסיום כל יום,•, HP במתחם Business Centerב-•בעמדות האינטרנט במלונות הילטון ודן•

Liveמילאת משוב - מגיעה לך חולצת It!

מילאת משוב בשלושת ימי הכנס?

כרטיס טיסה יש לך הזדמנות לזכות ב,BTCמתנת סוכנות לתאילנד

מתנת סמסונג, מכשיר בלאק ג'ק מתנת ניופאן,HTCמכשיר

ועוד...DataSafeמתנת מדיה סנטר

Page 50: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly
Page 51: Multiprocessing & The.Net Parallel Extensions Guy Ben Haim Senior Application Engineer Intel guy.ben.haim@intel.com  Asaf Shelly

© 2007 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only.MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.