Logo AND Algorithmique Numérique Distribuée

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