파이썬 병렬프로그래밍

Click here to load reader

Upload: yong-joon-moon

Post on 06-Jan-2017

987 views

Category:

Software


0 download

TRANSCRIPT

PowerPoint

Python Moon Yong Joon

1

1. . Parallel computers 2. Threading 3. Multiprocessing Moon Yong Jooncontents2

1. Parallel computersMoon Yong Joon3

Parallel computers Moon Yong Joon4

Parallel computers5

Parallel computersMultiprocessor/multicore: several processors work on data stored in shared memory

Cluster: several processor/memory units work together by exchanging data over a network

Co-processor: a general-purpose processor delegates specific tasks to a special-purpose processor (GPU, FPGA, ...)

Other: - Grid computing - Cluster of multicore nodes with GPUs - NUMA (non-uniform memory access) architecture6

grid computing , (computation intensive jobs) (data intensive jobs) .7

Parallelism vs. concurrencyParallelism: use multiple processors to make a computation faster.Concurrency: permit multiple tasks to proceed without waiting for each other.

Different goals that share implementation aspects. Scientific computing cares more about parallelism.Concurrency is rarely needed.8

concurrency 9

HPC10

GPU GPU (GPU) CPU , , ,

11

grid computing , (computation intensive jobs) (data intensive jobs) .12

grid computing (Computational Grid): CPU GPU .

(Data Grid): .

(Access Grid): .

(Equipment Grid) .13

Parallel Programming

Moon Yong Joon14

Parallel Programming15

Parallel Programming ., 16

Parallel ProgrammingDecomposition of the complete task into independent subtasks and the data flow between them.Distribution of the subtasks over the processors minimizing the total execution time.For clusters: distribution of the data over the nodes minimizing the communication time.For multiprocessors: optimization of the memory access patterns minimizing waiting times.Synchronization of the individual processes17

parallel programming problems18

DeadlockDeadlock:- Two processes are waiting for each other to finish.Usually caused by locks or by blocking communication.19

StarvationStarvation:- Now, imagine that a process A with high priority constantly consumes the CPU, while a lower priority process B never gets the chance. 20

Race conditionRace condition:- Two or more processes modify a shared resource (variable, file, ...)- Result depends on which process comes first.- Can be avoided using locks, but...- ... handling locks is very difficult and mistakes often cause deadlocks.21

Sync/async22

Sync/async 23

Shared memory communication24

Shared memory Shared memory (threading, multiprocessing)Requires locks for safe modification

25

Message passing communication26

Queue programmingQueue 27

pipeline programmingTask pipeline input 28

Moon Yong Joon29

process30

A process consists of a block of memory some executable code one or more threads that execute code independently but work on the same memoryProcess A running program Keeps track of current instruction and data Single-core processor: only one process actually runs at a time Many processes active at once OS goes from one to another via a context switch

31

core CPU

32

MultiprocessingMultiprocessing: using multiple processes with separate memory spaces for concurrency or parallelism

(symmetric) : cpu

(Asymmetric) : CPU . CPU CPU ( )

33

Thread 34

Thread (thread) , . , .

(multithread) .35

Thread CPU

36

MultithreadingMultithreading: using multiple threads in the same process for concurrency or parallelism

37

38

Thread : 2 Thread python

(User-Level Thread) , .

, .

.39

Thread : 2 Thread python

40 (Kernel-Level Thread) , .

, .

. , .

41

Thread Thread . ID, , , . , , . , (Thread-Specific Data, TSD) . , . , ID , TSD . TSD .42

43

fork : fork , fork . exec : fork , exec . fork . , fork exec 44

Global Interpreter LockMoon Yong Joon45

Global Interpreter Lock46

GIL : Global Interpreter Lock , , global global (Cpython )47

Python vs. OthersPython process Others process

48

Lock 49

GIL : Global Interpreter LockGIL GIL release 50

: http://www.dabeaz.com/python/UnderstandingGIL.pdf

Lock switching GIL execute lock . I/O lock 51 : http://www.dabeaz.com/python/UnderstandingGIL.pdf

High priority Thread 100tick check high priority Thread 52 : http://www.dabeaz.com/python/UnderstandingGIL.pdf

GIL 53

GIL thread GIL single thread (thread) (thread) 54

singlethread : GIL single thread 55

Mutlithread : Multi threading single thread 2 GIL 56

2. ThreadingMoon Yong Joon57

ThreadingThread class Moon Yong Joon58

Thread class 59

Thread class threading.Thread 60

Thread class threading.Thread name, ident(thread id), daemon 61

Daemon thread62

Daemon Thread , . . .63

Daemon Thread threading.Thread daemon True daemon 64

Thread class 65

Thread (thread) Thread class .66

Thread class threading.Thread thread.start() : thread start thread.join() : thread Thread.run() : subclass getName(), setName() : thread name is_alive() : thread isDaemon(), setDaemon() : daemon 67

Thread class threading.Thread /

68

Thread instance 69

Thread threading.Thread . subclass run

70

Thread threading.Thread

{'_stderr': ,

'_name': 'Thread-13',

'_target': ,

'_daemonic': False,

'_initialized': True,

'_ident': None,

'_is_stopped': False,

'_kwargs': {},

'_tstate_lock': None,

'_started': ,

'_args': ('name', 'config')}

71

Subclass subclass target , args, kwars run _target

72

ThreadingTaskMoon Yong Joon73

Task 74

Task (tasks) . , () , , . .75

Task . 76subclassCallable

Task thread 77

Task dict 78

f exec f exec

{: 'bbb,456', : 'aaa,123\n'}

Thread 79

Thread timeit thread 80

Thread timeit thread 3 default 81

Thread 82

Thread args tuple 83

Subclass.run Thread subclass thread target run run args

84

Threading /Moon Yong Joon85

86

threading.Thread /

87

threading.Thread /

88

Subclass : override89

thread threading.Thread run thread.run :

start run 90

Subclass : run override Thread subclass run method

91

Subclass : super class 92

Subclass : super class Thread subclass Thread class __init__ thread 93

Subclass : super class Thread __init__ start run() 94

Callable 95

callable Callable class __call__

96

Threading Moon Yong Joon97

98

threading 99

main_thread() main thread

100

active_count() thread

101

currentThread() thread

102

enumerate() thread list

103

Thread Moon Yong Joon104

105

Thread thread subclass __init__ Thread __init__

106

Thread Thread

107

Subcall 108

Thread thread subclass __init__ Thread __init__

109

Thread Thread

110

Threading Thread Moon Yong Joon111

Thread 112

Thread _start_new_threadthread() thread Thread class thread._start_new_thread(func, args, kwargs=None) func = thread args = func kwargs = 113

Thread _start_new_threadthread() thread

114

ThreadingcommunicationMoon Yong Joon115

Queue 116

Queue : put/get Queue 117

Share memory 118

Globale thread (GIL interrupt )119

thread 120

Queue 1121

queue thread (GIL interrupt )122

thread 123

Queue 2124

Queue : Put/get Queue 125

Thread : thread Queue put/get 126

thread Queue put thread get 127

Threading Moon Yong Joon128

129

1305 Lock : .

RLock. : .

Event : "" . .

Condition : . . - , .

Semaphore. : ; . 0 .

Lock 131 locking

ThreadingLock Moon Yong Joon132

interrupt 133

Interrupt 134Interrupt

Interrupt 135Interrupt

Lock 136

Lock release 137Lock release

Lock release 138Lock release

Lock : with 139

With 140 with aquire/release

Lock : subclass141

Lock Class 142 TOTAL Lock Locking

Lock Class 143 thread locking locking

Lock : decorator144

Lock Class 145Lock interrupt Lock

Lock Class 146 thread lock time.sleep() lock

ThreadingRLock Moon Yong Joon147

: reentrant148

RLock : 149 Rlock

RLock : 150

count acquire/release

Lock/RLock 151

Lock152 Lock

Lock count

Threadingsemaphore Moon Yong Joon153

Semaphore 154

semaphore155

Semaphore 156

BoundedSemaphore 157

Semaphore 158

Semaphore 159

Semaphore 160

Threadingcondition Moon Yong Joon161

: Condition162

Condition Class , . . Lock Condition object : , . wait : . , . notifyAll : , RuntimeError .163

Condition Class 1

164

Condition Class 2

165

3. MultiprocessingMoon Yong Joon166

ProcessclassMoon Yong Joon167

Process class168

Process Process class target , args, kwargs Procss(target=, name=None, args=, kwargs=, *, daemon=None)

169

Process : args() args args=()

Target doubler(number) target=doubler, args=(number,) 170

Process class 171

Process class 1Process class

172

Process class 2 authkey, daemon, name, ident, pid, sentinel

173

Process class 174

Process class name default

175

Process class name default

176

Process instance 177

Process instance 1Process class process

178

Process instance 2Process class process

(1, 2, 3) {'a': 1, 'b': 2}

{ '_popen': None, '_name': 'worker 1', '_target': , '_kwargs': {'a': 1, 'b': 2}, '_parent_pid': 12800, '_args': (1, 2, 3), '_config': { 'authkey': '\xca\xa2\x02\x19\xaaG\x88\xc8\x91c\xa2\xb9\xd2\xe9fK\x88\xc7\xeco^\xce\x13z\xc1\xc6\xfd\x11\xd6\xa89\xe0', 'semprefix': '/mp }, '_identity': (1,)}179

Process Moon Yong Joon180

Process 181

Process :

182

Process 183

Process :

Process class 3

184

ProcessDaemon Moon Yong Joon185

Process instance186

daemon processing daemon process

187

daemon processing daemon process is_alive

188

Processterminate Moon Yong Joon189

Process 190

terminamte process termiante

191

Daemon Process 192

terminamte daemon process termiante

sys.exit() 193

Process Moon Yong Joon194

195

current_process 1Pool process map

196

current_process 2 process

197

(, process ) Moon Yong Joon198

Process 199

Process target

200

Process /Process targer imort start

201

Process Pool Moon Yong Joon202

Pool203

Pool classPool class processes precess pool object

Pool(processes=None, initializer=None, initargs=(), maxtasksperchild=None)204

pool Pool

205

pool Pool 4 206

Pool.map 207

map map (iterable ) list

208

map : task ( data)

209

map :

210

map 211

Pool.map_async 212

process Pool Pool process map_async

213

process Pool processquffh

214

callback Pool process map_async callback

215

Pool.apply 216

Pool.apply()Pool apply_aync()

217

Pool.apply() Pool apply()

218

Pool.apply_async :callback219

process Pool Pool process apply_async

220

process Pool process

221

Pool.apply_async : get222

apply_async : pool.apply_async proxy .get()

223

apply_async :

224

Pool.apply_async()Pool apply_async() get

225

process Pool process

226

Processcommunicationshared MemoryMoon Yong Joon227

Value228

Value process Value

229

process Value

230

Processcommunicationqueue Moon Yong Joon231

Queue 232

Queue deque Process queue deque

233

process put/get 234

Queue: Process queue

235

Queue: Process queue

236

Process 237

Queue: process Process queue

238