Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
C++ify this example + fix bug in tesh
[simgrid.git] / teshsuite / surf / surf_usage / surf_usage.cpp
1 /* A few basic tests for the surf library                                   */
2
3 /* Copyright (c) 2004-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <stdio.h>
10 #include "simgrid/sg_config.h"
11 #include "simgrid/host.h"
12 #include "surf/surf.h"
13 #include "src/surf/surf_interface.hpp"
14 #include "src/surf/cpu_interface.hpp"
15 #include "src/surf/network_interface.hpp"
16
17 #include "xbt/log.h"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test, "Messages specific for surf example");
19
20 static const char *string_action(simgrid::surf::Action::State state)
21 {
22   switch (state) {
23   case (simgrid::surf::Action::State::ready):
24     return "SURF_ACTION_READY";
25   case (simgrid::surf::Action::State::running):
26     return "SURF_ACTION_RUNNING";
27   case (simgrid::surf::Action::State::failed):
28     return "SURF_ACTION_FAILED";
29   case (simgrid::surf::Action::State::done):
30     return "SURF_ACTION_DONE";
31   case (simgrid::surf::Action::State::not_in_the_system):
32     return "SURF_ACTION_NOT_IN_THE_SYSTEM";
33   default:
34     return "INVALID STATE";
35   }
36 }
37
38 int main(int argc, char **argv)
39 {
40   double now = -1.0;
41   surf_init(&argc, argv);       /* Initialize some common structures */
42   xbt_cfg_set_parse("cpu/model:Cas01");
43   xbt_cfg_set_parse("network/model:CM02");
44
45   xbt_assert(argc >1, "Usage : %s platform.txt\n", argv[0]);
46   parse_platform_file(argv[1]);
47
48   XBT_DEBUG("CPU model: %p", surf_cpu_model_pm);
49   XBT_DEBUG("Network model: %p", surf_network_model);
50   sg_host_t hostA = sg_host_by_name("Cpu A");
51   sg_host_t hostB = sg_host_by_name("Cpu B");
52
53   /* Let's check that those two processors exist */
54   XBT_DEBUG("%s : %p", sg_host_get_name(hostA), hostA);
55   XBT_DEBUG("%s : %p", sg_host_get_name(hostB), hostB);
56
57   /* Let's do something on it */
58   simgrid::surf::Action *actionA = hostA->pimpl_cpu->execution_start(1000.0);
59   simgrid::surf::Action *actionB = hostB->pimpl_cpu->execution_start(1000.0);
60   simgrid::surf::Action *actionC = surf_host_sleep(hostB, 7.32);
61
62   /* Use whatever calling style you want... */
63   simgrid::surf::Action::State stateActionA = actionA->getState(); /* When you know actionA model type */
64   simgrid::surf::Action::State stateActionB = actionB->getState(); /* If you're unsure about it's model type */
65   simgrid::surf::Action::State stateActionC = actionC->getState(); /* When you know actionA model type */
66
67   /* And just look at the state of these tasks */
68   XBT_INFO("actionA state: %s", string_action(stateActionA));
69   XBT_INFO("actionB state: %s", string_action(stateActionB));
70   XBT_INFO("actionC state: %s", string_action(stateActionC));
71
72
73   /* Let's do something on it */
74   surf_network_model_communicate(surf_network_model, hostA, hostB, 150.0, -1.0);
75
76   surf_solve(-1.0);
77   do {
78     simgrid::surf::ActionList *action_list = nullptr;
79     now = surf_get_clock();
80     XBT_INFO("Next Event : %g", now);
81     XBT_DEBUG("\t CPU actions");
82
83     action_list = surf_cpu_model_pm->getFailedActionSet();
84     for(simgrid::surf::ActionList::iterator it(action_list->begin()), itNext = it, itend(action_list->end()) ;
85         it != itend ; it=itNext) {
86       ++itNext;
87       simgrid::surf::Action *action = static_cast<simgrid::surf::CpuAction*>(&*it);
88        XBT_INFO("   CPU Failed action");
89        XBT_DEBUG("\t * Failed : %p", action);
90        action->unref();
91     }
92
93     action_list = surf_cpu_model_pm->getDoneActionSet();
94     for(simgrid::surf::ActionList::iterator it(action_list->begin()), itNext = it, itend(action_list->end()) ;
95         it != itend ; it=itNext) {
96       ++itNext;
97       simgrid::surf::Action *action = static_cast<simgrid::surf::CpuAction*>(&*it);
98       XBT_INFO("   CPU Done action");
99       XBT_DEBUG("\t * Done : %p", action);
100       action->unref();
101     }
102
103     action_list = surf_network_model->getFailedActionSet();
104     for(simgrid::surf::ActionList::iterator it(action_list->begin()), itNext = it, itend(action_list->end()) ;
105         it != itend ; it=itNext) {
106       ++itNext;
107       simgrid::surf::Action *action = static_cast<simgrid::surf::NetworkAction*>(&*it);
108        XBT_INFO("   Network Failed action");
109        XBT_DEBUG("\t * Failed : %p", action);
110        action->unref();
111     }
112
113     action_list = surf_network_model->getDoneActionSet();
114     for(simgrid::surf::ActionList::iterator it(action_list->begin()), itNext = it, itend(action_list->end()) ;
115         it != itend ; it=itNext) {
116       ++itNext;
117       simgrid::surf::Action *action = static_cast<simgrid::surf::NetworkAction*>(&*it);
118       XBT_INFO("   Network Done action");
119       XBT_DEBUG("\t * Done : %p", action);
120       action->unref();
121     }
122
123   } while ((surf_network_model->getRunningActionSet()->size() ||
124            surf_cpu_model_pm->getRunningActionSet()->size()) && surf_solve(-1.0) >= 0.0);
125
126   XBT_DEBUG("Simulation Terminated");
127
128   surf_exit();
129   return 0;
130 }