Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
s4u::Semaphore: use a simpler example with no mutex
authorMartin Quinson <martin.quinson@loria.fr>
Mon, 1 Oct 2018 21:01:36 +0000 (23:01 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Mon, 1 Oct 2018 21:01:40 +0000 (23:01 +0200)
Sorry Millian, I'm a psychopath.

examples/s4u/synchro-semaphore/s4u-synchro-semaphore.cpp
examples/s4u/synchro-semaphore/s4u-synchro-semaphore.tesh

index 8c96391..71532b8 100644 (file)
@@ -3,64 +3,51 @@
 /* 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. */
 
+// This example implements a simple producer/consumer schema,
+// passing a bunch of items from one to the other
+
 #include "simgrid/s4u.hpp"
 
 #include <memory>
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
 
-// This example implements a one-time use barrier with 1 semaphore and 1 mutex.
+const char* buffer;                                                        /* Where the data is exchanged */
+simgrid::s4u::SemaphorePtr sem_empty = simgrid::s4u::Semaphore::create(1); /* indicates whether the buffer is empty */
+simgrid::s4u::SemaphorePtr sem_full  = simgrid::s4u::Semaphore::create(0); /* indicates whether the buffer is full */
+;
 
-static void worker(simgrid::s4u::SemaphorePtr semaphore, simgrid::s4u::MutexPtr mutex, int process_count, std::shared_ptr<int> count)
+static void producer(std::vector<std::string>* args)
 {
-  mutex->lock();
-  XBT_INFO("Got mutex. Incrementing count.");
-  XBT_INFO("Count is %d", *count);
-  *count = (*count) + 1;
-  XBT_INFO("Count is now %d. Process count is %d.", *count, process_count);
-
-  if (*count == process_count) {
-    XBT_INFO("Releasing the semaphore %d times.", process_count-1);
-    for (int i = 0; i < process_count-1; i++) {
-      semaphore->release();
-    }
-
-    XBT_INFO("Releasing mutex.");
-    mutex->unlock();
-  }
-  else {
-    XBT_INFO("Releasing mutex.");
-    mutex->unlock();
-    XBT_INFO("Acquiring semaphore.");
-    semaphore->acquire();
+  for (auto str : *args) {
+    sem_empty->acquire();
+    XBT_INFO("Pushing '%s'", str.c_str());
+    buffer = str.c_str();
+    sem_full->release();
   }
 
   XBT_INFO("Bye!");
 }
-
-static void master(unsigned int process_count)
+static void consumer()
 {
-  simgrid::s4u::SemaphorePtr semaphore = simgrid::s4u::Semaphore::create(0);
-  simgrid::s4u::MutexPtr mutex = simgrid::s4u::Mutex::create();
-  std::shared_ptr<int> count(new int);
-  *count = 0;
+  std::string str;
+  do {
+    sem_full->acquire();
+    str = buffer;
+    XBT_INFO("Receiving '%s'", str.c_str());
+    sem_empty->release();
+  } while (str != "");
 
-  XBT_INFO("Spawning %d workers", process_count);
-  for (unsigned int i = 0; i < process_count; i++) {
-    simgrid::s4u::Actor::create("worker", simgrid::s4u::Host::by_name("Jupiter"), worker, semaphore, mutex, process_count, count);
-  }
+  XBT_INFO("Bye!");
 }
 
 int main(int argc, char **argv)
 {
-  // Parameter: Number of processes in the barrier
-  xbt_assert(argc >= 2, "Usage: %s <process-count>\n", argv[0]);
-  unsigned int process_count = std::stoi(argv[1]);
-  xbt_assert(process_count > 0, "<process-count> must be greater than 0");
-
+  std::vector<std::string> args = std::vector<std::string>({"one", "two", "three", ""});
   simgrid::s4u::Engine e(&argc, argv);
   e.load_platform("../../platforms/two_hosts.xml");
-  simgrid::s4u::Actor::create("master", simgrid::s4u::Host::by_name("Tremblay"), master, process_count);
+  simgrid::s4u::Actor::create("producer", simgrid::s4u::Host::by_name("Tremblay"), producer, &args);
+  simgrid::s4u::Actor::create("consumer", simgrid::s4u::Host::by_name("Jupiter"), consumer);
   e.run();
 
   return 0;
index 477a7a0..d740170 100644 (file)
 #!/usr/bin/env tesh
 
-$ $SG_TEST_EXENV ${bindir:=.}/s4u-synchro-semaphore 1
-> [Tremblay:master:(1) 0.000000] [s4u_test/INFO] Spawning 1 workers
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Got mutex. Incrementing count.
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Count is 0
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Count is now 1. Process count is 1.
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Releasing the semaphore 0 times.
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Releasing mutex.
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Bye!
-
-$ $SG_TEST_EXENV ${bindir:=.}/s4u-synchro-semaphore 2
-> [Tremblay:master:(1) 0.000000] [s4u_test/INFO] Spawning 2 workers
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Got mutex. Incrementing count.
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Count is 0
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Count is now 1. Process count is 2.
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Releasing mutex.
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Acquiring semaphore.
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Got mutex. Incrementing count.
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Count is 1
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Count is now 2. Process count is 2.
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Releasing the semaphore 1 times.
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Bye!
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Releasing mutex.
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Bye!
-
-$ $SG_TEST_EXENV ${bindir:=.}/s4u-synchro-semaphore 3
-> [Tremblay:master:(1) 0.000000] [s4u_test/INFO] Spawning 3 workers
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Got mutex. Incrementing count.
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Count is 0
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Count is now 1. Process count is 3.
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Releasing mutex.
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Acquiring semaphore.
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Got mutex. Incrementing count.
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Count is 1
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Count is now 2. Process count is 3.
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Releasing mutex.
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Acquiring semaphore.
-> [Jupiter:worker:(4) 0.000000] [s4u_test/INFO] Got mutex. Incrementing count.
-> [Jupiter:worker:(4) 0.000000] [s4u_test/INFO] Count is 2
-> [Jupiter:worker:(4) 0.000000] [s4u_test/INFO] Count is now 3. Process count is 3.
-> [Jupiter:worker:(4) 0.000000] [s4u_test/INFO] Releasing the semaphore 2 times.
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Bye!
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Bye!
-> [Jupiter:worker:(4) 0.000000] [s4u_test/INFO] Releasing mutex.
-> [Jupiter:worker:(4) 0.000000] [s4u_test/INFO] Bye!
-
-$ $SG_TEST_EXENV ${bindir:=.}/s4u-synchro-semaphore 10
-> [Tremblay:master:(1) 0.000000] [s4u_test/INFO] Spawning 10 workers
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Got mutex. Incrementing count.
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Count is 0
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Count is now 1. Process count is 10.
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Releasing mutex.
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Acquiring semaphore.
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Got mutex. Incrementing count.
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Count is 1
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Count is now 2. Process count is 10.
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Releasing mutex.
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Acquiring semaphore.
-> [Jupiter:worker:(4) 0.000000] [s4u_test/INFO] Got mutex. Incrementing count.
-> [Jupiter:worker:(4) 0.000000] [s4u_test/INFO] Count is 2
-> [Jupiter:worker:(4) 0.000000] [s4u_test/INFO] Count is now 3. Process count is 10.
-> [Jupiter:worker:(4) 0.000000] [s4u_test/INFO] Releasing mutex.
-> [Jupiter:worker:(4) 0.000000] [s4u_test/INFO] Acquiring semaphore.
-> [Jupiter:worker:(5) 0.000000] [s4u_test/INFO] Got mutex. Incrementing count.
-> [Jupiter:worker:(5) 0.000000] [s4u_test/INFO] Count is 3
-> [Jupiter:worker:(5) 0.000000] [s4u_test/INFO] Count is now 4. Process count is 10.
-> [Jupiter:worker:(5) 0.000000] [s4u_test/INFO] Releasing mutex.
-> [Jupiter:worker:(5) 0.000000] [s4u_test/INFO] Acquiring semaphore.
-> [Jupiter:worker:(6) 0.000000] [s4u_test/INFO] Got mutex. Incrementing count.
-> [Jupiter:worker:(6) 0.000000] [s4u_test/INFO] Count is 4
-> [Jupiter:worker:(6) 0.000000] [s4u_test/INFO] Count is now 5. Process count is 10.
-> [Jupiter:worker:(6) 0.000000] [s4u_test/INFO] Releasing mutex.
-> [Jupiter:worker:(6) 0.000000] [s4u_test/INFO] Acquiring semaphore.
-> [Jupiter:worker:(7) 0.000000] [s4u_test/INFO] Got mutex. Incrementing count.
-> [Jupiter:worker:(7) 0.000000] [s4u_test/INFO] Count is 5
-> [Jupiter:worker:(7) 0.000000] [s4u_test/INFO] Count is now 6. Process count is 10.
-> [Jupiter:worker:(7) 0.000000] [s4u_test/INFO] Releasing mutex.
-> [Jupiter:worker:(7) 0.000000] [s4u_test/INFO] Acquiring semaphore.
-> [Jupiter:worker:(8) 0.000000] [s4u_test/INFO] Got mutex. Incrementing count.
-> [Jupiter:worker:(8) 0.000000] [s4u_test/INFO] Count is 6
-> [Jupiter:worker:(8) 0.000000] [s4u_test/INFO] Count is now 7. Process count is 10.
-> [Jupiter:worker:(8) 0.000000] [s4u_test/INFO] Releasing mutex.
-> [Jupiter:worker:(8) 0.000000] [s4u_test/INFO] Acquiring semaphore.
-> [Jupiter:worker:(9) 0.000000] [s4u_test/INFO] Got mutex. Incrementing count.
-> [Jupiter:worker:(9) 0.000000] [s4u_test/INFO] Count is 7
-> [Jupiter:worker:(9) 0.000000] [s4u_test/INFO] Count is now 8. Process count is 10.
-> [Jupiter:worker:(9) 0.000000] [s4u_test/INFO] Releasing mutex.
-> [Jupiter:worker:(9) 0.000000] [s4u_test/INFO] Acquiring semaphore.
-> [Jupiter:worker:(10) 0.000000] [s4u_test/INFO] Got mutex. Incrementing count.
-> [Jupiter:worker:(10) 0.000000] [s4u_test/INFO] Count is 8
-> [Jupiter:worker:(10) 0.000000] [s4u_test/INFO] Count is now 9. Process count is 10.
-> [Jupiter:worker:(10) 0.000000] [s4u_test/INFO] Releasing mutex.
-> [Jupiter:worker:(10) 0.000000] [s4u_test/INFO] Acquiring semaphore.
-> [Jupiter:worker:(11) 0.000000] [s4u_test/INFO] Got mutex. Incrementing count.
-> [Jupiter:worker:(11) 0.000000] [s4u_test/INFO] Count is 9
-> [Jupiter:worker:(11) 0.000000] [s4u_test/INFO] Count is now 10. Process count is 10.
-> [Jupiter:worker:(11) 0.000000] [s4u_test/INFO] Releasing the semaphore 9 times.
-> [Jupiter:worker:(2) 0.000000] [s4u_test/INFO] Bye!
-> [Jupiter:worker:(3) 0.000000] [s4u_test/INFO] Bye!
-> [Jupiter:worker:(4) 0.000000] [s4u_test/INFO] Bye!
-> [Jupiter:worker:(5) 0.000000] [s4u_test/INFO] Bye!
-> [Jupiter:worker:(6) 0.000000] [s4u_test/INFO] Bye!
-> [Jupiter:worker:(7) 0.000000] [s4u_test/INFO] Bye!
-> [Jupiter:worker:(8) 0.000000] [s4u_test/INFO] Bye!
-> [Jupiter:worker:(9) 0.000000] [s4u_test/INFO] Bye!
-> [Jupiter:worker:(10) 0.000000] [s4u_test/INFO] Bye!
-> [Jupiter:worker:(11) 0.000000] [s4u_test/INFO] Releasing mutex.
-> [Jupiter:worker:(11) 0.000000] [s4u_test/INFO] Bye!
+$ $SG_TEST_EXENV ${bindir:=.}/s4u-synchro-semaphore
+> [Tremblay:producer:(1) 0.000000] [s4u_test/INFO] Pushing 'one'
+> [Jupiter:consumer:(2) 0.000000] [s4u_test/INFO] Receiving 'one'
+> [Tremblay:producer:(1) 0.000000] [s4u_test/INFO] Pushing 'two'
+> [Jupiter:consumer:(2) 0.000000] [s4u_test/INFO] Receiving 'two'
+> [Tremblay:producer:(1) 0.000000] [s4u_test/INFO] Pushing 'three'
+> [Jupiter:consumer:(2) 0.000000] [s4u_test/INFO] Receiving 'three'
+> [Tremblay:producer:(1) 0.000000] [s4u_test/INFO] Pushing ''
+> [Jupiter:consumer:(2) 0.000000] [s4u_test/INFO] Receiving ''
+> [Tremblay:producer:(1) 0.000000] [s4u_test/INFO] Bye!
+> [Jupiter:consumer:(2) 0.000000] [s4u_test/INFO] Bye!