36 Introduction à CUDA gael.guennebaud@inria.fr
38 Comment programmer les GPU? Notion de kernel exemple (n produits scalaires): T ci =ai b ( ai, b : vecteurs 3D, ci for(int i=0;i<n;++i) { c[i] = a[i].x * b.x + a[i].y * b.y + a[i].z * b.z; Comment définir les kernels?? Architectures spécifiques compilateurs spécifiques langages spécifiques? boucle sur les coefficients 1 donnée = 1 thread kernel : scalaire)
39 CUDA versus OpenCL OpenCL Solution initiée par Apple, ouvert à la OpenGL Ce veut générique (CPU/GPU) Approche à la OpenGL/GLSL Langage proche du C CUDA = «Compute Unified Device Architecture» Cuda c'est : un langage (C/C++ plus extensions et restrictions) un compilateur (nvcc) des bibliothèques : cuda et cudart (run-time) drivers interfaces pour Fortran, python, MatLab, Java, etc. Supporte uniquement le matériel Nvidia (GPU et Tesla)
40 OpenCL application source code (char*) commandes - compilation - exécution - copies des données - etc. OpenCL library Drivers Hardware (GPU ou CPU)
41 CUDA C/C++ cuda source code.cu common headers.h cuda compiler nvcc GPU assembly.ptx host code OCG GCC/CL object file.o object file.o.cpp application source code.c,.cpp GCC/CL object file.o
42 CUDA : qques notions de bases Compute device (co)processeur avec DRAM et nombreux threads en parallèle typiquement GPU Device kernel fonction principale exécutée sur un compute device par de nombreux cœurs préfixé par global : global void my_kernel(...) {
43 Types de fonctions Différent types de fonctions : Executed on the: Only callable from the: device float DeviceFunc(); device device global void device host host host host KernelFunc(); float HostFunc(); On peut avoir host et device en même temps global = kernel, retourne void (similaire aux main() des shaders) device : fonction sans adresse, pas de récursion, pas de variable static local à la fonction, pas de nombre variable d'arguments
44 CUDA : qques notions de bases Paradigme : tableau de threads (1D, 2D, ou 3D) chaque thread a son propre numéro (ID) utilisé pour accéder aux données et savoir quoi faire struct Vec3 { float x, y, z ; ; threadid float x = input[threadid]; float y = func(x); output[threadid] = y; global void my_kernel( const Vec3* a, Vec3 b, float* c) { int i = threadid; c[i] = a[i].x * b.x + a[i].y * b.y + a[i].z * b.z; Host : générer n threads exécutant my_kernel(a, b, c) ;
45 Thread Blocks Les threads sont regroupés en blocs Les threads d'un même bloc peuvent coopérer : mémoire partagée, opérations atomiques, synchronisation (barrière) ils sont exécutés sur un même SM nb threads/bloc >> nb cores d'un SM Les threads de deux blocs différents ne peuvent pas communiquer! les blocs sont exécutés dans un ordre arbitraire plusieurs blocs peuvent être assignés à un même SM exécuté de manière indépendante sur plusieurs SM Thread Block 1 Thread Block 0 threadid 0 1 2 3 4 5 6 float x = input[threadid]; float y = func(x); output[threadid] = y; 7 0 1 2 3 4 5 6 Thread Block N - 1 7 float x = input[threadid]; float y = func(x); output[threadid] = y; 0 1 2 3 4 5 6 7 float x = input[threadid]; float y = func(x); output[threadid] = y;
46 Block ID et Thread ID threadidx = numéro du thread dans le bloc courant (3D) blockidx =numéro du bloc dans la grille (2D) Global thread ID = Block ID (1D ou 2D) + Thread ID (1D, 2D, 3D) Choix du nombre de threads / bloc par l'utilisateur nd simplifie les accès mémoires pour les données multi-dimensionelles Traitement d'image Image volumique équations différentielles
47 Appeler un kernel cuda Un kernel est appelé avec une configuration d'exécution appelé dans dans un fichier.cu : global void my_kernel(...) ; dim3 DimGrid(100,50) ; // 100*50*1 = 5000 blocks dim3 DimBlock(4,8,8) ; // 4*8*8 = 256 threads / blocks my_kernel<<< DimGrid, DimBlock >>> ( ) ; exécution asynchrone
48 Gestion de la mémoire cudamalloc() Grid alloue de la mémoire globale sur le GPU libérée avec cudafree() Block (0, 0) Block (1, 0) Shared Memory Registers Registers Thread (0, 0) Thread (1, 0) Host int n = 64 ; float* d_data ; // convention d_ pour device int size = 64*64*sizeof(float) cudamalloc((void**)&d_data, size) ;... cudafree(d_data) ; Global Memory Shared Memory Registers Registers Thread (0, 0) Thread (1, 0)
49 Transfert de données cudamemcpy() transferts asynchrones 4 paramètres ptr de destination ptr de source nombre d'octets type : cudamemcpy*...hosttohost...hosttodevice...devicetodevice...devicetohost Grid Block (0, 0) Block (1, 0) Shared Memory Registers Registers Thread (0, 0) Thread (1, 0) Host Global Memory float* h_data = malloc(sizeof(float)*n*n) ; h_data = ; cudamemcpy(d_data, h_data, size, cudamemcpyhosttodevice) ;... cudamemcpy(h_data, d_data, size, cudamemcpydevicetohost) ; Shared Memory Registers Registers Thread (0, 0) Thread (1, 0)
50 Exemple I Calculer n produits scalaires global void many_dots_kernel(const Vec3* a, Vec3 b, float* c) { int i = threadidx.x; c[i] = a[i].x * b.x + a[i].y * b.y + a[i].z * b.z; host void many_dots(const std::vector<vec3>& a, const Vec3& b, std::vector<float>& c) { Vec3* d_a; // d_ for device float* d_c; int n = a.size() ; cudamalloc((void**)&d_a, n*sizeof(vec3)); cudamalloc((void**)&d_c, n*sizeof(float));!! un problème!! cudamemcpy(d_a, &a[0].x, n*sizeof(vec3), cudamemcpyhosttodevice) ; dim3 DimGrid(1,1); dim3 DimBlock(n,1); many_dots_kernel<<< DimGrid, DimBlock >>> (d_a, b, d_c); cudamemcpy(&c[0], d_c, n*sizeof(float), cudamemcpydevicetohost) ; cudafree(d_a); cudafree(d_c) ;
51 Exemple I Calculer n produits scalaires global void many_dots_kernel(const Vec3* a, Vec3 b, float* c) { int i = threadidx.x; c[i] = a[i].x * b.x + a[i].y * b.y + a[i].z * b.z; host void many_dots(const std::vector<vec3>& a, const Vec3& b, std::vector<float>& c) { Vec3* d_a; // d_ for device float* d_c; int n = a.size() ; cudamalloc((void**)&d_a, n*sizeof(vec3)); cudamalloc((void**)&d_c, n*sizeof(float));!! un seul bloc un seul SM sera utilisé cudamemcpy(d_a, &a[0].x, n*sizeof(vec3), cudamemcpyhosttodevice) ; dim3 DimGrid(1,1); dim3 DimBlock(n,1); many_dots_kernel<<< DimGrid, DimBlock >>> (d_a, b, d_c); cudamemcpy(&c[0], d_c, n*sizeof(float), cudamemcpydevicetohost) ; cudafree(d_a); cudafree(d_c) ;
52 Exemple I Calculer n produits scalaires global void many_dots_kernel(const Vec3* a, Vec3 b, float* c) { int i = blockid.x * blockdim.x + threadidx.x; c[i] = a[i].x * b.x + a[i].y * b.y + a[i].z * b.z; host void many_dots(const std::vector<vec3>& a, const Vec3& b, std::vector<float>& c) { Vec3* d_a; // d_ for device float* d_c; int n = a.size() ; cudamalloc((void**)&d_a, n*sizeof(vec3)); cudamalloc((void**)&d_c, n*sizeof(float)); tiling cudamemcpy(d_a, &a[0].x, n*sizeof(vec3), cudamemcpyhosttodevice) ; dim3 DimGrid((n+511)/512,1); dim3 DimBlock(512,1); many_dots_kernel<<< DimGrid, DimBlock >>> (d_a, b, d_c); cudamemcpy(&c[0], d_c, n*sizeof(float), cudamemcpydevicetohost) ; cudafree(d_a); cudafree(d_c) ;
53 Exemple I Calculer n produits scalaires global void many_dots_kernel(int n const Vec3* a, Vec3 b, float* c) { int i = blockid.x * blockdim.x + threadidx.x; if(i<n) c[i] = a[i].x * b.x + a[i].y * b.y + a[i].z * b.z; host void many_dots(const std::vector<vec3>& a, const Vec3& b, std::vector<float>& c) { Vec3* d_a; // d_ for device float* d_c; int n = a.size() ; cudamalloc((void**)&d_a, n*sizeof(vec3)); cudamalloc((void**)&d_c, n*sizeof(float));! out of range! cudamemcpy(d_a, &a[0].x, n*sizeof(vec3), cudamemcpyhosttodevice) ; dim3 DimGrid((n+511)/512,1); dim3 DimBlock(512,1); many_dots_kernel<<< DimGrid, DimBlock >>> (n, d_a, b, d_c); cudamemcpy(&c[0], d_c, n*sizeof(float), cudamemcpydevicetohost) ; cudafree(d_a); cudafree(d_c) ;
54 Choisir la taille des blocs/grilles Choisir la taille des blocs/grilles warp = ensemble de threads exécutés en même temps sur un même SM taille = f(nombre de cores) ex : 32 threads / warp taille des blocs multiple de 32 1 seul warp actif à la fois par SM, mais échange entre les warps actifs et en pause afin de masquer les attentes objectif : maximiser le nombre de warp / SM si grand nombre de threads optimiser la taille des blocs (voir exemple) en déduire les dimensions de la grille sinon réduire la taille des blocs pour occuper tous les SM nombre maximal de threads par bloc < nb_total_threads / nb_sm nombres de blocs = multiple du nombre de SMs benchmark automatique pour optimiser les dimensions
55 Choisir la taille des blocs/grilles Exemple de GPU: G80 caractéristiques : 32 threads / warp 8 blocs max par SM 768 threads max par SM 15 SMs block sizes: 8x8 64 threads x8 512 threads < 768 non optimal 16x16 256 threads x3 768 threads optimal? 32x32 1024 threads x1 1024 > 768 impossible 32x8 pour la cohérence des accès mémoires En réalité plus complexe car dépend également de la complexité du kernel : nombres de registres limités nombre de threads/sm peut être largement inférieur
56 Comment paralléliser son code?
Comment paralléliser son code? Dépend de nombreux facteurs Nature du problème Dimensions du problème Hardware visé Pas de solution universelle Pas de solution automatique (compilateur) Acquérir de l'expérience étudier des cas «simples» retrouver des motifs connus au sein d'applications complexes Dans tous les cas 1 - identifier les taches indépendantes approche hiérarchique 2 - regrouper les taches trouver la bonne granularité
Défis Trouver et exploiter la concurrence regarder le problème sous un autre angle Computational thinking (J. Wing) nombreuses façons de découper un même problème en taches Identifier et gérer les dépendances dépend du découpage De nombreux facteurs influent sur les performances surcoût du au traitement parallèle charge non équilibrée entre les unités de calculs partage des données inefficace saturation des ressources (bande-passante mémoire)... 58
59 Exemple II Convolution Convolution discrète Application d'un filtre f de rayon r : r I [ x, y ]= r I [ x+ i, y+ j ] f [i, j ] i= r j= r Exemple de filtre Gaussien 3x3 : (filtre basse-bas, supprime les hautes fréquences, lissage/flou) [ ] 1 2 1 f=2 4 2 1 2 1 <démo> input 1 passe 3 passes
60 Exemple II Appliquer un filtre à une image global void apply_filter(int w, int h, float* img, float filter[3][3]) { int x = threadidx.x ; int y = threadidx.y ; if(x<w && y<h) { r img [ x, y]= r img [ x+ i, y+ i = r j= r j ] filter [i, j ] ;!! deux problèmes!! host void apply_filter(myimage& image, float filter[3][3]) { float* d_img; /* */ dim3 DimGrid(1,1) ; dim3 DimBlock(image.width(),image.height(),1) ; apply_filter<<< DimGrid, DimBlock >>> (image.width(), image.height(), d_img, filter) ; /* */
61 Exemple II Appliquer un filtre à une image global void apply_filter(int w, int h, float* img, float filter[3][3]) { int x = threadidx.x ; int y = threadidx.y ; if(x<w && y<h) { r img [ x, y]= r img [ x+ i, y+ i = r j= r j ] filter [i, j] ; host void apply_filter(myimage& image, float filter[3][3]) { float* d_img;!! un seul bloc un seul SM sera utilisé /* */ dim3 DimGrid(1,1) ; dim3 DimBlock(image.width(),image.height(),1) ; apply_filter<<< DimGrid, DimBlock >>> (image.width(), image.height(), d_img, filter) ; /* */
62 Exemple II Appliquer un filtre à une image global void apply_filter(int w, int h, const float* img, float filter[3][3]) { int x = blockidx.x*blockdim.x + threadidx.x ; int y = blockidx.y*blockdim.y + threadidx.y ; if(x<w && y<h) { r img [ x, y]= r img [ x+ i, y+ i = r j= r j ] filter [i, j] ; host void apply_filter(myimage& image, float filter[3][3]) { float *d_img; tiling /* */ dim3 DimBlock(16,16,1) ; dim3 DimGrid((image.width()+15)/16, (image.height()+15)/16) ; apply_filter<<< DimGrid, DimBlock >>> (image.width(), image.height(), d_img, filter) ; /* */
63 Exemple II Appliquer un filtre à une image global void apply_filter(int w, int h, const float* img, float filter[3][3]) { int x = blockidx.x*blockdim.x + threadidx.x ; int y = blockidx.y*blockdim.y + threadidx.y ; if(x<w && y<h) { r img [ x, y ]= r img [ x+ i, y+ i= r j= r j] filter [i, j]; host void apply_filter(myimage& image, float filter[3][3]) { float *d_img;!! lecture-écriture dans le même buffer avec aliasing résultat incorrect /* */ dim3 DimBlock(16,16,1) ; dim3 DimGrid((image.width()+15)/16, (image.height()+15)/16) ; apply_filter<<< DimGrid, DimBlock >>> (image.width(), image.height(), d_img, filter) ; /* */
64 Exemple II Appliquer un filtre à une image global void apply_filter(int w, int h, const float* input, float* output, float filter[3][3]) { int x = threadidx.x ; int y = threadidx.y ; if(x<w && y<h) { r output [ x, y ]= r input [ x+ i, y+ i= r j= r j ] filter [i, j ]; utiliser deux buffers! host void apply_filter(myimage& image, float filter[3][3]) { float *d_input, *d_output; /* */ dim3 DimBlock(16,16,1) ; dim3 DimGrid((image.width()+15)/16, (image.height()+15)/16) ; apply_filter<<< DimGrid, DimBlock >>> (image.width(), image.height(), d_input, d_output, filter) ; /* */
Décomposition en tache Ex : produit matriciel B WIDTH opération de base pour résoudre Ax=b C = A*B parallélisme naturel : 1 tache = calcul d'un élément P(i,j) = 1 produit scalaire A C WIDTH P(i,j) WIDTH WIDTH
N-body simulation Exercice on a N particules avec : masses : Mi vitesse : Vi position : Pi forces d'attractions entre 2 particules i et j : F i, j= gmim j 2 P i P j forces appliquées à une particule i : F i = j i F i, j on en déduit les nouvelles vitesses et nouvelles positions v i (t+ Δ t )=v i (t )+ F i Δ t pi (t + Δ t )= pi (t )+ v i (t )Δ t exemple : 5k particules 11 g =6.7 10 2 N m kg 2
N-body simulation Solution 1 étape 1 calculer les Fij dans un tableau 2D kernel = calcul de 1 Fij nombre de threads = N^2 étape 2 Limitations calcul des F i = j i F i, j et mise à jour des vitesses et positions kernel = calcul de 1 Fi nombre de threads = N coût mémoire : N^2 peu de parallélisme (N threads) Fij
N-body simulation Solution 2 étape 1 calculer les Fij dans un tableau 2D kernel = calcul 1 Fij nombre de threads = N^2 étape 2 réduction parallèle (somme) dans une direction???? étape 3 mise à jour des vitesses et positions Limitations coût mémoire : N^2 Fij
N-body simulation Solution 2 étape 1 - [MAP] calculer les Fij dans un tableau 2D kernel = calcul 1 Fij nombre de threads = N^2 étape 2 - [REDUCE] réduction parallèle (somme) dans une direction???? étape 3 - [MAP] mise à jour des vitesses et positions Limitations coût mémoire : N^2 Fij
Map & Reduce Premières opérations de bases : Map = appliquer la même fonction à chacun des éléments d'un tableau Parallélisme évident : 1 thread 1 élément Reduce = réduire un tableau (ou sous-tableau) à une seule valeur Exemples : somme, produit, min, max, etc. Comment réaliser cette opération avec un maximum de parallélisme? exercice!
Shared Memory Ex : Réduction revisitée int i = threadidx.x; int offset = 2*blockIdx.x*blockDim.x; shared float partialsum[n1] ; partialsum[i] = data[offset+i]; partialsum[i+blockdim.x] = data[offset+i+blockdim.x]; for(int stride=blockdim.x; stride>0 && i<stride; stride >> 1) { syncthreads(); partialsum[i] = partialsum[i] + partialsum[i+stride]; if(t==0) data[blockidx.x] = partialsum[0] ; approche à combiner avec le N-body simulation...
Réduction Exercice : calcul du min/max algo récursif, n/2 threads synchronisation entre les passes log(n) passes différentes stratégies : inplace ping-pong ; accès mémoires
N-body simulation Solution 2 étape 1 calculer les Fij dans un tableau 2D kernel = calcul 1 Fij nombre de threads = N^2 étape 2 réduction parallèle (somme) dans une direction Fi (tableau 1D) log2(n) passes kernel = F(i,j)+F(i,j+1) nb threads : N/2, N/4, N/8, N/16,... étape 3 mise à jour des vitesses et positions Limitations coût mémoire : N^2 kernels très (trop?) simples Fij