Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branches 'master' and 'master' of github.com:simgrid/simgrid
[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 double start_time, end_time, elapsed_time;
15 double gl_data_size[NTASKS];
16 msg_task_t gl_task_array[NTASKS];
17 const char *workernames[NTASKS];
18 const char *masternames[NTASKS];
19 int gl_task_array_id = 0;
20 int count_finished = 0;
21
22 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
23
24 static int master(int argc, char *argv[])
25 {
26   msg_task_t todo;
27
28   xbt_assert(argc==4,"Strange number of arguments expected 3 got %d", argc - 1);
29
30   XBT_DEBUG ("Master started");
31
32   /* data size */
33   double task_comm_size = xbt_str_parse_double(argv[1], "Invalid task communication size: %s");
34
35   /* worker name */
36   char *workername = argv[2];
37   int id = xbt_str_parse_int(argv[3], "Invalid ID as argument 3: %s");   //unique id to control statistics
38   char *id_alias = xbt_malloc(20*sizeof(char));
39   snprintf(id_alias, 20, "flow_%d", id);
40   workernames[id] = workername;
41   TRACE_category(id_alias);
42
43   masternames[id] = MSG_host_get_name(MSG_host_self());
44
45   {                             /*  Task creation.  */
46     todo = MSG_task_create("Task_0", 100*task_comm_size, task_comm_size, NULL);
47     MSG_task_set_category(todo, id_alias);
48     //keep track of running tasks
49     gl_task_array[id] = todo;
50     gl_data_size[id] = task_comm_size;
51   }
52
53   MSG_host_by_name(workername);
54
55   count_finished++;
56   timer_start = 1 ;
57
58   /* time measurement */
59   snprintf(id_alias,20,"%d", id);
60   start_time = MSG_get_clock();
61   MSG_task_send(todo, id_alias);
62   end_time = MSG_get_clock();
63
64   XBT_DEBUG ("Finished");
65   xbt_free(id_alias);
66   return 0;
67 }
68
69 static int timer(int argc, char *argv[])
70 {
71   double sleep_time;
72   double first_sleep;
73
74   xbt_assert(argc==3,"Strange number of arguments expected 2 got %d", argc - 1);
75
76   sscanf(argv[1], "%lf", &first_sleep);
77   sscanf(argv[2], "%lf", &sleep_time);
78
79   XBT_DEBUG ("Timer started");
80
81   if(first_sleep){
82       MSG_process_sleep(first_sleep);
83   }
84
85   do {
86     XBT_DEBUG ("Get sleep");
87     MSG_process_sleep(sleep_time);
88   } while(timer_start);
89
90   XBT_DEBUG ("Finished");
91   return 0;
92 }
93
94 static int worker(int argc, char *argv[])
95 {
96   msg_task_t task = NULL;
97   char id_alias[10];
98
99   xbt_assert(argc==2,"Strange number of arguments expected 1 got %d", argc - 1);
100
101   XBT_DEBUG ("Worker started");
102
103   int id = xbt_str_parse_int(argv[1], "Invalid id: %s");
104   snprintf(id_alias,10, "%d", id);
105
106   msg_error_t a = MSG_task_receive(&(task), id_alias);
107
108   count_finished--;
109   if(count_finished == 0){
110       timer_start = 0;
111   }
112
113   xbt_assert(a == MSG_OK,"Hey?! What's up? Unexpected behavior");
114
115   elapsed_time = MSG_get_clock() - start_time;
116
117   XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s", id, MSG_task_get_bytes_amount(task), masternames[id],
118            workernames[id]);
119 //  MSG_task_execute(task);
120   MSG_task_destroy(task);
121
122   XBT_DEBUG ("Finished");
123   return 0;
124 }
125
126 int main(int argc, char *argv[])
127 {
128   MSG_init(&argc, argv);
129   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
130              "\tExample: %s platform.xml deployment.xml\n", argv[0], argv[0]);
131
132   MSG_create_environment(argv[1]);
133   TRACE_declare_mark("endmark");
134
135   MSG_function_register("master", master);
136   MSG_function_register("worker", worker);
137   MSG_function_register("timer", timer);
138
139   MSG_launch_application(argv[2]);
140
141   msg_error_t res = MSG_main();
142
143   return res != MSG_OK;
144 }