Skip to content

Program to calculate Multiplication of 2×2 matrices using loop splitting

Aim: Program to calculate  Multiplication of 2×2 matrices using loop splitting

Source Code:

#include<stdio.h>
#include”shmlib.h”
int main()
{
int *A,*B,id,id1,*C,i,j,P1flag=0,P2flag=0,k;    int id2,id3;
    A=(int *) shared(sizeof(int),&id1);
    B=(int *) shared(sizeof(int),&id2);
    C=(int *) shared(sizeof(int),&id3);
    int x;    x=2;
    printf(“Enter First matrix A:n”);
    for(j=0;j<4;j++)
    {
        scanf(“%d”,&A[j]);
    }
    printf(“Enter Second matrix B:n”);
    for(i=0;i<4;i++)
    {
        scanf(“%d”,&B[i]);
    }
    printf(“nmatrix An”);
    for(j=0;j<4;j++)
    {
        printf(“t%d”,A[j]);
        if(j==1)
            printf(“n”);
    }
    printf(“nmatrix Bn”);
    for(j=0;j<4;j++)
    {
        printf(“t%d”,B[j]);
            if(j==1)
            printf(“n”);
       
    }
    printf(“n”);
    id=create_process(2);
    if(id==0)
    {
        C[0]=A[0]*B[0]+A[1]*B[2];
        P1flag=1;
    }
    if(id==1)
    {
        C[1]=A[0]*B[1]+A[1]*B[3];
        P2flag=1;
    }
    if(P1flag==1)
    {
        id=0;
        C[2]=A[2]*B[0]+A[3]*B[2];
        P1flag=2;
    }else{
        id=1;
        C[2]=A[2]*B[0]+A[3]*B[2];
        P2flag=2;
    }
    if(P1flag==2)
    {
        id=0;
        C[3]=A[2]*B[1]+A[3]*B[3];
    }else{
        id=1;
        C[3]=A[2]*B[1]+A[3]*B[3];
    }
    join_process(2,id);
    printf(“nnMatrix after Multiplication(self Scheduling):n”);
    for(k=0;k<2;k++)
    {
        printf(“t%d”,C[k]);
    }
    printf(“n”);
    for(k=2;k<4;k++)
    {
        printf(“t%d”,C[k]);
    }
    printf(“n”);
    return 0;    
}

Output:

Comment bellow for your Query and Feedback

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!