pborawski

mpi_reduce zad3

Jan 14th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <mpi.h>
  3. #include <time.h>
  4.  
  5. void drukuj(int id, int numprocs, int *array, int size)
  6. {
  7.         int i,n;
  8.         //printf("%d : ",id);
  9.        
  10.         for(n = 0; n < numprocs; ++n)
  11.         {
  12.             if ( n == id )
  13.             {
  14.                 printf(" %d: ", id );
  15.                 for( i = 0; i < size; ++i)
  16.                 {
  17.                     printf(" %d ", array[i]);
  18.                 }
  19.                 printf("\n");
  20.                 if( id == numprocs - 1)
  21.                     printf("\n");
  22.             }
  23.             MPI_Barrier(MPI_COMM_WORLD);
  24.         }
  25. }
  26.  
  27. int main(int argc, char *argv[])
  28. {
  29.         int id, numproc;
  30.         int i, root;
  31.         int buf[10];
  32.         int reducebuf[10] = {0};
  33.        
  34.         MPI_Init(&argc, &argv);
  35.         MPI_Comm_rank(MPI_COMM_WORLD, &id);
  36.         MPI_Comm_size(MPI_COMM_WORLD, &numproc);
  37.        
  38.         srand( id * time(NULL) );
  39.         for( i = 0; i < 10; i++)
  40.         {
  41.             buf[i] = random()%10;
  42.         }
  43.         drukuj(id, numproc, buf, 10);
  44.         if ( id == 0 )
  45.             printf("\n");
  46.         MPI_Barrier(MPI_COMM_WORLD);
  47.         root = 0;
  48.        
  49.         MPI_Reduce( buf, reducebuf, 10, MPI_INT, MPI_MAX, root, MPI_COMM_WORLD);
  50.         drukuj(id, numproc, reducebuf, 10);
  51.        
  52.        
  53.         MPI_Finalize();
  54.         return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment