Logo AND Algorithmique Numérique Distribuée

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