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 Indirect Scheduling

Posted By: Guilherme Silva     Category: C Programming     Views: 3275

Program to multiply matrix (10 x 10) with a vector using Indirect Scheduling.

Code for Program to multiply matrix (10 x 10) with a vector using Indirect Scheduling 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"forkjoin.h"// included in attachment
# include"sharedlib.h"int main()
{
        
    int Mat[10][10];
    int Vect[10];
    int *resVect;
    
    int MatRow,MatCol;
    
    int iVal[100],kVal[100];    
    int iCount,jCount,kCount;
    int shmid1,shmid2;    
    
    
    int Index=0,nProc,shmid[6],id;
        
    
    
        printf("Enter Number of Rows :");
    scanf("%d",&MatRow);
    
    nProc=MatRow;
    
    printf("Enter Number of Columns :");
    scanf("%d",&MatCol);        
    
        resVect=(int *)sshared(sizeof(int)*MatRow,&shmid[0]);        
    
        
    printf("\n Enter Elements of Matrix ...\n");
        for(iCount=0;iCount<MatRow;iCount++)  
        {
                for(jCount=0;jCount<MatCol;jCount++)
                {
                        printf("Mat[%d][%d] :",iCount,jCount);
                        scanf("%d",&Mat[iCount][jCount]);                        
                }
        resVect[iCount]=0;
        }
        
        printf("\n Enter Elements of Vector ...\n");
        for(iCount=0;iCount<MatCol;iCount++)
        {                
             printf("Vect[%d] :",iCount);
                scanf("%d",&Vect[iCount]);                
        }
    
        for(iCount=0;iCount<MatRow;iCount++)
        {                
                for(kCount=0;kCount<MatCol;kCount++)
                {
                    iVal[Index] = iCount;                                
                    kVal[Index] = kCount;
                    Index++;
                }                
        }

        printf("\n Matrix ... \n");
        for(iCount=0;iCount<MatRow;iCount++)
        {
                for(jCount=0;jCount<MatCol;jCount++)
                {
                        printf("%d\t",Mat[iCount][jCount]);
                }
                printf("\n");
        }
        printf("\n Vector ... \n");
        for(iCount=0;iCount<MatCol;iCount++)
        {
            printf("%d\n",Vect[iCount]);
        }
            
    id=process_fork(nProc);
        
        for(Index=id;Index<MatRow*MatCol;Index+=nProc)
        {
                iCount=iVal[Index];         
                kCount=kVal[Index];
                resVect[iCount] += Mat[iCount][kCount]*Vect[kCount];
        }        
    process_join(nProc,id);
    
        printf("\nMultiplication of Matrix and Vector is ...\n");
        for(iCount=0;iCount<MatRow;iCount++)
        {
            printf("%d \n",resVect[iCount]);                                        
        }
        
    return 0;
}

/* Output

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

Enter Elements of Matrix ...
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 Elements of Vector ...
Vect[0] :1
Vect[1] :2
Vect[2] :3

Matrix ...
1 2 3
4 5 6

Vector ...
1
2
3

Multiplication of Matrix and Vector is ...
14
32

*/
  
Share: 



Guilherme  Silva
Guilherme Silva author of Program to multiply matrix (10 x 10) with a vector using Indirect Scheduling is from Salvador, Brazil.
 
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!