我的mpi程序随机运行

时间:2013-04-25 19:48:01

标签: multithreading mpi

我有一个随机行为的mpi程序,有时会正常完成而其他程序会挂起,我会在每个进程中创建一个额外的线程。我不知道会发生什么。 这是代码的摘要:

void* listen(void * args) {
    int id = ((__arg *) (args))->id;
    while (true) {
        MPI_Status status;
        MPI_Recv(&sender, 1, MPI_INT, MPI_ANY_SOURCE, 10000 + id, MPI_COMM_WORLD, &status);
        if (sender >= 90000) {
                    // ...
        } else {
            MPI_Recv(&node, 1, MPI_INT, sender, 30000 + id, MPI_COMM_WORLD, &status);
            int n = 3;//change later
            MPI_Send(&n, 1, MPI_INT, sender, 20000 + sender, MPI_COMM_WORLD);
        }
    }
    return NULL;
}

int main(int argc, char* argv[]) {
    MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
    int rank, size;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    pthread_t tid;
    __arg args;
    args.id = rank;
    args.np = size;
    pthread_create(&tid, NULL, &listen, (void *) (&args));

    for (int s_aux = 0; s_aux < local_vertex_count; ++s_aux) {
        int s = size * s_aux + rank;
        queue<int> Q;
        Q.push(s);
        while (!Q.empty()) {
            int v = Q.front();
            Q.pop();
            int own = v % size; 
            int idxv = v / size; 
            if (own == rank) {
                //...
            } 
            else {// send message to owner
                MPI_Send(&rank, 1, MPI_INT, own, 10000 + own, MPI_COMM_WORLD);
                MPI_Send(&v, 1, MPI_INT, own, 30000 + own, MPI_COMM_WORLD);
                int neighbors;
                MPI_Status status;
                MPI_Recv(&neighbors, 1, MPI_INT, own, 20000 + rank, MPI_COMM_WORLD, &status);
            }//send message to owner
        }// while (!Q.epmty())
    }

    //tell all processes to finish
    for (int i = 0; i < size; ++i) {
        int buf = 90000 + rank;
        MPI_Send((void*) &buf, 1, MPI_INT, i, 10000 + i, MPI_COMM_WORLD);
    }
    pthread_join(tid, NULL);
    MPI_Finalize();
    return 0;
}

0 个答案:

没有答案