Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include "mpi.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define NR 4
  5. #define INF 999
  6.  
  7. int main(int argc, char **argv) {
  8.  
  9. MPI_Init(&argc, &argv);
  10. int rank, numtasks;
  11. int masterGraph[NR][NR] = {{0}};
  12. MPI_Status stat;
  13. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  14. MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
  15.  
  16. int matrixGraph[NR][NR] = {
  17. { 0, 5, 15, 3 },
  18. { INF, 0, 6, INF },
  19. { INF, INF, 0, 9 },
  20. { 1, INF, INF, 0 },
  21. };
  22. //MPI_Bcast(matrixGraph, NR*NR, MPI_INT, 0, MPI_COMM_WORLD);
  23.  
  24. if (rank == 0) {
  25. for (int k = 0; k < NR; k++) {
  26. for (int j = 0; j < NR; j++) {
  27. if (matrixGraph[0][k] + matrixGraph[k][j] < matrixGraph[0][j]) {
  28. matrixGraph[0][j] = matrixGraph[0][k] + matrixGraph[k][j];
  29. }
  30. }
  31. }
  32. for (int i = 0; i < NR; i++) {
  33. masterGraph[0][i] = matrixGraph[0][i];
  34. if (i != 0)
  35. MPI_Send(&matrixGraph, NR*NR, MPI_INT, i, 1, MPI_COMM_WORLD);
  36. }
  37. printf("\n");
  38. }
  39. if (rank != 0) {
  40. MPI_Recv(matrixGraph, NR*NR, MPI_INT, 0, 1, MPI_COMM_WORLD, &stat);
  41. for (int k = 0; k < NR; k++) {
  42. for (int j = 0; j < NR; j++) {
  43. if (matrixGraph[rank][k] + matrixGraph[k][j] < matrixGraph[rank][j]) {
  44. matrixGraph[rank][j] = matrixGraph[rank][k] + matrixGraph[k][j];
  45. }
  46. }
  47. }
  48. MPI_Send(&matrixGraph[rank], NR, MPI_INT, 0, 1, MPI_COMM_WORLD);
  49. //MPI_Alltoall(&matrixGraph, NR*NR, MPI_INT, 0 , NR*NR, MPI_INT, MPI_COMM_WORLD);
  50. }
  51. if (rank == 0) {
  52.  
  53. for (int p = 1; p < numtasks; p++) {
  54. MPI_Recv(matrixGraph[p], NR, MPI_INT, p, 1, MPI_COMM_WORLD, &stat);
  55. for (int j = p; j < NR; j++) {
  56. for (int k = 0; k < NR; k++) {
  57. masterGraph[j][k] = matrixGraph[j][k];
  58. }
  59. }
  60. }
  61. for (int i = 0; i < NR; i++) {
  62. for (int j = 0; j < NR; j++) {
  63. if (masterGraph[i][j] == 999)
  64. printf("- ");
  65. else
  66. printf("%d ", masterGraph[i][j]);
  67.  
  68. }
  69. printf("\n");
  70. }
  71. }
  72. MPI_Finalize();
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement