Logo AND Algorithmique Numérique Distribuée

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