Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sed -i -e 's/\t/ /g' [sources] Please people, stop using tabs
[simgrid.git] / examples / simdag / dax / dax_test.c
1 /* simple test trying to load a DAX file.                                   */
2
3 /* Copyright (c) 2009-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include "simgrid/simdag.h"
12 #include "xbt/log.h"
13 #include "xbt/ex.h"
14 #include <string.h>
15
16 XBT_LOG_NEW_DEFAULT_CATEGORY(test,
17                              "Logging specific to this SimDag example");
18
19 static int name_compare_hosts(const void *n1, const void *n2)
20 {
21   return strcmp(
22       sg_host_get_name(*(sg_host_t *) n1),
23       sg_host_get_name(*(sg_host_t *) n2)
24   );
25 }
26
27 int main(int argc, char **argv)
28 {
29   xbt_dynar_t dax;
30   unsigned int cursor;
31   SD_task_t task;
32
33   /* SD initialization */
34   SD_init(&argc, argv);
35
36   /* Check our arguments */
37   xbt_assert(argc > 2, "Usage: %s platform_file dax_file [jedule_file]\n"
38        "\tExample: %s simulacrum_7_hosts.xml Montage_25.xml Montage_25.jed", 
39        argv[0], argv[0]);
40
41   char *last = strrchr(argv[2], '.');
42   char * tracefilename = bprintf("%.*s.trace",(int) (last == NULL ? 
43                strlen(argv[2]) : 
44                last - argv[2]), argv[2]);  
45   if (argc == 4)
46     tracefilename = xbt_strdup(argv[3]);
47  
48   /* creation of the environment */
49   SD_create_environment(argv[1]);
50
51   /* load the DAX file */
52   dax = SD_daxload(argv[2]);
53   if (!dax){
54     XBT_ERROR("A problem occurred during DAX parsing (cycle or syntax). Do not continue this test");
55     free(tracefilename);
56     SD_exit();
57     exit(255);
58   }
59
60   /* Display all the tasks */
61   XBT_INFO
62       ("------------------- Display all tasks of the loaded DAG ---------------------------");
63   xbt_dynar_foreach(dax, cursor, task) {
64     SD_task_dump(task);
65   }
66
67   FILE *dotout = fopen("dax.dot", "w");
68   fprintf(dotout, "digraph A {\n");
69   xbt_dynar_foreach(dax, cursor, task) {
70     SD_task_dotty(task, dotout);
71   }
72   fprintf(dotout, "}\n");
73   fclose(dotout);
74
75   /* Schedule them all on the first workstation */
76   XBT_INFO("------------------- Schedule tasks ---------------------------");
77   const sg_host_t *ws_list = sg_host_list();
78   int hosts_count = sg_host_count();
79   qsort((void *) ws_list, hosts_count, sizeof(sg_host_t),
80         name_compare_hosts);
81
82   xbt_dynar_foreach(dax, cursor, task) {
83     if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ) {
84       if (!strcmp(SD_task_get_name(task), "end"))
85         SD_task_schedulel(task, 1, ws_list[0]);
86       else
87         SD_task_schedulel(task, 1, ws_list[cursor % hosts_count]);
88     }
89   }
90
91   XBT_INFO
92       ("------------------- Run the schedule ---------------------------");
93   SD_simulate(-1);
94   XBT_INFO
95       ("------------------- Produce the trace file---------------------------");
96   XBT_INFO("Producing the trace of the run into %s", tracefilename);
97   FILE *out = fopen(tracefilename, "w");
98   xbt_assert(out, "Cannot write to %s", tracefilename);
99   free(tracefilename);
100
101   xbt_dynar_foreach(dax, cursor, task) {
102     int kind = SD_task_get_kind(task);
103     sg_host_t *wsl = SD_task_get_workstation_list(task);
104     switch (kind) {
105     case SD_TASK_COMP_SEQ:
106       fprintf(out, "[%f] %s compute %f # %s\n",
107               SD_task_get_start_time(task),
108               sg_host_get_name(wsl[0]), SD_task_get_amount(task),
109               SD_task_get_name(task));
110       break;
111     case SD_TASK_COMM_E2E:
112       fprintf(out, "[%f] %s send %s %f # %s\n",
113               SD_task_get_start_time(task),
114               sg_host_get_name(wsl[0]),
115               sg_host_get_name(wsl[1]), SD_task_get_amount(task),
116               SD_task_get_name(task));
117       fprintf(out, "[%f] %s recv %s %f # %s\n",
118               SD_task_get_finish_time(task),
119               sg_host_get_name(wsl[1]),
120               sg_host_get_name(wsl[0]), SD_task_get_amount(task),
121               SD_task_get_name(task));
122       break;
123     default:
124       xbt_die("Task %s is of unknown kind %d", SD_task_get_name(task),
125               SD_task_get_kind(task));
126     }
127     SD_task_destroy(task);
128   }
129   fclose(out);
130   xbt_dynar_free_container(&dax);
131   /* exit */
132   SD_exit();
133   return 0;
134 }