2016-06-03 2 views
1

내 코드를 수신의 범위는이 숫자의 위치를 ​​그들 사이 c를 번호를 입력하고 찾을 다음, 숫자 a (낮은 범위) 및 b (상위 범위)에 입력하는 것입니다MPI 보내기로 교착 상태와

때 우분투 터미널에서이 코드를 실행, 내가 송신을 언급하고 선을 회복하고 문 및 작업

보내기 전에 위치를 인쇄하려고, 숫자 abc 입력 한 후 실행을 중지하지만 난의 위치를 ​​찾으려면 c 및 마스터 프로세스로 보내 인쇄하십시오.

참고 : 보내기 및 받기 기능을 차단하고 있기 때문에 교착 상태가 발생할 수 있습니다. 내가 확실하지 않다 그래서이 문제

#include <stdio.h> 
#include <stdlib.h> 
#include "mpi.h" 

int main(int argc , char * argv[]) 
{ 
int rank,nprocess,tag=0,a,b,c,i=0,*arr,*subArr,arrSize=0,blockSize=0,pos=0,dest; 
MPI_Status status; 
/* Start up MPI */ 
MPI_Init(&argc , &argv); 

/* Find out process rank */ 
MPI_Comm_rank(MPI_COMM_WORLD, &rank); 

/* Find out number of process */ 
MPI_Comm_size(MPI_COMM_WORLD, &nprocess); 

if(rank==0){ 
    printf("Enter the lower Range and the upper Range : "); 
    if(scanf("%d",&a)){}; //2 
    if (scanf("%d",&b)){}; //9 
    printf("Enter the search number : "); 
    if(scanf("%d",&c)){}; //5 

    arrSize=(b-a)+1; 
    while(arrSize%nprocess!=0)arrSize++; //Enhance the array Size 

    printf("array size is : %d \n",arrSize); 

    arr=(int *)malloc(arrSize*sizeof(int)); 

    for(i=0;i<arrSize;i++){ 
     if(i<((b-a)+1))arr[i]=a+i; 
     else { arr[i]=b;} 
    } 

    printf("Array is :"); 
    for(i=0;i<arrSize;i++){ 
     printf("%d ",arr[i]); //2 3 4 5 6 7 8 9 9 
    } 
    printf("\n"); 

    blockSize=arrSize/nprocess; //3 

    for(i=0;i<nprocess;i++){ 
     dest=i; 
     MPI_Recv (&pos,1,MPI_INT,dest,tag,MPI_COMM_WORLD,&status); 
    } 

    printf("The postion of the number %d in the range from %d to %d is : %d \n ",c,a,b,pos); 

} 

MPI_Bcast (&c,1,MPI_INT,0,MPI_COMM_WORLD); 
MPI_Bcast (&blockSize,1,MPI_INT,0,MPI_COMM_WORLD); 


subArr=(int *) malloc(blockSize*sizeof(int)); 


MPI_Scatter (arr,blockSize,MPI_INT,subArr,blockSize,MPI_INT,0,MPI_COMM_WORLD); 


for(i=0;i<blockSize;i++){ 
    if(subArr[i]==c){ 
     pos=(blockSize+1)*rank; 
     printf("The Postion is : %d ",pos); // postion is 4 since array is 2 3 4 5 6 7 8 9 9 and I search for 5 
     MPI_Send (&pos,1,MPI_INT,0,tag,MPI_COMM_WORLD); 
     break; 
    } 
} 

MPI_Finalize(); 
return 0; 

} 실제로

답변

0

에 어떤 도움을 주셔서 감사합니다,이 교착 상태입니다. 루트 프로세스는 MPI_Recv() 개의 메시지를 다른 프로세스에서 시도하지만 다른 프로세스는 MPI_Bcast(&c,...)에서 차단되어 프로세스 0이 무언가를 브로드 캐스트한다고 예상합니다.

MPI_Bcast()MPI_Scatter()을 호출 한 후 MPI_Recv()을 호출하면 어떨까요?

마지막으로, 공정 sourceMPI_Send(.,.,.,to,.,.)에 correponding 호출을 일치해야합니다 to 과정에 대한 MPI_Recv(.,.,.,source,.,.) 각 호출. 배열에서 찾고있는 번호가 하나만 있기 때문에 MPI_Send(...,0,.)에 대한 단일 호출이 있습니다. 따라서 프로세스 0은 MPI_Recv()을 한 번만 호출해야합니다. 모든 프로세스가 메시지를 보낼 수 있으므로 매크로 MPI_ANY_SOURCEsource의 자리에 사용해야합니다.

다음 코드는 수정되었습니다. mpicc main.c -o main -Wall에 의해 컴파일되고 mpirun -np 3 main에 의해 실행됩니다.

#include <stdio.h> 
#include <stdlib.h> 
#include "mpi.h" 

int main(int argc , char * argv[]) 
{ 
    int rank,nprocess,tag=0,a,b,c,i=0,*arr,*subArr,arrSize=0,blockSize=0,pos=0,deadlock=0; 
    MPI_Status status; 
    /* Start up MPI */ 
    MPI_Init(&argc , &argv); 

    /* Find out process rank */ 
    MPI_Comm_rank(MPI_COMM_WORLD, &rank); 

    /* Find out number of process */ 
    MPI_Comm_size(MPI_COMM_WORLD, &nprocess); 

    if(rank==0){ 
     printf("Enter the lower Range and the upper Range : ");fflush(stdout); 
     if(scanf("%d",&a)){}; //2 
     if (scanf("%d",&b)){}; //9 
     printf("Enter the search number : ");fflush(stdout); 
     if(scanf("%d",&c)){}; //5 

     arrSize=(b-a)+1; 
     while(arrSize%nprocess!=0)arrSize++; //Enhance the array Size 

     printf("array size is : %d \n",arrSize); 

     arr=(int *)malloc(arrSize*sizeof(int)); 

     for(i=0;i<arrSize;i++){ 
      if(i<((b-a)+1))arr[i]=a+i; 
      else { arr[i]=b;} 
     } 

     printf("Array is :"); 
     for(i=0;i<arrSize;i++){ 
      printf("%d ",arr[i]); //2 3 4 5 6 7 8 9 9 
     } 
     printf("\n"); 

     blockSize=arrSize/nprocess; //3 



    } 

    MPI_Bcast (&c,1,MPI_INT,0,MPI_COMM_WORLD); 
    MPI_Bcast (&blockSize,1,MPI_INT,0,MPI_COMM_WORLD); 


    subArr=(int *) malloc(blockSize*sizeof(int)); 


    MPI_Scatter (arr,blockSize,MPI_INT,subArr,blockSize,MPI_INT,0,MPI_COMM_WORLD); 



    for(i=0;i<blockSize;i++){ 

     if(subArr[i]==c){ 
      pos=(blockSize)*rank+i; 
      printf("The Postion is : %d ",pos); // postion is 4 since array is 2 3 4 5 6 7 8 9 9 and I search for 5 
     if(rank==0){ 
      deadlock=1; 
      break; 
     } 
      MPI_Send (&pos,1,MPI_INT,0,tag,MPI_COMM_WORLD); 
      break; 
     } 
    } 

    if(rank==0){ 

     //for(i=0;i<nprocess;i++){ 
     //  dest=i; 
     if(deadlock==0) 
     MPI_Recv (&pos,1,MPI_INT,MPI_ANY_SOURCE,tag,MPI_COMM_WORLD,&status); 
     // } 

     printf("The postion of the number %d in the range from %d to %d is : %d \n ",c,a,b,pos); 
     free(arr); 
    } 
    free(subArr); 
    MPI_Finalize(); 
    return 0; 
} 
+0

도움 주셔서 대단히 감사합니다. 답변을 가장 잘 확인합니다. 친절하게 제 편집을 수락합니다. – Islams