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 sender(int argc, char *argv[]);
17 int receiver(int argc, char *argv[]);
19 MSG_error_t test_all(const char *platform_file, const char *application_file);
21 /** Sender function */
22 int sender(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 receivers_count = atol(argv[4]);
32 char sprintf_buffer[256];
33 d = xbt_dynar_new(sizeof(msg_comm_t), NULL);
34 msg_comm_t *comm = malloc((number_of_tasks+receivers_count)*sizeof(msg_comm_t));
35 msg_comm_t res_irecv = NULL;
36 for (i = 0; i < (number_of_tasks); i++)
38 sprintf(mailbox,"receiver-%ld",(i % receivers_count));
39 sprintf(sprintf_buffer, "Task_%d", i);
40 task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
41 comm[i] = MSG_task_isend(task, mailbox);
42 MSG_task_refcount_dec(task);
43 xbt_dynar_push_as(d, msg_comm_t, comm[i]);
44 INFO3("Send to receiver-%ld %s comm_size %f",i % receivers_count,sprintf_buffer,task_comm_size);
46 /* Here we are waiting for the completion of all communications*/
50 xbt_dynar_remove_at(d, MSG_comm_waitany(d), &res_irecv);
55 /* Here we are waiting for the completion of all tasks*/
56 sprintf(mailbox,"finalize");
58 for(i=0; i<receivers_count; i++)
62 res_irecv = MSG_task_irecv(&(task), mailbox);
63 xbt_assert0(MSG_comm_wait(res_irecv,-1) == MSG_OK, "MSG_task_get failed");
64 MSG_task_destroy(task);
67 INFO0("Goodbye now!");
71 /** Receiver function */
72 int receiver(int argc, char *argv[])
77 xbt_dynar_t comms = NULL;
78 int tasks = atof(argv[2]);
79 comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
81 xbt_assert1(sscanf(argv[1],"%d", &id),
82 "Invalid argument %s\n",argv[1]);
83 sprintf(mailbox,"receiver-%d",id);
85 msg_comm_t res_irecv = NULL;
87 m_task_t task_com = NULL;
88 for(i=0; i<tasks ;i++)
90 INFO1("Wait to receive task %d",i);
91 res_irecv = MSG_task_irecv(&task, mailbox);
92 xbt_dynar_push_as(comms, msg_comm_t , res_irecv);
97 /* Here we are waiting for the receiving of all communications*/
102 int num = MSG_comm_waitany(comms);
103 xbt_dynar_remove_at(comms, num, &res_irecv);
104 task_com = MSG_comm_get_task(res_irecv);
105 INFO1("Processing \"%s\"", MSG_task_get_name(task_com) );
106 MSG_task_execute(task_com);
107 INFO1("\"%s\" done", MSG_task_get_name(task_com));
108 MSG_task_destroy(task_com);
110 xbt_dynar_free(&comms);
112 /* Here we tell to sender that all tasks are done*/
113 sprintf(mailbox,"finalize");
114 res_irecv = MSG_task_isend(MSG_task_create(NULL, 0, 0, NULL), mailbox);
115 MSG_comm_wait(res_irecv,-1);
116 INFO0("I'm done. See you!");
118 } /* end_of_receiver */
124 MSG_error_t test_all(const char *platform_file,
125 const char *application_file)
127 MSG_error_t res = MSG_OK;
129 /* MSG_config("workstation/model","KCCFLN05"); */
130 { /* Simulation setting */
131 MSG_set_channel_number(0);
132 MSG_create_environment(platform_file);
134 { /* Application deployment */
135 MSG_function_register("sender", sender);
136 MSG_function_register("receiver", receiver);
137 MSG_launch_application(application_file);
141 INFO1("Simulation time %g",MSG_get_clock());
143 } /* end_of_test_all */
147 int main(int argc, char *argv[])
149 MSG_error_t res = MSG_OK;
151 MSG_global_init(&argc,argv);
153 printf ("Usage: %s platform_file deployment_file\n",argv[0]);
154 printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
157 res = test_all(argv[1],argv[2]);
158 SIMIX_message_sizes_output("toto.txt");