Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
46fbbbd6bfe06434d6f2d96f20b6427828335eb3
[simgrid.git] / teshsuite / surf / surf_usage / surf_usage.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 void test(char *platform);
42 void test(char *platform)
43 {
44   sg_host_t cpuA = NULL;
45   sg_host_t cpuB = NULL;
46   surf_action_t actionA = NULL;
47   surf_action_t actionB = NULL;
48   surf_action_t actionC = NULL;
49   e_surf_action_state_t stateActionA;
50   e_surf_action_state_t stateActionB;
51   e_surf_action_state_t stateActionC;
52   double now = -1.0;
53   xbt_cfg_set_parse(_sg_cfg_set, "cpu/model:Cas01");
54   xbt_cfg_set_parse(_sg_cfg_set, "network/model:CM02");
55   parse_platform_file(platform);
56
57   /*********************** CPU ***********************************/
58   XBT_DEBUG("%p", surf_cpu_model_pm);
59   cpuA = sg_host_by_name("Cpu A");
60   cpuB = sg_host_by_name("Cpu B");
61
62   /* Let's check that those two processors exist */
63   XBT_DEBUG("%s : %p", surf_cpu_name(sg_host_surfcpu(cpuA)), cpuA);
64   XBT_DEBUG("%s : %p", surf_cpu_name(sg_host_surfcpu(cpuB)), cpuB);
65
66   /* Let's do something on it */
67   actionA = surf_cpu_execute(cpuA, 1000.0);
68   actionB = surf_cpu_execute(cpuB, 1000.0);
69   actionC = surf_cpu_sleep(cpuB, 7.32);
70
71   /* Use whatever calling style you want... */
72   stateActionA = surf_action_get_state(actionA);     /* When you know actionA model type */
73   stateActionB = surf_action_get_state(actionB);        /* If you're unsure about it's model type */
74   stateActionC = surf_action_get_state(actionC);     /* When you know actionA model type */
75
76   /* And just look at the state of these tasks */
77   XBT_DEBUG("actionA : %p (%s)", actionA, string_action(stateActionA));
78   XBT_DEBUG("actionB : %p (%s)", actionB, string_action(stateActionB));
79   XBT_DEBUG("actionC : %p (%s)", actionB, string_action(stateActionC));
80
81   /*********************** Network *******************************/
82   XBT_DEBUG("%p", surf_network_model);
83
84   /* Let's do something on it */
85   surf_network_model_communicate(surf_network_model, cpuA, cpuB, 150.0, -1.0);
86
87   surf_solve(-1.0);                 /* Takes traces into account. Returns 0.0 */
88   do {
89     surf_action_t action = NULL;
90     now = surf_get_clock();
91     XBT_DEBUG("Next Event : %g", now);
92     XBT_DEBUG("\t CPU actions");
93     while ((action =
94             surf_model_extract_failed_action_set((surf_model_t)surf_cpu_model_pm))) {
95       XBT_DEBUG("\t * Failed : %p", action);
96       surf_action_unref(action);
97     }
98     while ((action =
99             surf_model_extract_done_action_set((surf_model_t)surf_cpu_model_pm))) {
100       XBT_DEBUG("\t * Done : %p", action);
101       surf_action_unref(action);
102     }
103     XBT_DEBUG("\t Network actions");
104     while ((action =
105             surf_model_extract_failed_action_set((surf_model_t)surf_network_model))) {
106       XBT_DEBUG("\t * Failed : %p", action);
107       surf_action_unref(action);
108     }
109     while ((action =
110             surf_model_extract_done_action_set((surf_model_t)surf_network_model))) {
111       XBT_DEBUG("\t * Done : %p", action);
112       surf_action_unref(action);
113     }
114
115   } while ((surf_model_running_action_set_size((surf_model_t)surf_network_model) ||
116             surf_model_running_action_set_size((surf_model_t)surf_cpu_model_pm)) &&
117            surf_solve(-1.0) >= 0.0);
118
119   XBT_DEBUG("Simulation Terminated");
120 }
121
122 #ifdef __BORLANDC__
123 #pragma argsused
124 #endif
125
126 int main(int argc, char **argv)
127 {
128   surf_init(&argc, argv);       /* Initialize some common structures */
129   if (argc == 1) {
130     fprintf(stderr, "Usage : %s platform.xml\n", argv[0]);
131     return 1;
132   }
133   test(argv[1]);
134
135   surf_exit();
136   return 0;
137 }