Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add Waitany for icomms to cmake tests.
[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   xbt_dynar_t d = NULL;
29   int i;
30   m_task_t task = NULL;
31   char mailbox[256];
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++)
37   {
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);
45   }
46   /* Here we are waiting for the completion of all communications*/
47
48   while(d->used)
49   {
50     xbt_dynar_remove_at(d, MSG_comm_waitany(d), &res_irecv);
51   }
52   xbt_dynar_free(&d);
53
54
55   /* Here we are waiting for the completion of all tasks*/
56   sprintf(mailbox,"finalize");
57
58   for(i=0; i<receivers_count; i++)
59   {
60           task = NULL;
61           res_irecv = NULL;
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);
65   }
66
67   INFO0("Goodbye now!");
68   return 0;
69 } /* end_of_sender */
70
71 /** Receiver function  */
72 int receiver(int argc, char *argv[])
73 {
74   int id = -1;
75   int i;
76   char mailbox[80];
77   xbt_dynar_t comms = NULL;
78   int tasks = atof(argv[2]);
79   comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
80
81   xbt_assert1(sscanf(argv[1],"%d", &id),
82          "Invalid argument %s\n",argv[1]);
83   sprintf(mailbox,"receiver-%d",id);
84
85   msg_comm_t res_irecv = NULL;
86   m_task_t task = NULL;
87   m_task_t task_com = NULL;
88   for(i=0; i<tasks ;i++)
89   {
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);
93         task = NULL;
94         res_irecv = NULL;
95   }
96
97   /* Here we are waiting for the receiving of all communications*/
98   while(comms->used)
99   {
100         task_com = NULL;
101         res_irecv = NULL;
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);
109   }
110   xbt_dynar_free(&comms);
111
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!");
117   return 0;
118 } /* end_of_receiver */
119
120
121
122
123 /** Test function */
124 MSG_error_t test_all(const char *platform_file,
125                             const char *application_file)
126 {
127   MSG_error_t res = MSG_OK;
128
129   /* MSG_config("workstation/model","KCCFLN05"); */
130   {                             /*  Simulation setting */
131     MSG_set_channel_number(0);
132     MSG_create_environment(platform_file);
133   }
134   {                            /*   Application deployment */
135     MSG_function_register("sender", sender);
136     MSG_function_register("receiver", receiver);
137     MSG_launch_application(application_file);
138   }
139   res = MSG_main();
140   
141   INFO1("Simulation time %g",MSG_get_clock());
142   return res;
143 } /* end_of_test_all */
144
145
146 /** Main function */
147 int main(int argc, char *argv[])
148 {
149   MSG_error_t res = MSG_OK;
150
151   MSG_global_init(&argc,argv);
152   if (argc < 3) {
153      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
154      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
155      exit(1);
156   }
157   res = test_all(argv[1],argv[2]);
158    SIMIX_message_sizes_output("toto.txt");
159   MSG_clean();
160
161   if(res==MSG_OK)
162     return 0;
163   else
164     return 1;
165 } /* end_of_main */