CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung...

30
Motivation (GP)GPU CUDA Zusammenfassung CUDA und Python Christian Wilms Integriertes Seminar Projekt Bildverarbeitung Universit¨ at Hamburg WiSe 2013/14 12. Dezember 2013 Christian CUDA und Python 1

Transcript of CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung...

Page 1: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

CUDA und Python

Christian Wilms

Integriertes SeminarProjekt Bildverarbeitung

Universitat HamburgWiSe 2013/14

12. Dezember 2013

Christian CUDA und Python 1

Page 2: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Gliederung

1 Motivation

2 (GP)GPU

3 CUDA

4 Zusammenfassung

Christian CUDA und Python 2

Page 3: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Ubersicht

1 Motivation

2 (GP)GPU

3 CUDA

4 Zusammenfassung

Christian CUDA und Python 3

Page 4: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Wie mache ich mein Programm schneller?

Bisherige Moglichkeiten

Interpreter wechseln

tlw. Programmiersprache wechseln

Aber was ist mit den ganz dicken Brettern?⇒ Wir brauchen schweres Gerat!

Weitere Moglichkeit: Wechsel der Architektur

Multi-Core CPU nutzen

GPU

Multi-Core CPU + GPU

Christian CUDA und Python 4

Page 5: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Welche Architekturen gibt es?

Abbildung 1: SISD

Christian CUDA und Python 5

Page 6: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Welche Architekturen gibt es?

Abbildung 2: MISD

Christian CUDA und Python 6

Page 7: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Welche Architekturen gibt es?

Abbildung 3: MIMD

Christian CUDA und Python 7

Page 8: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Welche Architekturen gibt es?

Abbildung 4: SIMD

Christian CUDA und Python 8

Page 9: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Ubersicht

1 Motivation

2 (GP)GPU

3 CUDA

4 Zusammenfassung

Christian CUDA und Python 9

Page 10: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Warum ist die GPU so schnell?

CPU

wenige Kerne

schnelle Kerne

muss alles konnen

minimale Latenz jeThread

großer Cache

ausgefallene Logik

GPU

viele Kerne

langsamere Kerne

Datenstrom durch Stufen

spezielleHardwarefunktionen

kaum Cache

maximaler Durchsatz

⇒ Eine handelsubliche Grafikkarte hat deutlich mehr Kerne als einentsprechender Multi-Core-PC (bis zu 3000).

Christian CUDA und Python 10

Page 11: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Warum ist die GPU so schnell?

Abbildung 5: CPU vs. GPU [5]

Christian CUDA und Python 11

Page 12: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

GPGPU als Stein der Weisen?

Starken

sehr viele Threads konnen gleichzeitig arbeiten ⇒ MPPmoglich

recht preiswerte Rechenleistung

Schwachen

alle Threads arbeiten den selben Code ab

vereinfachte Logik

Konditionale und Schleifen tlw. sehr aufwendig

Kommunikation zwischen Threads aufwendig

Datenstrukturen sollten zur GPU passen

Christian CUDA und Python 12

Page 13: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Wie kann ich die GPU einbinden?

Fruher

starke Orientierung an der Grafikpipeline

sehr limitierte Moglichkeiten

kaum Kontrollfluss außer Sequenz

basierend auf Grafik-APIs

Heute

Architektur ist flexibler

CUDA (NVIDIA), Stream (ATI), ...

weiterhin Geschwindigkeitseinbußen durch bestimmteStrukturen

⇒ Die Mittel mussen zur Karte passen, der Code auch!

Christian CUDA und Python 13

Page 14: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Ubersicht

1 Motivation

2 (GP)GPU

3 CUDA

4 Zusammenfassung

Christian CUDA und Python 14

Page 15: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Was ist CUDA?

Plattform zur Entwicklung von Programmteilen (ausschließlich) furNVIDIA-Karten.

unterstutzt ubliche Programmstrukturen

Nutzung als GPGPU wird ermoglicht

Nutzung der Special Function Units

