Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename directory.
[simgrid.git] / examples / msg / parallel_task / test_ptask.c
1 /* Copyright (c) 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include "msg/msg.h"            /* Yeah! If you want to use msg, you
9                                    need to include msg/msg.h */
10 #include "xbt/sysdep.h"         /* calloc, printf */
11
12 /* Create a log channel to have nice outputs. */
13 #include "xbt/log.h"
14 #include "xbt/asserts.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
16                              "Messages specific for this msg example");
17
18 int execute(int argc, char *argv[]);
19 int redistribute(int argc, char *argv[]);
20 MSG_error_t test_all(const char *platform_file, const char *application_file);
21
22 typedef enum {
23   PORT_22 = 0,
24   MAX_CHANNEL
25 } channel_t;
26
27
28 int execute(int argc, char *argv[])
29 {
30   char buffer[32];
31   int i, j;
32   m_host_t *m_host_list = NULL;
33   m_task_t task = NULL;
34   int host_list_size;
35   double *computation_duration = NULL;
36   double *communication_table = NULL;
37   double communication_amount;
38   double computation_amount;
39   double execution_time;
40
41
42   host_list_size = argc - 3;
43   DEBUG1("host_list_size=%d", host_list_size);
44   m_host_list = calloc(host_list_size, sizeof(m_host_t));
45   for (i = 1; i <= host_list_size; i++) {
46     m_host_list[i - 1] = MSG_get_host_by_name(argv[i]);
47     xbt_assert1(m_host_list[i - 1] != NULL,
48                 "Unknown host %s. Stopping Now! ", argv[i]);
49   }
50
51   xbt_assert1(sscanf(argv[argc - 2], "%lg", &computation_amount),
52               "Invalid argument %s\n", argv[argc - 2]);
53   xbt_assert1(sscanf(argv[argc - 1], "%lg", &communication_amount),
54               "Invalid argument %s\n", argv[argc - 1]);
55   computation_duration = (double *) calloc(host_list_size, sizeof(double));
56   communication_table =
57     (double *) calloc(host_list_size * host_list_size, sizeof(double));
58   for (i = 0; i < host_list_size; i++) {
59     computation_duration[i] = computation_amount / host_list_size;
60     for (j = 0; j < host_list_size; j++)
61       communication_table[i * host_list_size + j] =
62         communication_amount / (host_list_size * host_list_size);
63   }
64
65   sprintf(buffer, "redist#0\n");
66   task = MSG_parallel_task_create(buffer,
67                                   host_list_size,
68                                   m_host_list,
69                                   computation_duration,
70                                   communication_table, NULL);
71
72   execution_time = MSG_get_clock();
73   MSG_parallel_task_execute(task);
74   execution_time = MSG_get_clock() - execution_time;
75
76   INFO1("execution_time=%g ", execution_time);
77
78   return 0;
79 }
80
81
82 int redistribute(int argc, char *argv[])
83 {
84   char buffer[32];
85   int i, j;
86   m_host_t *m_host_list = NULL;
87   m_task_t task = NULL;
88   int host_list_size;
89   double *computation_duration = NULL;
90   double *communication_table = NULL;
91   double communication_amount;
92   double redistribution_time;
93
94
95   host_list_size = argc - 2;
96   DEBUG1("host_list_size=%d", host_list_size);
97   m_host_list = calloc(host_list_size, sizeof(m_host_t));
98   for (i = 1; i <= host_list_size; i++) {
99     m_host_list[i - 1] = MSG_get_host_by_name(argv[i]);
100     xbt_assert1(m_host_list[i - 1] != NULL,
101                 "Unknown host %s. Stopping Now! ", argv[i]);
102   }
103
104   xbt_assert1(sscanf(argv[argc - 1], "%lg", &communication_amount),
105               "Invalid argument %s\n", argv[argc - 1]);
106   computation_duration = (double *) calloc(host_list_size, sizeof(double));
107   communication_table =
108     (double *) calloc(host_list_size * host_list_size, sizeof(double));
109   for (i = 0; i < host_list_size; i++) {
110     for (j = 0; j < host_list_size; j++)
111       communication_table[i * host_list_size + j] =
112         communication_amount / (host_list_size * host_list_size);
113   }
114
115   sprintf(buffer, "redist#0\n");
116   task = MSG_parallel_task_create(buffer,
117                                   host_list_size,
118                                   m_host_list,
119                                   computation_duration,
120                                   communication_table, NULL);
121
122   redistribution_time = MSG_get_clock();
123   MSG_parallel_task_execute(task);
124   redistribution_time = MSG_get_clock() - redistribution_time;
125
126   INFO1("redistribution_time=%g ", redistribution_time);
127
128   return 0;
129 }
130
131
132 MSG_error_t test_all(const char *platform_file, const char *application_file)
133 {
134   MSG_error_t res = MSG_OK;
135
136
137   MSG_config("workstation/model", "ptask_L07");
138
139   /*  Simulation setting */
140   MSG_set_channel_number(MAX_CHANNEL);
141   MSG_create_environment(platform_file);
142
143   /*   Application deployment */
144   MSG_function_register("execute", execute);
145   MSG_function_register("redistribute", redistribute);
146   MSG_launch_application(application_file);
147
148   res = MSG_main();
149
150   INFO1("Simulation time %g", MSG_get_clock());
151   return res;
152 }
153
154
155 int main(int argc, char *argv[])
156 {
157   MSG_error_t res = MSG_OK;
158
159   MSG_global_init(&argc, argv);
160   if (argc < 3) {
161     printf("Usage: %s platform_file deployment_file\n", argv[0]);
162     exit(1);
163   }
164   res = test_all(argv[1], argv[2]);
165   MSG_clean();
166
167   if (res == MSG_OK)
168     return 0;
169   else
170     return 1;
171 }