Skip to content

Commit

Permalink
Merge pull request #12034 from JuliaLang/anj/gc
Browse files Browse the repository at this point in the history
Define gc counted allocator functions with libc API
  • Loading branch information
andreasnoack committed Jul 13, 2015
2 parents 46b8103 + ab7591c commit 2b0faa1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions base/sparse/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions contrib/repackage_system_suitesparse4.make
Original file line number Diff line number Diff line change
Expand Up @@ -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)

43 changes: 43 additions & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2689,6 +2689,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);
Expand All @@ -2711,6 +2722,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();
Expand Down

0 comments on commit 2b0faa1

Please sign in to comment.