Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Each action can now have a maximum duration. That is a convenient way to
[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(void);
34 void test(void)
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   xbt_maxmin_float_t now = -1.0;
48
49   surf_cpu_resource_init("platform.txt"); /* Now it is possible to use CPUs */
50   surf_network_resource_init("platform.txt"); /* 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", surf_cpu_resource->common_public->get_resource_name(cpuA), cpuA);
59   printf("%s : %p\n", surf_cpu_resource->common_public->get_resource_name(cpuB), cpuB);
60
61   /* Let's do something on it */
62   actionA = surf_cpu_resource->extension_public->execute(cpuA, 1000.0);
63   actionB = surf_cpu_resource->extension_public->execute(cpuB, 1000.0);
64   actionC = surf_cpu_resource->extension_public->sleep(cpuB, 7.32);
65
66   /* Use whatever calling style you want... */
67   stateActionA = surf_cpu_resource->common_public->action_get_state(actionA);   /* When you know actionA resource type */
68   stateActionB = actionB->resource_type->common_public->action_get_state(actionB);      /* If you're unsure about it's resource type */
69   stateActionC = surf_cpu_resource->common_public->action_get_state(actionC);   /* When you know actionA resource type */
70
71   /* And just look at the state of these tasks */
72   printf("actionA : %p (%s)\n", actionA, string_action(stateActionA));
73   printf("actionB : %p (%s)\n", actionB, string_action(stateActionB));
74   printf("actionC : %p (%s)\n", actionB, string_action(stateActionC));
75
76   /*********************** Network *******************************/
77   printf("%p \n", surf_network_resource);
78   cardA = surf_network_resource->common_public->name_service("Cpu A");
79   cardB = surf_network_resource->common_public->name_service("Cpu B");
80
81   /* Let's check that those two processors exist */
82   printf("%s : %p\n", surf_network_resource->common_public->get_resource_name(cardA), cardA);
83   printf("%s : %p\n", surf_network_resource->common_public->get_resource_name(cardB), cardB);
84
85   /* Let's do something on it */
86   commAB = surf_network_resource->extension_public->communicate(cardA, cardB, 150.0);
87
88   surf_solve(); /* Takes traces into account. Returns 0.0 */
89   do {
90     surf_action_t action = NULL;    
91     now = surf_get_clock();
92     printf("Next Event : " XBT_HEAP_FLOAT_T "\n", now);
93     printf("\t CPU actions\n");
94     while(action=xbt_swag_extract(surf_cpu_resource->common_public->states.failed_action_set)) {
95       printf("\t * Failed : %p\n", action);
96       action->resource_type->common_public->action_free(action);
97     }
98     while(action=xbt_swag_extract(surf_cpu_resource->common_public->states.done_action_set)) {
99       printf("\t * Done : %p\n", action);
100       action->resource_type->common_public->action_free(action);
101     }
102     printf("\t Network actions\n");
103     while(action=xbt_swag_extract(surf_network_resource->common_public->states.failed_action_set)) {
104       printf("\t * Failed : %p\n", action);
105       action->resource_type->common_public->action_free(action);
106     }
107     while(action=xbt_swag_extract(surf_network_resource->common_public->states.done_action_set)) {
108       printf("\t * Done : %p\n", action);
109       action->resource_type->common_public->action_free(action);
110     }
111
112   } while(surf_solve());
113
114   printf("Simulation Terminated\n");
115
116   surf_finalize();
117 }
118
119
120 int main(int argc, char **argv)
121 {
122   surf_init(&argc, argv); /* Initialize some common structures */
123   test();
124   return 0;
125 }