Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
new java example, on asynchronous communications
authorMartin Quinson <martin.quinson@loria.fr>
Mon, 22 Aug 2016 23:58:40 +0000 (01:58 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Mon, 22 Aug 2016 23:58:40 +0000 (01:58 +0200)
.gitignore
ChangeLog
examples/java/CMakeLists.txt
examples/java/async/waitAll/Main.java [new file with mode: 0644]
examples/java/async/waitAll/Receiver.java [new file with mode: 0644]
examples/java/async/waitAll/Sender.java [new file with mode: 0644]
examples/java/async/waitAll/async_waitAll.tesh [new file with mode: 0644]

index 95982bb..11801ee 100644 (file)
@@ -975,6 +975,7 @@ examples/java/app/masterworker/java_app_masterworker_compiled
 examples/java/app/pingpong/java_app_pingpong_compiled
 examples/java/app/tokenring/java_app_tokenring_compiled
 examples/java/async/dsend/java_async_dsend_compiled
+examples/java/async/waitAll/java_async_waitAll_compiled
 examples/java/dht/chord/java_dht_chord_compiled
 examples/java/dht/kademlia/java_dht_kademlia_compiled
 examples/java/cloud/masterworker/java_cloud_masterworker_compiled
index dc40fe4..2bf7ebb 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -38,6 +38,7 @@ SimGrid (3.14) UNRELEASED; urgency=low
  Java:
  * New function: msg.Comm.waitAll()
  * ex/app_tokenring: new example, very similar to the MSG Token Ring
+ * ex/async_waitAll: new example, on asynchronous communications
 
  -- $date Da SimGrid team <simgrid-devel@lists.gforge.inria.fr>
 
index 112a950..e6b6d5a 100644 (file)
@@ -17,6 +17,9 @@ set(app_pingpong_sources       ${srcdir}/Main.java  ${srcdir}/PingPongTask.java
 set (srcdir ${CMAKE_CURRENT_SOURCE_DIR}/app/tokenring)
 set(app_tokenring_sources      ${srcdir}/Main.java ${srcdir}/RelayRunner.java)
 
+set (srcdir ${CMAKE_CURRENT_SOURCE_DIR}/async/waitAll)
+set(async_waitAll_sources      ${srcdir}/Main.java  ${srcdir}/Receiver.java ${srcdir}/Sender.java)
+
 set (srcdir ${CMAKE_CURRENT_SOURCE_DIR}/async/dsend)
 set(async_dsend_sources        ${srcdir}/Main.java  ${srcdir}/Receiver.java ${srcdir}/Sender.java)
 
@@ -70,7 +73,7 @@ set(process_suspend_sources    ${srcdir}/Main.java  ${srcdir}/DreamMaster.java
 set (srcdir ${CMAKE_CURRENT_SOURCE_DIR}/task/priority)
 set(task_priority_sources      ${srcdir}/Main.java  ${srcdir}/Test.java)
 
-foreach (example app_bittorrent app_centralizedmutex app_masterworker app_pingpong app_tokenring async_dsend
+foreach (example app_bittorrent app_centralizedmutex app_masterworker app_pingpong app_tokenring async_waitAll async_dsend
          cloud_migration cloud_masterworker dht_chord dht_kademlia energy_consumption energy_vm io_file io_storage 
          process_kill process_migration process_startkilltime process_suspend task_priority trace_pingpong)
   string (REPLACE "_" "/" example_dir ${example})
@@ -104,7 +107,7 @@ set(xml_files     ${xml_files}     ${CMAKE_CURRENT_SOURCE_DIR}/app/bittorrent/bi
                                    ${CMAKE_CURRENT_SOURCE_DIR}/task/priority/priority.xml                  PARENT_SCOPE)
 
 if(enable_java)
-  foreach (example app_bittorrent app_centralizedmutex app_masterworker app_pingpong app_tokenring async_dsend
+  foreach (example app_bittorrent app_centralizedmutex app_masterworker app_pingpong app_tokenring async_waitAll async_dsend
            cloud_migration cloud_masterworker dht_chord dht_kademlia energy_consumption energy_vm io_file io_storage 
            process_kill process_migration process_startkilltime process_suspend task_priority trace_pingpong)
     string (REPLACE "_" "/" example_dir ${example})
diff --git a/examples/java/async/waitAll/Main.java b/examples/java/async/waitAll/Main.java
new file mode 100644 (file)
index 0000000..97ee446
--- /dev/null
@@ -0,0 +1,49 @@
+/* Copyright (c) 2006-2014, 2016. 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. */
+
+package async.waitAll;
+
+/** This example demonstrates the use of the asynchrounous communications
+ * 
+ *  Task.isend() and Task.irecv() are used to start the communications in non-blocking mode.
+ *  
+ *  The sends are then blocked onto with Comm.waitCompletion(), that locks until the given 
+ *  communication terminates.
+ *  
+ *  The receives are packed into an array, and the sender blocks until all of them terminate 
+ *  with Comm.waitAll().
+ */
+
+
+import org.simgrid.msg.Msg;
+import org.simgrid.msg.Host;
+import org.simgrid.msg.NativeException;
+import org.simgrid.msg.HostNotFoundException;
+
+class Main {
+  private Main() {
+    throw new IllegalAccessError("Utility class");
+  }
+
+  public static void main(String[] args) throws NativeException, HostNotFoundException {
+    Msg.init(args);
+
+    String platform = "../platforms/small_platform.xml";
+    if (args.length >= 1) {
+       platform = args[0]; // Override the default value if passed on the command line
+    }
+
+    /* construct the platform and deploy the application */
+    Msg.createEnvironment(platform);
+    Host[] hosts = Host.all();
+    new Sender(hosts[0],"Sender").start();
+    for (int i=1; i < hosts.length; i++){
+      new Receiver(hosts[i], "Receiver").start();
+    }
+    /*  execute the simulation. */
+    Msg.run();
+  }
+}
diff --git a/examples/java/async/waitAll/Receiver.java b/examples/java/async/waitAll/Receiver.java
new file mode 100644 (file)
index 0000000..84444fa
--- /dev/null
@@ -0,0 +1,34 @@
+/* Copyright (c) 2006-2007, 2010, 2013-2014, 2016. 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. */
+
+package async.waitAll;
+import org.simgrid.msg.Msg;
+import org.simgrid.msg.Comm;
+import org.simgrid.msg.Host;
+import org.simgrid.msg.Task;
+import org.simgrid.msg.Process;
+import org.simgrid.msg.HostFailureException;
+import org.simgrid.msg.TimeoutException;
+import org.simgrid.msg.TransferFailureException;
+
+public class Receiver extends Process {
+  public Receiver (Host host, String name) {
+    super(host,name);
+  }
+
+  public void main(String[] args) throws TransferFailureException, HostFailureException, TimeoutException {
+    Comm comm = Task.irecv(getHost().getName());
+    Msg.info("I started receiving on '"+ getHost().getName() +". Wait 0.1 second, and block on the communication.");
+    waitFor(0.1);
+    try {
+       comm.waitCompletion();
+    } catch (TimeoutException e) {
+       Msg.info("Timeout while waiting for my task");
+       return; // Stop this process
+    }
+    Msg.info("I got my task, good bye.");
+  }
+}
\ No newline at end of file
diff --git a/examples/java/async/waitAll/Sender.java b/examples/java/async/waitAll/Sender.java
new file mode 100644 (file)
index 0000000..eea0a76
--- /dev/null
@@ -0,0 +1,41 @@
+/* Copyright (c) 2006-2014, 2016. 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. */
+
+package async.waitAll;
+
+import org.simgrid.msg.Comm;
+import org.simgrid.msg.Host;
+import org.simgrid.msg.Msg;
+import org.simgrid.msg.MsgException;
+import org.simgrid.msg.Process;
+import org.simgrid.msg.Task;
+
+public class Sender extends Process {
+  public Sender (Host host, String name){
+    super(host,name);
+  }
+
+  public void main(String[] args) throws MsgException {
+    double taskComputeSize =0;
+    double taskCommunicateSize = 5000000;
+    Host[] hosts = Host.all();
+    int receiverCount = hosts.length - 1;
+
+    Msg.info("I have "+ receiverCount + " receivers to contact");
+
+    Comm[] communicators = new Comm[receiverCount];
+    for (int i = 1; i <= receiverCount; i++) {
+      Task task = new Task("Task_" + i, taskComputeSize, taskCommunicateSize); 
+      Msg.info("Start the Sending '" + task.getName()+ "' to '" + hosts[i].getName() + "'");
+      communicators[i-1] = task.isend(hosts[i].getName());
+    }
+
+    Msg.info("All tasks have been (asynchronously) dispatched. Let's wait for their completion.");
+    Comm.waitAll(communicators);
+
+    Msg.info("Goodbye now!");
+  }
+}
diff --git a/examples/java/async/waitAll/async_waitAll.tesh b/examples/java/async/waitAll/async_waitAll.tesh
new file mode 100644 (file)
index 0000000..666cccf
--- /dev/null
@@ -0,0 +1,26 @@
+#! tesh
+
+$ java -classpath ${classpath:=.} async/waitAll/Main ${srcdir:=.}/../platforms/small_platform.xml "--log=root.fmt:[%10.6r]%e(%i:%P@%h)%e%m%n"
+> [  0.000000] (0:maestro@) Using regular java threads.
+> [  0.000000] (1:Sender@Jacquelin) I have 6 receivers to contact
+> [  0.000000] (1:Sender@Jacquelin) Start the Sending 'Task_1' to 'Fafard'
+> [  0.000000] (1:Sender@Jacquelin) Start the Sending 'Task_2' to 'Tremblay'
+> [  0.000000] (2:Receiver@Fafard) I started receiving on 'Fafard. Wait 0.1 second, and block on the communication.
+> [  0.000000] (3:Receiver@Tremblay) I started receiving on 'Tremblay. Wait 0.1 second, and block on the communication.
+> [  0.000000] (4:Receiver@Bourassa) I started receiving on 'Bourassa. Wait 0.1 second, and block on the communication.
+> [  0.000000] (5:Receiver@Jupiter) I started receiving on 'Jupiter. Wait 0.1 second, and block on the communication.
+> [  0.000000] (6:Receiver@Boivin) I started receiving on 'Boivin. Wait 0.1 second, and block on the communication.
+> [  0.000000] (7:Receiver@Ginette) I started receiving on 'Ginette. Wait 0.1 second, and block on the communication.
+> [  0.000000] (1:Sender@Jacquelin) Start the Sending 'Task_3' to 'Bourassa'
+> [  0.000000] (1:Sender@Jacquelin) Start the Sending 'Task_4' to 'Jupiter'
+> [  0.000000] (1:Sender@Jacquelin) Start the Sending 'Task_5' to 'Boivin'
+> [  0.000000] (1:Sender@Jacquelin) Start the Sending 'Task_6' to 'Ginette'
+> [  0.000000] (1:Sender@Jacquelin) All tasks have been (asynchronously) dispatched. Let's wait for their completion.
+> [  6.801793] (4:Receiver@Bourassa) I got my task, good bye.
+> [  6.801793] (5:Receiver@Jupiter) I got my task, good bye.
+> [  6.801793] (7:Receiver@Ginette) I got my task, good bye.
+> [ 11.931226] (3:Receiver@Tremblay) I got my task, good bye.
+> [ 12.364875] (2:Receiver@Fafard) I got my task, good bye.
+> [ 12.575806] (6:Receiver@Boivin) I got my task, good bye.
+> [ 12.575806] (1:Sender@Jacquelin) Goodbye now!
+> [ 12.575806] (0:maestro@) MSG_main finished; Cleaning up the simulation...