Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
93e933cf50081cbc832cc8c7a6826248ce98da9f
[simgrid.git] / examples / msg / icomms / peer3.c
1 /* Copyright (c) 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 need to include msg/msg.h */
9 #include "xbt/sysdep.h"         /* calloc, printf */
10 #include <math.h>
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 sender(int argc, char *argv[]);
18 int receiver(int argc, char *argv[]);
19
20 MSG_error_t test_all(const char *platform_file,
21                      const char *application_file);
22
23 /** Sender function  */
24 int sender(int argc, char *argv[])
25 {
26   long number_of_tasks = atol(argv[1]);
27   double task_comp_size = atof(argv[2]);
28   double task_comm_size = atof(argv[3]);
29   long receivers_count = atol(argv[4]);
30   int diff_com = atol(argv[5]);
31   double coef = 0;
32   xbt_dynar_t d = NULL;
33   int i;
34   m_task_t task = NULL;
35   char mailbox[256];
36   char sprintf_buffer[256];
37   d = xbt_dynar_new(sizeof(msg_comm_t), NULL);
38   msg_comm_t *comm = xbt_new(msg_comm_t, number_of_tasks + receivers_count);
39   msg_comm_t res_irecv = NULL;
40   for (i = 0; i < (number_of_tasks); i++) {
41     task = NULL;
42     if (diff_com == 0)
43       coef = 1;
44     else
45       coef = (i + 1);
46
47     sprintf(mailbox, "receiver-%ld", (i % receivers_count));
48     sprintf(sprintf_buffer, "Task_%d", i);
49     task =
50         MSG_task_create(sprintf_buffer, task_comp_size,
51                         task_comm_size / coef, NULL);
52     comm[i] = MSG_task_isend(task, mailbox);
53     xbt_dynar_push_as(d, msg_comm_t, comm[i]);
54     INFO3("Send to receiver-%ld %s comm_size %f", i % receivers_count,
55           sprintf_buffer, task_comm_size / coef);
56   }
57   /* Here we are waiting for the completion of all communications */
58
59   while (d->used) {
60     xbt_dynar_remove_at(d, MSG_comm_waitany(d), &res_irecv);
61     MSG_comm_destroy(res_irecv);
62   }
63   xbt_dynar_free(&d);
64   xbt_free(comm);
65
66   /* Here we are waiting for the completion of all tasks */
67   sprintf(mailbox, "finalize");
68
69   for (i = 0; i < receivers_count; i++) {
70     task = NULL;
71     res_irecv = NULL;
72     res_irecv = MSG_task_irecv(&(task), mailbox);
73     xbt_assert0(MSG_comm_wait(res_irecv, -1) == MSG_OK,
74                 "MSG_comm_wait failed");
75     MSG_task_destroy(task);
76   }
77
78   INFO0("Goodbye now!");
79   return 0;
80 }                               /* end_of_sender */
81
82 /** Receiver function  */
83 int receiver(int argc, char *argv[])
84 {
85   int id = -1;
86   int i;
87   char mailbox[80];
88   xbt_dynar_t comms = NULL;
89   int tasks = atof(argv[2]);
90   comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
91
92   xbt_assert1(sscanf(argv[1], "%d", &id),
93               "Invalid argument %s\n", argv[1]);
94   sprintf(mailbox, "receiver-%d", id);
95   MSG_process_sleep(10);
96   msg_comm_t res_irecv = NULL;
97   m_task_t task = NULL;
98   m_task_t task_com = NULL;
99   for (i = 0; i < tasks; i++) {
100     INFO1("Wait to receive task %d", i);
101     res_irecv = MSG_task_irecv(&task, mailbox);
102     xbt_dynar_push_as(comms, msg_comm_t, res_irecv);
103     task = NULL;
104     res_irecv = NULL;
105   }
106
107   /* Here we are waiting for the receiving of all communications */
108   while (comms->used) {
109     task_com = NULL;
110     res_irecv = NULL;
111     MSG_error_t err = MSG_OK;
112     int num = MSG_comm_waitany(comms);
113     xbt_dynar_remove_at(comms, num, &res_irecv);
114     task_com = MSG_comm_get_task(res_irecv);
115     MSG_comm_destroy(res_irecv);
116     INFO1("Processing \"%s\"", MSG_task_get_name(task_com));
117     MSG_task_execute(task_com);
118     INFO1("\"%s\" done", MSG_task_get_name(task_com));
119     err = MSG_task_destroy(task_com);
120     xbt_assert0(err == MSG_OK, "MSG_task_destroy failed");
121   }
122   xbt_dynar_free(&comms);
123
124   /* Here we tell to sender that all tasks are done */
125   sprintf(mailbox, "finalize");
126   res_irecv = MSG_task_isend(MSG_task_create(NULL, 0, 0, NULL), mailbox);
127   MSG_comm_wait(res_irecv, -1);
128   INFO0("I'm done. See you!");
129   return 0;
130 }                               /* end_of_receiver */
131
132 /** Test function */
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   /* MSG_config("workstation/model","KCCFLN05"); */
139   {                             /*  Simulation setting */
140     MSG_set_channel_number(0);
141     MSG_create_environment(platform_file);
142   }
143   {                             /*   Application deployment */
144     MSG_function_register("sender", sender);
145     MSG_function_register("receiver", receiver);
146     MSG_launch_application(application_file);
147   }
148   res = MSG_main();
149
150   INFO1("Simulation time %g", MSG_get_clock());
151   return res;
152 }                               /* end_of_test_all */
153
154
155 /** Main function */
156 int main(int argc, char *argv[])
157 {
158   MSG_error_t res = MSG_OK;
159
160   MSG_global_init(&argc, argv);
161   if (argc < 3) {
162     printf("Usage: %s platform_file deployment_file\n", argv[0]);
163     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
164     exit(1);
165   }
166   res = test_all(argv[1], argv[2]);
167   SIMIX_message_sizes_output("toto.txt");
168   MSG_clean();
169
170   if (res == MSG_OK)
171     return 0;
172   else
173     return 1;
174 }                               /* end_of_main */