Logo AND Algorithmique Numérique Distribuée

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