Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ffde17484a8210ca5faec9fe74b99985ed51d1bb
[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     XBT_INFO("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   MSG_error_t res_wait;
68   for (i = 0; i < receivers_count; i++) {
69     task = NULL;
70     res_irecv = MSG_task_irecv(&(task), mailbox);
71     res_wait = MSG_comm_wait(res_irecv, -1);
72     xbt_assert0(res_wait == MSG_OK, "MSG_comm_wait failed");
73     MSG_comm_destroy(res_irecv);
74     MSG_task_destroy(task);
75   }
76
77   XBT_INFO("Goodbye now!");
78   return 0;
79 }                               /* end_of_sender */
80
81 /** Receiver function  */
82 int receiver(int argc, char *argv[])
83 {
84   int id = -1;
85   int i;
86   char mailbox[80];
87   xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
88   int tasks = atof(argv[2]);
89   m_task_t *task = xbt_new(m_task_t, tasks);
90
91   int read;
92   read = sscanf(argv[1], "%d", &id);
93   xbt_assert1(read, "Invalid argument %s\n", argv[1]);
94   sprintf(mailbox, "receiver-%d", id);
95   MSG_process_sleep(10);
96   msg_comm_t res_irecv;
97   for (i = 0; i < tasks; i++) {
98     XBT_INFO("Wait to receive task %d", i);
99     task[i] = NULL;
100     res_irecv = MSG_task_irecv(&task[i], mailbox);
101     xbt_dynar_push_as(comms, msg_comm_t, res_irecv);
102   }
103
104   /* Here we are waiting for the receiving of all communications */
105   m_task_t task_com;
106   while (!xbt_dynar_is_empty(comms)) {
107     MSG_error_t err;
108     xbt_dynar_remove_at(comms, MSG_comm_waitany(comms), &res_irecv);
109     task_com = MSG_comm_get_task(res_irecv);
110     MSG_comm_destroy(res_irecv);
111     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task_com));
112     MSG_task_execute(task_com);
113     XBT_INFO("\"%s\" done", MSG_task_get_name(task_com));
114     err = MSG_task_destroy(task_com);
115     xbt_assert0(err == MSG_OK, "MSG_task_destroy failed");
116   }
117   xbt_dynar_free(&comms);
118   xbt_free(task);
119
120   /* Here we tell to sender that all tasks are done */
121   sprintf(mailbox, "finalize");
122   res_irecv = MSG_task_isend(MSG_task_create(NULL, 0, 0, NULL), mailbox);
123   MSG_comm_wait(res_irecv, -1);
124   MSG_comm_destroy(res_irecv);
125   XBT_INFO("I'm done. See you!");
126   return 0;
127 }                               /* end_of_receiver */
128
129 /** Test function */
130 MSG_error_t test_all(const char *platform_file,
131                      const char *application_file)
132 {
133   MSG_error_t res = MSG_OK;
134
135   /* MSG_config("workstation/model","KCCFLN05"); */
136   {                             /*  Simulation setting */
137     MSG_set_channel_number(0);
138     MSG_create_environment(platform_file);
139   }
140   {                             /*   Application deployment */
141     MSG_function_register("sender", sender);
142     MSG_function_register("receiver", receiver);
143     MSG_launch_application(application_file);
144   }
145   res = MSG_main();
146
147   XBT_INFO("Simulation time %g", MSG_get_clock());
148   return res;
149 }                               /* end_of_test_all */
150
151
152 /** Main function */
153 int main(int argc, char *argv[])
154 {
155   MSG_error_t res = MSG_OK;
156
157   MSG_global_init(&argc, argv);
158   if (argc < 3) {
159     printf("Usage: %s platform_file deployment_file\n", argv[0]);
160     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
161     exit(1);
162   }
163   res = test_all(argv[1], argv[2]);
164   MSG_clean();
165
166   if (res == MSG_OK)
167     return 0;
168   else
169     return 1;
170 }                               /* end_of_main */