PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

92
PyCUDA: Harnessing the power of GPU with Python

description

Fabrizio Milo

Transcript of PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

Page 1: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCUDA: Harnessing the power of GPU with Python

Page 2: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

1. Why a GPU ? 2. How does It works ? 3. How do I Program it ? 4. Can I Use Python ?

Talk Structure

Page 3: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

1. Why a GPU ? 2. How does It works ? 3. How do I Program it ? 4. Can I Use Python ?

Talk Structure

Page 4: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

WHY A GPU ?

Page 5: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

APPLICATIONS & DEMOS

Page 6: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Why GPU?

Page 7: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

1. Why a GPU ? 2. How does it works ? 3. How do I Program it ? 4. Can I Use Python ?

Talk Structure

Page 8: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

How does it works ?

Page 9: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Cache

ALU

Control

ALU

ALU

ALU

DRAM

CPU

Page 10: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

DRAM

GPU

Page 11: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

DRAM

Cache

ALU Control

ALU

ALU

ALU

DRAM

CPU GPU

Page 12: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

CUDA

Page 13: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Compute Unified Device Architecture

Page 14: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

CUDA

A Parallel Computing Architecture for NVIDIA GPUs

Direct X Compute

Page 15: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Execution Model

CUDA Device Model

Page 16: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

EXECUTION MODEL

Page 17: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Thread

Smallest unit of logic

Page 18: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

A Block

A Group of Threads

Page 19: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

A Grid

A Group of Blocks

Page 20: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

One Block can have many threads

Page 21: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

One Grid can have many blocks

Page 22: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

DEVICE MODEL The hardware

Page 23: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Scalar Processor

Page 24: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Scalar Processor

Page 25: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Many Scalar Processors

Page 26: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

+ Register File

Page 27: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

+ Shared Memory

Page 28: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Multiprocessor

Page 29: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Device

Page 30: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Real Example: 10-Series Architecture

"  240 Scalar Processor (SP) cores execute kernel threads "  30 Streaming Multiprocessors (SMs) each contain "  8 scalar processors "  1 double precision unit "  Shared memory

Page 31: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Software Hardware

Thread

Scalar Processor

Thread Block Multiprocessor

Grid Device

Page 32: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Global Memory

Page 33: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Global Memory

Page 34: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Host - Device

RAM

Global Memory CPU

Page 35: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Host – Multi Device

RAM

CPU

Page 36: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

1. Why a GPU ? 2. How does It works ? 3. How do I Program it ? 4. Can I Use Python ?

Page 37: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Software Hardware

Thread

Scalar Processor

Thread Block Multiprocessor

Grid Device

Page 38: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

__global__ void multiply_them( float *dest, float *a, float *b )

{ const int i = threadIdx.x; dest[i] = a[i] * b[i];}

Kernel

Thread

Page 39: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

__global__ void multiply_them( float *dest, float *a, float *b )

{ const int i = threadIdx.x; dest[i] = a[i] * b[i];}

Kernel

Thread

Page 40: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

__global__ void multiply_them( float *dest, float *a, float *b )

{ const int i = threadIdx.x; dest[i] = a[i] * b[i];}

Kernel

Block

Page 41: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

__global__ void kernel( … ){ const int idx =

blockIdx.x * blockDim.x + threadIdx.x;…

}

Kernel

Grid

Page 42: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

.bin

NVCC

How do I Program it ?

GCC

.cubin CPU GPU

Kernel Main Logic

Page 43: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

.bin

NVCC

How do I Program it ?

GCC

.cubin

CPU

GPU

Kernel Main Logic

..bin .cubin

Page 44: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Host - Device

RAM

Global Memory CPU

Page 45: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

RAM

Global Memory CPU

Page 46: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Allocate Memory

cudaMalloc( pointer, size )

Page 47: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Copy to device

cudaMalloc( pointer, size )

cudaMemcpy( dest, src, size, direction)

Page 48: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Kernel Launch

cudaMalloc( pointer, size )

cudaMemcpy( dest, src, size, direction)

Kernel<<< # blocks, # threads >> (*params)

Page 49: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Get Back the Results

cudaMalloc( pointer, size )

cudaMemcpy( dest, src, size, direction)

Kernel<<< # blocks, # threads >> (*params)

cudaMemcpy( dest, src, size, direction)

Page 50: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Error Handling

If(cudaMalloc( pointer, size ) != cudaSuccess){ handle_error()}

Page 51: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

And soon it becomes …

If(cudaMalloc( pointer, size ) != cudaSuccess){ handle_error()}

if (cudaMemcpy( dest, src, size, direction ) == cudaSuccess) {}

