Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[java] new function: msg.Comm.waitAny()
authorMartin Quinson <martin.quinson@loria.fr>
Tue, 23 Aug 2016 00:31:40 +0000 (02:31 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Tue, 23 Aug 2016 00:31:40 +0000 (02:31 +0200)
ChangeLog
src/bindings/java/jmsg_comm.cpp
src/bindings/java/jmsg_comm.h
src/bindings/java/org/simgrid/msg/Comm.java

index 2bf7ebb..1fb6ff5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -36,7 +36,7 @@ SimGrid (3.14) UNRELEASED; urgency=low
    They may become useful to some lucky ones.
    
  Java:
    They may become useful to some lucky ones.
    
  Java:
- * New function: msg.Comm.waitAll()
+ * New functions: msg.Comm.waitAll() and msg.Comm.waitAny()
  * ex/app_tokenring: new example, very similar to the MSG Token Ring
  * ex/async_waitAll: new example, on asynchronous communications
 
  * ex/app_tokenring: new example, very similar to the MSG Token Ring
  * ex/async_waitAll: new example, on asynchronous communications
 
index c15abe3..96ddecf 100644 (file)
@@ -121,12 +121,12 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_waitCompletion(JNIEnv *env, job
   }
 }
 
   }
 }
 
-JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_waitAll(JNIEnv *env, jclass cls, jobjectArray jcomms, jdouble timeout)
+static msg_comm_t* jarray_to_commArray(JNIEnv *env, jobjectArray jcomms, /* OUT */ int *count)
 {
 {
-  int count = env->GetArrayLength(jcomms);
-  msg_comm_t* comms = xbt_new(msg_comm_t, count);
+  *count = env->GetArrayLength(jcomms);
+  msg_comm_t* comms = xbt_new(msg_comm_t, *count);
 
 
-  for (int i=0; i < count; i++) {
+  for (int i=0; i < *count; i++) {
      jobject jcomm = env->GetObjectArrayElement(jcomms, i);
      if (env->ExceptionOccurred())
         break;
      jobject jcomm = env->GetObjectArrayElement(jcomms, i);
      if (env->ExceptionOccurred())
         break;
@@ -134,17 +134,36 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_waitAll(JNIEnv *env, jclass cls
      comms[i] = (msg_comm_t) (uintptr_t) env->GetLongField(jcomm, jcomm_field_Comm_bind);
      if (!comms[i]) {
        jxbt_throw_native(env,bprintf("comm at rank %d is null",i));
      comms[i] = (msg_comm_t) (uintptr_t) env->GetLongField(jcomm, jcomm_field_Comm_bind);
      if (!comms[i]) {
        jxbt_throw_native(env,bprintf("comm at rank %d is null",i));
-       return;
+       return nullptr;
      }
 
      env->DeleteLocalRef(jcomm); // reduce the load on the garbage collector: we don't need that object anymore
   }
      }
 
      env->DeleteLocalRef(jcomm); // reduce the load on the garbage collector: we don't need that object anymore
   }
+  return comms;
+}
+JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_waitAll(JNIEnv *env, jclass cls, jobjectArray jcomms, jdouble timeout)
+{
+  int count;
+  msg_comm_t* comms = jarray_to_commArray(env, jcomms, &count);
+  if (!comms)
+    return;
+
   MSG_comm_waitall(comms, count, static_cast<double>(timeout));
   xbt_free(comms);
 }
   MSG_comm_waitall(comms, count, static_cast<double>(timeout));
   xbt_free(comms);
 }
-/*
-JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_waitAny(JNIEnv *env, jobject jcomm, jarray comms)
+JNIEXPORT int JNICALL Java_org_simgrid_msg_Comm_waitAny(JNIEnv *env, jclass cls, jobjectArray jcomms)
 {
 {
+  int count;
+  msg_comm_t* comms = jarray_to_commArray(env, jcomms, &count);
+  if (!comms)
+    return -1;
+  xbt_dynar_t dyn = xbt_dynar_new(sizeof(msg_comm_t),nullptr);
+  for (int i=0; i<count; i++) {
+    xbt_dynar_push(dyn, &(comms[i]));
+  }
 
 
+  int rank = MSG_comm_waitany(dyn);
+  xbt_free(comms);
+  xbt_dynar_free(&dyn);
+  return rank;
 }
 }
-*/
index 919f8d1..7df257c 100644 (file)
@@ -23,8 +23,8 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_nativeFinalize(JNIEnv *env, job
 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm);
 
 JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_waitCompletion(JNIEnv *env, jobject jcomm, jdouble timeout);
 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm);
 
 JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_waitCompletion(JNIEnv *env, jobject jcomm, jdouble timeout);
-JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_waitAll(JNIEnv *env, jclass cls, jobjectArray comms, jdouble timeout);
-//JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_waitAny(JNIEnv *env, jobject jcomm, jarray comms);
+JNIEXPORT void JNICALL Java_org_simgrid_msg_Comm_waitAll(JNIEnv *env, jclass cls, jobjectArray jcomms, jdouble timeout);
+JNIEXPORT int JNICALL Java_org_simgrid_msg_Comm_waitAny(JNIEnv *env, jclass cls, jobjectArray jcomms);
 
 SG_END_DECL()
 #endif /* MSG_JCOMM_H */
 
 SG_END_DECL()
 #endif /* MSG_JCOMM_H */
index 16844aa..2ea34c3 100644 (file)
@@ -55,13 +55,13 @@ public class Comm {
        public native void waitCompletion(double timeout) throws TransferFailureException, HostFailureException, TimeoutException;
 
        /** Wait all of the communications */
        public native void waitCompletion(double timeout) throws TransferFailureException, HostFailureException, TimeoutException;
 
        /** Wait all of the communications */
-       public native static void waitAll(Comm[] comms, double timeout) throws TransferFailureException, HostFailureException, TimeoutException;
+       public static native void waitAll(Comm[] comms, double timeout) throws TransferFailureException, HostFailureException, TimeoutException;
        /** Wait all of the communications, with no maximal delay */
        public static void waitAll(Comm[] comms) throws TransferFailureException, HostFailureException, TimeoutException {
                waitAll(comms, -1.);
        }
        /** Wait any of the communications, and return the rank of the terminating comm */
        /** Wait all of the communications, with no maximal delay */
        public static void waitAll(Comm[] comms) throws TransferFailureException, HostFailureException, TimeoutException {
                waitAll(comms, -1.);
        }
        /** Wait any of the communications, and return the rank of the terminating comm */
-       //public native int waitAny(Comm[] comms);
+       public static native void waitAny(Comm[] comms) throws TransferFailureException, HostFailureException, TimeoutException;
        /**
         * Returns the task associated with the communication.
         * if the communication isn't finished yet, will return null.
        /**
         * Returns the task associated with the communication.
         * if the communication isn't finished yet, will return null.