Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #2 from mquinson/master
[simgrid.git] / examples / msg / icomms / peer2.c
1 /* Copyright (c) 2010-2015. 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 "simgrid/msg.h"            /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
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/peer2.c</b>: demonstrates the @ref MSG_comm_waitall 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 = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
32   double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
33   double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
34   long receivers_count = xbt_str_parse_int(argv[4], "Invalid amount of receivers: %s");
35
36   msg_comm_t *comm = xbt_new(msg_comm_t, number_of_tasks + receivers_count);
37   int i;
38   msg_task_t task = NULL;
39   for (i = 0; i < number_of_tasks; i++) {
40     char mailbox[256];
41     char sprintf_buffer[256];
42     sprintf(mailbox, "receiver-%ld", i % receivers_count);
43     sprintf(sprintf_buffer, "Task_%d", i);
44     task =
45         MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
46                         NULL);
47     comm[i] = MSG_task_isend(task, mailbox);
48     XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
49   }
50   for (i = 0; i < receivers_count; i++) {
51     char mailbox[80];
52     sprintf(mailbox, "receiver-%ld", i % receivers_count);
53     task = MSG_task_create("finalize", 0, 0, 0);
54     comm[i + number_of_tasks] = MSG_task_isend(task, mailbox);
55     XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
56
57   }
58   /* Here we are waiting for the completion of all communications */
59   MSG_comm_waitall(comm, (number_of_tasks + receivers_count), -1);
60   for (i = 0; i < number_of_tasks + receivers_count; i++)
61     MSG_comm_destroy(comm[i]);
62
63   XBT_INFO("Goodbye now!");
64   xbt_free(comm);
65   return 0;
66 }                               /* end_of_sender */
67
68 /** Receiver function  */
69 int receiver(int argc, char *argv[])
70 {
71   msg_task_t task = NULL;
72   XBT_ATTRIB_UNUSED msg_error_t res;
73   int id = -1;
74   char mailbox[80];
75   msg_comm_t res_irecv;
76   XBT_ATTRIB_UNUSED int read;
77   read = sscanf(argv[1], "%d", &id);
78   xbt_assert(read, "Invalid argument %s\n", argv[1]);
79   MSG_process_sleep(10);
80   sprintf(mailbox, "receiver-%d", id);
81   while (1) {
82     res_irecv = MSG_task_irecv(&(task), mailbox);
83     XBT_INFO("Wait to receive a task");
84     res = MSG_comm_wait(res_irecv, -1);
85     MSG_comm_destroy(res_irecv);
86     xbt_assert(res == MSG_OK, "MSG_task_get failed");
87     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
88     if (!strcmp(MSG_task_get_name(task), "finalize")) {
89       MSG_task_destroy(task);
90       break;
91     }
92
93     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
94     MSG_task_execute(task);
95     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
96     MSG_task_destroy(task);
97     task = NULL;
98   }
99   XBT_INFO("I'm done. See you!");
100   return 0;
101 }                               /* end_of_receiver */
102
103 /** Test function */
104 msg_error_t test_all(const char *platform_file,
105                      const char *application_file)
106 {
107   msg_error_t res = MSG_OK;
108
109   {                             /*  Simulation setting */
110     MSG_create_environment(platform_file);
111   }
112   {                             /*   Application deployment */
113     MSG_function_register("sender", sender);
114     MSG_function_register("receiver", receiver);
115     MSG_launch_application(application_file);
116   }
117   res = MSG_main();
118
119   XBT_INFO("Simulation time %g", MSG_get_clock());
120   return res;
121 }                               /* end_of_test_all */
122
123
124 /** Main function */
125 int main(int argc, char *argv[])
126 {
127   msg_error_t res = MSG_OK;
128
129   MSG_init(&argc, argv);
130   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
131            "\tExample: %s msg_platform.xml msg_deployment.xml\n", 
132            argv[0], argv[0]);
133
134   res = test_all(argv[1], argv[2]);
135
136   return res != MSG_OK;
137 }