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: Easy Tutor     Category: C Programming     Views: 3027

Write a 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);    
}

  
Share: 



Easy Tutor
Easy Tutor author of Program to multiply matrix (10 x 10) with a vector using variation in Loop Splitting using multiple barriers is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
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!