Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- s#include <mpi.h>
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define PROC_MASTER 0
- typedef struct double_int{
- double val;
- int proc_id;
- }double_int;
- int main(int argc, char **argv)
- {
- int n_proc, proc_id;
- double *x = (double *)malloc(10*sizeof(double));
- MPI_Init(&argc, &argv);
- MPI_Comm_size(MPI_COMM_WORLD, &n_proc);
- MPI_Comm_rank(MPI_COMM_WORLD, &proc_id);
- double_int num;
- double_int num_min;
- srand(time(NULL));
- int i;
- for(i = 0; i < 1000; i++){
- if(i == 20 && proc_id == 2){
- MPI_Finalize();
- exit(EXIT_SUCCESS);
- }
- if(proc_id == PROC_MASTER){
- num.val = rand()/(double)RAND_MAX;
- num.proc_id = proc_id;
- }
- else if(proc_id == 1){
- num.val = rand()/(double)RAND_MAX;
- num.proc_id = proc_id;
- }
- else if(proc_id == 2){
- num.val = rand()/(double)RAND_MAX;
- num.proc_id = proc_id;
- }
- else if(proc_id == 3){
- num.val = rand()/(double)RAND_MAX;
- num.proc_id = proc_id;
- }
- MPI_Allreduce(&num, &num_min, 1, MPI_DOUBLE_INT, MPI_MINLOC, MPI_COMM_WORLD);
- if(proc_id == PROC_MASTER){
- printf("iter %d: ricevuto f = %f dal processo %d\n", i, num_min.val, num_min.proc_id);
- }
- MPI_Bcast(x, 10, MPI_DOUBLE, num_min.proc_id, MPI_COMM_WORLD);
- }
- MPI_Finalize();
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement