Advertisement
Guest User

meow c

a guest
May 29th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <mpi.h>
  3.  
  4. int main()
  5. {
  6. const int N = 4;
  7.  
  8. int matrix[N*N] =
  9. {
  10. 1, 5, 7, 8,
  11. 7, 2, 3, 4,
  12. 8, 1, 5, 2,
  13. 2, 5, 0, 1
  14. };
  15.  
  16. int vector[N] = {4, 3, 2, 1};
  17. int result[N];
  18.  
  19. MPI_Init( NULL, NULL );
  20.  
  21. int world_size, world_rank;
  22. MPI_Comm_size( MPI_COMM_WORLD, &world_size );
  23. MPI_Comm_rank( MPI_COMM_WORLD, &world_rank );
  24.  
  25. if ( world_size < N * N )
  26. {
  27. MPI_Finalize();
  28.  
  29. if ( world_rank == 0 )
  30. printf( "Error: Not enough processors\n" );
  31.  
  32. return 1;
  33. }
  34.  
  35. int local_data = matrix[ world_rank ] * vector[ world_rank % N ];
  36. int local_sum = 0;
  37.  
  38. for ( int i = 0; i < N * N; i++ )
  39. {
  40. MPI_Send( &local_data, 1, MPI_INT, ( i / N ), 0, MPI_COMM_WORLD );
  41.  
  42. if ( world_rank == ( i / N ) )
  43. {
  44. MPI_Recv( &local_data, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE );
  45. local_sum += local_data;
  46. }
  47. }
  48.  
  49. MPI_Gather( &local_sum, 1, MPI_INT, result, N, MPI_INT, 0, MPI_COMM_WORLD );
  50.  
  51. if ( world_rank == 0 )
  52. {
  53. for ( int i = 0; i < N; i++ )
  54. printf( "result[%i] = %i\n", i, result[i] );
  55. }
  56.  
  57. MPI_Finalize();
  58.  
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement