Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix tests.
[simgrid.git] / testsuite / surf / surf_usage2.c
1 /*      $Id$     */
2
3 /* A few basic tests for the surf library                                   */
4
5 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9 #ifdef __BORLANDC__
10 #pragma hdrstop
11 #endif
12
13 #include <stdio.h>
14 #include "surf/surf.h"
15
16 #include "xbt/log.h"
17 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test, "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   void *workstationA = NULL;
43   void *workstationB = NULL;
44   surf_action_t actionA = NULL;
45   surf_action_t actionB = NULL;
46   surf_action_t actionC = NULL;
47   surf_action_t commAB = NULL;
48   double now = -1.0;
49   int running;
50
51   int workstation_id =
52     find_model_description(surf_workstation_model_description, "CLM03");
53
54   surf_workstation_model_description[workstation_id].model_init_preparse(platform);
55   parse_platform_file(platform);
56   if (surf_workstation_model_description[workstation_id].model_init_postparse)
57     surf_workstation_model_description[workstation_id].model_init_postparse();
58
59   /*********************** WORKSTATION ***********************************/
60   workstationA = surf_model_resource_by_name(surf_workstation_model,"Cpu A");
61   workstationB = surf_model_resource_by_name(surf_workstation_model,"Cpu B");
62
63   /* Let's check that those two processors exist */
64   DEBUG2("%s : %p", surf_resource_name(workstationA), workstationA);
65   DEBUG2("%s : %p", surf_resource_name(workstationB), workstationB);
66
67   /* Let's do something on it */
68   actionA =
69     surf_workstation_model->extension.workstation.execute(workstationA, 1000.0);
70   actionB =
71     surf_workstation_model->extension.workstation.execute(workstationB, 1000.0);
72   actionC =
73     surf_workstation_model->extension.workstation.sleep(workstationB, 7.32);
74
75   commAB =
76     surf_workstation_model->extension.workstation.communicate(workstationA,
77                                                           workstationB, 150.0,
78                                                           -1.0);
79
80   surf_solve();                 /* Takes traces into account. Returns 0.0 */
81   do {
82     surf_action_t action = NULL;
83     unsigned int iter;
84     surf_model_t model = NULL;
85     running = 0;
86
87     now = surf_get_clock();
88     DEBUG1("Next Event : %g", now);
89
90     xbt_dynar_foreach(model_list, iter, model) {
91       DEBUG1("\t %s actions", model->name);
92       while ((action =
93               xbt_swag_extract(model->states.failed_action_set))) {
94         DEBUG1("\t * Failed : %p", action);
95         model->action_unref(action);
96       }
97       while ((action =
98               xbt_swag_extract(model->states.done_action_set))) {
99         DEBUG1("\t * Done : %p", action);
100         model->action_unref(action);
101       }
102       if (xbt_swag_size(model->states.running_action_set)) {
103         DEBUG1("running %s", model->name);
104         running = 1;
105       }
106     }
107   } while (running && surf_solve() >= 0.0);
108
109   DEBUG0("Simulation Terminated");
110
111 }
112
113 #ifdef __BORLANDC__
114 #pragma argsused
115 #endif
116
117 int main(int argc, char **argv)
118 {
119   surf_init(&argc, argv);       /* Initialize some common structures */
120   if (argc == 1) {
121     fprintf(stderr, "Usage : %s platform.txt\n", argv[0]);
122     surf_exit();
123     return 1;
124   }
125   test(argv[1]);
126
127   surf_exit();
128   return 0;
129 }