On Tue, Oct 11, 2022 at 04:15:25PM +0200, Jakub Jelinek wrote: > Well, it can use a weak symbol, if not linked against libgomp, the bit > that it is OpenMP shouldn't be set and so realloc/free will be used > and do > if (arrdescr.gomp_alloced_bit) > GOMP_free (arrdescr.data, 0); > else > free (arrdescr.data); > and similar. And I think we can just document that we do this only for > -fopenmp compiled code. > But do we have a place to store that bit? I presume in array descriptors > there could be some bit for it, but what to do about scalar allocatables, > or allocatable components etc.? > In theory we could use ugly stuff like if all the allocations would be > guaranteed to have at least 2 byte alignment use LSB bit of the pointer > to mark GOMP_alloc allocated memory for the scalar allocatables etc. but > then would need in -fopenmp compiled code to strip it away. > > As for pinned memory, if it is allocated through libgomp allocators, that > should just work if GOMP_free/GOMP_realloc is used, that is why we have > those extra data in front of the allocations where we store everything we > need. But those also make the OpenMP allocations incompatible with > malloc/free allocations.
Yet another option would be to change the way our OpenMP allocators work, instead of having allocation internal data before the allocated memory have them somewhere on the side and use some data structures mapping ranges of virtual memory to the allocation data. We'd either need to use mmap to have better control on where exactly we allocate stuff so that the on the side data structures wouldn't need to be for every allocation, or do those for every allocation perhaps with merging of adjacent allocations or something similar. Disadvantage is that it would be slower and might need more locking etc., advantage is that it could be then malloc/free compatible, any not tracked address would be forwarded from GOMP_free to free etc. And we'd not waste e.g. precious pinned etc. memory especially when doing allocations with very high alignment, where the data before allocation means we can waste up to max (32, alignment - 1) of extra memory. And gfortran inline emitted reallocation/deallocation could just emit GOMP_realloc/free always for -fopenmp. The way GOMP_ allocators are currently written, it is our internal choice if we do it the current way or the on the side way or some other way, but if we'd guarantee free compatibility we'd make it part of the ABI. CCing DJ and Carlos if they have thoughts about this. The OpenMP spec essentially requires that allocations through its allocator remember somewhere with which allocator (and its exact properties) each allocation has been done, so that it can be taken into account during reallocation or freeing. Jakub