If (Kernel<<< # blocks, # threads >> (*params) != cudaSuccess){ handle_error()}

If( cudaMemcpy( dest, src, size, direction) != cudaSuccess) { }

Page 52: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

And soon it becomes …

If(cudaMalloc( pointer, size ) != cudaSuccess){ handle_error()}

if (cudaMemcpy( dest, src, size, direction ) == cudaSuccess) {}

If (Kernel<<< # blocks, # threads >> (*params) != cudaSuccess){ handle_error()}

If( cudaMemcpy( dest, src, size, direction) != cudaSuccess) { }

If(cudaMalloc( pointer, size ) != cudaSuccess){ handle_error()}

if (cudaMemcpy( dest, src, size, direction ) == cudaSuccess) {}

If (Kernel<<< # blocks, # threads >> (*params) != cudaSuccess){ handle_error()}

If( cudaMemcpy( dest, src, size, direction) != cudaSuccess) { }

If(cudaMalloc( pointer, size ) != cudaSuccess){ handle_error()}

if (cudaMemcpy( dest, src, size, direction ) == cudaSuccess) {}

If (Kernel<<< # blocks, # threads >> (*params) != cudaSuccess){ handle_error()}

If( cudaMemcpy( dest, src, size, direction) != cudaSuccess) { }

If(cudaMalloc( pointer, size ) != cudaSuccess){ handle_error()}

if (cudaMemcpy( dest, src, size, direction ) == cudaSuccess) {}

If (Kernel<<< # blocks, # threads >> (*params) != cudaSuccess){ handle_error()}

If( cudaMemcpy( dest, src, size, direction) != cudaSuccess) { }

If(cudaMalloc( pointer, size ) != cudaSuccess){ handle_error()}

if (cudaMemcpy( dest, src, size, direction ) == cudaSuccess) {}

If (Kernel<<< # blocks, # threads >> (*params) != cudaSuccess){ handle_error()}

If( cudaMemcpy( dest, src, size, direction) != cudaSuccess) { }

If(cudaMalloc( pointer, size ) != cudaSuccess){ handle_error()}

if (cudaMemcpy( dest, src, size, direction ) == cudaSuccess) {}

If (Kernel<<< # blocks, # threads >> (*params) != cudaSuccess){ handle_error()}

If( cudaMemcpy( dest, src, size, direction) != cudaSuccess) { }

Page 53: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Page 54: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

1. Why a GPU ? 2. How does It works ? 3. How do I Program it ? 4. Can I Use Python ?

Page 55: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

= PYCUDA

+

& ANDREAS KLOCKNER

Page 56: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

PyCuda Philosopy

Provide Complete Access

Page 57: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

PyCuda Philosopy

AutoMatically Manage

Resources

Page 58: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

PyCuda Philosopy

Check and Report Errors

Page 59: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

PyCuda Philosopy

Cross Platform

Page 60: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

PyCuda Philosopy

Allow Interactive

Use

Page 61: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

PyCuda Philosopy

NumPy Integration

Page 62: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

NUMPY - ARRAY

Page 63: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

import numpy

my_array = numpy.array([1,] * 100)

1 1 1 1 1 1

99 0

Page 64: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

import numpy

my_array = numpy.array([1,] * 100)

my_array[3] = 0

0 1 1 1 1 1

Page 65: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

PyCuda: Workflow

Page 66: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

PyCuda: Workflow

Page 67: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

PyCuda: Workflow

Page 68: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Memory Allocation

cuda.mem_alloc( size_bytes )

Page 69: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Memory Copy

gpu_mem = cuda.mem_alloc( size_bytes )

cuda.memcpy_htod( gpu_mem, cpu_mem )

Page 70: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Kernel

gpu_mem = cuda.mem_alloc( size_bytes )

cuda.memcpy_htod( gpu_mem, cpu_mem )

SourceModule(“””__global__ void multiply_them( float *dest, float *a,

float *b ){ const int i = threadIdx.x; dest[i] = a[i] * b[i];}”””)

Page 71: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Kernel Launch

mod = SourceModule(“””__global__ void multiply_them( float *dest, float *a,

float *b ){ const int i = threadIdx.x; dest[i] = a[i] * b[i];}”””)

multiply_them = mod.get_function(“multiply_them”)multiply_them ( *args, block=(30, 64, 1))

Page 72: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Page 73: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Page 74: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Page 75: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

DEMO Hello Gpu

Page 76: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

GPUARRAY

Page 77: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

gpuarray

Page 78: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

gpuarray.to_gpu(numpy array)

numpy array = gpuarray.get()

PyCuda: GpuArray

Page 79: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

gpuarray.to_gpu(numpy array)

numpy array = gpuarray.get()

PyCuda: GpuArray

+, -, !, /, fill, sin, exp, rand, basic indexing, norm, inner product …

Page 80: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

PyCuda: GpuArray: ElementWise

from pycuda.elementwise import ElementwiseKernel

Page 81: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

PyCuda: GpuArray: ElementWise

from pycuda.elementwise import ElementwiseKernel

lincomb = ElementwiseKernel( ” float a , float !x , float b , float !y , float !z”, ”z [ i ] = a !x[ i ] + b!y[i ] ”

)

Page 82: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

PyCuda: GpuArray: ElementWise

from pycuda.elementwise import ElementwiseKernel

lin comb = ElementwiseKernel( ” float a , float !x , float b , float !y , float !z”, ”z [ i ] = a !x[ i ] + b!y[i ] ”

)

c gpu = gpuarray. empty like (a gpu) lincomb (5, a gpu, 6, b gpu, c gpu)

assert la . norm((c gpu ! (5!a gpu+6!b gpu)).get()) < 1e!5

Page 83: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Meta-Programming

__kernel_template__ = “””__global__ void kernel( args ){

for (int i=0; i={{ iterations }}; i++){ {{operations}}}

}”””

See for example jinja2

Page 84: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Meta-Programming

Page 85: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Meta-Programming

Generate Source !

Page 86: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

Performances ?

Page 87: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

DEMO mandelbrot

Page 88: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

PyCuda: Documentation

Page 89: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

PyCuda

WebSite: http://mathema.tician.de/software/ pycuda

License: X Consortium License

(no warranty, free for all use)

Dependencies: Python 2.4+, numpy, Boost

Page 90: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

In the Future …

OPENCL

Page 91: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

THANK YOU & HAVE FUN !

Page 92: PyCuda: Come sfruttare la potenza delle schede video nelle applicazioni python

PyCon 4 – Florence 2010 – Fabrizio Milo

?