Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
C files also need to know whether they're compiling with CSDP or not.
[simgrid.git] / testsuite / surf / surf_usage.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  
10 #ifdef __BORLANDC__
11 #pragma hdrstop
12 #endif
13
14 #include <stdio.h>
15 #include "surf/surf.h"
16
17 #include "xbt/log.h"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test,"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 *cpuA = NULL;
44   void *cpuB = NULL;
45   void *cardA = NULL;
46   void *cardB = NULL;
47   surf_action_t actionA = NULL;
48   surf_action_t actionB = NULL;
49   surf_action_t actionC = NULL;
50   surf_action_t commAB = NULL;
51   e_surf_action_state_t stateActionA;
52   e_surf_action_state_t stateActionB;
53   e_surf_action_state_t stateActionC;
54   double now = -1.0;
55
56   surf_cpu_resource_init_Cas01(platform);       /* Now it is possible to use CPUs */
57   surf_network_resource_init_CM02(platform);    /* Now it is possible to use eth0 */
58
59   /*********************** CPU ***********************************/
60   DEBUG1("%p \n", surf_cpu_resource);
61   cpuA = surf_cpu_resource->common_public->name_service("Cpu A");
62   cpuB = surf_cpu_resource->common_public->name_service("Cpu B");
63
64   /* Let's check that those two processors exist */
65   DEBUG2("%s : %p\n",
66          surf_cpu_resource->common_public->get_resource_name(cpuA), cpuA);
67   DEBUG2("%s : %p\n",
68          surf_cpu_resource->common_public->get_resource_name(cpuB), cpuB);
69
70   /* Let's do something on it */
71   actionA = surf_cpu_resource->extension_public->execute(cpuA, 1000.0);
72   actionB = surf_cpu_resource->extension_public->execute(cpuB, 1000.0);
73   actionC = surf_cpu_resource->extension_public->sleep(cpuB, 7.32);
74
75   /* Use whatever calling style you want... */
76   stateActionA = surf_cpu_resource->common_public->action_get_state(actionA);   /* When you know actionA resource type */
77   stateActionB = actionB->resource_type->common_public->action_get_state(actionB);      /* If you're unsure about it's resource type */
78   stateActionC = surf_cpu_resource->common_public->action_get_state(actionC);   /* When you know actionA resource type */
79
80   /* And just look at the state of these tasks */
81   DEBUG2("actionA : %p (%s)\n", actionA, string_action(stateActionA));
82   DEBUG2("actionB : %p (%s)\n", actionB, string_action(stateActionB));
83   DEBUG2("actionC : %p (%s)\n", actionB, string_action(stateActionC));
84
85   /*********************** Network *******************************/
86   DEBUG1("%p \n", surf_network_resource);
87   cardA = surf_network_resource->common_public->name_service("Cpu A");
88   cardB = surf_network_resource->common_public->name_service("Cpu B");
89
90   /* Let's check that those two processors exist */
91   DEBUG2("%s : %p\n",
92          surf_network_resource->common_public->get_resource_name(cardA),
93          cardA);
94   DEBUG2("%s : %p\n",
95          surf_network_resource->common_public->get_resource_name(cardB),
96          cardB);
97
98   /* Let's do something on it */
99   commAB =
100       surf_network_resource->extension_public->communicate(cardA, cardB,
101                                                            150.0,-1.0);
102
103   surf_solve();                 /* Takes traces into account. Returns 0.0 */
104   do {
105     surf_action_t action = NULL;
106     now = surf_get_clock();
107     DEBUG1("Next Event : " "%g" "\n", now);
108     DEBUG0("\t CPU actions\n");
109     while ((action =
110             xbt_swag_extract(surf_cpu_resource->common_public->states.
111                              failed_action_set))) {
112       DEBUG1("\t * Failed : %p\n", action);
113       action->resource_type->common_public->action_free(action);
114     }
115     while ((action =
116             xbt_swag_extract(surf_cpu_resource->common_public->states.
117                              done_action_set))) {
118       DEBUG1("\t * Done : %p\n", action);
119       action->resource_type->common_public->action_free(action);
120     }
121     DEBUG0("\t Network actions\n");
122     while ((action =
123             xbt_swag_extract(surf_network_resource->common_public->states.
124                              failed_action_set))) {
125       DEBUG1("\t * Failed : %p\n", action);
126       action->resource_type->common_public->action_free(action);
127     }
128     while ((action =
129             xbt_swag_extract(surf_network_resource->common_public->states.
130                              done_action_set))) {
131       DEBUG1("\t * Done : %p\n", action);
132       action->resource_type->common_public->action_free(action);
133     }
134
135   } while (surf_solve()>=0.0);
136
137   DEBUG0("Simulation Terminated\n");
138 }
139
140 #ifdef __BORLANDC__
141 #pragma argsused
142 #endif
143
144 int main(int argc, char **argv)
145 {
146   surf_init(&argc, argv);       /* Initialize some common structures */
147   if(argc==1) {
148      fprintf(stderr,"Usage : %s platform.txt\n",argv[0]);
149      return 1;
150   }
151   test(argv[1]);
152
153   surf_exit();
154   return 0;
155 }