Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / examples / deprecated / simdag / daxload / sd_daxload.c
1 /* simple test trying to load a DAX file.                                   */
2
3 /* Copyright (c) 2009-2021. 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
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(*(const sg_host_t*)n1), sg_host_get_name(*(const 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   const 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
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   sg_host_t *host_list = sg_host_list();
68   int hosts_count = sg_host_count();
69   qsort(host_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, host_list[0]);
75       else
76         SD_task_schedulel(task, 1, host_list[cursor % hosts_count]);
77     }
78   }
79   xbt_free(host_list);
80
81   XBT_INFO("------------------- Run the schedule ---------------------------");
82   SD_simulate(-1);
83   XBT_INFO("------------------- Produce the trace file---------------------------");
84   const char* basename = strrchr(tracefilename, '/');
85   XBT_INFO("Producing the trace of the run into %s", basename ? basename + 1 : tracefilename);
86   FILE *out = fopen(tracefilename, "w");
87   xbt_assert(out, "Cannot write to %s", tracefilename);
88   free(tracefilename);
89
90   xbt_dynar_foreach(dax, cursor, task) {
91     int kind = SD_task_get_kind(task);
92     sg_host_t *wsl = SD_task_get_workstation_list(task);
93     switch (kind) {
94     case SD_TASK_COMP_SEQ:
95       fprintf(out, "[%f] %s compute %f # %s\n", SD_task_get_start_time(task), sg_host_get_name(wsl[0]),
96               SD_task_get_amount(task), SD_task_get_name(task));
97       break;
98     case SD_TASK_COMM_E2E:
99       fprintf(out, "[%f] %s send %s %f # %s\n", SD_task_get_start_time(task), sg_host_get_name(wsl[0]),
100               sg_host_get_name(wsl[1]), SD_task_get_amount(task), SD_task_get_name(task));
101       fprintf(out, "[%f] %s recv %s %f # %s\n", SD_task_get_finish_time(task), sg_host_get_name(wsl[1]),
102               sg_host_get_name(wsl[0]), SD_task_get_amount(task), SD_task_get_name(task));
103       break;
104     default:
105       xbt_die("Task %s is of unknown kind %u", SD_task_get_name(task), SD_task_get_kind(task));
106     }
107     SD_task_destroy(task);
108   }
109   fclose(out);
110   xbt_dynar_free_container(&dax);
111
112   return 0;
113 }