Click here to Skip to main content
15,888,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello I want to ask why isn't the last MPI_Recv call working. Everything is correct but I can't figure out what's wrong with it. Whenever the program reaches this statement the program stops with no error. The arrow displays that MPI_recv call


C++
#include<mpi.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void parallelProgram(int * p, int lenght)
{
	int i = 0;
	int rank, size, sizee;
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	MPI_Comm_size(MPI_COMM_WORLD, &sizee);//sizee gets the number of process
	size = lenght / sizee;// division of array among the process
	int lL = (rank * size) + 1;
	int rL = (rank * size) + size;
	
	while (lL <= rL)
	{
		int i = 1;
		while (i - 1 < lenght)
		{
			if (1)
			{
				if (i%lL == 0)
				{
					p[i - 1] += 1;
				}
			}
			i++;
		}
		lL++;
	}
}

int main(int argc, char * argv[])
{
	int size, rank;
	MPI_Init(&argc, argv);
	MPI_Comm_size(MPI_COMM_WORLD, &size);
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	int * p;
	srand(time(NULL));
	int randNumber = 9; //rand()%11;
	
	p = (int*)malloc(randNumber*sizeof(int));
	if (rank == 0)
	{
		int i = 0;
		while (i < randNumber)
		{
			p[i] = 0;
			i++;
		}
	}
	if (rank == 0)
	{
		int i = 1;
		while (i < size)
		{
			printf("Now sending the arrays \n");
			MPI_Send(p, randNumber, MPI_INT, i, 23, MPI_COMM_WORLD);
			printf("Arrays sent \n");
			i++;
		}
	}
	else
	{

		MPI_Status s;
		MPI_Recv(p, randNumber, MPI_INT, 0, 23, MPI_COMM_WORLD, &s);
		printf("Arrays Recieved \n");

	}

	parallelProgram(p, randNumber); // parallel prog being called here 
	printf("Parallel program did its job \n");
	printf("rank -%d\n" , rank);
	MPI_Barrier(MPI_COMM_WORLD);
	if (rank == 0)
	{
		printf("Now 1 is going yo see what to do \n");
		printf("size- %d \n" , size);
		int i = 1;
		while (i < size)
		{
			printf("\n\n CHECKING \n\n");
			int * arr = (int *)malloc(sizeof(int *) * randNumber);
			MPI_Status s;
			printf("Rand Number - %d" , randNumber);
	------>		MPI_Recv(arr, randNumber, MPI_INT, i, 24, MPI_COMM_WORLD, &s);
			printf("\n\n\nArray recieved \n\n\n");
			int j = 0;
			while (j < randNumber)
			{
				p[j] += arr[j];
				j++;
			}
			i++;
		}

		i = 0;
		printf("%d - %d" , i , randNumber);
		while (i < randNumber)
		{
			printf("%d ," , p[i]);
			//if (p[i] == 2){
			//	printf("%d is the prime number. \n", i + 1);
				
			//}
			i++;
		}
	}
	else
	{
		printf("\n\nSending back the arrays \n\n");
		MPI_Send(p, randNumber, MPI_INT, 0, 24, MPI_COMM_WORLD);
	}
	return 0;
}
Posted

1 solution

thanks the problem is solved.I don't know how but It started working after I used MPI_Barrier(Communicator)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900