在写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_mem
:
https://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()
相比,使用 MPI_Alloc_mem()
返回的内存时,RMA 可能 更快。此外,MPI_Alloc_mem()
采用一个 MPI_Info
参数,您可以使用它来优化内存位置(请注意,此参数的值是特定于实现的,并且不在标准范围内,MPI_INFO_NULL
将始终有效。
此外,某些 MPI 实现可能会选择让 MPI_Alloc_mem()
返回与缓存行对齐的内存,因此可能会带来更好的性能。
长话短说,如果内存将用于 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.
关于c - MPI malloc vs MPI_Alloc_mem 何时使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55232526/