Advertisement
Sawy3R11

MPI_reduce_bcast

May 14th, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. // C:\Users\Dawid\source\repos\mpi_3\x64\Debug\
  6. //mpiexec -n 10 *.exe
  7. int main(int argc, char** argv) {
  8.    
  9.     MPI_Init(&argc, &argv);
  10.     int data = 1;
  11.  
  12.     int world_size;
  13.     MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  14.     int rank;
  15.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  16.  
  17.     //MPI_Barrier(MPI_COMM_WORLD);
  18.     MPI_Bcast(&data, 1, MPI_INT, 0, MPI_COMM_WORLD);
  19.     if( rank!= 0 )
  20.         data *= rank;
  21.     printf("\nprocess: #%d recv: %d", rank, data);
  22.  
  23.     int globalSum = 0;
  24.     int localSum = (rank+1);
  25.  
  26.     //funkcja zbiera dane z procesów konunikatora do wybranego root'a
  27.     MPI_Reduce(&localSum, &globalSum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  28.     printf("proc %d globalSum: %d", rank, globalSum);
  29.     if (rank == 0) {
  30.         printf("reduced value: %d", globalSum);
  31.     }
  32.  
  33.    
  34.  
  35.     MPI_Finalize();
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement