INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE...

145
INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDA Francisco Igual Peña León, 10 de mayo de 2011 miércoles 11 de mayo de 2011

Transcript of INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE...

Page 1: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

INTRODUCCIÓN A LA PROGRAMACIÓN DE

GPUS CON CUDA

Francisco Igual Peña

León, 10 de mayo de 2011

miércoles 11 de mayo de 2011

Page 2: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PLANIFICACIÓN

1. Miércoles: Conceptos introductorios

1. Breve presentación de conceptos básicos

2. Ejercicios introductorios: suma de vectores, suma de matrices, ejercicios con índices.

3. Reducción y producto de matrices básico

2. Jueves: Optimización en CUDA

1. Ejercicios: más sobre la reducción

2. Ejercicios: producto de matrices optimizado

3. Viernes: mini proyecto y conceptos avanzados (múltiples GPUs y OpenCL)

2

miércoles 11 de mayo de 2011

Page 3: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

ÍNDICE DE CONTENIDOS

1. Introducción / Motivación

2. La arquitectura CUDA

3. Espacios de memoria

4. Modelo de ejecución CUDA

5. Programación de kernels

6. Conceptos avanzados

3

miércoles 11 de mayo de 2011

Page 4: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

ÍNDICE DE CONTENIDOS

1. Introducción / Motivación

2. La arquitectura CUDA

3. Espacios de memoria

4. Modelo de ejecución CUDA

5. Programación de kernels

6. Conceptos avanzados

4

miércoles 11 de mayo de 2011

Page 5: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

¿POR QUÉ GPUS?Rendimiento

0

250

500

750

1.000

Ene 2003 Jun 2004 Mar 2006 May 2007 Ene 2009

Máx

. GFL

OPS

Nvidia GPU Intel CPU

G80

GT200

NV40

CUDA

Xeon Harpertown @3.2Ghz

5

miércoles 11 de mayo de 2011

Page 6: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

¿POR QUÉ GPUS?Facilidad de programación

6

Ensamblador

2006

OpenGL/DirectX

Cg

CUDA

Brook++ / RapidMind

Los inicios de GPGPU

CUBLAS

CUFFT

CUDPP

2009

OpenCL

Popularización de GPGPU...

¿El futuro de GPGPU?

miércoles 11 de mayo de 2011

Page 7: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

¿POR QUÉ GPUS?Versatilidad

7

Las GPUs ya no son únicamente dispositivos lúdicos... pero lo siguen siendo

miércoles 11 de mayo de 2011

Page 8: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

¿POR QUÉ GPUS?Precio y disponibilidad

8

•Más de 100 millones de GPUs CUDA en el mundo (2008)

2 x Xeon QuadCore2 x Xeon QuadCore + Tesla

s1070

Cores

Rendimiento pico (simple)

Rendimiento pico (doble)

Precio

Consumo

8 8 + 240*4 = 968

0,17 TFlops 4,14 TFlops

0,08 TFlops 0,35 TFlops

$ 3000 $ 11000

700 W 1500 W

+

miércoles 11 de mayo de 2011

Page 9: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

GPUS: CARACTERÍSTICASDistribución de transistores

9

miércoles 11 de mayo de 2011

Page 10: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

GPUS: CARACTERÍSTICASDistribución de transistores

Control

Cache

DRAM

CPU

ALU

•Grandes caches•Pocos elementos de proceso•Optimizada para localidad espacial y temporal•Sofisticados sistemas de control

9

miércoles 11 de mayo de 2011

Page 11: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

GPUS: CARACTERÍSTICASDistribución de transistores

Control

Cache

DRAM

CPU

ALU

•Grandes caches•Pocos elementos de proceso•Optimizada para localidad espacial y temporal•Sofisticados sistemas de control

DRAM

GPU

•Caches reducidas•Elevado número de elementos de proceso•Optimizada para acceso secuencial a datos (streaming)

9

miércoles 11 de mayo de 2011

Page 12: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

GPUS: CARACTERÍSTICASAncho de banda

10

CPU

GPU

MemoriaRAM

MemoriaGPU

40 GB/s

20-160 GB/s

1-8 GB/s1-2 GB/s

PCIExpressInfiniband...

ALUs Caches

6-30 GB/s

miércoles 11 de mayo de 2011

Page 13: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

ÍNDICE DE CONTENIDOS

1. Introducción / Motivación

2. La arquitectura CUDA

3. Espacios de memoria

4. Modelo de ejecución CUDA

5. Programación de kernels

6. Conceptos avanzados

11

miércoles 11 de mayo de 2011

Page 14: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

ARQUITECTURA CUDA

12

Arquitectura CUDA: conjunto de Streaming Multiprocessors (SM)

Arquitectura de un SM:

•8 Scalar Processors (SP)

•1 Unidad de Doble Precisión (DP)

•16 Kbytes de Memoria Compartida

•Banco de registros (16384, 64 Kbytes)

miércoles 11 de mayo de 2011

Page 15: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

LA ARQUITECTURA CUDA

• Nvidia GT200

• 30 SM

• 240 SP (“cores”)

• Arquitectura escalable

• Aumentando el número de SM

13

miércoles 11 de mayo de 2011

Page 16: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

ARQUITECTURA DE MEMORIA

14

miércoles 11 de mayo de 2011

Page 17: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

ARQUITECTURA DE MEMORIA

Memoria Localización Cache Acceso Ámbito Existencia

Registros On-chip N/A R/W Un thread Thread

Compartida On-chip N/A R/W Threads dentro de un mismo bloque Bloque

Global Off-chip No R/W Todos los threads + Host Aplicación

Constantes Off-chip Sí R Todos los threads + Host Aplicación

Texturas Off-chip Sí R Todos los threads + Host Aplicación

15

miércoles 11 de mayo de 2011

Page 18: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

ÍNDICE DE CONTENIDOS

1. Introducción / Motivación

2. La arquitectura CUDA

3. Espacios de memoria

4. Modelo de ejecución CUDA

5. Programación de kernels

6. Conceptos avanzados

16

miércoles 11 de mayo de 2011

Page 19: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

ESPACIOS DE MEMORIA• CPU y GPU poseen espacios de memoria independientes

• Los datos se mueven a través del bus PCIExpress

• Reserva/copia/liberación explícitas

• Cualquier operación de reserva/copia/liberación es realizada por el host

• Los punteros son sólo direcciones

• No posee información de situación (CPU/GPU)

• Precauciones:

• NO acceder a una dirección en GPU desde CPU

17

miércoles 11 de mayo de 2011

Page 20: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

GESTIÓN DE MEMORIA• El host (CPU): reserva y liberación de memoria

• cudaMalloc( void ** ptr, size_t nbytes )

• cudaMemset( void * ptr, int value, size_t count )

• cudaFree( void * ptr )

int main( void ){ int n = 1024 * sizeof( int ); int * d_A = NULL;

cudaMalloc( (void **)&d_A, n );

cudaMemset( d_A, 0, n );

cudaFree( d_A );}

18

miércoles 11 de mayo de 2011

Page 21: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

GESTIÓN DE MEMORIA• El host (CPU): reserva y liberación de memoria

• cudaMalloc( void ** ptr, size_t nbytes )

• cudaMemset( void * ptr, int value, size_t count )

• cudaFree( void * ptr )

int main( void ){ int n = 1024 * sizeof( int ); int * d_A = NULL;

cudaMalloc( (void **)&d_A, n );

cudaMemset( d_A, 0, n );

cudaFree( d_A );}

18

Reserva

miércoles 11 de mayo de 2011

Page 22: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

GESTIÓN DE MEMORIA• El host (CPU): reserva y liberación de memoria

• cudaMalloc( void ** ptr, size_t nbytes )

• cudaMemset( void * ptr, int value, size_t count )

• cudaFree( void * ptr )

int main( void ){ int n = 1024 * sizeof( int ); int * d_A = NULL;

cudaMalloc( (void **)&d_A, n );

cudaMemset( d_A, 0, n );

cudaFree( d_A );}

18

Reserva

Inicialización

miércoles 11 de mayo de 2011

Page 23: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

GESTIÓN DE MEMORIA• El host (CPU): reserva y liberación de memoria

• cudaMalloc( void ** ptr, size_t nbytes )

• cudaMemset( void * ptr, int value, size_t count )

• cudaFree( void * ptr )

int main( void ){ int n = 1024 * sizeof( int ); int * d_A = NULL;

cudaMalloc( (void **)&d_A, n );

cudaMemset( d_A, 0, n );

cudaFree( d_A );}

18

Reserva

Inicialización

Liberación

miércoles 11 de mayo de 2011

Page 24: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

GESTIÓN DE MEMORIA• El host (CPU): transferencias de datos

• cudaMemcpy( void * dst, void * src, size_t nbytes, enum cudaMemcpyKind direction )

• No comienza hasta la finalización de las llamadas CUDA anteriores

• Llamadas bloqueantes (también existen no bloqueantes)

• enum cudaMemcpyKind

✴ cudaMemcpyHostToDevice

✴ cudaMemcpyDeviceToHost

✴ cudaMemcpyDeviceToDevice

19

miércoles 11 de mayo de 2011

Page 25: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO 1

1. Reserva de memoria en CPU para n flotantes

2. Reserva de memoria en GPU para n flotantes

3. Inicialización de la memoria en GPU a 0s

4. Transferencia entre memoria de vídeo y RAM

5. Liberación de memoria

20

miércoles 11 de mayo de 2011

Page 26: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO

21

1 int main ( void )2 {3 int n = 16 ;4 int num bytes = n ∗ s izeof ( f loat ) ;5

6 f loat ∗ h A = NULL ;7 f loat ∗ d A = NULL ;8

9 h A = ( f loat ∗) mal l o c ( num bytes ) ;10 cudaMal loc ( (void ∗∗)&d A , num bytes ) ;11

12 cudaMemset ( d A , 0 , num bytes ) ;13 cudaMemcpy ( h A , d A , num bytes , cudaMemcpyDeviceToHost ) ;14

15 // Mostrar va l o r e s16 // . . .17

18 f r e e ( h A ) ;19 cudaFree ( d A ) ;20

21 return 0 ;22

23 }

miércoles 11 de mayo de 2011

Page 27: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO

21

1 int main ( void )2 {3 int n = 16 ;4 int num bytes = n ∗ s izeof ( f loat ) ;5

6 f loat ∗ h A = NULL ;7 f loat ∗ d A = NULL ;8

9 h A = ( f loat ∗) mal l o c ( num bytes ) ;10 cudaMal loc ( (void ∗∗)&d A , num bytes ) ;11

12 cudaMemset ( d A , 0 , num bytes ) ;13 cudaMemcpy ( h A , d A , num bytes , cudaMemcpyDeviceToHost ) ;14

15 // Mostrar va l o r e s16 // . . .17

18 f r e e ( h A ) ;19 cudaFree ( d A ) ;20

21 return 0 ;22

23 }

Reserva de memoria en GPU

miércoles 11 de mayo de 2011

Page 28: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO

21

1 int main ( void )2 {3 int n = 16 ;4 int num bytes = n ∗ s izeof ( f loat ) ;5

6 f loat ∗ h A = NULL ;7 f loat ∗ d A = NULL ;8

9 h A = ( f loat ∗) mal l o c ( num bytes ) ;10 cudaMal loc ( (void ∗∗)&d A , num bytes ) ;11

12 cudaMemset ( d A , 0 , num bytes ) ;13 cudaMemcpy ( h A , d A , num bytes , cudaMemcpyDeviceToHost ) ;14

15 // Mostrar va l o r e s16 // . . .17

18 f r e e ( h A ) ;19 cudaFree ( d A ) ;20

21 return 0 ;22

23 }

Reserva de memoria en GPU

Inicialización de memoria en GPU

miércoles 11 de mayo de 2011

Page 29: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO

21

1 int main ( void )2 {3 int n = 16 ;4 int num bytes = n ∗ s izeof ( f loat ) ;5

6 f loat ∗ h A = NULL ;7 f loat ∗ d A = NULL ;8

9 h A = ( f loat ∗) mal l o c ( num bytes ) ;10 cudaMal loc ( (void ∗∗)&d A , num bytes ) ;11

12 cudaMemset ( d A , 0 , num bytes ) ;13 cudaMemcpy ( h A , d A , num bytes , cudaMemcpyDeviceToHost ) ;14

15 // Mostrar va l o r e s16 // . . .17

18 f r e e ( h A ) ;19 cudaFree ( d A ) ;20

21 return 0 ;22

23 }

Reserva de memoria en GPU

Inicialización de memoria en GPU

Transferencia de datos entre CPU y GPU

miércoles 11 de mayo de 2011

Page 30: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

ÍNDICE DE CONTENIDOS

1. Introducción / Motivación

2. La arquitectura CUDA

3. Espacios de memoria

4. Modelo de ejecución CUDA

5. Programación de kernels

6. Conceptos avanzados

22

miércoles 11 de mayo de 2011

Page 31: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

MODELO DE EJECUCIÓN

23

•Un kernel CUDA es ejecutado por un array de threads•Todos los threads ejecutan el mismo código

•Cada thread recibe un identificador único

•En función de su ID:

•Tomará decisiones de control

•Operará sobre unos datos u otros (controlado por el programador)

miércoles 11 de mayo de 2011

Page 32: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

MODELO DE EJECUCIÓN

23

•Un kernel CUDA es ejecutado por un array de threads•Todos los threads ejecutan el mismo código

•Cada thread recibe un identificador único

•En función de su ID:

•Tomará decisiones de control

•Operará sobre unos datos u otros (controlado por el programador)

Paradigma SIMD(Simple Instruction -

Multiple Data)

miércoles 11 de mayo de 2011

Page 33: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

MODELO DE EJECUCIÓN

24

JERARQUÍA DE THREADS

•Los threads se particionan en bloques de threads

•Grid: conjunto de bloques de threads lanzados a ejecución

•Bloque de threads: grupo de threads que pueden

•Sincronizar su ejecución

•Comunicarse a través de la memoria compartida

•Número y disposición de threads se deciden en tiempo de

ejecución

miércoles 11 de mayo de 2011

Page 34: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

MODELO DE EJECUCIÓNSoftware Hardware

25

miércoles 11 de mayo de 2011

Page 35: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

MODELO DE EJECUCIÓNSoftware Hardware

Thread Streaming Processor

•Cada thread se ejecuta en un procesador escalar

25

miércoles 11 de mayo de 2011

Page 36: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

MODELO DE EJECUCIÓNSoftware Hardware

Thread Streaming Processor

•Cada thread se ejecuta en un procesador escalar

Bloque de threads Multiprocesador

•Los bloques de threads se ejecutan en los multiprocesadores

•Los bloques de threads NO migran entre MP

•Ejecución concurrente de múltiples bloques de threads en un MP -- Limitado por la memoria compartida y número de registros

25

miércoles 11 de mayo de 2011

Page 37: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

MODELO DE EJECUCIÓNSoftware Hardware

Thread Streaming Processor

•Cada thread se ejecuta en un procesador escalar

Bloque de threads Multiprocesador

•Los bloques de threads se ejecutan en los multiprocesadores

•Los bloques de threads NO migran entre MP

•Ejecución concurrente de múltiples bloques de threads en un MP -- Limitado por la memoria compartida y número de registros

GPUGrid

•Los kernels se ejecutan como grids de bloques

•UN único kernel concurrente*

25

miércoles 11 de mayo de 2011

Page 38: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

MODELO DE PROGRAMACIÓN• Kernel ejecutado por un conjunto de threads

• Los threads se agrupan en bloques de threads

• Para sincronizar su ejecución

• Para comunicarse a través de la memoria compartida

• El código del kernel es ejecutado por cada thread

• Cada thread puede ejecutar un camino distinto dentro del código

• Cada thread lleva asociados:

• Identificador propio dentro del bloque

• Identificador del bloque al que pertece dentro del grid26

miércoles 11 de mayo de 2011

Page 39: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

IDENTIFICADORES Y DIMENSIONES

• Especificados por el programador en tiempo de ejecución:

• Tamaño de bloque

• Número de bloques

• Variables intrínsecas

• threadIdx, blockIdx

• blockDim, gridDim

27

miércoles 11 de mayo de 2011

Page 40: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

IDENTIFICADORES Y DIMENSIONES

• Especificados por el programador en tiempo de ejecución:

• Tamaño de bloque

• Número de bloques

• Variables intrínsecas

• threadIdx, blockIdx

• blockDim, gridDim

27

threadIdx.x = 3threadIdx.y = 1IDENTIFICADORES

