parallel programming using python
TRANSCRIPT
![Page 1: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/1.jpg)
Introduction to Parallel Programming
using Python Presented by: Samah Gad
July 11, 2013
Friday, July 12, 13
![Page 2: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/2.jpg)
Road MapMotivationForking ProcessesThreadsInterprocess Communication - OverviewThe multiprocessing Module - Overview
Friday, July 12, 13
![Page 3: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/3.jpg)
Motivation
Problem:Most computers spend a lot of time doing nothing.Majority of the modern CPU’s capacity is often spent in an idle state.
Friday, July 12, 13
![Page 4: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/4.jpg)
Motivation -Cont.Solution:
Running more than one program at a time.
Dividing the CPU attention among a set of tasks.Parallel Processing, Multiprocessing, or Multitasking.
Friday, July 12, 13
![Page 5: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/5.jpg)
Parallel Processing in Python
Two main ways to run tasks:Process forks Spawned threads
Python built-in tools like: os.fork, threading, queue, and multiprocessing.Third Party domains offers more advanced tools.
Friday, July 12, 13
![Page 6: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/6.jpg)
Forking ProcessesTraditional ways to structure parallel tasks.Straight forward way to start an independent program.What is forking?
Copying programs.Python Module - os.fork
Friday, July 12, 13
![Page 7: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/7.jpg)
Example 1
Friday, July 12, 13
![Page 8: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/8.jpg)
Example 2
Friday, July 12, 13
![Page 9: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/9.jpg)
Threads
Another way to start activities running at the same time.Lightweight processes
Run within the same single process.
Friday, July 12, 13
![Page 10: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/10.jpg)
Threads - Advantages:
PerformanceSimplicityShared global memoryPortability
Friday, July 12, 13
![Page 11: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/11.jpg)
Python ModulesPython Modules:
_thread modulethreading modules
Both modules provide tools for synchronizing access to shared objects with locks.
Friday, July 12, 13
![Page 12: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/12.jpg)
The _thread Module
Start new independent threads of execution within a process.Doesn't support OOPPlatform independent module.
Friday, July 12, 13
![Page 13: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/13.jpg)
Example 3
Friday, July 12, 13
![Page 14: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/14.jpg)
Example 4
Friday, July 12, 13
![Page 15: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/15.jpg)
Synchronizing access to shared objects and names
What is the problem? Objects and namespaces in a process that span the life of threads are shared by all spawned threads.
Solution: Threads automatically come with a cross-task communications
Friday, July 12, 13
![Page 16: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/16.jpg)
Example 5
Friday, July 12, 13
![Page 17: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/17.jpg)
Threading Module
Internally uses the _thread module to implement objects that represent threads and common synchronization tools.Manage threads with high-level class-based objects.
Friday, July 12, 13
![Page 18: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/18.jpg)
Interprocess Communication - Overview
Other solutions don’t support cross-program communication Sockets, Pipes, and SignalsEnable performing Inter-Process Communication (IPC)
Friday, July 12, 13
![Page 19: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/19.jpg)
The multiprocessing Module - Overview
Provide the best of processes and threads.Platform independent.Uses processes instead of threads.Provide synchronizations tools.Leverage the capacity of multiple processors.
Friday, July 12, 13
![Page 20: Parallel programming using python](https://reader034.vdocuments.site/reader034/viewer/2022052321/5549f4dcb4c90512488b56f9/html5/thumbnails/20.jpg)
Reference
Title: Programming PythonAuthor: Mark LutzPublisher:O'Reilly Media; Fourth Edition edition (January 7, 2011)
Friday, July 12, 13