What`s wrong with my code
Mathias Brito
mathiasbrito at yahoo.com.br
Mon Nov 3 07:53:54 EST 2003
Well, I avoided to send my code, because it is not the
best way to solve the problem, and I using only the
basics calls of MPI. The programam make the sum of two
matrices. It generate two matrices ramdomically and
sum it. But it didn`t work with a matrix greather than
834x834. I dont kwon why. Some variables and functions
have portuguese names, but i commented it to say what
it do.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <mpi.h>
#define LINHAS 835 /*Number of lines*/
#define COLUNAS 835 /*Number of colums*/
#define TRUE 1
#define FALSE 0
void juntar(int *, int*); /*put the result of local
operation in the final result matrix*/
void somar(int *, int*, int*); /*make the sum*/
void imprimir(int[][COLUNAS]); /*print a matrix*/
void inicializar(int[][COLUNAS]); /*initialize matrix
with ramdom numbers*/
int main(int argc, char *argv[]) {
int minha_parte1[LINHAS], /*my_part, my_result*/
minha_parte2[LINHAS], meu_resultado[LINHAS] = {0};
int size, my_rank;
int i, j, tag = 0;
int master = 0;
int sair = 0; /*exit = 0*/
MPI_Status status;
srand(time(NULL));
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
if(my_rank == master) {
int matriz1[LINHAS][COLUNAS],
matriz2[LINHAS][COLUNAS], resultado[LINHAS][COLUNAS];
int linhas_env = 0; /*number of line sent*/
int linhas_rec = 0; /*number of lines received*/
inicializar(matriz1);
inicializar(matriz2);
//imprimir(matriz1);
//imprimir(matriz2);
for(i = 1; i < size; i++) {
if(linhas_env < LINHAS) {
if(MPI_Send(&matriz1[linhas_env][0], COLUNAS,
MPI_INT, i, tag, MPI_COMM_WORLD) == MPI_ERR_BUFFER)
printf("ERRO\n");
MPI_Send(&matriz2[linhas_env][0], COLUNAS,
MPI_INT, i, tag, MPI_COMM_WORLD);
//printf("MASTER: Enviando dados para o
processo %d\n", i);
linhas_env++;
}
}
i = 1;
while(TRUE) {
if(linhas_rec < LINHAS) {
MPI_Recv(&meu_resultado, COLUNAS, MPI_INT, i,
tag, MPI_COMM_WORLD, &status);
juntar(&resultado[linhas_rec][0],
meu_resultado);
//printf("MASTER: Recebendo dados do processo
%d. Total de linhas recebidas = %d\n", i, linhas_rec +
1);
linhas_rec++;
}
else
break;
if(linhas_env < LINHAS) {
MPI_Send(&matriz1[linhas_env][0], COLUNAS,
MPI_INT, i, tag, MPI_COMM_WORLD);
MPI_Send(&matriz2[linhas_env][0], COLUNAS,
MPI_INT, i, tag, MPI_COMM_WORLD);
//printf("MASTER: Enviado mais dados para o
processo %d. Total de linhas enviadas = %d\n", i,
linhas_env + 1);
linhas_env++;
}
if(i == size - 1)
i = 1;
else
i++;
}
for(i = 1; i < size; i++) {
MPI_Send(&sair, COLUNAS, MPI_INT, i, tag,
MPI_COMM_WORLD);
//printf("MASTER: Finalizando processo %d\n",
i);
}
printf("\n\n");
//imprimir(resultado);
}
else {
while(TRUE) {
MPI_Recv(&minha_parte1, COLUNAS, MPI_INT, master,
tag, MPI_COMM_WORLD, &status);
if(minha_parte1[0] == 0)
break;
MPI_Recv(&minha_parte2, COLUNAS, MPI_INT, master,
tag, MPI_COMM_WORLD, &status);
somar(minha_parte1, minha_parte2, meu_resultado);
MPI_Send(&meu_resultado, COLUNAS, MPI_INT,
master, tag, MPI_COMM_WORLD);
}
}
MPI_Finalize();
return 0;
}
void juntar(int *m, int *r) {
int i;
for(i = 0; i < COLUNAS; i++) {
m[i] = r[i];
}
}
void somar(int m[], int n[], int r[]) {
int i;
for(i = 0; i < COLUNAS; i++)
r[i] = m[i] + n[i];
}
void imprimir(int m[][COLUNAS]) {
int i, j;
for(i = 0;i < LINHAS; i++) {
for(j = 0; j < COLUNAS; j++) {
printf("%d\t", m[i][j]);
}
printf("\n");
}
printf("\n\n");
}
void inicializar(int m[][COLUNAS]) {
int i, j;
for(i = 0; i < LINHAS; i++) {
for(j = 0; j < COLUNAS; j++) {
m[i][j] = (rand() % 10) + 1;
}
}
}
=====
Mathias Brito
Universidade Estadual de Santa Cruz - UESC
Departamento de Ciências Exatas e Tecnológicas
Estudante do Curso de Ciência da Computação
Yahoo! Mail - o melhor webmail do Brasil
http://mail.yahoo.com.br
_______________________________________________
Beowulf mailing list, Beowulf at beowulf.org
To change your subscription (digest mode or unsubscribe) visit http://www.beowulf.org/mailman/listinfo/beowulf
More information about the Beowulf
mailing list