DENTRO DEL BLOQUE

miércoles 11 de mayo de 2011

Page 41: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

IDENTIFICADORES Y DIMENSIONES

• Especificados por el programador en tiempo de ejecución:

• Tamaño de bloque

• Número de bloques

• Variables intrínsecas

• threadIdx, blockIdx

• blockDim, gridDim

27

threadIdx.x = 3threadIdx.y = 1IDENTIFICADORES

DENTRO DEL BLOQUE

blockIdx.x = 1blockIdx.y = 1IDENTIFICADORES DENTRO DEL GRID

miércoles 11 de mayo de 2011

Page 42: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PLANIFICACIÓN Y EJECUCIÓN DE THREADS

• Threads ligeros: bajo sobrecoste en la creación y planificación

• Miles de threads: paralelismo de grano muy fino

• Ejecución de un kernel:

1. Se enumeran los bloques de threads

2. Se distribuyen los bloques de threads a los MP disponibles

3. Los threads dentro de un bloque se ejecutan concurrentemente en un MP

4. Al final de la ejecución de un bloque, se lanzan a ejecución nuevos bloques sobre el mismo MP

28

miércoles 11 de mayo de 2011

Page 43: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PLANIFICACIÓN

29

SM 0 SM 1 SM 2 SM 3

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

SM 0 SM 1

Grid

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

Grid

miércoles 11 de mayo de 2011

Page 44: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PLANIFICACIÓN

29

SM 0 SM 1 SM 2 SM 3

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

SM 0 SM 1

Grid

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

Grid

miércoles 11 de mayo de 2011

Page 45: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PLANIFICACIÓN

29

SM 0 SM 1 SM 2 SM 3

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

SM 0 SM 1

Grid

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

Grid

miércoles 11 de mayo de 2011

Page 46: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PLANIFICACIÓN

29

SM 0 SM 1 SM 2 SM 3

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

SM 0 SM 1

Grid

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

Grid

miércoles 11 de mayo de 2011

Page 47: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PLANIFICACIÓN

29

SM 0 SM 1 SM 2 SM 3

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

SM 0 SM 1

Grid

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

Grid

miércoles 11 de mayo de 2011

Page 48: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PLANIFICACIÓN

29

SM 0 SM 1 SM 2 SM 3

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

SM 0 SM 1

Grid

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

Grid

miércoles 11 de mayo de 2011

Page 49: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PLANIFICACIÓN

29

SM 0 SM 1 SM 2 SM 3

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

SM 0 SM 1

Grid

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

Grid

miércoles 11 de mayo de 2011

Page 50: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PLANIFICACIÓN

29

SM 0 SM 1 SM 2 SM 3

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

SM 0 SM 1

Grid

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

Grid

miércoles 11 de mayo de 2011

Page 51: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PLANIFICACIÓN

29

SM 0 SM 1 SM 2 SM 3

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

SM 0 SM 1

Grid

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

Grid

miércoles 11 de mayo de 2011

Page 52: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PLANIFICACIÓN

29

SM 0 SM 1 SM 2 SM 3

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

SM 0 SM 1

Grid

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

Grid

miércoles 11 de mayo de 2011

Page 53: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PLANIFICACIÓN

29

SM 0 SM 1 SM 2 SM 3

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

SM 0 SM 1

Grid

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

Grid

miércoles 11 de mayo de 2011

Page 54: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PLANIFICACIÓN

29

SM 0 SM 1 SM 2 SM 3

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

SM 0 SM 1

Grid

Bloque 4

Bloque 0

Bloque 5

Bloque 1

Bloque 7

Bloque 3

Bloque 6

Bloque 2

Grid

Escalabilidad!!

miércoles 11 de mayo de 2011

Page 55: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PLANIFICACIÓN

30

32 threads

• Unidad de planificación por MP: • Crea, planifica y lanza a ejecución threads dentro de un bloque

• Unidad mínima de planificación: WARP

WARP: grupo de 32 threads lanzados a ejecución concurrentemente sobre un MP

miércoles 11 de mayo de 2011

Page 56: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PLANIFICACIÓN

30

32 threads

• Unidad de planificación por MP: • Crea, planifica y lanza a ejecución threads dentro de un bloque

• Unidad mínima de planificación: WARP

WARP: grupo de 32 threads lanzados a ejecución concurrentemente sobre un MP

miércoles 11 de mayo de 2011

Page 57: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

Warp

PLANIFICACIÓN

30

32 threads

• Unidad de planificación por MP: • Crea, planifica y lanza a ejecución threads dentro de un bloque

• Unidad mínima de planificación: WARP

WARP: grupo de 32 threads lanzados a ejecución concurrentemente sobre un MP

miércoles 11 de mayo de 2011

Page 58: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

Warp

PLANIFICACIÓN

30

32 threads

• Unidad de planificación por MP: • Crea, planifica y lanza a ejecución threads dentro de un bloque

• Unidad mínima de planificación: WARP

WARP: grupo de 32 threads lanzados a ejecución concurrentemente sobre un MP

miércoles 11 de mayo de 2011

Page 59: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO BÁSICO

31

1 global void suma gpu ( f loat ∗A , f loat b , int N )2 {3 int i d x = b l o c k I d x . x ∗ blockDim . x + t h r e a d I d x . x ;4

5 i f ( i d x < N )6 A [ i d x ] = A [ i d x ] + b ;7 }8

9 void main ( )10 {11 . . .12 dim3 dimBlock ( b l o c k S i z e ) ;13 dim3 dimGr id ( c e i l ( N / ( f loat ) b l o c k S i z e ) ) ;14

15 suma gpu<<<dimGrid , dimBlock>>>( d A , b , N ) ;16 }

1 void suma cpu ( f loat ∗A , f loat b , int N )2 {3 for ( int i d x = 0 ; i d x < N ; i d x++ )4 A [ i d x ] = A [ i d x ] + b5 }6

7

8

9 void main ( )10 {11 . . .12 suma cpu ( A , b , N ) ;13 . . .14 }

Programa en CPU Programa en GPU

miércoles 11 de mayo de 2011

Page 60: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO BÁSICO

31

1 global void suma gpu ( f loat ∗A , f loat b , int N )2 {3 int i d x = b l o c k I d x . x ∗ blockDim . x + t h r e a d I d x . x ;4

5 i f ( i d x < N )6 A [ i d x ] = A [ i d x ] + b ;7 }8

9 void main ( )10 {11 . . .12 dim3 dimBlock ( b l o c k S i z e ) ;13 dim3 dimGr id ( c e i l ( N / ( f loat ) b l o c k S i z e ) ) ;14

15 suma gpu<<<dimGrid , dimBlock>>>( d A , b , N ) ;16 }

1 void suma cpu ( f loat ∗A , f loat b , int N )2 {3 for ( int i d x = 0 ; i d x < N ; i d x++ )4 A [ i d x ] = A [ i d x ] + b5 }6

7

8

9 void main ( )10 {11 . . .12 suma cpu ( A , b , N ) ;13 . . .14 }

Programa en CPU Programa en GPU

miércoles 11 de mayo de 2011

Page 61: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

ÍNDICE DE CONTENIDOS

1. Introducción / Motivación

2. La arquitectura CUDA

3. Espacios de memoria

4. Modelo de ejecución CUDA

5. Programación de kernels

6. Conceptos avanzados

32

miércoles 11 de mayo de 2011

Page 62: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PROGRAMACIÓN HÍBRIDA

33

Reserva de memoriaTransferencia a GPU

Transferencia a CPU(opcional)

miércoles 11 de mayo de 2011

Page 63: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

PROGRAMACIÓN DE KERNELS• Código C con algunas restricciones

• Sólo puede acceder a memoria de la GPU*

• No está permitido un número variable de argumentos

• Sin variables estáticas

• Recursividad no permitida

• Directivas para las funciones de nuestro programa

• __global__: ejecutado en la GPU, invocado por la CPU

• __device__: ejecutado en la GPU, invocado por la GPU

• __host__: ejecutado en la CPU

34

miércoles 11 de mayo de 2011

Page 64: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

INVOCACIÓN DE KERNELS• Configuración de la ejecución

• Dimensiones del grid (2D): variable de tipo dim3

• Dimensiones de los bloques de threads (3D): variable de tipo dim3

• Opcionales: cantidad de memoria compartida por bloque, identificador de stream

• Ejemplo: ...

dim3 gridSize( 16, 16 );

dim3 blockSize( 32, 32 );

kernel<<<gridSize, blockSize, 0, 0>>>( ... );

kernel<<<32, 512>>>( ... );

...

35

miércoles 11 de mayo de 2011

Page 65: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO 2

1. A partir de EJEMPLO 1

2. Invocación de kernel para inicializar valores

3. Copia de datos de GPU a CPU

4. Impresión de valores

36

miércoles 11 de mayo de 2011

Page 66: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO 2

37

1 global void k e r n e l ( f loat ∗ A )2 {3 a [ b l o c k I d x . x ∗ blockDim . x + t h r e a d I d x . x ] = 0 . 0 ;4 }5

6 int main ( void )7 {8 int n = 16 ;9 int num bytes = n ∗ s izeof ( f loat ) ;

10

11 f loat ∗ h A = NULL ;12 f loat ∗ d A = NULL ;13

14 h A = ( f loat ∗) mal l o c ( num bytes ) ;15 cudaMal loc ( (void ∗∗)&d A , num bytes ) ;16

17 dim3 b l o c k S i z e ( 4 ) ; dim3 g r i d S i z e ( n/ b l o ck . x ) ;18 k e r n e l <<<g r i d S i z e , b l o c kS i z e >>>( d A ) ;19

20 cudaMemcpy ( h A , d A , num bytes , cudaMemcpyDeviceToHost ) ;21

22 // Mostrar de va l o r e s23 // . . .24

25 f r e e ( h A ) ;26 cudaFree ( d A ) ;27 }

miércoles 11 de mayo de 2011

Page 67: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO 2

37

1 global void k e r n e l ( f loat ∗ A )2 {3 a [ b l o c k I d x . x ∗ blockDim . x + t h r e a d I d x . x ] = 0 . 0 ;4 }5

6 int main ( void )7 {8 int n = 16 ;9 int num bytes = n ∗ s izeof ( f loat ) ;

10

11 f loat ∗ h A = NULL ;12 f loat ∗ d A = NULL ;13

14 h A = ( f loat ∗) mal l o c ( num bytes ) ;15 cudaMal loc ( (void ∗∗)&d A , num bytes ) ;16

17 dim3 b l o c k S i z e ( 4 ) ; dim3 g r i d S i z e ( n/ b l o ck . x ) ;18 k e r n e l <<<g r i d S i z e , b l o c kS i z e >>>( d A ) ;19

20 cudaMemcpy ( h A , d A , num bytes , cudaMemcpyDeviceToHost ) ;21

22 // Mostrar de va l o r e s23 // . . .24

25 f r e e ( h A ) ;26 cudaFree ( d A ) ;27 }

Configuración de la ejecución:

Número de bloques: 16 / 4 = 4Threads por bloque: 4

Número total de threads: 16

miércoles 11 de mayo de 2011

Page 68: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO 2

37

1 global void k e r n e l ( f loat ∗ A )2 {3 a [ b l o c k I d x . x ∗ blockDim . x + t h r e a d I d x . x ] = 0 . 0 ;4 }5

6 int main ( void )7 {8 int n = 16 ;9 int num bytes = n ∗ s izeof ( f loat ) ;

10

11 f loat ∗ h A = NULL ;12 f loat ∗ d A = NULL ;13

14 h A = ( f loat ∗) mal l o c ( num bytes ) ;15 cudaMal loc ( (void ∗∗)&d A , num bytes ) ;16

17 dim3 b l o c k S i z e ( 4 ) ; dim3 g r i d S i z e ( n/ b l o ck . x ) ;18 k e r n e l <<<g r i d S i z e , b l o c kS i z e >>>( d A ) ;19

20 cudaMemcpy ( h A , d A , num bytes , cudaMemcpyDeviceToHost ) ;21

22 // Mostrar de va l o r e s23 // . . .24

25 f r e e ( h A ) ;26 cudaFree ( d A ) ;27 }

Configuración de la ejecución:

Número de bloques: 16 / 4 = 4Threads por bloque: 4

Número total de threads: 16

Invocación del kernel

miércoles 11 de mayo de 2011

Page 69: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO 2

37

1 global void k e r n e l ( f loat ∗ A )2 {3 a [ b l o c k I d x . x ∗ blockDim . x + t h r e a d I d x . x ] = 0 . 0 ;4 }5

6 int main ( void )7 {8 int n = 16 ;9 int num bytes = n ∗ s izeof ( f loat ) ;

10

11 f loat ∗ h A = NULL ;12 f loat ∗ d A = NULL ;13

14 h A = ( f loat ∗) mal l o c ( num bytes ) ;15 cudaMal loc ( (void ∗∗)&d A , num bytes ) ;16

17 dim3 b l o c k S i z e ( 4 ) ; dim3 g r i d S i z e ( n/ b l o ck . x ) ;18 k e r n e l <<<g r i d S i z e , b l o c kS i z e >>>( d A ) ;19

20 cudaMemcpy ( h A , d A , num bytes , cudaMemcpyDeviceToHost ) ;21

22 // Mostrar de va l o r e s23 // . . .24

25 f r e e ( h A ) ;26 cudaFree ( d A ) ;27 }

Configuración de la ejecución:

Número de bloques: 16 / 4 = 4Threads por bloque: 4

Número total de threads: 16

Invocación del kernel

Transferencia de datos entre GPU y CPU

miércoles 11 de mayo de 2011

Page 70: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = 7.0;}

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = blockIdx.x;}

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = threadIdx.x;}

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = blockIdx.x * blockDim.x + threadIdx.x;}

38

miércoles 11 de mayo de 2011

Page 71: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = 7.0;}

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = blockIdx.x;}

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = threadIdx.x;}

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = blockIdx.x * blockDim.x + threadIdx.x;}

Salida: 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7

38

miércoles 11 de mayo de 2011

Page 72: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = 7.0;}

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = blockIdx.x;}

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = threadIdx.x;}

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = blockIdx.x * blockDim.x + threadIdx.x;}

Salida: 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7

Salida: 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3

38

miércoles 11 de mayo de 2011

Page 73: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = 7.0;}

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = blockIdx.x;}

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = threadIdx.x;}

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = blockIdx.x * blockDim.x + threadIdx.x;}

Salida: 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7

Salida: 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3

Salida: 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3

38

miércoles 11 de mayo de 2011

Page 74: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = 7.0;}

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = blockIdx.x;}

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = threadIdx.x;}

__global__ void kernel( float * A ){ A[ blockIdx.x * blockDim.x + threadIdx.x ] = blockIdx.x * blockDim.x + threadIdx.x;}

Salida: 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7

Salida: 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3

Salida: 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3

38

Salida: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

miércoles 11 de mayo de 2011

Page 75: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

SINCRONIZACIÓN• Todas las invocaciones de kernels son asíncronas

• El control vuelve inmediatamente al programa principal (CPU)

• La ejecución comienza cuando las anteriores llamadas CUDA han finalizado

• cudaThreadSynchronize()

• Bloqueante, hasta que las anteriores llamadas a CUDA han finalizado

• Imprescindible en mediciones de tiempo

• Las copias de memoria son síncronas

• Desde CUDA 2.0 existen transferencias asíncronas

• Posibilidad de solapamiento comunicaciones/ejecución de kernels

39

miércoles 11 de mayo de 2011

Page 76: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

MEMORIA COMPARTIDA• Memoria on-chip

• Menor latencia - Mayor ancho de banda

• 16 Kbytes por MP (30 en Nvidia GT200)

• Asociada a un bloque de threads

• Accesible por todos los threads dentro de un mismo bloque

• NO accesible por otros bloques de threads

• Usos:

• Comunicación entre threads

• Cache software

40

miércoles 11 de mayo de 2011

Page 77: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

MEMORIA COMPARTIDATamaño conocido en

tiempo de compilación

41

Tamaño conocido en tiempo de ejecución

__global__ void kernel(...){...__shared__ float sData[256];...}

int main( void ){ ... kernel<<<nBlocks,blockSize>>>(...); ...}

__global__ void kernel(...){...extern __shared__ float sData[];...}

int main( void ){ ... smBytes = blockSize * sizeof(float); kernel<<<nBlocks,blockSize, smBytes>>>(...); ...}

miércoles 11 de mayo de 2011

Page 78: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

MEMORIA COMPARTIDA: EJEMPLO

42

Radio Radio

