Skip to content

Commit

Permalink
Move random number generation from src/support to external/random
Browse files Browse the repository at this point in the history
Addresses issue #67
  • Loading branch information
Viral Shah committed Jul 3, 2011
1 parent 8889fe7 commit d181c0c
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 53 deletions.
30 changes: 16 additions & 14 deletions external/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -127,32 +127,34 @@ clean-fdlibm:
rm -f $(FDLIBM_OBJ_SOURCE) $(FDLIBM_OBJ_TARGET)
distclean-fdlibm: clean-fdlibm

## dSFMT ##
## MT ##

DSFMT_OBJ_TARGET = $(EXTROOTLIB)/libdSFMT.$(SHLIB_EXT)
DSFMT_OBJ_SOURCE = dsfmt-$(DSFMT_VER)/libdSFMT.$(SHLIB_EXT)
DSFMT_OBJ_TARGET = $(EXTROOTLIB)/libMT.$(SHLIB_EXT)
DSFMT_OBJ_SOURCE = random/libMT.$(SHLIB_EXT)

compile-dsfmt: $(DSFMT_OBJ_SOURCE)
install-dsfmt: $(DSFMT_OBJ_TARGET)

dsfmt-$(DSFMT_VER).tar.gz:
curl http:https://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/dSFMT-src-2.1.tar.gz > $@
dsfmt-$(DSFMT_VER)/Makefile: dsfmt-$(DSFMT_VER).tar.gz
random/dsfmt-$(DSFMT_VER).tar.gz:
cd random && \
curl -o dsfmt-$(DSFMT_VER).tar.gz http:https://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/dSFMT-src-$(DSFMT_VER).tar.gz -O http:https://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/mt19937-64.tgz
random/dsfmt-$(DSFMT_VER)/Makefile: random/dsfmt-$(DSFMT_VER).tar.gz
cd random && \
mkdir -p dsfmt-$(DSFMT_VER) && \
tar -C dsfmt-$(DSFMT_VER) --strip-components 1 -xf $<
tar -C dsfmt-$(DSFMT_VER) --strip-components 1 -xf dsfmt-$(DSFMT_VER).tar.gz && \
tar zxf mt19937-64.tgz
touch $@
$(DSFMT_OBJ_SOURCE): dsfmt-$(DSFMT_VER)/Makefile
cd dsfmt-$(DSFMT_VER) && \
$(CC) -O3 -finline-functions -fomit-frame-pointer -DNDEBUG -fno-strict-aliasing --param max-inline-insns-single=1800 -Wmissing-prototypes -Wall -std=c99 -DDSFMT_MEXP=19937 -fPIC -shared dSFMT.c -o libdSFMT.$(SHLIB_EXT)
$(DSFMT_OBJ_SOURCE): random/dsfmt-$(DSFMT_VER)/Makefile
cd random && \
$(CC) -O3 -finline-functions -fomit-frame-pointer -DNDEBUG -fno-strict-aliasing --param max-inline-insns-single=1800 -Wmissing-prototypes -Wall -std=c99 -DDSFMT_MEXP=19937 -fPIC -shared jl_random.c dsfmt-2.1/dSFMT.c -I mt19937-64 mt19937-64/mt19937-64.c -o libMT.$(SHLIB_EXT)
$(DSFMT_OBJ_TARGET): $(DSFMT_OBJ_SOURCE)
mkdir -p $(EXTROOTLIB)
cp $< $@

clean-dsfmt:
make -C dSFMT-src-$(DSFMT_VER) clean
rm -f dSFMT-src-$(DSFMT_VER)/*.$(SHLIB_EXT)
distclean-dsfmt:
rm -rf dSFMT-src-$(DSFMT_VER).tar.gz dSFMT-src-$(DSFMT_VER)
rm -f random/libMT.$(SHLIB_EXT)
distclean-dsfmt: clean-dsfmt
cd random && rm -rf *.tgz *.tar.gz dsfmt-$(DSFMT_VER) mt19937-64

## OpenBLAS ##

Expand Down
67 changes: 67 additions & 0 deletions external/random/ieee754.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef __IEEE754_H_
#define __IEEE754_H_

union ieee754_float {
float f;

struct {
#if BYTE_ORDER == BIG_ENDIAN
unsigned int negative:1;
unsigned int exponent:8;
unsigned int mantissa:23;
#endif
#if BYTE_ORDER == LITTLE_ENDIAN
unsigned int mantissa:23;
unsigned int exponent:8;
unsigned int negative:1;
#endif
} ieee;
};

#define IEEE754_FLOAT_BIAS 0x7f

union ieee754_double {
double d;

struct {
#if BYTE_ORDER == BIG_ENDIAN
unsigned int negative:1;
unsigned int exponent:11;
unsigned int mantissa0:20;
unsigned int mantissa1:32;
#endif
#if BYTE_ORDER == LITTLE_ENDIAN
unsigned int mantissa1:32;
unsigned int mantissa0:20;
unsigned int exponent:11;
unsigned int negative:1;
#endif
} ieee;
};

#define IEEE754_DOUBLE_BIAS 0x3ff

union ieee854_long_double {
long double d;

struct {
#if BYTE_ORDER == BIG_ENDIAN
unsigned int negative:1;
unsigned int exponent:15;
unsigned int empty:16;
unsigned int mantissa0:32;
unsigned int mantissa1:32;
#endif
#if BYTE_ORDER == LITTLE_ENDIAN
unsigned int mantissa1:32;
unsigned int mantissa0:32;
unsigned int exponent:15;
unsigned int negative:1;
unsigned int empty:16;
#endif
} ieee;
};

#define IEEE854_LONG_DOUBLE_BIAS 0x3fff

#endif
17 changes: 8 additions & 9 deletions src/support/random.c → external/random/jl_random.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
/*
random numbers
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "dtypes.h"
#include "ieee754.h"
#include "utils.h"
#include "random.h"
#include "timefuncs.h"
#include "jl_random.h"

#include "mt19937ar.c"

Expand Down Expand Up @@ -69,5 +62,11 @@ void randomseed64(uint64_t s)

void randomize()
{
randomseed64(i64time());
struct timeval now;
uint64_t a;

gettimeofday(&now, NULL);
a = (((u_int64_t)now.tv_sec)<<32) + (u_int64_t)now.tv_usec;

randomseed64(a);
}
19 changes: 19 additions & 0 deletions external/random/jl_random.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef __JL_RANDOM_H_
#define __JL_RANDOM_H_

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <stdint.h>
#include "ieee754.h"

double rand_double();
float rand_float();
double randn();
void randomize();
uint32_t genrand_int32();
void randomseed32(uint32_t s);
void randomseed64(uint64_t s);

#endif
File renamed without changes.
25 changes: 16 additions & 9 deletions j/random.j
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
libdsfmt = dlopen("libdSFMT")
libmt = dlopen("libMT")

randomize() = ccall(dlsym(libmt, :randomize), Void, ())

function mt_init()
randomize()
dsfmt_init()
end

DSFMT_MEXP = int32(19937)
DSFMT_STATE = Array(Int32, 1000)
Expand All @@ -7,12 +14,12 @@ DSFMT_POOL_SIZE = 4096
DSFMT_POOL = Array(Float64, DSFMT_POOL_SIZE)
DSFMT_POOL_PTR = DSFMT_POOL_SIZE

dsfmt_init() = ccall(dlsym(libdsfmt, :dsfmt_chk_init_gen_rand),
dsfmt_init() = ccall(dlsym(libmt, :dsfmt_chk_init_gen_rand),
Void, (Ptr{Void}, Uint32, Int32),
DSFMT_STATE, uint32(0), DSFMT_MEXP)

dsfmt_fill_array_open_open(A::Array{Float64}, n::Size) =
ccall(dlsym(libdsfmt, :dsfmt_fill_array_open_open),
ccall(dlsym(libmt, :dsfmt_fill_array_open_open),
Void, (Ptr{Void}, Ptr{Float64}, Int32),
DSFMT_STATE, A, n)

Expand Down Expand Up @@ -63,12 +70,12 @@ end
randint(n::Int) = randint(one(n), n)

# Floating point random numbers
rand() = ccall(:rand_double, Float64, ())
randf() = ccall(:rand_float, Float32, ())
randui32() = ccall(:genrand_int32, Uint32, ())
randn() = ccall(:randn, Float64, ())
srand(s::Union(Int32,Uint32)) = ccall(:randomseed32, Void, (Uint32,), uint32(s))
srand(s::Union(Int64,Uint64)) = ccall(:randomseed64, Void, (Uint64,), uint64(s))
rand() = ccall(dlsym(libmt, :rand_double), Float64, ())
randf() = ccall(dlsym(libmt, :rand_float), Float32, ())
randui32() = ccall(dlsym(libmt, :genrand_int32), Uint32, ())
randn() = ccall(dlsym(libmt, :randn), Float64, ())
srand(s::Union(Int32,Uint32)) = ccall(dlsym(libmt, :randomseed32), Void, (Uint32,), uint32(s))
srand(s::Union(Int64,Uint64)) = ccall(dlsym(libmt, :randomseed64), Void, (Uint64,), uint64(s))

# Arrays of random numbers
macro rand_matrix_builder(t, f)
Expand Down
2 changes: 1 addition & 1 deletion j/start_image.j
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ libc = ccall(:jl_load_dynamic_library, Ptr{Void}, (Ptr{Uint8},), C_NULL)
libm = dlopen("libm")
libfdm = dlopen("libfdm")

libdsfmt = dlopen("libdSFMT")
libmt = dlopen("libMT")

libpcre = dlopen("libpcre")

Expand Down
2 changes: 1 addition & 1 deletion j/sysimg.j
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ load("intfuncs.j")
load("floatfuncs.j")
load("math.j")
load("math_libm.j")
load("random.j"); dsfmt_init();
load("random.j"); mt_init();
load("combinatorics.j")
load("linalg.j")
load("linalg_blas.j")
Expand Down
1 change: 0 additions & 1 deletion src/flisp/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <errno.h>
#include "llt.h"
#include "flisp.h"
#include "random.h"

size_t llength(value_t v)
{
Expand Down
2 changes: 1 addition & 1 deletion src/support/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ include $(JULIAHOME)/Make.inc

SRCS = hashing.c timefuncs.c dblprint.c ptrhash.c operators.c socket.c \
utf8.c ios.c cplxprint.c dirpath.c htable.c bitvector.c \
int2str.c dump.c random.c bswap.c lltinit.c arraylist.c
int2str.c dump.c bswap.c lltinit.c arraylist.c
OBJS = $(SRCS:%.c=%.o)
DOBJS = $(SRCS:%.c=%.do)
TARGET = libllt.a
Expand Down
1 change: 0 additions & 1 deletion src/support/hashing.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "hashing.h"
#include "timefuncs.h"
#include "ios.h"
#include "random.h"

uint_t nextipow2(uint_t i)
{
Expand Down
1 change: 0 additions & 1 deletion src/support/llt.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "ptrhash.h"
#include "bitvector.h"
#include "dirpath.h"
#include "random.h"

DLLEXPORT void llt_init();

Expand Down
2 changes: 0 additions & 2 deletions src/support/lltinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ void llt_init()
{
locale_is_utf8 = u8_is_locale_utf8(setlocale(LC_ALL, ""));

randomize();

ios_init_stdstreams();

D_PNAN = strtod("+NaN",NULL);
Expand Down
13 changes: 0 additions & 13 deletions src/support/random.h

This file was deleted.

0 comments on commit d181c0c

Please sign in to comment.