Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Windows compatibility.
[simgrid.git] / testsuite / surf / surf_usage.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
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 void test(char *platform);
39 void test(char *platform)
40 {
41   void *cpuA = NULL;
42   void *cpuB = NULL;
43   void *cardA = NULL;
44   void *cardB = 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   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
54   surf_cpu_model_init_Cas01(platform);  /* Now it is possible to use CPUs */
55   surf_network_model_init_CM02(platform);       /* Now it is possible to use eth0 */
56
57   parse_platform_file(platform);
58
59   /*********************** CPU ***********************************/
60   DEBUG1("%p", surf_cpu_model);
61   cpuA = surf_model_resource_by_name(surf_cpu_model,"Cpu A");
62   cpuB = surf_model_resource_by_name(surf_cpu_model,"Cpu B");
63
64   /* Let's check that those two processors exist */
65   DEBUG2("%s : %p",surf_resource_name(cpuA), cpuA);
66   DEBUG2("%s : %p",surf_resource_name(cpuB), cpuB);
67
68   /* Let's do something on it */
69   actionA = surf_cpu_model->extension.cpu.execute(cpuA, 1000.0);
70   actionB = surf_cpu_model->extension.cpu.execute(cpuB, 1000.0);
71   actionC = surf_cpu_model->extension.cpu.sleep(cpuB, 7.32);
72
73   /* Use whatever calling style you want... */
74   stateActionA = surf_cpu_model->action_state_get(actionA);      /* When you know actionA model type */
75   stateActionB = actionB->model_type->action_state_get(actionB); /* If you're unsure about it's model type */
76   stateActionC = surf_cpu_model->action_state_get(actionC);      /* When you know actionA model type */
77
78   /* And just look at the state of these tasks */
79   DEBUG2("actionA : %p (%s)", actionA, string_action(stateActionA));
80   DEBUG2("actionB : %p (%s)", actionB, string_action(stateActionB));
81   DEBUG2("actionC : %p (%s)", actionB, string_action(stateActionC));
82
83   /*********************** Network *******************************/
84   DEBUG1("%p", surf_network_model);
85   cardA = surf_model_resource_by_name(surf_network_model,"Cpu A");
86   cardB = surf_model_resource_by_name(surf_network_model,"Cpu B");
87
88   /* Let's check that those two processors exist */
89   DEBUG2("%s : %p", surf_resource_name(cardA), cardA);
90   DEBUG2("%s : %p", surf_resource_name(cardB), cardB);
91
92   /* Let's do something on it */
93   commAB =
94     surf_network_model->extension.network.communicate("Cpu A","Cpu B", 0, 1, /* FIXME: hardcoding host number is bad */
95                                                       150.0, -1.0);
96
97   surf_solve();                 /* Takes traces into account. Returns 0.0 */
98   do {
99     surf_action_t action = NULL;
100     now = surf_get_clock();
101     DEBUG1("Next Event : %g", now);
102     DEBUG0("\t CPU actions");
103     while ((action =
104             xbt_swag_extract(surf_cpu_model->states.failed_action_set))) {
105       DEBUG1("\t * Failed : %p", action);
106       action->model_type->action_unref(action);
107     }
108     while ((action =
109             xbt_swag_extract(surf_cpu_model->states.done_action_set))) {
110       DEBUG1("\t * Done : %p", action);
111       action->model_type->action_unref(action);
112     }
113     DEBUG0("\t Network actions");
114     while ((action =
115             xbt_swag_extract(surf_network_model->states.failed_action_set))) {
116       DEBUG1("\t * Failed : %p", action);
117       action->model_type->action_unref(action);
118     }
119     while ((action =
120             xbt_swag_extract(surf_network_model->states.done_action_set))) {
121       DEBUG1("\t * Done : %p", action);
122       action->model_type->action_unref(action);
123     }
124
125   } while ((xbt_swag_size(surf_network_model->states.running_action_set) ||
126             xbt_swag_size(surf_cpu_model->states.running_action_set)) &&
127             surf_solve() >= 0.0);
128
129   DEBUG0("Simulation Terminated");
130 }
131
132 #ifdef __BORLANDC__
133 #pragma argsused
134 #endif
135
136 int main(int argc, char **argv)
137 {
138   surf_init(&argc, argv);       /* Initialize some common structures */
139   if (argc == 1) {
140     fprintf(stderr, "Usage : %s platform.xml\n", argv[0]);
141     return 1;
142   }
143   test(argv[1]);
144
145   surf_exit();
146   return 0;
147 }