Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. //
  2. // main.c
  3. // Lab06
  4. //
  5. // Created by NUTH on 8/23/2559 BE.
  6. // Copyright © 2559 NUTHYLL. All rights reserved.
  7. //
  8.  
  9. #include <mpi.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. int **alloc_2d_int(int rows,int cols){
  14. int i ;
  15. int *data = (int *)malloc(rows*cols*sizeof(int));
  16. int **array = (int **)malloc(rows*sizeof(int*));
  17. for (i=0; i<rows; i++) {
  18. array[i] = &(data[cols*i]);
  19. }
  20. return array;
  21. }
  22.  
  23.  
  24. int main(int argc, char * argv[]) {
  25.  
  26. int rank,size;
  27. int rowsB,columnsB;
  28. int rowsA,columnsA;
  29. int eachRow,modRow;
  30.  
  31. MPI_Init(&argc, &argv);
  32. int **matA=NULL,**matB = NULL;
  33. double starttime = 0,endtime=0;
  34.  
  35.  
  36. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  37. MPI_Comm_size(MPI_COMM_WORLD,&size);
  38.  
  39. if(rank==0){
  40. FILE *fp1,*fp2;
  41. int i,j;
  42.  
  43. /* read matrix A */
  44. fp1 = fopen("/Users/Nuth/Desktop/MatrixMultipleData/matAlarge.txt" , "r");
  45. fscanf(fp1,"%d %d",&rowsA,&columnsA);
  46. matA = alloc_2d_int(rowsA, columnsA);
  47.  
  48. for (i=0; i<rowsA; i++) {
  49. for (j=0; j<columnsA; j++) {
  50. fscanf(fp1, "%d ",&matA[i][j]);
  51. }
  52. }
  53.  
  54. fclose(fp1);
  55.  
  56.  
  57. /*read matrix B*/
  58. fp2 = fopen("/Users/Nuth/Desktop/MatrixMultipleData/matBlarge.txt" , "r");
  59. fscanf(fp2,"%d %d",&rowsB,&columnsB);
  60. matB = alloc_2d_int(columnsB, rowsB);
  61.  
  62. for (i=0; i<rowsB; i++) {
  63. for (j=0; j<columnsB; j++) {
  64. fscanf(fp2, "%d ",&matB[j][i]);
  65. }
  66. }
  67.  
  68. fclose(fp2);
  69. starttime = MPI_Wtime();
  70. eachRow = rowsA/size;
  71. modRow = rowsA%size;
  72. }
  73.  
  74.  
  75. MPI_Bcast(&rowsA, 1, MPI_INT, 0, MPI_COMM_WORLD);
  76. MPI_Bcast(&columnsA, 1, MPI_INT, 0, MPI_COMM_WORLD);
  77. MPI_Bcast(&rowsB, 1, MPI_INT, 0, MPI_COMM_WORLD);
  78. MPI_Bcast(&columnsB, 1, MPI_INT, 0, MPI_COMM_WORLD);
  79. if(rank!=0){
  80. matB = alloc_2d_int(columnsB,rowsB);
  81. }
  82.  
  83. MPI_Bcast(&matB[0][0],rowsB*columnsB, MPI_INT, 0, MPI_COMM_WORLD);
  84. MPI_Bcast(&eachRow, 1, MPI_INT, 0, MPI_COMM_WORLD);
  85. MPI_Bcast(&modRow, 1, MPI_INT, 0, MPI_COMM_WORLD);
  86.  
  87. if(rank==0){
  88.  
  89. int i=0,j=0,k=0;
  90. for (i=1; i<size; i++) {
  91. MPI_Send(&matA[(eachRow*i)+modRow][0],eachRow*columnsA, MPI_INT, i, i, MPI_COMM_WORLD);
  92. }
  93.  
  94. int **result = alloc_2d_int(rowsA, columnsB);
  95.  
  96. for (i=0; i<eachRow+modRow; i++) {
  97. for (j=0; j<columnsB; j++) {
  98. result[i][j]=0;
  99. for (k=0; k<rowsB; k++) {
  100. result[i][j] += matA[i][k]*matB[j][k];
  101. }
  102. }
  103. }
  104.  
  105. for (i=1; i<size; i++) {
  106. MPI_Recv(&result[(eachRow*i)+modRow][0], eachRow*columnsB, MPI_INT,i, 115, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  107. }
  108.  
  109. endtime = MPI_Wtime();
  110. printf("Total %lf sec.\n",endtime-starttime);
  111.  
  112. FILE *of= fopen("/Users/Nuth/Desktop/MatrixMultipleData/out_large.txt", "w");
  113. for (i=0; i<rowsA; i++) {
  114. for (j=0; j<columnsB; j++) {
  115. fprintf(of,"%d ",result[i][j]);
  116. }
  117. fprintf(of,"\n");
  118. }
  119.  
  120. fclose(of);
  121.  
  122.  
  123.  
  124. }else{
  125. int **parA = alloc_2d_int(eachRow, columnsA);
  126. int **result = alloc_2d_int(eachRow,columnsB);
  127. MPI_Recv(&parA[0][0], eachRow*columnsA, MPI_INT, 0, rank, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  128. int i,j,k;
  129.  
  130. for (i=0; i<eachRow; i++) {
  131. for (j=0; j<columnsB; j++) {
  132. result[i][j]=0;
  133. for (k=0; k<rowsB; k++) {
  134. result[i][j] += parA[i][k]*matB[j][k];
  135. }
  136. }
  137. }
  138.  
  139. MPI_Send(&result[0][0], eachRow*columnsB, MPI_INT, 0, 115, MPI_COMM_WORLD);
  140. }
  141. MPI_Finalize();
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement