Logo 
Search:

C Programming Articles

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

PROGRAM FOR MATRIX VECTOR MULTIPLICATION USING INNER LOOP SPLITING FOR N NO.OF PROCESSES

Posted By: William Bouchard     Category: C Programming     Views: 3719

PROGRAM FOR MATRIX VECTOR MULTIPLICATION USING INNER LOOP SPLITING FOR N NO.OF PROCESSES.

Code for PROGRAM FOR MATRIX VECTOR MULTIPLICATION USING INNER LOOP SPLITING FOR N NO.OF PROCESSES in C Programming

#include<stdio.h>
#include "headfork.h"
#include "headshr.h"
#include "headbar.h"

main()
{
    int a[5][5],v[5],i,j,n,id,t;
    int **bar_arr,*tempsum,*result;
    int np,npg,**shmid;

    printf("Enter the limit : ");
    scanf("%d",&n);

    printf("Enter the elements of arr :\n");
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("a[%d][%d] : ",i,j);
            scanf("%d",&a[i][j]);
        }
    }
    
    printf("Enter the elements of vector :\n");
    for(i=0;i<n;i++)
    {
        printf("v[%d] : ",i);
        scanf("%d",&v[i]);
    }

    printf("Enter the no.of proc. : ");
    scanf("%d",&np);

    printf("Enter the no.of proc within a group : ");
    scanf("%d",&npg);

    shmid=(int **)malloc(sizeof(int)*(np/npg)+2);

    for(i=0;i<(np/npg)+2;i++)
        shmid[i]=(int *)malloc(sizeof(int));
    
    bar_arr=(int **)malloc(sizeof(int)*(np/npg));

    tempsum=shared(sizeof(int)*np+1,shmid[0]);

          result=shared(n+1,shmid[1]);

    for(i=0;i<(np/npg);i++)
        bar_arr[i]=shared(sizeof(int)*5,shmid[i+2]);

    for(i=0;i<(np/npg);i++)
        bar_arr[i]=bar_init(npg);

    id=p_fork(np);
    for(i=(id/npg);i<n;i+=(np/npg))
    {
        *(tempsum+id)=0;
        *(result+i)=0;
        for(j=(id%npg);j<n;j+=npg)
        {
            *(tempsum+id)+=a[i][j]*v[j];
        }
        barrier(bar_arr[id/npg]);

        if(id%npg==0)
        {
            for(t=id;t<(id+npg);t++)
                *(result+i)+=*(tempsum+t);
        }
        barrier(bar_arr[id/npg]);
    }

    p_join(np,id);
    for(i=0;i<n;i++)
        printf("%d\t",*(result+i));
}
    
 
OUTPUT

knoppix@ttyp0[pp]$ cc inr_lps1.c
 knoppix@ttyp0[pp]$ ./a.out

 Enter the limit : 3

 Enter the elements of arr :
 a[0][0] : 1
 a[0][1] : 2
 a[0][2] : 3
 a[1][0] : 0
 a[1][1] : 1
 a[1][2] : 0
 a[2][0] : 1
 a[2][1] : 0
 a[2][2] : 0

 Enter the elements of vector :
 v[0] : 1
 v[1] : 1
 v[2] : 1

 Enter the no.of proc. : 12

 Enter the no.of proc within a group : 3
 6       1       1
  
Share: 



William Bouchard
William Bouchard author of PROGRAM FOR MATRIX VECTOR MULTIPLICATION USING INNER LOOP SPLITING FOR N NO.OF PROCESSES is from Montreal, Canada.
 
View All Articles

 
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!