Advertisement
salmaNAS

mat2

Nov 22nd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.79 KB | None | 0 0
  1. #include <time.h>
  2. #include<stdio.h>
  3. #include<pthread.h>
  4. #include<stdlib.h>
  5. #include <time.h>
  6. int **Holder;
  7. int numberOfThreadsR;
  8. int numberOfThreadsE;
  9. //int *K;
  10.  
  11. //this function will branch into either element thread or row thread multplication
  12.  
  13. void fillMatrices(){
  14. FILE *file1;
  15. int r1 , c1 , r2 , c2;
  16. file1 = fopen("input.txt", "r"); // read mode
  17.  
  18. if (file1 == NULL)
  19. {
  20. perror("Error!!!\n");
  21. exit(-1);
  22. }
  23.  
  24. fscanf(file1 , "%d" , &r1);
  25. printf("matrix1 row size= %d\n" , r1);
  26.  
  27. fscanf(file1 , "%d" , &c1);
  28. printf("matrix1 column size= %d\n" , c1);
  29.  
  30. int M1[r1][c1];
  31. int i,j;
  32. //filling in matrix 1 after knowing its size from 1st line in file
  33. for(i=0 ; i<r1 ; i++){
  34. for(j=0 ; j<c1 ; j++){
  35. fscanf(file1 , "%d" , &M1[i][j]);
  36. }
  37. }
  38.  
  39. //printing content fo M1 to be sure
  40. printf("content of matrix 1 is: \n");
  41. int k , m;
  42. for(k=0 ; k<r1 ; k++){
  43. for(m=0 ; m<c1 ; m++){
  44. printf("%d " , M1[k][m]);
  45.  
  46. }
  47. printf("\n");
  48. }
  49.  
  50.  
  51. fscanf(file1 , "%d" , &r2);
  52. printf("matrix2 row size is= %d\n" , r2);
  53.  
  54. fscanf(file1 , "%d" , &c2);
  55. printf("matrix2 column size is= %d\n" , c2);
  56.  
  57. int M2[r2][c2];
  58.  
  59. //filling in matrix 2 after knowing its size
  60. for(i=0 ; i<r2 ; i++){
  61. for(j=0 ; j<c2 ; j++){
  62. fscanf(file1 , "%d" , &M2[i][j]);
  63. }
  64. }
  65.  
  66. //printing matrix2
  67.  
  68. printf("content of matrix2 is: \n");
  69. for(k=0 ; k<r2 ; k++){
  70. for(m=0 ; m<c2 ; m++){
  71. printf("%d " , M2[k][m]);
  72.  
  73. }
  74. printf("\n");
  75. }
  76.  
  77. int variation;
  78.  
  79. //checking on condition of multiplication of matrices
  80. if(c1 == r2){
  81. //printf(" Press 1 for ELEMENT thread or 2 for ROW thread multiplication: \n ");
  82. // scanf("%d" , &variation);
  83. //switch case on which variation of program is desired
  84. // switch (variation){
  85. // case 1:
  86. ThreadCreation(r1 , c1 , r2 , c2 , M1 , M2);
  87. /* break;
  88. case 2: ThreadCreation(r1 , c1 , r2 , c2 , M1 , M2);
  89. break;
  90. default:
  91. printf("weird number!!!");
  92. break;
  93. }*/
  94.  
  95. }
  96. else{
  97. printf("Matrices can't be multiplied together!!!\n");
  98. exit(-1);
  99. }
  100. fclose(file1);
  101. }
  102.  
  103. /* void *multiplyElement(int mergedIntoOne[] ){
  104.  
  105. int k = 0, i = 0;
  106. //printf("this is first element %d \n" ,mergedIntoOne[0] );
  107.  
  108. for (i = 1; i <= mergedIntoOne[0]; i++){
  109.  
  110. k += mergedIntoOne[i]*mergedIntoOne[i+mergedIntoOne[0]];
  111.  
  112.  
  113. }
  114. int *p = (int*)malloc(sizeof(int));
  115. *p = k;
  116.  
  117. pthread_exit(p);
  118.  
  119. }*/
  120.  
  121. /*void ElementThreadCreation(int r1 , int c1 , int r2 , int c2 , int M1[r1][c1] , int M2 [r2][c2]){
  122. printf("\n\n --------------1ST VARIATION------------- \n\n");
  123.  
  124. int numberOfOutputElements = r1 * c2;
  125. int numberOfElementsInBothMatrices = ((r1*c1) + (r2*c2));
  126. pthread_t Thread;
  127. Thread=(pthread_t*)malloc(numberOfOutputElements*sizeof(pthread_t));
  128. int i , j, k,m ,l;
  129. int *mergedIntoOne;
  130. mergedIntoOne = (int *)malloc((numberOfElementsInBothMatrices)*sizeof(int));
  131. mergedIntoOne[0] = c1;
  132.  
  133. for (i = 0; i < r1; i++){
  134. for (j = 0; j < c2; j++){
  135.  
  136. for (k = 0; k < c1; k++) {//moving through columns of matrix 1 and once done we fill corrosponding row fro matrix 2
  137. mergedIntoOne[k+1] = M1[i][k];
  138. }
  139. for (l= 0; l < r2; l++) {//step is the size of column of matrix 1 with moving through rows of matrix 2
  140. mergedIntoOne[l+c1+1] = M2[l][j];
  141. }
  142. pthread_create(&Thread, NULL,multiplyElement, mergedIntoOne);
  143.  
  144. }
  145.  
  146. }
  147. printf("size of merged into one %d\n" , sizeof(mergedIntoOne));
  148. for(i=0 ; i< numberOfElementsInBothMatrices; i++){
  149. printf("%d ", mergedIntoOne[i]);
  150. printf("\n");
  151. }
  152.  
  153. printf("RESULTANT MATRIX IS :- \n");
  154.  
  155.  
  156.  
  157.  
  158. for(i=0 ; i<numberOfOutputElements ; i++){
  159. void *k;
  160.  
  161. //Joining all threads and collecting return value
  162. pthread_join(Thread, &k);
  163.  
  164.  
  165. int *p = (int *)k;
  166. printf("%d ",*p);
  167. if ((i + 1) % c2 == 0)
  168. printf("\n");
  169. }
  170. }*/
  171.  
  172.  
  173.  
  174. void *multiplyRow(int *infoCarrier){
  175. int r1=infoCarrier[0] , c1= infoCarrier[1] , r2=infoCarrier[2] ,c2= infoCarrier[3] ,i,j,s ,k;
  176. int M1[r1][c1] , M2[r2][c2];
  177.  
  178. int numberOfElementsInBothMatrices = (c1*r1) + (c2*r2);
  179. //filling M1
  180. s=4;
  181. while(s<(4+(r1*c1))){
  182. for(i=0 ; i<r1 ; i++){
  183. for(j=0 ; j<c1 ; j++){
  184. M1[i][j]=infoCarrier[s];
  185. s++;
  186. }
  187. }
  188. }
  189.  
  190. //filling M2
  191. s= (4+(r1*c1));
  192. while(s<(4+numberOfElementsInBothMatrices)){
  193. for(i=0 ; i<r2; i++){
  194. for(j=0 ; j<c2 ; j++){
  195. M2[i][j]=infoCarrier[s];
  196. s++;
  197. }
  198. }
  199. }
  200.  
  201.  
  202. // rowsHolder[r1][c2];
  203. //initializing my row holder array
  204. for (i = 0; i < r1; i++) {
  205. for (j = 0; j < c2; j++)
  206. {Holder[i][j]=0;
  207. }
  208. }
  209.  
  210. // printf("Hiiiiiiiiiiiiiii\n");
  211. //multiplying
  212. //rowsHolder
  213. for(i=0;i<r1;i++)
  214. {
  215. for(j=0;j<c2;j++)
  216. {
  217. for(k=0;k<c1;k++)
  218. {
  219. Holder[i][j]+=M1[i][k]*M2[k][j];
  220. //rowsHolder[i][j]/=2;
  221. }
  222. }
  223. }
  224.  
  225. // return NULL;
  226. // printf("im here TOOO\n");
  227. pthread_exit(NULL);
  228. }
  229.  
  230. void ThreadCreation(int r1 , int c1 , int r2 , int c2 , int M1[r1][c1] , int M2 [r2][c2]){
  231.  
  232. int i , j, k ,m;
  233. //int variation;
  234.  
  235. // FILE *output;
  236. //output = fopen("output.txt" , "w+");
  237. printf("\n\n");
  238. numberOfThreadsR=r1;
  239. numberOfThreadsE=r1*c2;
  240. // scanf("%d" , &variation);
  241. // switch(variation){
  242. //case 1:
  243. // break;
  244. // case 2:
  245. // break;
  246.  
  247.  
  248. //}
  249.  
  250.  
  251.  
  252. //number of threads here will be equal to number of rows in 1st matrix (r1) and is going to be array of pointer to integers
  253. pthread_t ThreadR;
  254. //ThreadR=(pthread_t*)malloc(r1*sizeof(pthread_t));
  255.  
  256.  
  257. int numberOfOutputElements = r1 * c2;
  258. int numberOfElementsInBothMatrices = ((r1*c1) + (r2*c2));
  259.  
  260. int infoCarrier[4+numberOfElementsInBothMatrices];
  261.  
  262. // rowsHolder[r1][c2];
  263. Holder = (int*)malloc(r1*sizeof(int*));
  264. for( i=0; i<r1; i++)
  265. (Holder)[i] = (int)malloc(c2*sizeof(int));
  266.  
  267. //printf("holderrr\n");
  268.  
  269. for (i = 0; i < r1; i++) {
  270. for (j = 0; j < c2; j++)
  271. {Holder[i][j]=0;}
  272. }
  273. // printf("holderrr2\n");
  274. infoCarrier[0]= r1;
  275. infoCarrier[1]=c1;
  276. infoCarrier[2]= r2;
  277. infoCarrier[3]=c2;
  278.  
  279. int s;
  280.  
  281. s=4;
  282. while(s<(4+(r1*c1))){
  283. for(i=0 ; i<r1 ; i++){
  284. for(j=0 ; j<c1 ; j++){
  285. infoCarrier[s] = M1[i][j];
  286. s++;
  287. }
  288. }
  289. }
  290.  
  291. // printf("holderrr3\n");
  292. s= (4+(r1*c1));
  293. while(s<(4+numberOfElementsInBothMatrices)){
  294. for(m=0 ; m<r2; m++){
  295. for(k=0 ; k<c2 ; k++){
  296. infoCarrier[s] = M2[m][k];
  297. s++;
  298. }
  299. }
  300. }
  301.  
  302. // printf("holderrr4\n");
  303. infoCarrier[4+numberOfElementsInBothMatrices] = NULL;
  304.  
  305. /*printf("content of infoCarrier is: \n");
  306. for(i=0 ; i<(4+numberOfElementsInBothMatrices) ; i++) {
  307. printf("%d \n" , infoCarrier[i]);
  308. }*/
  309.  
  310.  
  311. /* s=4;
  312. while(s<(4+(r1*c1))){
  313. for(i=0 ; i<r1 ; i++){
  314. for(j=0 ; j<c1 ; j++){
  315. M1[i][j]=infoCarrier[s];
  316. s++;
  317. }
  318. }
  319. }*/
  320.  
  321. clock_t start1= clock();
  322. for(i=0 ; i<numberOfThreadsR;i++){
  323. pthread_create(&ThreadR, NULL,multiplyRow,infoCarrier );
  324. // printf("im here!!\n");
  325.  
  326. }
  327.  
  328. pthread_join(ThreadR,NULL);
  329.  
  330.  
  331. FILE *output;
  332. output = fopen("output.txt" , "a+");
  333.  
  334. clock_t end1 = clock();
  335. double elapsed_time=((double)(end1-start1))/CLOCKS_PER_SEC;
  336.  
  337.  
  338. printf("Result of ROW Multiplication\n");
  339. for(i=0;i<r1;i++)
  340. {
  341. for(j=0;j<c2;j++)
  342. {
  343. printf("%d ",Holder[i][j]);
  344. if(j==c2-1)
  345. printf("\n");
  346.  
  347. // fprintf(output , "%d" ,&Holder[i][j]);
  348. // if(j==c2-1)
  349. //fprintf(output,"\n");
  350. }
  351. }
  352.  
  353.  
  354. printf("Elapsed Time Of ROW Threading is= %f\n\n\n" , elapsed_time);
  355. //fprintf(output , "%f" , &elapsed_time);
  356.  
  357.  
  358. clock_t start2 = clock();
  359. for(i=0 ; i<numberOfThreadsE;i++){
  360. pthread_create(&ThreadR, NULL,multiplyRow,infoCarrier );
  361. // printf("im here!!\n");
  362.  
  363. }
  364.  
  365. pthread_join(ThreadR,NULL);
  366. clock_t end2 = clock();
  367.  
  368. double elapsed_time2 = (double)(end2-start2) /CLOCKS_PER_SEC;
  369.  
  370. printf("Result of Element Multiplication\n");
  371. for(i=0;i<r1;i++)
  372. {
  373. for(j=0;j<c2;j++)
  374. {
  375. printf("%d ",Holder[i][j]);
  376. if(j==c2-1)
  377. printf("\n");
  378.  
  379. // fprintf(output , "%d" ,&Holder[i][j]);
  380. // if(j==c2-1)
  381. // fprintf(output,"\n");
  382. }
  383. }
  384.  
  385. printf("Elapsed time of ELEMENT threading is= %f" , elapsed_time2);
  386. for(i=0;i<r1;i++)
  387. {
  388. for(j=0;j<c2;j++)
  389. {
  390.  
  391. fprintf(output , "%d " ,Holder[i][j]);
  392. if(j==c2-1)
  393. fprintf(output,"\n");
  394. }
  395. }
  396.  
  397. fprintf(output , " elapsed time of row: %f \n" , elapsed_time);
  398. fprintf(output , " elapsed time of element: %f \n" , elapsed_time2);
  399. fclose(output);
  400.  
  401. }
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408. int main()
  409. {
  410.  
  411. fillMatrices();
  412.  
  413. return 0;
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement