Skip to content

Commit

Permalink
Merge pull request JuliaLang#7448 from nalimilan/librandom
Browse files Browse the repository at this point in the history
Allow using system dSFMT
  • Loading branch information
Viral B. Shah committed Jun 28, 2014
2 parents a4ba5e5 + d79010b commit bdbab62
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 47 deletions.
1 change: 1 addition & 0 deletions Make.inc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ USE_SYSTEM_LIBM=0
USE_SYSTEM_OPENLIBM=0
UNTRUSTED_SYSTEM_LIBM=0
USE_SYSTEM_OPENSPECFUN=0
USE_SYSTEM_DSFMT=0
USE_SYSTEM_BLAS=0
USE_SYSTEM_LAPACK=0
USE_SYSTEM_FFTW=0
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ $(build_bindir)/stringpatch: $(build_bindir) contrib/stringpatch.c
JL_LIBS = julia julia-debug

# private libraries, that are installed in $(prefix)/lib/julia
JL_PRIVATE_LIBS = random suitesparse_wrapper grisu Rmath
JL_PRIVATE_LIBS = suitesparse_wrapper grisu Rmath
ifeq ($(USE_SYSTEM_FFTW),0)
JL_PRIVATE_LIBS += fftw3 fftw3f fftw3_threads fftw3f_threads
endif
Expand All @@ -119,6 +119,9 @@ endif
ifeq ($(USE_SYSTEM_OPENSPECFUN),0)
JL_PRIVATE_LIBS += openspecfun
endif
ifeq ($(USE_SYSTEM_DSFMT),0)
JL_PRIVATE_LIBS += dSFMT
endif
ifeq ($(USE_SYSTEM_BLAS),0)
JL_PRIVATE_LIBS += openblas
else ifeq ($(USE_SYSTEM_LAPACK),0)
Expand Down
28 changes: 13 additions & 15 deletions base/librandom.jl → base/dSFMT.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module LibRandom
module dSFMT

export DSFMT_state, dsfmt_get_min_array_size, dsfmt_get_idstring,
dsfmt_init_gen_rand, dsfmt_gv_init_gen_rand,
Expand All @@ -9,91 +9,89 @@ export DSFMT_state, dsfmt_get_min_array_size, dsfmt_get_idstring,
randmtzig_randn, randmtzig_exprnd,
win32_SystemFunction036!

## DSFMT

type DSFMT_state
val::Vector{Int32}
DSFMT_state() = new(Array(Int32, 770))
end

function dsfmt_get_idstring()
idstring = ccall((:dsfmt_get_idstring,:librandom),
idstring = ccall((:dsfmt_get_idstring,:libdSFMT),
Ptr{Uint8},
())
return bytestring(idstring)
end

function dsfmt_get_min_array_size()
min_array_size = ccall((:dsfmt_get_min_array_size,:librandom),
min_array_size = ccall((:dsfmt_get_min_array_size,:libdSFMT),
Int32,
())
end

const dsfmt_min_array_size = dsfmt_get_min_array_size()

function dsfmt_init_gen_rand(s::DSFMT_state, seed::Uint32)
ccall((:dsfmt_init_gen_rand,:librandom),
ccall((:dsfmt_init_gen_rand,:libdSFMT),
Void,
(Ptr{Void}, Uint32,),
s.val, seed)
end

function dsfmt_gv_init_gen_rand(seed::Uint32)
ccall((:dsfmt_gv_init_gen_rand,:librandom),
ccall((:dsfmt_gv_init_gen_rand,:libdSFMT),
Void,
(Uint32,),
seed)
end

function dsfmt_init_by_array(s::DSFMT_state, seed::Vector{Uint32})
ccall((:dsfmt_init_by_array,:librandom),
ccall((:dsfmt_init_by_array,:libdSFMT),
Void,
(Ptr{Void}, Ptr{Uint32}, Int32),
s.val, seed, length(seed))
end

function dsfmt_gv_init_by_array(seed::Vector{Uint32})
ccall((:dsfmt_gv_init_by_array,:librandom),
ccall((:dsfmt_gv_init_by_array,:libdSFMT),
Void,
(Ptr{Uint32}, Int32),
seed, length(seed))
end

function dsfmt_genrand_close_open(s::DSFMT_state)
ccall((:dsfmt_genrand_close_open, :librandom),
ccall((:dsfmt_genrand_close_open, :libdSFMT),
Float64,
(Ptr{Void},),
s.val)
end

function dsfmt_gv_genrand_close_open()
ccall((:dsfmt_gv_genrand_close_open, :librandom),
ccall((:dsfmt_gv_genrand_close_open, :libdSFMT),
Float64,
())
end

function dsfmt_genrand_close1_open2(s::DSFMT_state)
ccall((:dsfmt_genrand_close1_open2, :librandom),
ccall((:dsfmt_genrand_close1_open2, :libdSFMT),
Float64,
(Ptr{Void},),
s.val)
end

function dsfmt_gv_genrand_close1_open2()
ccall((:dsfmt_gv_genrand_close1_open2, :librandom),
ccall((:dsfmt_gv_genrand_close1_open2, :libdSFMT),
Float64,
())
end

function dsfmt_genrand_uint32(s::DSFMT_state)
ccall((:dsfmt_genrand_uint32,:librandom),
ccall((:dsfmt_genrand_uint32,:libdSFMT),
Uint32,
(Ptr{Void},),
s.val)
end

function dsfmt_gv_genrand_uint32()
ccall((:dsfmt_gv_genrand_uint32,:librandom),
ccall((:dsfmt_gv_genrand_uint32,:libdSFMT),
Uint32,
())
end
Expand Down
2 changes: 1 addition & 1 deletion base/random.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Random

using Base.LibRandom
using Base.dSFMT

export srand,
rand, rand!,
Expand Down
2 changes: 1 addition & 1 deletion base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ big(z::Complex) = complex(big(real(z)),big(imag(z)))
include("hashing2.jl")

# random number generation
include("librandom.jl")
include("dSFMT.jl")
include("random.jl")
importall .Random

Expand Down
44 changes: 26 additions & 18 deletions deps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ MAKE_COMMON = DESTDIR="" prefix=$(build_prefix) bindir=$(build_bindir) libdir=$(
# prevent installing libs into usr/lib64 on opensuse
unexport CONFIG_SITE

STAGE1_DEPS = random double-conversion
STAGE1_DEPS = double-conversion
STAGE2_DEPS = Rmath
STAGE3_DEPS = suitesparse-wrapper

Expand Down Expand Up @@ -63,6 +63,10 @@ ifeq ($(USE_SYSTEM_OPENSPECFUN), 0)
STAGE1_DEPS += openspecfun
endif

ifeq ($(USE_SYSTEM_DSFMT), 0)
STAGE1_DEPS += random
endif

ifeq ($(USE_SYSTEM_LLVM), 0)
STAGE1_DEPS += llvm
endif
Expand Down Expand Up @@ -636,20 +640,24 @@ compile-openspecfun: $(OPENSPECFUN_OBJ_SOURCE)
check-openspecfun: compile-openspecfun
install-openspecfun: $(OPENSPECFUN_OBJ_TARGET)

## LIBRANDOM ##
## DSFMT ##

ifeq ($(USE_SYSTEM_DSFMT),0)
DSFMT_FAKE_TARGET = $(DSFMT_OBJ_TARGET)
endif

LIBRANDOM_OBJ_TARGET = $(build_shlibdir)/librandom.$(SHLIB_EXT)
LIBRANDOM_OBJ_SOURCE = librandom.$(SHLIB_EXT)
DSFMT_OBJ_TARGET = $(build_shlibdir)/libdSFMT.$(SHLIB_EXT)
DSFMT_OBJ_SOURCE = libdSFMT.$(SHLIB_EXT)

LIBRANDOM_CFLAGS = $(CFLAGS) -DNDEBUG -DDSFMT_MEXP=19937 $(fPIC) -DDSFMT_DO_NOT_USE_OLD_NAMES
DSFMT_CFLAGS = $(CFLAGS) -DNDEBUG -DDSFMT_MEXP=19937 $(fPIC) -DDSFMT_DO_NOT_USE_OLD_NAMES
ifneq ($(USEMSVC), 1)
LIBRANDOM_CFLAGS += -O3 -finline-functions -fomit-frame-pointer -fno-strict-aliasing \
DSFMT_CFLAGS += -O3 -finline-functions -fomit-frame-pointer -fno-strict-aliasing \
--param max-inline-insns-single=1800 -Wmissing-prototypes -Wall -std=c99 -shared
else
LIBRANDOM_CFLAGS += -Wl,-dll
DSFMT_CFLAGS += -Wl,-dll
endif
ifeq ($(ARCH), x86_64)
LIBRANDOM_CFLAGS += -msse2 -DHAVE_SSE2
DSFMT_CFLAGS += -msse2 -DHAVE_SSE2
endif

dsfmt-$(DSFMT_VER).tar.gz:
Expand All @@ -661,22 +669,22 @@ dsfmt-$(DSFMT_VER)/config.status: dsfmt-$(DSFMT_VER).tar.gz
$(TAR) -C dsfmt-$(DSFMT_VER) --strip-components 1 -xf dsfmt-$(DSFMT_VER).tar.gz && \
cd dsfmt-$(DSFMT_VER) && patch < ../dSFMT.h.patch && patch < ../dSFMT.c.patch
echo 1 > $@
$(LIBRANDOM_OBJ_SOURCE): dsfmt-$(DSFMT_VER)/config.status
$(CC) $(CPPFLAGS) $(LIBRANDOM_CFLAGS) $(LDFLAGS) dsfmt-$(DSFMT_VER)/dSFMT.c -o librandom.$(SHLIB_EXT) && \
$(INSTALL_NAME_CMD)librandom.$(SHLIB_EXT) librandom.$(SHLIB_EXT)
$(LIBRANDOM_OBJ_TARGET): $(LIBRANDOM_OBJ_SOURCE)
$(DSFMT_OBJ_SOURCE): dsfmt-$(DSFMT_VER)/config.status
$(CC) $(CPPFLAGS) $(DSFMT_CFLAGS) $(LDFLAGS) dsfmt-$(DSFMT_VER)/dSFMT.c -o libdSFMT.$(SHLIB_EXT) && \
$(INSTALL_NAME_CMD)libdSFMT.$(SHLIB_EXT) libdSFMT.$(SHLIB_EXT)
$(DSFMT_OBJ_TARGET): $(DSFMT_OBJ_SOURCE)
cp $< $@

clean-random:
-rm -f librandom.$(SHLIB_EXT)
-rm -f libdSFMT.$(SHLIB_EXT)
distclean-random: clean-random
-rm -rf *.tar.gz dsfmt-$(DSFMT_VER)

get-random: dsfmt-$(DSFMT_VER).tar.gz
configure-random: dsfmt-$(DSFMT_VER)/config.status
compile-random: $(LIBRANDOM_OBJ_SOURCE)
compile-random: $(DSFMT_OBJ_SOURCE)
check-random: compile-random
install-random: $(LIBRANDOM_OBJ_TARGET)
install-random: $(DSFMT_OBJ_TARGET)


## Rmath ##
Expand All @@ -686,8 +694,8 @@ RMATH_OBJ_SOURCE = Rmath/src/libRmath-julia.$(SHLIB_EXT)

RMATH_FLAGS += CC="$(CC)" USECLANG=$(USECLANG) USEGCC=$(USEGCC) \
OS="$(OS)" ARCH="$(ARCH)" \
USE_LIBRANDOM=1 LIBRANDOM_PATH="$(build_shlibdir)" \
DSFMT_PATH=$(JULIAHOME)/deps/dsfmt-$(DSFMT_VER)
USE_DSFMT=1 DSFMT_libdir="$(build_shlibdir)" \
DSFMT_includedir="$(build_shlibdir)"

Rmath/Make.inc:
(cd .. && git submodule init && git submodule update)
Expand All @@ -697,7 +705,7 @@ endif
ifeq (exists, $(shell [ -d $(JULIAHOME)/.git/modules/deps/Rmath ] && echo exists ))
$(RMATH_OBJ_SOURCE): $(JULIAHOME)/.git/modules/deps/Rmath/HEAD
endif
$(RMATH_OBJ_SOURCE): Rmath/Make.inc $(LIBRANDOM_OBJ_TARGET)
$(RMATH_OBJ_SOURCE): Rmath/Make.inc $(DSFMT_OBJ_TARGET)
$(MAKE) -C Rmath/src $(RMATH_FLAGS) $(MAKE_COMMON)
touch -c $@
$(RMATH_OBJ_TARGET): $(RMATH_OBJ_SOURCE) | $(build_shlibdir)
Expand Down
2 changes: 1 addition & 1 deletion deps/Rmath
Submodule Rmath updated from e432b0 to 92e958
8 changes: 4 additions & 4 deletions doc/stdlib/profile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ we'll use the text-based display that comes with the standard library::
23 client.jl; eval_user_input; line: 91
23 profile.jl; anonymous; line: 14
8 none; myfunc; line: 2
8 librandom.jl; dsfmt_gv_fill_array_close_open!; line: 128
8 dSFMT.jl; dsfmt_gv_fill_array_close_open!; line: 128
15 none; myfunc; line: 3
2 reduce.jl; max; line: 35
2 reduce.jl; max; line: 36
Expand Down Expand Up @@ -113,7 +113,7 @@ rather than putting it in a file; if we had used a file, this would
show the file name. Line 2 of ``myfunc()`` contains the call to
``rand``, and there were 8 (out of 23) backtraces that occurred at
this line. Below that, you can see a call to
``dsfmt_gv_fill_array_close_open!`` inside ``librandom.jl``. You might be surprised not to see the
``dsfmt_gv_fill_array_close_open!`` inside ``dSFMT.jl``. You might be surprised not to see the
``rand`` function listed explicitly: that's because ``rand`` is *inlined*,
and hence doesn't appear in the backtraces.

Expand All @@ -140,7 +140,7 @@ more samples::
3121 client.jl; eval_user_input; line: 91
3121 profile.jl; anonymous; line: 1
848 none; myfunc; line: 2
842 librandom.jl; dsfmt_gv_fill_array_close_open!; line: 128
842 dSFMT.jl; dsfmt_gv_fill_array_close_open!; line: 128
1510 none; myfunc; line: 3
74 reduce.jl; max; line: 35
122 reduce.jl; max; line: 36
Expand All @@ -163,7 +163,7 @@ dump, which accumulates counts independent of their nesting::
3121 client.jl _start 373
3121 client.jl eval_user_input 91
3121 client.jl run_repl 166
842 librandom.jl dsfmt_gv_fill_array_close_open! 128
842 dSFMT.jl dsfmt_gv_fill_array_close_open! 128
848 none myfunc 2
1510 none myfunc 3
3121 profile.jl anonymous 1
Expand Down
12 changes: 6 additions & 6 deletions test/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ function randmtzig_fill_ziggurat_tables() # Operates on the global arrays
return nothing
end
randmtzig_fill_ziggurat_tables()
@test all(ki == Base.LibRandom.ki)
@test all(wi == Base.LibRandom.wi)
@test all(fi == Base.LibRandom.fi)
@test all(ke == Base.LibRandom.ke)
@test all(we == Base.LibRandom.we)
@test all(fe == Base.LibRandom.fe)
@test all(ki == Base.dSFMT.ki)
@test all(wi == Base.dSFMT.wi)
@test all(fi == Base.dSFMT.fi)
@test all(ke == Base.dSFMT.ke)
@test all(we == Base.dSFMT.we)
@test all(fe == Base.dSFMT.fe)

#same random numbers on for small ranges on all systems

Expand Down

0 comments on commit bdbab62

Please sign in to comment.