Created
December 16, 2016 15:55
-
-
Save jjvillavicencio/2509f47aa759c6e2699b4dc9258e2966 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <mpi.h> | |
int main (int argc, char* argv[]) | |
{ | |
int rank, size, origen, destino, ndat, tag; | |
int n, i, j; | |
int suma = 0; | |
int cont = 0; | |
int pares[5]; | |
int factorial[100]; | |
int factoria = 1; | |
double start, end; | |
MPI_Status info; | |
MPI_Init (&argc, &argv); /* Empezar MPI */ | |
MPI_Comm_rank (MPI_COMM_WORLD, &rank); /* Obtener el ID del proceso actual*/ | |
MPI_Comm_size (MPI_COMM_WORLD, &size); /* Objetener el numero de proceso */ | |
MPI_Barrier(MPI_COMM_WORLD); | |
if (rank == 0){ | |
start=MPI_Wtime(); | |
printf("********************************\n"); | |
printf("*##############################*\n"); | |
printf("*# Procesador 0 calcúla pares #*\n"); | |
printf("*##############################*\n"); | |
printf("********************************\n"); | |
printf("\nIngresa la cantidad de números a calcular >_"); | |
scanf("%d",&n); | |
for(i = 0; i < n; i++){ | |
if (cont%2 == 0){ | |
pares[i] = cont; | |
}else{ | |
i = i - 1; | |
} | |
cont += 1; | |
} | |
printf("\n Los pares son:\n"); | |
for( i = 0; i < n; i++){ | |
if (i != n-1){ | |
printf("[%d!] + ", pares[i]); | |
}else{ | |
printf("[%d!]\n", pares[i]); | |
} | |
} | |
destino = 1; | |
tag = 0; | |
MPI_Send(&n, 1, MPI_INT, destino, tag, MPI_COMM_WORLD); | |
MPI_Send(&n, 1, MPI_INT, 2, tag, MPI_COMM_WORLD); | |
MPI_Send(&pares[0], n, MPI_INT, destino, tag, MPI_COMM_WORLD); | |
end=MPI_Wtime(); | |
}else if (rank == 1){ | |
start=MPI_Wtime(); | |
origen = 0; | |
tag = 0; | |
MPI_Recv(&n, 1, MPI_INT, origen, tag, MPI_COMM_WORLD, &info); | |
MPI_Recv(&pares[0], n, MPI_INT, origen, tag, MPI_COMM_WORLD, &info); | |
MPI_Get_count (&info, MPI_INT, &ndat); | |
printf("\n"); | |
printf("************************************\n"); | |
printf("*##################################*\n"); | |
printf("*# Procesador 1 calcúla factorial #*\n"); | |
printf("*# recv: %d ndat: %d #*\n",info.MPI_SOURCE, ndat); | |
printf("*##################################*\n"); | |
printf("************************************\n"); | |
for(i = 0; i < n; i++){ | |
for(j = 1; j <= pares[i]; ++j){ | |
factoria *= j; | |
} | |
factorial[i]=factoria; | |
factoria = 1; | |
} | |
printf("\n Los factoriales son:\n"); | |
for( i = 0; i < n; i++){ | |
if (i != n-1){ | |
printf("[%d] + ", factorial[i]); | |
}else{ | |
printf("[%d]\n", factorial[i]); | |
} | |
} | |
destino = 2; | |
tag = 1; | |
MPI_Send(&factorial[0], n, MPI_INT, destino, tag, MPI_COMM_WORLD); | |
end=MPI_Wtime(); | |
}else if (rank == 2){ | |
start=MPI_Wtime(); | |
origen = 1; | |
tag = 1; | |
MPI_Recv(&n, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &info); | |
MPI_Recv(&factorial[0], n, MPI_INT, origen, tag, MPI_COMM_WORLD, &info); | |
MPI_Get_count (&info, MPI_INT, &ndat); | |
printf("\n"); | |
printf("************************************\n"); | |
printf("*##################################*\n"); | |
printf("*# Procesador 2 suma factorial #*\n"); | |
printf("*# recv: %d ndat: %d #*\n",info.MPI_SOURCE, ndat); | |
printf("*##################################*\n"); | |
printf("************************************\n"); | |
printf("\n La suma de factoriales es:\n"); | |
for( i = 0; i < n; i++){ | |
suma += factorial[i]; | |
} | |
printf("[ %d ]", suma); | |
end=MPI_Wtime(); | |
} | |
MPI_Finalize(); | |
printf("\nRUNTIME FOR PROCESS %d is %lf\n",rank,end-start); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment