From: Samuel Lepetit Date: Fri, 4 May 2012 14:24:59 +0000 (+0200) Subject: Move some functions from MsgNative to Task, to make one less call X-Git-Tag: v3_9_90~569^2~19^2~105 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/8e72922a58f5c591eb71fde589d362f6669a7a0f Move some functions from MsgNative to Task, to make one less call --- diff --git a/org/simgrid/msg/MsgNative.java b/org/simgrid/msg/MsgNative.java index 3c11df804f..c93dd88d5d 100644 --- a/org/simgrid/msg/MsgNative.java +++ b/org/simgrid/msg/MsgNative.java @@ -264,28 +264,6 @@ final class MsgNative { * The natively implemented methods connected to the MSG task * ******************************************************************/ - /** - * The natively implemented method to create a MSG task. - * - * @param name The name of th task. - * @param computeDuration A value of the processing amount (in flop) needed - * to process the task. If 0, then it cannot be executed - * with the execute() method. This value has to be >= 0. - * @param messageSize A value of amount of data (in bytes) needed to transfert - * this task. If 0, then it cannot be transfered this task. - * If 0, then it cannot be transfered with the get() and put() - * methods. This value has to be >= 0. - * @param task The java task object to bind with the native task to create. - * - * @exception IllegalArgumentException if compute duration <0 or message size <0 - * - * @see Task.create() - */ - final static native void taskCreate(Task task, String name, - double computeDuration, - double messageSize) - throws IllegalArgumentException; - /** * The natively implemented method to get the sender of a task. * @@ -330,23 +308,6 @@ final class MsgNative { */ final static native void taskCancel(Task task); - /** - * The natively implemented method to create a MSG parallel task. - * - * @param name The name of the parallel task. - * @param hosts The list of hosts implied by the parallel task. - * @param computeDurations The total number of operations that have to be performed - * on the hosts. - * @param messageSizes An array of doubles - * - * @see ParallelTask.create() - */ - final static native void parallelTaskCreate(Task pTask, String name, - Host[]hosts, - double[]computeDurations, - double[]messageSizes) - throws NullPointerException, IllegalArgumentException; - /** * The natively implemented method to get the computing amount of the task. * @@ -380,16 +341,6 @@ final class MsgNative { */ final static native void taskSetPriority(Task task, double priority); - /** - * The natively implemented method to destroy a MSG task. - * - * @param The task to destroy. - * - * - * @see Task.destroy() - */ - final static native void taskDestroy(Task task) ; - /** * The natively implemented method to execute a MSG task. * diff --git a/org/simgrid/msg/Task.java b/org/simgrid/msg/Task.java index 393b2e75ac..441ccb777b 100644 --- a/org/simgrid/msg/Task.java +++ b/org/simgrid/msg/Task.java @@ -27,8 +27,9 @@ public class Task { /** Default constructor (all fields to 0 or null) */ public Task() { - MsgNative.taskCreate(this, null, 0, 0); + create(null, 0, 0); } + /* * * * * * Constructors * * * * * */ @@ -47,7 +48,7 @@ public class Task { * This value has to be >= 0. */ public Task(String name, double computeDuration, double messageSize) { - MsgNative.taskCreate(this, name, computeDuration, messageSize); + create(name, computeDuration, messageSize); } /** * Construct an new parallel task with the specified processing amount and amount for each host @@ -59,17 +60,49 @@ public class Task { * @param messageSizes A matrix describing the amount of data to exchange between hosts. */ public Task(String name, Host[]hosts, double[]computeDurations, double[]messageSizes) { - MsgNative.parallelTaskCreate(this, name, hosts, computeDurations, messageSizes); + parallelCreate(name, hosts, computeDurations, messageSizes); } + + /** + * The natively implemented method to create a MSG task. + * + * @param name The name of th task. + * @param computeDuration A value of the processing amount (in flop) needed + * to process the task. If 0, then it cannot be executed + * with the execute() method. This value has to be >= 0. + * @param messageSize A value of amount of data (in bytes) needed to transfert + * this task. If 0, then it cannot be transfered this task. + * If 0, then it cannot be transfered with the get() and put() + * methods. This value has to be >= 0. + * @exception IllegalArgumentException if compute duration <0 or message size <0 + */ + final native void create(String name, + double computeDuration, + double messageSize) + throws IllegalArgumentException; + /** + * The natively implemented method to create a MSG parallel task. + * + * @param name The name of the parallel task. + * @param hosts The list of hosts implied by the parallel task. + * @param computeDurations The total number of operations that have to be performed + * on the hosts. + * @param messageSizes An array of doubles + * + */ + final native void parallelCreate(String name, + Host[]hosts, + double[]computeDurations, + double[]messageSizes) + throws NullPointerException, IllegalArgumentException; /* * * * * * Getters / Setters * * * * * */ /** Gets the name of a task + * FIXME: Cache it. * @return */ - public String getName() { - return MsgNative.taskGetName(this); - } + public native String getName(); /** Gets the sender of the task */ Process getSender() { return MsgNative.taskGetSender(this); @@ -133,10 +166,12 @@ public class Task { * @exception NativeException if the destruction failed. */ protected void finalize() throws NativeException { - if (this.bind != 0) - MsgNative.taskDestroy(this); + destroy(); } - + /** + * The natively implemented method to destroy a MSG task. + */ + protected native void destroy(); /** Send the task asynchronously on the mailbox identified by the specified name, * with no way to retrieve whether the communication succeeded or not diff --git a/src/jmsg.c b/src/jmsg.c index bc81dc2fb9..87706b50a0 100644 --- a/src/jmsg.c +++ b/src/jmsg.c @@ -513,136 +513,6 @@ Java_org_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls, * The MSG task connected functions implementation. * ***************************************************************************************/ -JNIEXPORT void JNICALL -Java_org_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls, - jobject jtask, jstring jname, - jdouble jcomputeDuration, - jdouble jmessageSize) -{ - m_task_t task; /* the native task to create */ - const char *name = NULL; /* the name of the task */ - - if (jcomputeDuration < 0) { - jxbt_throw_illegal(env, - bprintf - ("Task ComputeDuration (%f) cannot be negative", - (double) jcomputeDuration)); - return; - } - - if (jmessageSize < 0) { - jxbt_throw_illegal(env, - bprintf("Task MessageSize (%f) cannot be negative", - (double) jmessageSize)); - return; - } - - if (jname) { - /* get the C string from the java string */ - name = (*env)->GetStringUTFChars(env, jname, 0); - } - - - /* create the task */ - task = - MSG_task_create(name, (double) jcomputeDuration, - (double) jmessageSize, NULL); - if (jname) - (*env)->ReleaseStringUTFChars(env, jname, name); - - /* bind & store the task */ - jtask_bind(jtask, task, env); - MSG_task_set_data(task, jtask); -} - -JNIEXPORT void JNICALL -Java_org_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls, - jobject jtask, - jstring jname, - jobjectArray jhosts, - jdoubleArray - jcomputeDurations_arg, - jdoubleArray - jmessageSizes_arg) -{ - - m_task_t task; /* the native parallel task to create */ - const char *name; /* the name of the task */ - int host_count; - m_host_t *hosts; - double *computeDurations; - double *messageSizes; - jdouble *jcomputeDurations; - jdouble *jmessageSizes; - - jobject jhost; - int index; - - - if (!jcomputeDurations_arg) { - jxbt_throw_null(env, - xbt_strdup - ("Parallel task compute durations cannot be null")); - return; - } - - if (!jmessageSizes_arg) { - jxbt_throw_null(env, - xbt_strdup - ("Parallel task message sizes cannot be null")); - return; - } - - if (!jname) { - jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null")); - return; - } - - host_count = (int) (*env)->GetArrayLength(env, jhosts); - - - hosts = xbt_new0(m_host_t, host_count); - computeDurations = xbt_new0(double, host_count); - messageSizes = xbt_new0(double, host_count * host_count); - - jcomputeDurations = - (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0); - jmessageSizes = - (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0); - - for (index = 0; index < host_count; index++) { - jhost = (*env)->GetObjectArrayElement(env, jhosts, index); - hosts[index] = jhost_get_native(env, jhost); - computeDurations[index] = jcomputeDurations[index]; - } - for (index = 0; index < host_count * host_count; index++) { - messageSizes[index] = jmessageSizes[index]; - } - - (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg, - jcomputeDurations, 0); - (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes, - 0); - - - /* get the C string from the java string */ - name = (*env)->GetStringUTFChars(env, jname, 0); - - task = - MSG_parallel_task_create(name, host_count, hosts, computeDurations, - messageSizes, NULL); - - (*env)->ReleaseStringUTFChars(env, jname, name); - - /* associate the java task object and the native task */ - jtask_bind(jtask, task, env); - - MSG_task_set_data(task, (void *) jtask); - - if (!MSG_task_get_data(task)) - jxbt_throw_jni(env, "global ref allocation failed"); -} - JNIEXPORT jobject JNICALL Java_org_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls, jobject jtask) @@ -754,23 +624,6 @@ Java_org_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls, MSG_task_set_priority(task, (double) priority); } -JNIEXPORT void JNICALL -Java_org_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls, - jobject jtask_arg) -{ - /* get the native task */ - m_task_t task = jtask_to_native_task(jtask_arg, env); - - if (!task) { - jxbt_throw_notbound(env, "task", task); - return; - } - MSG_error_t rv = MSG_task_destroy(task); - - jxbt_check_res("MSG_task_destroy()", rv, MSG_OK, - bprintf("unexpected error , please report this bug")); -} - JNIEXPORT void JNICALL Java_org_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls, jobject jtask) diff --git a/src/jmsg.h b/src/jmsg.h index 9fdbaebd36..d5a231fb92 100644 --- a/src/jmsg.h +++ b/src/jmsg.h @@ -189,19 +189,6 @@ Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls, JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_MsgNative_hostIsAvail (JNIEnv *, jclass, jobject); -/* - * Class simgrid_msg_Msg - * Method taskCreate - * Signature (Lsimgrid/msg/Task;Ljava/lang/String;DD)V - */ -JNIEXPORT void JNICALL Java_org_simgrid_msg_MsgNative_taskCreate - (JNIEnv *, jclass, jobject, jstring, jdouble, jdouble); - -JNIEXPORT void JNICALL -Java_org_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv *, jclass, jobject, - jstring, jobjectArray, - jdoubleArray, jdoubleArray); - /* * Class simgrid_msg_Msg * Method taskGetSender @@ -259,14 +246,6 @@ Java_org_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv *, jclass, JNIEXPORT void JNICALL Java_org_simgrid_msg_MsgNative_taskSetPriority (JNIEnv *, jclass, jobject, jdouble); -/* - * Class simgrid_msg_Msg - * Method taskDestroy - * Signature (Lsimgrid/msg/Task;)V - */ -JNIEXPORT void JNICALL Java_org_simgrid_msg_MsgNative_taskDestroy - (JNIEnv *, jclass, jobject); - /* * Class simgrid_msg_Msg * Method taskExecute diff --git a/src/jmsg_comm.c b/src/jmsg_comm.c index 374461fa5f..7ea8c1bf6c 100644 --- a/src/jmsg_comm.c +++ b/src/jmsg_comm.c @@ -66,9 +66,7 @@ Java_org_simgrid_msg_Comm_unbind(JNIEnv *env, jobject jcomm) { m_task_t *task_received; task_received = (m_task_t*) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_taskBind); - if (task_received != NULL) { - xbt_free(task_received); - } + xbt_free(task_received); comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind); MSG_comm_destroy(comm); diff --git a/src/jmsg_task.c b/src/jmsg_task.c index 20c560e8a1..573a2dc4a6 100644 --- a/src/jmsg_task.c +++ b/src/jmsg_task.c @@ -9,6 +9,7 @@ #include "jmsg.h" #include "jmsg_task.h" #include "jxbt_utilities.h" +#include "jmsg_host.h" #include XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg); @@ -47,6 +48,148 @@ Java_org_simgrid_msg_Task_nativeInit(JNIEnv *env, jclass cls) { jtask_field_Comm_receiving = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "receiving", "Z"); } +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Task_create(JNIEnv * env, + jobject jtask, jstring jname, + jdouble jcomputeDuration, + jdouble jmessageSize) +{ + m_task_t task; /* the native task to create */ + const char *name = NULL; /* the name of the task */ + + if (jcomputeDuration < 0) { + jxbt_throw_illegal(env, + bprintf + ("Task ComputeDuration (%f) cannot be negative", + (double) jcomputeDuration)); + return; + } + + if (jmessageSize < 0) { + jxbt_throw_illegal(env, + bprintf("Task MessageSize (%f) cannot be negative", + (double) jmessageSize)); + return; + } + + if (jname) { + /* get the C string from the java string */ + name = (*env)->GetStringUTFChars(env, jname, 0); + } + + + /* create the task */ + task = + MSG_task_create(name, (double) jcomputeDuration, + (double) jmessageSize, NULL); + if (jname) + (*env)->ReleaseStringUTFChars(env, jname, name); + + /* bind & store the task */ + jtask_bind(jtask, task, env); + MSG_task_set_data(task, jtask); +} + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Task_parallelCreate(JNIEnv * env, + jobject jtask, + jstring jname, + jobjectArray jhosts, + jdoubleArray + jcomputeDurations_arg, + jdoubleArray + jmessageSizes_arg) { + + m_task_t task; /* the native parallel task to create */ + const char *name; /* the name of the task */ + int host_count; + m_host_t *hosts; + double *computeDurations; + double *messageSizes; + jdouble *jcomputeDurations; + jdouble *jmessageSizes; + + jobject jhost; + int index; + + + if (!jcomputeDurations_arg) { + jxbt_throw_null(env, + xbt_strdup + ("Parallel task compute durations cannot be null")); + return; + } + + if (!jmessageSizes_arg) { + jxbt_throw_null(env, + xbt_strdup + ("Parallel task message sizes cannot be null")); + return; + } + + if (!jname) { + jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null")); + return; + } + + host_count = (int) (*env)->GetArrayLength(env, jhosts); + + + hosts = xbt_new0(m_host_t, host_count); + computeDurations = xbt_new0(double, host_count); + messageSizes = xbt_new0(double, host_count * host_count); + + jcomputeDurations = + (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0); + jmessageSizes = + (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0); + + for (index = 0; index < host_count; index++) { + jhost = (*env)->GetObjectArrayElement(env, jhosts, index); + hosts[index] = jhost_get_native(env, jhost); + computeDurations[index] = jcomputeDurations[index]; + } + for (index = 0; index < host_count * host_count; index++) { + messageSizes[index] = jmessageSizes[index]; + } + + (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg, + jcomputeDurations, 0); + (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes, + 0); + + + /* get the C string from the java string */ + name = (*env)->GetStringUTFChars(env, jname, 0); + + task = + MSG_parallel_task_create(name, host_count, hosts, computeDurations, + messageSizes, NULL); + + (*env)->ReleaseStringUTFChars(env, jname, name); + + /* associate the java task object and the native task */ + jtask_bind(jtask, task, env); + + MSG_task_set_data(task, (void *) jtask); + + if (!MSG_task_get_data(task)) + jxbt_throw_jni(env, "global ref allocation failed"); +} + +JNIEXPORT jstring JNICALL +Java_org_simgrid_msg_Task_getName(JNIEnv * env, + jobject jtask) { + m_task_t task = jtask_to_native_task(jtask, env); + + if (!task) { + jxbt_throw_notbound(env, "task", jtask); + return NULL; + } + + return (*env)->NewStringUTF(env, MSG_task_get_name(task)); +} + JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_irecv(JNIEnv * env, jclass cls, jstring jmailbox) { msg_comm_t comm; diff --git a/src/jmsg_task.h b/src/jmsg_task.h index 08771f41bc..53c11a32e8 100644 --- a/src/jmsg_task.h +++ b/src/jmsg_task.h @@ -74,13 +74,57 @@ m_task_t jtask_to_native_task(jobject jtask, JNIEnv * env); * Otherwise the function returns false. */ jboolean jtask_is_valid(jobject jtask, JNIEnv * env); +/* + * Class org_simgrid_msg_Task + * Method create + */ +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Task_create + (JNIEnv * env, jobject jtask, jstring jname, jdouble jcomputeDuration, jdouble jmessageSize); +/* + * Class org_simgrid_msg_Task + * Method parallelCreate + */ +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Task_parallelCreate + (JNIEnv *, jobject, + jstring, jobjectArray, + jdoubleArray, + jdoubleArray); +/* + * Class org_simgrid_msg_Task + * Method destroy + * Signature (Lsimgrid/msg/Task;)V + */ +JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_destroy + (JNIEnv *, jobject); +/* + * Class org_simgrid_msg_Task + * Method getName + * Signature ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_simgrid_msg_Task_getName + (JNIEnv *, jobject); +/* + * Class org_simgrid_msg_Task + * Method nativeInit + * Signature (); + */ JNIEXPORT void JNICALL Java_org_simgrid_msg_Task_nativeInit(JNIEnv *env, jclass cls); - +/** + * Class org_simgrid_msg_Task + * Method irecv + * Signature (Ljava/lang/String;)Lorg/simgrid/msg/Comm; + */ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_irecv(JNIEnv * env, jclass cls, jstring jmailbox); - +/** + * Class org_simgrid_msg_Task + * Method isend + * Signature (Lorg/simgrid/msg/Task;Ljava/lang/String;)Lorg/simgrid/msg/Comm; + */ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_isend(JNIEnv *env, jobject jtask, jstring jmailbox);