为什么要使用_mm_malloc? (相对于_aligned_malloc,alligned_alloc或posix_memalign) [英] Why use _mm_malloc? (as opposed to _aligned_malloc, alligned_alloc, or posix_memalign)

查看:4714
本文介绍了为什么要使用_mm_malloc? (相对于_aligned_malloc,alligned_alloc或posix_memalign)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有获取的内存块对齐的几个选择,但他们非常相似,主要问题归结为你的目标是什么语言的标准和平台。

There are a few options for acquiring an aligned block of memory but they're very similar and the issue mostly boils down to what language standard and platforms you're targeting.

C11

void * aligned_alloc (size_t alignment, size_t size)

POSIX

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

视窗

void * _aligned_malloc(size_t size, size_t alignment);

当然,它也总是用手对齐选项。

And of course it's also always an option to align by hand.

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

Intel offers another option.

英特尔

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

基于英特尔发布的源代码code

,这似乎是分配对齐的存储他们的工程师preFER的方法,但我不能找到它比较其他方法的任何文件。我发现只是最近承认,其他的对准内存分配例程存在。

Based on source code released by Intel, this seems to be the method of allocating aligned memory their engineers prefer but I can't find any documentation comparing it to other methods. The closest I found simply acknowledges that other aligned memory allocation routines exist.

<一个href=\"https://software.intel.com/en-us/articles/memory-management-for-optimal-performance-on-intel-xeon-phi-coprocessor-alignment-and\" rel=\"nofollow\">https://software.intel.com/en-us/articles/memory-management-for-optimal-performance-on-intel-xeon-phi-coprocessor-alignment-and

要动态地分配一块内存对齐的,使用posix_memalign,
  这是由GCC以及英特尔编译器支持。这样做的好处
  使用它,你不必改变内存处理API。
  您可以使用免费的()为你总是这样。但要注意的
  参数简介:

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(无效** memptr,为size_t对齐,为size_t大小);​​

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

英特尔编译器还提供了另一组的内存分配
  蜜蜂。 C / C ++程序员可以使用_mm_malloc和_mm_free分配
  和自由对准的内存块。例如,下面的
  声明要求一个64字节对齐的内存块8浮点
  元素。

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 =(浮动*)__ mm_malloc(8 * sizeof的(浮动),64);

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

这是使用_mm_malloc分配,必须使用被释放的内存
  _mm_free。呼吁免费内存_mm_malloc分配或调用由malloc分配的内存_mm_free将导致未predictable行为。

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 要求与 _mm_malloc 分配CPU直接和编译器的支持和内存必须以 _mm_free 被释放。鉴于这些缺点,什么是永远使用 _mm_malloc的原因?可它有轻微的性能优势?历史的偶然?

The clear differences from a user perspective is that _mm_malloc requires direct CPU and compiler support and memory allocated with _mm_malloc must be freed with _mm_free. Given these drawbacks, what is the reason for ever using _mm_malloc? Can it have a slight performance advantage? Historical accident?

推荐答案

这是很容易理解这种情况。英特尔编译器支持POSIX(Linux的)和非POSIX(视窗)操作系统,因此不能依靠POSIX和Windows函数。因此,选择了一个编译器具体,但OS无关的解决方案。

It's very easy to understand this situation. Intel compilers support POSIX (Linux) and non-POSIX (Windows) operating systems, hence cannot rely upon either the POSIX or the Windows function. Thus, a compiler-specific but OS-agnostic solution was chosen.

C11是一个很好的解决方案,但微软甚至不支持C99还,所以如果他们将永远支持C11谁知道。

C11 is a great solution but Microsoft doesn't even support C99 yet, so who knows if they will ever support C11.

免责声明:我为英特尔工作,但这些决定之前,我加入了该公司即发生长时间没有专业知识

这篇关于为什么要使用_mm_malloc? (相对于_aligned_malloc,alligned_alloc或posix_memalign)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