Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
slave2worker cont'd
[simgrid.git] / examples / msg / ns3 / ns3.c
1 /* Copyright (c) 2007-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 /** @addtogroup MSG_examples
12  * 
13  *  @section MSG_ex_models Models-related examples
14  * 
15  *  @subsection MSG_ex_PLS Packet level simulators
16  * 
17  *  These examples demonstrate how to use the bindings to classicalPacket-Level Simulators (PLS), as explained in
18  *  \ref pls. The most interesting is probably not the C files since they are unchanged from the other simulations,
19  *  but the associated files, such as the platform files to see how to declare a platform to be used with the PLS
20  *  bindings of SimGrid and the tesh files to see how to actually start a simulation in these settings.
21  * 
22  * - <b>ns3</b>: Simple ping-pong using ns3 instead of the SimGrid network models. 
23  */
24
25 int timer_start; //set as 1 in the master process
26
27 //keep a pointer to all surf running tasks.
28 #define NTASKS 1500
29 int bool_printed = 0;
30 double start_time, end_time, elapsed_time;
31 double gl_data_size[NTASKS];
32 msg_task_t gl_task_array[NTASKS];
33 const char *workernames[NTASKS];
34 const char *masternames[NTASKS];
35 int gl_task_array_id = 0;
36 int count_finished = 0;
37
38 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
39
40 static int master(int argc, char *argv[])
41 {
42   msg_task_t todo;
43
44   xbt_assert(argc==4,"Strange number of arguments expected 3 got %d", argc - 1);
45
46   XBT_DEBUG ("Master started");
47
48   /* data size */
49   double task_comm_size = xbt_str_parse_double(argv[1], "Invalid task communication size: %s");
50
51   /* worker name */
52   char *workername = argv[2];
53   int id = xbt_str_parse_int(argv[3], "Invalid ID as argument 3: %s");   //unique id to control statistics
54   char *id_alias = bprintf("flow_%d", id);
55   workernames[id] = workername;
56   TRACE_category(id_alias);
57
58   masternames[id] = MSG_host_get_name(MSG_host_self());
59
60   {                             /*  Task creation.  */
61     todo = MSG_task_create("Task_0", 100*task_comm_size, task_comm_size, NULL);
62     MSG_task_set_category(todo, id_alias);
63     //keep track of running tasks
64     gl_task_array[id] = todo;
65     gl_data_size[id] = task_comm_size;
66   }
67
68   MSG_host_by_name(workername);
69
70   count_finished++;
71   timer_start = 1 ;
72
73   /* time measurement */
74   sprintf(id_alias, "%d", id);
75   start_time = MSG_get_clock();
76   MSG_task_send(todo, id_alias);
77   end_time = MSG_get_clock();
78
79   XBT_DEBUG ("Finished");
80   xbt_free(id_alias);
81   return 0;
82 }
83
84 static int timer(int argc, char *argv[])
85 {
86   double sleep_time;
87   double first_sleep;
88
89   xbt_assert(argc==3,"Strange number of arguments expected 2 got %d", argc - 1);
90
91   sscanf(argv[1], "%lf", &first_sleep);
92   sscanf(argv[2], "%lf", &sleep_time);
93
94   XBT_DEBUG ("Timer started");
95
96   if(first_sleep){
97       MSG_process_sleep(first_sleep);
98   }
99
100   do {
101     XBT_DEBUG ("Get sleep");
102     MSG_process_sleep(sleep_time);
103   } while(timer_start);
104
105   XBT_DEBUG ("Finished");
106   return 0;
107 }
108
109 static int worker(int argc, char *argv[])
110 {
111   msg_task_t task = NULL;
112   char id_alias[10];
113
114   xbt_assert(argc==2,"Strange number of arguments expected 1 got %d", argc - 1);
115
116   XBT_DEBUG ("Worker started");
117
118   int id = xbt_str_parse_int(argv[1], "Invalid id: %s");
119   sprintf(id_alias, "%d", id);
120
121   msg_error_t a = MSG_task_receive(&(task), id_alias);
122
123   count_finished--;
124   if(count_finished == 0){
125       timer_start = 0;
126   }
127
128   xbt_assert(a == MSG_OK,"Hey?! What's up? Unexpected behavior");
129
130   elapsed_time = MSG_get_clock() - start_time;
131
132   XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s", id, MSG_task_get_bytes_amount(task), masternames[id],
133            workernames[id]);
134 //  MSG_task_execute(task);
135   MSG_task_destroy(task);
136
137   XBT_DEBUG ("Finished");
138   return 0;
139 }
140
141 int main(int argc, char *argv[])
142 {
143   msg_error_t res = MSG_OK;
144   bool_printed = 0;
145
146   MSG_init(&argc, argv);
147   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
148              "\tExample: %s platform.xml deployment.xml\n", argv[0], argv[0]);
149
150   MSG_create_environment(argv[1]);
151   TRACE_declare_mark("endmark");
152
153   MSG_function_register("master", master);
154   MSG_function_register("worker", worker);
155   MSG_function_register("timer", timer);
156
157   MSG_launch_application(argv[2]);
158
159   res = MSG_main();
160
161   return res != MSG_OK;
162 }