<div dir="ltr"><div><div>Hi Barry,<br><br></div>I like the sound of this, but I think we'd need to be careful about not messing up data alignment if we do this. If we want a malloc that is going to let us put the start of an array on, say, a 64 byte alignment boundary, then we need to not mess that up by putting this integer value there. We could pad with an extra 64 bytes internally, though that may be getting too wasteful. I don't know how to get a malloc that gives us a starting address that is 64 bits *before* an alignment boundary (so that the memory the user sees from the malloc call indeed starts at the boundary), but maybe that's doable...<br><br></div><div>If the goal is to simply deal with allocations to high bandwidth memory on KNL, the memkind-provided free() will do the right thing with allocations in DRAM or MCDRAM. But, as you say, there are issues in other cases, like with -malloc_debug.<br></div><div><br></div>--Richard<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Mar 9, 2017 at 4:19 PM, Barry Smith <span dir="ltr"><<a href="mailto:bsmith@mcs.anl.gov" target="_blank">bsmith@mcs.anl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Using different mallocs for different objects/arrays in PETSc is very iffy because each free() has to match the malloc used for that memory. This is even true with just -malloc_debug in that certain initialization functions in PETSc need to use the raw malloc() because we cannot be sure if the (*PetscTrMalloc)() has been set yet and the raw free() that comes at PetscFinalize() time needs to be matched with it.<br>
<br>
Why not have PetscMalloc() ALWAYS allocate an extra 64 bit space at the beginning and put in an integer indicating the malloc family that has been used to get the space. PetscFree() would use this integer to determine the correct free() to use. A mechanism to register new malloc families could be easily done, for example<br>
<br>
PetscMallocRegister(malloc,<wbr>realloc,free,&basicmalloc);<br>
PetscMallocRegister(<wbr>PetscMallocDebug,<wbr>PetscReallocDebug,<wbr>PetscFreeDebug,&debugmalloc);<br>
PetscMallocRegister(<wbr>PetscMallocHBW,<wbr>PetscReallocHBW,PetscFreeHBW,&<wbr>hbwmalloc);<br>
<br>
To change the malloc used you would do PetscMallocPush(debugmalloc); PetscMalloc(....); .... PetscMallocPop(); Note that you can register additional malloc families at any time (it doesn't have to be as soon as the program starts up).<br>
<br>
What is wrong with the model and why shouldn't we use it?<br>
<br>
Barry<br>
<br>
Notes:<br>
<br>
It is easy to implement, so that is not a reason.<br>
<br>
The extra memory usage is trivial.<br>
<br>
The mapping from integer to malloc() or free() would be a bounds check and then accessing the function pointer from a little array so pretty cheap.<br>
<br>
if certain mallocs are missing (like PetscMallocHBW) the hbwmalloc variable could be set to the basicmalloc value (or some other) so one would not need to ifdef if if () code deciding which malloc to use in many places.<br>
<br>
It seems so simple something must be fundamentally flawed with it. Even with just PetscTrMallocDefault() and PetscMallocAlign() I feel like implementing it.<br>
<br>
<br>
<br>
<br>
</blockquote></div><br></div>