#include #include int main(int argc, char** argv) { int rank, size, i, j, lin1[2], lin2[2]; int local_matrix[2]; // Cada processo armazena uma linha int result_matrix[2][2]; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); // Inicializando as matrizes no processo 0 if (rank == 0) { int matrix1[2][2] = {{1, 2}, {3, 4}}; int matrix2[2][2] = {{5, 6}, {7, 8}}; // Enviando as primeiras linhas para o processo 1 MPI_Send(&matrix1[0], 2, MPI_INT, 1, 0, MPI_COMM_WORLD); MPI_Send(&matrix2[0], 2, MPI_INT, 1, 1, MPI_COMM_WORLD); // Enviando as segundas linhas para o processo 2 MPI_Send(&matrix1[1], 2, MPI_INT, 2, 0, MPI_COMM_WORLD); MPI_Send(&matrix2[1], 2, MPI_INT, 2, 1, MPI_COMM_WORLD); } // Recebendo as linhas nos processos 1 e 2 if (rank == 1 || rank == 2) { MPI_Recv(lin1, 2, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); MPI_Recv(lin2, 2, MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE); // Realizando a soma local for (i = 0; i < 2; i++) { printf("Rank: %d soma %d com %d\n",rank,lin1[i],lin2[i]); local_matrix[i] = lin1[i] + lin2[i]; } // Enviando o resultado para o processo 0 MPI_Send(local_matrix, 2, MPI_INT, 0, rank, MPI_COMM_WORLD); } // Recebendo os resultados no processo 0 if (rank == 0) { MPI_Recv(&result_matrix[0], 2, MPI_INT, 1, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE); MPI_Recv(&result_matrix[1], 2, MPI_INT, 2, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE); // Imprimindo a matriz resultante printf("Matriz resultante:\n"); for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { printf("%d \n", result_matrix[i][j]); } } } MPI_Finalize(); return 0; }