From 5c5dd5462b24783502c7340b86a35460b7610fa9 Mon Sep 17 00:00:00 2001 From: Samuel Lepetit Date: Thu, 31 May 2012 15:10:18 +0200 Subject: [PATCH] Export RngStreams to Java --- CMakeLists.txt | 3 + org/simgrid/msg/RngStream.java | 118 ++++++++++++++++++++++++++++ src/jmsg_process.c | 2 +- src/jmsg_rngstream.c | 135 +++++++++++++++++++++++++++++++++ src/jmsg_rngstream.h | 51 +++++++++++++ 5 files changed, 308 insertions(+), 1 deletion(-) create mode 100644 org/simgrid/msg/RngStream.java create mode 100644 src/jmsg_rngstream.c create mode 100644 src/jmsg_rngstream.h diff --git a/CMakeLists.txt b/CMakeLists.txt index dd3263f723..0cc2249d2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,6 +88,8 @@ set(JMSG_C_SRC src/jmsg_task.h src/jmsg_synchro.c src/jmsg_synchro.h + src/jmsg_rngstream.c + src/jmsg_rngstream.h ) set(JMSG_JAVA_SRC @@ -107,6 +109,7 @@ set(JMSG_JAVA_SRC org/simgrid/msg/TransferFailureException.java org/simgrid/msg/Mutex.java org/simgrid/msg/Comm.java + org/simgrid/msg/RngStream.java ) set(JAVA_EXAMPLES diff --git a/org/simgrid/msg/RngStream.java b/org/simgrid/msg/RngStream.java new file mode 100644 index 0000000000..4b9b734f6c --- /dev/null +++ b/org/simgrid/msg/RngStream.java @@ -0,0 +1,118 @@ +/* + * JNI interface to C RngStream code + * + * Copyright 2006,2007,2010,2012 The SimGrid Team. + * All right reserved. + * + * This program is free software; you can redistribute + * it and/or modify it under the terms of the license + * (GNU LGPL) which comes with this package. + */ +package org.simgrid.msg; +/** + * Export of RngStreams for Java + */ +public class RngStream { + /** + * Represents the bind between the RngStream java object and the C object. + */ + public long bind; + /** + * Creates and returns a new stream without identifier. + * This procedure reserves space to keep the information relative to + * the RngStream, initializes its seed Ig , sets Bg and Cg equal to Ig , sets its antithetic and + * precision switches to 0. The seed Ig is equal to the initial seed of the package given by + * setPackageSeed if this is the first stream created, otherwise it is Z steps ahead + * of that of the most recently created stream. + */ + public RngStream() { + create(""); + } + /** + * Creates and returns a new stream with identifier "name". + * This procedure reserves space to keep the information relative to + * the RngStream, initializes its seed Ig , sets Bg and Cg equal to Ig , sets its antithetic and + * precision switches to 0. The seed Ig is equal to the initial seed of the package given by + * setPackageSeed if this is the first stream created, otherwise it is Z steps ahead + * of that of the most recently created stream. + */ + public RngStream(String name) { + create(name); + } + /** + * The natively implemented method to create a C RngStream object. + */ + private native void create(String name); + /** + * Destructor + */ + protected void finalize() { + destroy(); + } + /** + * Release the C RngStream object + */ + private native void destroy(); + + /** + * Sets the initial seed of the package RngStreams to the six integers in the vector seed. This will + * be the seed (initial state) of the first stream. If this procedure is not called, the default initial + * seed is (12345, 12345, 12345, 12345, 12345, 12345). If it is called, the first 3 values of the seed + * must all be less than m1 = 4294967087, and not all 0; and the last 3 values must all be less + * than m2 = 4294944443, and not all 0. Returns false for invalid seeds, and true otherwise. + */ + public static native boolean setPackageSeed(int seed[]); + /** + * Reinitializes the stream g to its initial state: Cg and Bg are set to Ig . + */ + public native void resetStart(); + /** + * Reinitializes the stream g to the beginning of its current substream: Cg is set to Bg . + */ + public native void restartStartSubstream(); + /** + * Reinitializes the stream g to the beginning of its next substream: Ng is computed, and Cg and + * Bg are set to Ng . + */ + public native void resetNextSubstream(); + /** + * If a = true the stream g will start generating antithetic variates, i.e., 1 − U instead of U , until + * this method is called again with a = false. + */ + public native void setAntithetic(boolean a); + /** + * Sets the initial seed Ig of stream g to the vector seed. This vector must satisfy the same + * conditions as in setPackageSeed. The stream is then reset to this initial seed. The + * states and seeds of the other streams are not modified. As a result, after calling this procedure, + * the initial seeds of the streams are no longer spaced Z values apart. We discourage the use of + * this procedure. Returns false for invalid seeds, and true otherwise. + */ + public native boolean setSeed(int seed[]); + /** + * Advances the state of the stream by k values, without modifying the states of other streams (as + * in RngStream_SetSeed), nor the values of Bg and Ig associated with this stream. If e > 0, then + * k = 2e + c; if e < 0, then k = −2−e + c; and if e = 0, then k = c. Note: c is allowed to take + *negative values. We discourage the use of this procedure. + */ + public native void advanceState(int e, int g); + + /** + * Returns a (pseudo)random number from the uniform distribution over the interval (0, 1), after advancing the state by one step. The returned number has 32 bits of precision + * in the sense that it is always a multiple of 1/(232 − 208), unless RngStream_IncreasedPrecis + * has been called for this stream. + */ + public native double randU01(); + /** + * Returns a (pseudo)random number from the discrete uniform distribution over the integers + * {i, i + 1, . . . , j} + */ + public native int randInt(int i, int j); + + /** + * Class initializer, to initialize various JNI stuff + */ + public static native void nativeInit(); + static { + nativeInit(); + } +} \ No newline at end of file diff --git a/src/jmsg_process.c b/src/jmsg_process.c index ecb1890a4b..6b2284dfdc 100644 --- a/src/jmsg_process.c +++ b/src/jmsg_process.c @@ -43,7 +43,7 @@ m_process_t jprocess_to_native_process(jobject jprocess, JNIEnv * env) void jprocess_bind(jobject jprocess, m_process_t process, JNIEnv * env) { - (*env)->SetLongField(env, jprocess, jprocess_field_Process_bind, (jlong) (long) (process)); + (*env)->SetLongField(env, jprocess, jprocess_field_Process_bind, (jlong)(process)); } jlong jprocess_get_id(jobject jprocess, JNIEnv * env) diff --git a/src/jmsg_rngstream.c b/src/jmsg_rngstream.c new file mode 100644 index 0000000000..f2ae496ee3 --- /dev/null +++ b/src/jmsg_rngstream.c @@ -0,0 +1,135 @@ +/* Functions related to the RngStream Java port */ + +/* Copyright (c) 2007, 2009, 2010, 2012. The SimGrid Team. + * All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + + +#include "jmsg_rngstream.h" +#include "jxbt_utilities.h" + +jfieldID jrngstream_bind; + +RngStream jrngstream_to_native(JNIEnv *env, jobject jrngstream) { + RngStream rngstream = (RngStream) (*env)->GetLongField(env, jrngstream, jrngstream_bind); + if (!rngstream) { + jxbt_throw_notbound(env, "rngstream", jrngstream); + return NULL; + } + return rngstream; +} + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_RngStream_nativeInit(JNIEnv *env, jclass cls) { + jclass class_RngStream = (*env)->FindClass(env, "org/simgrid/msg/RngStream"); + + jrngstream_bind = jxbt_get_jfield(env, class_RngStream, "bind", "J"); +} + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_RngStream_create(JNIEnv *env, jobject jrngstream, jstring jname) { + const char *name = (*env)->GetStringUTFChars(env, jname, 0); + RngStream rngstream = RngStream_CreateStream(name); + //Bind the RngStream object + (*env)->SetLongField(env, jrngstream, jrngstream_bind, (jlong)rngstream); + + (*env)->ReleaseStringUTFChars(env, jname, name); +} +JNIEXPORT void JNICALL +Java_org_simgrid_msg_RngStream_destroy(JNIEnv *env, jobject jrngstream) { + RngStream rngstream = jrngstream_to_native(env, jrngstream); + RngStream_DeleteStream(&rngstream); + (*env)->SetLongField(env, jrngstream, jrngstream_bind, (jlong)NULL); +} +JNIEXPORT jboolean JNICALL +Java_org_simgrid_msg_RngStream_setPackageSeed(JNIEnv *env, jobject jrngstream, jintArray jseed) { + jint buffer[6]; + + (*env)->GetIntArrayRegion(env, jseed, 0, 6, buffer); + + RngStream rngstream = jrngstream_to_native(env, jrngstream); + if (!rngstream) + return JNI_FALSE; + + int result = RngStream_SetPackageSeed((unsigned long*)buffer); + + return result == -1 ? JNI_FALSE : JNI_TRUE; +} +JNIEXPORT void JNICALL +Java_org_simgrid_msg_RngStream_resetStart(JNIEnv *env, jobject jrngstream) { + RngStream rngstream = jrngstream_to_native(env, jrngstream); + if (!rngstream) + return; + + RngStream_ResetStartStream(rngstream); +} +JNIEXPORT void JNICALL +Java_org_simgrid_msg_RngStream_resetStartSubstream(JNIEnv *env, jobject jrngstream) { + RngStream rngstream = jrngstream_to_native(env, jrngstream); + if (!rngstream) + return; + + RngStream_ResetStartSubstream(rngstream); +} +JNIEXPORT void JNICALL +Java_org_simgrid_msg_RngStream_resetNextSubstream(JNIEnv *env, jobject jrngstream) { + RngStream rngstream = jrngstream_to_native(env, jrngstream); + if (!rngstream) + return; + + RngStream_ResetNextSubstream(rngstream); +} +JNIEXPORT void JNICALL +Java_org_simgrid_msg_RngStream_setAntithetic(JNIEnv *env, jobject jrngstream, jboolean ja) { + RngStream rngstream = jrngstream_to_native(env, jrngstream); + if (!rngstream) + return; + + if (ja == JNI_TRUE) { + RngStream_SetAntithetic(rngstream,-1); + } + else { + RngStream_SetAntithetic(rngstream,0); + } +} +JNIEXPORT jboolean JNICALL +Java_org_simgrid_msg_RngStream_setSeed(JNIEnv *env, jobject jrngstream, jintArray jseed) { + jint buffer[6]; + + (*env)->GetIntArrayRegion(env, jseed, 0, 6, buffer); + + RngStream rngstream = jrngstream_to_native(env, jrngstream); + if (!rngstream) + return JNI_FALSE; + + + int result = RngStream_SetSeed(rngstream, (unsigned long*)buffer); + + return result == -1 ? JNI_FALSE : JNI_TRUE; +} +JNIEXPORT void JNICALL +Java_org_simgrid_msg_RngStream_advanceState(JNIEnv *env, jobject jrngstream, jint e, jint g) { + RngStream rngstream = jrngstream_to_native(env, jrngstream); + if (!rngstream) + return; + + RngStream_AdvanceState(rngstream, (long)e, (long)g); +} +JNIEXPORT jdouble JNICALL +Java_org_simgrid_msg_RngStream_randU01(JNIEnv *env, jobject jrngstream) { + RngStream rngstream = jrngstream_to_native(env, jrngstream); + if (!rngstream) + return 0; + + return (jdouble)RngStream_RandU01(rngstream); +} +JNIEXPORT jint JNICALL +Java_org_simgrid_msg_RngStream_randInt(JNIEnv *env, jobject jrngstream, jint i, jint j) { + RngStream rngstream = jrngstream_to_native(env, jrngstream); + if (!rngstream) + return 0; + + return (jint)RngStream_RandInt(rngstream, (int)i, (int)j); +} diff --git a/src/jmsg_rngstream.h b/src/jmsg_rngstream.h new file mode 100644 index 0000000000..b8976bbb4e --- /dev/null +++ b/src/jmsg_rngstream.h @@ -0,0 +1,51 @@ +/* Functions related to the RngStream Java port */ + +/* Copyright (c) 2007, 2009, 2010, 2012. The SimGrid Team. + * All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ +#ifndef MSG_RNGSTREAM_H +#define MSG_RNGSTREAM_H +#include +#include + +RngStream jrngstream_to_native(JNIEnv *env, jobject jrngstream); + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_RngStream_nativeInit(JNIEnv *env, jclass cls); + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_RngStream_create(JNIEnv *env, jobject jrngstream, jstring name); + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_RngStream_destroy(JNIEnv *env, jobject jrngstream); + +JNIEXPORT jboolean JNICALL +Java_org_simgrid_msg_RngStream_setPackageSeed(JNIEnv *env, jobject jrngstream, jintArray seed); + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_RngStream_resetStart(JNIEnv *env, jobject jrngstream); + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_RngStream_resetStartSubstream(JNIEnv *env, jobject jrngstream); + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_RngStream_resetNextSubstream(JNIEnv *env, jobject jrngstream); + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_RngStream_setAntithetic(JNIEnv *env, jobject jrngstream, jboolean ja); + +JNIEXPORT jboolean JNICALL +Java_org_simgrid_msg_RngStream_setSeed(JNIEnv *env, jobject jrngstream, jintArray jseed); + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_RngStream_advanceState(JNIEnv *env, jobject jrngstream, jint e, jint g); + +JNIEXPORT jdouble JNICALL +Java_org_simgrid_msg_RngStream_randU01(JNIEnv *env, jobject jrngstream); + +JNIEXPORT jint JNICALL +Java_org_simgrid_msg_RngStream_randInt(JNIEnv *env, jobject jrngstream, jint i, jint j); + +#endif /* MSG_RNGSTREAM_H */ -- 2.20.1