i am quite new to parallel processing. I have an example of calculating and storing elements of a 2D array in a sequential way. I wanted to convert this into a parallel program using MPI.
Following is the code for sequential program.
#include <stdio.h>
int x,y,xMax=10,yMax=10;
int main ()
{
int arr[yMax][xMax];
for(y =0; y<yMax;x++)
{
for (x=0;x<xMax;x++)
{
arr[y][x]=x+y;
printf("%d",arr[y][x]);
}
printf("\n");
}
}
I tried to convert this to an equivalent MPI program by parallelizing the outer for loop as follows: Here I want every process to calculate values for each array row and send the output array to root process which at the end will gather all the arrays and combine them into single 2D array.
#include <stdio.h>
#include <mpi.h>
int x,y,xMax=10,yMax=10,size,rank;
int main()
{
int arr[xMax];
int arrall[yMax][xMax];
MPI_Init(NULL,NULL);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
for(y =0+rank; y<yMax;y=y+size)
{
for (x=0;y<xMax;x++)
{
arr[y][x]=x+y;
}
MPI_Send(arr,xMax,MPI_INT,0,0,MPI_COMM_WORLD);
}
if(rank==0)
{
MPI_Gather(arr,xMax,MPI_INT,&arrall,xMax*yMax,MPI_INT,0,MPI_COMM_WORLD);
}
MPI_Finalize();
}
But when i compile the program and wait quite a while for it but it still does not give any result. and it does not show the errors. I am trying on this for quite some time and searched for the any solution but unable to find. Any help will be much appreciated. Thanks