+++ /dev/null
-#include <cstring> // strlen
-#include <cstdio> // sprintf
-#include <time.h> // clock()
-#include <msg/msg.h>
-#define XBT_LOG_OLD_STYLE
-#include <xbt/log.h>
-#include "simgrid_features.h"
-
-// Creates a new log category and makes it the default
-XBT_LOG_NEW_DEFAULT_CATEGORY(simu, "Simulation messages");
-
-#define N_MBOX 5
-#define N_MESG 16
-
-// Failure exit status
-enum {
- EXIT_NO_FAILURE = 0x00, // no error
- EXIT_FAILURE_ARGS = 0x01, // bad arguments
- EXIT_FAILURE_INIT = 0x02, // failed to initialize simulator
- EXIT_FAILURE_SIMU = 0x04, // simulation failed
- EXIT_FAILURE_CLEAN = 0x08, // error at cleanup
-};
-
-int sender(int, char* [])
-{
- char mbox_stack[N_MBOX][100];
- msg_comm_t comm_stack[N_MBOX * N_MESG];
- msg_comm_t* pcomm = comm_stack;
- for (int i = 0 ; i < N_MBOX ; i++)
- sprintf(mbox_stack[i], "MBox_%02d", i);
-
- INFO0("Starting...");
- int n = 0;
- for (int i = 0 ; i < N_MBOX ; i++)
- for (int j = 0 ; j < N_MESG ; j++) {
- char task_name[100];
- const char* mailbox = mbox_stack[i];
- unsigned shift = j;
- unsigned comm_size = 1 << shift;
- m_task_t task;
-
- sprintf(task_name, "Task_%02d", n);
- task = MSG_task_create(task_name, 0, 1024.0 * comm_size, NULL);
- INFO4("At %02d, send %s, size %.0f to \"%s\"", n,
- MSG_task_get_name(task),
- MSG_task_get_data_size(task), mailbox);
- *pcomm++ = MSG_task_isend(task, mailbox);
- ++n;
- }
-
- INFO0("Wait for communications to terminate...");
- MSG_comm_waitall(comm_stack, pcomm - comm_stack, -1.0);
- if (!MSG_WAIT_DESTROYS_COMMS) {
- while (pcomm > comm_stack)
- MSG_comm_destroy(*--pcomm);
- }
-
- INFO0("Finished.");
- return 0;
-}
-
-int receiver(int, char* [])
-{
- char mbox[N_MBOX][100];
- int comm_count[N_MBOX];
- m_task_t tasks[N_MBOX];
- msg_comm_t comms[N_MBOX];
-
- for (int i = 0 ; i < N_MBOX ; i++) {
- sprintf(mbox[i], "MBox_%02d", i);
- comm_count[i] = N_MESG;
- tasks[i] = NULL;
- comms[i] = NULL;
- }
-
- INFO0("Starting...");
- xbt_dynar_t dcomms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
- for (int i = 0 ; i < N_MBOX ; i++) {
- if (comm_count[i] > 0) {
- comms[i] = MSG_task_irecv(&tasks[i], mbox[i]);
- xbt_dynar_push(dcomms, &comms[i]);
- --comm_count[i];
- }
- }
- int n = 0;
- while (!xbt_dynar_is_empty(dcomms)) {
- MSG_comm_waitany(dcomms);
- xbt_dynar_reset(dcomms);
- for (int i = 0 ; i < N_MBOX ; i++) {
- if (!comms[i])
- continue;
- if (!MSG_comm_test(comms[i])) {
- xbt_dynar_push(dcomms, &comms[i]);
- continue;
- }
- MSG_comm_destroy(comms[i]);
- comms[i] = NULL;
-
- INFO4("At %02d, received %s, size %.0f from \"%s\"", n++,
- MSG_task_get_name(tasks[i]),
- MSG_task_get_data_size(tasks[i]),
- mbox[i]);
-
- MSG_task_destroy(tasks[i]);
- tasks[i] = NULL;
-
- if (comm_count[i] > 0) {
- comms[i] = MSG_task_irecv(&tasks[i], mbox[i]);
- xbt_dynar_push(dcomms, &comms[i]);
- --comm_count[i];
- }
- }
- }
- xbt_dynar_free(&dcomms);
-
- INFO0("Finished.");
- return 0;
-}
-
-int main(int argc, char* argv[])
-{
- const char* platform_file;
- const char* application_file;
- // Note: variables used after THROW must be declared as volatile.
- volatile int exit_status; // global exit status
- volatile double simulated_time = -1.0;
- volatile clock_t start_time = clock();
- xbt_ex_t ex;
- MSG_error_t res;
-
- // Initialize some MSG internal data.
- // Note: MSG_global_init() may throw an exception, but it seems
- // impossible to catch it correctly :-(
- MSG_global_init(&argc, argv);
-
- exit_status = EXIT_FAILURE_ARGS; // =====
- TRY {
-
- // Parse global parameters
- if (argc != 3) {
- INFO1("Usage: %s platform_file application_file", argv[0]);
- THROW0(0, 0, "Failed to parse command line\n");
- }
- platform_file = argv[1];
- application_file = argv[2];
-
- INFO0(",----[ Simulation parameters ]");
- INFO1("| platform_file.....: \"%s\"", platform_file);
- INFO1("| application_file..: \"%s\"", application_file);
- INFO0("`----");
-
- exit_status = EXIT_FAILURE_INIT; // =====
-
- // Register the main functions of an agent in a global table.
- MSG_function_register("sender", sender);
- MSG_function_register("receiver", receiver);
-
- // Create the platform and the application.
- MSG_create_environment(platform_file);
- MSG_launch_application(application_file);
-
- exit_status = EXIT_FAILURE_SIMU; // =====
-
- // Launch the MSG simulation.
- INFO0("Starting simulation...");
- res = MSG_main();
- INFO0("Simulation ended.");
- simulated_time = MSG_get_clock();
- if (res != MSG_OK)
- THROW1(0, 0, "MSG_main() failed with status %#x", res);
-
- exit_status = EXIT_NO_FAILURE; // =====
- }
- CATCH (ex) {
- int len = strlen(ex.msg);
- if (len > 0 && ex.msg[len - 1] == '\n')
- len--; // strip the ending '\n'
- ERROR2("%.*s", len, ex.msg);
- DEBUG3("Error from %s() in %s:%d", ex.func, ex.file, ex.line);
- xbt_ex_free(ex);
- }
-
- // Clean the MSG simulation.
- res = MSG_clean();
- if (res != MSG_OK) {
- ERROR1("MSG_clean() failed with status %#x", res);
- exit_status |= EXIT_FAILURE_CLEAN;
- }
-
- // Report final simulation status.
- if (simulated_time >= 0.0) {
- clock_t end_time = clock();
- double simulation_time =
- (double )(end_time - start_time) / CLOCKS_PER_SEC;
- INFO0(",----[ Results ]");
- INFO1("| Total simulated time...: %g", simulated_time);
- INFO1("| Total simulation time..: %g", simulation_time);
- INFO0("`----");
- }
- if (exit_status == 0)
- INFO0("Simulation succeeded.");
- else
- ERROR1("Simulation failed (%#x).", exit_status);
-
- return exit_status;
-}
-
-// Local variables:
-// mode: c++
-// End: