13.04.2013 Views

Técnicas de Otimização de Código para Placas de ... - UFMG

Técnicas de Otimização de Código para Placas de ... - UFMG

Técnicas de Otimização de Código para Placas de ... - UFMG

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

__global__ void matMulUnroll ( f l o a t∗ B, f l o a t∗ C, f l o a t∗ Pd , i n t Width ) {<br />

__shared__ f l o a t Bs [ TILE_WIDTH ] [ TILE_WIDTH ] ;<br />

__shared__ f l o a t Cs [ TILE_WIDTH ] [ TILE_WIDTH ] ;<br />

i n t t x = threadIdx . x ;<br />

i n t t y = threadIdx . y ;<br />

i n t Row = b l o c k I dx . x ∗ TILE_WIDTH + t x ;<br />

i n t Col = b l o c k I d x . y ∗ TILE_WIDTH + t y ;<br />

f l o a t Pvalue = 0;<br />

for ( i n t m = 0; m < Width / TILE_WIDTH ; ++m) {<br />

Bs [ t y ] [ t x ] = B [ Col ∗ Width + (m ∗ TILE_WIDTH + t x ) ] ;<br />

Cs [ t y ] [ t x ] = C[Row + (m ∗ TILE_WIDTH + t y ) ∗ Width ] ;<br />

__syncthreads ( ) ;<br />

Pvalue += Bs [ t y ] [ 0] ∗ Cs [ 0 ] [ t x ] ;<br />

Pvalue += Bs [ t y ] [ 1] ∗ Cs [ 1 ] [ t x ] ;<br />

Pvalue += Bs [ t y ] [ 2] ∗ Cs [ 2 ] [ t x ] ;<br />

Pvalue += Bs [ t y ] [ 3] ∗ Cs [ 3 ] [ t x ] ;<br />

Pvalue += Bs [ t y ] [ 4] ∗ Cs [ 4 ] [ t x ] ;<br />

Pvalue += Bs [ t y ] [ 5] ∗ Cs [ 5 ] [ t x ] ;<br />

Pvalue += Bs [ t y ] [ 6] ∗ Cs [ 6 ] [ t x ] ;<br />

Pvalue += Bs [ t y ] [ 7] ∗ Cs [ 7 ] [ t x ] ;<br />

Pvalue += Bs [ t y ] [ 8] ∗ Cs [ 8 ] [ t x ] ;<br />

Pvalue += Bs [ t y ] [ 9] ∗ Cs [ 9 ] [ t x ] ;<br />

Pvalue += Bs [ t y ] [ 1 0 ] ∗ Cs [ 1 0 ] [ t x ] ;<br />

Pvalue += Bs [ t y ] [ 1 1 ] ∗ Cs [ 1 1 ] [ t x ] ;<br />

Pvalue += Bs [ t y ] [ 1 2 ] ∗ Cs [ 1 2 ] [ t x ] ;<br />

Pvalue += Bs [ t y ] [ 1 3 ] ∗ Cs [ 1 3 ] [ t x ] ;<br />

Pvalue += Bs [ t y ] [ 1 4 ] ∗ Cs [ 1 4 ] [ t x ] ;<br />

Pvalue += Bs [ t y ] [ 1 5 ] ∗ Cs [ 1 5 ] [ t x ] ;<br />

__syncthreads ( ) ;<br />

}<br />

Pd [ Col ∗ Width + Row] = Pvalue ;<br />

}<br />

Figura 1.26. Multiplicação <strong>de</strong> matrizes após<br />

a aplicação do <strong>de</strong>senrolamento.<br />

seja N 2 /2 o tamanho <strong>de</strong> cada ladrilho. Temos então, um total<br />

<strong>de</strong> 2(N/(N/2)) 2 ladrilhos. Cada ladrilho é lido N/(N/2) vezes.<br />

Portanto, temos 4N 2 leituras da memória global. Dadas as 2N 3<br />

leituras do programa original, observamos uma redução substancial<br />

<strong>de</strong>stes acesso, com consequâncias não menos notáveis<br />

<strong>para</strong> o <strong>de</strong>sempenho da aplicação ladrilhada: o programa da figura<br />

1.24 é cerca <strong>de</strong> 25% mais rápido que o programa da figura<br />

1.22, quando executado em uma placa GPU 9400M.<br />

Desenlaço. Aumentamos a taxa <strong>de</strong> trabalho útil realizado por<br />

um kernel diminuindo a quantida<strong>de</strong> <strong>de</strong> instruções que não realizam<br />

operações <strong>de</strong> ponto-flutuante – no caso <strong>de</strong> um programa<br />

que produz saída neste formato. Entre estas instruções, <strong>de</strong>stacam-se<br />

as operações <strong>de</strong> acesso à memória e as operações que<br />

controlam o fluxo do programa: <strong>de</strong>svios condicionais e incon-<br />

30

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!