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,"Messages specific for this msg example");
16 int master(int argc, char *argv[]);
17 int slave(int argc, char *argv[]);
18 int forwarder(int argc, char *argv[]);
19 MSG_error_t test_all(const char *platform_file, const char *application_file);
21 /** Emitter function */
22 int master(int argc, char *argv[])
24 long number_of_tasks = atol(argv[1]);
25 double task_comp_size = atof(argv[2]);
26 double task_comm_size = atof(argv[3]);
27 long slaves_count = atol(argv[4]);
29 TRACE_host_variable_set ("is_master", 1);
33 INFO2("Got %ld slaves and %ld tasks to process", slaves_count,number_of_tasks);
35 for (i = 0; i < number_of_tasks; i++) {
37 char sprintf_buffer[256];
40 sprintf(mailbox,"slave-%ld",i % slaves_count);
41 sprintf(sprintf_buffer, "Task_%d", i);
42 task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
43 TRACE_host_variable_set ("task_creation", i);
44 TRACE_msg_set_task_category (task, "compute");
45 if (number_of_tasks<10000 || i%10000 == 0)
46 INFO3("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name, number_of_tasks, mailbox);
48 MSG_task_send(task, mailbox);
51 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
52 for (i = 0; i < slaves_count; i++) {
55 sprintf(mailbox,"slave-%ld",i % slaves_count);
56 m_task_t finalize = MSG_task_create ("finalize", 0, 0, 0);
57 TRACE_msg_set_task_category(finalize, "finalize");
58 MSG_task_send(finalize, mailbox);
61 // INFO0("Goodbye now!");
65 /** Receiver function */
66 int slave(int argc, char *argv[])
73 TRACE_host_variable_set ("is_slave", 1);
75 xbt_assert1(sscanf(argv[1],"%d", &id),
76 "Invalid argument %s\n",argv[1]);
78 sprintf(mailbox,"slave-%d",id);
81 res = MSG_task_receive(&(task), mailbox);
82 xbt_assert0(res == MSG_OK, "MSG_task_get failed");
84 // INFO1("Received \"%s\"", MSG_task_get_name(task));
85 if (!strcmp(MSG_task_get_name(task),"finalize")) {
86 MSG_task_destroy(task);
90 // INFO1("Processing \"%s\"", MSG_task_get_name(task));
91 TRACE_host_variable_add ("task_computation", MSG_task_get_compute_duration(task));
92 MSG_task_execute(task);
93 // INFO1("\"%s\" done", MSG_task_get_name(task));
94 MSG_task_destroy(task);
97 INFO0("I'm done. See you!");
102 MSG_error_t test_all(const char *platform_file,
103 const char *application_file)
105 MSG_error_t res = MSG_OK;
107 /* MSG_config("workstation/model","KCCFLN05"); */
108 { /* Simulation setting */
109 MSG_set_channel_number(0);
110 MSG_create_environment(platform_file);
112 { /* Application deployment */
113 MSG_function_register("master", master);
114 MSG_function_register("slave", slave);
115 MSG_launch_application(application_file);
119 INFO1("Simulation time %g",MSG_get_clock());
121 } /* end_of_test_all */
125 int main(int argc, char *argv[])
127 MSG_error_t res = MSG_OK;
131 for (i = 0; i < argc; i++){
132 if (!strcmp (argv[i], "--trace")){
138 //if TRACE_start is not called, all other tracing
139 //functions will be disabled
140 TRACE_start ("simulation.trace");
142 TRACE_host_variable_declare ("is_slave");
143 TRACE_host_variable_declare ("is_master");
144 TRACE_host_variable_declare ("task_creation");
145 TRACE_host_variable_declare ("task_computation");
146 TRACE_category ("compute");
147 TRACE_category ("finalize");
149 MSG_global_init(&argc,argv);
151 printf ("Usage: %s platform_file deployment_file\n",argv[0]);
152 printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
155 res = test_all(argv[1],argv[2]);
156 SIMIX_message_sizes_output("toto.txt");