MPI malloc vs MPI_Alloc_mem 什么时候使用?

MPI malloc vs MPI_Alloc_mem when to use?

在写MPI程序的时候,我一直使用malloc来分配动态内存,例如:

    int main(int argc, char* argv[]) {
        MPI_Init(&argc, &argv);
        int arr_size = 10;
        int* arr = malloc( sizeof(int) * arr_size );

        // do some MPI stuff

        MPI_Finalize();

    }

但是,我在这里遇到了这个 MPI 函数:MPI_Alloc_memhttps://www.mpich.org/static/docs/v3.2/www3/MPI_Alloc_mem.html 但我不确定那是为了什么。什么时候应该使用malloc,什么时候应该使用MPI_Alloc_mem? 读完后:http://mpi.deino.net/mpi_functions/MPI_Alloc_mem.html MPI_Alloc_mem 似乎主要用于远程内存访问。为什么不直接使用 malloc 呢?我找不到任何说明何时选择 MPI_Alloc_mem 而不是 malloc 的答案。

malloc() 相比,

RMA 在使用由 MPI_Alloc_mem() 编辑的内存 return 时可能 更快。此外,MPI_Alloc_mem() 采用一个 MPI_Info 参数,您可以使用它来优化内存位置(请注意,此参数的值是特定于实现的,并且不在标准范围内,MPI_INFO_NULL 将始终有效)。

此外,某些 MPI 实现可能会选择 MPI_Alloc_mem() return 内存在缓存行上对齐,因此可能会带来更好的性能。

长话短说,如果内存将用于 RMA 操作,使用 MPI_Alloc_mem() 也无妨。但这确实是一个优化问题,无论您如何分配内存,您的应用程序都应该可以正常工作(减去一些限制,见下文)

来自 MPI 3.1,第 8.2 章

In some systems, message-passing and remote-memory-access (RMA) operations run faster when accessing specially allocated memory (e.g., memory that is shared by the other processes in the communicating group on an SMP). MPI provides a mechanism for allocating and freeing such special memory. The use of such memory for message-passing or RMA is not mandatory, and this memory can be used without restrictions as any other dynamically allocated memory. However, implementations may restrict the use of some RMA functionality as dened in Section 11.5.3.