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]);
28 int diff_com = atol(argv[5]);
34 char sprintf_buffer[256];
35 d = xbt_dynar_new(sizeof(msg_comm_t), NULL);
36 msg_comm_t *comm = malloc((number_of_tasks+receivers_count)*sizeof(msg_comm_t));
37 msg_comm_t res_irecv = NULL;
38 for (i = 0; i < (number_of_tasks); i++)
41 if(diff_com == 0) coef = 1;
44 sprintf(mailbox,"receiver-%ld",(i % receivers_count));
45 sprintf(sprintf_buffer, "Task_%d", i);
46 task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size/coef, NULL);
47 comm[i] = MSG_task_isend(task, mailbox);
48 MSG_task_refcount_dec(task);
49 xbt_dynar_push_as(d, msg_comm_t, comm[i]);
50 INFO3("Send to receiver-%ld %s comm_size %f",i % receivers_count,sprintf_buffer,task_comm_size/coef);
52 /* Here we are waiting for the completion of all communications*/
56 xbt_dynar_remove_at(d, MSG_comm_waitany(d), &res_irecv);
61 /* Here we are waiting for the completion of all tasks*/
62 sprintf(mailbox,"finalize");
64 for(i=0; i<receivers_count; i++)
68 res_irecv = MSG_task_irecv(&(task), mailbox);
69 xbt_assert0(MSG_comm_wait(res_irecv,-1) == MSG_OK, "MSG_comm_wait failed");
70 MSG_task_destroy(task);
73 INFO0("Goodbye now!");
77 /** Receiver function */
78 int receiver(int argc, char *argv[])
83 xbt_dynar_t comms = NULL;
84 int tasks = atof(argv[2]);
85 comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
87 xbt_assert1(sscanf(argv[1],"%d", &id),
88 "Invalid argument %s\n",argv[1]);
89 sprintf(mailbox,"receiver-%d",id);
90 MSG_process_sleep (10);
91 msg_comm_t res_irecv = NULL;
93 m_task_t task_com = NULL;
94 for(i=0; i<tasks ;i++)
96 INFO1("Wait to receive task %d",i);
97 res_irecv = MSG_task_irecv(&task, mailbox);
98 xbt_dynar_push_as(comms, msg_comm_t , res_irecv);
103 /* Here we are waiting for the receiving of all communications*/
108 MSG_error_t err = MSG_OK;
109 int num = MSG_comm_waitany(comms);
110 xbt_dynar_remove_at(comms, num, &res_irecv);
111 task_com = MSG_comm_get_task(res_irecv);
112 INFO1("Processing \"%s\"", MSG_task_get_name(task_com) );
113 MSG_task_execute(task_com);
114 INFO1("\"%s\" done", MSG_task_get_name(task_com));
115 err = MSG_task_destroy(task_com);
116 xbt_assert0(err == MSG_OK, "MSG_task_destroy failed");
118 xbt_dynar_free(&comms);
120 /* Here we tell to sender that all tasks are done*/
121 sprintf(mailbox,"finalize");
122 res_irecv = MSG_task_isend(MSG_task_create(NULL, 0, 0, NULL), mailbox);
123 MSG_comm_wait(res_irecv,-1);
124 INFO0("I'm done. See you!");
126 } /* end_of_receiver */
129 MSG_error_t test_all(const char *platform_file,
130 const char *application_file)
132 MSG_error_t res = MSG_OK;
134 /* MSG_config("workstation/model","KCCFLN05"); */
135 { /* Simulation setting */
136 MSG_set_channel_number(0);
137 MSG_create_environment(platform_file);
139 { /* Application deployment */
140 MSG_function_register("sender", sender);
141 MSG_function_register("receiver", receiver);
142 MSG_launch_application(application_file);
146 INFO1("Simulation time %g",MSG_get_clock());
148 } /* end_of_test_all */
152 int main(int argc, char *argv[])
154 MSG_error_t res = MSG_OK;
156 MSG_global_init(&argc,argv);
158 printf ("Usage: %s platform_file deployment_file\n",argv[0]);
159 printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
162 res = test_all(argv[1],argv[2]);
163 SIMIX_message_sizes_output("toto.txt");