Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.

MKL Memory Management

Samee__Sameeul
Beginner
686 Views

I am trying to use a custom memory manager for my project. I looked at the example code given in "i_malloc.h" but this seems to not working for me. 

Here is a snippet of what I have. I am only replacing malloc here as example, but I am replacing all four in my actual code.  

 #include <i_malloc.h>


   void * my_malloc(size_t size){
     printf("Testing my_malloc\n");
     return malloc(size);
   }

   int main( int argc, int argv )
   {
       // override normal pointers
       i_malloc = my_malloc;

       // Some MKL calls that involves malloc inside the library calls
   }

 

It looks like the code is not using my_malloc for allocation. Any idea what I am missing?

 

Thanks,

Samee

0 Kudos
4 Replies
Samee__Sameeul
Beginner
686 Views

Some update: My code is more like this:


 

   #include <i_malloc.h>

   void * my_malloc(size_t size){
     printf("Testing my_malloc\n");
     return malloc(size);
   }

   int main( int argc, int argv )
   {

      //Some code that calls MKL library (which might be using malloc)

        ....

.       ...
       // override normal pointers []
       i_malloc = my_malloc;

       // Some MKL calls that involves malloc inside the library calls
   }

 

0 Kudos
Gennady_F_Intel
Moderator
686 Views

how did you decided that this doesn't work for you? 

0 Kudos
Samee__Sameeul
Beginner
686 Views

If it was going through the my_malloc function, it would also execute the printf function inside my_malloc. Since I did not see any output, I am assuming, my_malloc is not getting called for memory allocation. Also, what I found is that, before calling any MKL function, if I redefine i_malloc with my_malloc, it works. But, if I call any of the MKL functions and then at some stage down the road, I redefine i_malloc with my_malloc, then, MKL memory allocation does not use my_malloc. 

Here is an example:

#include<stdio.h>
#include "mkl.h"
#include "mkl_service.h"
#include "i_malloc.h"
 
void * my_malloc(size_t size){
  printf("Inside my_malloc\n");
  return malloc(size);
}
 
 
void main(void){

  double *a, *b, *c;
  double alpha, beta;
  int n, i;

  alpha = 1.1; 
  beta = -1.2;
  n = 1000;

  i_malloc = my_malloc;

  a = (double*)mkl_malloc(n*n*sizeof(double),64);
  b = (double*)mkl_malloc(n*n*sizeof(double),64);
  c = (double*)mkl_calloc(n*n,sizeof(double),64);
 
 
  for (i=0;i<(n*n);i++) {
     a = (double)(i+1);
     b = (double)(-i-1);
  }
  
  dgemm("N","N",&n,&n,&n,&alpha,a,&n,b,&n,&beta,c,&n);

  mkl_free_buffers();
  mkl_free(a);
  mkl_free(b);
  mkl_free(c);
}

In this case, my_malloc is getting used for memory allocation.

However, if I move i_malloc = my_malloc after the first mkl_malloc (or any other MKL function) [line 24], my_malloc no longer gets used for memory allocation. 

 

Also, I found that mkl_disable_fast_mm() returns 0 [Fail], if I try to call it after calling any MKL function. Can you help to understand what I am missing? How can I redefine i_malloc with my_malloc somewhere in the middle of the code after I have already done some computation using MKL.

 

Thanks,

Samee

0 Kudos
Spencer_P_Intel
Employee
686 Views

Hi Samee,

These observations you have made are actually all expected behavior.  Once you have used a certain memory allocation function, it can no longer be changed.  The reason is the following:  each implementation of the malloc/free pair ( and others like realloc, etc) may have a different way of encoding what was allocated and where.  Typically there is some sort of header data stored in the memory block before the start of the actual data block where all sorts of allocation details are stored, but the specifics of what and how are not standardized. If I allocate something with a specific malloc(), then I need to make sure to deallocate it with the corresponding free().  Otherwise I could get all sorts of crashes/errors. Thus, once a specific implementation has been used, it is not able to (should not) be changed.

Your observation of i_malloc being used or not used when moving around in the code is exactly demonstrating this behavior.  When it is moved deeper into the code, it appears that you are trying to change the malloc that is being used internally after it has already been used previously and so it will not be changed.  However if you set i_malloc before calls to mkl, then it will be picked up and used everywhere as expected. 

It turns out that choosing to disable fast mm is also changing the internal choice of malloc/free, so it also is not allowed to change once one has been used.

I hope this answers your question and concerns.  Please let us know if we can help further.

Best,

Spencer

0 Kudos
Reply