3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
10 #include "xbt/sysdep.h" /* calloc, printf */
12 /* Create a log channel to have nice outputs. */
14 #include "xbt/asserts.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"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, const char *application_file);
22 /** Emitter function */
23 int master(int argc, char *argv[])
25 long number_of_tasks = atol(argv[1]);
26 double task_comp_size = atof(argv[2]);
27 double task_comm_size = atof(argv[3]);
28 long slaves_count = atol(argv[4]);
30 TRACE_host_variable_set ("is_master", 1);
34 INFO2("Got %ld slaves and %ld tasks to process", slaves_count,number_of_tasks);
36 for (i = 0; i < number_of_tasks; i++) {
38 char sprintf_buffer[256];
41 sprintf(mailbox,"slave-%ld",i % slaves_count);
42 sprintf(sprintf_buffer, "Task_%d", i);
43 task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
44 TRACE_host_variable_set ("task_creation", i);
45 TRACE_msg_set_task_category (task, "compute");
46 if (number_of_tasks<10000 || i%10000 == 0)
47 INFO3("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name, number_of_tasks, mailbox);
49 MSG_task_send(task, mailbox);
52 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
53 for (i = 0; i < slaves_count; i++) {
56 sprintf(mailbox,"slave-%ld",i % slaves_count);
57 m_task_t finalize = MSG_task_create ("finalize", 0, 0, 0);
58 TRACE_msg_set_task_category(finalize, "finalize");
59 MSG_task_send(finalize, mailbox);
62 // INFO0("Goodbye now!");
66 /** Receiver function */
67 int slave(int argc, char *argv[])
74 TRACE_host_variable_set ("is_slave", 1);
76 xbt_assert1(sscanf(argv[1],"%d", &id),
77 "Invalid argument %s\n",argv[1]);
79 sprintf(mailbox,"slave-%d",id);
82 res = MSG_task_receive(&(task), mailbox);
83 xbt_assert0(res == MSG_OK, "MSG_task_get failed");
85 // INFO1("Received \"%s\"", MSG_task_get_name(task));
86 if (!strcmp(MSG_task_get_name(task),"finalize")) {
87 MSG_task_destroy(task);
91 // INFO1("Processing \"%s\"", MSG_task_get_name(task));
92 TRACE_host_variable_add ("task_computation", MSG_task_get_compute_duration(task));
93 MSG_task_execute(task);
94 // INFO1("\"%s\" done", MSG_task_get_name(task));
95 MSG_task_destroy(task);
98 INFO0("I'm done. See you!");
103 MSG_error_t test_all(const char *platform_file,
104 const char *application_file)
106 MSG_error_t res = MSG_OK;
108 /* MSG_config("workstation/model","KCCFLN05"); */
109 { /* Simulation setting */
110 MSG_set_channel_number(0);
111 MSG_create_environment(platform_file);
113 { /* Application deployment */
114 MSG_function_register("master", master);
115 MSG_function_register("slave", slave);
116 MSG_launch_application(application_file);
120 INFO1("Simulation time %g",MSG_get_clock());
122 } /* end_of_test_all */
126 int main(int argc, char *argv[])
128 MSG_error_t res = MSG_OK;
132 for (i = 0; i < argc; i++){
133 if (!strcmp (argv[i], "--trace")){
139 //if TRACE_start is not called, all other tracing
140 //functions will be disabled
141 TRACE_start ("simulation.trace");
143 TRACE_host_variable_declare ("is_slave");
144 TRACE_host_variable_declare ("is_master");
145 TRACE_host_variable_declare ("task_creation");
146 TRACE_host_variable_declare ("task_computation");
147 TRACE_category ("compute");
148 TRACE_category ("finalize");
150 MSG_global_init(&argc,argv);
152 printf ("Usage: %s platform_file deployment_file\n",argv[0]);
153 printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
156 res = test_all(argv[1],argv[2]);
157 SIMIX_message_sizes_output("toto.txt");