jeudi 18 avril 2019

Random number generated to each process in MPI C

I am trying to generate random numbers and assign these numbers to the array in every task. I want to make sure the random numbers in different tasks are different. How could I achieve that?

If every MPI task initialize its own array with random numbers (like the code I attached), are these numbers different among task?

I know I could generate a large set of random numbers and broadcast to each task, but this may cause memory issues for large arrays.

Thank you so much in advance for any informations.

void initialize(float* inarray, int n){
    int i;
    for (i=0; i<n; i++){
            inarray[i] = random() / (float)RAND_MAX;
        }
    }
}

void main(int argc, char* argv[]){

    MPI_Comm comm=MPI_COMM_WORLD;
    int numnodes, myid, ierr;
    ierr=MPI_Init(&argc, &argv);
    ierr=MPI_Comm_size(comm, &numnodes);
    ierr=MPI_Comm_rank(comm, &myid);

    int n = 100;
    float *x = malloc(sizeof(float)*n);
    initialize(x, n);

    ierr=MPI_Finalize();
}




Aucun commentaire:

Enregistrer un commentaire