•Stencil 1D•Estructura de datos unidimensional•Por cada elemento de salida, suma los elementos dentro de un radio

•Ejemplo: Radio = 3•Suma de 7 elementos

•Objetivo: reducir accesos a memoria global

miércoles 11 de mayo de 2011

Page 79: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

MEMORIA COMPARTIDA: EJEMPLO

43

“Halo” “Halo”

•Bloques de threads unidimensionaes•Particionado de la salida

•Cada bloque de threads computa BLOCK_DIM elementos•Lee datos de entrada desde memoria global

•Necesita BLOCK_DIMX + 2 * RADIO elementos de entrada•Transfiere a memoria compartida•Computa•Escribe a memoria compartida

Elementos de entradaTantos como threads por bloque de threads

miércoles 11 de mayo de 2011

Page 80: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

MEMORIA COMPARTIDA: EJEMPLO

44

1 global void s t e n c i l ( int ∗ output , int ∗ i nput ,2 int dimx , int dimy )3 {4 shared int s a [BLOCK DIMX+2∗RADIUS ] ;5

6 int g l o b a l i x = b l o c k I d x . x∗blockDim . x + t h r e a d I d x . x ;7 int l o c a l i x = t h r e a d I d x . x + RADIUS ;8

9 s a [ l o c a l i x ] = i n pu t [ g l o b a l i x ] ;10

11 i f ( t h r e a d I d x . x < RADIUS ) {12 s a [ l o c a l i x − RADIUS ] = i n pu t [ g l o b a l i x − RADIUS ] ;13 s a [ l o c a l i x + BLOCK DIMX + RADIUS ] =14 i n pu t [ g l o b a l i x + BLOCK DIMX + RADIUS ] ;15 }16

17 syncthreads ( ) ;18

19 int v a l u e = 0 ;20 for ( o f f s e t = −RADIUS ; o f f s e t <=RADIUS ; o f f s e t++ )21 v a l u e += s a [ l o c a l i x + o f f s e t ] ;22

23 output [ g l o b a l i x ] = v a l u e ;24 }

miércoles 11 de mayo de 2011

Page 81: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

MEMORIA COMPARTIDA: EJEMPLO

44

1 global void s t e n c i l ( int ∗ output , int ∗ i nput ,2 int dimx , int dimy )3 {4 shared int s a [BLOCK DIMX+2∗RADIUS ] ;5

6 int g l o b a l i x = b l o c k I d x . x∗blockDim . x + t h r e a d I d x . x ;7 int l o c a l i x = t h r e a d I d x . x + RADIUS ;8

9 s a [ l o c a l i x ] = i n pu t [ g l o b a l i x ] ;10

11 i f ( t h r e a d I d x . x < RADIUS ) {12 s a [ l o c a l i x − RADIUS ] = i n pu t [ g l o b a l i x − RADIUS ] ;13 s a [ l o c a l i x + BLOCK DIMX + RADIUS ] =14 i n pu t [ g l o b a l i x + BLOCK DIMX + RADIUS ] ;15 }16

17 syncthreads ( ) ;18

19 int v a l u e = 0 ;20 for ( o f f s e t = −RADIUS ; o f f s e t <=RADIUS ; o f f s e t++ )21 v a l u e += s a [ l o c a l i x + o f f s e t ] ;22

23 output [ g l o b a l i x ] = v a l u e ;24 }

miércoles 11 de mayo de 2011

Page 82: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

MEMORIA COMPARTIDA: EJEMPLO

44

1 global void s t e n c i l ( int ∗ output , int ∗ i nput ,2 int dimx , int dimy )3 {4 shared int s a [BLOCK DIMX+2∗RADIUS ] ;5

6 int g l o b a l i x = b l o c k I d x . x∗blockDim . x + t h r e a d I d x . x ;7 int l o c a l i x = t h r e a d I d x . x + RADIUS ;8

9 s a [ l o c a l i x ] = i n pu t [ g l o b a l i x ] ;10

11 i f ( t h r e a d I d x . x < RADIUS ) {12 s a [ l o c a l i x − RADIUS ] = i n pu t [ g l o b a l i x − RADIUS ] ;13 s a [ l o c a l i x + BLOCK DIMX + RADIUS ] =14 i n pu t [ g l o b a l i x + BLOCK DIMX + RADIUS ] ;15 }16

17 syncthreads ( ) ;18

19 int v a l u e = 0 ;20 for ( o f f s e t = −RADIUS ; o f f s e t <=RADIUS ; o f f s e t++ )21 v a l u e += s a [ l o c a l i x + o f f s e t ] ;22

23 output [ g l o b a l i x ] = v a l u e ;24 }

miércoles 11 de mayo de 2011

Page 83: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

MEMORIA COMPARTIDA: EJEMPLO

44

1 global void s t e n c i l ( int ∗ output , int ∗ i nput ,2 int dimx , int dimy )3 {4 shared int s a [BLOCK DIMX+2∗RADIUS ] ;5

6 int g l o b a l i x = b l o c k I d x . x∗blockDim . x + t h r e a d I d x . x ;7 int l o c a l i x = t h r e a d I d x . x + RADIUS ;8

9 s a [ l o c a l i x ] = i n pu t [ g l o b a l i x ] ;10

11 i f ( t h r e a d I d x . x < RADIUS ) {12 s a [ l o c a l i x − RADIUS ] = i n pu t [ g l o b a l i x − RADIUS ] ;13 s a [ l o c a l i x + BLOCK DIMX + RADIUS ] =14 i n pu t [ g l o b a l i x + BLOCK DIMX + RADIUS ] ;15 }16

17 syncthreads ( ) ;18

19 int v a l u e = 0 ;20 for ( o f f s e t = −RADIUS ; o f f s e t <=RADIUS ; o f f s e t++ )21 v a l u e += s a [ l o c a l i x + o f f s e t ] ;22

23 output [ g l o b a l i x ] = v a l u e ;24 }

miércoles 11 de mayo de 2011

Page 84: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

SINCRONIZACIÓN DE THREADS

• void __syncthreads( );

• Sincroniza los threads de un mismo bloque de threads

• Los threads se planifican en tiempo de ejecución

• Evita riesgos RAW/WAR/WAW en acceso a memoria compartida

• Cuando todos los threads llegan a este punto la ejecución continúa

• Precaución al usarlo dentro de condicionales

45

miércoles 11 de mayo de 2011

Page 85: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

Implementación básica

• Cada thread calcula UN elemento de la matriz C

• Lee una fila de A

• Lee una columna de B

• Accesos a memoria global

• A: B.width lecturas

• B: A.heigth lecturas

46

miércoles 11 de mayo de 2011

Page 86: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

47

1 void MatMul ( const Matr i x A , const Matr i x B , Matr i x C)2 {3 Matr i x d A ;4 d A . width = A . width ; d A . h e i g h t = A . h e i g h t ;5 s i z e t s i z e = A . width ∗ A . h e i g h t ∗ s izeof ( f loat ) ;6

7 // Reserva de memoria para A en GPU8 cudaMal loc ( ( void∗∗)&d A . e lements , s i z e ) ;9 cudaMemcpy ( d A . e lements , A . e lements , s i z e ,

10 cudaMemcpyHostToDevice ) ;11

12 // Reserva de memoria para B y C en GPU13 // . . .14

15 // Invocac ion de l k e rne l16 dim3 dimBlock (BLOCK SIZE , BLOCK SIZE ) ;17 dim3 dimGr id (B . width / dimBlock . x , A . h e i g h t / dimBlock . y ) ;18 MatMulKernel<<<dimGrid , dimBlock>>>(d A , d B , d C ) ;19

20 // Trans f e r enc i a de C de vue l ta a RAM21 cudaMemcpy (C . e lements , Cd . e lements , s i z e ,22 cudaMemcpyDeviceToHost ) ;23

24 // L ibe rac i on de memoria25 cudaFree ( d A . e l ement s ) ;26 cudaFree ( d B . e l ement s ) ;27 cudaFree ( d C . e l ement s ) ;28 }

miércoles 11 de mayo de 2011

Page 87: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

48

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C)2 {3 // Cada thread c a l c u l a un elemento de C4 // acumulando e l r e su l t ado en Cvalue5 f loat Cva lue = 0 ;6 int row = b l o c k I d x . y ∗ blockDim . y + t h r e a d I d x . y ;7 int c o l = b l o c k I d x . x ∗ blockDim . x + t h r e a d I d x . x ;8

9 for ( int e = 0 ; e < A . width ; ++e )10 Cva lue += A . e l ement s [ row ∗ A . width + e ]11 ∗ B . e l ement s [ e ∗ B . width + c o l ] ;12

13 C . e l ement s [ row ∗ C . width + c o l ] = Cva lue ;14 }

miércoles 11 de mayo de 2011

Page 88: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMULImplementación optimizada

49

• Cada bloque de threads calcula una submatriz de C

• Objetivo:

• Minimizar accesos a mem. global

• Memoria compartida

miércoles 11 de mayo de 2011

Page 89: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

miércoles 11 de mayo de 2011

Page 90: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

ID del bloque dentro del grid

miércoles 11 de mayo de 2011

Page 91: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

miércoles 11 de mayo de 2011

Page 92: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

Submatriz de C a obtener

miércoles 11 de mayo de 2011

Page 93: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

miércoles 11 de mayo de 2011

Page 94: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

ID del thread dentro del bloque

miércoles 11 de mayo de 2011

Page 95: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

miércoles 11 de mayo de 2011

Page 96: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

Submatrices de A y B

miércoles 11 de mayo de 2011

Page 97: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

miércoles 11 de mayo de 2011

Page 98: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

Memoria compartida para

Asub y Bsub

miércoles 11 de mayo de 2011

Page 99: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

miércoles 11 de mayo de 2011

Page 100: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

Carga de A y B en memoria compartida: un elemento

por thread

miércoles 11 de mayo de 2011

Page 101: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

miércoles 11 de mayo de 2011

Page 102: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

Importante: Sincronización

miércoles 11 de mayo de 2011

Page 103: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

miércoles 11 de mayo de 2011

Page 104: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

Cálculo de Csub

miércoles 11 de mayo de 2011

Page 105: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

miércoles 11 de mayo de 2011

Page 106: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

Escritura en memoria global

miércoles 11 de mayo de 2011

Page 107: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

EJEMPLO: MATMUL

50

1 global void MatMulKernel ( Matr i x A , Matr i x B , Matr i x C) {2 int blockRow = b l o c k I d x . y ;3 int b l o ckCo l = b l o c k I d x . x ;4

5 Matr i x Csub = GetSubMatr ix (C , blockRow , b l o ckCo l ) ;6

7 int row = t h r e a d I d x . y ; int c o l = t h r e a d I d x . x ;8

9 // I t e r a c i o n sobre submatr ices de A y B10 for ( int m = 0 ; m < (A . width / BLOCK SIZE ) ; ++m) {11 Matr i x Asub = GetSubMatr ix (A , blockRow , m) ;12 Matr i x Bsub = GetSubMatr ix (B , m, b l o ckCo l ) ;13

14 shared f loat As [ BLOCK SIZE ] [ BLOCK SIZE ] ;15 shared f loat Bs [ BLOCK SIZE ] [ BLOCK SIZE ] ;16

17 As [ row ] [ c o l ] = GetElement (Asub , row , c o l ) ;18 Bs [ row ] [ c o l ] = GetElement (Bsub , row , c o l ) ;19

20 syncthreads ( ) ;21

22 // Asub ∗ Bsub23 for ( int e = 0 ; e < BLOCK SIZE ; ++e )24 Cva lue += As [ row ] [ e ] ∗ Bs [ e ] [ c o l ] ;25 syncthreads ( ) ;26 }27

28 SetE lement (Csub , row , co l , Cva lue ) ;29 }

miércoles 11 de mayo de 2011

Page 108: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

ÍNDICE DE CONTENIDOS

1. Introducción / Motivación

2. La arquitectura CUDA

3. Espacios de memoria

4. Modelo de ejecución CUDA

5. Programación de kernels

6. Conceptos avanzados

51

miércoles 11 de mayo de 2011

Page 109: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

• Agrupar transferencias

• Evitar transferencias pequeñas

• Uso de “page-locked memory”

• Mayor ancho de banda

• Copias asíncronas (solapamiento cálculo-comunicaciones)

• Posibilidad de mapear memoria del host en el device

• Recurso limitado!!

52

Optimización de transferencias

malloc( );

cudaHostAlloc();

miércoles 11 de mayo de 2011

Page 110: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

53

Solapamiento de cálculo y comunicaciones

miércoles 11 de mayo de 2011

Page 111: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

53

Solapamiento de cálculo y comunicaciones

Tx. FFT Tx.

miércoles 11 de mayo de 2011

Page 112: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

53

Solapamiento de cálculo y comunicaciones

Tx. FFT Tx. Tx. FFT Tx.

miércoles 11 de mayo de 2011

Page 113: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

53

Solapamiento de cálculo y comunicaciones

Tx. FFT Tx. Tx. FFT Tx. Tx. FFT Tx.

miércoles 11 de mayo de 2011

Page 114: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

53

Solapamiento de cálculo y comunicaciones

Tx. FFT Tx. Tx. FFT Tx. Tx. FFT Tx.

Tx.

miércoles 11 de mayo de 2011

Page 115: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

53

Solapamiento de cálculo y comunicaciones

Tx. FFT Tx. Tx. FFT Tx. Tx. FFT Tx.

Tx. FFT

Tx.

miércoles 11 de mayo de 2011

Page 116: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

53

Solapamiento de cálculo y comunicaciones

Tx. FFT Tx. Tx. FFT Tx. Tx. FFT Tx.

Tx. FFT

Tx.

Tx.

FFT

Tx.

miércoles 11 de mayo de 2011

Page 117: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

53

Solapamiento de cálculo y comunicaciones

Tx. FFT Tx. Tx. FFT Tx. Tx. FFT Tx.

Tx. FFT

Tx.

Tx.

FFT

Tx.

Tx.

FFT

miércoles 11 de mayo de 2011

Page 118: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

53

Solapamiento de cálculo y comunicaciones

Tx. FFT Tx. Tx. FFT Tx. Tx. FFT Tx.

Tx. FFT

Tx.

Tx.

FFT

Tx.

Tx.

FFT Tx.

miércoles 11 de mayo de 2011

Page 119: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

53

Solapamiento de cálculo y comunicaciones

Tx. FFT Tx. Tx. FFT Tx. Tx. FFT Tx.

Tx. FFT

Tx.

Tx.

FFT

Tx.

Tx.

FFT Tx.

Stream: secuencia de comandosejecutados en orden

miércoles 11 de mayo de 2011

Page 120: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

54

Solapamiento de cálculo y comunicaciones1 #define NUM STREAMS 32 cudaSt ream t st ream [ NUM STREAMS ] ;3

4 for ( int i =0; i<NUM STREAMS ; i++ )5 cudaStreamCreate ( &s t ream [ i ] ) ;6

7 f loat ∗ ho s tP t r ;8 cudaMal locHost ( (void ∗∗)& hos tPt r , NUM STREAMS ∗ s i z e ) ;9

10 for ( int i =0; i<NUM STREAMS, i++ )11 cudaMemcpyAsync ( i nputDevPt r + i ∗ s i z e ,12 ho s tP t r + i ∗ s i z e ,13 s i z e , cudaMemcpyHostToDevice ,14 s t ream [ i ] ) ;15

16 for ( int i =0; i<NUM STREAMS, i++ )17 f f t <<<100, 512 , 0 , s t ream [ i ]>>>18 ( outputDevPtr + i ∗ s i z e ,19 i nputDevPt r + i ∗ s i z e ,20 s t ream [ i ] ) ;21

22 for ( int i =0; i<NUM STREAMS, i++ )23 cudaMemcpyAsync ( ho s tP t r + i ∗ s i z e ,24 ou tpu tDev i c ePt r + i ∗ s i z e ,25 s i z e , cudadMemcpyDeviceToHost ,26 s t ream [ i ] ) ;27

28 cudaThreadSynchron i ze ( ) ;

miércoles 11 de mayo de 2011

Page 121: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

54

Solapamiento de cálculo y comunicaciones1 #define NUM STREAMS 32 cudaSt ream t st ream [ NUM STREAMS ] ;3

4 for ( int i =0; i<NUM STREAMS ; i++ )5 cudaStreamCreate ( &s t ream [ i ] ) ;6

7 f loat ∗ ho s tP t r ;8 cudaMal locHost ( (void ∗∗)& hos tPt r , NUM STREAMS ∗ s i z e ) ;9

