I am writing a program that uses Intel's MKL to do some matrix multiplications. I have a frustrating requirement that only a custom version of dynamic memory allocation is utilized. I'm aware this is usually considered a terrible idea, but I am using the linker's --wrap
functionality to wrap malloc
and free
with my own custom implementation. In general, this has gone well so far.
However, it seems that some of the MKL code is performing dynamic allocation, and it is not invoking my custom malloc. I understand that MKL has also replaced the system malloc with its own custom malloc, but in my program, I am calling mkl_disable_fast_mm()
which, to my understanding, should turn off the use of the MKL-custom malloc and revert to the system malloc. Now, since I've --wrap
ped the system malloc with my custom malloc, I was expecting to see my custom malloc called when MKL does its dynamic allocation.
When I run my program normally (as described above), I can see my custom malloc getting called everywhere that malloc is used except for the calls from inside MKL.
To add another level of complication, if I run the program with valgrind, then I do see my custom malloc invoked everywhere, including from within MKL. I realize that valgrind is ALSO replacing malloc with its own custom malloc, so there's several levels of malloc-replacement going on in this case.
My question, then, is: how can I get MKL to call my custom malloc when it does dynamic allocation. It seems that it must be possible, since it seems that using valgrind makes it happen, but I haven't been able to track down a way without using valgrind.
(As of right now, this is all inside a much bigger program, so I don't have a small runnable piece of code to demonstrate. If necessary, I can try to extract out the related parts and generate a post-able example.)