Logo AND Algorithmique Numérique Distribuée

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