Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // matrix_multiplication_with_threads
- //
- // Created by Ahmed on 10/18/17.
- // Copyright © 2017 Abdellah. All rights reserved.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- typedef long long ll;
- struct Matrix {
- ll **mat;
- int n,m;
- } a, b, res1, res2;
- // this function to allocate matrix
- void allocate(struct Matrix *a,int n,int m) {
- a->n = n , a->m = n;
- a->mat = (ll**) malloc(n*sizeof(ll*));
- for(int i=0; i < n ; ++i)
- a->mat[i] = (ll*) malloc(m*sizeof(ll));
- for(int i=0 ; i < n ; ++i)
- for (int j=0; j < m; ++j)
- a->mat[i][j] = 0;
- }
- // this struct for identifying the cell in method_1 thread
- struct cell {
- int i,j;
- };
- // this method for computing the cell in (method_1)
- void * compute_cell(void *args) {
- int i = ((struct cell*) args)->i , j = ((struct cell*) args)->j;
- printf("cell:%d %d\n",i,j);
- for(int k = 0; k < b.n ; ++k)
- res1.mat[i][j] += a.mat[i][k]*b.mat[k][j];
- pthread_exit(0);
- }
- // this funciton for calculating result matrix each elemnt in it copmuted in separate thread
- void method_1 () {
- pthread_t threads[a.n][b.m];
- for(int i=0 ; i < a.n ; ++i) {
- for (int j=0; j < b.m ; ++j) {
- struct cell* args = (struct cell*) malloc(sizeof(struct cell));
- args->i = i , args->j = j;
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- int rc = pthread_create(&threads[i][j], &attr, compute_cell, (void*) args);
- if(rc)
- printf("Error, happened in creating thread int Method_1 cell:%d %d and returns %d.",i,j,rc);
- free(args);
- }
- }
- // finish each thread and each cell in matrix
- for(int i=0 ; i < a.n ; ++i)
- for (int j=0; j < b.m; ++j)
- pthread_join(threads[i][j], NULL);
- }
- // this function for computing the roww in (method_2)
- void *compute_row (void *args) {
- int *row = (int*) args;
- for(int j=0 ; j < b.m ; ++j)
- for(int k=0 ; k < b.n ; ++k)
- res2.mat[*row][j] += a.mat[*row][k]*b.mat[k][j];
- pthread_exit(0);
- }
- // this function for calculating result matrix each row in it computed in separate thread
- void method_2 () {
- pthread_t threads[a.n];
- for(int i=0; i < a.n ;++i) {
- int args = i;
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- int rc = pthread_create(&threads[i], &attr, compute_row, &args);
- if(rc)
- printf("Error, happened in creating thread int Method_2 row:%d and returns %d.",i,rc);
- }
- // finish each threa and each row in matrix
- for (int i=0; i < a.n ; ++i)
- pthread_join(threads[i], NULL);
- }
- // this function for reading and allocating each matrix
- void read_matrix(struct Matrix *a) {
- int n,m;
- scanf("%d%d",&n,&m);
- allocate(a,n,m);
- for(int i=0; i < a->n ; ++i)
- for(int j=0 ; j < a->m ; ++j)
- scanf("%lld",&a->mat[i][j]);
- }
- void print(struct Matrix *a) {
- for(int i=0; i < a->n ; ++i) {
- for(int j=0 ; j < a->m ; ++j)
- printf("%lld\t",a->mat[i][j]);
- printf("\n");
- }
- }
- void true_ans() {
- printf("---------- true answer ----------\n");
- for(int i=0;i<a.n;++i){
- for(int j=0;j<b.m;++j){
- ll cell = 0;
- for(int k=0;k<b.n;++k) cell += a.mat[i][k]*b.mat[k][j];
- printf("%lld\t",cell);
- }
- printf("\n");
- }
- }
- int main(int argc, const char * argv[]) {
- read_matrix(&a) , read_matrix(&b);
- allocate(&res1, a.n, b.m) , allocate(&res2, a.n, b.m);
- printf("Method_ 1:\n");
- method_1() , print(&res1);
- printf("Method_ 2:\n");
- method_2() , print(&res2);
- true_ans();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement