Ads

Sunday 6 April 2014

Object Oriented and Multi-core Programming Assignment Group C -1

#include<iostream>
#include<pthread.h>
#include<string>
#include<unistd.h>
#include<cstdlib>
#include<iomanip>
#include<sched.h>

using namespace std;




class threadClass
{
 pthread_t t1;
 
 public :
  //friend void function();
  void run();
  void join();
  void stick_thread(int coreid);
  pthread_t self();
  //virtual void *run(void) = 0;
};




class oddeve:public threadClass
 {
  int a[10],n;
  public:
  //void *run();
  void exchange(int,int);
  void compare(int,int);
  void oddEvenMerge(int,int,int);
  void oddEvenMergeSort(int,int);
  void getdata();
  void display();
 };

static void *function1(void *);
void *function1(void *arg)
{
 ((oddeve*)arg)->getdata();
}
/*
static void *function2(void *);
void *function2(void *arg)
{
 ((oddeve*)arg)->display();
}
*/

void threadClass::join()
{
 pthread_join(t1,NULL);
}


pthread_t threadClass::self()
{ 
 return t1;
}


void threadClass::stick_thread(int core_id)
{ 
 cpu_set_t cpuset;
 CPU_ZERO(&cpuset);
 CPU_SET(core_id, &cpuset);
 //pthread_t current_thread = pthread_self();    
 sched_setaffinity(getpid(), sizeof(cpu_set_t), &cpuset);
}


void threadClass::run()
{
 
 pthread_create(&t1,NULL,function1,this);
}

void oddeve::exchange(int i, int j) 
{
 int t = a[i];
 a[i] = a[j];
 a[j] = t;
}
 
void oddeve::compare(int i, int j) 
{
 if (a[i] > a[j])
  exchange(i, j);
}
 
/**
* lo is the starting position and
* n is the length of the piece to be merged,
* r is the distance of the elements to be compared
*/
void oddeve::oddEvenMerge(int lo, int n, int r) 
{
 int m = r * 2;
 if (m < n) 
 {
  oddEvenMerge(lo, n, m); // even subsequence
  oddEvenMerge(lo + r, n, m); // odd subsequence
  for (int i = lo + r; i + r < lo + n; i += m)
  compare(i, i + r);
 } 
 else
 compare(lo, lo + r);
}
 
/**
* sorts a piece of length n of the array
* starting at position lo
*/
void oddeve::oddEvenMergeSort(int lo, int n) 
{
 if (n > 1) 
 {
  int m = n / 2;
  oddEvenMergeSort(lo, m);
  oddEvenMergeSort(lo+m, m);
  oddEvenMerge(lo, n, 1);
 }
}
 
void oddeve::getdata()
{

 cout<<"\n\nCORE : "<<sched_getcpu();
 cout<<"\nTHREAD : "<<(long)self()<<"\n";
 cout<<"\nEnter no. of elements in array (n th power of 2) : ";
 cin>>n;
 cout<<"\nEnter array elements:";
 for (int i = 0; i < n; i++)
  cin>>a[i];
 
 oddEvenMergeSort(0, n);
 
} 

void oddeve::display()
{
 //cout<<"\n\nCORE : "<<sched_getcpu();
 cout<<"\nTHREAD ID: "<<(long)self()<<"\n";
 cout<<"\nSorted Array: ";
 for (int i = 0; i < n; i++)
  cout<<a[i]<<"\t";
 cout<<endl;
}
 


int main()
{
 oddeve *thread1=new oddeve;
 oddeve *thread2=new oddeve;
 //oddeve *thread1=new oddeve;
 thread1->stick_thread(0);
 thread1->run();
 sleep(20);
 
 

 thread2->stick_thread(1);
 thread2->run();
 sleep(20);
 thread1->display();
 thread2->display();
 pthread_exit(NULL);
 return 0;
}
 
 



Object Oriented and Multi-core Programming Assignment Group C -3

/* 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

*/

Wednesday 2 April 2014

Programming Laboratory Assignments VC++

Object Oriented and Multi-core Programming Assignment Group B codes 11-18

Microprocessors and Interfacing Technology Assignment -Hardware Outputs

Microprocessors and Interfacing Technology Assignment-TSR "WriteUp"

Microprocessors and Interfacing Technology Assignment- I5 Case Study "WriteUp"

Microprocessors and Interfacing Technology Assignment- ADC n DAC "Write up"

Microprocessors and Interfacing Technology Assignment -8087 "writeup"

Tuesday 1 April 2014

Programming Laboratory Assignments Codes

Object Oriented and Multi-core Programming Assignment Group B codes

Programming Laboratory Assignments Writeups