Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用MPI-C语言编写程序_C_Parallel Processing_Mpi_Dining Philosopher - Fatal编程技术网

用MPI-C语言编写程序

用MPI-C语言编写程序,c,parallel-processing,mpi,dining-philosopher,C,Parallel Processing,Mpi,Dining Philosopher,我的MPI C程序有问题。代码如下: void wantEat(int p, int rank, char *state, char* stateLeft, char* stateRight){ char *s; MPI_Status status ; /* if left or right neighbor is eating */ if(compare(stateLeft, "eat") || compare (stateRight, "eat")){

我的MPI C程序有问题。代码如下:

void wantEat(int p, int rank, char *state, char* stateLeft, char* stateRight){

    char *s;

    MPI_Status status ;

    /* if left or right neighbor is eating */

    if(compare(stateLeft, "eat") || compare (stateRight, "eat")){
        state = "want_Eat";
        printf("%s : I wait for eating\n", nomPhilosophe(rank));

        /* the process have to send his new state to his neighbors */

        MPI_Send(state, strlen(state)+1, MPI_CHAR,
                (rank - 1 + p) % p, 0, MPI_COMM_WORLD);
        MPI_Send(state, strlen(state)+1, MPI_CHAR,
                (rank + 1) % p, 0, MPI_COMM_WORLD);

        /* if only left neighbor is eating */

        if(compare(stateLeft,"eat") &&  !compare(stateRight,"eat")){

            /* Wait for left neighbor finishes eating */

            MPI_Recv(stateLeft, 6, MPI_CHAR, (rank - 1 + p) % p, 0,
                    MPI_COMM_WORLD, &status);
            /* and eat */
            state = "eat";
        }
        /* if only right neighbor is eating  */

        if(compare(stateRight,"eat") &&  !compare(stateLeft,"eat")){

            /* wait for right neighbor message */
            MPI_Recv(stateRight, 6, MPI_CHAR, (rank + 1) % p, 0,
                    MPI_COMM_WORLD, &status);
            /* and eat */
            state = "eat";
        }
        /* if both neighboors are eating */

        if(compare(stateRight,"eat") &&  compare(stateLeft,"eat")){

            /* wait for messages of the 2 neighbors */

            MPI_Recv(stateLeft, strlen("think")+1, MPI_CHAR, MPI_ANY_SOURCE, 0,
                    MPI_COMM_WORLD, &status);
            MPI_Recv(stateRight, strlen("think")+1, MPI_CHAR, MPI_ANY_SOURCE, 0,
                    MPI_COMM_WORLD, &status);

            /* and eat */
            state = "eat";
        }
    }

    /* if neighbors are not eating */

    else{
        /* eat */ 
        state= "eat";
    }

    /* send the new state to neighbors */

    MPI_Send(state, strlen(state)+1, MPI_CHAR,
            (rank - 1 + p) % p, 0, MPI_COMM_WORLD);
    MPI_Send(state, strlen(state)+1, MPI_CHAR,
            (rank + 1) % p, 0, MPI_COMM_WORLD);
}

int main(int argc, char* argv[]){
    int  my_rank; /* rank of process */
    int  p;       /* number of processes */

    char* state = "think";   /* state of process (think, eat, or want_eat) */
    char* stateLeft = "think";  /* state of the left neighbor of the      process  */
    char* etatD = "think";  /* state of the right neighbor of the process */

    /* start up MPI */

    MPI_Init(&argc, &argv);

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

    /* find out number of processes */
    MPI_Comm_size(MPI_COMM_WORLD, &p); 

    think(my_rank); /* process "my_rank" is thinking */
    wantEat(p, my_rank, state, stateLeft, stateRight); /* process "my_rank" wants to eat */
    eat(my_rank); /* process "my_rank" is eating */ 



    /* shut down MPI */
    MPI_Finalize(); 


    return 0;
}

问题是,所有进程都在不等待邻居的情况下同时进食。我认为新州的MPI发送不会被邻居接收到。你有办法解决这个问题吗?

你的程序启动了,每个人都处于状态
“思考”
。这意味着在所有进程中都会跳过if语句,因为没有人在吃东西,每个人都执行else语句并将其状态设置为“eat”。然后,他们每次调用
MPI\u Send
,等待邻居调用
MPI\u Recv
,但他们不能,因为他们也被困在
MPI\u Send
上,所以我想你的程序会挂起吗?我是不是在你没有展示的功能中遗漏了一些特别的东西?为什么您希望行为与此不同?在我的“思考”函数中,每个进程都会等待一段随机时间,然后才会想吃东西,因此进程不希望在完全相同的时间吃东西。我想我应该为我的每个MPI_发送添加一个MPI_recv,但我不知道在哪里。事实上,每个进程在
recv
it之前都不知道关于其邻居状态的任何其他信息。最好在
wantEat
函数的开头执行
MPI\u Sendrecv
,以便每个进程都知道其邻居的最新信息,然后相应地更改其状态
Sendrecv
还可以帮助解决您可能遇到的悬而未决的问题,因为您不必担心在不同进程上发送和接收的顺序。