Programmierung in CUDA C/C++ oder CUDA Fortran

Bindings fur viele Sprachen vorhanden, auch Python(PyCUDA, NumbaPro,...)

weitere Bibliotheken und Tools vorhanden

Meist werden nur (geeignete) Teile des Programms in CUDAausgelagert.

Christian CUDA und Python 15

Page 16: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Wie arbeitet CUDA?

Abbildung 6: [6]

Daten auf die Karte laden

Kernel auf die Karte laden

ein Stapel von Threadswird starteten

alle Threads fuhren denselben Kernel aus

die Daten von der Karteladen

Christian CUDA und Python 16

Page 17: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Wie arbeitet CUDA?

Abbildung 7: [5]

jeder Thread hat eigenenSpeicher

Threads treten immer inBlocken auf

jeder Block hat einengemeinsamen Speicher

Blocks sind in Grid angeordnet

keine Kommunikation uberBlockgrenzen

alle Grids haben einenKontextspeicher (von außenzugreifbar)

Christian CUDA und Python 17

Page 18: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Ein PyCUDA-Beispiel I [3]

1 import pycuda . d r i v e r as cuda2 import pycuda . a u t o i n i t3 from pycuda . c o m p i l e r import SourceModule4

5 a = numpy . random . randn ( 4 , 4 )6 a = a . a s t y p e ( numpy . f l o a t 3 2 )7 a gpu = cuda . mem al loc ( a . n b y t e s )8 cuda . memcpy htod ( a gpu , a )9

10 f u n c = mod . g e t f u n c t i o n ( ” d o u b l i f y ” ) # Kerne l11 f u n c ( a gpu , b l o c k =(4 ,4 ,1) )12

13 a d o u b l e d = numpy . e m p t y l i k e ( a )14 cuda . memcpy dtoh ( a doub led , a gpu )

Christian CUDA und Python 18

Page 19: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Ein PyCUDA-Beispiel II [3]

1 mod = SourceModule ( ”””2 g l o b a l v o i d d o u b l i f y ( f l o a t *a )3 {4 i n t i d x = t h r e a d I d x . x + t h r e a d I d x . y *4 ;5 a [ i d x ] *= 2 ;6 }7 ””” )

Aufruf:

10 f u n c = mod . g e t f u n c t i o n ( ” d o u b l i f y ” ) # Kerne l11 f u n c ( a gpu , b l o c k =(4 ,4 ,1) )

Christian CUDA und Python 19

Page 20: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Das Wichtigste in der Ubersicht I

Kontext erzeugen, die Karte als Device registrieren und CUDAvorbereiten.

2 import pycuda . a u t o i n i t

Speicher allokieren und Daten auf die Karte schreiben (Host toDevice - htod).

7 a gpu = cuda . mem al loc ( a . n b y t e s )8 cuda . memcpy htod ( a gpu , a )

Christian CUDA und Python 20

Page 21: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Das Wichtigste in der Ubersicht II

Definierte Kernelfunktion holen und mit Parametern sowieThread-Blockgroße aufrufen. Auch Grid-Große u.a. kannspezifiziert werden.

10 f u n c = mod . g e t f u n c t i o n ( ” d o u b l i f y ” ) # Kerne l11 f u n c ( a gpu , b l o c k =(4 ,4 ,1) )

Daten von der Karte kopieren (Device to Host - dtoh).

14 cuda . memcpy dtoh ( a doub led , a gpu )

Alternative:

11 f u n c ( cuda . InOut ( a ) , b l o c k =(4 , 4 , 1) )

Christian CUDA und Python 21

Page 22: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Das Beispiel in ultrakompakter Schreibweise mit GPUArray

1 import pycuda . g p u a r r a y as g p u a r r a y2 import pycuda . d r i v e r as cuda3 import pycuda . a u t o i n i t4 import numpy5

6 a gpu = g p u a r r a y . t o g p u ( numpy . random . randn( 4 , 4 ) . a s t y p e ( numpy . f l o a t 3 2 ) )

