Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add new example to simdag using lua script(...not yet improved)
[simgrid.git] / examples / simdag / dot / dot_test.c
1 /* simple test trying to load a DOT file.                                   */
2
3 /* Copyright (c) 2010. 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 "simdag/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 int main(int argc, char **argv) {
20     xbt_dynar_t dot, changed;
21     unsigned int cursor;
22     SD_task_t task;
23
24     /* initialisation of SD */
25     SD_init(&argc, argv);
26
27     /* Check our arguments */
28     if (argc < 3) {
29         INFO1("Usage: %s platform_file dot_file [trace_file]", argv[0]);
30         INFO1("example: %s ../2clusters.xml dag.dot dag.mytrace", argv[0]);
31         exit(1);
32     }
33     char *tracefilename;
34     if (argc == 3) {
35         char *last=strrchr(argv[2],'.');
36
37         tracefilename=bprintf("%.*s.trace",(int)(last==NULL?strlen(argv[2]):last-argv[2]),argv[2]);
38     } else {
39         tracefilename = xbt_strdup(argv[3]);
40     }
41
42     /* creation of the environment */
43     SD_create_environment(argv[1]);
44
45     /* load the DOT file */
46     dot=SD_dotload(argv[2]);
47
48     /* Display all the tasks */
49     INFO0("------------------- Display all tasks of the loaded DAG ---------------------------");
50     xbt_dynar_foreach(dot,cursor,task) {
51         SD_task_dump(task);
52     }
53
54     FILE *dotout = fopen("dot.dot","w");
55     fprintf(dotout,"digraph A {\n");
56     xbt_dynar_foreach(dot,cursor,task) {
57         SD_task_dotty(task,dotout);
58     }
59     fprintf(dotout,"}\n");
60     fclose(dotout);
61
62     /* Schedule them all on the first workstation */
63     INFO0("------------------- Schedule tasks ---------------------------");
64     const SD_workstation_t *ws_list =  SD_workstation_get_list();
65
66     int count = SD_workstation_get_number();
67     xbt_dynar_foreach(dot,cursor,task) {
68         if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ) {
69             if (!strcmp(SD_task_get_name(task),"end"))
70                 SD_task_schedulel(task,1,ws_list[0]);
71             else
72                 SD_task_schedulel(task,1,ws_list[cursor%count]);
73         }
74     }
75
76     INFO0("------------------- Run the schedule ---------------------------");
77     changed = SD_simulate(-1);
78     xbt_dynar_free_container(&changed);
79     INFO0("------------------- Produce the trace file---------------------------");
80     INFO1("Producing the trace of the run into %s",tracefilename);
81     FILE*out = fopen(tracefilename,"w");
82     xbt_assert1(out,"Cannot write to %s",tracefilename);
83     free(tracefilename);
84
85     xbt_dynar_foreach(dot,cursor,task) {
86         int kind = SD_task_get_kind(task);
87         SD_workstation_t *wsl = SD_task_get_workstation_list(task);
88         switch (kind) {
89             case SD_TASK_COMP_SEQ:
90                 fprintf(out,"[%f] %s compute %f # %s\n",SD_task_get_start_time(task),
91                         SD_workstation_get_name(wsl[0]),SD_task_get_amount(task),
92                         SD_task_get_name(task));
93                 break;
94             case SD_TASK_COMM_E2E:
95                 fprintf(out,"[%f] %s send %s %f # %s\n",SD_task_get_start_time(task),
96                         SD_workstation_get_name(wsl[0]),SD_workstation_get_name(wsl[1]),
97                         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),
99                         SD_workstation_get_name(wsl[1]),SD_workstation_get_name(wsl[0]),
100                         SD_task_get_amount(task), SD_task_get_name(task));
101                 break;
102             default:
103                 xbt_die(bprintf("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
109     /* exit */
110     SD_exit();
111     return 0;
112 }