26
1 Parallel Programming Paradigms Multithreading Task level parallelism By Apex TG India Pvt Ltd http://www.apextgi.in

Multi Threading - Task Level Parallelism

Embed Size (px)

DESCRIPTION

Multithreading is the ability of a program or an operating system to serve more than one user at a time and to manage multiple simultaneous requests without the need to have multiple copies of the programs running within the computer.

Citation preview

  • 1

    Parallel Programming Paradigms

    MultithreadingTask level parallelism

    ByApex TG India Pvt Ltdhttp://www.apextgi.in

    http://www.apextgi.in/
  • 2

    Serial Vs. Parallel

    QPlease

    COUNTER

    COUNTER 1

    COUNTER 2

  • 3

    Single and Multithreaded Processes

    Single-threaded Process

    Single instruction stream Multiple instruction stream

    Multiplethreaded ProcessThreads of

    Execution

    CommonAddress Space

  • 4

    OS:Multi-Processing, Multi-Threaded

    Application

    Application Application

    Application

    CPU

    Better Response Times in Multiple Application Environments

    Higher Throughput for Parallelizeable Applications

    CPUCPU

    CPU CPU CPU

    Threaded Libraries, Multi-threaded I/OThreaded Libraries, Multi-threaded I/O

  • 5

    Multi-threading, continued...Multi-threaded OS enables parallel, scalable I/O

    Application

    CPU CPU CPU

    Application

    Application

    OS KernelMultiple, independent I/O requests can be satisfied simultaneously because all the major disk, tape, and network drivers have been multi-threaded, allowing any given driver to run on multiple CPUs simultaneously.

  • 6

    Shared memory

    segments, pipes, open

    files or mmapd

    files

    Shared memory

    segments, pipes, open

    files or mmapd

    files

    Basic Process Model

    DATADATA

    STACK

    TEXTTEXT

    DATADATA

    STACK

    TEXTTEXT

    processesprocessesShared Memory

    maintained by kernelShared Memory

    maintained by kernel processesprocesses

  • 7

    What are Threads?

    Thread is a piece of code that can execute in concurrence with other threads.

    It is a schedule entity on a processor

    Local stateGlobal/ shared statePCHard/Software Context

    RegistersRegisters

    HardwareContext

    Status WordStatus Word

    Program CounterProgram Counter

    Running Thread Object

  • 8

    Threaded Process Model

    THREAD STACK

    THREAD STACK

    THREAD DATA

    THREAD DATA

    THREAD TEXT

    THREAD TEXT

    SHARED MEMORY

    SHARED MEMORY

    Threads within a process Independent executables All threads are parts of a process hence communication easier and simpler.

  • 9

    Code-GranularityCode ItemLarge grain(task level)Program

    Medium grain(control level)Function (thread)

    Fine grain(data level)Loop

    Very fine grain(multiple issue)With hardware

    Code-GranularityCode ItemLarge grain(task level)Program

    Medium grain(control level)Function (thread)

    Fine grain(data level)Loop

    Very fine grain(multiple issue)With hardware

    Levels of ParallelismLevels of Parallelism

    Task i-lTask i-l Task iTask i Task i+1Task i+1

    func1 ( ){........}

    func1 ( ){........}

    func2 ( ){........}

    func2 ( ){........}

    func3 ( ){........}

    func3 ( ){........}

    a ( 0 ) =..b ( 0 ) =..

    a ( 0 ) =..b ( 0 ) =..

    a ( 1 )=..b ( 1 )=..

    a ( 1 )=..b ( 1 )=..

    a ( 2 )=..b ( 2 )=..

    a ( 2 )=..b ( 2 )=..

    ++ xx LoadLoad

    Task Control Data Multiple Issue

    Task Control Data Multiple Issue

  • 10

    Java

    Multithreading in Java

  • 11

    Java - An Introduction

    Java - The new programming language from Sun Microsystems

    Java -Allows anyone to publish a web page with Java code in it

    Java - CPU Independent language Created for consumer electronics Java - James , Arthur Van , and others Java -The name that survived a patent search Oak -The predecessor of Java Java is C++ -- ++

  • 12

    Object Oriented Languages -A comparison

    Feature C++ ObjectiveC

    Ada Java

    Encapsulation Yes Yes Yes YesInheritance Yes Yes No YesMultiple Inherit. Yes Yes No NoPolymorphism Yes Yes Yes YesBinding (Early or Late) Both Both Early LateConcurrency Poor Poor Difficult YesGarbage Collection No Yes No YesGenericity Yes No Yes NoClass Libraries Yes Yes Limited Yes

  • 13

    Sun defines Java as:

    Simple and PowerfulSimple and Powerful SafeSafe Object OrientedObject Oriented RobustRobust Architecture Neutral and PortableArchitecture Neutral and Portable Interpreted and High PerformanceInterpreted and High Performance Threaded Threaded DynamicDynamic

  • 14

    Java Integrates Power of Compiled Languages

    and Flexibility of Interpreted Languages

  • 15

    Threads

    Java has built in thread support for Multithreading Synchronization Thread Scheduling Inter-Thread Communication:

    currentThread start setPriorityyield run getPrioritysleep stop suspend

    resume Java Garbage Collector is a low-priority thread

  • 16

    Ways of Multithreading in Java Create a class that extends the Thread class Create a class that implements the Runnable interface 1st Method: Extending the Thread class class MyThread extends Thread {

    public void run() { // thread body of execution } } Creating thread: MyThread thr1 = new MyThread(); Start Execution: thr1.start();

  • 17

    2nd method: Threads by implementing Runnable interface

    class ClassName implements Runnable{ ..... public void run() { // thread body of execution }} Creating Object: ClassName myObject = new ClassName(); Creating Thread Object: Thread thr1 = new Thread( myObject ); Start Execution: thr1.start();

  • 18

    Thread Class Members...public class java.lang.Thread extends java.lang.Object implements java.lang.Runnable {// Fieldspublic final static int MAX_PRIORITY;public final static int MIN_PRIORITY;public final static int NORM_PRIORITY;// Constructorspublic Thread();public Thread(Runnable target);public Thread(Runnable target, String name);public Thread(String name);public Thread(ThreadGroup group, Runnable target);public Thread(ThreadGroup group, Runnable target, String name);public Thread(ThreadGroup group, String name);// Methodspublic static int activeCount();public void checkAccess();public int countStackFrames();public static Thread currentThread();public void destroy();public static void dumpStack();public static int enumerate(Thread tarray[]);public final String getName();

  • 19

    ...Thread Class Members.public final int getPriority(); // 1 to 10 priority-pre-emption at mid.public final ThreadGroup getThreadGroup();public void interrupt();public static boolean interrupted();public final boolean isAlive();public final boolean isDaemon();public boolean isInterrupted();public final void join();public final void join(long millis);public final void join(long millis, int nanos);public final void resume();public void run();public final void setDaemon(boolean on);public final void setName(String name);public final void setPriority(int newPriority);public static void sleep(long millis);public static void sleep(long millis, int nanos);public void start();public final void stop();public final void stop(Throwable obj);public final void suspend();public String toString();public static void yield();}

  • 20

    Manipulation of Current Thread// CurrentThreadDemo.javaclass CurrentThreadDemo { public static void main(String arg[]) { Thread ct = Thread.currentThread(); ct.setName( "My Thread" ); System.out.println("Current Thread : "+ct); try { for(int i=5; i>0; i--) { System.out.println(" " + i); Thread.sleep(1000); } } catch(InterruptedException e) { System.out.println("Interrupted."); } }}Run:Current Thread : Thread[My Thread,5,main] 5 4 3 2 1

  • 21

    Creating new Thread...

    // ThreadDemo.javaclass ThreadDemo implements Runnable{ ThreadDemo() { Thread ct = Thread.currentThread(); System.out.println("Current Thread : "+ct); Thread t = new Thread(this,"Demo Thread"); t.start(); try { Thread.sleep(3000); } catch(InterruptedException e) { System.out.println("Interrupted."); } System.out.println("Exiting main thread."); }

  • 22

    ...Creating new Thread.public void run() { try { for(int i=5; i>0; i--) { System.out.println(" " + i); Thread.sleep(1000); } } catch(InterruptedException e) { System.out.println("Child interrupted."); } System.out.println("Exiting child thread."); } public static void main(String args[]) { new ThreadDemo(); }}Run:Current Thread : Thread[main,5,main] 5 4 3Exiting main thread. 2 1Exiting child thread.

  • 23

    Thread Priority...// HiLoPri.javaclass Clicker implements Runnable { int click = 0; private Thread t; private boolean running = true; public Clicker(int p) { t = new Thread(this); t.setPriority(p); } public void run() { while(running) click++; } public void start() { t.start(); } public void stop() { running = false; }}

  • 24

    ...Thread Priorityclass HiLoPri{ public static void main(String args[]) { Thread.currentThread().setPriority(Thread.MAX_PRIORITY); Clicker Hi = new Clicker(Thread.NORM_PRIORITY+2); Clicker Lo = new Clicker(Thread.NORM_PRIORITY-2); Lo.start(); Hi.start(); try { Thread.sleep(10000); } catch (Exception e) { } Lo.stop(); Hi.stop(); System.out.println(Lo.click + " vs. " + Hi.click); }}Run1: (on Solaris)0 vs. 956228Run2: (Window 95)304300 vs. 4066666

  • 25

    The Java monitor model

    Method 1

    Method 2

    Block 1Key

    Threads

    Monitor (synchronised) solves race-condition problem

  • 26

    Parallel Programming ParadigmsSerial Vs. ParallelSingle and Multithreaded ProcessesPowerPoint PresentationSlide 5Basic Process ModelWhat are Threads?Threaded Process ModelSlide 9JavaJava - An IntroductionObject Oriented Languages -A comparisonSun defines Java as:ThreadsWays of Multithreading in Java2nd method: Threads by implementing Runnable interfaceThread Class Members......Thread Class Members.Manipulation of Current ThreadCreating new Thread......Creating new Thread.Thread Priority......Thread PriorityThe Java monitor modelSlide 26