Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove old function surf_model_resource_by_name.
[simgrid.git] / testsuite / surf / surf_usage2.c
1 /* A few basic tests for the surf library                                   */
2
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. 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 #ifdef __BORLANDC__
9 #pragma hdrstop
10 #endif
11
12 #include <stdio.h>
13 #include "surf/surf.h"
14 #include "surf/surfxml_parse.h" // for reset callback
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
40 void test(char *platform);
41 void test(char *platform)
42 {
43   void *workstationA = NULL;
44   void *workstationB = NULL;
45   surf_action_t actionA = NULL;
46   surf_action_t actionB = NULL;
47   surf_action_t actionC = NULL;
48   surf_action_t commAB = NULL;
49   double now = -1.0;
50   int running;
51
52   int workstation_id =
53       find_model_description(surf_workstation_model_description, "CLM03");
54   surf_parse_reset_callbacks();
55   surf_workstation_model_description[workstation_id].
56       model_init_preparse(platform);
57   parse_platform_file(platform);
58   if (surf_workstation_model_description[workstation_id].
59       model_init_postparse)
60     surf_workstation_model_description[workstation_id].
61         model_init_postparse();
62
63   /*********************** WORKSTATION ***********************************/
64   workstationA =
65       surf_workstation_resource_by_name("Cpu A");
66   workstationB =
67       surf_workstation_resource_by_name("Cpu B");
68
69   /* Let's check that those two processors exist */
70   XBT_DEBUG("%s : %p", surf_resource_name(workstationA), workstationA);
71   XBT_DEBUG("%s : %p", surf_resource_name(workstationB), workstationB);
72
73   /* Let's do something on it */
74   actionA =
75       surf_workstation_model->extension.workstation.execute(workstationA,
76                                                             1000.0);
77   actionB =
78       surf_workstation_model->extension.workstation.execute(workstationB,
79                                                             1000.0);
80   actionC =
81       surf_workstation_model->extension.workstation.sleep(workstationB,
82                                                           7.32);
83
84   commAB =
85       surf_workstation_model->extension.workstation.
86       communicate(workstationA, workstationB, 150.0, -1.0);
87
88   surf_solve(-1.0);                 /* Takes traces into account. Returns 0.0 */
89   do {
90     surf_action_t action = NULL;
91     unsigned int iter;
92     surf_model_t model = NULL;
93     running = 0;
94
95     now = surf_get_clock();
96     XBT_DEBUG("Next Event : %g", now);
97
98     xbt_dynar_foreach(model_list, iter, model) {
99       XBT_DEBUG("\t %s actions", model->name);
100       while ((action = xbt_swag_extract(model->states.failed_action_set))) {
101         XBT_DEBUG("\t * Failed : %p", action);
102         model->action_unref(action);
103       }
104       while ((action = xbt_swag_extract(model->states.done_action_set))) {
105         XBT_DEBUG("\t * Done : %p", action);
106         model->action_unref(action);
107       }
108       if (xbt_swag_size(model->states.running_action_set)) {
109         XBT_DEBUG("running %s", model->name);
110         running = 1;
111       }
112     }
113   } while (running && surf_solve(-1.0) >= 0.0);
114
115   XBT_DEBUG("Simulation Terminated");
116
117 }
118
119 #ifdef __BORLANDC__
120 #pragma argsused
121 #endif
122
123 int main(int argc, char **argv)
124 {
125   surf_init(&argc, argv);       /* Initialize some common structures */
126   if (argc == 1) {
127     fprintf(stderr, "Usage : %s platform.txt\n", argv[0]);
128     surf_exit();
129     return 1;
130   }
131   test(argv[1]);
132
133   surf_exit();
134   return 0;
135 }