Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

Program to multiply matrix (10 x 10) with a vector using variation in Loop Splitting using multiple barriers

Posted By: Isabella Brown     Category: C Programming     Views: 2269

Program to multiply matrix (10 x 10) with a vector using variation in Loop Splitting using multiple barriers.

Code for Program to multiply matrix (10 x 10) with a vector using variation in Loop Splitting using multiple barriers in C Programming

# include<stdio.h>
# include "/usr/include/sys/types.h"
# include "/usr/include/sys/shm.h"
# include "/usr/include/sys/ipc.h"
# include "/usr/include/sys/sem.h"
# include "sharedlib.h"
# include "forkjoin.h"
# include "spinlock.h"
# include "barrier.h"
main()
{    
    int MatRows,MatCols;
    int iCount,jCount;    
    int Mat[10][10];
    int Vect[10];
    
    int shmid1,shmid2,shmid3;    
    int nProc=16,id;
    
    int *bararr;    // Barrier Arrayint *temp;    // Holding Intermediate Resultint *resVect;    // Result Vector
        
    printf("Enter Number of Rows :");
    scanf("%d",&MatRows);
    
    printf("Enter Number of Columns :");
    scanf("%d",&MatCols);
    
    printf("\n Enter Matrix Elements ... \n");
    for(iCount=0;iCount<MatRows;iCount++)
    {        
        for (jCount=0;jCount<MatCols;jCount++)
        {
            printf("Mat[%d][%d] :",iCount,jCount);
            scanf("%d",&Mat[iCount][jCount]);
        }
    }
    
    printf("\n Enter Vector Elements ... \n");
    for(iCount=0;iCount<MatCols;iCount++)
    {
        printf("Vect [%d] :",iCount);
        scanf("%d",&Vect[iCount]);
    }
    
    bararr=(int *)sshared(sizeof(int)*nProc,&shmid1);    
    resVect=(int *)sshared(sizeof(int)*MatRows,&shmid2);
    temp=(int *)sshared(sizeof(int)*nProc,&shmid3);
    
    //Initializing 4 barriers i.e. bar[0],bar[4],bar[8],bar[12] for(iCount=0;iCount<4;iCount++)
    {
        barrier_init(&bararr[4*iCount],4);
    }
 
    id=process_fork(nProc);
 
    for(iCount=id/4;iCount<MatRows;iCount+=nProc/4)
    {
        temp[id]=0;
        resVect[iCount]=0;
        for(jCount=id%4;jCount<MatCols;jCount+=4)
        {
            temp[id]+=Mat[iCount][jCount] * Vect[jCount];
        }
        
        /* 
Wait For Processes So that,
they Can Finish Partial Calcuation
*/
barrier(&bararr[(id/4)*4]); if (id%4==0) { resVect[iCount]+=temp[id]+temp[id+1]+temp[id+2]+temp[id+3]; } /*
Barrier to Avoid Race Condition,
Wait Until all Processes add their
Partial Sum to resVect
*/
barrier(&bararr[(id/4)*4]); } process_join(nProc,id); printf("\n Matrix ...\n"); for(iCount=0;iCount<MatRows;iCount++) { for(jCount=0;jCount<MatCols;jCount++) { printf("%d\t",Mat[iCount][jCount]); } printf("\n"); } printf("\n Vector ...\n"); for(iCount=0;iCount<MatCols;iCount++) { printf("%d\n",Vect[iCount]); } printf("\n Result Vector ...\n"); for(iCount=0;iCount<MatRows;iCount++) { printf("%d\n",resVect[iCount]); } cleanup_memory(&shmid1); cleanup_memory(&shmid2); cleanup_memory(&shmid3); } /* Output

[divyen@localhost pp-tw5]$ cc -o Prog02 Prog02.c
[divyen@localhost pp-tw5]$ ./Prog02
Enter Number of Rows :2
Enter Number of Columns :3

Enter Matrix Elements ...
Mat[0][0] :1
Mat[0][1] :2
Mat[0][2] :3
Mat[1][0] :4
Mat[1][1] :5
Mat[1][2] :6

Enter Vector Elements ...
Vect [0] :1
Vect [1] :2
Vect [2] :3

Matrix ...
1 2 3
4 5 6

Vector ...
1
2
3

Result Vector ...
14
32

*/
  
Share: 



Isabella Brown
Isabella Brown author of Program to multiply matrix (10 x 10) with a vector using variation in Loop Splitting using multiple barriers is from London, United Kingdom.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!