Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill that example
[simgrid.git] / examples / simdag / dax / sd_dax.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 "simgrid/simdag.h"
10 #include "xbt/file.h"
11 #include <stdio.h>
12 #include <string.h>
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Logging specific to this SimDag example");
15
16 static int name_compare_hosts(const void *n1, const void *n2)
17 {
18   return strcmp(sg_host_get_name(*(sg_host_t *) n1), sg_host_get_name(*(sg_host_t *) n2));
19 }
20
21 int main(int argc, char **argv)
22 {
23   xbt_dynar_t dax;
24   unsigned int cursor;
25   SD_task_t task;
26
27   /* SD initialization */
28   SD_init(&argc, argv);
29
30   /* Check our arguments */
31   xbt_assert(argc > 2, "Usage: %s platform_file dax_file [jedule_file]\n"
32        "\tExample: %s simulacrum_7_hosts.xml Montage_25.xml Montage_25.jed", argv[0], argv[0]);
33
34   char *last = strrchr(argv[2], '.');
35   char * tracefilename = bprintf("%.*s.trace",(int) (last == NULL ? strlen(argv[2]):last - argv[2]), argv[2]);
36   if (argc == 4)
37     tracefilename = xbt_strdup(argv[3]);
38
39   /* creation of the environment */
40   SD_create_environment(argv[1]);
41
42   /* load the DAX file */
43   dax = SD_daxload(argv[2]);
44   if (!dax){
45     XBT_ERROR("A problem occurred during DAX parsing (cycle or syntax). Do not continue this test");
46     free(tracefilename);
47     SD_exit();
48     exit(255);
49   }
50
51   /* Display all the tasks */
52   XBT_INFO("------------------- Display all tasks of the loaded DAG ---------------------------");
53   xbt_dynar_foreach(dax, cursor, task) {
54     SD_task_dump(task);
55   }
56
57   FILE *dotout = fopen("dax.dot", "w");
58   fprintf(dotout, "digraph A {\n");
59   xbt_dynar_foreach(dax, cursor, task) {
60     SD_task_dotty(task, dotout);
61   }
62   fprintf(dotout, "}\n");
63   fclose(dotout);
64
65   /* Schedule them all on the first host */
66   XBT_INFO("------------------- Schedule tasks ---------------------------");
67   const sg_host_t *ws_list = sg_host_list();
68   int hosts_count = sg_host_count();
69   qsort((void *) ws_list, hosts_count, sizeof(sg_host_t), name_compare_hosts);
70
71   xbt_dynar_foreach(dax, cursor, task) {
72     if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ) {
73       if (!strcmp(SD_task_get_name(task), "end"))
74         SD_task_schedulel(task, 1, ws_list[0]);
75       else
76         SD_task_schedulel(task, 1, ws_list[cursor % hosts_count]);
77     }
78   }
79
80   XBT_INFO("------------------- Run the schedule ---------------------------");
81   SD_simulate(-1);
82   XBT_INFO("------------------- Produce the trace file---------------------------");
83   XBT_INFO("Producing the trace of the run into %s", xbt_basename(tracefilename));
84   FILE *out = fopen(tracefilename, "w");
85   xbt_assert(out, "Cannot write to %s", tracefilename);
86   free(tracefilename);
87
88   xbt_dynar_foreach(dax, cursor, task) {
89     int kind = SD_task_get_kind(task);
90     sg_host_t *wsl = SD_task_get_workstation_list(task);
91     switch (kind) {
92     case SD_TASK_COMP_SEQ:
93       fprintf(out, "[%f] %s compute %f # %s\n", SD_task_get_start_time(task), sg_host_get_name(wsl[0]),
94               SD_task_get_amount(task), SD_task_get_name(task));
95       break;
96     case SD_TASK_COMM_E2E:
97       fprintf(out, "[%f] %s send %s %f # %s\n", SD_task_get_start_time(task), sg_host_get_name(wsl[0]),
98               sg_host_get_name(wsl[1]), SD_task_get_amount(task), SD_task_get_name(task));
99       fprintf(out, "[%f] %s recv %s %f # %s\n", SD_task_get_finish_time(task), sg_host_get_name(wsl[1]),
100               sg_host_get_name(wsl[0]), SD_task_get_amount(task), SD_task_get_name(task));
101       break;
102     default:
103       xbt_die("Task %s is of unknown kind %d", SD_task_get_name(task), SD_task_get_kind(task));
104     }
105     SD_task_destroy(task);
106   }
107   fclose(out);
108   xbt_dynar_free_container(&dax);
109
110   SD_exit();
111   return 0;
112 }