1 /* Copyright (c) 2007-2018. 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 #include <stdio.h> /* snprintf */
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
12 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
14 static int master(int argc, char *argv[])
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 workers_count = xbt_str_parse_int(argv[4], "Invalid amount of workers: %s");
23 XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
25 for (i = 0; i < number_of_tasks; i++) {
27 snprintf(mailbox, 255, "worker-%ld", i % workers_count);
29 msg_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size, xbt_new0(double, 1));
30 *((double *) task->data) = MSG_get_clock();
32 switch ( MSG_task_send_with_timeout(task,mailbox,10.0) ) {
34 XBT_INFO("Send completed");
37 case MSG_HOST_FAILURE:
38 XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
40 MSG_task_destroy(task);
43 case MSG_TRANSFER_FAILURE:
44 XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", mailbox);
46 MSG_task_destroy(task);
50 XBT_INFO ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox);
52 MSG_task_destroy(task);
56 xbt_die( "Unexpected behavior");
60 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
61 for (i = 0; i < workers_count; i++) {
63 snprintf(mailbox, 255, "worker-%ld", i % workers_count);
64 msg_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
66 switch (MSG_task_send_with_timeout(task,mailbox,1.0)) {
67 case MSG_HOST_FAILURE:
68 XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
69 MSG_task_destroy(task);
72 case MSG_TRANSFER_FAILURE:
73 XBT_INFO("Mmh. Can't reach '%s'! Nevermind. Let's keep going!", mailbox);
74 MSG_task_destroy(task);
78 XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox);
79 MSG_task_destroy(task);
87 xbt_die("Unexpected behavior with '%s'", mailbox);
91 XBT_INFO("Goodbye now!");
95 static int worker(int argc, char *argv[])
99 long id= xbt_str_parse_int(argv[1], "Invalid argument %s");
101 snprintf(mailbox, 79,"worker-%ld", id);
104 double time1 = MSG_get_clock();
105 msg_task_t task = NULL;
106 int retcode = MSG_task_receive( &(task), mailbox);
107 double time2 = MSG_get_clock();
108 if (retcode == MSG_OK) {
109 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
110 if (MSG_task_get_data(task) == FINALIZE) {
111 MSG_task_destroy(task);
114 if (time1 < *((double *) task->data))
115 time1 = *((double *) task->data);
116 XBT_INFO("Communication time : \"%f\"", time2 - time1);
117 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
118 retcode = MSG_task_execute(task);
119 if (retcode == MSG_OK) {
120 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
122 MSG_task_destroy(task);
123 } else if (retcode == MSG_HOST_FAILURE) {
124 XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
126 MSG_task_destroy(task);
129 XBT_INFO("Hey ?! What's up ? ");
130 xbt_die("Unexpected behavior");
132 } else if (retcode == MSG_HOST_FAILURE) {
133 XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
135 } else if (retcode == MSG_TRANSFER_FAILURE) {
136 XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
138 xbt_die("Unexpected behavior");
141 XBT_INFO("I'm done. See you!");
145 int main(int argc, char *argv[])
147 MSG_init(&argc, argv);
148 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
149 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
151 MSG_create_environment(argv[1]);
153 MSG_function_register("master", master);
154 MSG_function_register("worker", worker);
155 MSG_launch_application(argv[2]);
157 msg_error_t res = MSG_main();
159 XBT_INFO("Simulation time %g", MSG_get_clock());
161 return res != MSG_OK;