Logo AND Algorithmique Numérique Distribuée

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