From 0d93a8a080a9647de8d4fa279ac0f4c303df70e9 Mon Sep 17 00:00:00 2001 From: Mick Jordan <mick.jordan@oracle.com> Date: Tue, 5 Aug 2014 20:04:36 -0700 Subject: [PATCH] add test.native project with user_rng test --- .../include/R_ext/Boolean.h | 37 +++++++++ .../include/R_ext/Random.h | 75 +++++++++++++++++++ com.oracle.truffle.r.test.native/Makefile | 33 ++++++++ .../urand/src/Makefile | 51 +++++++++++++ .../urand/src/urand.c | 29 +++++++ .../truffle/r/test/rffi/TestUserRNG.java | 10 ++- mx.fastr/projects | 6 +- 7 files changed, 236 insertions(+), 5 deletions(-) create mode 100644 com.oracle.truffle.r.native/include/R_ext/Boolean.h create mode 100644 com.oracle.truffle.r.native/include/R_ext/Random.h create mode 100644 com.oracle.truffle.r.test.native/Makefile create mode 100644 com.oracle.truffle.r.test.native/urand/src/Makefile create mode 100644 com.oracle.truffle.r.test.native/urand/src/urand.c diff --git a/com.oracle.truffle.r.native/include/R_ext/Boolean.h b/com.oracle.truffle.r.native/include/R_ext/Boolean.h new file mode 100644 index 0000000000..ea855a2973 --- /dev/null +++ b/com.oracle.truffle.r.native/include/R_ext/Boolean.h @@ -0,0 +1,37 @@ +/* + * R : A Computer Language for Statistical Data Analysis + * Copyright (C) 2000, 2001 The R Core Team. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, a copy is available at + * http://www.r-project.org/Licenses/ + */ + +/* Included by R.h: API */ + +#ifndef R_EXT_BOOLEAN_H_ +#define R_EXT_BOOLEAN_H_ + +#undef FALSE +#undef TRUE + +#ifdef __cplusplus +extern "C" { +#endif +typedef enum { FALSE = 0, TRUE /*, MAYBE */ } Rboolean; + +#ifdef __cplusplus +} +#endif + +#endif /* R_EXT_BOOLEAN_H_ */ diff --git a/com.oracle.truffle.r.native/include/R_ext/Random.h b/com.oracle.truffle.r.native/include/R_ext/Random.h new file mode 100644 index 0000000000..0a264a3954 --- /dev/null +++ b/com.oracle.truffle.r.native/include/R_ext/Random.h @@ -0,0 +1,75 @@ +/* + * R : A Computer Language for Statistical Data Analysis + * Copyright (C) 1998-2011 The R Core Team + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, a copy is available at + * http://www.r-project.org/Licenses/ + */ + +/* Included by R.h: API */ + +#ifndef R_RANDOM_H +#define R_RANDOM_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include <R_ext/Boolean.h> + +typedef enum { + WICHMANN_HILL, + MARSAGLIA_MULTICARRY, + SUPER_DUPER, + MERSENNE_TWISTER, + KNUTH_TAOCP, + USER_UNIF, + KNUTH_TAOCP2, + LECUYER_CMRG +} RNGtype; + +/* Different kinds of "N(0,1)" generators :*/ +typedef enum { + BUGGY_KINDERMAN_RAMAGE, + AHRENS_DIETER, + BOX_MULLER, + USER_NORM, + INVERSION, + KINDERMAN_RAMAGE +} N01type; + + +void GetRNGstate(void); +void PutRNGstate(void); + +double unif_rand(void); +/* These are also defined in Rmath.h */ +double norm_rand(void); +double exp_rand(void); + +typedef unsigned int Int32; +double * user_unif_rand(void); +void user_unif_init(Int32); +int * user_unif_nseed(void); +int * user_unif_seedloc(void); + +double * user_norm_rand(void); + +void FixupProb(double *, int, int, Rboolean); + +#ifdef __cplusplus +} +#endif + +#endif /* R_RANDOM_H */ diff --git a/com.oracle.truffle.r.test.native/Makefile b/com.oracle.truffle.r.test.native/Makefile new file mode 100644 index 0000000000..b2e68ef591 --- /dev/null +++ b/com.oracle.truffle.r.test.native/Makefile @@ -0,0 +1,33 @@ +# +# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# This code is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 2 only, as +# published by the Free Software Foundation. +# +# This code is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# version 2 for more details (a copy is included in the LICENSE file that +# accompanied this code). +# +# You should have received a copy of the GNU General Public License version +# 2 along with this work; if not, write to the Free Software Foundation, +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA +# or visit www.oracle.com if you need additional information or have any +# questions. +# + +.PHONY: urand clean + +export TOPDIR = $(CURDIR) + +urand: + $(MAKE) -C urand/src + +clean: + $(MAKE) -C urand/src clean + \ No newline at end of file diff --git a/com.oracle.truffle.r.test.native/urand/src/Makefile b/com.oracle.truffle.r.test.native/urand/src/Makefile new file mode 100644 index 0000000000..e78bd38380 --- /dev/null +++ b/com.oracle.truffle.r.test.native/urand/src/Makefile @@ -0,0 +1,51 @@ +# +# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# This code is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 2 only, as +# published by the Free Software Foundation. +# +# This code is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# version 2 for more details (a copy is included in the LICENSE file that +# accompanied this code). +# +# You should have received a copy of the GNU General Public License version +# 2 along with this work; if not, write to the Free Software Foundation, +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA +# or visit www.oracle.com if you need additional information or have any +# questions. +# + +.PHONY: all clean + +BIN = ../bin +SRC = $(CURDIR) +C_SOURCES := $(wildcard *.c) +C_LIBNAME := lib$(C_SOURCES:.c=.so) +C_OBJECTS := $(BIN)/$(C_SOURCES:.c=.o) +C_LIB := $(BIN)/$(C_LIBNAME) + + +ifeq ($(TOPDIR),) + TOPDIR = $(abspath ../..) +endif + +INCLUDE_DIR := $(subst test.native,native,$(TOPDIR))/include + +all: $(C_OBJECTS) +ifneq ($(shell uname), Darwin) + gcc -fPIC -shared -o $(C_LIB) $(C_OBJECTS) +else + gcc -dynamiclib -undefined dynamic_lookup -o $(C_LIB) $(C_OBJECTS) +endif + +$(BIN)/%.o: %.c + gcc -I$(INCLUDE_DIR) -fPIC -O2 -c $< -o $@ + +clean: + rm $(BIN)/* diff --git a/com.oracle.truffle.r.test.native/urand/src/urand.c b/com.oracle.truffle.r.test.native/urand/src/urand.c new file mode 100644 index 0000000000..6a701ff2fd --- /dev/null +++ b/com.oracle.truffle.r.test.native/urand/src/urand.c @@ -0,0 +1,29 @@ +/* + * This material is distributed under the GNU General Public License + * Version 2. You may review the terms of this license at + * http://www.gnu.org/licenses/gpl-2.0.html + * + * Copyright (c) 1995-2012, The R Core Team + * Copyright (c) 2003, The R Foundation + * Copyright (c) 2013, Oracle and/or its affiliates + * + * All rights reserved. + */ + +#include <R_ext/Random.h> + +static Int32 seed; +static double res; +static int nseed = 1; + +double * user_unif_rand() +{ + seed = 69069 * seed + 1; + res = seed * 2.32830643653869e-10; + return &res; +} + +void user_unif_init(Int32 seed_in) { seed = seed_in; } +int * user_unif_nseed() { return &nseed; } +int * user_unif_seedloc() { return (int *) &seed; } + diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/rffi/TestUserRNG.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/rffi/TestUserRNG.java index 6731987a96..266bc5c132 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/rffi/TestUserRNG.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/rffi/TestUserRNG.java @@ -22,15 +22,21 @@ */ package com.oracle.truffle.r.test.rffi; +import java.nio.file.*; + import org.junit.*; +import com.oracle.truffle.r.runtime.*; import com.oracle.truffle.r.test.*; public class TestUserRNG extends TestBase { @Test - @Ignore public void testUserRNG() { - // TODO + Path cwd = Paths.get(System.getProperty("user.dir")); + Path libPath = Paths.get(REnvVars.rHome(), "com.oracle.truffle.r.test.native/urand/bin/liburand.so"); + Path relLibPath = cwd.relativize(libPath); + String testExpr = String.format("{ dyn.load(\"%s\"); RNGkind(\"user\"); print(RNGkind()); set.seed(4567); runif(10) }", relLibPath); + assertEval(testExpr); } } diff --git a/mx.fastr/projects b/mx.fastr/projects index 130007ee1b..944e24afa3 100644 --- a/mx.fastr/projects +++ b/mx.fastr/projects @@ -121,9 +121,9 @@ project@com.oracle.truffle.r.test@annotationProcessors=com.oracle.truffle.r.test project@com.oracle.truffle.r.test@workingSets=Truffle,FastR,Test # com.oracle.truffle.r.test.native -# project@com.oracle.truffle.r.test.native@sourceDirs= -# project@com.oracle.truffle.r.test.native@native=true -# project@com.oracle.truffle.r.test.native@workingSets=FastR +project@com.oracle.truffle.r.test.native@sourceDirs= +project@com.oracle.truffle.r.test.native@native=true +project@com.oracle.truffle.r.test.native@workingSets=FastR # com.oracle.truffle.r.engine project@com.oracle.truffle.r.engine@sourceDirs=src -- GitLab