10 for ( int i =0; i<NUM STREAMS, i++ )11 cudaMemcpyAsync ( i nputDevPt r + i ∗ s i z e ,12 ho s tP t r + i ∗ s i z e ,13 s i z e , cudaMemcpyHostToDevice ,14 s t ream [ i ] ) ;15

16 for ( int i =0; i<NUM STREAMS, i++ )17 f f t <<<100, 512 , 0 , s t ream [ i ]>>>18 ( outputDevPtr + i ∗ s i z e ,19 i nputDevPt r + i ∗ s i z e ,20 s t ream [ i ] ) ;21

22 for ( int i =0; i<NUM STREAMS, i++ )23 cudaMemcpyAsync ( ho s tP t r + i ∗ s i z e ,24 ou tpu tDev i c ePt r + i ∗ s i z e ,25 s i z e , cudadMemcpyDeviceToHost ,26 s t ream [ i ] ) ;27

28 cudaThreadSynchron i ze ( ) ;

Creación de streams

miércoles 11 de mayo de 2011

Page 122: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

54

Solapamiento de cálculo y comunicaciones1 #define NUM STREAMS 32 cudaSt ream t st ream [ NUM STREAMS ] ;3

4 for ( int i =0; i<NUM STREAMS ; i++ )5 cudaStreamCreate ( &s t ream [ i ] ) ;6

7 f loat ∗ ho s tP t r ;8 cudaMal locHost ( (void ∗∗)& hos tPt r , NUM STREAMS ∗ s i z e ) ;9

10 for ( int i =0; i<NUM STREAMS, i++ )11 cudaMemcpyAsync ( i nputDevPt r + i ∗ s i z e ,12 ho s tP t r + i ∗ s i z e ,13 s i z e , cudaMemcpyHostToDevice ,14 s t ream [ i ] ) ;15

16 for ( int i =0; i<NUM STREAMS, i++ )17 f f t <<<100, 512 , 0 , s t ream [ i ]>>>18 ( outputDevPtr + i ∗ s i z e ,19 i nputDevPt r + i ∗ s i z e ,20 s t ream [ i ] ) ;21

22 for ( int i =0; i<NUM STREAMS, i++ )23 cudaMemcpyAsync ( ho s tP t r + i ∗ s i z e ,24 ou tpu tDev i c ePt r + i ∗ s i z e ,25 s i z e , cudadMemcpyDeviceToHost ,26 s t ream [ i ] ) ;27

28 cudaThreadSynchron i ze ( ) ;

Creación de streams

Page-locked memory

miércoles 11 de mayo de 2011

Page 123: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

54

Solapamiento de cálculo y comunicaciones1 #define NUM STREAMS 32 cudaSt ream t st ream [ NUM STREAMS ] ;3

4 for ( int i =0; i<NUM STREAMS ; i++ )5 cudaStreamCreate ( &s t ream [ i ] ) ;6

7 f loat ∗ ho s tP t r ;8 cudaMal locHost ( (void ∗∗)& hos tPt r , NUM STREAMS ∗ s i z e ) ;9

10 for ( int i =0; i<NUM STREAMS, i++ )11 cudaMemcpyAsync ( i nputDevPt r + i ∗ s i z e ,12 ho s tP t r + i ∗ s i z e ,13 s i z e , cudaMemcpyHostToDevice ,14 s t ream [ i ] ) ;15

16 for ( int i =0; i<NUM STREAMS, i++ )17 f f t <<<100, 512 , 0 , s t ream [ i ]>>>18 ( outputDevPtr + i ∗ s i z e ,19 i nputDevPt r + i ∗ s i z e ,20 s t ream [ i ] ) ;21

22 for ( int i =0; i<NUM STREAMS, i++ )23 cudaMemcpyAsync ( ho s tP t r + i ∗ s i z e ,24 ou tpu tDev i c ePt r + i ∗ s i z e ,25 s i z e , cudadMemcpyDeviceToHost ,26 s t ream [ i ] ) ;27

28 cudaThreadSynchron i ze ( ) ;

Creación de streams

Page-locked memory

Cada stream lleva asociado:

1.Transferencia asíncrona

2.Ejecución 3.Transferencia asíncrona

miércoles 11 de mayo de 2011

Page 124: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

54

Solapamiento de cálculo y comunicaciones1 #define NUM STREAMS 32 cudaSt ream t st ream [ NUM STREAMS ] ;3

4 for ( int i =0; i<NUM STREAMS ; i++ )5 cudaStreamCreate ( &s t ream [ i ] ) ;6

7 f loat ∗ ho s tP t r ;8 cudaMal locHost ( (void ∗∗)& hos tPt r , NUM STREAMS ∗ s i z e ) ;9

10 for ( int i =0; i<NUM STREAMS, i++ )11 cudaMemcpyAsync ( i nputDevPt r + i ∗ s i z e ,12 ho s tP t r + i ∗ s i z e ,13 s i z e , cudaMemcpyHostToDevice ,14 s t ream [ i ] ) ;15

16 for ( int i =0; i<NUM STREAMS, i++ )17 f f t <<<100, 512 , 0 , s t ream [ i ]>>>18 ( outputDevPtr + i ∗ s i z e ,19 i nputDevPt r + i ∗ s i z e ,20 s t ream [ i ] ) ;21

22 for ( int i =0; i<NUM STREAMS, i++ )23 cudaMemcpyAsync ( ho s tP t r + i ∗ s i z e ,24 ou tpu tDev i c ePt r + i ∗ s i z e ,25 s i z e , cudadMemcpyDeviceToHost ,26 s t ream [ i ] ) ;27

28 cudaThreadSynchron i ze ( ) ;

Creación de streams

Page-locked memory

Cada stream lleva asociado:

1.Transferencia asíncrona

2.Ejecución 3.Transferencia asíncrona

Sincronización

miércoles 11 de mayo de 2011

Page 125: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

• nvcc source.cu

• nvcc --device-emu source.cu

• Define la macro __DEVICE_EMULATION__

• Ventajas:

• Uso de herramientas de depuración (gdb)

• Invocaciones a printf( ... ) dentro del kernel

• Un sólo espacio de direcciones

• Detección de deadlocks

55

Compilación. El modo emulación

miércoles 11 de mayo de 2011

Page 126: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

• CUDA incluye:

• CUBLAS: implementación BLAS

• CUFFT: implementación FFT

• CUDPP (Data Parallel Primitives):

• Funciones de ordenación, reducción, ...

56

Bibliotecas

miércoles 11 de mayo de 2011

Page 127: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

• Características

• Transformaciones 1D, 2D y 3D sobre datos reales y complejos

• “Batched execution” para múltiples transformaciones en paralelo

• Hasta 8M elementos para 1D, [2-16384] para 2D y 3D

• Transformaciones in-place y out-of-place

57

Bibliotecas. CUFFT

miércoles 11 de mayo de 2011

Page 128: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

58

Bibliotecas. CUFFT

1 #define NX 256

2 #define NY 128

3

4 c u f f tHand l e p l an ;

5 cu f f tComp l ex ∗ i d a t a , ∗ odata ;

6

7 cudaMal loc ( ( void∗∗)& i d a t a , s izeof ( cu f f tComp l ex )∗NX∗NY ) ;

8 cudaMal loc ( ( void∗∗)&odata , s izeof ( cu f f tComp l ex )∗NX∗NY ) ;

9 . . .

10 /∗ Creacion de plan 2D FFT. ∗/11 c u f f tP l a n 2d (&plan , NX,NY, CUFFT C2C ) ;

12

13 /∗ Transformacion OUT−OF−PLACE. ∗/14 cuf f tExecC2C ( plan , i d a t a , odata , CUFFT FORWARD ) ;

15

16 /∗ Transformacion inve r s a in−p lace . ∗/17 cuf f tExecC2C ( plan , odata , odata , CUFFT INVERSE ) ;

18

19 /∗ Destrucc ion de l plan . ∗/20 c u f f tD e s t r o y ( p l an ) ;

21

22 cudaFree ( i d a t a ) , cudaFree ( odata ) ;

Punteros de entrada y salida distintos implican transformación out-of-place

miércoles 11 de mayo de 2011

Page 129: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

OPTIMIZACIÓN DE RENDIMIENTO

• Ejecución de un thread en un MP:

1. Lectura de operandos para cada thread

2. Ejecución

