1 /* Copyright (c) 2010-2017. 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. */
7 #include "simgrid/msg.h"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11 static int master(int /*argc*/, char* /*argv*/ [])
13 double task_comp_size = 5E7;
14 double task_comm_size = 1E6;
17 msg_task_t task = MSG_task_create("normal", task_comp_size, task_comm_size, NULL);
18 XBT_INFO("Sending task: \"%s\"", task->name);
19 MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
21 task = MSG_task_create("cancel directly", task_comp_size, task_comm_size, NULL);
22 XBT_INFO("Canceling task \"%s\" directly", task->name);
23 MSG_task_cancel(task);
24 MSG_task_destroy(task);
26 task = MSG_task_create("destroy directly", task_comp_size, task_comm_size, NULL);
27 XBT_INFO("Destroying task \"%s\" directly", task->name);
28 MSG_task_destroy(task);
30 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
31 msg_comm_t comm = MSG_task_isend(task, "worker_mailbox");
32 XBT_INFO("Canceling task \"%s\" during comm", task->name);
33 MSG_task_cancel(task);
35 MSG_comm_wait(comm, -1);
38 MSG_comm_destroy(comm);
40 MSG_task_destroy(task);
42 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
43 comm = MSG_task_isend(task, "worker_mailbox");
44 XBT_INFO("Destroying task \"%s\" during comm", task->name);
45 MSG_task_destroy(task);
47 MSG_comm_wait(comm, -1);
50 MSG_comm_destroy(comm);
53 task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
54 MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
56 task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
57 MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
59 XBT_INFO("Goodbye now!");
63 static int worker_main(int /*argc*/, char* /*argv*/ [])
65 msg_task_t task = (msg_task_t) MSG_process_get_data(MSG_process_self());
67 XBT_INFO("Start %s", task->name);
68 res = MSG_task_execute(task);
69 XBT_INFO("Task %s", res == MSG_OK ? "done" : "failed");
70 MSG_task_destroy(task);
74 static int worker(int /*argc*/, char* /*argv*/ [])
77 msg_task_t task = NULL;
78 XBT_ATTRIB_UNUSED int res = MSG_task_receive(&(task), "worker_mailbox");
79 xbt_assert(res == MSG_OK, "MSG_task_get failed");
80 XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
82 if (not strcmp(MSG_task_get_name(task), "finalize")) {
83 XBT_INFO("Destroying task \"%s\"", task->name);
84 MSG_task_destroy(task);
88 if (not strcmp(MSG_task_get_name(task), "cancel")) {
89 MSG_process_create("worker1", worker_main, task, MSG_host_self());
90 MSG_process_sleep(0.1);
91 XBT_INFO("Canceling task \"%s\"", task->name);
92 MSG_task_cancel(task);
96 double start = MSG_get_clock();
97 MSG_task_execute(task);
98 double end = MSG_get_clock();
99 XBT_INFO("Task \"%s\" done in %f (amount %f)", MSG_task_get_name(task), end - start,
100 MSG_task_get_flops_amount(task));
102 MSG_task_destroy(task);
104 XBT_INFO("I'm done. See you!");
108 int main(int argc, char *argv[])
110 MSG_init(&argc, argv);
111 xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
113 MSG_create_environment(argv[1]);
115 MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay"));
116 MSG_process_create("worker", worker, NULL, MSG_get_host_by_name("Jupiter"));
118 msg_error_t res = MSG_main();
120 XBT_INFO("Simulation time %g", MSG_get_clock());
122 return res != MSG_OK;