Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8626beb145ae779621009191fda5b1ebfab87125
[simgrid.git] / testsuite / surf / surf_usage.c
1 /* A few basic tests for the surf library                                   */
2
3 /* Authors: Arnaud Legrand                                                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6    under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include <stdio.h>
9 #include "surf/surf.h"
10
11 const char *string_action(e_surf_action_state_t state);
12 const char *string_action(e_surf_action_state_t state)
13 {
14   switch (state) {
15   case (SURF_ACTION_READY):
16     return "SURF_ACTION_READY";
17   case (SURF_ACTION_RUNNING):
18     return "SURF_ACTION_RUNNING";
19   case (SURF_ACTION_FAILED):
20     return "SURF_ACTION_FAILED";
21   case (SURF_ACTION_DONE):
22     return "SURF_ACTION_DONE";
23   case (SURF_ACTION_NOT_IN_THE_SYSTEM):
24     return "SURF_ACTION_NOT_IN_THE_SYSTEM";
25   default:
26     return "INVALID STATE";
27   }
28 }
29
30
31 void test(void);
32 void test(void)
33 {
34   void *cpuA = NULL;
35   void *cpuB = NULL;
36   surf_action_t actionA = NULL;
37   surf_action_t actionB = NULL;
38   e_surf_action_state_t stateActionA;
39   e_surf_action_state_t stateActionB;
40   xbt_maxmin_float_t now = -1.0;
41
42   surf_init(); /* Initialize some common structures */
43   surf_cpu_resource_init("platform.txt"); /* Now it is possible to use CPUs */
44   surf_network_resource_init("platform.txt"); /* Now it is possible to use eth0 */
45
46   printf("%p \n", surf_cpu_resource);
47   cpuA = surf_cpu_resource->common_public->name_service("Cpu A");
48   cpuB = surf_cpu_resource->common_public->name_service("Cpu B");
49
50   /* Let's check that those two processors exist */
51   printf("%s : %p\n", surf_cpu_resource->common_public->get_resource_name(cpuA), cpuA);
52   printf("%s : %p\n", surf_cpu_resource->common_public->get_resource_name(cpuB), cpuB);
53
54   /* Let's do something on it */
55   actionA = surf_cpu_resource->extension_public->execute(cpuA, 1000.0);
56   actionB = surf_cpu_resource->extension_public->execute(cpuB, 1000.0);
57
58   /* Use whatever calling style you want... */
59   stateActionA = surf_cpu_resource->common_public->action_get_state(actionA);   /* When you know actionA resource type */
60   stateActionB = actionB->resource_type->common_public->action_get_state(actionB);      /* If you're unsure about it's resource type */
61
62   /* And just look at the stat of these tasks */
63   printf("actionA : %p (%s)\n", actionA, string_action(stateActionA));
64   printf("actionB : %p (%s)\n", actionB, string_action(stateActionB));
65
66   surf_solve(); /* Takes traces into account. Returns 0.0 */
67   do {
68     surf_action_t action = NULL;    
69     now = surf_get_clock();
70     printf("Next Event : " XBT_HEAP_FLOAT_T "\n", now);
71     while(action=xbt_swag_extract(surf_cpu_resource->common_public->states.failed_action_set)) {
72       printf("\tFailed : %p\n", action);
73       action->resource_type->common_public->action_free(action);
74     }
75     while(action=xbt_swag_extract(surf_cpu_resource->common_public->states.done_action_set)) {
76       printf("\tDone : %p\n", action);
77       action->resource_type->common_public->action_free(action);
78     }
79   } while(surf_solve());
80
81   printf("Simulation Terminated\n");
82
83   surf_finalize();
84 }
85
86
87 int main(int argc, char **argv)
88 {
89   test();
90   return 0;
91 }