fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "mpi.h"
  4. /*
  5.   after the user chooses the way of entering matrices and entering them:
  6.   convert every matrix to 1d array and second matrix is converted by column
  7.   then check if there's a remainder in the first matrix rows, the root will
  8.   do their multiplication and scatter the rest of rows to the process to
  9.   do
  10.  
  11.   if there's not a remainder , the rows
  12.  
  13.  
  14.  
  15. */
  16. int main(int argc, char *argv[])
  17. {
  18. int rank;
  19. int nprocess;
  20. int rem;
  21. double startTime;
  22. double endTime;
  23. double elapsedTime;
  24. int matrix1Row=0,matrix1Col=-1;
  25. int matrix2Row=-2,matrix2Col=0;
  26. int** matrix1;
  27. int** matrix2;
  28. int* mat1;
  29. int* mat2;
  30. int* globalArr1;
  31. int* globalArr2;
  32. int choice=0;
  33. int i=0,j=0,k=0,m=0;
  34. int subSize1;
  35. int localSubSize=0;
  36. int* subResult;
  37. int* transferedResult;
  38. int* finalResult;
  39. int* localList;
  40. int otherProcessesPortion=0;
  41. int sum=0;
  42. char filename[15];
  43. MPI_Status status;
  44.  
  45. MPI_Init( &argc , &argv );
  46. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  47. MPI_Comm_size(MPI_COMM_WORLD, &nprocess);
  48.  
  49. if(rank==0)
  50. {
  51. printf("Welcome to vector Matrix multiplication program!!\n");
  52. printf("if you want program input matrices from file Enter 1\n");
  53. printf("if you want program input 2 matrices from console Enter 2\n");
  54. scanf("%d",&choice);
  55.  
  56. if(choice==1)
  57. {
  58. printf("enter your filename(including .txt)\n");
  59. scanf("%s",&filename);
  60. FILE *file;
  61. file = fopen(filename, "r");
  62.  
  63. if(file)
  64. {
  65. fscanf(file,"%d",&matrix1Row);
  66. fscanf(file,"%d",&matrix1Col);
  67.  
  68. fscanf(file,"%d",&matrix2Row);
  69. fscanf(file,"%d",&matrix2Col);
  70.  
  71. matrix1=malloc(matrix1Row*sizeof(int));
  72. for(i=0;i<matrix1Row;i++)
  73. {
  74. matrix1[i]=malloc(matrix1Col*sizeof(int));
  75. }
  76. for(i=0;i<matrix1Row;i++)
  77. {
  78. for(j=0;j<matrix1Col;j++)
  79. {
  80. fscanf(file,"%d",&matrix1[i][j]);
  81. }
  82. }
  83.  
  84. matrix2=malloc(matrix2Row*sizeof(int));
  85. for(i=0;i<matrix2Row;i++)
  86. {
  87. matrix2[i]=malloc(matrix2Col*sizeof(int));
  88. }
  89. for(i=0;i<matrix2Row;i++)
  90. {
  91. for(j=0;j<matrix2Col;j++)
  92. {
  93. fscanf(file,"%d",&matrix2[i][j]);
  94. }
  95. }
  96.  
  97. if(matrix1Col!=matrix2Row)
  98. printf("matrices can't be multiplied\n");
  99. }
  100. fclose(file);
  101. }
  102. else if(choice==2)
  103. {
  104. while(matrix1Col!=matrix2Row)
  105. {
  106. printf("enter matrix #1 dimension:\n");
  107. scanf("%d",&matrix1Row);
  108. scanf("%d",&matrix1Col);
  109. matrix1=malloc(matrix1Row*sizeof(int));
  110.  
  111. printf("enter matrix #2 dimension:\n");
  112. scanf("%d",&matrix2Row);
  113. scanf("%d",&matrix2Col);
  114. matrix2=malloc(matrix2Row*sizeof(int));
  115.  
  116. if(matrix1Col!=matrix2Row)
  117. printf("matrices can't be multiplied\n");
  118. }
  119. for(i=0;i<matrix1Row;i++)
  120. {
  121. matrix1[i]=malloc(matrix1Col*sizeof(int));
  122. }
  123. printf("enter matrix #1 elements:\n");
  124. for(i=0;i<matrix1Row;i++)
  125. {
  126. for(j=0;j<matrix1Col;j++)
  127. {
  128. scanf("%d",&matrix1[i][j]);
  129. }
  130. }
  131.  
  132. for(i=0;i<matrix2Row;i++)
  133. {
  134. matrix2[i]=malloc(matrix2Col*sizeof(int));
  135. }
  136. printf("enter matrix #2 elements:\n");
  137. for(i=0;i<matrix2Row;i++)
  138. {
  139. for(j=0;j<matrix2Col;j++)
  140. {
  141. scanf("%d",&matrix2[i][j]);
  142. }
  143. }
  144. }
  145. startTime= MPI_Wtime();
  146. printf("starting time of program: %f\n",rank,startTime);
  147.  
  148. //converting 2D matrices to 1D arrays
  149. mat1=malloc((matrix1Row*matrix1Col)*sizeof(int));
  150. for(i=0;i<matrix1Row;i++)
  151. {
  152. for(j=0;j<matrix1Col;j++)
  153. {
  154. mat1[j+(matrix1Col*i)]=matrix1[i][j];
  155. }
  156. }
  157.  
  158. mat2=malloc((matrix2Row*matrix2Col)*sizeof(int));
  159. for(i=0;i<matrix2Col;i++)
  160. {
  161. for(j=0;j<matrix2Row;j++)
  162. {
  163. mat2[j+(matrix1Col*i)]=matrix2[j][i];
  164. }
  165. }
  166.  
  167. rem=matrix1Row%nprocess;
  168. subSize1=matrix1Row/nprocess;
  169. if(rem!=0 && matrix1Col==matrix2Row)
  170. {
  171. otherProcessesPortion=matrix1Row-rem;
  172. for (i=0;i<rem;i++)
  173. {
  174. for (j=0;j<matrix2Col;j++)
  175. {
  176. for (k=0;k<matrix1Col;k++)
  177. {
  178. sum+=mat1[i*matrix1Col+k]*mat2[k+(j*matrix2Row)];
  179. }
  180. printf("%d ",sum);
  181. sum=0;
  182. }
  183. printf("\n");
  184. }
  185.  
  186. localSubSize=otherProcessesPortion/nprocess;
  187. localList=malloc((otherProcessesPortion*matrix1Col)*sizeof(int));
  188. j=0;
  189.  
  190. for(i=(rem*matrix1Col);i<(matrix1Col*matrix1Row);i++)
  191. {
  192. localList[j]=mat1[i];
  193. j++;
  194. }
  195. }
  196. }
  197. MPI_Bcast(&matrix1Row,1,MPI_INT,0,MPI_COMM_WORLD);
  198. MPI_Bcast(&matrix1Col,1,MPI_INT,0,MPI_COMM_WORLD);
  199. MPI_Bcast(&matrix2Row,1,MPI_INT,0,MPI_COMM_WORLD);
  200. MPI_Bcast(&subSize1,1,MPI_INT,0,MPI_COMM_WORLD);
  201. MPI_Bcast(&matrix2Col,1,MPI_INT,0,MPI_COMM_WORLD);
  202. if(rank!=0)
  203. mat2 = malloc(sizeof(int) * matrix2Col*matrix2Row);
  204.  
  205. MPI_Bcast(&mat2[0],(matrix2Col*matrix2Row),MPI_INT,0,MPI_COMM_WORLD);
  206.  
  207. if(matrix1Row%nprocess>0 && matrix1Col==matrix2Row)
  208. {
  209. MPI_Bcast(&otherProcessesPortion,1,MPI_INT,0,MPI_COMM_WORLD);
  210. MPI_Bcast(&localSubSize,1,MPI_INT,0,MPI_COMM_WORLD);
  211.  
  212. globalArr2=malloc((localSubSize*matrix1Col)*sizeof(int));
  213. MPI_Scatter(localList,(localSubSize*matrix1Col),MPI_INT,globalArr2,(localSubSize*matrix1Col),MPI_INT,0,MPI_COMM_WORLD);
  214.  
  215. sum=0;
  216. subResult=malloc((localSubSize*matrix2Col)*sizeof(int));
  217. for(i=0;i<localSubSize;i++)
  218. {
  219. for (j=0;j<matrix2Col;j++)
  220. {
  221. for (k=0;k<matrix1Col;k++)
  222. {
  223. sum+=globalArr2[i*matrix1Col+k]*mat2[k+(j*matrix2Row)];
  224. }
  225. subResult[i*matrix2Col+j]=sum;
  226. sum=0;
  227. }
  228. }
  229. transferedResult=malloc((otherProcessesPortion*matrix2Col)*sizeof(int));
  230. MPI_Gather(subResult,matrix2Col,MPI_INT,transferedResult,matrix2Col,MPI_INT,0,MPI_COMM_WORLD);
  231.  
  232. if(rank==0)
  233. {
  234. finalResult=malloc((localSubSize*matrix2Col)*sizeof(int));
  235. for(j=0;j<(otherProcessesPortion*matrix2Col);j++)
  236. {
  237. printf("%d ",transferedResult[j]);
  238. if((j+1)%matrix2Col==0)
  239. printf("\n");
  240. }
  241. printf("\n");
  242. }
  243. }
  244. else if(matrix1Row%nprocess==0 && matrix1Col==matrix2Row)
  245. {
  246. globalArr1=malloc((matrix1Col*subSize1)*sizeof(int));
  247. MPI_Scatter(mat1,(matrix1Col*subSize1),MPI_INT,globalArr1,(matrix1Col*subSize1),MPI_INT,0,MPI_COMM_WORLD);
  248.  
  249. j=0,k=0,i=0;
  250. subResult=malloc((subSize1*matrix2Col)*sizeof(int));
  251.  
  252. for (i=0;i<subSize1;i++)
  253. {
  254. for (j=0;j<matrix2Col;j++)
  255. {
  256. for (k=0;k<matrix1Col;k++)
  257. {
  258. sum+=globalArr1[i*matrix1Col+k]*mat2[k+(j*matrix2Row)];
  259. }
  260. subResult[i*matrix2Col+j]=sum;
  261. sum=0;
  262. }
  263. }
  264. transferedResult=malloc((matrix1Row*matrix2Col)*sizeof(int));
  265. MPI_Gather(subResult,matrix2Col,MPI_INT,transferedResult,matrix2Col,MPI_INT,0,MPI_COMM_WORLD);
  266.  
  267. if(rank==0)
  268. {
  269. finalResult=malloc((matrix1Row*matrix2Col)*sizeof(int));
  270. for(j=0;j<(matrix1Row*matrix2Col);j++)
  271. {
  272. printf("%d ",transferedResult[j]);
  273. if((j+1)==(matrix1Row*matrix2Col)/2)
  274. printf("\n");
  275. }
  276. printf("\n");
  277. }
  278. }
  279. endTime= MPI_Wtime();
  280. elapsedTime=endTime-startTime;
  281. printf("Ending time of program: %f\n",elapsedTime);
  282. /* shutdown MPI */
  283. MPI_Finalize();
  284. return 0;
  285. }
  286.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:3:17: fatal error: mpi.h: No such file or directory
compilation terminated.
stdout
Standard output is empty