Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <mpi.h>
- int main()
- {
- const int N = 4;
- int matrix[N*N] =
- {
- 1, 5, 7, 8,
- 7, 2, 3, 4,
- 8, 1, 5, 2,
- 2, 5, 0, 1
- };
- int vector[N] = {4, 3, 2, 1};
- int result[N];
- MPI_Init( NULL, NULL );
- int world_size, world_rank;
- MPI_Comm_size( MPI_COMM_WORLD, &world_size );
- MPI_Comm_rank( MPI_COMM_WORLD, &world_rank );
- if ( world_size < N * N )
- {
- MPI_Finalize();
- if ( world_rank == 0 )
- printf( "Error: Not enough processors\n" );
- return 1;
- }
- int local_data = matrix[ world_rank ] * vector[ world_rank % N ];
- int local_sum = 0;
- for ( int i = 0; i < N * N; i++ )
- {
- MPI_Send( &local_data, 1, MPI_INT, ( i / N ), 0, MPI_COMM_WORLD );
- if ( world_rank == ( i / N ) )
- {
- MPI_Recv( &local_data, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE );
- local_sum += local_data;
- }
- }
- MPI_Gather( &local_sum, 1, MPI_INT, result, N, MPI_INT, 0, MPI_COMM_WORLD );
- if ( world_rank == 0 )
- {
- for ( int i = 0; i < N; i++ )
- printf( "result[%i] = %i\n", i, result[i] );
- }
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement