Logo AND Algorithmique Numérique Distribuée

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