Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename the async-* tests into comm-*
[simgrid.git] / examples / c / comm-waitall / comm-waitall.c
1 /* Copyright (c) 2010-2020. 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/asserts.h"
13 #include "xbt/log.h"
14 #include "xbt/str.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: %s");
24   long message_size    = xbt_str_parse_int(argv[2], "Invalid message size: %s");
25   long receivers_count = xbt_str_parse_int(argv[3], "Invalid amount of receivers: %s");
26
27   /* Array in which we store all ongoing communications */
28   sg_comm_t* pending_comms = xbt_malloc(sizeof(sg_comm_t) * (messages_count + receivers_count));
29   int pending_comms_count  = 0;
30
31   /* Make an array of the mailboxes to use */
32   sg_mailbox_t* mboxes = xbt_malloc(sizeof(sg_mailbox_t) * receivers_count);
33   for (long i = 0; i < receivers_count; i++) {
34     char mailbox_name[80];
35     snprintf(mailbox_name, 79, "receiver-%ld", i);
36     sg_mailbox_t mbox = sg_mailbox_by_name(mailbox_name);
37     mboxes[i]         = mbox;
38   }
39
40   /* Start dispatching all messages to receivers, in a round robin fashion */
41   for (int i = 0; i < messages_count; i++) {
42     char msg_content[80];
43     snprintf(msg_content, 79, "Message %d", i);
44     sg_mailbox_t mbox = mboxes[i % receivers_count];
45     XBT_INFO("Send '%s' to '%s'", msg_content, sg_mailbox_get_name(mbox));
46     /* Create a communication representing the ongoing communication, and store it in pending_comms */
47     pending_comms[pending_comms_count++] = sg_mailbox_put_async(mbox, xbt_strdup(msg_content), message_size);
48   }
49
50   /* Start sending messages to let the workers know that they should stop */
51   for (int i = 0; i < receivers_count; i++) {
52     XBT_INFO("Send 'finalize' to 'receiver-%d'", i);
53     char* end_msg                        = xbt_strdup("finalize");
54     sg_mailbox_t mbox                    = mboxes[i % receivers_count];
55     pending_comms[pending_comms_count++] = sg_mailbox_put_async(mbox, end_msg, 0);
56   }
57
58   XBT_INFO("Done dispatching all messages");
59
60   /* Now that all message exchanges were initiated, wait for their completion in one single call */
61   sg_comm_wait_all(pending_comms, pending_comms_count);
62
63   free(pending_comms);
64   free(mboxes);
65
66   XBT_INFO("Goodbye now!");
67 }
68
69 static void receiver(int argc, char* argv[])
70 {
71   xbt_assert(argc == 2, "Expecting one parameter from the XML deployment file but got %d", argc);
72   int id = xbt_str_parse_int(argv[1], "ID should be numerical, not %s");
73   char mailbox_name[80];
74   snprintf(mailbox_name, 79, "receiver-%d", id);
75   sg_mailbox_t mbox = sg_mailbox_by_name(mailbox_name);
76   XBT_INFO("Wait for my first message");
77   while (1) {
78     char* received = (char*)sg_mailbox_get(mbox);
79     XBT_INFO("I got a '%s'.", received);
80     if (!strcmp(received, "finalize")) { // If it's a finalize message, we're done
81       xbt_free(received);
82       break;
83     }
84     xbt_free(received);
85   }
86 }
87
88 int main(int argc, char* argv[])
89 {
90   simgrid_init(&argc, argv);
91   xbt_assert(argc > 2,
92              "Usage: %s platform_file deployment_file\n"
93              "\tExample: %s msg_platform.xml msg_deployment.xml\n",
94              argv[0], argv[0]);
95
96   simgrid_load_platform(argv[1]);
97
98   simgrid_register_function("sender", sender);
99   simgrid_register_function("receiver", receiver);
100   simgrid_load_deployment(argv[2]);
101
102   simgrid_run();
103
104   return 0;
105 }