1 / 138

Curs 8

Curs 8. Procesare paralela in Java. Arhitecturi Paralele. Clasificare arhitecturi paralele. Clasificarea Flyn SISD - conventional SIMD - calcul vectorial MISD - scalcul sistolic MIMD – cazul general. SISD : Von Neuman. Instructiuni. Procesor. Date Intrare. Date Iesire.

Download Presentation

Curs 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Curs 8 Procesareparalela in Java

  2. ArhitecturiParalele

  3. Clasificarearhitecturiparalele • ClasificareaFlyn • SISD - conventional • SIMD - calculvectorial • MISD - scalculsistolic • MIMD – cazul general

  4. SISD : Von Neuman Instructiuni Procesor Date Intrare Date Iesire

  5. Flux de Instructiuni B Procesor A Flux de date de Iesire Procesor B Procesor C Flux de Instructiuni A Arhitectura MISD Flux de Instructiuni C Flux de date de Intrare

  6. Flux de Instructiuni Flux de Date de Intrare A Procesor A Procesor B Procesor C Architectura SIMD Flux de Date de Iesire A Flux de Date de Iesire B Flux de Date de Intrare B Flux de Date de Iesire C Flux de Date de Intrare C Ci<= Ai * Bi

  7. Architectura MIMD Flux de Instructiuni C Flux de Instructiuni B Flux de Instructiuni A Flux de Date de Iesire A Flux de Date de Intrare A Procesor A Flux de Date de Iesire B Flux de Date de Intrare B Procesor B Flux de Date de Iesire C Procesor C Flux de Date de Intrare C

  8. MAGISTRALA MAGISTRALA MAGISTRALA MEMORIE MEMORIE MEMORIE Masina MIMD cu memoriecomuna Procesor A Procesor B Procesor C Sistem de MemorieGlobala

  9. MAGISTRALA MAGISTRALA MAGISTRALA MEMORIE MEMORIE MEMORIE MIMD cu memoriedistribuita Canal Comunicatie Canal Comunicatie Procesor A Procesor B Procesor C Memorie SistemA Memorie SistemB Memorie SistemC

  10. Nivele de paralelism

  11. Nivele de Paralelism Granularitate cod Entitate Cod Granularitate mare (nivel task) Program Granularitatemedie (nivel control) Functie(thread) Granularitatefina (nivel date) Bucla Granularitatefoartefina (alegeri multiple ) Cu suport hard Task i-l Task i Task i+1 func1 ( ) { .... .... } func2 ( ) { .... .... } func3 ( ) { .... .... } a ( 0 ) =.. b ( 0 ) =.. a ( 1 )=.. b ( 1 )=.. a ( 2 )=.. b ( 2 )=.. + x Load

  12. Paralelism la nivel de procese

  13. Procesulclasic Stiva Stiva Memorie COMUNA, segmente, pipes, Fisieredeschisesaumapate in memorie Date Date Text Text Memoriecomuna Mentinuta de kernel processe procese

  14. Definirea/InstantiereaunorproceseExemple de relatii de precedenta

  15. Procese cu unulsaumaimulte fire interne de executie Proces cu un singur fir de executie Proces cu maimulte fire de executie Fire de executie Thread-uri Flux unic de instructiuni Spatiu de adrese COMUN pt fire Al procesului Flux multiplu de instructiuni

  16. Thread-uri

  17. Context Hardware/ software Registri Cuvant stare Program Counter executare Ce suntfirele de executie? • Un fir de executieeste o portiune de cod executabil care se poateexecuta in paralel (concurent) cu alte fire de executie)

  18. STIVA FIRULUI Memorie Comuna • Thread light (“procesusor”) • Un fir de executiepeste o zona mica continua de memorie care esteprimit de la (si din) procesulparinte DATELE FIRULUI TEXTUL FIRULUI

  19. Exemplu Thread in Linux void *func ( ) { /* define local data */ - - - - - - - - - - - - - - - - - - - - - - /* function code */ - - - - - - - - - - - thr_exit(exit_value); } main ( ) { thread_ttid; intexit_value; - - - - - - - - - - - thread_create (0, 0, func (), NULL, &tid); - - - - - - - - - - - thread_join (tid, 0, &exit_value); - - - - - - - - - - - }

  20. Paralelism in procese int add (int a, int b, int & result) // corpul int sub(int a, int b, int & result) // corpul Date Procesor a b r1 c d r2 IS1 add pthread t1, t2; pthread-create(&t1, add, a,b, & r1); pthread-create(&t2, sub, c,d, & r2); pthread-par (2, t1, t2); Processor IS2 sub

  21. Paralelism date Date sort( int *array, int count) //...... //...... Procesor do “ “ dn/2 dn2/+1 “ “ dn Sortare pthread-t, thread1, thread2; “ “ pthread-create(& thread1, sort, array, N/2); pthread-create(& thread2, sort, array, N/2); pthread-par(2, thread1, thread2); IS Procesor Sortare

  22. De ce thread-uri? • Proces cu un singur fir de executie: apeluriblocante, executiesecventiala • Cu automat finit (bazatpeeveniment) apelurineblocantesiparalelism

  23. Sistem de operare

  24. Thread-uriutilizator • Thread-urilesuntgestionate de o biblioteca de thread-uri

  25. Thread-uri la nivel kernel • Kernel-ulesteconstient de existenta thread-uri

  26. Procese “usoare” (Light-weight-LWP) • Presupunmapareamapareamaimultor LWP peste un proces real (greu - heavy-weight)

  27. Sisteme Multitasking Proces Spatiu Utilizator Spatiu Kernel StructuraProcesului UNIX Hardware (UNIX, VMS, MVS, NT, OS/2 etc.)

  28. Sisteme Multitasking Procese P3 P4 P1 P2 kernel Hardware

  29. Procese Multithread T1’s SP T3’sPC T1’sPC T2’sPC T1’s SP Cod Utilizator Date globale T2’s SP StructuraProcesului Kernel

  30. Maparea thread-urilor 1:1 DEC, NT, OS/1, AIX. IRIX M:1 HP-UNIX M:M 2-level

  31. Modelulpedouanivele al SunOS Proces Traditional Proc 1 Proc 2 Proc 3 Proc 4 Proc 5 Utilizator LWP-uri Thread-uri Kernel Kernel Hardware Procesoare

  32. Paralelism la nivel de thread-uri

  33. Multithreading – Mono procesor Concurenta • ConcuretaVs Paralelism P1 CPU P2 P3 timp

  34. Multithreading - Multiprocesor Concurenta Vs Paralelism CPU P1 CPU P2 CPU P3 timp

  35. Thread-uri la nivel user Procesoarevirtuale Procesoarefizice Model de lucru Planificarenivel user (User) Planificarenivel Kernel (Kernel)

  36. Architecturagenerala a modeluluibazatpe thread-uri • Ascundedetaliilearhitecturiimasinii • Mapeaza thread-urile user pestecele native ale kernel • Memoriaprocesuluiparinteestevazuta in comun de thread-uri

  37. Modele de programarefolosind thread-uri 1. master/slave Peer 3. pipeline

  38. Modelul master slave Program Resurse sclavi Fisiere taskX Baze Date Stapan taskY main ( ) Input (Stream) Disc-uri taskZ Dispozitive Speciale

  39. taskZ Modelul peer Program Resurse Sclavi Input (static) Fisiere taskX Baze Date taskY Disc-uri Dispozitive Speciale

  40. Pipeline de thread-uri Fisiere Fisiere Fisiere Baze date Baze date Baze date Disc-uri Disc-uri Disc-uri DispozitiveSpeciale DispozitiveSpeciale DispozitiveSpeciale Program Filtru Thread-uri Nivel1 Nivel2 Nivel3 Input (Stream) Resurse

  41. Observatiiprivindutilizarea thread-urilor • Dacatoateoperatiilesunt cu maiconsumatoare de procesor nu esterecomandatsa se lucrezepe thread-uri • Desicreareaestesimplatotusifolosesteeainsasinisteresurse • Thread-urileprea simple (5 linii) nu sunteficiente

  42. Detalii fire de executie

  43. Cai de creare a unui thread in Java class MyThread extends Thread { public void run() { // corpul thread cetrebuieexecutat } } MyThread thr1 = new MyThread(); thr1.start();

  44. Se creaza o clasa care implementeazainterfata Runnable class ClassName implements Runnable { ..... public void run() { // corpul thread cetrebuieexecutat } } ClassNamemyObject = new ClassName(); Thread thr1 = new Thread( myObject ); thr1.start();

  45. Exemplul 2 class 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(InterruptedExceptione) { System.out.println("Interrupted."); } System.out.println("Exiting mainthread."); }

  46. public void run() { try { for(inti=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(); } }

  47. Gestiunea thread-uluicurent class CurrentThreadDemo { public static void main(String arg[]) { Thread ct = Thread.currentThread(); ct.setName( "My Thread" ); System.out.println("Current Thread : "+ct); try { for(inti=5; i>0; i--) { System.out.println(" " + i); Thread.sleep(1000); } }

  48. catch(InterruptedException e) { System.out.println("Interrupted.") } } } Run: Current Thread : Thread[My Thread,5,main] 5 4 3 2 1

  49. Ciclul de viata al unui thread

  50. Runnable Thread-uleste lansat în execuţie prin apelul metodei start() din clasa Thread. • NotRunnable - se poate ajunge în această stare dacă:

More Related