Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid into tomerge
[simgrid.git] / teshsuite / surf / surf_usage / surf_usage2.c
1 /* A few basic tests for the surf library                                   */
2
3 /* Copyright (c) 2004-2014. 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 #ifdef __BORLANDC__
10 #pragma hdrstop
11 #endif
12
13 #include <stdio.h>
14 #include "simgrid/sg_config.h"
15 #include "surf/surf.h"
16 #include "surf/surfxml_parse.h" // for reset callback
17
18 #include "xbt/log.h"
19 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test,
20                              "Messages specific for surf example");
21
22 const char *string_action(e_surf_action_state_t state);
23 const char *string_action(e_surf_action_state_t state)
24 {
25   switch (state) {
26   case (SURF_ACTION_READY):
27     return "SURF_ACTION_READY";
28   case (SURF_ACTION_RUNNING):
29     return "SURF_ACTION_RUNNING";
30   case (SURF_ACTION_FAILED):
31     return "SURF_ACTION_FAILED";
32   case (SURF_ACTION_DONE):
33     return "SURF_ACTION_DONE";
34   case (SURF_ACTION_NOT_IN_THE_SYSTEM):
35     return "SURF_ACTION_NOT_IN_THE_SYSTEM";
36   default:
37     return "INVALID STATE";
38   }
39 }
40
41
42 void test(char *platform);
43 void test(char *platform)
44 {
45   sg_host_t hostA = NULL;
46   sg_host_t hostB = NULL;
47   double now = -1.0;
48   int running;
49
50   xbt_cfg_set_parse(_sg_cfg_set, "network/model:CM02");
51   xbt_cfg_set_parse(_sg_cfg_set, "cpu/model:Cas01");
52   parse_platform_file(platform);
53
54   /*********************** HOST ***********************************/
55   hostA = sg_host_by_name("Cpu A");
56   hostB = sg_host_by_name("Cpu B");
57
58   /* Let's check that those two processors exist */
59   XBT_DEBUG("%s : %p", sg_host_get_name(hostA), hostA);
60   XBT_DEBUG("%s : %p", sg_host_get_name(hostB), hostB);
61
62   /* Let's do something on it */
63   surf_host_execute(hostA, 1000.0);
64   surf_host_execute(hostB, 1000.0);
65   surf_host_sleep(hostB, 7.32);
66
67   surf_network_model_communicate(surf_network_model, hostA, hostB, 150.0, -1.0);
68
69   surf_solve(-1.0);                 /* Takes traces into account. Returns 0.0 */
70   do {
71     surf_action_t action = NULL;
72     unsigned int iter;
73     surf_model_t model = NULL;
74     running = 0;
75
76     now = surf_get_clock();
77     XBT_DEBUG("Next Event : %g", now);
78
79     xbt_dynar_foreach(model_list, iter, model) {
80       XBT_DEBUG("\t %s actions", surf_model_name(model));
81       while ((action = surf_model_extract_failed_action_set((surf_model_t)model))) {
82         XBT_DEBUG("\t * Failed : %p", action);
83         surf_action_unref(action);
84       }
85       while ((action = surf_model_extract_done_action_set((surf_model_t)model))) {
86         XBT_DEBUG("\t * Done : %p", action);
87         surf_action_unref(action);
88       }
89       if (surf_model_running_action_set_size((surf_model_t)model)) {
90         XBT_DEBUG("running %s", surf_model_name(model));
91         running = 1;
92       }
93     }
94   } while (running && surf_solve(-1.0) >= 0.0);
95
96   XBT_DEBUG("Simulation Terminated");
97
98 }
99
100 #ifdef __BORLANDC__
101 #pragma argsused
102 #endif
103
104 int main(int argc, char **argv)
105 {
106   surf_init(&argc, argv);       /* Initialize some common structures */
107   if (argc == 1) {
108     fprintf(stderr, "Usage : %s platform.txt\n", argv[0]);
109     surf_exit();
110     return 1;
111   }
112   test(argv[1]);
113
114   surf_exit();
115   return 0;
116 }