1 /* Copyright (c) 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "xbt/sysdep.h" /* calloc, printf */
11 /* Create a log channel to have nice outputs. */
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15 "Messages specific for this msg example");
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19 int forwarder(int argc, char *argv[]);
20 msg_error_t test_all(const char *platform_file,
21 const char *application_file);
23 /** Emitter function */
24 int master(int argc, char *argv[])
26 long number_of_tasks = atol(argv[1]);
27 double task_comp_size = atof(argv[2]);
28 double task_comm_size = atof(argv[3]);
29 long slaves_count = atol(argv[4]);
33 XBT_INFO("Got %ld slaves and %ld tasks to process", slaves_count,
36 for (i = 0; i < number_of_tasks; i++) {
38 char sprintf_buffer[256];
39 msg_task_t task = NULL;
41 sprintf(mailbox, "slave-%ld", i % slaves_count);
42 sprintf(sprintf_buffer, "Task_%d", i);
44 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
46 if (number_of_tasks < 10000 || i % 10000 == 0)
47 XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name,
48 number_of_tasks, mailbox);
50 MSG_task_send(task, mailbox);
54 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
55 for (i = 0; i < slaves_count; i++) {
58 sprintf(mailbox, "slave-%ld", i % slaves_count);
59 msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
60 MSG_task_send(finalize, mailbox);
63 // XBT_INFO("Goodbye now!");
67 /** Receiver function */
68 int slave(int argc, char *argv[])
70 msg_task_t task = NULL;
71 _XBT_GNUC_UNUSED int res;
74 _XBT_GNUC_UNUSED int read;
76 read = sscanf(argv[1], "%d", &id);
77 xbt_assert(read, "Invalid argument %s\n", argv[1]);
79 sprintf(mailbox, "slave-%d", id);
82 res = MSG_task_receive(&(task), mailbox);
83 xbt_assert(res == MSG_OK, "MSG_task_get failed");
85 // XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
86 if (!strcmp(MSG_task_get_name(task), "finalize")) {
87 MSG_task_destroy(task);
90 // XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
91 MSG_task_execute(task);
92 // XBT_INFO("\"%s\" done", MSG_task_get_name(task));
93 MSG_task_destroy(task);
96 XBT_INFO("I'm done. See you!");
101 int main(int argc, char *argv[])
104 const char *platform_file;
105 const char *application_file;
107 MSG_init(&argc, argv);
109 printf("Usage: %s platform_file deployment_file\n", argv[0]);
110 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
113 platform_file = argv[1];
114 application_file = argv[2];
116 /* MSG_config("workstation/model","KCCFLN05"); */
117 { /* Simulation setting */
118 MSG_create_environment(platform_file);
120 { /* Application deployment */
121 MSG_function_register("master", master);
122 MSG_function_register("slave", slave);
123 MSG_launch_application(application_file);
127 XBT_INFO("Simulation time %g", MSG_get_clock());