Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr/gitroot/simgrid/simgrid
[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 "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 /** @addtogroup MSG_examples
12  * 
13  * - <b>msg/icomms/peer2.c</b>: demonstrates the @ref MSG_comm_waitall function
14  */
15
16 static int sender(int argc, char *argv[])
17 {
18   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
19   double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
20   double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
21   long receivers_count = xbt_str_parse_int(argv[4], "Invalid amount of receivers: %s");
22
23   msg_comm_t *comm = xbt_new(msg_comm_t, number_of_tasks + receivers_count);
24   int i;
25   msg_task_t task = NULL;
26   for (i = 0; i < number_of_tasks; i++) {
27     char mailbox[256];
28     char sprintf_buffer[256];
29     sprintf(mailbox, "receiver-%ld", i % receivers_count);
30     sprintf(sprintf_buffer, "Task_%d", i);
31     task =
32         MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
33                         NULL);
34     comm[i] = MSG_task_isend(task, mailbox);
35     XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
36   }
37   for (i = 0; i < receivers_count; i++) {
38     char mailbox[80];
39     sprintf(mailbox, "receiver-%ld", i % receivers_count);
40     task = MSG_task_create("finalize", 0, 0, 0);
41     comm[i + number_of_tasks] = MSG_task_isend(task, mailbox);
42     XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
43
44   }
45   /* Here we are waiting for the completion of all communications */
46   MSG_comm_waitall(comm, (number_of_tasks + receivers_count), -1);
47   for (i = 0; i < number_of_tasks + receivers_count; i++)
48     MSG_comm_destroy(comm[i]);
49
50   XBT_INFO("Goodbye now!");
51   xbt_free(comm);
52   return 0;
53 }
54
55 static int receiver(int argc, char *argv[])
56 {
57   msg_task_t task = NULL;
58   XBT_ATTRIB_UNUSED msg_error_t res;
59   int id = -1;
60   char mailbox[80];
61   msg_comm_t res_irecv;
62   XBT_ATTRIB_UNUSED int read;
63   read = sscanf(argv[1], "%d", &id);
64   xbt_assert(read, "Invalid argument %s\n", argv[1]);
65   MSG_process_sleep(10);
66   sprintf(mailbox, "receiver-%d", id);
67   while (1) {
68     res_irecv = MSG_task_irecv(&(task), mailbox);
69     XBT_INFO("Wait to receive a task");
70     res = MSG_comm_wait(res_irecv, -1);
71     MSG_comm_destroy(res_irecv);
72     xbt_assert(res == MSG_OK, "MSG_task_get failed");
73     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
74     if (!strcmp(MSG_task_get_name(task), "finalize")) {
75       MSG_task_destroy(task);
76       break;
77     }
78
79     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
80     MSG_task_execute(task);
81     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
82     MSG_task_destroy(task);
83     task = NULL;
84   }
85   XBT_INFO("I'm done. See you!");
86   return 0;
87 }
88
89 int main(int argc, char *argv[])
90 {
91   msg_error_t res = MSG_OK;
92
93   MSG_init(&argc, argv);
94   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
95              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
96
97   MSG_create_environment(argv[1]);
98   MSG_function_register("sender", sender);
99   MSG_function_register("receiver", receiver);
100   MSG_launch_application(argv[2]);
101
102   res = MSG_main();
103
104   XBT_INFO("Simulation time %g", MSG_get_clock());
105
106   return res != MSG_OK;
107 }