Logo AND Algorithmique Numérique Distribuée

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