BSP/CGM Algorithms - USP
BSP/CGM Algorithms - USP
BSP/CGM Algorithms - USP
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Workshop Integrade<br />
26 e 27 de Janeiro de 2004 - IME-<strong>USP</strong>-São Paulo-SP<br />
1/24<br />
<strong>BSP</strong>/<strong>CGM</strong> <strong>Algorithms</strong><br />
Edson Norberto Cáceres<br />
Henrique Mongelli<br />
Siang Wun Song<br />
CNPq and FAPESP
Outline<br />
1. Parallel and Distributed Computation Systems √<br />
2. Parallel <strong>Algorithms</strong> and Complexity √<br />
3. Models of Parallel and Distributed Computation √<br />
• Shared Memory Model √<br />
• Network Model √<br />
• Realistic Models<br />
4. Implementation of Parallel <strong>Algorithms</strong><br />
• MPI<br />
• PVM<br />
• <strong>BSP</strong><br />
• <strong>CGM</strong><br />
2/24
Realistic Models<br />
3/24<br />
At the end of the 80´s the area of parallel algorithms<br />
went through a serious crisis.<br />
Several theoretical results and for specific machines (meshes<br />
and hypercubes).<br />
Practical results gave disappointing speedups.<br />
Programs without portability.<br />
• <strong>BSP</strong><br />
• <strong>CGM</strong><br />
• LogP √
The <strong>BSP</strong> Model<br />
4/24<br />
• The <strong>BSP</strong> (Bulk Synchronous Parallel) model was introduced<br />
by Valiant.<br />
• A <strong>BSP</strong> machine consists of p processors with local<br />
memory.<br />
• The processors communicate with each other through<br />
some interconnection scheme, managed by a router.<br />
• Also offers the capability of synchronizing the processors<br />
in regular intervals of L units of time.
The <strong>BSP</strong> Model<br />
5/24<br />
• <strong>BSP</strong> algorithm consists of a sequence of supersteps.<br />
• In each superstep each processor executes a combination<br />
of local computation, transmission and receiving<br />
of messages from other processors.<br />
• After each period of L units of time a barrier synchronization<br />
is carried out.
The <strong>BSP</strong> Model<br />
6/24
The <strong>BSP</strong> Model<br />
7/24<br />
• Parâmetros:<br />
– n : tamanho do problema.<br />
– p: número de processadores com memória local.<br />
– L: tempo máximo de um superpasso (periodicidade)<br />
- latência.<br />
– g: taxa de eficiência da computação/comunicação.<br />
• Custo (superpasso i): w i + gh i + L, w i = max{L, t 1 , . . . , t p }<br />
e h i = max{L, c 1 , . . . , c p }<br />
• Custo Total: W + gH + LT , W = ∑ p<br />
i=1 w i, H = ∑ p<br />
i=1 h i.
The <strong>CGM</strong> Model<br />
8/24<br />
• O modelo <strong>CGM</strong> (Coarse Grained Multicomputer) foi<br />
proposto por Dehne et al.<br />
• Utiliza apenas dois parâmetros:<br />
1. N: tamanho da entrada.<br />
2. P: número de processadores.<br />
• Uma máquina <strong>CGM</strong> consiste de um conjunto de p processadores,<br />
cada um com memória local de tamanho<br />
O(n/p).<br />
• Os processadores se comunicam através de um meio<br />
de interconexão.
The <strong>CGM</strong> Model<br />
9/24<br />
Computation round<br />
Communication round<br />
P p−1<br />
P 2<br />
P 1<br />
P 0<br />
Global Communication<br />
Synchronization Barrier<br />
Local computation
The <strong>CGM</strong> Model<br />
10/24<br />
• Um algoritmo <strong>CGM</strong> consiste de uma seqüência alternada<br />
de rodadas de computação e rodadas de comunicação<br />
separadas por uma barreira de sincronização.<br />
• Na fase de comunicação cada processador troca no<br />
máximo um total de O(n/p) dados com outros processadores.<br />
• Custo:<br />
– Computação: análogo ao <strong>BSP</strong>.<br />
– Comunicação: #H n,p onde H n,p é o custo único para<br />
cada rodada de comunicação.
<strong>BSP</strong>: Sum Algorithm<br />
11/24<br />
<strong>BSP</strong> Sum Algorithm(i,p,n,B = b((i − 1)r + 1 : ir);S)<br />
1 z ← b[1] + ... + b[r];<br />
2 if i = 1<br />
3 then S ← z;<br />
4 else send(z, p 1 );<br />
5 bspsync()<br />
6 if i = 1<br />
7 then for i ← 2 to p<br />
8 do receive(z, p i );<br />
9 S ← S + z;
<strong>BSP</strong>: Algorithm Complexity<br />
12/24<br />
• Passo 1: cada p i efetua r operações<br />
• Passo 2-3: p 1 efetua uma operação<br />
• Passo 4: p i ’s (diferentes de 1) enviam uma MSG<br />
• Passo 5: Sincronização<br />
• Passo 6-9: p 1 recebe p − 1 mensagens e efetua p − 1<br />
operações.<br />
Uma sincronização.<br />
Dois superpassos.<br />
O(n/p) computação.
<strong>CGM</strong>: Sum Algorithm<br />
13/24<br />
<strong>CGM</strong> Sum Algorithm(i,p,n,B = b((i − 1)r + 1 : ir);S)<br />
1 z i ← b[1] + ... + b[r];<br />
2 if i = 1<br />
3 then S ← z 1 ;<br />
4 else send(z, p 1 );<br />
5 if i = 1<br />
6 then for i ← 2 to p<br />
7 do receive(z i , p i );<br />
8 S ← S + z 2 + . . . + z p ;
<strong>CGM</strong>: Sum Algorithm - Complexity<br />
14/24<br />
• Passo 1: cada p i efetua r operações<br />
• Passo 2-3: p 1 efetua uma operação<br />
• Passo 4: p i ’s (diferentes de 1) enviam uma MSG<br />
• Passo 5-8: p 1 recebe p − 1 mensagens e efetua p − 1<br />
operações.<br />
Uma rodada de comunicação.<br />
O(n/p) computação.
<strong>BSP</strong>/<strong>CGM</strong>: Prefix Sum Algorithm<br />
15/24<br />
Given a vector A of n elements A[0], . . . , A[n − 1],<br />
prefix sum computes the values:<br />
A[0]<br />
A[0] ⊕ A[1]<br />
A[0] ⊕ A[1] ⊕ A[2]<br />
.<br />
A[0] ⊕ . . . ⊕ A[n − 1]<br />
where ⊕ is a binary associative operation.
<strong>BSP</strong>/<strong>CGM</strong>: Prefix Sum Algorithm<br />
16/24<br />
Example:<br />
⊕ is addition.<br />
Input Vector:<br />
[3 7 5 1 2 4 0 9]<br />
Result Vector:<br />
[3 10 15 16 18 22 22 31]
<strong>BSP</strong>/<strong>CGM</strong>: Prefix Sum Algorithm<br />
17/24<br />
Example:<br />
Quatro Processadores: P 1 , P 2 , P 3 , P 4<br />
Input Vector: [3 7 5 1 2 4 0 9]<br />
P 1 = [3 7], P 2 = [5 1], P 3 = [2 4] e P 4 = [0 9]<br />
SP 1 = 10, SP 2 = 6, SP 3 = 6 e SP 4 = 9<br />
P 1 ← 0, P 2 ← SP 1 , P 3 ← SP 1 + SP 2 e P 4 ← SP 1 + SP 2 + SP 3<br />
P 1 ← 0, P 2 ← 10, P 3 ← 16 e P 4 ← 22<br />
Result Vector: [3 10 15 16 18 22 22 31]
<strong>BSP</strong>/<strong>CGM</strong>: Prefix Sum Algorithm<br />
18/24<br />
Prefix Sum Algorithm(i,p,n,A[(i − 1)n/p + 1 : in/p];S)<br />
1 T ← A[(i − 1) ∗ n/p + 1] + . . . + A[i ∗ n/p];<br />
2 for j ← i+1 to p<br />
3 do send(T, p j );<br />
4 bspsync();<br />
5 for k ← 1 to i − 1<br />
6 do receive(p k , T [k]);<br />
7 ST ← T [1] + . . . + T [i − 1];<br />
8 for j ← (i − 1) ∗ n/p + 1 to i ∗ n/p<br />
9 do S[j] ← ST + ∑ j<br />
k=1 A[k];
Prefix Sum - <strong>BSP</strong><br />
// Passo 1. Inicilizacao<br />
bsplib_saveargs(&argc, &argv);<br />
bsplib_init(<strong>BSP</strong>LIB_STDPARAMS, &bsp); // iniciliza o pub<br />
size = bsp_nprocs(&bsp); // numero de tarefas<br />
rank = bsp_pid(&bsp); // identificacao da tarefa<br />
// Passo 2. Envie os dados as tarefas filhos<br />
bsp_push_reg(&bsp, &numelem, sizeof(int));<br />
bsp_get(&bsp, 0, &numelem, 0, &numelem, sizeof(int));<br />
bsp_sync(&bsp);<br />
bsp_pop_reg(&bsp, &numelem);<br />
tam = (int) numelem/size;<br />
// Passo 3. Receba em SubVetor rank*tam-esima parte de VetorDados<br />
bsp_push_reg(&bsp, VetorDados, numelem*sizeof(int));<br />
bsp_get(&bsp, 0, VetorDados, rank*tam*sizeof(int), SubVetor, tam*sizeof(int));<br />
bsp_sync(&bsp);<br />
bsp_pop_reg(&bsp, VetorDados);<br />
// Passo 4. Calcule a Soma em cada tarefa<br />
for (i = 0; i < tam; i++)<br />
SomaP = SomaP + SubVetor[i];<br />
soma = SomaP;<br />
19/24
Prefix Sum - <strong>BSP</strong><br />
// Passo 5. Receba as Somas das tarefas com pid < rank<br />
for (i = 0; i < size-1; i++) {<br />
bsp_push_reg(&bsp, &soma, sizeof(int));<br />
if (rank > i) {<br />
bsp_get(&bsp, i, &soma, 0, &soma, sizeof(int));}<br />
bsp_sync(&bsp);<br />
if (rank > i) {<br />
Soma = Soma + soma;}<br />
bsp_pop_reg(&bsp, &soma);<br />
soma = SomaP;<br />
}<br />
// Passo 6. Calcule as Somas de Prefixos em cada tarefa<br />
SPre[0] = Soma + SubVetor[0];<br />
for (i = 1; i < tam; i++)<br />
SPre[i] = SPre[i-1] + SubVetor[i];<br />
// Passo 8. Armazene a soma de prefixos na tarefa 0<br />
for (i = 0; i < tam; i++)<br />
printf("rank %d e SPre[%d]: %d\n",rank, i, SPre[i]);<br />
// Passo 7. Finalize o <strong>BSP</strong><br />
bsplib_done();<br />
20/24
Prefix Sum - <strong>BSP</strong><br />
21/24<br />
// Passo 7. Envie os valores para a raiz<br />
bsp_push_reg(&bsp, SomaPre, numelem*sizeof(int));<br />
bsp_put(&bsp, 0, SPre, SomaPre, tam*rank*sizeof(int), tam*sizeof(int));<br />
bsp_sync(&bsp);<br />
bsp_pop_reg(&bsp, SomaPre);<br />
// Passo 8. Armazene a soma de prefixos na tarefa 0<br />
if (rank == root) {<br />
// Passo 8.1. Abre o arquivo<br />
ArqS = fopen("ArquivoS.txt", "a");<br />
// Passo 8.2. Escreve no arquivo<br />
fprintf(ArqS, "%i %i\n", numelem, size);<br />
fclose(ArqS);<br />
for (i = 0; i < numelem; i++) {<br />
fprintf(ArqS, "%d ", SomaPre[i]);<br />
}<br />
}
Prefix Sum - MPI<br />
// Passo 1. Inicilizacao<br />
MPI_Init(&argc, &argv);<br />
MPI_Comm_size(MPI_COMM_WORLD, &size); // numero de tarefas<br />
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // identificacao da tarefa<br />
tam = (int) TAMMAX/size; // tamanho subvetor<br />
// Passo 2. Envie os dados as tarefas filhos<br />
MPI_Scatter(VD, tam, MPI_INT, SV, tam, MPI_INT, root, MPI_COMM_WORLD);<br />
// Passo 3. Calcule a Soma em cada tarefa<br />
for (i = 0; i < tam; i++)<br />
SomaP = SomaP + SV[i];<br />
// Passo 4. Receba as Somas Parciais<br />
MPI_Scan(&SomaP, &Soma, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);<br />
// Passo 5. Calcule as Somas Parciais em cada tarefa<br />
SomaPre[0] = Soma - SomaP + SV[0];<br />
for (i = 1; i < tam; i++)<br />
SomaPre[i] = SomaPre[i-1] + SV[i];<br />
// Passo 6. Imprima o valor da Soma (Processador 0)<br />
for (i = 0; i < tam; i++)<br />
printf("rank %d e SomaPre[%d]: %d\n",rank, i, SomaPre[i]);<br />
// Passo 7. Finalize o MPI<br />
MPI_Finalize();<br />
22/24
Conclusions<br />
23/24<br />
• O <strong>BSP</strong>/<strong>CGM</strong> é um modelo adequado para o projeto<br />
de algoritmos paralelos.<br />
• A Biblioteca MPI utiliza um número menor de sincronizações<br />
explicítas.<br />
• A utilização excessiva de sincronizações pode comprometer<br />
a execução de programas paralelos em ambientes<br />
de grade.
References<br />
24/24<br />
• Cáceres, E. N., Mongelli, H. and Song, S. W. Algoritmos Paralelos usando<br />
<strong>CGM</strong>/PVM/MPI: Uma Introdução. JAI/SBC 2001, Fortaleza, 2001.(versão<br />
atualizada www.ime.usp.br/∼song).