Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8626a98026d1449dfaca6ea73c4a8bd7cea89630
[simgrid.git] / examples / msg / parallel_task / test_ptask.c
1 /* Copyright (c) 2008-2014. 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,
21                      const char *application_file);
22
23
24 int execute(int argc, char *argv[])
25 {
26   char buffer[32];
27   int i, j;
28   msg_host_t *m_host_list = NULL;
29   msg_task_t task = NULL;
30   int host_list_size;
31   double *computation_duration = NULL;
32   double *communication_table = NULL;
33   double communication_amount = 0;
34   double computation_amount = 0;
35   double execution_time;
36
37
38   host_list_size = argc - 3;
39   XBT_DEBUG("host_list_size=%d", host_list_size);
40   m_host_list = calloc(host_list_size, sizeof(msg_host_t));
41   for (i = 1; i <= host_list_size; i++) {
42     m_host_list[i - 1] = MSG_get_host_by_name(argv[i]);
43     xbt_assert(m_host_list[i - 1] != NULL,
44                 "Unknown host %s. Stopping Now! ", argv[i]);
45   }
46
47   _XBT_GNUC_UNUSED int read;
48   read = sscanf(argv[argc - 2], "%lg", &computation_amount);
49   xbt_assert(read, "Invalid argument %s\n", argv[argc - 2]);
50   read = sscanf(argv[argc - 1], "%lg", &communication_amount);
51   xbt_assert(read, "Invalid argument %s\n", argv[argc - 1]);
52   computation_duration = (double *) calloc(host_list_size, sizeof(double));
53   communication_table =
54       (double *) calloc(host_list_size * host_list_size, sizeof(double));
55   for (i = 0; i < host_list_size; i++) {
56     computation_duration[i] = computation_amount / host_list_size;
57     for (j = 0; j < host_list_size; j++)
58       communication_table[i * host_list_size + j] =
59           communication_amount / (host_list_size * host_list_size);
60   }
61
62   sprintf(buffer, "redist#0\n");
63   task = MSG_parallel_task_create(buffer,
64                                   host_list_size,
65                                   m_host_list,
66                                   computation_duration,
67                                   communication_table, NULL);
68
69   execution_time = MSG_get_clock();
70   MSG_parallel_task_execute(task);
71   MSG_task_destroy(task);
72   xbt_free(m_host_list);
73   execution_time = MSG_get_clock() - execution_time;
74
75   XBT_INFO("execution_time=%g ", execution_time);
76
77   return 0;
78 }
79
80
81 int redistribute(int argc, char *argv[])
82 {
83   char buffer[32];
84   int i, j;
85   msg_host_t *m_host_list = NULL;
86   msg_task_t task = NULL;
87   int host_list_size;
88   double *computation_duration = NULL;
89   double *communication_table = NULL;
90   double communication_amount = 0;
91   double redistribution_time;
92
93
94   host_list_size = argc - 2;
95   XBT_DEBUG("host_list_size=%d", host_list_size);
96   m_host_list = calloc(host_list_size, sizeof(msg_host_t));
97   for (i = 1; i <= host_list_size; i++) {
98     m_host_list[i - 1] = MSG_get_host_by_name(argv[i]);
99     xbt_assert(m_host_list[i - 1] != NULL,
100                 "Unknown host %s. Stopping Now! ", argv[i]);
101   }
102
103   _XBT_GNUC_UNUSED int read;
104   read = sscanf(argv[argc - 1], "%lg", &communication_amount);
105   xbt_assert(read, "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   MSG_task_destroy(task);
125   xbt_free(m_host_list);
126   redistribution_time = MSG_get_clock() - redistribution_time;
127
128   XBT_INFO("redistribution_time=%g ", redistribution_time);
129
130   return 0;
131 }
132
133
134 msg_error_t test_all(const char *platform_file,
135                      const char *application_file)
136 {
137   msg_error_t res = MSG_OK;
138
139
140   MSG_config("workstation/model", "ptask_L07");
141
142   /*  Simulation setting */
143   MSG_create_environment(platform_file);
144
145   /*   Application deployment */
146   MSG_function_register("execute", execute);
147   MSG_function_register("redistribute", redistribute);
148   MSG_launch_application(application_file);
149
150   res = MSG_main();
151
152   XBT_INFO("Simulation time %g", MSG_get_clock());
153   return res;
154 }
155
156
157 int main(int argc, char *argv[])
158 {
159   msg_error_t res = MSG_OK;
160
161   MSG_init(&argc, argv);
162   if (argc < 3) {
163     printf("Usage: %s platform_file deployment_file\n", argv[0]);
164     exit(1);
165   }
166   res = test_all(argv[1], argv[2]);
167
168   if (res == MSG_OK)
169     return 0;
170   else
171     return 1;
172 }