c - 为什么要使用 _mm_malloc? (相对于 _aligned_malloc、alligned_alloc 或 posix_memalign)

标签 c memory-management intel dynamic-memory-allocation

获取对齐的内存块有几个选项,但它们非常相似,问题主要归结为您的目标语言标准和平台。

C11

void * aligned_alloc (size_t alignment, size_t size)

POSIX

int posix_memalign (void **memptr, size_t alignment, size_t size)

window

void * _aligned_malloc(size_t size, size_t alignment);

当然,手动对齐也是一种选择。

英特尔提供了另一种选择。

英特尔

void* _mm_malloc (int size, int align)
void _mm_free (void *p)

根据 Intel 发布的源代码,这似乎是他们的工程师更喜欢的分配对齐内存的方法,但我找不到任何文档将其与其他方法进行比较。我找到的最接近的只是承认存在其他对齐的内存分配例程。

https://software.intel.com/en-us/articles/memory-management-for-optimal-performance-on-intel-xeon-phi-coprocessor-alignment-and

To dynamically allocate a piece of aligned memory, use posix_memalign, which is supported by GCC as well as the Intel Compiler. The benefit of using it is that you don’t have to change the memory disposal API. You can use free() as you always do. But pay attention to the parameter profile:

  int posix_memalign (void **memptr, size_t align, size_t size);

The Intel Compiler also provides another set of memory allocation APIs. C/C++ programmers can use _mm_malloc and _mm_free to allocate and free aligned blocks of memory. For example, the following statement requests a 64-byte aligned memory block for 8 floating point elements.

  farray = (float *)__mm_malloc(8*sizeof(float), 64);

Memory that is allocated using _mm_malloc must be freed using _mm_free. Calling free on memory allocated with _mm_malloc or calling _mm_free on memory allocated with malloc will result in unpredictable behavior.

从用户的角度来看,明显的区别是 _mm_malloc 需要直接的 CPU 和编译器支持,并且使用 _mm_malloc 分配的内存必须使用 _mm_free 释放.鉴于这些缺点,为什么一直使用 _mm_malloc? 它能有一点性能优势吗?历史事故?

最佳答案

英特尔编译器支持 POSIX (Linux) 和非 POSIX (Windows) 操作系统,因此不能依赖 POSIX 或 Windows 函数。因此,选择了特定于编译器但与操作系统无关的解决方案。

C11 是一个很好的解决方案,但 Microsoft 甚至还不支持 C99,所以谁知道他们是否会支持 C11。

更新:与 C11/POSIX/Windows 分配函数不同,ICC 内部函数包含一个释放函数。这允许此 API 使用与默认堆管理器不同的单独堆管理器。我不知道它是否/何时真的这样做了,但它对支持这个模型很有用。

免责声明:我在英特尔工作,但对这些决策并不了解,这些决策早在我加入公司之前就已经发生了。

关于c - 为什么要使用 _mm_malloc? (相对于 _aligned_malloc、alligned_alloc 或 posix_memalign),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32612881/

相关文章:

c++ - scanf 返回条件

计算每个唯一数字的出现次数 : algorithm almost works

PHP 包含和内存

caching - 为什么现代处理器中的集合缓存关联性是 8 路集合关联性?

opencl - 我如何对英特尔 GPU 进行编程

x86-64 - 在 Intel x86-64 架构上是否以 little endian 4 字节字获取机器代码指令?

c - 如何从 mongodb C 驱动程序结果中过滤掉 "_id"

c - 腻子 Shift 箭头

C:函数参数输入看似随机变化

c - 获取 C 程序使用的峰值内存量