gpt4 book ai didi

c++ - 从不同线程调用 MPI_Init 是否安全?

转载 作者:行者123 更新时间:2023-11-30 01:39:05 25 4
gpt4 key购买 nike

std::thread 调用 MPI_Init 是否安全?

int main(int argc, char** argv) {

std::thread mpi_thread([...](){
MPI_Init(...);

// All MPI calls are done by this thread.

MPI_Finalize();
});

// The main thread is doing different stuff

mpi_thread.join();
return 0;
}

最佳答案

MPI standard有一节关于 MPI 和线程。一个重要的部分是:

Initialization and Completion The call to MPI_FINALIZE should occur on the same thread that initialized MPI. We call this thread the main thread. The call should occur only after all process threads have completed their MPI calls, and have no pending communications or I/O operations.

您的代码确实满足此要求。

MPI 知道多个级别的线程支持。您的代码需要:

MPI_THREAD_FUNNELED The process may be multi-threaded, but the application must ensure that only the main thread makes MPI calls.

为了更好的衡量,您应该调用以下而不是 MPI_Init:

int provided;
MPI_Init_thread(NULL, NULL, MPI_THREAD_FUNNELED, &provided);
if (provided < MPI_THREAD_FUNNELED)
MPI_Abort(MPI_COMM_WORLD, -1);

不兼容线程的 MPI 库必须返回 provided == MPI_THREAD_SINGLE,表明您将无法在您的代码中正确使用此实现。

在实践中,您应该熟悉常见的实现。有关不同级别的线程支持的更多信息,请参阅 MPI 标准中的 12.4.3。使用更高级别的线程支持,您的里程可能会因您选择的实现而异。

关于c++ - 从不同线程调用 MPI_Init 是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46508208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com