• API
• FAQ
• Tools
• Archive
daily pastebin goal
86%
SHARE
TWEET

# Untitled

a guest Oct 19th, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. //
2. //  main.c
4. //
5. //  Created by Ahmed on 10/18/17.
7. //
8.
9. #include <stdio.h>
10. #include <stdlib.h>
12.
13. typedef long long ll;
14.
15. struct Matrix {
16.     ll **mat;
17.     int n,m;
18.
19. } a, b, res1, res2;
20.
21.
22. // this function to allocate matrix
23. void allocate(struct Matrix *a,int n,int m) {
24.     a->n = n , a->m = n;
25.     a->mat = (ll**) malloc(n*sizeof(ll*));
26.     for(int i=0; i < n ; ++i)
27.         a->mat[i] = (ll*) malloc(m*sizeof(ll));
28.
29.     for(int i=0 ; i < n ; ++i)
30.         for (int j=0; j < m; ++j)
31.             a->mat[i][j] = 0;
32. }
33.
34. // this struct for identifying the cell in method_1 thread
35. struct cell {
36.     int i,j;
37. };
38.
39. // this method for computing the cell in (method_1)
40. void * compute_cell(void *args) {
41.
42.     int i = ((struct cell*) args)->i , j = ((struct cell*) args)->j;
43.     printf("cell:%d %d\n",i,j);
44.
45.     for(int k = 0; k < b.n ; ++k)
46.         res1.mat[i][j] += a.mat[i][k]*b.mat[k][j];
47.
49. }
50.
51.
52.
53.
54. // this funciton for calculating result matrix each elemnt in it copmuted in separate thread
55. void method_1 () {
56.
58.     for(int i=0 ; i < a.n ; ++i) {
59.         for (int j=0; j < b.m ; ++j) {
60.             struct cell* args = (struct cell*) malloc(sizeof(struct cell));
61.             args->i = i , args->j = j;
64.             int rc = pthread_create(&threads[i][j], &attr, compute_cell, (void*) args);
65.             if(rc)
66.                 printf("Error, happened in creating thread int Method_1 cell:%d %d and returns %d.",i,j,rc);
67.             free(args);
68.         }
69.     }
70.
71.     // finish each thread and each cell in matrix
72.     for(int i=0 ; i < a.n ; ++i)
73.         for (int j=0; j < b.m; ++j)
75.
76. }
77.
78.
79. // this function for computing the roww in (method_2)
80. void *compute_row (void *args) {
81.
82.     int *row = (int*) args;
83.
84.     for(int j=0 ; j < b.m ; ++j)
85.         for(int k=0 ; k < b.n ; ++k)
86.             res2.mat[*row][j] += a.mat[*row][k]*b.mat[k][j];
87.
89. }
90.
91. // this function for calculating result matrix each row in it computed in separate thread
92. void method_2 () {
94.
95.     for(int i=0; i < a.n ;++i) {
96.         int args = i;
99.         int rc = pthread_create(&threads[i], &attr, compute_row, &args);
100.         if(rc)
101.             printf("Error, happened in creating thread int Method_2 row:%d and returns %d.",i,rc);
102.
103.     }
104.     // finish each threa and each row in matrix
105.     for (int i=0; i < a.n ; ++i)
107.
108. }
109.
110. // this function for reading and allocating each matrix
111. void read_matrix(struct Matrix *a) {
112.     int n,m;
113.     scanf("%d%d",&n,&m);
114.
115.     allocate(a,n,m);
116.
117.     for(int i=0; i < a->n ; ++i)
118.         for(int j=0 ; j < a->m ; ++j)
119.             scanf("%lld",&a->mat[i][j]);
120.
121. }
122.
123. void print(struct Matrix *a) {
124.     for(int i=0; i < a->n ; ++i) {
125.         for(int j=0 ; j < a->m ; ++j)
126.             printf("%lld\t",a->mat[i][j]);
127.         printf("\n");
128.     }
129. }
130.
131. void true_ans() {
132.
133.     printf("---------- true answer ----------\n");
134.     for(int i=0;i<a.n;++i){
135.         for(int j=0;j<b.m;++j){
136.             ll cell = 0;
137.             for(int k=0;k<b.n;++k) cell += a.mat[i][k]*b.mat[k][j];
138.             printf("%lld\t",cell);
139.         }
140.         printf("\n");
141.     }
142. }
143. int main(int argc, const char * argv[]) {
144.
145.