Logo AND Algorithmique Numérique Distribuée

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