From ab7591c2a29977fcb2e092bacd35ef7d896c7206 Mon Sep 17 00:00:00 2001 From: Andreas Noack Date: Sat, 4 Jul 2015 19:33:56 -0400 Subject: [PATCH] Define gc counted allocator functions with libc API and use them in CHOLMOD wrappers --- .travis.yml | 2 +- base/sparse/cholmod.jl | 11 ++++++ contrib/repackage_system_suitesparse4.make | 12 +++--- src/gc.c | 43 ++++++++++++++++++++++ 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index af371aa4c0b3c..65242ceaa028d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -55,7 +55,7 @@ script: - make $BUILDOPTS -C base version_git.jl.phony - make $BUILDOPTS NO_GIT=1 JULIA_SYSIMG_BUILD_FLAGS="--output-ji ../usr/lib/julia/sys.ji" prefix=/tmp/julia install | moreutils/ts -s "%.s" - if [ `uname` = "Darwin" ]; then - for name in spqr umfpack colamd cholmod amd suitesparse_wrapper; do + for name in suitesparseconfig spqr umfpack colamd cholmod amd suitesparse_wrapper; do install -pm755 usr/lib/lib${name}*.dylib* /tmp/julia/lib/julia/; done; fi diff --git a/base/sparse/cholmod.jl b/base/sparse/cholmod.jl index eaff5b67f7f37..9e4668120a89d 100644 --- a/base/sparse/cholmod.jl +++ b/base/sparse/cholmod.jl @@ -65,6 +65,7 @@ const version = VersionNumber(version_array...) function __init__() ### Check if the linked library is compatible with the Julia code if Libdl.dlsym(Libdl.dlopen("libcholmod"), :cholmod_version) == C_NULL + hasversion = false warn(""" CHOLMOD version incompatibility @@ -81,6 +82,7 @@ function __init__() versions of all dependencies. """) else + hasversion = true tmp = Array(Cint, 3) ccall((:cholmod_version, :libcholmod), Cint, (Ptr{Cint},), version_array) ccall((:jl_cholmod_version, :libsuitesparse_wrapper), Cint, (Ptr{Cint},), tmp) @@ -140,6 +142,15 @@ function __init__() start(commonStruct) # initializes CHOLMOD set_print_level(commonStruct, 0) # no printing from CHOLMOD by default + # Register gc tracked allocator if CHOLMOD is new enough + if hasversion && version >= v"3.0.0" + cnfg = cglobal((:SuiteSparse_config, :libsuitesparseconfig), Ptr{Void}) + unsafe_store!(cnfg, cglobal(:jl_malloc, Ptr{Void}), 1) + unsafe_store!(cnfg, cglobal(:jl_calloc, Ptr{Void}), 2) + unsafe_store!(cnfg, cglobal(:jl_realloc, Ptr{Void}), 3) + unsafe_store!(cnfg, cglobal(:jl_free, Ptr{Void}), 4) + end + end function set_print_level(cm::Array{UInt8}, lev::Integer) diff --git a/contrib/repackage_system_suitesparse4.make b/contrib/repackage_system_suitesparse4.make index 3a619d7935113..08f80f164dd84 100755 --- a/contrib/repackage_system_suitesparse4.make +++ b/contrib/repackage_system_suitesparse4.make @@ -25,14 +25,16 @@ default: mkdir -p $(JULIAHOME)/deps/SuiteSparse-SYSTEM/lib cd $(JULIAHOME)/deps/SuiteSparse-SYSTEM/lib && \ rm -f $(build_libdir)/lib{amd,cholmod,colamd,spqr,umfpack}.$(SHLIB_EXT) - $(CC) -shared $(WHOLE_ARCHIVE) $(SS_LIB)/libamd.a $(SS_LIB)/libsuitesparseconfig.a $(NO_WHOLE_ARCHIVE) -o $(build_libdir)/libamd.$(SHLIB_EXT) + $(CC) -shared $(WHOLE_ARCHIVE) $(SS_LIB)/libsuitesparseconfig.a $(NO_WHOLE_ARCHIVE) -o $(build_libdir)/libsuitesparseconfig.$(SHLIB_EXT) + $(INSTALL_NAME_CMD)libsuitesparseconfig.$(SHLIB_EXT) $(build_libdir)/libsuitesparseconfig.$(SHLIB_EXT) + $(CC) -shared $(WHOLE_ARCHIVE) $(SS_LIB)/libamd.a $(NO_WHOLE_ARCHIVE) -o $(build_libdir)/libamd.$(SHLIB_EXT) $(LDFLAGS) -L$(build_libdir) -lsuitesparseconfig $(RPATH_ORIGIN) $(INSTALL_NAME_CMD)libamd.$(SHLIB_EXT) $(build_libdir)/libamd.$(SHLIB_EXT) - $(CC) -shared $(WHOLE_ARCHIVE) $(SS_LIB)/libcolamd.a $(SS_LIB)/libsuitesparseconfig.a $(NO_WHOLE_ARCHIVE) -o $(build_libdir)/libcolamd.$(SHLIB_EXT) $(LDFLAGS) $(LIBBLAS) + $(CC) -shared $(WHOLE_ARCHIVE) $(SS_LIB)/libcolamd.a $(NO_WHOLE_ARCHIVE) -o $(build_libdir)/libcolamd.$(SHLIB_EXT) $(LDFLAGS) $(LIBBLAS) -L$(build_libdir) -lsuitesparseconfig $(RPATH_ORIGIN) $(INSTALL_NAME_CMD)libcolamd.$(SHLIB_EXT) $(build_libdir)/libcolamd.$(SHLIB_EXT) - $(CXX) -shared $(WHOLE_ARCHIVE) $(SS_LIB)/libcholmod.a $(SS_LIB)/libsuitesparseconfig.a $(NO_WHOLE_ARCHIVE) -o $(build_libdir)/libcholmod.$(SHLIB_EXT) $(LDFLAGS) $(LIBBLAS) -L$(build_libdir) -lcolamd -lccolamd -lcamd -lamd $(RPATH_ORIGIN) + $(CXX) -shared $(WHOLE_ARCHIVE) $(SS_LIB)/libcholmod.a $(NO_WHOLE_ARCHIVE) -o $(build_libdir)/libcholmod.$(SHLIB_EXT) $(LDFLAGS) $(LIBBLAS) -L$(build_libdir) -lsuitesparseconfig -lcolamd -lccolamd -lcamd -lamd $(RPATH_ORIGIN) $(INSTALL_NAME_CMD)libcholmod.$(SHLIB_EXT) $(build_libdir)/libcholmod.$(SHLIB_EXT) - $(CXX) -shared $(WHOLE_ARCHIVE) $(SS_LIB)/libsuitesparseconfig.a $(SS_LIB)/libumfpack.a $(NO_WHOLE_ARCHIVE) -o $(build_libdir)/libumfpack.$(SHLIB_EXT) $(LDFLAGS) $(LIBBLAS) -L$(build_libdir) -lcolamd -lccolamd -lcamd -lamd -lcholmod $(RPATH_ORIGIN) + $(CXX) -shared $(WHOLE_ARCHIVE) $(SS_LIB)/libumfpack.a $(NO_WHOLE_ARCHIVE) -o $(build_libdir)/libumfpack.$(SHLIB_EXT) $(LDFLAGS) $(LIBBLAS) -L$(build_libdir) -lsuitesparseconfig -lcolamd -lccolamd -lcamd -lamd -lcholmod $(RPATH_ORIGIN) $(INSTALL_NAME_CMD)libumfpack.$(SHLIB_EXT) $(build_libdir)/libumfpack.$(SHLIB_EXT) - $(CXX) -shared $(WHOLE_ARCHIVE) $(SS_LIB)/libsuitesparseconfig.a $(SS_LIB)/libspqr.a $(NO_WHOLE_ARCHIVE) -o $(build_libdir)/libspqr.$(SHLIB_EXT) $(LDFLAGS) $(LIBBLAS) -L$(build_libdir) -lcolamd -lccolamd -lcamd -lamd -lcholmod $(RPATH_ORIGIN) + $(CXX) -shared $(WHOLE_ARCHIVE) $(SS_LIB)/libspqr.a $(NO_WHOLE_ARCHIVE) -o $(build_libdir)/libspqr.$(SHLIB_EXT) $(LDFLAGS) $(LIBBLAS) -L$(build_libdir) -lsuitesparseconfig -lcolamd -lccolamd -lcamd -lamd -lcholmod $(RPATH_ORIGIN) $(INSTALL_NAME_CMD)libspqr.$(SHLIB_EXT) $(build_libdir)/libspqr.$(SHLIB_EXT) diff --git a/src/gc.c b/src/gc.c index 91d26eecb8a3f..d56d833794261 100644 --- a/src/gc.c +++ b/src/gc.c @@ -2690,6 +2690,17 @@ DLLEXPORT void *jl_gc_counted_malloc(size_t sz) return b; } +DLLEXPORT void *jl_gc_counted_calloc(size_t nm, size_t sz) +{ + maybe_collect(); + allocd_bytes += nm*sz; + gc_num.malloc++; + void *b = calloc(nm, sz); + if (b == NULL) + jl_throw(jl_memory_exception); + return b; +} + DLLEXPORT void jl_gc_counted_free(void *p, size_t sz) { free(p); @@ -2712,6 +2723,38 @@ DLLEXPORT void *jl_gc_counted_realloc_with_old_size(void *p, size_t old, size_t return b; } +DLLEXPORT void *jl_malloc(size_t sz) +{ + int64_t *p = (int64_t *)jl_gc_counted_malloc(sz + 16); + p[0] = sz; + return (void *)(p + 2); +} + +DLLEXPORT void *jl_calloc(size_t nm, size_t sz) +{ + int64_t *p; + size_t nmsz = nm*sz; + p = (int64_t *)jl_gc_counted_calloc(nmsz + 16, 1); + p[0] = nmsz; + return (void *)(p + 2); +} + +DLLEXPORT void jl_free(void *p) +{ + int64_t *pp = (int64_t *)p - 2; + size_t sz = pp[0]; + jl_gc_counted_free(pp, sz + 16); +} + +DLLEXPORT void *jl_realloc(void *p, size_t sz) +{ + int64_t *pp = (int64_t *)p - 2; + size_t szold = pp[0]; + int64_t *pnew = (int64_t *)jl_gc_counted_realloc_with_old_size(pp, szold + 16, sz + 16); + pnew[0] = sz; + return (void *)(pnew + 2); +} + DLLEXPORT void *jl_gc_managed_malloc(size_t sz) { maybe_collect();