Logo AND Algorithmique Numérique Distribuée

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