【发布时间】:2010-03-11 05:58:46
【问题描述】:
我正在试用 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); }
};
如果有人有任何建议,请随时改进。
【问题讨论】:
-
16-byte 对齐,我想。
-
是的,绝对正确。已更正。
标签: c++ memory-management intel-mkl