Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d4093d548c1d670803541699e1838fc5319878a8
[simgrid.git] / examples / simdag / dax / dax_test.c
1 /* simple test trying to load a DAX file.                                   */
2
3 /* Copyright (c) 2009 Da SimGrid Team. All rights reserved.                 */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include "simdag/simdag.h"
11 #include "xbt/log.h"
12 #include "xbt/ex.h"
13 #include <string.h>
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(test,
16                              "Logging specific to this SimDag example");
17
18 static int name_compare_hosts(const void *n1, const void *n2)
19 {
20   char name1[80], name2[80];
21   strcpy(name1, SD_workstation_get_name(*((SD_workstation_t *) n1)));
22   strcpy(name2, SD_workstation_get_name(*((SD_workstation_t *) n2)));
23
24   return strcmp(name1, name2);
25 }
26
27 int main(int argc, char **argv) {
28   xbt_dynar_t dax;
29   unsigned int cursor;
30   SD_task_t task;
31
32   /* initialisation of SD */
33   SD_init(&argc, argv);
34
35   /* Check our arguments */
36   if (argc < 3) {
37     INFO1("Usage: %s platform_file dax_file [trace_file]", argv[0]);
38     INFO1("example: %s ../sd_platform.xml Montage_50.xml Montage_50.mytrace", argv[0]);
39     exit(1);
40   }
41   char *tracefilename;
42   if (argc == 3) {
43     char *last=strrchr(argv[2],'.');
44
45     tracefilename=bprintf("%.*s.trace",last==NULL?strlen(argv[2]):last-argv[2],argv[2]);
46   } else {
47     tracefilename = xbt_strdup(argv[3]);
48   }
49
50   /* creation of the environment */
51   SD_create_environment(argv[1]);
52
53   /* load the DAX file */
54   dax=SD_daxload(argv[2]);
55
56   /* Display all the tasks */
57   INFO0("------------------- Display all tasks of the loaded DAG ---------------------------");
58   xbt_dynar_foreach(dax,cursor,task) {
59     SD_task_dump(task);
60   }
61
62   FILE *dotout = fopen("dax.dot","w");
63   fprintf(dotout,"digraph A {\n");
64   xbt_dynar_foreach(dax,cursor,task) {
65     SD_task_dotty(task,dotout);
66   }
67   fprintf(dotout,"}\n");
68   fclose(dotout);
69
70   /* Schedule them all on the first workstation */
71   INFO0("------------------- Schedule tasks ---------------------------");
72   const SD_workstation_t *ws_list =  SD_workstation_get_list();
73   int totalHosts = SD_workstation_get_number();
74   qsort((void *) ws_list, totalHosts, sizeof(SD_workstation_t),
75         name_compare_hosts);
76
77   int count = SD_workstation_get_number();
78   xbt_dynar_foreach(dax,cursor,task) {
79     if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ) {
80       if (!strcmp(SD_task_get_name(task),"end"))
81         SD_task_schedulel(task,1,ws_list[0]);
82       else
83         SD_task_schedulel(task,1,ws_list[cursor%count]);
84     }
85   }
86
87   INFO0("------------------- Run the schedule ---------------------------");
88   SD_simulate(-1);
89   INFO0("------------------- Produce the trace file---------------------------");
90   INFO1("Producing the trace of the run into %s",tracefilename);
91   FILE*out = fopen(tracefilename,"w");
92   xbt_assert1(out,"Cannot write to %s",tracefilename);
93   free(tracefilename);
94
95   xbt_dynar_foreach(dax,cursor,task) {
96     int kind = SD_task_get_kind(task);
97     SD_workstation_t *wsl = SD_task_get_workstation_list(task);
98     switch (kind) {
99     case SD_TASK_COMP_SEQ:
100       fprintf(out,"[%f] %s compute %f # %s\n",SD_task_get_start_time(task),
101           SD_workstation_get_name(wsl[0]),SD_task_get_amount(task),
102           SD_task_get_name(task));
103       break;
104     case SD_TASK_COMM_E2E:
105       fprintf(out,"[%f] %s send %s %f # %s\n",SD_task_get_start_time(task),
106           SD_workstation_get_name(wsl[0]),SD_workstation_get_name(wsl[1]),
107           SD_task_get_amount(task), SD_task_get_name(task));
108       fprintf(out,"[%f] %s recv %s %f # %s\n",SD_task_get_start_time(task),
109           SD_workstation_get_name(wsl[1]),SD_workstation_get_name(wsl[0]),
110           SD_task_get_amount(task), SD_task_get_name(task));
111       break;
112     default:
113       xbt_die(bprintf("Task %s is of unknown kind %d",SD_task_get_name(task),SD_task_get_kind(task)));
114     }
115   }
116   fclose(out);
117
118   /* exit */
119   SD_exit();
120   return 0;
121 }