7 a d o u b l e d = (2* a gpu ) . g e t ( )

Viele weitere klassische Operationen verfugbar, tlw. auch inanderen Paketen wie pycuda.cumath.

Christian CUDA und Python 22

Page 23: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Ein komplexerer Kernel [4]

1 k e r n e l = SourceModule ( ”””2 g l o b a l v o i d t h r e s h o l d ( f l o a t *x , i n t l e n )3 {4 i n t i d x = b l o c k I d x . x * blockDim . x +

t h r e a d I d x . x ;5 i n t numThreads = blockDim . x * gr idDim . x ;6 f o r ( i n t i = i d x ; i < l e n ; i+=numThreads )7 x [ i ] = −1 < x [ i ] && x [ i ] < +1 ? 1 . 0 : 0 . 0 ;8 } ””” )

Aufruf:

1 t h r e s h o l d = k e r n e l . g e t f u n c t i o n ( ’ t h r e s h o l d ’ )2 t h r e s h o l d ( x gpu , l e n ( x ) , b l o c k =(256 ,1 ,1) , g r i d

=(16 ,1) )Christian CUDA und Python 23

Page 24: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Was heißt schneller? [7]

Bildgroße CPU(ms) GPU(ms) Speedup

5120x5120 1746.40 37.29 46.81600x1200 128.51 3.25 39.51024x816 54.60 1.79 30.5512x512 17.53 0.93 18.8320x408 8.83 0.97 9.1

Tabelle 1: Histogram-Equalization

Bildgroße CPU (ms) GPU (ms) Speedup

4096x4096 31925.99 402.01 79.4700x525 1745.02 21.98 79.7

Tabelle 2: Wolkenentfernung

Christian CUDA und Python 24

Page 25: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Was heißt schneller? [1]

Abbildung 8: Hough-Transformation [1]

Christian CUDA und Python 25

Page 26: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Ubersicht

1 Motivation

2 (GP)GPU

3 CUDA

4 Zusammenfassung

Christian CUDA und Python 26

Page 27: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Was soll ich mitnehmen?

SIMD-Architektur als Grundlage der GPU

Schnelligkeit durch viele spezialisierte Prozessoren und kleinesAufgabenspektrum

Probleme mit komplexeren Kontrollstrukturen und großerKommunikation

CUDA/PyCUDA ermoglicht einfache Programmierung underweitere Moglichkeiten

eigene Kernels in CUDA C bieten große Moglichkeiten

Abstraktion durch GPUArrays in PyCuda

⇒ Vieles lasst sich mit der GPU stark beschleunigen, aber nichtalles!

Christian CUDA und Python 27

Page 28: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Quellen I

S. Chen, H. JiangAccelerating the Hough Transform with CUDA on GraphicsProecessing UnitsInternational Conference on Parallel and DistributedProcessung Techniques and Applications, 2011

M. GarriguesImage Processing on GPU with CUDA and C++ENSTA-ParisTech, 2011

A. KloecknerTutorial - PyCUDAPyCUDA Documentation, 2013

Christian CUDA und Python 28

Page 29: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Quellen II

M. Lazaro-GredillaAn introduction to CUDA using PythonUniversidad Carlos III de Madrid, 2013

T. O’NeilCUDA LectureUniversity of Akron, 2011

TosakaCUDA processing flowWikipedia

Z. Yang, Y. Zhu, Y. Pu.Parallel Image Processing Based on CUDAInternational Conference on Computer Science and SoftwareEngineereing, 2008

Christian CUDA und Python 29

Page 30: CUDA und Python - uni-hamburg.deseppke/...2013/12/12  · Nutzung als GPGPU wird erm oglicht Nutzung der Special Function Units Programmierung in CUDA C/C++ oder CUDA Fortran Bindings

Motivation (GP)GPU CUDA Zusammenfassung

Quellen III

C. ZellerTutorial CUDANVIDIA Developer Technology, 2008

Christian CUDA und Python 30