Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Deployment file generator in Ruby, takes a platform file, parses hostnames and output...
[simgrid.git] / examples / msg / kadeploy / kadeploy.c
index cc8b0a3..2beb023 100644 (file)
 #include "xbt/log.h"
 #include "xbt/asserts.h"
 
+#include "iterator.h"
+#include "messages.h"
+#include "broadcaster.h"
+#include "peer.h"
+
 /** @addtogroup MSG_examples
  * 
  *  - <b>kadeploy/kadeploy.c: Kadeploy implementation</b>.
 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_kadeploy,
                              "Messages specific for kadeploy");
 
-#define MESSAGE_SIZE 1
-#define HOSTNAME_LENGTH 20
-
 /*
  Data structures
  */
 
-/* Random iterator for xbt_dynar */
-typedef struct xbt_dynar_iterator_struct {
-  xbt_dynar_t list;
-  xbt_dynar_t indices_list;
-  int current;
-  unsigned long length;
-  int (*criteria_fn)(void* it);
-} *xbt_dynar_iterator_t;
-typedef struct xbt_dynar_iterator_struct xbt_dynar_iterator_s;
-
-/* Messages enum */
-typedef enum {
-  MESSAGE_BUILD_CHAIN = 0,
-  MESSAGE_SEND_DATA,
-  MESSAGE_END_DATA
-} e_message_type;
-
-/* Message struct */
-typedef struct s_message {
-  e_message_type type;
-  const char *issuer_hostname;
-  const char *mailbox;
-  const char *prev_hostname;
-  const char *next_hostname;
-  const char *data_block;
-  unsigned int data_length;
-} s_message_t, *message_t;
-
-/* Iterator methods */
-xbt_dynar_iterator_t xbt_dynar_iterator_new(xbt_dynar_t list, int (*criteria_fn)(void*));
-void *xbt_dynar_iterator_next(xbt_dynar_iterator_t it);
-void xbt_dynar_iterator_delete(xbt_dynar_iterator_t it);
-int xbt_dynar_iterator_forward_criteria(void *p);
-
-/* Message methods */
-msg_task_t task_message_new(e_message_type type, const char *issuer_hostname, const char *mailbox);
-msg_task_t task_message_chain_new(const char *issuer_hostname, const char *mailbox, const char* prev, const char *next);
-msg_task_t task_message_data_new(const char *issuer_hostname, const char *mailbox, const char *block, unsigned int len);
-msg_task_t task_message_end_data_new(const char *issuer_hostname, const char *mailbox);
-void task_message_delete(void *);
-
-/* Tasks */
-int broadcaster(int argc, char *argv[]);
-int peer(int argc, char *argv[]);
-
-xbt_dynar_t build_hostlist_from_hostcount(int hostcount); 
-/*xbt_dynar_t build_hostlist_from_argv(int argc, char *argv[]);*/
-
-/* Broadcaster: helper functions */
-int broadcaster_build_chain(xbt_dynar_t host_list);
-int broadcaster_send_file(xbt_dynar_t host_list);
-int broadcaster_finish(xbt_dynar_t host_list);
-
-/* Peer: helper functions */
-int peer_wait_for_init();
-
 /* Initialization stuff */
 msg_error_t test_all(const char *platform_file,
                      const char *application_file);
 
-/* Allocates and initializes a new xbt_dynar iterator for list, using criteria_fn as iteration criteria
-   criteria_fn: given an iterator, it must update the iterator and give the next element's index, 
-   less than 0 otherwise*/
-xbt_dynar_iterator_t xbt_dynar_iterator_new(xbt_dynar_t list, int (*criteria_fn)(void*))
-{
-  xbt_dynar_iterator_t it = xbt_new(xbt_dynar_iterator_s, 1);
-  
-  it->list = list;
-  it->length = xbt_dynar_length(list);
-  it->indices_list = xbt_dynar_new(sizeof(int), NULL);
-  it->criteria_fn = criteria_fn;
-  it->current = -1;
-}
-
-/* Returns the next element iterated by iterator it, NULL if there are no more elements */
-void *xbt_dynar_iterator_next(xbt_dynar_iterator_t it)
-{
-  int next = it->criteria_fn((xbt_dynar_iterator_t)it);
-  XBT_INFO("%d current\n", next);
-  if (next < 0) {
-    XBT_INFO("Nothing to return!\n");
-    return NULL;
-  } else {
-    xbt_dynar_push(it->indices_list, &next);
-    return xbt_dynar_get_ptr(it->list, next);
-  }
-}
-
-void xbt_dynar_iterator_delete(xbt_dynar_iterator_t it)
-{
-  xbt_dynar_free_container(&(it->indices_list));
-  xbt_free_ref(&it);
-}
-
-int xbt_dynar_iterator_forward_criteria(void *p)
-{
-  xbt_dynar_iterator_t it = (xbt_dynar_iterator_t)p;
-  int r = -1;
-  if (it->current == -1) {
-    /* iterator initialization */
-    it->current = 0;
-  }
-  if (it->current < it->length) {
-    r = it->current;
-    it->current++;
-  }
-
-  return r;
-}
-
-msg_task_t task_message_new(e_message_type type, const char *issuer_hostname, const char *mailbox)
-{
-  message_t msg = xbt_new(s_message_t, 1);
-  msg->type = type;
-  msg->issuer_hostname = issuer_hostname;
-  msg->mailbox = mailbox;
-  msg_task_t task = MSG_task_create(NULL, 0, MESSAGE_SIZE, msg); 
-
-  return task;
-}
-
-msg_task_t task_message_chain_new(const char *issuer_hostname, const char *mailbox, const char* prev, const char *next)
-{
-  msg_task_t task = task_message_new(MESSAGE_BUILD_CHAIN, issuer_hostname, mailbox);
-  message_t msg = MSG_task_get_data(task);
-  msg->prev_hostname = prev;
-  msg->next_hostname = next;
-
-  return task;
-}
-
-msg_task_t task_message_data_new(const char *issuer_hostname, const char *mailbox, const char *block, unsigned int len)
-{
-  msg_task_t task = task_message_new(MESSAGE_SEND_DATA, issuer_hostname, mailbox);
-  message_t msg = MSG_task_get_data(task);
-  msg->data_block = block;
-  msg->data_length = len;
-
-  return task;
-}
-
-msg_task_t task_message_end_data_new(const char *issuer_hostname, const char *mailbox)
-{
-  return task_message_new(MESSAGE_END_DATA, issuer_hostname, mailbox);
-}
-
-
-void task_message_delete(void *task)
-{
-  message_t msg = MSG_task_get_data(task);
-  xbt_free(msg);
-  MSG_task_destroy(task);
-}
-
-xbt_dynar_t build_hostlist_from_hostcount(int hostcount)
-{
-  xbt_dynar_t host_list = xbt_dynar_new(sizeof(char*), NULL);
-  char *hostname = NULL;
-  msg_host_t h = NULL;
-  int i = 1;
-  
-  for (; i < hostcount+1; i++) {
-    hostname = xbt_new(char, HOSTNAME_LENGTH);
-    snprintf(hostname, HOSTNAME_LENGTH, "host%d", i);
-    XBT_INFO("%s", hostname);
-    h = MSG_get_host_by_name(hostname);
-    if (h == NULL) {
-      XBT_INFO("Unknown host %s. Stopping Now! ", hostname);
-      abort();
-    } else {
-      xbt_dynar_push(host_list, &hostname);
-    }
-  }
-  return host_list;
-}
-
-/*xbt_dynar_t build_hostlist_from_argv(int argc, char *argv[])
-{
-  xbt_dynar_t host_list = xbt_dynar_new(sizeof(char*), NULL);
-  msg_host_t h = NULL;
-  int i = 1;
-  
-  for (; i < argc; i++) {
-    XBT_INFO("host%d = %s", i, argv[i]);
-    h = MSG_get_host_by_name(argv[i]);
-    if (h == NULL) {
-      XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
-      abort();
-    } else {
-      xbt_dynar_push(host_list, &(argv[i]));
-    }
-  }
-  return host_list;
-}*/
-
-void delete_hostlist(xbt_dynar_t h)
-{
-  xbt_dynar_free(&h);
-}
-
-int broadcaster_build_chain(xbt_dynar_t host_list)
-{
-  xbt_dynar_iterator_t it = xbt_dynar_iterator_new(host_list, xbt_dynar_iterator_forward_criteria);
-  msg_task_t task = NULL;
-  char **cur = (char**)xbt_dynar_iterator_next(it);
-  const char *current_host = NULL;
-  const char *prev = NULL;
-  const char *next = NULL;
-  const char *me = MSG_host_get_name(MSG_host_self());
-  const char *last = NULL;
-
-  /* Build the chain if there's at least one peer */
-  if (cur != NULL) {
-    /* init: prev=NULL, host=current cur, next=next cur */
-    next = *cur;
-
-    /* This iterator iterates one step ahead: cur is current iterated element, 
-       but it's actually the next one in the chain */
-    do {
-      /* following steps: prev=last, host=next, next=cur */
-      cur = (char**)xbt_dynar_iterator_next(it);
-      prev = last;
-      current_host = next;
-      if (cur != NULL)
-        next = *cur;
-      else
-        next = NULL;
-      XBT_INFO("Building chain -- broadcaster:\"%s\" dest:\"%s\" prev:\"%s\" next:\"%s\"", me, current_host, prev, next);
-    
-      /* Send message to current peer */
-      task = task_message_chain_new(me, current_host, prev, next);
-      MSG_task_send(task, current_host);
-
-      last = current_host;
-    } while (cur != NULL);
-  }
-  xbt_dynar_iterator_delete(it);
-
-  return MSG_OK;
-}
-
-int broadcaster_send_file(xbt_dynar_t host_list)
-{
-  /* ... */
-
-  return MSG_OK;
-}
-
-int broadcaster_finish(xbt_dynar_t host_list)
-{
-  xbt_dynar_iterator_t it = xbt_dynar_iterator_new(host_list, xbt_dynar_iterator_forward_criteria);
-  msg_task_t task = NULL;
-  const char *me = MSG_host_get_name(MSG_host_self());
-  const char *current_host = NULL;
-  char **cur = NULL;
-
-  /* Send goodbye message to every peer */
-  for (cur = (char**)xbt_dynar_iterator_next(it); cur != NULL; cur = (char**)xbt_dynar_iterator_next(it)) {
-      /* Send message to current peer */
-      current_host = *cur;
-      task = task_message_end_data_new(me, current_host);
-      MSG_task_send(task, current_host);
-  }
-
-  return MSG_OK;
-}
-
-
-/** Emitter function  */
-int broadcaster(int argc, char *argv[])
-{
-  xbt_dynar_t host_list = NULL;
-  const char *first = NULL;
-  int status = !MSG_OK;
-
-  XBT_INFO("broadcaster");
-
-  /* Check that every host given by the hostcount in argv[1] exists and add it
-     to a dynamic array */
-  host_list = build_hostlist_from_hostcount(atoi(argv[1]));
-  /*host_list = build_hostlist_from_argv(argc, argv);*/
-  
-  /* TODO: Error checking */
-  status = broadcaster_build_chain(host_list);
-  status = broadcaster_send_file(host_list);
-  status = broadcaster_finish(host_list);
-
-  delete_hostlist(host_list);
-
-  /* Latency */
-  /*time = MSG_get_clock();
-  sprintf(sprintf_buffer_la, "latency task");
-  task_la =
-      MSG_task_create(sprintf_buffer_la, 0.0, task_comm_size_lat, NULL);
-  task_la->data = xbt_new(double, 1);
-  *(double *) task_la->data = time;
-  XBT_INFO("task_la->data = %le", *((double *) task_la->data));
-  MSG_task_send(task_la, argv[1]);*/
-
-  /* Bandwidth */
-  /*time = MSG_get_clock();
-  sprintf(sprintf_buffer_bw, "bandwidth task");
-  task_bw =
-      MSG_task_create(sprintf_buffer_bw, 0.0, task_comm_size_bw, NULL);
-  task_bw->data = xbt_new(double, 1);
-  *(double *) task_bw->data = time;
-  XBT_INFO("task_bw->data = %le", *((double *) task_bw->data));
-  MSG_task_send(task_bw, argv[1]);
-  */
-  return status;
-}
-
-int peer_wait_for_init()
-{
-  msg_task_t task = NULL;
-  const char *me = MSG_host_get_name(MSG_host_self());
-
-  int a = MSG_task_receive(&task, me);
-
-  if (a == MSG_OK) {
-    XBT_INFO("Peer %s got message\n", me);
-  }
-
-  task_message_delete(task);
-
-  return MSG_OK;
-}
-
-/** Peer function  */
-int peer(int argc, char *argv[])
-{
-  double time, time1, sender_time;
-  msg_task_t task_la = NULL;
-  msg_task_t task_bw = NULL;
-  int a;
-  double communication_time = 0;
-
-  XBT_INFO("peer");
-
-  time = MSG_get_clock();
-
-  a = peer_wait_for_init();
-  /* Get Latency */
-  /*a = MSG_task_receive(&task_la,MSG_host_get_name(MSG_host_self()));
-  if (a == MSG_OK) {
-    time1 = MSG_get_clock();
-    sender_time = *((double *) (task_la->data));
-    time = sender_time;
-    communication_time = time1 - time;
-    XBT_INFO("Task received : %s", task_la->name);
-    xbt_free(task_la->data);
-    MSG_task_destroy(task_la);
-    XBT_INFO("Communic. time %le", communication_time);
-    XBT_INFO("--- la %f ----", communication_time);
-  } else {
-    xbt_die("Unexpected behavior");
-  }*/
-
-
-  /* Get Bandwidth */
-  /*a = MSG_task_receive(&task_bw,MSG_host_get_name(MSG_host_self()));
-  if (a == MSG_OK) {
-    time1 = MSG_get_clock();
-    sender_time = *((double *) (task_bw->data));
-    time = sender_time;
-    communication_time = time1 - time;
-    XBT_INFO("Task received : %s", task_bw->name);
-    xbt_free(task_bw->data);
-    MSG_task_destroy(task_bw);
-    XBT_INFO("Communic. time %le", communication_time);
-    XBT_INFO("--- bw %f ----", task_comm_size_bw / communication_time);
-  } else {
-    xbt_die("Unexpected behavior");
-  }*/
-
-
-  return 0;
-}                               /* end_of_receiver */
-
 
 /** Test function */
 msg_error_t test_all(const char *platform_file,
@@ -425,6 +52,17 @@ msg_error_t test_all(const char *platform_file,
   /*  Simulation setting */
   MSG_create_environment(platform_file);
 
+  /* Trace categories */
+  TRACE_category_with_color("host0", "0 0 1");
+  TRACE_category_with_color("host1", "0 1 0");
+  TRACE_category_with_color("host2", "0 1 1");
+  TRACE_category_with_color("host3", "1 0 0");
+  TRACE_category_with_color("host4", "1 0 1");
+  TRACE_category_with_color("host5", "0 0 0");
+  TRACE_category_with_color("host6", "1 1 0");
+  TRACE_category_with_color("host7", "1 1 1");
+  TRACE_category_with_color("host8", "0 1 0");
+
   /*   Application deployment */
   MSG_function_register("broadcaster", broadcaster);
   MSG_function_register("peer", peer);
@@ -449,7 +87,6 @@ int main(int argc, char *argv[])
 
   MSG_init(&argc, argv);
 
-
   /*if (argc <= 3) {
     XBT_CRITICAL("Usage: %s platform_file deployment_file <model>\n",
               argv[0]);