Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
control better random, returning to assertion
[simgrid.git] / teshsuite / surf / surf_usage / surf_usage2.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 "src/surf/surf_interface.hpp"
13 #include "src/surf/cpu_interface.hpp"
14
15 #include "xbt/log.h"
16 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test,
17                              "Messages specific for surf example");
18
19 const char *string_action(e_surf_action_state_t state);
20 const char *string_action(e_surf_action_state_t state)
21 {
22   switch (state) {
23   case (SURF_ACTION_READY):
24     return "SURF_ACTION_READY";
25   case (SURF_ACTION_RUNNING):
26     return "SURF_ACTION_RUNNING";
27   case (SURF_ACTION_FAILED):
28     return "SURF_ACTION_FAILED";
29   case (SURF_ACTION_DONE):
30     return "SURF_ACTION_DONE";
31   case (SURF_ACTION_NOT_IN_THE_SYSTEM):
32     return "SURF_ACTION_NOT_IN_THE_SYSTEM";
33   default:
34     return "INVALID STATE";
35   }
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   double now = -1.0;
45   int running;
46
47   xbt_cfg_set_parse(_sg_cfg_set, "network/model:CM02");
48   xbt_cfg_set_parse(_sg_cfg_set, "cpu/model:Cas01");
49   parse_platform_file(platform);
50
51   /*********************** HOST ***********************************/
52   hostA = sg_host_by_name("Cpu A");
53   hostB = sg_host_by_name("Cpu B");
54
55   /* Let's check that those two processors exist */
56   XBT_DEBUG("%s : %p", sg_host_get_name(hostA), hostA);
57   XBT_DEBUG("%s : %p", sg_host_get_name(hostB), hostB);
58
59   /* Let's do something on it */
60   hostA->pimpl_cpu->execution_start(1000.0);
61   hostB->pimpl_cpu->execution_start(1000.0);
62   surf_host_sleep(hostB, 7.32);
63
64   surf_network_model_communicate(surf_network_model, hostA, hostB, 150.0, -1.0);
65
66   surf_solve(-1.0);                 /* Takes traces into account. Returns 0.0 */
67   do {
68     surf_action_t action = NULL;
69     unsigned int iter;
70     surf_model_t model = NULL;
71     running = 0;
72
73     now = surf_get_clock();
74     XBT_DEBUG("Next Event : %g", now);
75
76     xbt_dynar_foreach(all_existing_models, iter, model) {
77       XBT_DEBUG("\t Actions");
78       while ((action = surf_model_extract_failed_action_set((surf_model_t)model))) {
79         XBT_DEBUG("\t * Failed : %p", action);
80         action->unref();
81       }
82       while ((action = surf_model_extract_done_action_set((surf_model_t)model))) {
83         XBT_DEBUG("\t * Done : %p", action);
84         action->unref();
85       }
86       if (surf_model_running_action_set_size((surf_model_t)model)) {
87         XBT_DEBUG("running that model");
88         running = 1;
89       }
90     }
91   } while (running && surf_solve(-1.0) >= 0.0);
92
93   XBT_DEBUG("Simulation Terminated");
94
95 }
96
97 int main(int argc, char **argv)
98 {
99   surf_init(&argc, argv);       /* Initialize some common structures */
100   if (argc == 1) {
101     fprintf(stderr, "Usage : %s platform.txt\n", argv[0]);
102     surf_exit();
103     return 1;
104   }
105   test(argv[1]);
106
107   surf_exit();
108   return 0;
109 }