Advertisement
mohamedmahmoud97

matMul

Nov 22nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.07 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <time.h>
  6.  
  7. void nonThreadedMatMult(double**A, double**B, FILE*outFile);
  8. void readMatrixFiles(FILE*inFile1, FILE*inFile2);
  9. int threadedMatMultPerElement(char *argv[], FILE*outFile);
  10. void *elementCalc(void*param);
  11. int threadedMatMultPerRow(char *argv[], FILE*outFile);
  12. void *rowCalc(void*param);
  13. int fpeek(FILE *stream);
  14.  
  15. int x=0, y_1=0, y2=0, z=0;
  16. int a=0,b=0,c=0;
  17. double **A, **B, **C;
  18.  
  19. typedef struct threadargs{
  20. int arg1;
  21. int arg2;
  22. }threadargs;
  23.  
  24. int main(int argc, char *argv[])
  25. {
  26. FILE *inFile1;
  27. FILE *inFile2;
  28. FILE *outFile;
  29.  
  30. int noOfTinE, noOfTinR;
  31.  
  32. readMatrixFiles(inFile1, inFile2);
  33.  
  34. nonThreadedMatMult(A,B,outFile);
  35.  
  36. noOfTinE = threadedMatMultPerElement(argv, outFile);
  37.  
  38. noOfTinR = threadedMatMultPerRow(argv, outFile);
  39.  
  40. printf("# of threads in elements used: %d\n", noOfTinE);
  41.  
  42. printf("# of threads in rows used: %d\n", noOfTinR);
  43.  
  44. //fclose(inFile1);
  45. //fclose(inFile2);
  46. //fclose(outFile);
  47.  
  48. return 0;
  49. }
  50.  
  51. void readMatrixFiles(FILE*inFile1, FILE*inFile2)
  52. {
  53. bool fileFound = false;
  54. bool validSize = false;
  55.  
  56. int i,j,k;
  57. int count;
  58.  
  59. double temp;
  60.  
  61. //open files and set mode to read
  62. do
  63. {
  64. inFile1 = fopen("matA.txt","r");
  65. if(!inFile1)
  66. {
  67. printf("File Not Found!!\n");
  68. fileFound=true;
  69. }
  70. else
  71. fileFound=false;
  72.  
  73. }while(fileFound);
  74.  
  75. do
  76. {
  77. inFile2 = fopen("matB.txt","r");
  78. if(!inFile2)
  79. {
  80. printf("File Not Found!!\n");
  81. fileFound=true;
  82. }
  83. else
  84. fileFound=false;
  85.  
  86. }while(fileFound);
  87.  
  88. //////////////////////////////////////////////////////////////////////////////////////
  89.  
  90. //Read matrix A of unknown sizefrom file 1
  91. while(!feof(inFile1))
  92. {
  93. count = 0;
  94. printf("x = %d\n", x);
  95. while(fscanf(inFile1, "%lf", &temp) && fpeek(inFile1)!='\n' && !feof(inFile1))
  96. {
  97. printf("%lf\n", temp);
  98. if(x==0)
  99. {
  100. y_1++;
  101. }
  102. else
  103. {
  104. if(count>y_1)
  105. {
  106. printf("There is an error in line number: %d\n", x);
  107. return;
  108. }
  109. count++;
  110. }
  111. }
  112. if (x==0)
  113. y_1++;
  114. x++;
  115. }
  116. printf("%d\n",y_1);
  117. printf("%d\n", x);
  118.  
  119. fseek(inFile1,0,0);
  120.  
  121. A = (double **)malloc(x * sizeof(double*));
  122. for(i = 0; i < x; i++)
  123. {
  124. A[i] = (double *)malloc(y_1 * sizeof(double));
  125. }
  126.  
  127. for(i=0;i<x;i++)
  128. {
  129. for(j=0;j<y_1;j++){
  130. fscanf(inFile1, "%lf", &A[i][j]);
  131. printf("matA: %lf\n", A[i][j]);
  132. }
  133. }
  134.  
  135.  
  136. //Read matrix B of unknown sizefrom file 2
  137. while(!feof(inFile2))
  138. {
  139. count = 0;
  140. printf("y2 = %d\n", y2);
  141. while(fscanf(inFile2, "%lf", &temp) && fpeek(inFile2)!='\n' && !feof(inFile2))
  142. {
  143. printf("%lf\n", temp);
  144. if(y2==0)
  145. {
  146. z++;
  147. }
  148. else
  149. {
  150. if(count>z)
  151. {
  152. printf("There is an error in line number: %d\n", y2);
  153. return;
  154. }
  155. count++;
  156. }
  157. }
  158. if (y2==0)
  159. z++;
  160. y2++;
  161. }
  162.  
  163. printf("%d\n",y2);
  164. printf("%d\n", z);
  165.  
  166. fseek(inFile2,0,0);
  167.  
  168. B = (double **)malloc(y2 * sizeof(double*));
  169. for(i = 0; i < y2; i++)
  170. {
  171. B[i] = (double *)malloc(z * sizeof(double));
  172. }
  173.  
  174. for(i=0;i<y2;i++)
  175. {
  176. for(j=0;j<z;j++){
  177. fscanf(inFile2, "%lf", &B[i][j]);
  178. printf("matB: %lf\n", B[i][j]);
  179. }
  180. }
  181.  
  182. //check if they have valid sizes to be multipilcated
  183. if(y_1!=y2)
  184. validSize = false;
  185. else
  186. validSize = true;
  187.  
  188. if(validSize == false)
  189. {
  190. printf("Matrix Sizes are not valid you Fugget!! \nCant multiply those two matrices \n");
  191. fclose(inFile1);
  192. fclose(inFile2);
  193. return;
  194. }
  195. else
  196. {
  197. printf("matrices sizes are valid\n");
  198. //allocate Matrcies
  199.  
  200. C = (double **)malloc(x * sizeof(double*));
  201. for(i = 0; i < x; i++)
  202. C[i] = (double *)malloc(z * sizeof(double));
  203.  
  204. ////////////////////////////////////////////////
  205. printf("\n\n");
  206. //print matrix 1
  207. printf("MatA:\n");
  208. for(i=0;i<x;i++)
  209. {
  210. for(j=0;j<y_1;j++)
  211. printf("%lf\t",A[i][j]);
  212.  
  213. printf("\n");
  214. }
  215. //////////////////////////////////////////////
  216. printf("\n\n");
  217. //print matrix 2
  218. printf("MatB:\n");
  219. for(i=0;i<y2;i++)
  220. {
  221. for(j=0;j<z;j++)
  222. printf("%lf\t",B[i][j]);
  223.  
  224. printf("\n");
  225. }
  226. /////////////////////////////////////////////
  227. }
  228. printf("\nMatrices are read successfully\n\n");
  229. return;
  230. }
  231.  
  232. void nonThreadedMatMult(double**A, double**B, FILE*outFile)
  233. {
  234. clock_t start, end;
  235. double cpu_time_used;
  236.  
  237. start = clock();
  238.  
  239. int i=0, j=0, k=0;
  240. bool fileFound = false;
  241.  
  242. for (i = 0; i < x; ++i)
  243. {
  244. for (j = 0; j < z; ++j)
  245. {
  246. for (k = 0; k < y_1; ++k)
  247. {
  248. C[i][j] += A[i][k] * B[k][j];
  249. }
  250. }
  251. }
  252.  
  253. //open files and set mode to read
  254. do
  255. {
  256. outFile = fopen("matC.txt","w");
  257. if(!outFile)
  258. {
  259. printf("File Not Found!!\n");
  260. fileFound=true;
  261. }
  262. else
  263. fileFound=false;
  264.  
  265. }while(fileFound);
  266.  
  267. for(i=0;i<x;i++)
  268. {
  269. for(j=0;j<z;j++)
  270. fprintf(outFile, "%lf\t",C[i][j]);
  271. fprintf(outFile, "\n");
  272. }
  273.  
  274. printf("MatC:\n");
  275. for(i=0;i<x;i++)
  276. {
  277. for(j=0;j<z;j++)
  278. printf("%lf\t",C[i][j]);
  279.  
  280. printf("\n");
  281. }
  282.  
  283. end = clock();
  284. cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
  285. printf("time taken for multiplication without threads: %lf sec\n", cpu_time_used);
  286.  
  287. return;
  288. }
  289.  
  290. int threadedMatMultPerElement(char *argv[], FILE*outFile)
  291. {
  292. clock_t start, end;
  293. double cpu_time_used;
  294.  
  295. start = clock();
  296.  
  297. int i=0,j=0;
  298. int E = x*z;
  299. pthread_t elementThread[E];
  300. //pthread_attr_t attr;
  301.  
  302. for (a = 0; a < x; ++a)
  303. {
  304. for (b = 0; b < z; ++b)
  305. {
  306. threadargs *args = malloc(sizeof(struct threadargs));
  307. args->arg1 = a;
  308. args->arg2 = b;
  309. printf("%d and %d\n", args->arg1, args->arg2);
  310. //pthread_attr_init(&attr);
  311. //edit this line of creating threads
  312. pthread_create(&elementThread[i],NULL,elementCalc,(void *)args);
  313.  
  314. i++;
  315. }
  316. }
  317. for (i = 0; i < E; ++i)
  318. {
  319. pthread_join(elementThread[i],NULL);
  320. }
  321.  
  322. outFile = fopen("matC.txt","w");
  323.  
  324. for(i=0;i<x;i++)
  325. {
  326. for(j=0;j<z;j++)
  327. fprintf(outFile, "%lf\t",C[i][j]);
  328. fprintf(outFile, "\n");
  329. }
  330.  
  331. printf("MatC:\n");
  332. for(i=0;i<x;i++)
  333. {
  334. for(j=0;j<z;j++)
  335. printf("%lf\t",C[i][j]);
  336.  
  337. printf("\n");
  338. }
  339.  
  340. end = clock();
  341. cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
  342. printf("Time of using thread per element: %lf sec\n", cpu_time_used);
  343.  
  344. return E;
  345. }
  346.  
  347. void *elementCalc(void*argsE)
  348. {
  349. threadargs *args = argsE;
  350. for (c = 0; c < y_1; ++c)
  351. {
  352. C[args->arg1][args->arg2] += A[args->arg1][c] * B[c][args->arg2];
  353. }
  354.  
  355. pthread_exit(0);
  356. }
  357.  
  358. int threadedMatMultPerRow(char *argv[], FILE*outFile)
  359. {
  360. clock_t start, end;
  361. double cpu_time_used;
  362.  
  363. start = clock();
  364.  
  365. int i=0,j=0;
  366. int E = x;
  367. pthread_t elementThread[E];
  368. pthread_attr_t attr;
  369.  
  370. for (a = 0; a < x; ++a)
  371. {
  372. threadargs *args = malloc(sizeof(struct threadargs));
  373. args->arg1 = a;
  374.  
  375. printf("%d \n", args->arg1);
  376. pthread_create(&elementThread[i],NULL,rowCalc,(void *)args);
  377.  
  378. i++;
  379. }
  380. for (i = 0; i < E; ++i)
  381. {
  382. pthread_join(elementThread[i],NULL);
  383. }
  384.  
  385. outFile = fopen("matC.txt","w");
  386.  
  387. for(i=0;i<x;i++)
  388. {
  389. for(j=0;j<z;j++)
  390. fprintf(outFile, "%lf\t",C[i][j]);
  391. fprintf(outFile, "\n");
  392. }
  393.  
  394. printf("MatC:\n");
  395. for(i=0;i<x;i++)
  396. {
  397. for(j=0;j<z;j++)
  398. printf("%lf\t",C[i][j]);
  399.  
  400. printf("\n");
  401. }
  402.  
  403. end = clock();
  404. cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
  405. printf("Time of using thread per row: %lf sec\n", cpu_time_used);
  406.  
  407. return E;
  408. }
  409.  
  410. void *rowCalc(void*argsE)
  411. {
  412. threadargs *args = argsE;
  413. for (int b = 0; b < z; ++b)
  414. {
  415. for (c = 0; c < y_1; ++c)
  416. {
  417. C[args->arg1][b] += A[args->arg1][c] * B[c][b];
  418. }
  419. }
  420.  
  421. pthread_exit(0);
  422. }
  423.  
  424. int fpeek(FILE *stream)
  425. {
  426. int c;
  427.  
  428. c = fgetc(stream);
  429. ungetc(c, stream);
  430.  
  431. return c;
  432. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement