/* 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 <mutex>
+
#include "simgrid/msg.h"
#include "simgrid/simix.h" /* semaphores for the barrier */
#include <xbt/replay.h>
-#include "simgrid/s4u.h"
+#include "simgrid/s4u.hpp"
XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
int communicator_size = 0;
typedef struct {
int last_Irecv_sender_id;
int bcast_counter;
- xbt_dynar_t isends; /* of msg_comm_t */
/* Used to implement irecv+wait */
+ xbt_dynar_t isends; /* of msg_comm_t */
xbt_dynar_t irecvs; /* of msg_comm_t */
- xbt_dynar_t tasks; /* of msg_task_t */
+ std::vector<msg_task_t> tasks;
} s_process_globals_t, *process_globals_t;
-/* Helper function */
-static double parse_double(const char *string)
-{
- double value;
- char *endptr;
-
- value = strtod(string, &endptr);
- if (*endptr != '\0')
- THROWF(unknown_error, 0, "%s is not a double", string);
- return value;
-}
#define ACT_DEBUG(...) \
if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) { \
}
}
-static void asynchronous_cleanup(void)
+static void asynchronous_cleanup()
{
- process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
+ process_globals_t globals = static_cast<process_globals_t>( MSG_process_get_data(MSG_process_self()) );
/* Destroy any isend which correspond to completed communications */
- int found;
msg_comm_t comm;
- while ((found = MSG_comm_testany(globals->isends)) != -1) {
- xbt_dynar_remove_at(globals->isends, found, &comm);
+ while (true) {
+ int pos_found = MSG_comm_testany(globals->isends);
+ if (pos_found == -1) /* none remaining */
+ break;
+ xbt_dynar_remove_at(globals->isends, pos_found, &comm);
MSG_comm_destroy(comm);
}
}
static void action_send(const char *const *action)
{
char to[250];
- const char *size_str = action[3];
- double size = parse_double(size_str);
+ double size = xbt_str_parse_double(action[3], "%s is not a double");
double clock = MSG_get_clock();
snprintf(to,249, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
static void action_Isend(const char *const *action)
{
char to[250];
- const char *size = action[3];
+ double size = xbt_str_parse_double(action[3], "%s is not a double");
double clock = MSG_get_clock();
- process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
+ process_globals_t globals = static_cast<process_globals_t>( MSG_process_get_data(MSG_process_self()) );
snprintf(to,249, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
- msg_comm_t comm = MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
+ msg_comm_t comm = MSG_task_isend(MSG_task_create(to, 0, size, NULL), to);
xbt_dynar_push(globals->isends, &comm);
XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
{
char mailbox[250];
double clock = MSG_get_clock();
- process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
+ process_globals_t globals = static_cast<process_globals_t>( MSG_process_get_data(MSG_process_self()) );
XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
snprintf(mailbox,249, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
- msg_task_t t = NULL;
- xbt_dynar_push(globals->tasks, &t);
- msg_comm_t c = MSG_task_irecv((msg_task**)xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks) - 1), mailbox);
+ globals->tasks.push_back(nullptr);
+ msg_comm_t c = MSG_task_irecv(&globals->tasks.back(), mailbox);
xbt_dynar_push(globals->irecvs, &c);
-
log_action(action, MSG_get_clock() - clock);
asynchronous_cleanup();
}
static void action_wait(const char *const *action)
{
- msg_task_t task = NULL;
msg_comm_t comm;
double clock = MSG_get_clock();
- process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
+ process_globals_t globals = static_cast<process_globals_t>( MSG_process_get_data(MSG_process_self()) );
xbt_assert(xbt_dynar_length(globals->irecvs), "action wait not preceded by any irecv: %s",
xbt_str_join_array(action, " "));
ACT_DEBUG("Entering %s", NAME);
comm = xbt_dynar_pop_as(globals->irecvs, msg_comm_t);
MSG_comm_wait(comm, -1);
- task = xbt_dynar_pop_as(globals->tasks, msg_task_t);
- MSG_comm_destroy(comm);
+ msg_task_t task = globals->tasks.back();
MSG_task_destroy(task);
+ globals->tasks.pop_back();
+ MSG_comm_destroy(comm);
log_action(action, MSG_get_clock() - clock);
}
/* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
static void action_barrier(const char *const *action)
{
- // static smx_mutex_t mutex = NULL;
- // static smx_cond_t cond = NULL;
- static simgrid::s4u::Mutex *mutex = NULL;
- static simgrid::s4u::ConditionVariable *cond = NULL;
+ static simgrid::s4u::MutexPtr mutex = nullptr;
+ static simgrid::s4u::ConditionVariablePtr cond = nullptr;
static int processes_arrived_sofar = 0;
- if (mutex == NULL) { // first arriving on the barrier
- mutex = new simgrid::s4u::Mutex();
- cond = new simgrid::s4u::ConditionVariable();
+ if (mutex == nullptr) { // first arriving on the barrier
+ mutex = simgrid::s4u::Mutex::createMutex();
+ cond = simgrid::s4u::ConditionVariable::createConditionVariable();
processes_arrived_sofar = 0;
}
ACT_DEBUG("Entering barrier: %s (%d already there)", NAME, processes_arrived_sofar);
- mutex->lock();
- if (++processes_arrived_sofar == communicator_size) {
- cond->notify_all();
- mutex->unlock();
- } else {
- cond->wait(mutex);
- mutex->unlock();
+
+ {
+ std::unique_lock<simgrid::s4u::Mutex> lock(*mutex);
+ if (++processes_arrived_sofar == communicator_size) {
+ // We can notify without the lock:
+ lock.unlock();
+ cond->notify_all();
+ } else {
+ cond->wait(lock);
+ }
}
ACT_DEBUG("Exiting barrier: %s", NAME);
processes_arrived_sofar--;
if (processes_arrived_sofar<=0) {
- delete cond;
- delete mutex;
- mutex = NULL;
+ cond = nullptr;
+ mutex = nullptr;
}
}
static void action_bcast(const char *const *action)
{
char mailbox[80];
- double comm_size = parse_double(action[2]);
+ double comm_size = xbt_str_parse_double(action[2], "%s is not a double");
msg_task_t task = NULL;
double clock = MSG_get_clock();
- process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
+ process_globals_t counters = static_cast<process_globals_t>( MSG_process_get_data(MSG_process_self()) );
xbt_assert(communicator_size, "Size of Communicator is not defined, can't use collective operations");
static void action_comm_size(const char *const *action)
{
- const char *size = action[2];
double clock = MSG_get_clock();
- communicator_size = parse_double(size);
+ communicator_size = xbt_str_parse_int(action[2], "%s is not an int");
log_action(action, MSG_get_clock() - clock);
}
static void action_compute(const char *const *action)
{
- const char *amount = action[2];
- msg_task_t task = MSG_task_create("task", parse_double(amount), 0, NULL);
+ double amount = xbt_str_parse_double(action[2], "%s is not a double");
+ msg_task_t task = MSG_task_create("task", amount, 0, NULL);
double clock = MSG_get_clock();
ACT_DEBUG("Entering %s", NAME);
static void action_init(const char *const *action)
{
XBT_DEBUG("Initialize the counters");
- process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
+ process_globals_t globals = static_cast<process_globals_t>( calloc(1, sizeof(s_process_globals_t)) );
globals->isends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
- globals->tasks = xbt_dynar_new(sizeof(msg_task_t), NULL);
MSG_process_set_data(MSG_process_self(), globals);
}
static void action_finalize(const char *const *action)
{
- process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
+ process_globals_t globals = static_cast<process_globals_t>( MSG_process_get_data(MSG_process_self()) );
if (globals) {
asynchronous_cleanup();
xbt_dynar_free_container(&(globals->isends));
xbt_dynar_free_container(&(globals->irecvs));
- xbt_dynar_free_container(&(globals->tasks));
xbt_free(globals);
}
}