Logo AND Algorithmique Numérique Distribuée

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