I wrote a program which inputs matrix size and number of threads and then generated a random binary matrix of 0's and 1's. Then I need to find clusters of 1's and give each cluster a unique number.
I am getting the output correctly but I am having a problem parallelizing the function.
My professor asked me to break the matrix rows into "thread_cnt" parts. i.e.: thread size is 4 and matrix size is 8 then it breaks into 4 matrices having 2 rows each.
The code is as follows:
//Inputted Matrix size n and generated a binary matrix rand1[][]
//
begin = omp_get_wtime();
width = n/thread_cnt;
#pragma omp parallel num_threads(thread_cnt) for
for(d=0;d<n;d=d++)
{
b=d+width;
Mat(d,b);
d=(d-1)+width;
}
Mat(int w,int x)
{
//printf("\n Entered function\n");
for(i=w;i<x;i++)
{
for(j=0;j<n;j++)
{
//printf("\n Entered the loop also\n");
//printf("i = %d, j = %d\n",i,j);
if(rand1[i][j]==1)
{
rand1[i][j]=q;
adj(i,j,q);
q++;
}
}
}
}
adj(int p, int e, int m) //Function to find adjacent 1's
{
//printf("\n Entered adj function\n");
//printf("\n p = %d e = %d m = %d\n",p,e,m);
if (rand1[p][e+1] == 1)
{
//printf("Test1\n");
rand1[p][e+1]=m;
adj(p,e+1,m);
}
if (rand1[p+1][e] == 1)
{
rand1[p+1][e]=m;
//printf("Test2\n");
adj(p+1,e,m);
}
if (rand1[p][e-1] == 1 && e-1>=0)
{
rand1[p][e-1]=m;
//printf("Test3\n");
adj(p,e-1,m);
}
if (p-1>=0 && rand1[p-1][e] == 1)
{
rand1[p-1][e]=m;
//printf("Test4\n");
adj(p-1,e,m);
}
}
The code gives me correct output. But the time increases instead of decreasing when I increase the number of threads. For 1 thread I get 0.000076 and for 2 threads I get 0.000136.
It looks like its iterating instead of parallelizing. Can anyone help me out on this?
PS: I need to show both Serial time and parallel time and show that I have got a performance increase because of parallelization.