递归mpi生成(从spawn产生)

时间:2017-03-28 12:39:28

标签: c recursion openmpi

问题很简单:经理催生了X工人。如果满足条件,每个工人再生产2名工人。只要工人不再产生(所以条件没有达到,或者只有第一个“原始”工人产生;所有其他工人都挂起),一切正常。

经理代码:

int main(int argc, char *argv[]) {
int myrank, numworkers, tag = 3, sum = 0, K = 1;
MPI_Status status;
MPI_Comm workercomm;

numworkers = atoi(argv[1]);

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
MPI_Comm_spawn("Project_child",
        MPI_ARGV_NULL, numworkers, MPI_INFO_NULL, 0, MPI_COMM_SELF,
        &workercomm, MPI_ERRCODES_IGNORE);

for (int i = 0; i < numworkers; i++) { //sends 1,2,3... to workers
    K=i+1;
    MPI_Send(&K, 1, MPI_INT, i, tag, workercomm);
}

for (int i = 0; i < numworkers; i++) { //receives an int from worker
    MPI_Recv(&K, 1, MPI_INT, i, tag, workercomm, &status);
    sum += K;
}
printf("%d \n", sum);

MPI_Comm_free(&workercomm);
MPI_Finalize();
return 0;
}

工人代码:

int main(int argc, char *argv[]) {
int K, myrank, tag = 3;
MPI_Status status;
MPI_Comm parentcomm;

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
MPI_Comm_get_parent(&parentcomm);

MPI_Recv(&K, 1, MPI_INT, 0, tag, parentcomm, &status); //recv K
printf("child %d: k=%d\n", myrank, K);
K++;
if (K<5) { // && myrank==0) {
    MPI_Comm childParentComm;
    MPI_Comm_spawn("Project_child",
            MPI_ARGV_NULL, 2, MPI_INFO_NULL, 0, MPI_COMM_SELF,
            &childParentComm, MPI_ERRCODES_IGNORE);
    //problem might be here?^

    //sends the K to first worker_child
    MPI_Send(&K, 1, MPI_INT, 0, tag, childParentComm);

    //!!!!!!! IT HANGS HERE if worker != 0

    K++;
    //sends K+1 to second worker_child
    MPI_Send(&K, 1, MPI_INT, 1, tag, childParentComm);

    int K1, K2;
    MPI_Recv(&K1, 1, MPI_INT, 0, tag, childParentComm, &status);
    MPI_Recv(&K2, 1, MPI_INT, 1, tag, childParentComm, &status);
    K = K1 + K2;
    MPI_Comm_free(&childParentComm);
}
MPI_Send(&K, 1, MPI_INT, 0, tag, parentcomm);
MPI_Comm_free(&parentcomm);
MPI_Finalize();
return 0;
}

好的,我更改了代码以简化它。

测试代码,问题是一样的:worker0会产生额外的孩子(甚至他的孩子会产生额外的孩子),其他孩子则没有,他们会在第二次发送时挂起。因此,如果条件是(如果(K <5)&amp;&amp; myrank == 0),它将起作用,但这不是我需要的。

0 个答案:

没有答案