Logo AND Algorithmique Numérique Distribuée

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