Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <mpi.h>
- //Comandos para compilar y ejecutar
- //mpicc main.c -o main -lm
- //mpirun --hostfile hostfile -np 90 main 10 A
- void inicializaCadena(char *cadena, int n){
- int i;
- for(i=0; i<n/2; i++){
- cadena[i] = 'A';
- }
- for(i=n/2; i<3*n/4; i++){
- cadena[i] = 'C';
- }
- for(i=3*n/4; i<9*n/10; i++){
- cadena[i] = 'G';
- }
- for(i=9*n/10; i<n; i++){
- cadena[i] = 'T';
- }
- }
- void MPI_BinomialColectiva(){
- }
- void MPI_FlatreeColectiva(void * buff, void * recvbuff, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm){
- int i; int *a; int *final; int rank;
- MPI_Comm_rank (MPI_COMM_WORLD , &rank);
- MPI_Status status;
- a=(int*)buff;
- if(rank == 0) {
- for(i=1; i<count; i++){
- int count2=0;
- MPI_Recv(&count2, 1, MPI_INT, i,0 ,MPI_COMM_WORLD, &status);
- *a=*a+count2;
- }
- }else{
- MPI_Send(a, 1, MPI_INT, 0,0 ,MPI_COMM_WORLD);
- }
- final=(int*)recvbuff;
- *final=*a;
- }
- void MPI_BinomialBcast(void * buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm){
- int rank; int i; int n; char L; int numprocs; int j;
- MPI_Status status;
- numprocs=count;
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- for(i=0; i<numprocs; i++){
- if(rank<pow(2,i-1)) {
- MPI_Send(&n, 1, MPI_INT, rank + pow(2, i - 1), 0, MPI_COMM_WORLD);
- MPI_Send(&L, 1, MPI_CHAR, rank + pow(2, i - 1), 0, MPI_COMM_WORLD);
- }
- }
- for(i=1;i<=numprocs;i++) {
- for(j=0; numprocs<rank+pow(2,i-1);j++){
- MPI_Recv(&n, 1, MPI_INT, rank-pow(2,j-1), 0, MPI_COMM_WORLD, &status);
- MPI_Recv(&L, 1, MPI_CHAR, rank-pow(2,j-1), 0, MPI_COMM_WORLD, &status);
- }
- }
- }
- int main(int argc, char *argv[])
- {
- int numprocs , rank , namelen;
- char processor_name [MPI_MAX_PROCESSOR_NAME];
- MPI_Init(&argc, &argv);
- MPI_Comm_size (MPI_COMM_WORLD , &numprocs);
- MPI_Comm_rank (MPI_COMM_WORLD , &rank);
- MPI_Get_processor_name (processor_name , &namelen);
- if(argc != 3){
- printf("Numero incorrecto de parametros\nLa sintaxis debe ser: program n L\n program es el nombre del ejecutable\n n es el tamaño de la cadena a generar\n L es la letra de la que se quiere contar apariciones (A, C, G o T)\n");
- exit(1);
- }
- int i, n, count=0, recibir=0;
- char *cadena;
- char L;
- MPI_Status status;
- if(rank==0) {
- n = atoi(argv[1]);
- L = *argv[2];
- }
- //MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
- //MPI_Bcast(&L, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
- MPI_BinomialBcast(&n, numprocs, MPI_INT, 0, MPI_COMM_WORLD);
- MPI_BinomialBcast(&L, numprocs, MPI_INT, 0, MPI_COMM_WORLD);
- cadena = (char *) malloc(n*sizeof(char));
- inicializaCadena(cadena, n);
- for(i=rank; i<n; i=i+numprocs){
- if(cadena[i] == L){
- count++;
- }
- }
- //MPI_Reduce (&count, &recibir, 1, MPI_INT , MPI_SUM, 0, MPI_COMM_WORLD);
- MPI_FlatreeColectiva(&count, &recibir, numprocs, MPI_INT , MPI_SUM, 0, MPI_COMM_WORLD);
- if(rank==0) {
- printf("El número de apariciones de la letra %c es %d\n", L, recibir);
- }
- free(cadena);
- MPI_Finalize();
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement