1 /* Copyright (c) 2007, 2008, 2009, 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 "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
11 /* Create a log channel to have nice outputs. */
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14 "Messages specific for this msg example");
15 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19 MSG_error_t test_all(const char*);
26 /** Emitter function */
27 int master(int argc, char *argv[])
29 long number_of_tasks = atol(argv[1]);
30 double task_comp_size = atof(argv[2]);
31 double task_comm_size = atof(argv[3]);
32 long slaves_count = atol(argv[4]);
35 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 if (number_of_tasks<10000 || i%10000 == 0)
45 INFO3("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name, number_of_tasks, mailbox);
46 MSG_task_send(task, mailbox);
49 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
50 for (i = 0; i < slaves_count; i++) {
53 sprintf(mailbox,"slave-%ld",i % slaves_count);
54 m_task_t finalize = MSG_task_create ("finalize", 0, 0, 0);
55 MSG_task_send(finalize, mailbox);
58 INFO0("Goodbye now!");
62 /** Receiver function */
63 int slave(int argc, char *argv[])
70 xbt_assert1(sscanf(argv[1],"%d", &id),
71 "Invalid argument %s\n",argv[1]);
73 sprintf(mailbox,"slave-%d",id);
76 res = MSG_task_receive(&(task), mailbox);
77 xbt_assert0(res == MSG_OK, "MSG_task_get failed");
79 INFO1("Received \"%s\"", MSG_task_get_name(task));
80 if (!strcmp(MSG_task_get_name(task),"finalize")) {
81 MSG_task_destroy(task);
85 INFO1("Processing \"%s\"", MSG_task_get_name(task));
86 MSG_task_execute(task);
87 INFO1("\"%s\" done", MSG_task_get_name(task));
88 MSG_task_destroy(task);
91 INFO0("I'm done. See you!");
96 MSG_error_t test_all(const char *file)//(void)
98 MSG_error_t res = MSG_OK;
99 /* Simulation setting */
100 MSG_set_channel_number(MAX_CHANNEL);
101 /*start by registering functions before loading script */
102 MSG_function_register("master", master);
103 MSG_function_register("slave", slave);
104 MSG_load_platform_script(file);
108 INFO1("Simulation time %g", MSG_get_clock());
110 } /* end_of_test_all */
113 int main(int argc, char *argv[])
115 MSG_error_t res = MSG_OK;
117 MSG_global_init(&argc, argv);
119 printf ("Usage: %s platform_script[.lua]\n",argv[0]);
120 printf ("example: %s platform_script.lua\n",argv[0]);
123 res = test_all(argv[1]);