3. Escritura de resultados para cada thread

59

• Mecanismos de optimización:

1. Optimización del ancho de banda para cada tipo de memoria

2. Permitir al planificador solapar transacciones de memoria con computación:

• Gran cantidad de threads activos por multiprocesador

• Programas con elevada intensidad aritmética (número de cálculos por acceso a memoria)

miércoles 11 de mayo de 2011

Page 130: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

OPTIMIZACIÓN DE RENDIMIENTO

•Optimizar los algoritmos para adaptarlos a la arquitectura

•Maximizar el paralelismo de datos

•Maximizar la intensidad aritmética de los kernels (cálculo/accesos a memoria)

•En ocasiones, mejor recomputar que almacenar

•En ocasiones, mejor llevar cálculos “anti-GPU” a la GPU antes que aumentar transferencias

60

miércoles 11 de mayo de 2011

Page 131: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

OPTIMIZACIÓN DE RENDIMIENTO

•Conocer el modelo de ejecución: usarlo eficientemente

•Particionado del problema para mantener los MPs ocupados

•Kernels ligeros: los recursos son limitados

•Evitar saltos y condicionales

61

miércoles 11 de mayo de 2011

Page 132: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

SALTOS Y CONDICIONALES

62

0 1 2 3 4 5 6 7

T T F F T F F T

X X X XX X X X

XX X XXX X X

if( cond ){ op1; op2;}else{ op3; op4;}

Evitar condicionales

miércoles 11 de mayo de 2011

Page 133: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

OPTIMIZACIÓN DE RENDIMIENTO

•Optimizar accesos a memoria

•Coalesced vs. No-Coalesced = Orden de magnitud

•Optimizar localidad espacial y usar memoria de texturas

•En memoria compartida, evitar conflictos

63

miércoles 11 de mayo de 2011

Page 134: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

OPTIMIZACIÓN DE RENDIMIENTO

•USAR MEMORIA COMPARTIDA

•Cientos de veces más rápida que memoria global

•Vía de comunicación entre threads

•Recurso escaso (16 Kbytes por MP)

64

miércoles 11 de mayo de 2011

Page 135: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

OPTIMIZACIÓN DE RENDIMIENTO

65

Memoria global

•Mayor latencia / menor ancho de banda que memoria compartida

•Evitar usarla en la medida de lo posible

•Programa básico por cada thread de un bloque:

1.Carga de datos desde memoria global a memoria compartida

2.Sincronización de threads

3.Procesamiento usando sólo memoria compartida

4.Sincronización (sin necesario)

5.Escritura en memoria global

miércoles 11 de mayo de 2011

Page 136: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

Posibilidad de acceso no contiguo

OPTIMIZACIÓN DE RENDIMIENTO

66

Memoria global

•Accesos a memoria global por half-warp (16 threads)•Coalescing: grupo de transferencias realizadas en una sola transacción

32B

64B

128B

Coalescing en GPUS 1.1 e inferiores

Coalescing en GPUS 1.2 y superiores

2 transacciones:1 segmento de 64B + 1 segmento de 32B

1 Transacción 16 Transacciones 16 Transacciones

miércoles 11 de mayo de 2011

Page 137: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

OPTIMIZACIÓN DE RENDIMIENTO

67

Memoria compartida

•Tan rápida como acceder a registros (si no hay conflictos)

•Dividida en módulos (bancos). Posibilidad de acceso paralelo a los bancos

•Objetivo: evitar conflictos•Acceso simultáneo a un mismo banco por dos threads

•Sucesivas palabras de 32 bits sucesivos bancos de memoria

•16 bancos de memoria en GPUs actuales

__shared__ char shared[ 32 ];char data = shared[ baseIndex + tid ];

__shared__ char shared[ 32 ];char data = shared[ baseIndex + 4 * tid ];

miércoles 11 de mayo de 2011

Page 138: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

OPTIMIZACIÓN DE RENDIMIENTO

68

Thread 0

Thread 1

Thread 2

Thread 3

Thread 4

Thread 5

Thread 6

Thread 7

Thread 15

Banco 0

Banco 1

Banco 2

Banco 3

Banco 4

Banco 5

Banco 6

Banco 7

Banco 15

......

Thread 0

Thread 1

Thread 2

Thread 3

Thread 4

Thread 5

Thread 6

Thread 7

Thread 15

Banco 0

Banco 1

Banco 2

Banco 3

Banco 4

Banco 5

Banco 6

Banco 7

Banco 15

......

Thread 0

Thread 1

Thread 2

Thread 3

Thread 4

Thread 8

Thread 9

Thread 10

Thread 11

Banco 0

Banco 1

Banco 2

Banco 3

Banco 4

Banco 5

Banco 6

Banco 7

Banco 15

...

...

Sin conflictosDireccionamiento lineal

Sin conflictosDireccionamiento aleatorio

Con conflictosDireccionamiento lineal

stride = 2

miércoles 11 de mayo de 2011

Page 139: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

CONCEPTOS AVANZADOS

• Memoria de texturas

• Cache de texturas: optimizada para localidad espacial 2D

• Ventajas:

• Si no es posible coalescing sobre memoria global

• Mayor ancho de banda si existe localidad espacial

• Funciones especiales para acceso a datos (tex2D(...), ...)

• Memoria de constantes

• Cache de constantes

69

Otras memorias

miércoles 11 de mayo de 2011

Page 140: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

OPTIMIZACIÓN DE RENDIMIENTO

70

CONFIGURACIÓN DE LA EJECUCIÓN

Número de threads y bloques

•¡¡¡Experimentación!!!Pequeñas variaciones pueden ser clave

•La ejecución fallará si:•Superamos el máximo número de threads por bloque de la arquitectura

•Nuestro kernel requiere más recursos de los disponibles por MP:•Recurso: registro y cantidad de memoria compartida

miércoles 11 de mayo de 2011

Page 141: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

OPTIMIZACIÓN DE RENDIMIENTO

71

CONFIGURACIÓN DE LA EJECUCIÓN

Número de threads y bloques

•Número de bloques

•Al menos tantos como MPs

•Si creamos uno por MP:•Ciclos perdidos en sincronizaciones y accesos a memoria

• Dos o más por MP:•Solapamiento entre bloques en espera y bloques en ejecución

•Suficientemente alto para escalar en futuras arquitecturas

miércoles 11 de mayo de 2011

Page 142: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

OPTIMIZACIÓN DE RENDIMIENTO

72

CONFIGURACIÓN DE LA EJECUCIÓN

Número de threads y bloques

•Recomendaciones (CUDA Programming Guide)

•Mínimo número de bloques: 2 x (#MPs)

•O(100) bloques: escalabilidad a futuros dispositivos

•O(1000) bloques: escalabilidad a futuras generaciones

•Número de threads por bloque:•Múltiplo del tamaño de warp: evitar warps con menos de (32) threads•Aconsejable múltiplos de 64 threads por bloque

•Planificador optimizado para evitar conflictos en acceso a registros•Tamaños habituales: 192 - 256 threads por bloque

miércoles 11 de mayo de 2011

Page 143: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

HERRAMIENTAS SOFTWARE

• CUDA Toolkit (versión 2.3, 3.0 Beta ya disponible)

• Implementaciones para Linux, MacOS y Windows (7)

• CUDA SDK: ejemplos, scripts de compilación, utilidades, ...

• CUDA Profiler :

• Tiempos de ejecución, número de accessos coalesced, conflictos en acceso a memoria compartida, ...

• Nvidia Nexus

73

miércoles 11 de mayo de 2011

Page 144: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

DOCUMENTACIÓN

• CUDA Programming Guide

• CUDA Best Practices Guide

• CUDA Zone (http://www.nvidia.com/cuda)

• CUDA SDK (proporcionado con el paquete CUDA)

• GPGPU.org (http://www.gpgpu.org)

74

miércoles 11 de mayo de 2011

Page 145: INTRODUCCIÓN A LA PROGRAMACIÓN DE GPUS CON CUDAfigual/CUDA/tutorial_leon.pdf · MODELO DE PROGRAMACIÓN • Kernel ejecutado por un conjunto de threads • Los threads se agrupan

GRACIAS POR VUESTRA ATENCIÓN

¿¿...PREGUNTAS...??

75

[email protected]

http://www3.uji.es/~figualhttp://www.hpca.uji.es

miércoles 11 de mayo de 2011