views:

115

answers:

2

Hello everyone,

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.

+1  A: 

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); }
aaa
that is a very good idea. Thanks!
Andrew
+1  A: 

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.

Jason