/* Assignment 3 (Group C)
* Problem Statement :- Write a concurrent
program for Matrix Multiplication.
* Effective use of Multicore Architecture is
expected.
*/
#include<iostream>
#include<pthread.h>
#include<string>
#include<unistd.h>
#include<cstdlib>
#include<iomanip>
#include<sched.h>
using namespace std;
class threadClass
{
pthread_t tid;
public :
int run();
int join();
pthread_t self();
virtual void *DoSomething(void) = 0;
};
class matrix : public
threadClass
{
int m[10][10];
public:
int stick_this_thread_to_core(int core_id);
void *DoSomething();
void get_value(int,int);
int mul(matrix, matrix, int, int, int);
void display(int, int);
};
static void* function(void*
arg);
int threadClass::run()
{
int result = pthread_create(&tid,NULL,function,this);
return result;
}
int threadClass::join()
{
return (pthread_join(tid,NULL));
}
static void* function(void*
arg)
{
return ((threadClass*)arg)->DoSomething();
}
int
matrix::stick_this_thread_to_core(int core_id)
{
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core_id, &cpuset);
pthread_t current_thread = pthread_self();
return sched_setaffinity(getpid(), sizeof(cpu_set_t),
&cpuset);
}
pthread_t
threadClass::self()
{
return tid;
}
//for getting data
void matrix::get_value(int
r,int c)
{
cout<<"\n";
//r means row and c means column
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<"
M["<<i+1<<"]["<<j+1<<"] = ";
cin>>m[i][j];
}
}
}
int matrix::mul(matrix A,
matrix B, int r, int c, int c1)
{
//Initialization matrix to 0
for(int i=0;i>r;i++)
{
for(int j=0;j<c;j++)
{
m[i][j]=0;
}
}
//Multiplication of matrices
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
m[i][j]=0;
for(int k=0;k<c1;k++)
{
m[i][j]+=(A.m[i][k] *
B.m[k][j]);
}
}
}
}
void matrix::display(int
r,int c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<setw(7)<<m[i][j]<<setw(7);
}
cout<<"\n";
}
//cout<<"\n\n";
}
void*
matrix::DoSomething(void)
{
cout<<"\n\nCORE :
"<<sched_getcpu();
cout<<"\nTHREAD :
"<<(int)self()<<"\n";
matrix i1,i2,i3;
int ra,rb,ca,cb;
//Enter Number of rows and columns
cout<<"\nEnter rows and columns of A matrix :
";
cout<<"\nRows : ";
cin>>ra;
cout<<"Columns : ";
cin>>ca;
cout<<"\nEnter rows and columns of B matrix :
";
cout<<"\nRows : ";
cin>>rb;
cout<<"Columns : ";
cin>>cb;
//Enter elements of matrices
cout<<"\n Enter Elements of Matrix A";
i1.get_value(ra,ca);
cout<<"\n Enter Elements of Matrix B";
i2.get_value(rb,cb);
//Display Contents of matrices
cout<<"\n\n MATRIX A\n";
i1.display(ra,ca);
cout<<"\n\n MATRIX B\n";
i2.display(rb,cb);
sleep(30);
cout<<"\n\nThread ID :
"<<(int)self();
if(ca == rb)
{
cout<<"\n Matrices Multiplication
\n";
i3.mul(i1,i2,ra,cb,ca);
i3.display(ra,cb);
}
else
{
cout<<"\n Multiplition is not
possible \n";
}
cout<<"Exiting thread :
"<<(int)self();
cout<<"\n\n";
return NULL;
}
int main()
{
matrix *thread1 = new matrix();
matrix *thread2 = new matrix();
thread1->stick_this_thread_to_core(0);
thread1->run();
sleep(30);
thread2->stick_this_thread_to_core(1);
thread2->run();
thread1->join();
thread2->join();
exit(0);
return 0;
}
/*OUTPUT :-
CORE : 0
THREAD : -1976924416
Enter rows and columns of A
matrix :
Rows : 2
Columns : 2
Enter rows and columns of B
matrix :
Rows : 2
Columns : 2
Enter Elements of Matrix A
M[1][1] = 1
M[1][2] = 2
M[2][1] = 5
M[2][2] = 8
Enter Elements of Matrix B
M[1][1] = 1
M[1][2] = 4
M[2][1] = 2
M[2][2] = 6
MATRIX A
1
2
5
8
MATRIX B
1
4
2
6
CORE : 1
THREAD : -1985317120
Enter rows and columns of A
matrix :
Rows : 2
Columns : 2
Enter rows and columns of B
matrix :
Rows : 2
Columns : 2
Enter Elements of Matrix A
M[1][1] = 3
M[1][2] = 4
M[2][1] = 7
M[2][2] = 1
Enter Elements of Matrix B
M[1][1] = 9
M[1][2] = 2
M[2][1] = 4
M[2][2] = 7
MATRIX A
3
4
7
1
MATRIX B
9
2
4
7
Thread ID : -1976924416
Matrices Multiplication
5
16
21
68
Exiting thread : -1976924416
Thread ID : -1985317120
Matrices Multiplication
43
34
67
21
Exiting thread : -1985317120
*/