英特尔 MKL 内存管理和异常

发布于 2024-08-24 16:30:22 字数 1199 浏览 1 评论 0原文

我正在尝试 Intel MKL,看起来他们有自己的内存管理(C 风格)。

他们建议对向量和矩阵使用 MKL_malloc/MKL_free 对,我不知道处理它的好方法是什么。原因之一是建议内存对齐至少为 16 字节,并且在这些例程中明确指定。

我曾经非常依赖 auto_ptr 和 boost::smart_ptr 来忘记内存清理。

我如何使用 MKL 内存管理编写异常安全程序,或者我应该只使用常规 auto_ptr 而不必打扰?

提前致谢。

编辑 http://software.intel.com/sites/ products/documentation/hpc/mkl/win/index.htm

这个链接可以解释为什么我提出这个问题

更新

我使用了下面分配器答案中的一个想法。这就是我现在所拥有的:

template <typename T, size_t TALIGN=16, size_t TBLOCK=4>
class aligned_allocator : public std::allocator<T>
{
public:
 pointer allocate(size_type n, const void *hint)
 {
  pointer p = NULL;
  size_t count = sizeof(T) * n;
  size_t count_left = count % TBLOCK;
  if( count_left != 0 ) count += TBLOCK - count_left;
  if ( !hint ) p = reinterpret_cast<pointer>(MKL_malloc (count,TALIGN));
  else   p = reinterpret_cast<pointer>(MKL_realloc((void*)hint,count,TALIGN));
  return p;
     } 
 void deallocate(pointer p, size_type n){ MKL_free(p); }
};

如果有人有任何建议,请随时改进。

I am trying out Intel MKL and it appears that they have their own memory management (C-style).

They suggest using their MKL_malloc/MKL_free pairs for vectors and matrices and I do not know what is a good way to handle it. One of the reasons for that is that memory-alignment is recommended to be at least 16-byte and with these routines it is specified explicitly.

I used to rely on auto_ptr and boost::smart_ptr a lot to forget about memory clean-ups.

How can I write an exception-safe program with MKL memory management or should I just use regular auto_ptr's and not bother?

Thanks in advance.

EDIT
http://software.intel.com/sites/products/documentation/hpc/mkl/win/index.htm

this link may explain why I brought up the question

UPDATE

I used an idea from the answer below for allocator. This is what I have now:

template <typename T, size_t TALIGN=16, size_t TBLOCK=4>
class aligned_allocator : public std::allocator<T>
{
public:
 pointer allocate(size_type n, const void *hint)
 {
  pointer p = NULL;
  size_t count = sizeof(T) * n;
  size_t count_left = count % TBLOCK;
  if( count_left != 0 ) count += TBLOCK - count_left;
  if ( !hint ) p = reinterpret_cast<pointer>(MKL_malloc (count,TALIGN));
  else   p = reinterpret_cast<pointer>(MKL_realloc((void*)hint,count,TALIGN));
  return p;
     } 
 void deallocate(pointer p, size_type n){ MKL_free(p); }
};

If anybody has any suggestions, feel free to make it better.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执手闯天涯 2024-08-31 16:30:22

您可以将 std::vector 与自定义分配器一起使用,例如提到的此处以确保 16 字节对齐。然后您可以将第一个元素的地址作为 MKL 函数的输入指针。 16 字节对齐非常重要,因为 MKL 广泛使用 SIMD 来提高性能。

You could use a std::vector with a custom allocator like the ones mentioned here to ensure 16 byte alignment. Then you can just take address of the first element as the input pointer to the MKL functions. It is important that you have 16 byte alignment since the MKL uses SIMD extensively for performance.

树深时见影 2024-08-31 16:30:22

使用 C++ new[] 运算符分配内存,但保留额外的 15 个字节用于对齐。
然后创建某种包装器,它返回/包含从第一个 16 字节边界开始的智能指针的内存地址。这会产生 16 字节对齐的内存。

template
T* address16(T *address) { return (T*)((char*)address + 15) & ~0xf); }

allocate memory using C++ new[] operator, but reserve extra 15 bytes for alignment.
Then create some kind of wrapper, which returns/contains memory address of your smart pointer starting at first 16 byte boundary. This produces 16 byte aligned memory.

template
T* address16(T *address) { return (T*)((char*)address + 15) & ~0xf); }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文