MPI_发送到运行在同一进程上的多个POSIX线程

MPI_Send to multiple POSIX threads running on the same process

本文关键字:进程 线程 POSIX 运行 MPI      更新时间:2023-10-16

我在进程#0上启动n个POSIX线程,以侦听来自进程#0…#n的传入调用。然而,我的代码不起作用,而是得到了一个segfault。我认为问题可能是因为缓冲区重叠。我是C++新手。你能提出一个解决方案吗?

void *listener(void *arg) {
    ...
    int status_switch;    
    while (true) {                
        MPI_Recv(&status_switch, 1, MPI_INT, fd->id, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);        
        ...
    }
}
int main(int argc, char * argv[])
{
    MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);    
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);            
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
    if (world_rank == root)
    {
        int nthreads = world_size;
        threads=(pthread_t *)malloc(nthreads*sizeof(threads));
        fd=(struct_t *)malloc(sizeof(struct_t)*nthreads);
        //Start listeners for each node on node0
        for (int i = 0; i < world_size; i++) {                        
            fd[i].id=i;  
            pthread_create(&threads[i], NULL, listener, (void *)(fd+i) );                    
        }                                       
    }    
    int status_switch;
    status_switch = 0;    
    MPI_Send(&status_switch, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
    ...
}

我不熟悉MPI,但您似乎在这里分配了错误的空间:

threads = (pthread_t *) malloc(nthreads*sizeof(threads));

你的代码很可能因此而出错。

如果threadspthread_t *,则需要为nthreads*sizeof(*threads)分配足够的空间来容纳pthread_tnthreads实例。

此外,您不应该强制转换malloc的结果。