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

```# 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 Array
int *temp;    // Holding Intermediate Result
int *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);
}
```