1 /* Copyright (c) 2010-2014. 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 "simgrid/msg.h" /* Yeah! If you want to use msg, you need to include simgrid/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 /** @addtogroup MSG_examples
19 * - <b>msg/icomms/peer2.c</b>: demonstrates the @ref MSG_comm_waitall function
22 int sender(int argc, char *argv[]);
23 int receiver(int argc, char *argv[]);
25 msg_error_t test_all(const char *platform_file,
26 const char *application_file);
28 /** Sender function */
29 int sender(int argc, char *argv[])
31 long number_of_tasks = atol(argv[1]);
32 double task_comp_size = atof(argv[2]);
33 double task_comm_size = atof(argv[3]);
34 long receivers_count = atol(argv[4]);
36 msg_comm_t *comm = xbt_new(msg_comm_t, number_of_tasks + receivers_count);
38 msg_task_t task = NULL;
39 for (i = 0; i < number_of_tasks; i++) {
41 char sprintf_buffer[256];
42 sprintf(mailbox, "receiver-%ld", i % receivers_count);
43 sprintf(sprintf_buffer, "Task_%d", i);
45 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
47 comm[i] = MSG_task_isend(task, mailbox);
48 XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
50 for (i = 0; i < receivers_count; i++) {
52 sprintf(mailbox, "receiver-%ld", i % receivers_count);
53 task = MSG_task_create("finalize", 0, 0, 0);
54 comm[i + number_of_tasks] = MSG_task_isend(task, mailbox);
55 XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
58 /* Here we are waiting for the completion of all communications */
59 MSG_comm_waitall(comm, (number_of_tasks + receivers_count), -1);
60 for (i = 0; i < number_of_tasks + receivers_count; i++)
61 MSG_comm_destroy(comm[i]);
63 XBT_INFO("Goodbye now!");
68 /** Receiver function */
69 int receiver(int argc, char *argv[])
71 msg_task_t task = NULL;
72 _XBT_GNUC_UNUSED msg_error_t res;
76 _XBT_GNUC_UNUSED int read;
77 read = sscanf(argv[1], "%d", &id);
78 xbt_assert(read, "Invalid argument %s\n", argv[1]);
79 MSG_process_sleep(10);
80 sprintf(mailbox, "receiver-%d", id);
82 res_irecv = MSG_task_irecv(&(task), mailbox);
83 XBT_INFO("Wait to receive a task");
84 res = MSG_comm_wait(res_irecv, -1);
85 MSG_comm_destroy(res_irecv);
86 xbt_assert(res == MSG_OK, "MSG_task_get failed");
87 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
88 if (!strcmp(MSG_task_get_name(task), "finalize")) {
89 MSG_task_destroy(task);
93 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
94 MSG_task_execute(task);
95 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
96 MSG_task_destroy(task);
99 XBT_INFO("I'm done. See you!");
101 } /* end_of_receiver */
104 msg_error_t test_all(const char *platform_file,
105 const char *application_file)
107 msg_error_t res = MSG_OK;
109 /* MSG_config("workstation/model","KCCFLN05"); */
110 { /* Simulation setting */
111 MSG_create_environment(platform_file);
113 { /* Application deployment */
114 MSG_function_register("sender", sender);
115 MSG_function_register("receiver", receiver);
116 MSG_launch_application(application_file);
120 XBT_INFO("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;
130 MSG_init(&argc, argv);
132 printf("Usage: %s platform_file deployment_file\n", argv[0]);
133 printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
136 res = test_all(argv[1], argv[2]);