Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c4a817334c0594620f3e1a2b7f90aeadcc7446c4
[simgrid.git] / examples / c / comm-waitall / comm-waitall.c
1 /* Copyright (c) 2010-2022. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/actor.h"
7 #include "simgrid/comm.h"
8 #include "simgrid/engine.h"
9 #include "simgrid/host.h"
10 #include "simgrid/mailbox.h"
11
12 #include "xbt/log.h"
13 #include "xbt/str.h"
14 #include "xbt/sysdep.h"
15
16 #include <stdio.h> /* snprintf */
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(comm_waitall, "Messages specific for this msg example");
19
20 static void sender(int argc, char* argv[])
21 {
22   xbt_assert(argc == 4, "This function expects 3 parameters from the XML deployment file");
23   long messages_count  = xbt_str_parse_int(argv[1], "Invalid message count");
24   long message_size    = xbt_str_parse_int(argv[2], "Invalid message size");
25   long receivers_count = xbt_str_parse_int(argv[3], "Invalid amount of receivers");
26   xbt_assert(receivers_count > 0);
27
28   /* Array in which we store all ongoing communications */
29   sg_comm_t* pending_comms = xbt_malloc(sizeof(sg_comm_t) * (messages_count + receivers_count));
30   int pending_comms_count  = 0;
31
32   /* Make an array of the mailboxes to use */
33   sg_mailbox_t* mboxes = xbt_malloc(sizeof(sg_mailbox_t) * receivers_count);
34   for (long i = 0; i < receivers_count; i++) {
35     char mailbox_name[80];
36     snprintf(mailbox_name, 79, "receiver-%ld", i);
37     sg_mailbox_t mbox = sg_mailbox_by_name(mailbox_name);
38     mboxes[i]         = mbox;
39   }
40
41   /* Start dispatching all messages to receivers, in a round robin fashion */
42   for (long i = 0; i < messages_count; i++) {
43     char msg_content[80];
44     snprintf(msg_content, 79, "Message %ld", i);
45     sg_mailbox_t mbox = mboxes[i % receivers_count];
46     XBT_INFO("Send '%s' to '%s'", msg_content, sg_mailbox_get_name(mbox));
47     /* Create a communication representing the ongoing communication, and store it in pending_comms */
48     pending_comms[pending_comms_count++] = sg_mailbox_put_async(mbox, xbt_strdup(msg_content), message_size);
49   }
50
51   /* Start sending messages to let the workers know that they should stop */
52   for (long i = 0; i < receivers_count; i++) {
53     XBT_INFO("Send 'finalize' to 'receiver-%ld'", i);
54     char* end_msg                        = xbt_strdup("finalize");
55     sg_mailbox_t mbox                    = mboxes[i % receivers_count];
56     pending_comms[pending_comms_count++] = sg_mailbox_put_async(mbox, end_msg, 0);
57   }
58
59   XBT_INFO("Done dispatching all messages");
60
61   /* Now that all message exchanges were initiated, wait for their completion in one single call */
62   sg_comm_wait_all(pending_comms, pending_comms_count);
63
64   free(pending_comms);
65   free(mboxes);
66
67   XBT_INFO("Goodbye now!");
68 }
69
70 static void receiver(int argc, char* argv[])
71 {
72   xbt_assert(argc == 2, "Expecting one parameter from the XML deployment file but got %d", argc);
73   int id = (int)xbt_str_parse_int(argv[1], "ID should be numerical");
74   char mailbox_name[80];
75   snprintf(mailbox_name, 79, "receiver-%d", id);
76   sg_mailbox_t mbox = sg_mailbox_by_name(mailbox_name);
77   XBT_INFO("Wait for my first message");
78   while (1) {
79     char* received = (char*)sg_mailbox_get(mbox);
80     XBT_INFO("I got a '%s'.", received);
81     if (!strcmp(received, "finalize")) { // If it's a finalize message, we're done
82       xbt_free(received);
83       break;
84     }
85     xbt_free(received);
86   }
87 }
88
89 int main(int argc, char* argv[])
90 {
91   simgrid_init(&argc, argv);
92   xbt_assert(argc > 2,
93              "Usage: %s platform_file deployment_file\n"
94              "\tExample: %s platform.xml deployment.xml\n",
95              argv[0], argv[0]);
96
97   simgrid_load_platform(argv[1]);
98
99   simgrid_register_function("sender", sender);
100   simgrid_register_function("receiver", receiver);
101   simgrid_load_deployment(argv[2]);
102
103   simgrid_run();
104
105   return 0;
106 }