为什么MPI_Recv不起作用? [英] Why is'nt the MPI_Recv working ?

查看:187
本文介绍了为什么MPI_Recv不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我想问为什么最后一次MPI_Recv调用不起作用。一切都是正确的,但我无法弄清楚它有什么问题。只要程序到达此语句,程序就会停止并且没有错误。箭头显示MPI_recv调用





  #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获取进程数
size = lenght / sizee; // 流程中数组的划分
< span class =code-keyword> 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 ++;
}
}
如果(rank == 0
{
int i = 1 ;
while (i< size)
{
printf( 现在发送数组\ n);
MPI_Send(p,randNumber,MPI_INT,i, 23 ,MPI_COMM_WORLD);
printf( 发送的数组\ n);
i ++;
}
}
其他
{

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

}

parallelProgram(p,randNumber); // 此处调用并行编程
printf( 并行程序完成了它的工作\ n);
printf( rank - %d \ n,rank);
MPI_Barrier(MPI_COMM_WORLD);
if (rank == 0
{
printf( 现在1你要去看看该做什么\ n);
printf( size-%d \ n,size);
int i = 1 ;
while (i< size)
{
printf( \ n \ n检查\ 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 \ nn \ nArray收到\ n \ n \\ nn );
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){
< span class =code-comment> //
printf(%d是素数。\ n,i + 1);

// }
i ++;
}
}
else
{
printf( \ n \ n发回数组\ n \ n);
MPI_Send(p,randNumber,MPI_INT, 0 24 ,MPI_COMM_WORLD);
}
return 0 ;
}

解决方案

谢谢问题解决了。我不知道如何但是在我使用MPI_Barrier后它开始工作了通信器)

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


#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;
}

解决方案

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


这篇关于为什么MPI_Recv不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