Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
17c8002669ea32e51d32998c9fae00dce1409064
[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
16 #include "xbt/log.h"
17 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test, "Messages specific for surf example");
18
19 static const char *string_action(simgrid::surf::Action::State state)
20 {
21   switch (state) {
22   case (simgrid::surf::Action::State::ready):
23     return "SURF_ACTION_READY";
24   case (simgrid::surf::Action::State::running):
25     return "SURF_ACTION_RUNNING";
26   case (simgrid::surf::Action::State::failed):
27     return "SURF_ACTION_FAILED";
28   case (simgrid::surf::Action::State::done):
29     return "SURF_ACTION_DONE";
30   case (simgrid::surf::Action::State::not_in_the_system):
31     return "SURF_ACTION_NOT_IN_THE_SYSTEM";
32   default:
33     return "INVALID STATE";
34   }
35 }
36
37 int main(int argc, char **argv)
38 {
39   double now = -1.0;
40   surf_init(&argc, argv);       /* Initialize some common structures */
41   xbt_cfg_set_parse(_sg_cfg_set, "cpu/model:Cas01");
42   xbt_cfg_set_parse(_sg_cfg_set, "network/model:CM02");
43
44   xbt_assert(argc >1, "Usage : %s platform.txt\n", argv[0]);
45   parse_platform_file(argv[1]);
46
47   XBT_DEBUG("CPU model: %p", surf_cpu_model_pm);
48   XBT_DEBUG("Network model: %p", surf_network_model);
49   sg_host_t hostA = sg_host_by_name("Cpu A");
50   sg_host_t hostB = sg_host_by_name("Cpu B");
51
52   /* Let's check that those two processors exist */
53   XBT_DEBUG("%s : %p", sg_host_get_name(hostA), hostA);
54   XBT_DEBUG("%s : %p", sg_host_get_name(hostB), hostB);
55
56   /* Let's do something on it */
57   surf_action_t actionA = hostA->pimpl_cpu->execution_start(1000.0);
58   surf_action_t actionB = hostB->pimpl_cpu->execution_start(1000.0);
59   surf_action_t actionC = surf_host_sleep(hostB, 7.32);
60
61   /* Use whatever calling style you want... */
62   simgrid::surf::Action::State stateActionA = actionA->getState(); /* When you know actionA model type */
63   simgrid::surf::Action::State stateActionB = actionB->getState(); /* If you're unsure about it's model type */
64   simgrid::surf::Action::State stateActionC = actionC->getState(); /* When you know actionA model type */
65
66   /* And just look at the state of these tasks */
67   XBT_INFO("actionA state: %s", string_action(stateActionA));
68   XBT_INFO("actionB state: %s", string_action(stateActionB));
69   XBT_INFO("actionC state: %s", string_action(stateActionC));
70
71
72   /* Let's do something on it */
73   surf_network_model_communicate(surf_network_model, hostA, hostB, 150.0, -1.0);
74
75   surf_solve(-1.0);
76   do {
77     surf_action_t action = NULL;
78     now = surf_get_clock();
79     XBT_INFO("Next Event : %g", now);
80     XBT_DEBUG("\t CPU actions");
81     while ((action = surf_model_extract_failed_action_set((surf_model_t)surf_cpu_model_pm))) {
82        XBT_INFO("   CPU Failed action");
83        XBT_DEBUG("\t * Failed : %p", action);
84        action->unref();
85     }
86     while ((action = surf_model_extract_done_action_set((surf_model_t)surf_cpu_model_pm))) {
87       XBT_INFO("   CPU Done action");
88       XBT_DEBUG("\t * Done : %p", action);
89       action->unref();
90     }
91     XBT_DEBUG("\t Network actions");
92     while ((action = surf_model_extract_failed_action_set((surf_model_t)surf_network_model))) {
93       XBT_INFO("   Network Failed action");
94       XBT_DEBUG("\t * Failed : %p", action);
95       action->unref();
96     }
97     while ((action = surf_model_extract_done_action_set((surf_model_t)surf_network_model))) {
98       XBT_INFO("   Network Failed action");
99       XBT_DEBUG("\t * Done : %p", action);
100       action->unref();
101     }
102
103   } while ((surf_model_running_action_set_size((surf_model_t)surf_network_model) ||
104             surf_model_running_action_set_size((surf_model_t)surf_cpu_model_pm)) && surf_solve(-1.0) >= 0.0);
105
106   XBT_DEBUG("Simulation Terminated");
107
108   surf_exit();
109   return 0;
110 }