I've put together an initial implementation: https://bitbucket.org/petsc/petsc/branch/dalcinl/malloc-coalesced
On 16 April 2015 at 11:49, Lisandro Dalcin <[email protected]> wrote: > In on-going discussion about malloc(0), I made a mention about this > approach. As this idea is somehow independent of the malloc(0) stuff, > I'm starting a new thread. > > Folks, in the attached file you can see how easy is to support > coalesced mallocs at runtime. This would enable us to disable the > coalesced mallocs for optimized builds and run the code under valgrind > to catch invalid memory accesses. > > The code has memory leaks on purpose to get output from -malloc_dump, > you can try it with debug/opt builds, passing -malloc_coalesced [0|1] > option and value. If you pass "-n size1,size2" you can control the > sizes for the PetscMalloc2 calls. > > If you use a optimized build, and any of the sizes is 0, and you pass > -malloc_coalesced 0, valgrind will be able to catch the invalid > writes. > > $ PETSC_ARCH=arch-linux2-c-opt make TestMalloc > > $ valgrind -q ./TestMalloc -n 0,1 -malloc_coalesced 1 2>&1 | grep > "Invalid write" > <no-ouput> > > $ valgrind -q ./TestMalloc -n 0,1 -malloc_coalesced 0 2>&1 | grep > "Invalid write" > ==2304== Invalid write of size 1 > ==2304== Invalid write of size 4 > ==2304== Invalid write of size 8 > > $ valgrind -q ./TestMalloc -n 0,0 -malloc_coalesced 0 2>&1 | grep > "Invalid write" > ==2306== Invalid write of size 1 > ==2306== Invalid write of size 1 > ==2306== Invalid write of size 4 > ==2306== Invalid write of size 4 > ==2306== Invalid write of size 8 > ==2306== Invalid write of size 8 > > > IMHO, we should definitely implement this approach. If you agree, let > discuss about when to enable/disable the coalesced mallocs (e.g., if > users pass -malloc or -malloc_dump, perhaps we should disable > coalescing, and only enable it back if -malloc_coalesced 1 is > explicitly asked for). > > > -- > Lisandro Dalcin > ============ > Research Scientist > Computer, Electrical and Mathematical Sciences & Engineering (CEMSE) > Numerical Porous Media Center (NumPor) > King Abdullah University of Science and Technology (KAUST) > http://numpor.kaust.edu.sa/ > > 4700 King Abdullah University of Science and Technology > al-Khawarizmi Bldg (Bldg 1), Office # 4332 > Thuwal 23955-6900, Kingdom of Saudi Arabia > http://www.kaust.edu.sa > > Office Phone: +966 12 808-0459 -- Lisandro Dalcin ============ Research Scientist Computer, Electrical and Mathematical Sciences & Engineering (CEMSE) Numerical Porous Media Center (NumPor) King Abdullah University of Science and Technology (KAUST) http://numpor.kaust.edu.sa/ 4700 King Abdullah University of Science and Technology al-Khawarizmi Bldg (Bldg 1), Office # 4332 Thuwal 23955-6900, Kingdom of Saudi Arabia http://www.kaust.edu.sa Office Phone: +966 12 808-0459
