Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Choice if we want have different communication size or not.
[simgrid.git] / examples / msg / icomms / peer3.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <stdio.h>
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 */
10 #include <math.h>
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
15
16 int sender(int argc, char *argv[]);
17 int receiver(int argc, char *argv[]);
18
19 MSG_error_t test_all(const char *platform_file, const char *application_file);
20
21 /** Sender function  */
22 int sender(int argc, char *argv[])
23 {
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]);
29   double coef= 0;
30   xbt_dynar_t d = NULL;
31   int i;
32   m_task_t task = NULL;
33   char mailbox[256];
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++)
39   {
40         task = NULL;
41         if(diff_com == 0) coef = 1;
42         else coef = (i+1);
43
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);
51   }
52   /* Here we are waiting for the completion of all communications*/
53
54   while(d->used)
55   {
56     xbt_dynar_remove_at(d, MSG_comm_waitany(d), &res_irecv);
57   }
58   xbt_dynar_free(&d);
59
60
61   /* Here we are waiting for the completion of all tasks*/
62   sprintf(mailbox,"finalize");
63
64   for(i=0; i<receivers_count; i++)
65   {
66           task = NULL;
67           res_irecv = NULL;
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);
71   }
72
73   INFO0("Goodbye now!");
74   return 0;
75 } /* end_of_sender */
76
77 /** Receiver function  */
78 int receiver(int argc, char *argv[])
79 {
80   int id = -1;
81   int i;
82   char mailbox[80];
83   xbt_dynar_t comms = NULL;
84   int tasks = atof(argv[2]);
85   comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
86
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;
92   m_task_t task = NULL;
93   m_task_t task_com = NULL;
94   for(i=0; i<tasks ;i++)
95   {
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);
99         task = NULL;
100         res_irecv = NULL;
101   }
102
103   /* Here we are waiting for the receiving of all communications*/
104   while(comms->used)
105   {
106         task_com = NULL;
107         res_irecv = NULL;
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");
117   }
118   xbt_dynar_free(&comms);
119
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!");
125   return 0;
126 } /* end_of_receiver */
127
128 /** Test function */
129 MSG_error_t test_all(const char *platform_file,
130                             const char *application_file)
131 {
132   MSG_error_t res = MSG_OK;
133
134   /* MSG_config("workstation/model","KCCFLN05"); */
135   {                             /*  Simulation setting */
136     MSG_set_channel_number(0);
137     MSG_create_environment(platform_file);
138   }
139   {                            /*   Application deployment */
140     MSG_function_register("sender", sender);
141     MSG_function_register("receiver", receiver);
142     MSG_launch_application(application_file);
143   }
144   res = MSG_main();
145   
146   INFO1("Simulation time %g",MSG_get_clock());
147   return res;
148 } /* end_of_test_all */
149
150
151 /** Main function */
152 int main(int argc, char *argv[])
153 {
154   MSG_error_t res = MSG_OK;
155
156   MSG_global_init(&argc,argv);
157   if (argc < 3) {
158      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
159      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
160      exit(1);
161   }
162   res = test_all(argv[1],argv[2]);
163    SIMIX_message_sizes_output("toto.txt");
164   MSG_clean();
165
166   if(res==MSG_OK)
167     return 0;
168   else
169     return 1;
170 } /* end_of_main */