2

Im having troubles with the following code

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>

//1 mostrar lista
//2 borrar elementos
//3 ordenar lista
//4 añadir elemento

int main(int argc,char**argv){
    int rank;
    int *list;  //Lista
    int *liste; //Lista encargados
    int *perm;  //Permutaciones
    int *capa;  //Capacidad
    int size;
    int i;
    int aux;
    const int root = 0;
    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Comm_size(MPI_COMM_WORLD,&size);
    if(rank==root){
        perm = (int*)malloc(size*sizeof(int));
        capa = (int*)malloc(size*sizeof(int));

        for(i=0;i<size;i++){
            scanf("%d",&aux);
            capa[i]=aux;
        }

        for(i=0;i<size;i++){
            scanf("%d",&aux);
            perm[i]=aux;
        }   

    }

    //printf("\n[%d]: Antes de Bcast: %d %d %d %d\n",rank,capa[0],capa[1],capa[2],capa[4]);

    MPI_Bcast(&capa[0],size,MPI_INT,root,MPI_COMM_WORLD);
    MPI_Bcast(&perm[0],size,MPI_INT,root,MPI_COMM_WORLD);
    //MPI_Bcast(&aux,1,MPI_INT,root,MPI_COMM_WORLD);



    printf("\n[%d]: Despues de Bcast capa: %d %d %d %d\n",rank,capa[0],capa[1],capa[2],capa[3]);


    printf("\n[%d]: Despues de Bcast perm: %d %d %d %d\n",rank,perm[0],perm[1],perm[2],perm[3]);

    MPI_Finalize();
    return 0;


}

Well there is nothing special, Im just trying to Bcast two arrays of ints (for now Im trying with 4-lenght strings of ints, that why the last printfs are statics).

Anyway, I tried first just with the "capa" string and it worked, but when I tried it with both strings, Im getting a segmentation fault error but I dont know why, both string are the same size, the only differences are their values.

EDIT:

Just changed the code to this and worked:

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>

//1 mostrar lista
//2 borrar elementos
//3 ordenar lista
//4 añadir elemento

int main(int argc,char**argv){
    int rank;
    int *list;  //Lista
    int *liste; //Lista encargados
    int *perm;  //Permutaciones
    int *capa;  //Capacidad
    int size;
    int i;
    int aux;
    const int root = 0;
    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Comm_size(MPI_COMM_WORLD,&size);
    perm = (int*)malloc(size*sizeof(int));
    capa = (int*)malloc(size*sizeof(int));
    if(rank==root){


        for(i=0;i<size;i++){
            scanf("%d",&aux);
            capa[i]=aux;
        }

        for(i=0;i<size;i++){
            scanf("%d",&aux);
            perm[i]=aux;
        }   

    }

    //printf("\n[%d]: Antes de Bcast: %d %d %d %d\n",rank,capa[0],capa[1],capa[2],capa[4]);

    MPI_Bcast(&capa[0],size,MPI_INT,root,MPI_COMM_WORLD);
    MPI_Bcast(&perm[0],size,MPI_INT,root,MPI_COMM_WORLD);
    //MPI_Bcast(&aux,1,MPI_INT,root,MPI_COMM_WORLD);



    printf("\n[%d]: Despues de Bcast capa: %d %d %d %d\n",rank,capa[0],capa[1],capa[2],capa[3]);


    printf("\n[%d]: Despues de Bcast perm: %d %d %d %d\n",rank,perm[0],perm[1],perm[2],perm[3]);

    MPI_Finalize();
    return 0;


}
Sage Harpuia
  • 348
  • 2
  • 13
  • 5
    Well I think the memory should be allocated (for perm and capa) in all nodes, not just the root node. Receiver nodes don't have an allocated space to write received data! – saeedn Mar 02 '16 at 02:20
  • @saeedn Thanks a lot! you were right, I just changed what you said and it worked – Sage Harpuia Mar 02 '16 at 08:27

0 Answers0