Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. #include <mpi.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. int rank, nproc;
  8. int row1,col1;
  9. int row2,col2;
  10. FILE *fp1, *fp2, *fp3;
  11. unsigned int i,j,k,sum;
  12. int *matrix1, *matrix2, *matrixtemp, *temp1, *temp2, *matrix1r, *matrix2r, *result;
  13. double StartTime, EndTime;
  14.  
  15. MPI_Init(&argc, &argv);
  16. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  17. MPI_Comm_size(MPI_COMM_WORLD, &nproc);
  18.  
  19. if(rank==0){
  20. fp1 = fopen("/Users/win/Desktop/MatrixMultiplication_large/matAlarge.txt", "r");
  21. fp2 = fopen("/Users/win/Desktop/MatrixMultiplication_large/matBlarge.txt", "r");
  22. //fp1 = fopen("/Users/win/Desktop/MatrixMul/matrix2.txt", "r");
  23. //fp2 = fopen("/Users/win/Desktop/MatrixMul/matrix2.txt", "r");
  24. fp3 = fopen("/Users/win/Desktop/matSol.txt", "w");
  25.  
  26. fscanf(fp1, "%d", &row1);
  27. fscanf(fp1, "%d", &col1);
  28. fscanf(fp2, "%d", &row2);
  29. fscanf(fp2, "%d", &col2);
  30.  
  31. fprintf(fp3, "%d %d\n", row1, col2);
  32.  
  33. MPI_Bcast(&row1, 1, MPI_INT, 0, MPI_COMM_WORLD);
  34. MPI_Bcast(&col1, 1, MPI_INT, 0, MPI_COMM_WORLD);
  35. MPI_Bcast(&row2, 1, MPI_INT, 0, MPI_COMM_WORLD);
  36. MPI_Bcast(&col2, 1, MPI_INT, 0, MPI_COMM_WORLD);
  37.  
  38. matrix1 = (int*)malloc(row1 * col1 * sizeof(int));
  39. matrix2 = (int*)malloc(row2 * col2 * sizeof(int));
  40. temp1 = (int*)malloc(row1 * col1 * sizeof(int));
  41. temp2 = (int*)malloc(row2 * col2 * sizeof(int));
  42. result = (int*)malloc(row1 * col2 * sizeof(int));
  43.  
  44. for(i=0; i<row1; i++){
  45. for(j=0; j<col1; j++){
  46. fscanf(fp1, "%d", &matrix1[(i*col1)+j]);
  47. }
  48. }
  49.  
  50. //transpose matrix
  51. for(i=0; i<row2; i++){
  52. for(j=0; j<col2; j++){
  53. fscanf(fp2, "%d", &matrix2[(j*row2)+i]);
  54. }
  55. }
  56.  
  57. StartTime = MPI_Wtime();
  58.  
  59. for(i=1;i<nproc;i++){
  60. if(i==nproc-1){
  61. MPI_Send(&matrix1[i*(row1/nproc)*col1], (row1/nproc)*col1 + (row1%(row1/nproc))*col1, MPI_INT, i, 1, MPI_COMM_WORLD);
  62. } else {
  63. MPI_Send(&matrix1[i*(row1/nproc)*col1], (row1/nproc)*col1, MPI_INT, i, 1, MPI_COMM_WORLD);
  64. }
  65. MPI_Send(&matrix2[0], row2*col2, MPI_INT, i, 2, MPI_COMM_WORLD);
  66. }
  67.  
  68. for(i=0;i<row1/nproc;i++){ // loop row matrix1
  69. for(j=0;j<col2;j++){ // loop row matrix2
  70. sum = 0;
  71. for(k=0;k<row2;k++){ // loop col matrix2 (sum)
  72. sum += matrix1[(i*col1)+k] * matrix2[(j*row2)+k];
  73. }
  74. result[(i*col2)+j] = sum;
  75.  
  76. }
  77. }
  78.  
  79. for(i=1;i<nproc;i++){
  80. if(i==nproc-1){
  81. MPI_Recv(&result[i*(row1/nproc)*col2], (row1/nproc)*col2 + (row1%(row1/nproc))*col2, MPI_INT, i, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  82. } else {
  83. MPI_Recv(&result[(i*(row1/nproc))*col2], (row1/nproc)*col2, MPI_INT, i, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  84. }
  85. }
  86.  
  87. EndTime = MPI_Wtime();
  88.  
  89. for(i=0; i<row1; i++){
  90. for(j=0; j<col2; j++){
  91. fprintf(fp3, "%d ", result[(i*col2)+j]);
  92. }
  93. fprintf(fp3, "\n");
  94. }
  95.  
  96. fclose(fp1);
  97. fclose(fp2);
  98. fclose(fp3);
  99. printf("Timings : %f Sec\n", EndTime - StartTime);
  100. } else {
  101.  
  102. MPI_Bcast(&row1, 1, MPI_INT, 0, MPI_COMM_WORLD);
  103. MPI_Bcast(&col1, 1, MPI_INT, 0, MPI_COMM_WORLD);
  104. MPI_Bcast(&row2, 1, MPI_INT, 0, MPI_COMM_WORLD);
  105. MPI_Bcast(&col2, 1, MPI_INT, 0, MPI_COMM_WORLD);
  106.  
  107. matrix1r = malloc(row1 * col1 * sizeof(int));
  108. matrix2r = malloc(row2 * col2 * sizeof(int));
  109. matrixtemp = malloc(row1 * col2 * sizeof(int));
  110. if(rank==nproc-1){
  111. MPI_Recv(&matrix1r[0], (row1/nproc)*col1 + (row1%(row1/nproc))*col1, MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  112. } else {
  113. MPI_Recv(&matrix1r[0], (row1/nproc)*col1, MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  114. }
  115. MPI_Recv(&matrix2r[0], row2*col2, MPI_INT, 0, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  116.  
  117.  
  118. if(rank==nproc-1){
  119. for(i=0;i<(row1/nproc) + (row1%(row1/nproc));i++){ // loop row matrix1
  120. for(j=0;j<col2;j++){ // loop row matrix2
  121. sum = 0;
  122. for(k=0;k<row2;k++){ // loop col matrix2 (sum)
  123. sum += matrix1r[(i*col1)+k] * matrix2r[(j*row2)+k];
  124. }
  125. matrixtemp[(i*col2)+j] = sum;
  126. }
  127. }
  128. } else {
  129. for(i=0;i<row1/nproc;i++){ // loop row matrix1
  130. for(j=0;j<col2;j++){ // loop row matrix2
  131. sum = 0;
  132. for(k=0;k<row2;k++){ // loop col matrix2 (sum)
  133. sum += matrix1r[(i*col1)+k] * matrix2r[(j*row2)+k];
  134. }
  135. matrixtemp[(i*col2)+j] = sum;
  136.  
  137. }
  138. }
  139. }
  140.  
  141. printf("Process %d done!\n", rank) ;
  142. if(rank==nproc-1){
  143. MPI_Send(&matrixtemp[0], (row1/nproc)*col2 + (row1%(row1/nproc))*col2, MPI_INT, 0, 1, MPI_COMM_WORLD);
  144. } else {
  145. MPI_Send(&matrixtemp[0], (row1/nproc)*col2, MPI_INT, 0, 1, MPI_COMM_WORLD);
  146. }
  147. }
  148.  
  149. MPI_Finalize();
  150.  
  151. return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement