Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <mpi.h>
  4. #include <omp.h>
  5.  
  6. #define N 3
  7.  
  8. int main(int argc, char *argv[]){
  9.  
  10. int size, rank, sum = 0, result = 0, i, j, cij, recv_rank;
  11. int row[N], col[N];
  12.  
  13. int a [N][N] = {1,2,3,
  14. 4,5,6,
  15. 7,8,9};
  16.  
  17. int b [N][N] = {1,2,3,
  18. 4,5,6,
  19. 7,8,9};
  20.  
  21. int c [N][N]; //Result {30,36,42,66,81,96,102,126,150}
  22.  
  23. MPI_Datatype mytype;
  24.  
  25. MPI_Status status;
  26. MPI_Init(NULL, NULL);
  27. MPI_Comm_size(MPI_COMM_WORLD, &size);
  28. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  29.  
  30. if(size != (N*N)){
  31. printf("Número de processos diferente da ordem da matriz!!!\n");
  32. exit(0);
  33. }
  34.  
  35. if(rank == 0){
  36.  
  37. printf("\nMatriz A\n");
  38. for(i = 0; i < N; i++){
  39. for(j = 0; j < N; j++){
  40. printf("[%d]\t", a[i][j]);
  41. }
  42. printf("\n");
  43. }
  44.  
  45. printf("\nMatriz B\n");
  46. for(i = 0; i < N; i++){
  47. for(j = 0; j < N; j++){
  48. printf("[%d]\t", b[i][j]);
  49. }
  50. printf("\n");
  51. }
  52.  
  53. int _ij;
  54.  
  55.  
  56. MPI_Type_vector(N, 1, N, MPI_INT, &mytype);
  57. MPI_Type_commit(&mytype);
  58.  
  59. for(i = 0; i < N; i++){
  60. for(j = 0; j < N; j++){
  61. _ij = i*N+j;
  62. if((_ij)!=0){
  63. MPI_Send(&a[i][0], N, MPI_INT, _ij, 0, MPI_COMM_WORLD);
  64. MPI_Send(&b[0][j], 1, mytype, _ij, 0, MPI_COMM_WORLD);
  65. }
  66. }
  67. col[i] = b[0][i];
  68. row[i] = a[i][0];
  69. }
  70.  
  71. /*for(i = 0; i < N; i++){
  72. printf("r = [%d]\n", row[i]);
  73. printf("c = [%d]\n", col[i]);
  74. }*/
  75.  
  76. //Recv 1 to n-1 processos
  77. for (i = 1; i < size; i++){
  78. MPI_Recv(&cij, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &status);
  79. c[(i/N)][(i%N)] = cij;
  80. }
  81.  
  82. #pragma omp parallel reduction (+: sum) num_threads(N)
  83. {
  84. int thread_number = omp_get_thread_num();
  85. sum = (row[thread_number] * col[thread_number]);
  86. }
  87. c[0][0] = sum;
  88.  
  89. printf("\nResult: \n");
  90.  
  91. for(i = 0; i < N; i++){
  92. for(j = 0; j < N; j++){
  93. printf("[%d]\t", c[i][j]);
  94. }
  95. printf("\n");
  96. }
  97. printf("\n");
  98. } else {
  99. // 1 até n-1 processos
  100. //printf("My Rank %d\n",rank);
  101.  
  102. MPI_Recv(&row, N, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
  103. MPI_Recv(&col, N, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
  104.  
  105. /*for(i = 0; i < N; i++){
  106. printf("My Rank %d r = [%d]\n", rank, row[i]);
  107. }
  108.  
  109. for(i = 0; i < N; i++){
  110. printf("My Rank %d c = [%d]\n", rank, col[i]);
  111. }*/
  112.  
  113. #pragma omp parallel reduction (+: sum) num_threads(N)
  114. {
  115. int thread_number = omp_get_thread_num();
  116. sum = (row[thread_number] * col[thread_number]);
  117. }
  118. //printf("From Rank %d c[%d][%d] = %d\n", rank, (rank/N),(rank%N), sum);
  119. MPI_Send(&sum, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
  120. }
  121. MPI_Finalize();
  122. return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement