Question

I am writing an image registration program and I find that a strange error when using mkl_free(). I wrote an implementation of the matrix exponential function - expm() - from MATLAB using MKL functions. I have tested it individually by itself and it works without running into any errors.

The code can be found here: https://github.com/poliu2s/MKL/blob/master/matrix_exponential.cpp

The problem occurs when I call it from other functions who need it for calculation. My program currently crashes right before I call the first mkl_free() so I suspect it is some form of memory leak. But shouldn't matrices used within the function be self contained? I do not see how freeing them would cause my entire program to crash.

Calls to mkl_free_buffers(); and mkl_thread_free_buffers(); produce no effect that I can observe. I have tried turning off the Intel Memory Manager with mkl_disable_fast_mm() at the cost of speed at the beginning of the program's execution but also to no avail.

I have checked the inputs going into the function itself and the matrix is perfectly valid - just like the ones I used during testing.

Is there any reason why does my program would crash when I call mkl_free(array) in certain loops?

Was it helpful?

Solution

There are a couple of issues with your code. Also not necessarily but potentially leading to segfault but you should fix them while investigating your issue.

  • dcopy signature is:

    void cblas_dcopy (const MKL_INT N, const double *X, const MKL_INT incX, double *Y, const MKL_INT incY);

Assuming you use mkls function, passing the adress &one, &sxtn to dcopy is wrong. You can end up writing anywhere of your process memory.

Two more comments about style:

  • I would recommend to use same type for input and output variables of factorial e.g. unsigned long or unsigned double but not a mixed signature for a recursive function.
  • As a user of this function, I would expect that I am responsible for allocating memory for m_exp. So I would but this into the function signature. Compare your function call to mkl's, all memory the user should take care of has to be taken care of by him, and all internal memory allocations are done internally without exposing it to the user, but in your case internal memory allocation for m_exp is exposed to the user. This potentially leads to a memory leak in the user code.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top