Logo AND Algorithmique Numérique Distribuée

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