Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6688dd5bdfa1cd36153d8e2f5075abc253175119
[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",(int)(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_task_t *changed = SD_simulate(-1);
89   free(changed);
90   INFO0("------------------- Produce the trace file---------------------------");
91   INFO1("Producing the trace of the run into %s",tracefilename);
92   FILE*out = fopen(tracefilename,"w");
93   xbt_assert1(out,"Cannot write to %s",tracefilename);
94   free(tracefilename);
95
96   xbt_dynar_foreach(dax,cursor,task) {
97     int kind = SD_task_get_kind(task);
98     SD_workstation_t *wsl = SD_task_get_workstation_list(task);
99     switch (kind) {
100     case SD_TASK_COMP_SEQ:
101       fprintf(out,"[%f] %s compute %f # %s\n",SD_task_get_start_time(task),
102           SD_workstation_get_name(wsl[0]),SD_task_get_amount(task),
103           SD_task_get_name(task));
104       break;
105     case SD_TASK_COMM_E2E:
106       fprintf(out,"[%f] %s send %s %f # %s\n",SD_task_get_start_time(task),
107           SD_workstation_get_name(wsl[0]),SD_workstation_get_name(wsl[1]),
108           SD_task_get_amount(task), SD_task_get_name(task));
109       fprintf(out,"[%f] %s recv %s %f # %s\n",SD_task_get_start_time(task),
110           SD_workstation_get_name(wsl[1]),SD_workstation_get_name(wsl[0]),
111           SD_task_get_amount(task), SD_task_get_name(task));
112       break;
113     default:
114       xbt_die(bprintf("Task %s is of unknown kind %d",SD_task_get_name(task),SD_task_get_kind(task)));
115     }
116     SD_task_destroy(task);
117   }
118   fclose(out);
119
120   /* exit */
121   SD_exit();
122   return 0;
123 }