-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BB-5: Pointers to allocators/deallocators #3
Comments
I don't know the best way to do this, but I agree in principle that a user should be able to specific the allocator used for any objects created by the graphBLAS. This may not be practical (I haven't thought through the implications) but it is something we may need. |
This is essential. I propose some method like (pick a name):
RedisGraph requires it: Julia requires it: MATLAB requires it: I cannot use malloc/calloc/realloc/free. mexFunctions must use mxMalloc/mxCalloc/mxRealloc/mxFree: When used inside MATLAB, all libraries must use internal functions of the MathWorks: utMalloc/utCalloc/utRealloc/utFree, but this is not documented. I must take in function pointers to these 4 functions. I handle this in SuiteSparse as a result. Python requires it too (but I couldn't find the source code). On the GPU, I will require rmm_malloc/rmm_calloc/rmm_realloc/rmm_free, which are ANSI C11 compatible wrappers for functions that rely on the Rapids Memory Manager. TBB has its own too: scalable_malloc/scallable_calloc/scalable_realloc/scalable_free: I know of no major user of GraphBLAS that doesn't require the use of their own memory manager. Defining the memory manager must be done at GrB_init time, since that function will likely need to malloc some memory. Currently, my GrB_init does not have to malloc anything, but it did in the past, to support the no-input GrB_wait() function and to create thread-local space for the no-input GrB_error(). Those are gone now, fortunately! But in general, it seems likely that GrB_init would like to malloc something, which then gets freed by GrB_finalize. |
We call Note that
@DrTimothyAldenDavis, your current: I would also like to point out that Python allows you to use user-defined allocators, and NumPy is about to: https://numpy.org/neps/nep-0049.html There is a lot of discussion around these proposals and the potential benefits of accepting user-defined allocators. Notably, I want to point out that both NumPy and Python user-defined allocator functions accept a context argument. This flexibility is sometimes needed, and it future-proofs the API. This context can serve the same purpose as the "thread safe or not" argument in This is NumPy's proposal in a nutshell: /* The declaration of free differs from PyMemAllocatorEx */
typedef struct {
void *ctx;
void* (*malloc) (void *ctx, size_t size);
void* (*calloc) (void *ctx, size_t nelem, size_t elsize);
void* (*realloc) (void *ctx, void *ptr, size_t new_size);
void (*free) (void *ctx, void *ptr, size_t size);
} PyDataMemAllocator;
typedef struct {
char name[128]; /* multiple of 64 to keep the struct aligned */
PyDataMemAllocator allocator;
} PyDataMem_Handler;
const PyDataMem_Handler *PyDataMem_SetHandler(PyDataMem_Handler *handler) |
MATLAB’s managers are not threadsafe but that is a special case. I can write wrappers for the that call mxMalloc etc inside a critical section. That way I do not expose this ugliness to the world. So I’m suggesting a GrB init with the mode, and the 4 function pointers, and nothing else. I actually only need malloc and free. I never calloc. And since internally I keep track of the sizes of all my malloced objects I can do my own realloc. I use the realloc function if provided but do my own if the function pointer is NULL. But other implementations might want all 4 functions. |
Regarding the python context pointer: I don't think that will work to add the context pointer to the malloc/calloc/etc signatures. It's too disruptive. All the other methods can provide the 4 functions with the same signature as the ANSI C11 malloc/calloc/etc functions. Couldn't Python place the ctx in a global pointer somewhere and then provide a function like this?
|
This is required by #31 |
|
Ignore the thread-safe thing. That can be fixed outside of GraphBLAS. The
allocator functions should be threadsafe. This option no longer appears.
This is something where the user application needs to be in control. The
GraphBLAS library can’t make this decision. GraphBLAS is at the bottom of
the food chain, so to speak. It must coexist with many packages above it.
Those other packages have to all agree on what allocators to use, if we
want O(1) time and space sharing of data between them.
Many examples:
MATLAB requires that I use their internal allocators, utMalloc, utFree,
etc. These are like mxMalloc and friends but at a lower level. If GraphBLAS
couldn’t be forced to use utMalloc and friends, then it couldn’t be used
inside MATLAB. Non starter. Say if instead GraphBLAS used only the ANSI C
malloc and friends. Then the only way MathWorks could use GraphBLAS inside
MATLAB would be to patch GraphBLAS somehow.
Using the GPU and unified shared memory requires that I use malloc-syntax
wrappers for the Rapids Memory Manager. I can’t use the GPU otherwise. But
if I decide to use these internally then the user application must either
use them too, or be willing to give up O(1) time and space sharing.
Same is true for RedisGraph, and I think Python too.
…--
Sent from Gmail Mobile
|
GrB_init
should be extended to pass in pointers tomalloc
,calloc
,realloc
, andfree
. Required for matrix import/export. See SuiteSparse:GraphBLASGxB_init()
.The text was updated successfully, but these errors were encountered: