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
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Computes the m a trix product using l i n e matrices :<br />
void mulMatCPU ( f l o a t∗ B, f l o a t∗ C, f l o a t∗ A, unsigned W) {<br />
for ( unsigned i n t i = 0; i < W; ++ i ) {<br />
for ( unsigned i n t j = 0; j < W; ++ j ) {<br />
A [ i ∗ W + j ] = 0 . 0 ;<br />
for ( unsigned i n t k = 0; k < W; ++k ) {<br />
A [ i ∗ W + j ] += B [ i ∗ W + k ] ∗ C[ k ∗ W + j ] ;<br />
}<br />
}<br />
}<br />
}<br />
Figura 1.20. C program that performs the<br />
matrix product A = B × C, using linearized<br />
matrices.<br />
__global__ void matMul ( f l o a t∗ B, f l o a t∗ C, f l o a t∗ A, i n t W) {<br />
f l o a t Pvalue = 0 . 0 ;<br />
i n t t x = b l o c k I d x . x ∗ blockDim . x + threadIdx . x ;<br />
i n t t y = b l o c k I d x . y ∗ blockDim . y + threadIdx . y ;<br />
for ( i n t k = 0; k < W; ++k ) {<br />
Pvalue += B [ t x ∗ W + k ] ∗ C[ k ∗ W + t y ] ;<br />
}<br />
A [ t y + t x ∗ W] = Pvalue ;<br />
}<br />
Figura 1.21. Kernel que realiza o produto<br />
matricial A = B×C. Esta é uma <strong>para</strong>lelização<br />
do algoritmo visto na figura 1.20.<br />
et al. [32] a fim <strong>de</strong> <strong>de</strong>monstrar o uso eficiente tanto da memória<br />
global quanto <strong>de</strong> outros níveis <strong>de</strong> memória, os quais serão<br />
apresentados em seções posteriores. Este kernel é obtido da<br />
<strong>para</strong>lelização do programa visto na figura 1.20.<br />
Em um mo<strong>de</strong>lo PRAM <strong>de</strong> processamento é possível realizar<br />
a multiplicação matricial em tempo O(ln N). Esta solução,<br />
contudo, não é exatamente trivial e nós iremos utilizar, em vez<br />
<strong>de</strong>la, uma solução O(N) no mo<strong>de</strong>lo PRAM, em que N é a largura<br />
da matriz. O algoritmo escrito em C <strong>para</strong> CUDA é dado<br />
na figura 1.21. Cada thread é reponsável pela produção <strong>de</strong> um<br />
24