Exemplo n.º 1
0
static int master(int argc, char *argv[])
{
  long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
  double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
  double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
  long workers_count = xbt_str_parse_int(argv[4], "Invalid amount of workers: %s");

  //setting the variable "is_master" (previously declared) to value 1
  TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "is_master", 1);

  TRACE_mark("msmark", "start_send_tasks");
  for (int i = 0; i < number_of_tasks; i++) {
    msg_task_t task = NULL;
    task = MSG_task_create("task", task_comp_size, task_comm_size, NULL);

    //setting the variable "task_creation" to value i
    TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "task_creation", i);

    //setting the category of task to "compute"
    //the category of a task must be defined before it is sent or executed
    MSG_task_set_category(task, "compute");
    MSG_task_send(task, "master_mailbox");
  }
  TRACE_mark("msmark", "finish_send_tasks");

  for (int i = 0; i < workers_count; i++) {
    msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
    MSG_task_set_category(finalize, "finalize");
    MSG_task_send(finalize, "master_mailbox");
  }

  return 0;
}
Exemplo n.º 2
0
static int master(int argc, char *argv[])
{
  long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
  long workers_count = xbt_str_parse_int(argv[4], "Invalid amount of workers: %s");

  for (int i = 0; i < number_of_tasks; i++) {
    msg_task_t task = NULL;

    /* creating task and setting its category */
    if (i % 2) {
      task = MSG_task_create("task_compute", 10000000, 0, NULL);
      MSG_task_set_category(task, "compute");
    } else if (i % 3) {
      task = MSG_task_create("task_request", 10, 10, NULL);
      MSG_task_set_category(task, "request");
    } else {
      task = MSG_task_create("task_data", 10, 10000000, NULL);
      MSG_task_set_category(task, "data");
    }
    MSG_task_send(task, "master_mailbox");
  }

  for (int i = 0; i < workers_count; i++) {
    msg_task_t finalize = MSG_task_create("finalize", 0, 1000, 0);
    MSG_task_set_category(finalize, "finalize");
    MSG_task_send(finalize, "master_mailbox");
  }

  return 0;
}
Exemplo n.º 3
0
int job_requester(int argc, char* argv[]){
	/**
	   @type simgrid process (run on all tiers)
		Every @timeout checks amount of free (non running) cores.
		If this amount is greater than some N then it sends a job request
		 to scheduler to get new batch of jobs. 

         Simgrid process parameters:
         --------------------------
         None		 
	*/
    std::string host_name = MSG_host_get_name(MSG_host_self());
    std::string CERN = "CERN";
    msg_task_t task = NULL;
    double timeout = 1000;
    long freeCoreAmount;
    int fullCoreAmount = MSG_host_get_core_number(MSG_host_self());
    MSG_process_sleep(0.01);

    while (1){
        freeCoreAmount = fullCoreAmount - xbt_str_parse_int(MSG_host_get_property_value(MSG_host_self(), "activeCore"), "error") -
                xbt_str_parse_int(MSG_host_get_property_value(MSG_host_self(), "corruptedCore"), "error");

        MSG_sem_acquire(sem_requester);
        if (GLOBAL_QUEUE->empty()){
            MSG_sem_release(sem_requester);
            break;
        }

        if (freeCoreAmount > 0){
            JobBatchRequest* jobRequest = new JobBatchRequest;
            jobRequest->coreAmount = freeCoreAmount;

            task = MSG_task_create("request", 0.0, MESSAGES_SIZE, jobRequest);

            plusLinkCounter(host_name, CERN);
            msg_error_t err = MSG_task_send(task, "scheduler");

            switch(err){
                case MSG_OK:
                    minusLinkCounter(host_name, CERN);
                    break;
                case MSG_TRANSFER_FAILURE:
                    minusLinkCounter(host_name, CERN);
                    MSG_task_destroy(task);
                    task = NULL;
                    break;
                case MSG_HOST_FAILURE:
                    MSG_task_destroy(task);
                    task = NULL;
                    break;
            }
        }else MSG_sem_release(sem_requester);

        MSG_process_sleep(timeout);
    }
    return 0;
}
Exemplo n.º 4
0
/* Sender process expects 6 arguments: */
static int sender(int argc, char *argv[])
{
  long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");        /* - number of tasks */
  double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /* - computational cost */
  double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /* - communication cost */
  long receivers_count = xbt_str_parse_int(argv[4], "Invalid amount of receivers: %s");    /* - number of receivers */
  double sleep_start_time = xbt_str_parse_double(argv[5], "Invalid sleep start time: %s"); /* - start time */
  double sleep_test_time = xbt_str_parse_double(argv[6], "Invalid test time: %s");         /* - test time */

  XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);

  int i;
  msg_task_t task = NULL;
  msg_comm_t comm = NULL;
  MSG_process_sleep(sleep_start_time);
  for (i = 0; i < number_of_tasks; i++) {
    char mailbox[256];
    char snprintf_buffer[256];

    snprintf(mailbox,255, "receiver-%ld", i % receivers_count);
    snprintf(snprintf_buffer,255, "Task_%d", i);

    /* This process first creates a task and send it asynchronously with @ref MSG_task_isend. Then, if: */
    task = MSG_task_create(snprintf_buffer, task_comp_size, task_comm_size, NULL);
    comm = MSG_task_isend(task, mailbox);
    XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);

    if (sleep_test_time > 0) { /* - "test_time" is set to 0, wait on @ref MSG_comm_wait */
      while (MSG_comm_test(comm) == 0) { /* - Call @ref MSG_comm_test every "test_time" otherwise */
        MSG_process_sleep(sleep_test_time);
      };
    } else {
      MSG_comm_wait(comm, -1);
    }
    MSG_comm_destroy(comm);
  }

  for (i = 0; i < receivers_count; i++) {
    char mailbox[80];
    snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
    task = MSG_task_create("finalize", 0, 0, 0);
    comm = MSG_task_isend(task, mailbox);
    XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
    if (sleep_test_time > 0) {
      while (MSG_comm_test(comm) == 0) {
        MSG_process_sleep(sleep_test_time);
      }
    } else {
      MSG_comm_wait(comm, -1);
    }
    MSG_comm_destroy(comm);
  }

  XBT_INFO("Goodbye now!");
  return 0;
}
Exemplo n.º 5
0
int smpi_global_size(void)
{
  char *value = getenv("SMPI_GLOBAL_SIZE");
  xbt_assert(value,"Please set env var SMPI_GLOBAL_SIZE to the expected number of processes.");

  return xbt_str_parse_int(value, "SMPI_GLOBAL_SIZE contains a non-numerical value: %s");
}
Exemplo n.º 6
0
static int worker_main(int argc, char *argv[])
{
  double computation_amount = xbt_str_parse_double(argv[1], "Invalid computation amount: %s");
  int use_bound = xbt_str_parse_int(argv[2], "Second parameter (use_bound) should be 0 or 1 but is: %s");
  double bound = xbt_str_parse_double(argv[3], "Invalid bound: %s");

  double clock_sta = MSG_get_clock();

  msg_task_t task = MSG_task_create("Task", computation_amount, 0, NULL);
  if (use_bound)
     MSG_task_set_bound(task, bound);
  MSG_task_execute(task);
  MSG_task_destroy(task);

  double clock_end = MSG_get_clock();
  double duration = clock_end - clock_sta;
  double flops_per_sec = computation_amount / duration;

  if (use_bound)
    XBT_INFO("bound to %f => duration %f (%f flops/s)", bound, duration, flops_per_sec);
  else
    XBT_INFO("not bound => duration %f (%f flops/s)", duration, flops_per_sec);

  return 0;
}
Exemplo n.º 7
0
static int sender(int argc, char *argv[])
{
  xbt_assert(argc==6, "This function expects 5 parameters from the XML deployment file");
  long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
  double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
  double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
  long receivers_count = xbt_str_parse_int(argv[4], "Invalid amount of receivers: %s");
  int diff_com = xbt_str_parse_int(argv[5], "Invalid value for diff_comm: %s");

  xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
  /* First pack the communications in the dynar */
  for (int i = 0; i < number_of_tasks; i++) {
    double coef = (diff_com == 0) ? 1 : (i + 1);

    char mailbox[80];
    char taskname[80];
    snprintf(mailbox,79, "receiver-%ld", (i % receivers_count));
    snprintf(taskname,79, "Task_%d", i);
    msg_task_t task = MSG_task_create(taskname, task_comp_size, task_comm_size / coef, NULL);
    msg_comm_t comm = MSG_task_isend(task, mailbox);
    xbt_dynar_push_as(comms, msg_comm_t, comm);
    XBT_INFO("Send to receiver-%ld %s comm_size %f", i % receivers_count, taskname, task_comm_size / coef);
  }
   
  /* Here we are waiting for the completion of all communications */
  while (xbt_dynar_is_empty(comms) == 0) {
    msg_comm_t comm;
    xbt_dynar_remove_at(comms, MSG_comm_waitany(comms), &comm);
    MSG_comm_destroy(comm);
  }
  xbt_dynar_free(&comms);

  /* Here we are waiting for the completion of all tasks */
  for (int i = 0; i < receivers_count; i++) {
    msg_task_t task = NULL;
    msg_comm_t comm = MSG_task_irecv(&task, "finalize");
    msg_error_t res_wait = MSG_comm_wait(comm, -1);
    xbt_assert(res_wait == MSG_OK, "MSG_comm_wait failed");
    MSG_comm_destroy(comm);
    MSG_task_destroy(task);
  }

  XBT_INFO("Goodbye now!");
  return 0;
}
Exemplo n.º 8
0
/**
 * \brief Main function.
 */
int main(int argc, char *argv[])
{
  MSG_init(&argc, argv);
  xbt_assert(argc > 2, 
       "Usage: %s [-nb_bits=n] [-timeout=t] platform_file deployment_file\n"
       "\tExample: %s ../msg_platform.xml pastry10.xml\n", 
       argv[0], argv[0]);
  
  char **options = &argv[1];
  while (!strncmp(options[0], "-", 1)) {

    int length = strlen("-nb_bits=");
    if (!strncmp(options[0], "-nb_bits=", length) && strlen(options[0]) > length) {
      nb_bits = xbt_str_parse_int(options[0] + length, "Invalid nb_bits parameter: %s");
      XBT_DEBUG("Set nb_bits to %d", nb_bits);
    }
    else {

      length = strlen("-timeout=");
      if (!strncmp(options[0], "-timeout=", length) && strlen(options[0]) > length) {
        timeout = xbt_str_parse_int(options[0] + length, "Invalid timeout parameter: %s");
        XBT_DEBUG("Set timeout to %d", timeout);
      }
      else {
        xbt_die("Invalid chord option '%s'", options[0]);
      }
    }
    options++;
  }

  const char* platform_file = options[0];
  const char* application_file = options[1];

  MSG_create_environment(platform_file);
  
  MSG_function_register("node", node);
  MSG_launch_application(application_file);

  msg_error_t res = MSG_main();
  XBT_CRITICAL("Messages created: %ld", smx_total_comms);
  XBT_INFO("Simulated time: %g", MSG_get_clock());

  return res != MSG_OK;
}
Exemplo n.º 9
0
void smpi_process_init(int *argc, char ***argv)
{
  int index=-1;
  smpi_process_data_t data;
  smx_process_t proc;

  if (argc && argv) {
    proc = SIMIX_process_self();
    //FIXME: dirty cleanup method to avoid using msg cleanup functions on these processes when using MSG+SMPI
    SIMIX_process_set_cleanup_function(proc, MSG_process_cleanup_from_SIMIX);
    char* instance_id = (*argv)[1];
    int rank = xbt_str_parse_int((*argv)[2], "Invalid rank: %s");
    index = smpi_process_index_of_smx_process(proc);

    if(!index_to_process_data){
      index_to_process_data=(int*)xbt_malloc(SIMIX_process_count()*sizeof(int));
    }

    if(smpi_privatize_global_variables){
      /* Now using segment index of the process  */
      index = proc->segment_index;
      /* Done at the process's creation */
      SMPI_switch_data_segment(index);
    }

    MPI_Comm* temp_comm_world;
    xbt_bar_t temp_bar;
    smpi_deployment_register_process(instance_id, rank, index, &temp_comm_world, &temp_bar);
    data              = smpi_process_remote_data(index);
    data->comm_world  = temp_comm_world;
    if(temp_bar != NULL) data->finalization_barrier = temp_bar;
    data->index       = index;
    data->instance_id = instance_id;
    data->replaying   = 0;
    //xbt_free(simcall_process_get_data(proc));

    simdata_process_t simdata = static_cast<simdata_process_t>(simcall_process_get_data(proc));
    simdata->data             = data;

    if (*argc > 3) {
      free((*argv)[1]);
      memmove(&(*argv)[0], &(*argv)[2], sizeof(char *) * (*argc - 2));
      (*argv)[(*argc) - 1] = NULL;
      (*argv)[(*argc) - 2] = NULL;
    }
    (*argc)-=2;
    data->argc = argc;
    data->argv = argv;
    // set the process attached to the mailbox
    simcall_mbox_set_receiver(data->mailbox_small, proc);
    XBT_DEBUG("<%d> New process in the game: %p", index, proc);
  }
  xbt_assert(smpi_process_data(),
      "smpi_process_data() returned NULL. You probably gave a NULL parameter to MPI_Init. Although it's required by "
      "MPI-2, this is currently not supported by SMPI.");
}
Exemplo n.º 10
0
/* Main function of the Yielder process */
static int yielder(int argc, char* argv[])
{
  xbt_assert(argc == 2, "The sender function expects 1 arguments from the XML deployment file");
  long number_of_yields = xbt_str_parse_int(argv[1], "Invalid amount of yields: %s"); /* - number of yields */

  for (int i = 0; i < number_of_yields; i++)
    MSG_process_yield();
  XBT_INFO("I yielded %ld times. Goodbye now!", number_of_yields);
  return 0;
}
Exemplo n.º 11
0
static int receiver(int argc, char *argv[])
{
  xbt_assert(argc==3, "This function expects 2 parameters from the XML deployment file");
  int id = xbt_str_parse_int(argv[1], "ID should be numerical, not %s");
  int task_amount = xbt_str_parse_int(argv[2], "Invalid amount of tasks: %s");
  msg_task_t *tasks = xbt_new(msg_task_t, task_amount);
  xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);

  char mailbox[80];
  snprintf(mailbox,79, "receiver-%d", id);
   
  MSG_process_sleep(10);
  for (int i = 0; i < task_amount; i++) {
    XBT_INFO("Wait to receive task %d", i);
    tasks[i] = NULL;
    msg_comm_t comm = MSG_task_irecv(&tasks[i], mailbox);
    xbt_dynar_push_as(comms, msg_comm_t, comm);
  }

  /* Here we are waiting for the receiving of all communications */
  while (!xbt_dynar_is_empty(comms)) {
    msg_comm_t comm;
    // MSG_comm_waitany returns the rank of the comm that just ended. Remove it.
    xbt_dynar_remove_at(comms, MSG_comm_waitany(comms), &comm);
    msg_task_t task = MSG_comm_get_task(comm);
    MSG_comm_destroy(comm);
    XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
    MSG_task_execute(task);
    XBT_INFO("\"%s\" done", MSG_task_get_name(task));
    msg_error_t err = MSG_task_destroy(task);
    xbt_assert(err == MSG_OK, "MSG_task_destroy failed");
  }
  xbt_dynar_free(&comms);
  xbt_free(tasks);

  /* Here we tell to sender that all tasks are done */
  MSG_task_send(MSG_task_create(NULL, 0, 0, NULL), "finalize");
  XBT_INFO("I'm done. See you!");
  return 0;
}
Exemplo n.º 12
0
static int worker(int argc, char *argv[])
{
  msg_task_t task = NULL;
  char mailbox[80];

  long id= xbt_str_parse_int(argv[1], "Invalid argument %s");

  snprintf(mailbox, 79,"worker-%ld", id);

  while (1) {
    double time1 = MSG_get_clock();
    int retcode = MSG_task_receive( &(task), mailbox);
    double time2 = MSG_get_clock();
    if (retcode == MSG_OK) {
      XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
      if (MSG_task_get_data(task) == FINALIZE) {
        MSG_task_destroy(task);
        task = NULL;
        break;
      }
      if (time1 < *((double *) task->data))
        time1 = *((double *) task->data);
      XBT_INFO("Communication time : \"%f\"", time2 - time1);
      XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
      retcode = MSG_task_execute(task);
      if (retcode == MSG_OK) {
        XBT_INFO("\"%s\" done", MSG_task_get_name(task));
        MSG_task_destroy(task);
        task = NULL;
      } else if (retcode == MSG_HOST_FAILURE) {
        XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
        MSG_task_destroy(task);
        task = NULL;
        return 0;
      } else {
        XBT_INFO("Hey ?! What's up ? ");
        xbt_die("Unexpected behavior");
      }
    } else if (retcode == MSG_HOST_FAILURE) {
      XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
      return 0;
    } else if (retcode == MSG_TRANSFER_FAILURE) {
      XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
    } else {
      xbt_die("Unexpected behavior");
    }
  }
  XBT_INFO("I'm done. See you!");
  return 0;
}
Exemplo n.º 13
0
/** Master expects 3+ arguments given in the XML deployment file: */
static int master(int argc, char* argv[])
{
  long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");       /** - Number of tasks      */
  double comp_size     = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost    */
  double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */

  /* Create the tasks in advance */
  msg_task_t* todo = xbt_new0(msg_task_t, number_of_tasks);

  for (int i = 0; i < number_of_tasks; i++) {
    char sprintf_buffer[64];
    sprintf(sprintf_buffer, "Task_%d", i);
    todo[i] = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
  }

  /* Get the info about the worker processes from my parameters */
  int worker_count    = argc - 4;
  msg_host_t* workers = xbt_new0(msg_host_t, worker_count);

  for (int i = 4; i < argc; i++) {
    workers[i - 4] = MSG_get_host_by_name(argv[i]);
    xbt_assert(workers[i - 4] != NULL, "Unknown host %s. Stopping Now! ", argv[i]);
  }
  XBT_INFO("Got %d workers and %ld tasks to process", worker_count, number_of_tasks);

  /* Dispatch the tasks */
  for (int i = 0; i < number_of_tasks; i++) {
    XBT_INFO("Sending '%s' to '%s'", todo[i]->name, MSG_host_get_name(workers[i % worker_count]));
    if (MSG_host_self() == workers[i % worker_count]) {
      XBT_INFO("Hey ! It's me ! :)");
    }

    MSG_task_send(todo[i], MSG_host_get_name(workers[i % worker_count]));
    XBT_INFO("Sent");
  }

  XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
  for (int i = 0; i < worker_count; i++) {
    msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
    MSG_task_send(finalize, MSG_host_get_name(workers[i]));
  }

  XBT_INFO("Goodbye now!");
  free(workers);
  free(todo);
  return 0;
}
Exemplo n.º 14
0
int main(int argc, char *argv[]){
  msg_error_t res;

  MSG_init(&argc, argv);

  xbt_assert(argc > 3, "Usage: %s description_file platform_file deployment_file\n"
             "\tExample: %s smpi_multiple_apps msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);

  /*  Simulation setting */
  MSG_create_environment(argv[2]);

  /*   Application deployment: read the description file in order to identify instances to launch */
  FILE* fp = fopen(argv[1], "r");
  if (fp == NULL)
    xbt_die("Cannot open %s", argv[1]);
  char *line = NULL;
  size_t n   = 0;
  int instance_size = 0;
  const char* instance_id = NULL;
  while (xbt_getline(&line, &n, fp) != -1 ){
    xbt_dynar_t elems = xbt_str_split_quoted_in_place(line);
    if(xbt_dynar_length(elems)<3){
      xbt_die ("Not enough elements in the line");
    }

    const char** line_char= xbt_dynar_to_array(elems);
    instance_id = line_char[0];
    instance_size = xbt_str_parse_int(line_char[2], "Invalid size: %s");

    XBT_INFO("Initializing instance %s of size %d", instance_id, instance_size);
    SMPI_app_instance_register(instance_id, smpi_replay,instance_size);

    xbt_free(line_char);
  }

  fclose(fp);

  MSG_launch_application(argv[3]);
  SMPI_init();

  res = MSG_main();

  XBT_INFO("Simulation time %g", MSG_get_clock());

  SMPI_finalize();
  return res != MSG_OK;
}
Exemplo n.º 15
0
int server(int argc, char *argv[])
{
  msg_task_t task1,task2;

  msg_comm_t comm1 = MSG_task_irecv(&task1, "mymailbox1");
  msg_comm_t comm2 = MSG_task_irecv(&task2, "mymailbox2");
  MSG_comm_wait(comm1, -1);
  MSG_comm_wait(comm2, -1);

  long val1 = xbt_str_parse_int(MSG_task_get_name(task1), "Task name is not a numerical ID: %s");
  XBT_INFO("Received %lu", val1);

  MC_assert(val1 == 2);

  XBT_INFO("OK");
  return 0;
}
Exemplo n.º 16
0
static int server(int argc, char *argv[])
{
  msg_task_t task = NULL;
  int count = 0;
  while (count < N) {
    if (task) {
      MSG_task_destroy(task);
      task = NULL;
    }
    MSG_task_receive(&task, "mymailbox");
    count++;
  }
  MC_assert(xbt_str_parse_int(MSG_task_get_name(task), "Task names must be integers, not '%s'") == 3);

  XBT_INFO("OK");
  return 0;
}
Exemplo n.º 17
0
/* Receiver process expects 3 arguments: */
static int receiver(int argc, char *argv[])
{
  msg_task_t task = NULL;
  XBT_ATTRIB_UNUSED msg_error_t res;
  char mailbox[80];
  msg_comm_t res_irecv;
  int id = xbt_str_parse_int(argv[1], "Invalid id: %s");                                        /* - unique id */
  double sleep_start_time = xbt_str_parse_double(argv[2], "Invalid sleep start parameter: %s"); /* - start time */
  double sleep_test_time = xbt_str_parse_double(argv[3], "Invalid sleep test parameter: %s");   /* - test time */
  XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);

  MSG_process_sleep(sleep_start_time); /* This process first sleeps for "start time" seconds.  */

  snprintf(mailbox,79, "receiver-%d", id);
  while (1) {
    res_irecv = MSG_task_irecv(&(task), mailbox); /* Then it posts asynchronous receives (@ref MSG_task_irecv) and*/
    XBT_INFO("Wait to receive a task");

    if (sleep_test_time > 0) {               /* - if "test_time" is set to 0, wait on @ref MSG_comm_wait */
      while (MSG_comm_test(res_irecv) == 0) { /* - Call @ref MSG_comm_test every "test_time" otherwise */
        MSG_process_sleep(sleep_test_time);
      }
    } else {
      res = MSG_comm_wait(res_irecv, -1);
      xbt_assert(res == MSG_OK, "MSG_task_get failed");
    }
    MSG_comm_destroy(res_irecv);

    XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
    if (strcmp(MSG_task_get_name(task), "finalize") == 0) { /* If the received task is "finalize", the process ends */
      MSG_task_destroy(task);
      break;
    }

    XBT_INFO("Processing \"%s\"", MSG_task_get_name(task)); /* Otherwise, the task is processed */
    MSG_task_execute(task);
    XBT_INFO("\"%s\" done", MSG_task_get_name(task));
    MSG_task_destroy(task);
    task = NULL;
  }
  XBT_INFO("I'm done. See you!");
  return 0;
}
Exemplo n.º 18
0
/* Main function of all processes used in this example */
static int relay_runner(int argc, char* argv[])
{
  xbt_assert(argc == 0, "The relay_runner function does not accept any parameter from the XML deployment file");
  int rank = xbt_str_parse_int(MSG_process_get_name(MSG_process_self()),
                               "Any process of this example must have a numerical name, not %s");
  char mailbox[256];

  if (rank == 0) {
    /* The root process (rank 0) first sends the token then waits to receive it back */
    snprintf(mailbox, 255, "%d", rank + 1);
    unsigned int task_comm_size = 1000000; /* The token is 1MB long*/
    msg_task_t task             = MSG_task_create("Token", 0, task_comm_size, NULL);
    XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"", rank, task->name, mailbox);
    MSG_task_send(task, mailbox);
    task    = NULL;
    int res = MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
    xbt_assert(res == MSG_OK, "MSG_task_get failed");
    XBT_INFO("Host \"%d\" received \"%s\"", rank, MSG_task_get_name(task));
    MSG_task_destroy(task);

  } else {
    /* The others processes receive from their left neighbor (rank-1) and send to their right neighbor (rank+1) */
    msg_task_t task = NULL;
    int res         = MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
    xbt_assert(res == MSG_OK, "MSG_task_get failed");
    XBT_INFO("Host \"%d\" received \"%s\"", rank, MSG_task_get_name(task));

    if (rank + 1 == MSG_get_host_number())
      /* But the last process, which sends the token back to rank 0 */
      snprintf(mailbox, 255, "0");
    else
      snprintf(mailbox, 255, "%d", rank + 1);
    XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"", rank, task->name, mailbox);
    MSG_task_send(task, mailbox);
  }
  return 0;
}
Exemplo n.º 19
0
/** Emitter function  */
static int master(int argc, char *argv[])
{
  int workers_count = 0;
  msg_host_t *workers = NULL;
  msg_task_t *todo = NULL;
  msg_host_t host_self = MSG_host_self();
  char *master_name = (char *) MSG_host_get_name(host_self);
  char channel[1024];

  int i;

  long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");    /** - Number of tasks      */
  double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");  /** - Task compute cost    */
  double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");  /** - Task communication size */

  {                             /*  Task creation */
    char sprintf_buffer[64];

    todo = xbt_new0(msg_task_t, number_of_tasks);

    for (i = 0; i < number_of_tasks; i++) {
      sprintf(sprintf_buffer, "Task_%d", i);
      todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
    }
  }

  {                             /* Process organization */
    workers_count = MSG_get_host_number();
    workers = xbt_dynar_to_array(MSG_hosts_as_dynar());

    for (i = 0; i < workers_count; i++)
      if(host_self == workers[i]) {
         workers[i] = workers[workers_count-1];
         workers_count--;
         break;
      }

    for (i = 0; i < workers_count; i++)
  MSG_process_create("worker", worker, master_name, workers[i]);
  }

  XBT_INFO("Got %d workers and %Ld tasks to process", workers_count, number_of_tasks);

  for (i = 0; i < number_of_tasks; i++) {
    build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count]));

    XBT_INFO("Sending \"%s\" to channel \"%s\"", todo[i]->name, channel);

    MSG_task_send(todo[i], channel);
    XBT_INFO("Sent");
  }

  XBT_INFO ("All tasks have been dispatched. Let's tell everybody the computation is over.");
  for (i = 0; i < workers_count; i++) {
    msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
    MSG_task_send(finalize, build_channel_name(channel,master_name, MSG_host_get_name(workers[i % workers_count])));
  }

  XBT_INFO("Goodbye now!");
  free(workers);
  free(todo);
  return 0;
}                               /* end_of_master */
Exemplo n.º 20
0
/**
 * \brief Node Function
 * Arguments:
 * - my id
 * - the id of a guy I know in the system (except for the first node)
 * - the time to sleep before I join (except for the first node)
 * - the deadline time
 */
static int node(int argc, char *argv[])
{
  double init_time = MSG_get_clock();
  msg_task_t task_received = NULL;  
  int join_success = 0;  
  double deadline;
  xbt_assert(argc == 3 || argc == 5, "Wrong number of arguments for this node");
  s_node_t node = {0};
  node.id = xbt_str_parse_int(argv[1], "Invalid ID: %s");
  node.known_id = -1;
  node.ready = -1;
  node.pending_tasks = xbt_fifo_new();
  get_mailbox(node.id, node.mailbox);
  XBT_DEBUG("New node with id %s (%08x)", node.mailbox, node.id);
  
  int i,j,d;
  for (i=0; i<LEVELS_COUNT; i++){
    d = domain(node.id, i);
    for (j=0; j<LEVEL_SIZE; j++)
      node.routing_table[i][j] = (d==j) ? node.id : -1;
  }

  for (i=0; i<NEIGHBORHOOD_SIZE; i++)
    node.neighborhood_set[i] = -1;

  for (i=0; i<NAMESPACE_SIZE; i++)
    node.namespace_set[i] = -1;

  if (argc == 3) { // first ring
    XBT_DEBUG("Hey! Let's create the system.");
    deadline = xbt_str_parse_double(argv[2], "Invalid deadline: %s");
    create(&node);
    join_success = 1;
  }
  else {
    node.known_id = xbt_str_parse_int(argv[2], "Invalid known ID: %s");
    double sleep_time = xbt_str_parse_double(argv[3], "Invalid sleep time: %s");
    deadline = xbt_str_parse_double(argv[4], "Invalid deadline: %s");

    // sleep before starting
    XBT_DEBUG("Let's sleep during %f", sleep_time);
    MSG_process_sleep(sleep_time);
    XBT_DEBUG("Hey! Let's join the system.");

    join_success = join(&node);
  }

  if (join_success) {
    XBT_DEBUG("Waiting ….");

    while (MSG_get_clock() < init_time + deadline
//      && MSG_get_clock() < node.last_change_date + 1000
        && MSG_get_clock() < max_simulation_time) {
      if (node.comm_receive == NULL) {
        task_received = NULL;
        node.comm_receive = MSG_task_irecv(&task_received, node.mailbox);
        // FIXME: do not make MSG_task_irecv() calls from several functions
      }
      if (!MSG_comm_test(node.comm_receive)) {
        MSG_process_sleep(5);
      } else {
        // a transfer has occurred

        msg_error_t status = MSG_comm_get_status(node.comm_receive);

        if (status != MSG_OK) {
          XBT_DEBUG("Failed to receive a task. Nevermind.");
          MSG_comm_destroy(node.comm_receive);
          node.comm_receive = NULL;
        }
        else {
          // the task was successfully received
          MSG_comm_destroy(node.comm_receive);
          node.comm_receive = NULL;
          handle_task(&node, task_received);
        }
      }

    }
    print_node(&node);
  }
  return 1;
}
Exemplo n.º 21
0
static int master(int argc, char *argv[])
{
  int i;

  long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
  double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
  double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
  long workers_count = xbt_str_parse_int(argv[4], "Invalid amount of workers: %s");

  XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);

  for (i = 0; i < number_of_tasks; i++) {
    char mailbox[256];
    snprintf(mailbox, 255, "worker-%ld", i % workers_count);

    msg_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size, xbt_new0(double, 1));
    *((double *) task->data) = MSG_get_clock();

    switch ( MSG_task_send_with_timeout(task,mailbox,10.0) ) {
    case MSG_OK:
      XBT_INFO("Send completed");
      break;

    case MSG_HOST_FAILURE:
      XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
      free(task->data);
      MSG_task_destroy(task);
      return 0;

    case MSG_TRANSFER_FAILURE:
      XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", mailbox);
      free(task->data);
      MSG_task_destroy(task);
      break;

    case MSG_TIMEOUT:
      XBT_INFO ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox);
      free(task->data);
      MSG_task_destroy(task);
      break;

    default:
      xbt_die( "Unexpected behavior");
    }
  }

  XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
  for (i = 0; i < workers_count; i++) {
    char mailbox[256];
    snprintf(mailbox, 255, "worker-%ld", i % workers_count);
    msg_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);

    switch (MSG_task_send_with_timeout(task,mailbox,1.0)) {
    case MSG_HOST_FAILURE:
      XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
      MSG_task_destroy(task);
      break;

    case MSG_TRANSFER_FAILURE:
      XBT_INFO("Mmh. Can't reach '%s'! Nevermind. Let's keep going!", mailbox);
      MSG_task_destroy(task);
      break;

    case MSG_TIMEOUT:
      XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox);
      MSG_task_destroy(task);
      break;

    case MSG_OK:
      /* nothing */
      break;

    default:
      xbt_die("Unexpected behavior with '%s'", mailbox);
    }
  }

  XBT_INFO("Goodbye now!");
  return 0;
}