1 /* Copyright (c) 2010-2016. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/msg.h"
8 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitany, "Messages specific for this msg example");
10 static int sender(int argc, char *argv[])
12 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
13 double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
14 double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
15 long receivers_count = xbt_str_parse_int(argv[4], "Invalid amount of receivers: %s");
16 int diff_com = xbt_str_parse_int(argv[5], "Invalid value for diff_comm: %s");
18 xbt_dynar_t d = xbt_dynar_new(sizeof(msg_comm_t), NULL);
22 char snprintf_buffer[256];
25 for (i = 0; i < number_of_tasks; i++) {
31 snprintf(mailbox,255, "receiver-%ld", (i % receivers_count));
32 snprintf(snprintf_buffer,255, "Task_%d", i);
33 task = MSG_task_create(snprintf_buffer, task_comp_size, task_comm_size / coef, NULL);
34 comm = MSG_task_isend(task, mailbox);
35 xbt_dynar_push_as(d, msg_comm_t, comm);
36 XBT_INFO("Send to receiver-%ld %s comm_size %f", i % receivers_count, snprintf_buffer, task_comm_size / coef);
38 /* Here we are waiting for the completion of all communications */
40 while (xbt_dynar_is_empty(d) == 0) {
41 xbt_dynar_remove_at(d, MSG_comm_waitany(d), &comm);
42 MSG_comm_destroy(comm);
46 /* Here we are waiting for the completion of all tasks */
48 XBT_ATTRIB_UNUSED msg_error_t res_wait;
49 for (i = 0; i < receivers_count; i++) {
51 res_irecv = MSG_task_irecv(&(task), "finalize");
52 res_wait = MSG_comm_wait(res_irecv, -1);
53 xbt_assert(res_wait == MSG_OK, "MSG_comm_wait failed");
54 MSG_comm_destroy(res_irecv);
55 MSG_task_destroy(task);
58 XBT_INFO("Goodbye now!");
62 static int receiver(int argc, char *argv[])
66 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
67 int tasks = xbt_str_parse_int(argv[2], "Invalid amount of tasks: %s");
68 msg_task_t *task = xbt_new(msg_task_t, tasks);
70 XBT_ATTRIB_UNUSED int read;
71 read = sscanf(argv[1], "%d", &id);
72 xbt_assert(read, "Invalid argument %s\n", argv[1]);
73 snprintf(mailbox,79, "receiver-%d", id);
74 MSG_process_sleep(10);
76 for (int i = 0; i < tasks; i++) {
77 XBT_INFO("Wait to receive task %d", i);
79 res_irecv = MSG_task_irecv(&task[i], mailbox);
80 xbt_dynar_push_as(comms, msg_comm_t, res_irecv);
83 /* Here we are waiting for the receiving of all communications */
85 while (!xbt_dynar_is_empty(comms)) {
86 XBT_ATTRIB_UNUSED msg_error_t err;
87 xbt_dynar_remove_at(comms, MSG_comm_waitany(comms), &res_irecv);
88 task_com = MSG_comm_get_task(res_irecv);
89 MSG_comm_destroy(res_irecv);
90 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task_com));
91 MSG_task_execute(task_com);
92 XBT_INFO("\"%s\" done", MSG_task_get_name(task_com));
93 err = MSG_task_destroy(task_com);
94 xbt_assert(err == MSG_OK, "MSG_task_destroy failed");
96 xbt_dynar_free(&comms);
99 /* Here we tell to sender that all tasks are done */
100 res_irecv = MSG_task_isend(MSG_task_create(NULL, 0, 0, NULL), "finalize");
101 MSG_comm_wait(res_irecv, -1);
102 MSG_comm_destroy(res_irecv);
103 XBT_INFO("I'm done. See you!");
107 int main(int argc, char *argv[])
109 msg_error_t res = MSG_OK;
111 MSG_init(&argc, argv);
112 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
113 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
115 MSG_create_environment(argv[1]);
117 MSG_function_register("sender", sender);
118 MSG_function_register("receiver", receiver);
119 MSG_launch_application(argv[2]);
123 XBT_INFO("Simulation time %g", MSG_get_clock());
125 return res != MSG_OK;