Please disable adblock to view this page.

← Go home

Matrix Multiplication – Using Multicore Programming (C++)

April 24, 2016
Published By : Pratik Kataria
Categorised in: ,

/*
Write a program for Matrix Multiplication using multicore programming.
*/


#include<iostream>
#include<pthread.h>
using namespace std;

int MAT1[10][10],MAT2[10][10],MAT3[10][10];
int r1=3,c1=3,r2=3,c2=3;

void *multi(void *p)
{
int i=0;
i= (int &)p;
for(int j=0;j<3;j++)
{
//MAT3[i][j]=0;
for(int k=0;k<3;k++)
{
MAT3[i][j]=MAT3[i][j]+(MAT1[i][k]*MAT2[k][j]);
}
}
}

void read_mat(int MAT[10][10], int r, int c)
{
cout<<"Enter the elements in matrix...: "<<endl;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{

cout<<"Enter the elements for row and column here: "<<endl;
cin>>MAT[i][j];
}
}

}

void print_mat(int MAT[10][10], int r, int c)
{

cout<<"\n";
for(int i=0;i<r;i++)
{
cout<<"\n";
for(int j=0;j<c;j++)
{
cout<<MAT[i][j]<<"\t";
}
cout<<"\n";
}
}

main()
{

pthread_t tid[3];
read_mat(MAT1,r1,c1);
read_mat(MAT2,r2,c2);
cout<<"MATRIX 1"<<endl;
print_mat(MAT1,r1,c1);
cout<<"MATRIX 2"<<endl;
print_mat(MAT2,r2,c2);
for(int i=0;i<3;i++)
{
pthread_create(&tid[i],NULL,multi,(void *)i);
}
for(int i=0;i<3;i++)
{
pthread_join (tid[i],NULL);

}
cout<<"FINAL MATRIX"<<endl;
print_mat(MAT3,r1,c2);
}

View Article Page
Download

Pratik Kataria is a budding programmer, web designer and developer.