gpt4 book ai didi

c++ - 英特尔 MKL 内存管理和异常

转载 作者:太空狗 更新时间:2023-10-29 21:30:37 24 4
gpt4 key购买 nike

我正在试用 Intel MKL,他们似乎有自己的内存管理(C 风格)。

他们建议对 vector 和矩阵使用他们的 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); }
};

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

最佳答案

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

关于c++ - 英特尔 MKL 内存管理和异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2422898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com