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 sender(int argc, char *argv[]);
18 int receiver(int argc, char *argv[]);
20 MSG_error_t test_all(const char *platform_file,
21 const char *application_file);
23 /** Sender function */
24 int sender(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 receivers_count = atol(argv[4]);
31 msg_comm_t *comm = xbt_new(msg_comm_t, number_of_tasks + receivers_count);
34 for (i = 0; i < number_of_tasks; i++) {
36 char sprintf_buffer[256];
37 sprintf(mailbox, "receiver-%ld", i % receivers_count);
38 sprintf(sprintf_buffer, "Task_%d", i);
40 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
42 comm[i] = MSG_task_isend(task, mailbox);
43 XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
45 for (i = 0; i < receivers_count; i++) {
47 sprintf(mailbox, "receiver-%ld", i % receivers_count);
48 task = MSG_task_create("finalize", 0, 0, 0);
49 comm[i + number_of_tasks] = MSG_task_isend(task, mailbox);
50 XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
53 /* Here we are waiting for the completion of all communications */
54 MSG_comm_waitall(comm, (number_of_tasks + receivers_count), -1);
56 XBT_INFO("Goodbye now!");
61 /** Receiver function */
62 int receiver(int argc, char *argv[])
65 _XBT_GNUC_UNUSED MSG_error_t res;
69 _XBT_GNUC_UNUSED int read;
70 read = sscanf(argv[1], "%d", &id);
71 xbt_assert(read, "Invalid argument %s\n", argv[1]);
72 MSG_process_sleep(10);
73 sprintf(mailbox, "receiver-%d", id);
75 res_irecv = MSG_task_irecv(&(task), mailbox);
76 XBT_INFO("Wait to receive a task");
77 res = MSG_comm_wait(res_irecv, -1);
78 xbt_assert(res == MSG_OK, "MSG_task_get failed");
79 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
80 if (!strcmp(MSG_task_get_name(task), "finalize")) {
81 MSG_task_destroy(task);
85 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
86 MSG_task_execute(task);
87 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
88 MSG_task_destroy(task);
91 XBT_INFO("I'm done. See you!");
93 } /* end_of_receiver */
96 MSG_error_t test_all(const char *platform_file,
97 const char *application_file)
99 MSG_error_t res = MSG_OK;
101 /* MSG_config("workstation/model","KCCFLN05"); */
102 { /* Simulation setting */
103 MSG_create_environment(platform_file);
105 { /* Application deployment */
106 MSG_function_register("sender", sender);
107 MSG_function_register("receiver", receiver);
108 MSG_launch_application(application_file);
112 XBT_INFO("Simulation time %g", MSG_get_clock());
114 } /* end_of_test_all */
118 int main(int argc, char *argv[])
120 MSG_error_t res = MSG_OK;
122 MSG_global_init(&argc, argv);
124 printf("Usage: %s platform_file deployment_file\n", argv[0]);
125 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
128 res = test_all(argv[1], argv[2]);