/* Copyright (c) 2010-2019. 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. */

#include "simgrid/msg.h"

XBT_LOG_NEW_DEFAULT_CATEGORY(msg_trace_masterworker, "Messages specific for this msg example");

static int master(int argc, char *argv[])
{
  xbt_assert(argc == 5);
  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 = 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;
}

static int worker(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
{
  msg_task_t task = NULL;

  TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "is_worker", 1);
  TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "task_computation", 0);
  while (1) {
    MSG_task_receive(&(task), "master_mailbox");

    if (strcmp(MSG_task_get_name(task), "finalize") == 0) {
      MSG_task_destroy(task);
      break;
    }
    // adding the value returned by MSG_task_get_compute_duration(task) to the variable "task_computation"
    TRACE_host_variable_add(MSG_host_get_name(MSG_host_self()), "task_computation", MSG_task_get_flops_amount(task));
    MSG_task_execute(task);
    MSG_task_destroy(task);
    task = NULL;
  }
  return 0;
}

int main(int argc, char *argv[])
{
  MSG_init(&argc, argv);
  xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
             "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);

  MSG_create_environment(argv[1]);

  //declaring user variables
  TRACE_host_variable_declare("is_worker");
  TRACE_host_variable_declare("is_master");
  TRACE_host_variable_declare("task_creation");
  TRACE_host_variable_declare("task_computation");

  //declaring user markers and values
  TRACE_declare_mark("msmark");
  TRACE_declare_mark_value ("msmark", "start_send_tasks");
  TRACE_declare_mark_value ("msmark", "finish_send_tasks");

  //declaring user categories with RGB colors (values from 0 to 1)
  TRACE_category_with_color ("compute", "1 0 0");  //compute is red
  TRACE_category_with_color ("finalize", "0 1 0"); //finalize is green
  //categories without user-defined colors receive random colors generated by the tracing system
  TRACE_category ("request");
  TRACE_category_with_color ("report", NULL);

  MSG_function_register("master", master);
  MSG_function_register("worker", worker);
  MSG_launch_application(argv[2]);

  MSG_main();

  unsigned int cursor;
  xbt_dynar_t categories = TRACE_get_categories ();
  if (categories){
    XBT_INFO ("Declared tracing categories:");
    char *category;
    xbt_dynar_foreach (categories, cursor, category){
      XBT_INFO ("%s", category);
    }
    xbt_dynar_free (&categories);
  }

  xbt_dynar_t marks = TRACE_get_marks ();
  if (marks){
    XBT_INFO ("Declared marks:");
    char *mark;
    xbt_dynar_foreach (marks, cursor, mark){
      XBT_INFO ("%s", mark);
    }
    xbt_dynar_free (&marks);
  }

  return 0;
}
